Filters in Moodle are pluggable elements that can convert an output text to some other text. This is currently used for the functionality like:
- replacing text based emoticons with images (emoticon filter)
- generating images for mathematical expressions (tex filer)
- converting plain text URLs (http://…) into proper HTML links
- and much more
In this tutorial we will create a simple, stand-alone filter for Moodle 2. The simplest case is actually so simple, that nearly all functionality can be coded in just few lines of code, like:
[sourcecode language=”php”]
defined(‘MOODLE_INTERNAL’) || die();
class filter_tidy extends moodle_text_filter {
function filter($text, array $options = array()) {
$text = tidy_repair_string($text, $tidyoptions, ‘utf8’);
return $text;
}
}
[/sourcecode]
All we need to do is inherit class moodle_text_filter and implement it’s abstract method:
[sourcecode language=”php”]
public abstract function filter($text, array $options = array());
[/sourcecode]
The example above is copied and shortened code from the standard “tex” filter. For our tutorial we will create a simple filter for removing (censoring) “bad words”. There is a similar module in core Moodle called censor, we will create a new version of it. First, create a new directory named censor2. Inside create a file filter.php with full basic functionality of our filter:
[sourcecode language=”php”]
<?php
defined(‘MOODLE_INTERNAL’) || die();
class filter_censor2 extends moodle_text_filter {
function filter($text, array $options = array()){
$badwords = array(‘badword1′,’badword2’);
foreach ($badwords as $badword) {
$badword = trim($badword);
$words[] = new filterobject($badword, ‘<span class=”censoredtext” title=”censored”>’,
‘</span>’, false, false,”****”);
}
return filter_phrases($text, $words);
}
}
[/sourcecode]
We only need one more file: censor2/lang/en/filter_censor2.php that will contain just a name of our plugin:
[sourcecode language=”php”]
<?php
$string[‘filtername’] = ‘Word Censorship 2’;
[/sourcecode]
In our filter_censor2, we have used a filter_phrases function provided by Moodle API and defined in lib/filterlib.php. The function takes two arguments:
- a full text for filtering – we simply pass our $text argument here
- a list of filterobject objects
Each filterobject is a very simple object that stores following information (in the same order as arguments for constructor):
- $phrase – the phrase to be matched
- $hreftagbegin – the text to be put before the matched text (default: <span class=”highlight”>)
- $hreftagend – the text to be added after the match (default: <span class=”highlight”>)
- $casesensitive – should matching $phrase be case sensitive (default: false)
- $fullmatch – should we match only full words (default: false)
- $replacementphrase – a string for replacing matched phrase (default NULL – meaning don’t replace the $phrase)
Only the first argument is required, all the others have sensible default values.
At this point we have a very simple but fully functional Moodle 2 filter! All that is left is to deploy it: copy whole censor2 directory into <your_Moodle_installation>/filter, go to Site administration -> Plugins -> Filters -> Manage filters and enable new filter. You can download full source code.