3、添加语言文件<?xml:namespace prefix = o ns = “urn:schemas-microsoft-com: office: office” /> 添加语言文件(Add language file)。在“helloworld”目录下创建一个“lang”目录,然后在“lang”下创建一个“en_utf8”,它表示这是一个language folder for Englist with Unicode encoding。如果你希望创建中文语言文件则在“lang”下创建一个“zh_cn_utf8”。 然后在”en_utf8”里创建一个与block page同名的php file:“block_helloworld.php”,然后把所有要用到的message都放到该文件里。其格式是把这些messages都存储在”$string”数组变量里。 下面是一个英文block_helloworld.php的例子: <?PHP $string[‘helloworld’] = ‘Hello World’; $string[‘helloworld:view’] = ‘View Hello World Block’; $string[‘blockname’] = ‘HelloWorld’; ?> 中文block_helloworld.php放在“zh_cn_utf8”里,内容入下: <?PHP $string[‘helloworld’] = ‘你好,世界’; $string[‘helloworld:view’] = ‘显示你好世界版块’; $string[‘blockname’] = ‘你好,世界’; ?> 回顾一下步骤2的init函数里用到一个函数 get_string(‘helloworld’, ‘block_helloworld’); 该函数返回本地化的字符串。 第一个参数对应的是language file里的message key,例如上例的第一个参数就是对应language file里的“helloworld”的message。 第二个参数是指定来自哪一个module的language file,例如上例指定的是helloworld block。该参数值实际就是存储该message的php file name (不要.php 扩展名)。例如:我们的language php file是block_helloworld.php,所以参数值为” block_helloworld”。 另外还有一个函数与get_string类似,就是print_string,它是把返回的string 输出。 4、设置capability权限许可 见《Moodle插件开发笔记5:基础知识五》。 在“helloworld”文件夹下创建一个db目录,然后在db目录下创建一个access.php,该文件用来定义capability。我们在helloworld block里定义一个block/helloworld:view capability,该capability的type是read,该capability是属于system context level里,并设置只有admin role user拥有该capability,其他role没有。 代码如下: <?php $block_helloworld_capabilities = array( ‘block/helloworld:view’ => array( ‘captype’ => ‘read’, ‘contextlevel’ => CONTEXT_SYSTEM, ‘legacy’ => array( ‘guest’ => CAP_PREVENT, ‘student’ => CAP_PREVENT, ‘teacher’ => CAP_PREVENT, ‘editingteacher’ => CAP_PREVENT, ‘coursecreator’ => CAP_PREVENT, ‘admin’ => CAP_ALLOW ) ) ); ?> 5、添加配置界面 添加helloworld block的配置界面(configuration interface),使得在edit mode,在block区域里多一个按钮链接到配置界面。 例子:使helloworld block具有configuration功能,使得可以设置$this->content的值。 1)需要在“block_helloworld.php”里的block_helloworld class里添加一个return true的instance_allow_config函数,代码如下: function instance_allow_config() { return true; } 2)在block root目录下创建一个config_instance.html文件。该文件的代码实际上是php代码: <?php print_string( ‘configcontent’ ,’block_helloword’ ) ;?> : <?php print_textarea( true ,10 ,50 ,0 ,0 ,’text’ ,$this -> config -> text ) ;?> < input type= “submit” value= “<?php print_string(‘savechanges’) ?>”/> <?php use_html_editor() ;?> 第一行是输出一个’configcontent’标题,在语言文件中定义本地语言。 第二行是输出一个text area (name为text),其值为$this->config->text (注意:text area的name的值必须与$this->context里的对应的key name相同),在submit之后text area的值就会自动赋值给对应的$this->context里对应的变量。例如,该例的text area的name为text,submit之后该值就会赋给$this->context->text。 第四行是嵌入Moodle’s integrated WYSIWYG editor。 上面的代码就是一个简单的配置页面。你会奇怪怎么没看到 <form> ?对的,不需要写 <form> ,只需要提供你需要 config 的东东的 input ,以及一个 submit button 即可。当你 click submit button 时, moodle 会自动波帮你存储 config setting 到 $this->config 里。例如上例 name 为“ text ”的 input field value 就会存储在 $this->config->text 里 。 this->config 变量可以供 block_helloword 类里除了 init() 方法之外的任何地方调用! 3)修改block_helloworld class的get_content()函数,使得输出的content来自configuration interface设置的值。 $this->content->text = $this->config->text; 未完,待续…… |
Moodle开发笔记7-Block开发(二)
3、添加语言文件<?xml:namespace prefix = o ns = “urn:schemas-microsoft-com: office: offic…