Filters of moodle plug-in)

Source: Internet
Author: User

Filters of moodle plug-in)

A filter is a method that automatically converts content before output.

Objective: To create a filter named helloworld, replace the pre-output "world" string with "Hello World", and then upgrade it to provide settings for replacing strings and new strings, the filter must be replaced according to the settings.

Basic directory structure:

 

Take the Filter Name (helloworld) as the folder, under the/filter directory, that is, the Directory of this plug-in is/filter/helloworld, and all the files/directories of the filter plug-in are in it, that is,/filter/helloworld /*.

The following file/directory descriptions are all under the plug-in directory, that is, under the/filter/helloworld/directory.

Steps:

1. Create a basic Filter

1) create a file called "filter. php ".

2) define a class named filter_helloworld in this file and inherit from the moodle_text_filter class.

<? PHP
ClassFilter_helloworldExtendsMoodle_text_filter {
//...
}
?>

Note: All plug-in classes are plug-in category (filter) + plug-in name (helloworld), connected. Because text is filtered, all classes that inherit text filtering (moodle_text_filter ).

3) in this class, a "filter" method must be defined. This method requires HTML as a parameter to be filtered. Then, the method is converted and the processed text is returned. Replace "//…", Like this:

class filter_helloworld extends moodle_text_filter {
    public function filter($text, array $options = array())        {
        return str_replace(‘world‘, ‘hello world!‘, $text);
    }
}

Note: If this filter is enabled, the filter class will be instantiated before the content is output by moodle, and the filter method will be called to filter the output content.

 

2.Name the filter

4) create a folder named "Lang" and create a sub-folder named "en" (in English). The Chinese name is "zh_cn ".

5) in the en directory, create a file named "filter_helloworld.php", that is, the file filter/helloworld/lang/en/filter_helloworld.php.

6) in this file, add

<?php // $Id$
// Language string for filter/helloworld.
 
$string[‘filtername‘] = ‘Hello world!‘;

Only add the plug-in name to the language file.

 3.Install Filter

Before installation, you must define the plug-in version and other information.

7) create a version. php file

8) Add the following content to the file:

// Prevent direct access through the path address.

Defined ('moodle _ internal ') | die ();

// Plug-in version number and other information

$ Plugin-> Version = 2016030201;

$ Plugin-> requires = 2016020201; // requires this moodle version.

$ Plugin-> component = 'filter _ helloworld ';

9) Plug-in Installation

After the Administrator logs on, the system automatically checks the agent updates or opens the notification page.

 

We can see the related content in version. php. Click "upgrade moodle database now" to start installation.

 

Prompt that the installation is successful. Click "continue" to go to the filter Management page.

4.Test Filter

Take the course description as an example. If the string "world" is contained, it will be replaced with "Hello World ".

Before enabling a filter, for example:

 

Enable filter:

 

After it is enabled, the output content is shown in:

 

We can compare the course description output before and after, which clearly shows that our filter is valid. Of course, we can also try to replace it with other strings.

  1. Add a global settings page

Add global settings, and perform operations on what to replace and what to replace according to the settings.

8) create the "Settings. php" file (filesettings. php before moodle2.6 ).

9) in the "Settings. php" file, add the following content:

$settings->add(new admin_setting_configtext(‘filter_helloworld/word‘,
        get_string(‘word‘, ‘filter_helloworld‘),
        get_string(‘word_desc‘, ‘filter_helloworld‘), ‘world‘, PARAM_NOTAGS));
$settings->add(new admin_setting_configtext(‘filter_helloworld/newword‘,
        get_string(‘newword‘, ‘filter_helloworld‘),
        get_string(‘newword_desc‘, ‘filter_helloworld‘), ‘newworld‘, PARAM_NOTAGS));
 

10) in the language file 'filter/helloworld/lang/en/filter_helloworld.php ', add the necessary strings:

$string[‘word‘] = ‘The thing to be replaced‘;
$string[‘word_desc‘] = ‘The hello world filter will replace this word by any other content!‘;
$string[‘newword‘] = ‘The thing to replace old word‘;
$string[‘newword_desc‘] = ‘The hello world filter will replace old word with new word!‘;

11) change the filter and use the new settings:

class filter_helloworld extends moodle_text_filter {
    public function filter($text, array $options = array()) {
        global $CFG;
        return str_replace($CFG->filter_helloworld/word,
                $CFG->filter_helloworld/newword, $text);
    }
}

$ Cfg-> filter_helloworld/word, which can be replaced by get_config ('filter _ helloworld', 'word') to obtain the global Word settings of the helloworld plug-in.

12) after adding the settings, refresh the management filter page to view the plug-in setting button. For details, see:

 

Click "Settings" to go to the settings page. For details, see:

 

We can see the scheduled prompt information and the default value, and keep the default value and save it.

 

Similarly, the content in the red box, that is, the course description, indicates that the global settings of the filter have been enabled and can be used properly.

  1. Performance description

When a filter is created and enabled, the filter is called by format_text () or format_string () to convert each byte of text output, resulting in a certain load. The glossary (Vocabulary) filter is an example in terms of load reduction.

If your filter uses a special syntax or a string based on the text, we recommend that you perform a quick and inexpensive strpos () search before performing a complete regular expression replacement.

/**
 * Example of a filter that uses <a> links in some way.
 */

