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

Using a block as a code container

Sometimes we will want to make additions to Moodle that don't necessarily have a user interface, or at least a block interface. In these situations we can still benefit from creating a block. Firstly, the block is one of the simplest plugins that we can make for Moodle. Secondly, blocks have a simple, well-understood installation procedure. Moodle also provides many facilities for plugins, such as configuration and database storage. And finally, it gives us a simple and standardized method for dealing with version upgrades.

Let's take a look at the repository block, which is part of a suite of additions to Moodle that we call the Enterprise Learning Intelligence Suite (ELIS). The repository block is never used to display content. It simply acts as a storage container for capabilities associated with the Alfresco integration. The Alfresco integration itself is a separate set of functions contained in its own directory. Alfresco is a popular and powerful open source document management system written in Java.

Creating a block stub for our container

Let's take a look at some code. In this first section of code we are looking at the block's main file, block_repository.php. This file uses the PHPDoc format for commenting code, which can be used with tools like PHPXref to automatically generate documentation. In our initial function, we again define the minimum values for a block: title and version:

<?php // $Id: block_repository.php,v 1.2 2009/04/22 15:14:08 jfilip Exp $
/**
* Class for the repository control block.
*
* @version $Id: block_repository.php,v 1.2 2009/04/22 15:14:08 jfilip Exp $
* @author Open Knowledge Technologies - http://www.oktech.ca/
* @author Remote Learner - http://www.remote-learner.net/
* @author Justin Filip <jfilip@oktech.ca>
*/
class block_repository extends block_base {
function block_repository() {
$this->title = get_string('blockname', 'block_repository');
$this->version = 2009042100;
} //function block_repository

In the final section of our main file, we make sure that our block content is set to blank values so that the block will never display on screen:

function get_content() {
if($this->content !== NULL) {
return $this->content;
}
$this->content = new stdClass;
$this->content->text = ''; $this->content->footer = '';
return $this->content;
}

Creating capabilities for our container

As we can see, there is not much happening in the main block file. This is evidence of the fact that we are just using the block structure as a container. Next, let's take a look at the functional component of the block, which is defined in the db folder. We have a single file defined called access.php. This functions just as it does in our Hello World block. We simply define an array of access capabilities. Note that in the case of this block, we are using the riskbitmask value in the array. This provides the administrator with information about the risk associated with giving this capability to a particular role:

$block_repository_capabilities = array(
'block/repository:createsitecontent' => array(
'riskbitmask' => RISK_XSS| RISK_DATALOSS,
'captype' => 'write',
'contextlevel' => CONTEXT_SYSTEM,
'legacy' => array(
'admin' => CAP_ALLOW
)
),
主站蜘蛛池模板: 加查县| 两当县| 海晏县| 平度市| 石渠县| 孟津县| 阿拉善盟| 珠海市| 天气| 洛扎县| 定南县| 元氏县| 苍山县| 台东县| 泰顺县| 米脂县| 南和县| 无锡市| 安宁市| 长武县| 庆阳市| 晋江市| 垫江县| 伽师县| 大同县| 安徽省| 普兰店市| 庆安县| 宁乡县| 宁明县| 佛山市| 蓬安县| 保靖县| 洪雅县| 辽宁省| 揭东县| 赣州市| 日土县| 阜南县| 深州市| 澄江县|