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

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.

主站蜘蛛池模板: 沛县| 昂仁县| 桦南县| 岳池县| 孟津县| 乌兰察布市| 新丰县| 河源市| 横峰县| 万宁市| 新疆| 宜都市| 莱阳市| 南乐县| 宜都市| 英超| 平原县| 乌拉特中旗| 乡宁县| 沙洋县| 盘山县| 尉氏县| 夏邑县| 泗洪县| 姚安县| 安吉县| 柞水县| 内黄县| 隆子县| 灌云县| 清镇市| 新民市| 涟水县| 萝北县| 乾安县| SHOW| 克什克腾旗| 淮安市| 奎屯市| 张北县| 都匀市|