Schedule database update

Schedule database update

by Ulrik Sandgren -
Number of replies: 2

Whats the best way to get a scheduled job (once a day) that makes a update in the database? My first idea was to use "Ad-hoc database queries" but it was not possible because db writing is not allowed.

The use case is that we would add a role (insert a new post in mdl_role_assignments) for users that fullfils some preconditions and have this done every night.

Do I need to use the Task API and create a new class for this?


Average of ratings: -
In reply to Ulrik Sandgren

Re: Schedule database update

by Tim Hunt -
Picture of Core developers Picture of Documentation writers Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of Plugin developers
Using the Task API would be the normal way to do this sort of thing.
In reply to Ulrik Sandgren

Re: Schedule database update

by Dan Marsden -
Picture of Core developers Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of Plugin developers Picture of Plugins guardians Picture of Testers Picture of Translators

you shouldn't insert directly to the mdl_role_assignments and should really be using the standard API functions to do this. Ideally you should be calling "role_assign" from accesslib.php or the webservice core_role_assign_roles to do this.

If the pre-conditions involve moodle events, eg "course completion" etc - you might even be able to use the tool_trigger plugin to listen to course_completion events, and then based on the data you receive you could make a webservice call to the core_role_assign_roles function. It might take a bit to understand how the tool_trigger plugin works and if this will suit your requirements..

alternatively you could just create a plugin that implements the Moodle task api, and triggers this for you (like Tim says)