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

Adding instance configuration

We will now add some real functionality to our block by adding the ability to configure what the block will display. This is done by adding another function to the block class. Moodle makes it very simple to add this functionality. With just a few lines of code we will add a full WYSIWYG editing capability for entering the text to be displayed by our block. When completed, our simple Hello World block will have most of the functionality of the built-in HTML block, plus it will have its own capability to control access to who sees the resulting information.

To begin, let's tell Moodle that our block allows each instance to be configured. We do this by adding the instance_allow_config function, and having it return true:

function instance_allow_config() {
return true;
}//function

Note that all Moodle modules also support Global Configuration, which is covered inChapter 3, Creating and Modifying Filters, on Filter Modules.

Capturing your configuration content

Now we make a config_instance.html file. Add the following text to this file. The first line references and prints a new text string that we will have to add to our language file for the block. This illustrates how simple it is to reference an arbitrary new string in our code. It is important that any code that we create uses this string rather than hardcoding the text in the code. This is necessary for our code to function properly for localization to other languages. The next line prints a textarea input box with the contents, if any, of this instance of the block. The last line displays Moodle's integrated WYSIWYG editor, which is based on the HTMLArea JavaScript editor (this editor is scheduled to be replaced in Moodle 2.0, in order to provide better browser support):

<?php print_string('configcontent', 'block_helloworld'); ?>
<?php print_textarea(true, 10, 50, 0, 0, 'text', $this->config->text); ?>
<input type="submit" value="<?php print_string('savechanges') ?>" />
<?php use_html_editor(); ?>

Note that this is not a complete HTML form; Moodle pulls in this partial form information and turns it into a complete and functional form for us.

Displaying your content

Finally, we have to edit one line in our get_content function. Our original text assignment was:

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

Let's change it to the following new line, which will grab the text entered into the configuration editor and display it in the block:

$this->content->text = $this->config->text;

In the following screenshot, we see how this new configuration provides not just the ability to enter arbitrary text, but also to format that text by using Moodle's WYSIWYG editor:

Displaying your content

The end result of these new changes can be seen in the following screenshot. Notice the edit icon next to the block display, which was added as a result of adding the instance_allow_config() function. This version of the block is found in the helloworld_final source code folder. While we are still using roughly the same text, we have made it larger, and have formatted it to be bold and italic:

Displaying your content

We now have a fully-functional and usable block. At this point, the title Hello World may be the wrong name for our block. With the latest changes it is actually very similar to the core HTML block, but with the addition of its own dedicated capability to control access. Modifying the HTML block to add new capability controls is actually a common project. The HTML block is very powerful because it's the Swiss army knife of blocks. It's not always the most elegant solution to a problem, but it's often possible to put together some HTML in this block in order to quickly and easily make changes to the Moodle interface. Adding a capability control that allows the block to be visible or invisible to certain groups of users adds just enough flexibility to meet a whole new class of common use cases. We can take this basic template and change the context for the capability to work in different situations. This is especially useful if you need to toggle the visibility of a course sticky block, which doesn't currently work with role overrides. This also illustrates how Moodle's API allows us to add a lot of functionality with a small amount of code.

主站蜘蛛池模板: 横山县| 潢川县| 牡丹江市| 平邑县| 灵武市| 循化| 万全县| 杭州市| 四平市| 汽车| 虞城县| 葵青区| 邵东县| 临颍县| 大冶市| 洪湖市| 常山县| 英吉沙县| 东乌珠穆沁旗| 桃江县| 沾化县| 宁阳县| 青川县| 宣恩县| 额敏县| 绵阳市| 曲周县| 江北区| 东丽区| 绥滨县| 抚顺县| 巢湖市| 黄大仙区| 都兰县| 马边| 夏邑县| 永兴县| 成武县| 博罗县| 青州市| 广宗县|