Thursday, May 6, 2010

How to Create and Modify Moodle Filters


by Jonathan Moore Michael Churchward | May 2010 | Moodle
In this article by Jonathan Moore and Michael Churchward, authors of Moodle 1.9 Extension Development, we will discuss the basic concepts of creating a Moodle filter, which includes:
  • How a filter works
  • Using the API to create filter code
  • How to use language files
  • How to create configuration settings
Moodle filters modify content from the database as it is output to the screen, thus adding function to the display. An example of this is the multimedia filter, which can detect references to video and audio files, and can replace them with a "mini-player" embedded in the content.

How a filter works

Before trying to build a filter, it would help to understand how it works. To begin with, any text written to the screen in Moodle should be processed through the format_text function. The purpose of this function is to process the text, such that it is always safe to be displayed. This means making sure there are no security issues and that any HTML used contains only allowed tags.
Additionally, the output is run through the filter_text function, and this is the function we are interested in. This function takes the text destined for the screen, and applies all enabled filters to it. The resulting text will be the result of all of these filters.
filter_text applies each enabled filter to the text in the order defined in the filter configuration screen (shown in the following screenshot). The order is important; each filter will be fed the output of the previous filter's text. So it is always possible that one filter may change the text in a way that impacts the next filter.

No comments:

Post a Comment