public function filter($text, array $options = array()) {
 
    if (!is_string($text) or empty($text)) {
        // Non-string data can not be filtered anyway.
        return $text;
    }
 
    if (stripos($text, ‘</a>‘) === false) {
        // Performance shortcut - if there is no </a> tag, nothing can match.
        return $text;
    }
 
    // Here we can perform some more complex operations with the <a>
    // links in the text.
}

The actual code is as follows:

Public Function Filter ($ text, array $ Options = array ()){

// Verify whether the text is a string or empty

If (! Is_string ($ text) or empty ($ text )){

// Non-string data can not be filtered anyway.

Return $ text;

}

Global $ cfg;

// Obtain the global configuration string

$ Word = get_config ('filter _ helloworld', 'word ');

$ Newword = get_config ('filter _ helloworld', 'newword ');

// Whether to filter strings

If (stripos ($ text, $ word) ===false ){

// Performance shortcut-if there is no </a> tag, nothing can match.

Return $ text;

}

Return str_replace ($ word, $ newword, $ text );

}

At this point, simple filter development is basically complete. Add and adjust specific functions based on the actual situation.

Refer to the censor (sensitive word) filter.

Here are some instructions on censor (sensitive word) filters:

 

  1. File structure

Similar to the preceding example:

/Lang: All Language Pack Directories

/Lang/EN: English Language Pack

/Lang/en/filter_censor.php: English string (translation) File

/Readme.txt: describes how to install and use the plug-in. We recommend that you add (especially for plug-ins to be released ).

/Filter. php: the core file of the filter, including the filter class and the custom filtering method (which will be called by moodle)

/Settings. php: plug-in global settings (form) File

/Version. php: contains the plug-in version number and other information.

  1. Code sorting

1) filter. php

 

 

 

 

2) settings. php

 

3)/lang/en/filter_censor.php

 

  1. Operation display

1) log on as an administrator and go to site administration> plugins> Filters> Manage filters and find the sensitive word (censor) filter, that is, word censorship. See the red and green boxes respectively.

At this time, the filter has not been opened and is displayed as a gray font and cannot be set or uninstalled.

List explanation:

Filter: displays the names of each filter.

Active (activated ?) : Both agent status and active installation.

Order: Display and moodle call order

Apply to: Content and title of the application filter.

Settings: The corresponding filter setting button

Uninstall (uninstall): The corresponding filter Uninstall button

 

2) Open the filter.

 

Select the filter status as "on" and click it to activate the filter. As shown in, the set and unmount buttons are available.

 

3) settings

Click the sensitive word setting button to go to the setting page (for example). The content in the red box is the form elements defined in settings. php, as well as the display name, description, and default value.

The input filter string is separated by a comma (,), for example, "Fuck, fucker ".

Save the settings.

 

4) Test the filter.

A) when this filter is not enabled, the test course description is displayed as follows:

 

B) enable the filter, but keep the default value (defined in/lang/en/filter_censor.php). The test course description is shown as follows:

 

C) custom filter content: "censor", indicating that only the string "censor" is filtered.

 

Save.

 

Prompt successful.

In this case, the description of the test course is displayed in this way. You can clearly see that different operations correspond to different display contents.

 

So far, the introduction to sensitive word filters has been completed. I believe you know what filters and how to use them.

Filters of moodle plug-in)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.