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
Calling Block lib.php Function from Cron class - Moodle 3.9.2+
Number of replies: 5Re: Calling Block lib.php Function from Cron class - Moodle 3.9.2+
Hi,
Use a locallib.php file and call the functionality from both your lib.php and your scheduled task script.
Regards
Use a locallib.php file and call the functionality from both your lib.php and your scheduled task script.
Regards
Re: Calling Block lib.php Function from Cron class - Moodle 3.9.2+
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
Cheers
Dave
Re: Calling Block lib.php Function from Cron class - Moodle 3.9.2+
If I'm understanding you correctly, what you want is something like this:
blocks/myblock/lib.php:
blocks/myblock/classes/task/mytask.php:
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)
Re: Calling Block lib.php Function from Cron class - Moodle 3.9.2+
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:
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
Re: Calling Block lib.php Function from Cron class - Moodle 3.9.2+
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
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