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

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
)
),
主站蜘蛛池模板: 汝城县| 天峻县| 偃师市| 积石山| 沙田区| 抚州市| 阿坝县| 延川县| 桑日县| 当雄县| 中方县| 濉溪县| 萝北县| 诏安县| 赤峰市| 扎赉特旗| 阳谷县| 环江| 兴安县| 丘北县| 无锡市| 西吉县| 安仁县| 彰化市| 正阳县| 双柏县| 博客| 湛江市| 且末县| 如东县| 遂溪县| 桐梓县| 晋中市| 苍山县| 建湖县| 长兴县| 永安市| 东安县| 临洮县| 涞水县| 湖北省|