Cron Tasks not class based - Moodle 3.6+

Cron Tasks not class based - Moodle 3.6+

by Dave Emsley -
Number of replies: 4

I want to run a cron task that doesn't use OOP as I need access to functions within:

require_once($CFG->libdir.'/grouplib.php');
require_once($CFG->libdir.'/gradelib.php');

In my cron_task class can I execute a "normal" php function from my 

    public function execute() {
        //code to run a non-OOP function goes here;
        return true;       
    }

I hope this makes sense,

Best Regards

Dave




Average of ratings: -
In reply to Dave Emsley

Re: Cron Tasks not class based - Moodle 3.6+

by Davo Smith -
Picture of Core developers Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of Plugin developers

Yes.

For a fuller answer, as long as you require_once() and library files that are needed, you can call any functions that you like that are defined in Moodle (or any standard PHP functions).

Average of ratings: Useful (1)
In reply to Davo Smith

Re: Cron Tasks not class based - Moodle 3.6+

by Dave Emsley -
Thanks for that Davo; makes sense.

However I'm getting an error when trying to get the user context:

$usercontext = context_user::instance($user->id);

gives the error:

Default exception handler: Exception - Class 'block_ccc\task\context_user' not found (block_ccc is my block's name.


Cheers

Dave
In reply to Dave Emsley

Re: Cron Tasks not class based - Moodle 3.6+

by Andreas Grabs -
Picture of Core developers Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of Plugin developers Picture of Translators

Hi Dave,

you are working in a class in your own namespace. The moodle context class is defined in the global namespace so you have to prefix it with a backslash "\":

$usercontext = \context_user::instance($user->id);

Best regards
Andreas

Average of ratings: Useful (1)