官术网_书友最值得收藏!

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.

Adding scheduled actions to our block

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.

主站蜘蛛池模板: 乌兰察布市| 藁城市| 桃园市| 平乡县| 板桥市| 中阳县| 澎湖县| 凤凰县| 乳源| 抚松县| 宁海县| 吉隆县| 黄龙县| 昌吉市| 叙永县| 新郑市| 芦溪县| 天峨县| 西安市| 开远市| 卫辉市| 松原市| 上高县| 岳普湖县| 衡山县| 弥渡县| 蓬莱市| 本溪| 泰兴市| 琼结县| 石景山区| 休宁县| 会理县| 阳城县| 绥化市| 安庆市| 鹤峰县| 正定县| 景泰县| 高平市| 长汀县|