+353 (0)1 602 4784
info@enovation.ie
  • Client Support
  • FR
  • UK
EnovationEnovationEnovationEnovation
+353 (0)1 602 4784
  • WHAT WE DO
    • OUR SERVICES
      • Consultancy
      • eLearning Content
      • Build & Design
      • Training
      • Moodle Educator Certification Program (MEC)
      • Hosting
      • Support & Maintenance
    • OUR SOLUTIONS
      • Aura Talent
      • Moodle
      • Moodle Workplace
      • Totara’s Talent Experience Platform
        • Totara Learn
        • Totara Engage
        • Totara Perform
      • Mahara
      • Go1 Premium Content
  • WHO WE ARE
    • About Enovation
    • Careers
  • CASE STUDIES
  • OUR PARTNERS
    • Microsoft Teams
    • IntelliBoard
    • BigBlueButton
  • INSIGHTS
  • CONTACT US

Creating simple filter in Moodle 2

    Home Blog Creating simple filter in Moodle 2

    Creating simple filter in Moodle 2

    Tomek Muras | Blog, News | 8 December, 2010

    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.

    Next Previous
    Filter, Moodle+, Moodle2

    Related Post

    • Whats new in Moodle 2.0

      Mark Melia | Blog, News | 5 December, 2010

      Moodle 2.0, the long awaited next generation of Moodle was released on the 24th of November to much aclaim. In this blog post I am going to have a quick look at some of theRead more

    • Our 2020 Highlights-It’s been quite a year!

      Emma Cronin | Blog, eLearning, Enovation News, News | 16 December, 2020

      2020 is a year that we will never forget. We have adapted to new ways of working and together we have come through this challenging time by working together. Although it has been a difficultRead more

    • Enovation: Microsoft Education Partner

      Microsoft Teams: Add Teams Meeting Scheduling to Your Solution

      Enovation Admin | News | 26 November, 2020

      Enovation is proud to be a certified Microsoft Education Specialist Partner. This partnership seeks to give educators the tools they need to unlock limitless learning for students and help them achieve their full potential. WithRead more

    • Manage multiple learning environments from a single installation with Moodle Workplace 3.9.2

      Enovation Admin | Blog, eLearning, Enovation News, Events, Featured News, Media, Moodle, News | 19 October, 2020

      The latest version of Moodle’s corporate LMS takes multi-tenancy one step further with the new shared space, shared programs and shared certifications. Moodle Workplace is our learning management system designed to create collaborative learning experiencesRead more

    • Upcoming Webinar’s: August&September ’20. Save Your Seat!

      Enovation Admin | Blog, eLearning, Enovation News, Events, Featured News | 13 August, 2020

      MEC Webinar- August 27th If you are interested in hearing more about the Moodle Educator Program (MEC) we have an upcoming webinar that will begin at 10:00 AM on the 27th of August, eLearning ConsultantRead more

    NextPrevious

    Latest Insights

    • 23 February, 2021
      Comments Off on Learn2Analyze MOOC B Launches on March 1st 2021

      Learn2Analyze MOOC B Launches on March 1st 2021

    • 19 February, 2021
      Comments Off on Totara Perform: The Flexible Performance Management Platform that Works for them and for you.

      Totara Perform: The Flexible Performance Management Platform that Works for them and for you.

    • 17 February, 2021
      Comments Off on Guest Post: 3 Reasons Why Collaborative Learning is the Key to Employee Engagement

      Guest Post: 3 Reasons Why Collaborative Learning is the Key to Employee Engagement

    • Enovation Totara Talent Experience Platform
      16 February, 2021
      Comments Off on Guest Blog: Totara’s Talent Experience Platform: Transform your Learning, Engagement and Performance

      Guest Blog: Totara’s Talent Experience Platform: Transform your Learning, Engagement and Performance

    Archives

    • February 2021
    • January 2021
    • December 2020
    • November 2020
    • October 2020
    • September 2020
    • August 2020
    • July 2020
    • June 2020
    • May 2020
    • April 2020
    • March 2020
    • February 2020
    • January 2020
    • November 2019
    • October 2019
    • September 2019
    • August 2019
    • July 2019
    • June 2019
    • May 2019
    • March 2019
    • February 2019
    • January 2019
    • December 2018
    • November 2018
    • October 2018
    • September 2018
    • August 2018
    • July 2018
    • June 2018
    • May 2018
    • April 2018
    • March 2018
    • February 2018
    • January 2018
    • December 2017
    • November 2017
    • October 2017
    • September 2017
    • August 2017
    • July 2017
    • June 2017
    • May 2017
    • April 2017
    • March 2017
    • January 2017
    • November 2016
    • October 2016
    • June 2016
    • March 2016
    • February 2016
    • January 2016
    • December 2015
    • November 2015
    • October 2015
    • September 2015
    • July 2015
    • June 2015
    • May 2015
    • March 2015
    • February 2015
    • January 2015
    • December 2014
    • November 2014
    • September 2014
    • June 2014
    • May 2014
    • January 2014
    • August 2013
    • April 2013
    • March 2013
    • February 2013
    • January 2013
    • September 2012
    • July 2012
    • May 2012
    • April 2012
    • February 2012
    • December 2011
    • November 2011
    • June 2011
    • May 2011
    • April 2011
    • March 2011
    • February 2011
    • January 2011
    • December 2010

    Sign-up to get our insights direct to your inbox

      Enovation Square

      Enovation Solutions Ltd.,
      The Friary,
      Bow Street,
      Dublin, D07 X680,
      T: +353 (0) 1 602 4784

      Partners & Certifications

      images

      Moodle Partner Logo

      Latest Insights

      • Learn2Analyze MOOC B Launches on March 1st 2021

        Date: 1 March 2021-1 May 2021 | Duration: 9 weeks| Pre-Requisites: None| Time Commitment: 100 hours in total | Certificate available: Yes| Costs: Free (course and certificate)

        23 February, 2021
      • Totara Perform: The Flexible Performance Management Platform that Works for them and for you.

        Totara Perform is the performance management software that boosts workforce productivity. You can develop your people into the very best they can be with a flexible performance management platform that works for you both.

        19 February, 2021
      • Guest Post: 3 Reasons Why Collaborative Learning is the Key to Employee Engagement

        Guest Author:  Hassan Ud-deen, Content Writer at Totara Learn. 

        17 February, 2021

      Latest Tweets

      Tweets by @EnovationIRE
      • Legal Information
      © 2021 Enovation Your eLearning Partner
      • WHAT WE DO
        • OUR SERVICES
          • Consultancy
          • eLearning Content
          • Build & Design
          • Training
          • Moodle Educator Certification Program (MEC)
          • Hosting
          • Support & Maintenance
        • OUR SOLUTIONS
          • Aura Talent
          • Moodle
          • Moodle Workplace
          • Totara’s Talent Experience Platform
            • Totara Learn
            • Totara Engage
            • Totara Perform
          • Mahara
          • Go1 Premium Content
      • WHO WE ARE
        • About Enovation
        • Careers
      • CASE STUDIES
      • OUR PARTNERS
        • Microsoft Teams
        • IntelliBoard
        • BigBlueButton
      • INSIGHTS
      • CONTACT US
      Enovation
      +353 (0)1 602 4784