2 // This file is part of Moodle - http://moodle.org/
4 // Moodle is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
9 // Moodle is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
14 // You should have received a copy of the GNU General Public License
15 // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
18 * This filter provides automatic linking to
19 * glossary entries, aliases and categories when
20 * found inside every Moodle text.
23 * @subpackage glossary
24 * @copyright 2004 onwards Eloy Lafuente (stronk7) {@link http://stronk7.com}
25 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
28 defined('MOODLE_INTERNAL') ||
die();
31 * Glossary linking filter class.
33 * NOTE: multilang glossary entries are not compatible with this filter.
35 class filter_glossary
extends moodle_text_filter
{
36 /** @var int $cachecourseid cache invalidation flag in case content from multiple courses displayed. */
37 protected $cachecourseid = null;
38 /** @var int $cacheuserid cache invalidation flag in case user is switched. */
39 protected $cacheuserid = null;
40 /** @var array $cacheconceptlist page level filter cache, this should be always faster than MUC */
41 protected $cacheconceptlist = null;
43 public function setup($page, $context) {
44 // This only requires execution once per request.
45 static $jsinitialised = false;
46 if (empty($jsinitialised)) {
47 $page->requires
->yui_module(
48 'moodle-filter_glossary-autolinker',
49 'M.filter_glossary.init_filter_autolinking',
50 array(array('courseid' => 0)));
51 $page->requires
->strings_for_js(array('ok'), 'moodle');
52 $jsinitialised = true;
56 public function filter($text, array $options = array()) {
57 global $CFG, $USER, $GLOSSARY_EXCLUDEENTRY;
59 // Try to get current course.
60 $coursectx = $this->context
->get_course_context(false);
62 // Only global glossaries will be linked.
65 $courseid = $coursectx->instanceid
;
68 if ($this->cachecourseid
!= $courseid or $this->cacheuserid
!= $USER->id
) {
69 // Invalidate the page cache.
70 $this->cacheconceptlist
= null;
73 if (is_array($this->cacheconceptlist
) and empty($GLOSSARY_EXCLUDEENTRY)) {
74 if (empty($this->cacheconceptlist
)) {
77 return filter_phrases($text, $this->cacheconceptlist
);
80 list($glossaries, $allconcepts) = \mod_glossary\local\concept_cache
::get_concepts($courseid);
83 $this->cacheuserid
= $USER->id
;
84 $this->cachecourseid
= $courseid;
85 $this->cacheconcepts
= array();
89 $strcategory = get_string('category', 'glossary');
91 $conceptlist = array();
94 foreach ($allconcepts as $concepts) {
95 foreach ($concepts as $concept) {
96 if (!empty($GLOSSARY_EXCLUDEENTRY) and $concept->id
== $GLOSSARY_EXCLUDEENTRY) {
100 if ($concept->category
) { // Link to a category.
101 // TODO: Fix this string usage.
102 $title = $glossaries[$concept->glossaryid
] . ': ' . $strcategory . ' ' . $concept->concept
;
103 $link = new moodle_url('/mod/glossary/view.php', array('g' => $concept->glossaryid
, 'mode' => 'cat', 'hook' => $concept->id
));
107 'class' => 'glossary autolink category glossaryid' . $concept->glossaryid
);
109 } else { // Link to entry or alias
110 $title = $glossaries[$concept->glossaryid
] . ': ' . $concept->concept
;
111 // Hardcoding dictionary format in the URL rather than defaulting
112 // to the current glossary format which may not work in a popup.
113 // for example "entry list" means the popup would only contain
114 // a link that opens another popup.
115 $link = new moodle_url('/mod/glossary/showentry.php', array('eid' => $concept->id
, 'displayformat' => 'dictionary'));
118 'title' => str_replace('&', '&', $title), // Undo the s() mangling.
119 'class' => 'glossary autolink concept glossaryid' . $concept->glossaryid
);
121 // This flag is optionally set by resource_pluginfile()
122 // if processing an embedded file use target to prevent getting nested Moodles.
123 if (!empty($CFG->embeddedsoforcelinktarget
)) {
124 $attributes['target'] = '_top';
126 $href_tag_begin = html_writer
::start_tag('a', $attributes);
128 $conceptlist[] = new filterobject($concept->concept
, $href_tag_begin, '</a>',
129 $concept->casesensitive
, $concept->fullmatch
);
133 usort($conceptlist, 'filter_glossary::sort_entries_by_length');
136 // Do not cache the excluded list here, it is used once per page only.
137 $this->cacheuserid
= $USER->id
;
138 $this->cachecourseid
= $courseid;
139 $this->cacheconceptlist
= $conceptlist;
142 if (empty($conceptlist)) {
145 return filter_phrases($text, $conceptlist); // Actually search for concepts!
148 private static function sort_entries_by_length($entry0, $entry1) {
149 $len0 = strlen($entry0->phrase
);
150 $len1 = strlen($entry1->phrase
);
154 } else if ($len0 > $len1) {