- 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.
- 擁抱開源(第2版)
- Excel 2013使用詳解(修訂版)
- VMware虛擬化與云計算:vSphere運維卷
- OpenStack實戰指南
- Software Testing using Visual Studio 2010
- Moldflow 2010完全自學與速查手冊(模流分析·成本控制)
- Inkscape 0.48 Illustrator's Cookbook
- After Effects CC 2019 影視后期特效合成案例教程
- MATLAB 2022a從入門到精通
- PHP 5 Social Networking
- Liferay Portal Systems Development
- SharePoint Designer Tutorial: Working with SharePoint Websites
- Creo快速入門教程(Creo 8.0中文版)
- 中文版3ds Max 2012基礎培訓教程(第2版)
- Moodle 1.9 Math