Permalink
Browse files

Merge branch 'MDL-47962' of git://github.com/timhunt/moodle

  • Loading branch information...
dmonllao committed Sep 24, 2018
2 parents bdc5113 + 3923151 commit ccff34bf07517fa672a6853a5cbd7b9ef36dbd28
Showing with 544 additions and 290 deletions.
  1. +112 −73 filter/glossary/filter.php
  2. +2 −0 filter/glossary/lang/en/filter_glossary.php
  3. +190 −3 filter/glossary/tests/filter_test.php
  4. +14 −0 filter/upgrade.txt
  5. +226 −214 lib/filterlib.php
@@ -33,12 +33,8 @@
* NOTE: multilang glossary entries are not compatible with this filter.
*/
class filter_glossary extends moodle_text_filter {
/** @var int $cachecourseid cache invalidation flag in case content from multiple courses displayed. */
protected $cachecourseid = null;
/** @var int $cacheuserid cache invalidation flag in case user is switched. */
protected $cacheuserid = null;
/** @var array $cacheconceptlist page level filter cache, this should be always faster than MUC */
protected $cacheconceptlist = null;
/** @var null|cache_store cache used to store the terms for this course. */
protected $cache = null;
public function setup($page, $context) {
if ($page->requires->should_create_one_time_item_now('filter_glossary_autolinker')) {
@@ -50,8 +46,16 @@ public function setup($page, $context) {
}
}
public function filter($text, array $options = array()) {
global $CFG, $USER, $GLOSSARY_EXCLUDEENTRY;
/**
* Get all the concepts for this context.
* @return filterobject[] the concepts, and filterobjects.
*/
protected function get_all_concepts() {
global $USER;
if ($this->cache === null) {
$this->cache = cache::make_from_params(cache_store::MODE_REQUEST, 'filter', 'glossary');
}
// Try to get current course.
$coursectx = $this->context->get_course_context(false);
@@ -62,96 +66,131 @@ public function filter($text, array $options = array()) {
$courseid = $coursectx->instanceid;
}
if ($this->cachecourseid != $courseid or $this->cacheuserid != $USER->id) {
$cached = $this->cache->get('concepts');
if ($cached !== false && ($cached->cachecourseid != $courseid || $cached->cacheuserid != $USER->id)) {
// Invalidate the page cache.
$this->cacheconceptlist = null;
$cached = false;
}
if (is_array($this->cacheconceptlist) and empty($GLOSSARY_EXCLUDEENTRY)) {
if (empty($this->cacheconceptlist)) {
return $text;
}
return filter_phrases($text, $this->cacheconceptlist);
if ($cached !== false && is_array($cached->cacheconceptlist)) {
return $cached->cacheconceptlist;
}
list($glossaries, $allconcepts) = \mod_glossary\local\concept_cache::get_concepts($courseid);
if (!$allconcepts) {
$this->cacheuserid = $USER->id;
$this->cachecourseid = $courseid;
$this->cacheconcepts = array();
return $text;
$tocache = new stdClass();
$tocache->cacheuserid = $USER->id;
$tocache->cachecourseid = $courseid;
$tocache->cacheconceptlist = [];
$this->cache->set('concepts', $tocache);
return [];
}
$strcategory = get_string('category', 'glossary');
$conceptlist = array();
$excluded = false;
foreach ($allconcepts as $concepts) {
foreach ($concepts as $concept) {
if (!empty($GLOSSARY_EXCLUDEENTRY) and $concept->id == $GLOSSARY_EXCLUDEENTRY) {
$excluded = true;
continue;
}
if ($concept->category) { // Link to a category.
// TODO: Fix this string usage.
$title = $glossaries[$concept->glossaryid] . ': ' . $strcategory . ' ' . $concept->concept;
$link = new moodle_url('/mod/glossary/view.php', array('g' => $concept->glossaryid, 'mode' => 'cat', 'hook' => $concept->id));
$attributes = array(
'href' => $link,
'title' => $title,
'class' => 'glossary autolink category glossaryid' . $concept->glossaryid);
} else { // Link to entry or alias
$title = $glossaries[$concept->glossaryid] . ': ' . $concept->concept;
// Hardcoding dictionary format in the URL rather than defaulting
// to the current glossary format which may not work in a popup.
// for example "entry list" means the popup would only contain
// a link that opens another popup.
$link = new moodle_url('/mod/glossary/showentry.php', array('eid' => $concept->id, 'displayformat' => 'dictionary'));
$attributes = array(
'href' => $link,
'title' => str_replace('&', '&', $title), // Undo the s() mangling.
'class' => 'glossary autolink concept glossaryid' . $concept->glossaryid);
}
// This flag is optionally set by resource_pluginfile()
// if processing an embedded file use target to prevent getting nested Moodles.
if (!empty($CFG->embeddedsoforcelinktarget)) {
$attributes['target'] = '_top';
}
$href_tag_begin = html_writer::start_tag('a', $attributes);
$conceptlist[] = new filterobject($concept->concept, $href_tag_begin, '</a>',
$concept->casesensitive, $concept->fullmatch);
$conceptlist[] = new filterobject($concept->concept, null, null,
$concept->casesensitive, $concept->fullmatch, null,
[$this, 'filterobject_prepare_replacement_callback'], [$concept, $glossaries]);
}
}
usort($conceptlist, 'filter_glossary::sort_entries_by_length');
// We sort longest first, so that when we replace the terms,
// the longest ones are replaced first. This does the right thing
// when you have two terms like 'Moodle' and 'Moodle 3.5'. You want the longest match.
usort($conceptlist, [$this, 'sort_entries_by_length']);
$conceptlist = filter_prepare_phrases_for_filtering($conceptlist);
$tocache = new stdClass();
$tocache->cacheuserid = $USER->id;
$tocache->cachecourseid = $courseid;
$tocache->cacheconceptlist = $conceptlist;
$this->cache->set('concepts', $tocache);
return $conceptlist;
}
/**
* Callback used by filterobject / filter_phrases.
*
* @param object $concept the concept that is being replaced (from get_all_concepts).
* @param array $glossaries the list of glossary titles (from get_all_concepts).
* @return array [$hreftagbegin, $hreftagend, $replacementphrase] for filterobject.
*/
public function filterobject_prepare_replacement_callback($concept, $glossaries) {
global $CFG;
if ($concept->category) { // Link to a category.
$title = get_string('glossarycategory', 'filter_glossary',
['glossary' => $glossaries[$concept->glossaryid], 'category' => $concept->concept]);
$link = new moodle_url('/mod/glossary/view.php',
['g' => $concept->glossaryid, 'mode' => 'cat', 'hook' => $concept->id]);
$attributes = array(
'href' => $link,
'title' => $title,
'class' => 'glossary autolink category glossaryid' . $concept->glossaryid);
} else { // Link to entry or alias.
$title = get_string('glossaryconcept', 'filter_glossary',
['glossary' => $glossaries[$concept->glossaryid], 'concept' => $concept->concept]);
// Hardcoding dictionary format in the URL rather than defaulting
// to the current glossary format which may not work in a popup.
// for example "entry list" means the popup would only contain
// a link that opens another popup.
$link = new moodle_url('/mod/glossary/showentry.php',
['eid' => $concept->id, 'displayformat' => 'dictionary']);
$attributes = array(
'href' => $link,
'title' => str_replace('&amp;', '&', $title), // Undo the s() mangling.
'class' => 'glossary autolink concept glossaryid' . $concept->glossaryid);
}
if (!$excluded) {
// Do not cache the excluded list here, it is used once per page only.
$this->cacheuserid = $USER->id;
$this->cachecourseid = $courseid;
$this->cacheconceptlist = $conceptlist;
// This flag is optionally set by resource_pluginfile()
// if processing an embedded file use target to prevent getting nested Moodles.
if (!empty($CFG->embeddedsoforcelinktarget)) {
$attributes['target'] = '_top';
}
return [html_writer::start_tag('a', $attributes), '</a>', null];
}
public function filter($text, array $options = array()) {
global $GLOSSARY_EXCLUDEENTRY;
$conceptlist = $this->get_all_concepts();
if (empty($conceptlist)) {
return $text;
}
return filter_phrases($text, $conceptlist); // Actually search for concepts!
}
private static function sort_entries_by_length($entry0, $entry1) {
$len0 = strlen($entry0->phrase);
$len1 = strlen($entry1->phrase);
if (!empty($GLOSSARY_EXCLUDEENTRY)) {
foreach ($conceptlist as $key => $filterobj) {
// The original concept object was stored here in when $filterobj was constructed in
// get_all_concepts(). Get it back out now so we can check to see if it is excluded.
$concept = $filterobj->replacementcallbackdata[0];
if (!$concept->category && $concept->id == $GLOSSARY_EXCLUDEENTRY) {
unset($conceptlist[$key]);
}
}
}
if ($len0 < $len1) {
return 1;
} else if ($len0 > $len1) {
return -1;
} else {
return 0;
if (empty($conceptlist)) {
return $text;
}
return filter_phrases($text, $conceptlist, null, null, false, true);
}
/**
* usort helper used in get_all_concepts above.
* @param filterobject $filterobject0 first item to compare.
* @param filterobject $filterobject1 second item to compare.
* @return int -1, 0 or 1.
*/
private function sort_entries_by_length($filterobject0, $filterobject1) {
return strlen($filterobject1->phrase) <=> strlen($filterobject0->phrase);
}
}
@@ -25,5 +25,7 @@
defined('MOODLE_INTERNAL') || die();
$string['glossarycategory'] = '{$a->glossary}: Category {$a->category}';
$string['glossaryconcept'] = '{$a->glossary}: {$a->concept}';
$string['filtername'] = 'Glossary auto-linking';
$string['privacy:metadata'] = 'The Glossary auto-linking plugin does not store any personal data.';
Oops, something went wrong.

0 comments on commit ccff34b

Please sign in to comment.