Calling Block lib.php Function from Cron class - Moodle 3.9.2+

Calling Block lib.php Function from Cron class - Moodle 3.9.2+

by Dave Emsley -
Number of replies: 5

Hi All,

This might be obvious but I must be missing something.

I have a block lib.php file and I want to call one of the functions from the cron_task execute() function.

Is this doable?  If so how?


Cheers

Dave

Average of ratings: -
In reply to Dave Emsley

Re: Calling Block lib.php Function from Cron class - Moodle 3.9.2+

by Benjamin Ellis -
Picture of Particularly helpful Moodlers
Hi,

Use a locallib.php file and call the functionality from both your lib.php and your scheduled task script.

Regards
In reply to Benjamin Ellis

Re: Calling Block lib.php Function from Cron class - Moodle 3.9.2+

by Dave Emsley -
Hi Ben, thanks for the reply but I'm not clear how that would be any different. Can you explain please? Or point me at an example.

Cheers

Dave
In reply to Dave Emsley

Re: Calling Block lib.php Function from Cron class - Moodle 3.9.2+

by Davo Smith -
Picture of Core developers Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of Plugin developers
If I'm understanding you correctly, what you want is something like this:

blocks/myblock/lib.php:
function block_myblock_somefunction() {
...
}

blocks/myblock/classes/task/mytask.php:
namespace block_myblock\task;
class mytask extends \core\task\schedlued_task {
   public function execute() {
        global $CFG;
        require_once($CFG->dirroot.'/blocks/myblock/lib.php');
        block_myblock_somefunction();
   }
...
}
But, you really want to avoid doing this, if at all possible.

These days, you are much better off sticking the function you want in some sort of helper class inside block/myblock/classes - then you don't need to remember to use require_once() to load the code, before you use it (you certainly don't want to be using locallib.php these days, it has none of the advantages of automatic class loading).

e.g.

blocks/myblock/classes/helper.php:

namespace block_myblock;
class helper {
   public static myfunction() {
    ...
    }
}

blocks/myblock/classes/task/mytask.php:

namespace block_myblock\task;
class mytask extends \core\task\schedlued_task {
   public function execute() {
        \block_myblock\helper::myfunction();
   }
...
}
(note you may need to purge caches or bump your plugin version number for Moodle to spot this existence of your new class)
Average of ratings: Useful (2)
In reply to Davo Smith

Re: Calling Block lib.php Function from Cron class - Moodle 3.9.2+

by Dave Emsley -
Hi Davo,

Many thanks for that - all that makes sense for OOP functions but in the non-OOP code can I still call the helper functions?

For example:

blocks/myblock/lib.php:
function block_myblock_somefunction() {
$somearray = \block_myblock\helper::myfunction();
}

EDIT - Actually quicker to try it myself of course and YES it does work.

Cheers
Dave

In reply to Dave Emsley

Re: Calling Block lib.php Function from Cron class - Moodle 3.9.2+

by Benjamin Ellis -
Picture of Particularly helpful Moodlers
Hi Dave,

Davo's response is a much better solution though it requires more thought and coding??? - my suggestion was the 'quick' solution - not always the best way of doing things ...

Ben
Average of ratings: Useful (1)