Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

MDL-32279 glossary filter: nasty hack to get filter working with cach…

…ed texts.

For Moodle 2.3 the filters API has been extended to offer a
setup() method, able to define all the page/init requirements
for each filter.

For this (2.2) version, where the setup() method is
not available, we are abusing badly the hash() method because
it's the unique point out from the caching mechanism where we
can inject such requirements. Alternative solutions like
backporting the API or add the requirements to all the pages
have been considered worse than this abuse.

Note, finally that this is not backportable to 2.1 because the
glossary filter there is a legacy one, so it does not have the
hash() facility either. Unique workaround there is to disable caching.
  • Loading branch information...
commit aa0bf8596a3fcf6bf2969fb10515f86d386ce11b 1 parent 63e5dde
Eloy Lafuente (stronk7) stronk7 authored

Showing 1 changed file with 38 additions and 12 deletions. Show diff stats Hide diff stats

  1. +38 12 filter/glossary/filter.php
50 filter/glossary/filter.php
@@ -35,14 +35,50 @@
35 35 */
36 36 class filter_glossary extends moodle_text_filter {
37 37
  38 + /**
  39 + * Abuse the hash() method to fix MDL-32279 in 2.2 stable.
  40 + *
  41 + * For Moodle 2.3 the filters API has been extended to offer a
  42 + * setup() method, able to define all the page/init requirements
  43 + * for each filter.
  44 + *
  45 + * For this (2.2) version, where the setup() method is
  46 + * not available, we are abusing badly the hash() method because
  47 + * it's the unique point out from the caching mechanism where we
  48 + * can inject such requirements. Alternative solutions like
  49 + * backporting the API or add the requirements to all the pages
  50 + * have been considered worse than this abuse.
  51 + *
  52 + * Note, finally that this is not backportable to 2.1 because the
  53 + * glossary filter there is a legacy one, so it does not have the
  54 + * hash() facility either. Unique workaround there is to disable caching.
  55 + */
  56 + public function hash() {
  57 + global $PAGE;
  58 +
  59 + // Don't forget it: this is bad abuse of the method to
  60 + // provide caching-independent requirements support to filters! MDL-32279.
  61 +
  62 + // This only requires execution once per request.
  63 + static $jsinitialised = false;
  64 + if (empty($jsinitialised)) {
  65 + $PAGE->requires->yui_module(
  66 + 'moodle-filter_glossary-autolinker',
  67 + 'M.filter_glossary.init_filter_autolinking',
  68 + array(array('courseid' => 0)));
  69 + $jsinitialised = true;
  70 + }
  71 + // Now return hash() normally.
  72 + return parent::hash();
  73 + }
  74 +
38 75 public function filter($text, array $options = array()) {
39   - global $CFG, $DB, $GLOSSARY_EXCLUDECONCEPTS, $PAGE;
  76 + global $CFG, $DB, $GLOSSARY_EXCLUDECONCEPTS;
40 77
41 78 // Trivial-cache - keyed on $cachedcontextid
42 79 static $cachedcontextid;
43 80 static $conceptlist;
44 81
45   - static $jsinitialised; // To control unique js init
46 82 static $nothingtodo; // To avoid processing if no glossaries / concepts are found
47 83
48 84 // Try to get current course
@@ -185,16 +221,6 @@ public function filter($text, array $options = array()) {
185 221 }
186 222
187 223 $conceptlist = filter_remove_duplicates($conceptlist);
188   -
189   - if (empty($jsinitialised)) {
190   - // Add a JavaScript event to open popup's here. This only ever need to be
191   - // added once!
192   - $PAGE->requires->yui_module(
193   - 'moodle-filter_glossary-autolinker',
194   - 'M.filter_glossary.init_filter_autolinking',
195   - array(array('courseid' => $courseid)));
196   - $jsinitialised = true;
197   - }
198 224 }
199 225
200 226 if (!empty($GLOSSARY_EXCLUDECONCEPTS)) {

0 comments on commit aa0bf85

Please sign in to comment.
Something went wrong with that request. Please try again.