- Moodle 1.9 Extension Development
- Jonathan Moore Michael Churchward
- 334字
- 2021-08-06 17:24:06
Adding scheduled actions to our block
Moodle's cron.php
is an administrative process that is run on a scheduled basis to perform maintenance tasks such as e-mail delivery and backups. It is typically run once in every 5 to 15 minutes on most sites. Occasionally, while working with blocks or other plugins, it will be necessary to add some of your own processing to cron.php
. This is a convenient way of making sure that your module can run background processes without giving the end user a series of complex instructions for a separate cron entry.
To add cron support to our block we just have to set the cron interval in the init()
function with the following line of code, and define a cron()
function in our block class:
$this->cron = 5;
The value assigned to $this->cron
is the number of seconds between cron runs. It is important to note that no matter what this is set to, the module cron code will only run as frequently as the main site is set to run.
To complete our functionality, we add a cron()
function. We are just going to print a line of output to confirm that everything is working as expected, as seen in the following code. As we continue to gain new Moodle programming skills, we will expand on this functionality to carry out more useful functions:
function cron(){ print("Hello World is running its cron process.\n"); }//function cron
We can test our function by manually running cron by loading http://my_moodle_domain/admin/cron.php into our web browser.
Let's review the output of cron.php
. Note the section "Starting blocks", where we see the output of the print command that we added to the block's cron()
function.

Although this output isn't particularly useful, it does demonstrate how to connect our module with the cron system in order to perform arbitrary actions on a programmed interval. To see a real world example of this concept in action, see the grade categorization block in the Moodle plugin database at: http://moodle.org/mod/data/view.php?d=13&rid=2068.
- ColdFusion 9 Developer Tutorial
- Lightroom Classic完全自學一本通
- SolidWorks 2008機械設計一冊通
- Android從入門到精通
- 綁定的藝術:Maya高級角色骨骼綁定技法(第2版)
- AutoCAD 2024建筑設計從入門到精通(升級版)
- Building Websites with PHP/Nuke
- Moodle Course Conversion: Beginner's Guide
- iPad Procreate風格繪畫之美
- 中文版Maya 2014基礎培訓教程
- Photoshop后期強:多重曝光專業技法寶典
- 中文版AutoCAD自學經典
- Joomla! 1.5 JavaScript jQuery
- Cinema 4D R20完全學習手冊
- 玩轉掌上生活 手機網上生活達人秘笈