Skip to content

Commit

Permalink
Merge branch 'MDL-32279' of git://github.com/stronk7/moodle
Browse files Browse the repository at this point in the history
  • Loading branch information
danpoltawski committed Jun 15, 2012
2 parents 8db5df0 + d24f821 commit 66d9586
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 15 deletions.
25 changes: 13 additions & 12 deletions filter/glossary/filter.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,25 @@
*/
class filter_glossary extends moodle_text_filter {

public function setup($page, $context) {
// This only requires execution once per request.
static $jsinitialised = false;
if (empty($jsinitialised)) {
$page->requires->yui_module(
'moodle-filter_glossary-autolinker',
'M.filter_glossary.init_filter_autolinking',
array(array('courseid' => 0)));
$jsinitialised = true;
}
}

public function filter($text, array $options = array()) {
global $CFG, $DB, $GLOSSARY_EXCLUDECONCEPTS, $PAGE;
global $CFG, $DB, $GLOSSARY_EXCLUDECONCEPTS;

// Trivial-cache - keyed on $cachedcontextid
static $cachedcontextid;
static $conceptlist;

static $jsinitialised; // To control unique js init
static $nothingtodo; // To avoid processing if no glossaries / concepts are found

// Try to get current course
Expand Down Expand Up @@ -185,16 +196,6 @@ public function filter($text, array $options = array()) {
}

$conceptlist = filter_remove_duplicates($conceptlist);

if (empty($jsinitialised)) {
// Add a JavaScript event to open popup's here. This only ever need to be
// added once!
$PAGE->requires->yui_module(
'moodle-filter_glossary-autolinker',
'M.filter_glossary.init_filter_autolinking',
array(array('courseid' => $courseid)));
$jsinitialised = true;
}
}

if (!empty($GLOSSARY_EXCLUDECONCEPTS)) {
Expand Down
4 changes: 1 addition & 3 deletions filter/glossary/yui/autolinker/autolinker.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ YUI.add('moodle-filter_glossary-autolinker', function(Y) {
POPUPNAME = 'name',
POPUPOPTIONS = 'options',
TITLE = 'title',
COURSEID = 'courseid',
WIDTH = 'width',
HEIGHT = 'height',
MENUBAR = 'menubar',
Expand Down Expand Up @@ -122,8 +121,7 @@ YUI.add('moodle-filter_glossary-autolinker', function(Y) {
status : {value : true},
directories : {value : false},
fullscreen : {value : false},
dependent : {value : true},
courseid : {value : 1}
dependent : {value : true}
}
});

Expand Down
7 changes: 7 additions & 0 deletions filter/upgrade.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
This file describes API changes in core filter API and plugins,
information provided here is intended especially for developers.

=== 2.3 ===

* new setup() method added to moodle_text_filter, invoked before
filtering happens, used to add all the requirements to the page
(js, css...) and/or other init tasks. See filter/glossary for
an example using the API (and MDL-32279 for its justification).

=== 2.2 ===

* legacy filters and legacy locations have been deprecated, so any
Expand Down
41 changes: 41 additions & 0 deletions lib/filterlib.php
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,29 @@ public function text_filtering_hash($context) {
}
return implode('-', $hashes);
}

/**
* Setup page with filters requirements and other prepare stuff.
*
* This method is used by {@see format_text()} and {@see format_string()}
* in order to allow filters to setup any page requirement (js, css...)
* or perform any action needed to get them prepared before filtering itself
* happens by calling to each every active setup() method.
*
* Note it's executed for each piece of text filtered, so filter implementations
* are responsible of controlling the cardinality of the executions that may
* be different depending of the stuff to prepare.
*
* @param moodle_page $page the page we are going to add requirements to.
* @param context $context the context which contents are going to be filtered.
* @since 2.3
*/
public function setup_page_for_filters($page, $context) {
$filters = $this->get_text_filters($context);
foreach ($filters as $filter) {
$filter->setup($page, $context);
}
}
}

/**
Expand Down Expand Up @@ -357,6 +380,24 @@ public function hash() {
return __CLASS__;
}

/**
* Setup page with filter requirements and other prepare stuff.
*
* Override this method if the filter needs to setup page
* requirements or needs other stuff to be executed.
*
* Note this method is invoked from {@see setup_page_for_filters()}
* for each piece of text being filtered, so it is responsible
* for controlling its own execution cardinality.
*
* @param moodle_page $page the page we are going to add requirements to.
* @param context $context the context which contents are going to be filtered.
* @since 2.3
*/
public function setup($page, $context) {
// Override me, if needed.
}

/**
* Override this function to actually implement the filtering.
*
Expand Down
2 changes: 2 additions & 0 deletions lib/weblib.php
Original file line number Diff line number Diff line change
Expand Up @@ -1090,6 +1090,7 @@ function format_text($text, $format = FORMAT_MOODLE, $options = NULL, $courseid_

if ($options['filter']) {
$filtermanager = filter_manager::instance();
$filtermanager->setup_page_for_filters($PAGE, $context); // Setup global stuff filters may have.
} else {
$filtermanager = new null_filter_manager();
}
Expand Down Expand Up @@ -1302,6 +1303,7 @@ function format_string($string, $striplinks = true, $options = NULL) {

if (!empty($CFG->filterall)) {
$string = filter_manager::instance()->filter_string($string, $options['context']);
$filtermanager->setup_page_for_filters($PAGE, $options['context']); // Setup global stuff filters may have.
}

// If the site requires it, strip ALL tags from this string
Expand Down

0 comments on commit 66d9586

Please sign in to comment.