Best approach to write a plugin that customizes behavior of the policy tool?

Best approach to write a plugin that customizes behavior of the policy tool?

Jordan Kintzle གིས-
Number of replies: 1

Hi all, I'm hoping someone can help me understand a proper way to implement custom behavior to the policy tool in Moodle 4.4+. I've managed to get the behavior I want by modifying the original script, but now I need to figure out how to implement similar behavior using a plugin.

I successfully built an activity module plugin as well as a simple local plugin that adds new functionality to our site, but this is my first attempt at customizing core code. 

My Objective:

Simplify the policy consent user flow during user registration when using tool_policy as the Site policy handler in a Moodle 4.4 and 4.5 instance.

The Undesired Behavior:

Currently the new user registration flow requires the user to view each policy page before reaching the Consent page where they actually click the checkboxes and provide their consent. Viewing each policies and clicking next to reach the Consent page is not desired.

About the Solution:

The Consent page offers the ability to view a policy document should the user want to read it. So my current solution changes the flow for a new user so that after clicking the Create new account button, they land directly on the Consent page bypassing the individual view of each policy.

Note: All of our policies are currently mandatory and do not require their own consent page.

Current Implementation:

I was able to create the desired behavior by directly modifying the logic in the `redirect_to_policies()` function in the script  admin/tool/policy/classes/output/page_agreedocs.php. This is still a half baked solution but it proved we could get the desired functionality. I planned to make a quality solution as I redevelop the behavior using a custom plugin.

Here's a diffchecker link showing my changes to the page_agreedocs.php script. Hopefully that's enough context to understand what I'm getting after. Please note that my development Moodle instance is running 4.5 so the diff I linked to is from the 405 stable branch.

Alternative Solutions:

There are a variety of ways to improve and simplify the policy consent for user flow. If anyone knows of an existing solution/plugin that can provide similar behavior, let me know as I'm not fixed on this particular solution.

I also considered building a custom signup page that requires the user to consent to the policies in order to create their account, but haven't gone down that road yet.

Where I'm Stuck:

I'm trying to write a local plugin that replicates this behavior and I think I just don't understand what I'm doing. I believe my initial approach is trying to override the class however I'm finding that the plugin is not doing anything. I added logs all over my code and I don't see a single entry from the plugin in my log.

I have worked on two other plugins recently: an activity module plugin and a local plugin. The local plugin was quite simple and added functionality to the site to load Google Tag Manager and set the user id in the data layer for user tracking in Analytics. This is my first attempt at customizing behavior of Moodle core functionality. I've read a ton of documentation and searched the forums but haven't found the right guidance yet, but at this point I don't think I'm taking the right approach for this plugin.

How to customize core functionality with a plugin?

I've read about using class overrides, extending classes, or using event observers to modify/extend functionality without directly overriding core classes. I've also read that hooks and callbacks are additional ways to modify behavior, but I need help understanding the best approach for my objective.

  • Can anyone provide some insight on how to go after this plugin following Moodle's best practices?
  • Can anyone point me to an existing plugin that can be used as a reference or template to help me implement my custom behavior?

Thank you in advance!

དཔྱ་སྙོམས་ཀྱི་སྐུགས་ཚུ།: -
In reply to Jordan Kintzle

Re: Best approach to write a plugin that customizes behavior of the policy tool?

Davo Smith གིས-
Core developers གི་པར Particularly helpful Moodlers གི་པར Peer reviewers གི་པར Plugin developers གི་པར

The simple answer is that Moodle does not provide a generic mechanism to arbitrarily override existing code in core.

There are lots of hooks / callbacks in various places, that allow you to hook in to specific areas. It is also possible to use themes to override renderers or templates (because Moodle core provides specific mechanisms for declaring those overrides). If you're not overriding a renderer (in a theme) and there isn't a specific hook / callback in the area you are wanting to override, then your best option is to open a Moodle tracker ticket and request a new hook to be added in a future major release of Moodle.

Extending an existing class will not do anything, as there is no way for the Moodle core code to know it should use your new class, instead of the original one it knows about (and, in addition, if there was such a mechanism, there would be no way for it to know which class to use, in the case that 2 different plugins both overrode the same class).