Moodle
  1. Moodle
  2. MDL-32059

Provide filters with more context for generating hashes.

    Details

    • Affected Branches:
      MOODLE_22_STABLE
    • Rank:
      38749

      Description

      I wanted to add support for using the groups and groupings of which a user is a member in styling Moodle output. For example, I have a Subject Outline page which describes (among other things) the word limit on an essay. For students in the 300-level grouping, I wanted to be able to use CSS to show a span describing the 300-level word limit and hide a span describing the 500-level word limit. I've implemented this myself, but need to create a small patch to core so that the text to be filtered and options are passed to filters' hash functions. The hash function is unused in core, so the API change should only affect other 3rd party modules.

        Activity

        Hide
        Michael de Raadt added a comment -

        Hi, Nigel.

        Thanks for the suggestion.

        If you can work on this yourself, you might want to do that. I don't think this one will rise up through the ranks soon.

        Perhaps you could make use of multi-lang filters to achieve your purpose (http://docs.moodle.org/22/en/Multi-language_content_filter), although I can't think of a way of automatically tying a language to a group.

        Show
        Michael de Raadt added a comment - Hi, Nigel. Thanks for the suggestion. If you can work on this yourself, you might want to do that. I don't think this one will rise up through the ranks soon. Perhaps you could make use of multi-lang filters to achieve your purpose ( http://docs.moodle.org/22/en/Multi-language_content_filter ), although I can't think of a way of automatically tying a language to a group.
        Hide
        Nigel Cunningham added a comment -

        Thanks for the quick response.

        That does look quite similar. I'll take a look.

        I was mainly worried about caching, but I guess (from a brief look at the Multilang description) that I'll be able to get around those issues by following the same methodology.

        Show
        Nigel Cunningham added a comment - Thanks for the quick response. That does look quite similar. I'll take a look. I was mainly worried about caching, but I guess (from a brief look at the Multilang description) that I'll be able to get around those issues by following the same methodology.
        Hide
        Nigel Cunningham added a comment -

        Well, I have a filter written, loading the text into a Dom and using the user's groups to delete bits of text that don't apply to them. I haven't made it generic enough to share yet (the group names and class names and the relationship between them are hard coded), but that side of things is otherwise fine.

        The difficulty I've run into now is caching. I can't see in Multilang how caching is handled. Without having examined the code (yet), it seems to me that I'm going to be wanting to have some sort of additional function that core can use to see whether my filter is going to consider the context to be the same. (ie I need to be able to give it a key describing the groups that were used in producing the filtered output).

        I'll go investigate how caching works now

        Show
        Nigel Cunningham added a comment - Well, I have a filter written, loading the text into a Dom and using the user's groups to delete bits of text that don't apply to them. I haven't made it generic enough to share yet (the group names and class names and the relationship between them are hard coded), but that side of things is otherwise fine. The difficulty I've run into now is caching. I can't see in Multilang how caching is handled. Without having examined the code (yet), it seems to me that I'm going to be wanting to have some sort of additional function that core can use to see whether my filter is going to consider the context to be the same. (ie I need to be able to give it a key describing the groups that were used in producing the filtered output). I'll go investigate how caching works now
        Hide
        Nigel Cunningham added a comment -

        So... the hash is calculated at line 1037 of lib/weblib.php (Moodle 2.2), and has hardcoded in it the language (among other things), which will I assume be how multilang works.

        I guess this means I need to come up with a patch that makes that more generic - let filters that are going to be used each return a string describing any additional context they'll use, and combine those together as an additional part of the string. Perhaps also push the current_language() call down into such code in multilang?

        Show
        Nigel Cunningham added a comment - So... the hash is calculated at line 1037 of lib/weblib.php (Moodle 2.2), and has hardcoded in it the language (among other things), which will I assume be how multilang works. I guess this means I need to come up with a patch that makes that more generic - let filters that are going to be used each return a string describing any additional context they'll use, and combine those together as an additional part of the string. Perhaps also push the current_language() call down into such code in multilang?
        Hide
        Nigel Cunningham added a comment -

        Ahhh!

        text_filtering_hash... but it doesn't give the filter any context.

        Show
        Nigel Cunningham added a comment - Ahhh! text_filtering_hash... but it doesn't give the filter any context.
        Hide
        Nigel Cunningham added a comment -

        Okay. With a little patch to core, it works Here's the git diff (can't see from a quick check how to attach a file)...

        diff --git a/lib/filterlib.php b/lib/filterlib.php
        index 3496f37..f95f5c0 100644
        --- a/lib/filterlib.php
        +++ b/lib/filterlib.php
        @@ -214,11 +214,11 @@ class filter_manager {
              * @param object $context
              * @return object A string filter
              */
        -    public function text_filtering_hash($context) {
        +    public function text_filtering_hash($context, $text, $options) {
                 $filters = $this->get_text_filters($context);
                 $hashes = array();
                 foreach ($filters as $filter) {
        -            $hashes[] = $filter->hash();
        +            $hashes[] = $filter->hash($text, $options);
                 }
                 return implode('-', $hashes);
             }
        diff --git a/lib/weblib.php b/lib/weblib.php
        index 4cb9ca4..d0fa567 100644
        --- a/lib/weblib.php
        +++ b/lib/weblib.php
        @@ -1034,7 +1034,8 @@ function format_text($text, $format = FORMAT_MOODLE, $options = NULL, $courseid_
             }
         
             if (!empty($CFG->cachetext) and empty($options['nocache'])) {
        -        $hashstr = $text.'-'.$filtermanager->text_filtering_hash($context).'-'.$context->id.'-'.current_language().'-'.
        +        $hashstr = $text.'-'.$filtermanager->text_filtering_hash($context, $text, array('originalformat' => $format, 'noclean' => $options['noclean'])).
        +                '-'.$context->id.'-'.current_language().'-'.
                         (int)$format.(int)$options['trusted'].(int)$options['noclean'].
                         (int)$options['para'].(int)$options['newlines'];
         
        
        Show
        Nigel Cunningham added a comment - Okay. With a little patch to core, it works Here's the git diff (can't see from a quick check how to attach a file)... diff --git a/lib/filterlib.php b/lib/filterlib.php index 3496f37..f95f5c0 100644 --- a/lib/filterlib.php +++ b/lib/filterlib.php @@ -214,11 +214,11 @@ class filter_manager { * @param object $context * @ return object A string filter */ - public function text_filtering_hash($context) { + public function text_filtering_hash($context, $text, $options) { $filters = $ this ->get_text_filters($context); $hashes = array(); foreach ($filters as $filter) { - $hashes[] = $filter->hash(); + $hashes[] = $filter->hash($text, $options); } return implode('-', $hashes); } diff --git a/lib/weblib.php b/lib/weblib.php index 4cb9ca4..d0fa567 100644 --- a/lib/weblib.php +++ b/lib/weblib.php @@ -1034,7 +1034,8 @@ function format_text($text, $format = FORMAT_MOODLE, $options = NULL, $courseid_ } if (!empty($CFG->cachetext) and empty($options['nocache'])) { - $hashstr = $text.'-'.$filtermanager->text_filtering_hash($context).'-'.$context->id.'-'.current_language().'-'. + $hashstr = $text.'-'.$filtermanager->text_filtering_hash($context, $text, array('originalformat' => $format, 'noclean' => $options['noclean'])). + '-'.$context->id.'-'.current_language().'-'. ( int )$format.( int )$options['trusted'].( int )$options['noclean']. ( int )$options['para'].( int )$options['newlines'];
        Hide
        Nigel Cunningham added a comment -

        Patch version of the above.

        Show
        Nigel Cunningham added a comment - Patch version of the above.

          People

          • Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

            Dates

            • Created:
              Updated: