Moodle开发笔记2-Block开发

本文详细介绍了如何在Moodle中开发一个基础的Block插件,名为'helloworld'。Block是Moodle页面左右列显示的模块,通过创建'helloworld'文件夹,编写'block_helloworld.php',定义继承自'block_base'的类,并实现'init'和'get_content'两个关键函数,完成Block的基本结构。'init'函数设置Block的标题和版本,'get_content'返回显示的内容。此外,文章还提及了语言文件的添加、配置界面的创建、任务调度(cron)等功能的可选实现方法。
摘要由CSDN通过智能技术生成

Block 是以一个长方形区域出现在 moodle site page 的左列或右列。 Block 是最简单、也是最常用的 moodle plugin

 

下面讲解如何开发一个 hello world moodle block “helloworld”

1. create “helloworld” folder ( 目录名来自你的 module name) in ”moodle/blocks” folder

 

2. ”helloworld” 目录下创建 block page “block_helloworld.php” ( 命名格式是 [ module-type]_[module-name].php )

 

Block php file 要创建一个同名的 class ,该 class extends “block_base” class 。同时需要有 2 个基本的函数: ”init” and “ get_content”

<?php

class block_helloworld extends block_base {

function init() {

$this->title = get_string('helloworld', 'block_helloworld');

$this->version = 2009050700;

}

function get_content() {

if ($this->content !== NULL) {

return $this->content;

}

$this->content = new stdClass;

$this->content->text = 'Hello World!';

return $this->content;

}

}

?>

 

Init 函数至少要设置 2 个变量: block title and block version

 

block title 将会显示在 block 区域的最上端。

block version 注意:你每次 upload 你的 plugin to moodle system ,即将上传的 version 必须大于当前存在于 moodle plugin version version 的格式为: YYYYMMDD## 。最后 2 位是 YYYYMMDD 当天的第 n 个版本。例如 2010062903 ,表示 2010 6 29 日第 3 update version

 

get_content 函数所返回值就会作为 block page 显示的 content 。同时它会存储在 $this->content 变量里。

 

以上 2 步就完成了最简单的 helloworld block

如何激活?

admin account login ,然后 click “ Notifications ” in “ Site Administration ” block moodle 就会即时 check and install new plugin

 

安装好之后如何使用?

admin account login ,然后在你要添加 helloworld page 里启动 edit mode ,然后 in “Blocks ” pull down list select “helloworld” block 即可

 

 

上面的 2 步是必须的,下面的步骤是可选的

 

3. (Optional) Add language file (like java message properties file) 。在 ”helloworld” 目录下创建一个 lang 目录,然后在 ”lang” 下创建一个 ”en_utf8 ,它表示这是一个 language folder for Englist with Unicode encoding 。如果你希望有一个 for US 方言的 englist language pack ,那么就在 ”en_utf8” 目录下创建一个 en_us_utf8 目录。 Moodle里, child language会继承 parent language的所有 string message

 

然后在 ”en_utf8 创建一个与 block page 同名的 php file “block_helloworld.php” ,然后把所有要用到的 message 都放到该文件里。其格式是把这些 messages 存储在 ”$string” 数组变量里

 

下面一个 language php file 的例子:

<?PHP

$string[ 'helloworld'] = 'Hello World';

$string['helloworld:view'] = 'View Hello World Block';

$string['blockname'] = 'HelloWorld';

?>

 

回顾一下步骤 2 init 函数里用到一个函数

            get_string('helloworld', 'block_helloworld');

该函数 Returns a localized string

 

第一个参数对应的是 language file 里的 message key ,例如上例的第一个参数就是对应 language file 里的 ”helloworld” message

 

第二个参数是指定来自哪一个 module language file ,例如上例指定的是 helloworld block 。该参数值实际就是存储该 message php file name ( 不要 .php extension) 。例如:我们的 language php file block_helloworld.php ,所以参数值为 ” block_helloworld”

 

另外还有一个函数与 get_string 类似,就是 print_string ,它是把返回的 string 输出。

 

4. (Optional but very mportant) Working with capabilities capabilities 见上面的章节。

 

5. (Optional but very mportant) 添加 helloworld block configuration interface ,使得在 edit mode ,在 block 区域里多一个 button link to configuration interface

 

例子:使 helloworld block 具有 configuration 功能,使得可以设置 $this->content 的值

1 )需要在 block main page ” block_helloworld.php block_helloworld class里添加一个 return true instance_allow_config 函数,代码如下:

function instance_allow_config() {

return true;

}

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值