Schedule database update

Schedule database update

Ulrik Sandgren -
Vastausten määrä: 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?


Arviointien keskiarvo: -
Vastaus Ulrik Sandgren

Re: Schedule database update

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

Re: Schedule database update

Dan Marsden -
Kuva: Core developers Kuva: Particularly helpful Moodlers Kuva: Peer reviewers Kuva: Plugin developers Kuva: Plugins guardians Kuva: Testers Kuva: 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)