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 if ($page->requires
->should_create_one_time_item_now('filter_glossary_autolinker')) {
45 $page->requires
->yui_module(
46 'moodle-filter_glossary-autolinker',
47 'M.filter_glossary.init_filter_autolinking',
48 array(array('courseid' => 0)));
49 $page->requires
->strings_for_js(array('ok'), 'moodle');
53 public function filter($text, array $options = array()) {
54 global $CFG, $USER, $GLOSSARY_EXCLUDEENTRY;
56 // Try to get current course.
57 $coursectx = $this->context
->get_course_context(false);
59 // Only global glossaries will be linked.
62 $courseid = $coursectx->instanceid
;
65 if ($this->cachecourseid
!= $courseid or $this->cacheuserid
!= $USER->id
) {
66 // Invalidate the page cache.
67 $this->cacheconceptlist
= null;
70 if (is_array($this->cacheconceptlist
) and empty($GLOSSARY_EXCLUDEENTRY)) {
71 if (empty($this->cacheconceptlist
)) {
74 return filter_phrases($text, $this->cacheconceptlist
);
77 list($glossaries, $allconcepts) = \mod_glossary\local\concept_cache
::get_concepts($courseid);
80 $this->cacheuserid
= $USER->id
;
81 $this->cachecourseid
= $courseid;
82 $this->cacheconcepts
= array();
86 $strcategory = get_string('category', 'glossary');
88 $conceptlist = array();
91 foreach ($allconcepts as $concepts) {
92 foreach ($concepts as $concept) {
93 if (!empty($GLOSSARY_EXCLUDEENTRY) and $concept->id
== $GLOSSARY_EXCLUDEENTRY) {
97 if ($concept->category
) { // Link to a category.
98 // TODO: Fix this string usage.
99 $title = $glossaries[$concept->glossaryid
] . ': ' . $strcategory . ' ' . $concept->concept
;
100 $link = new moodle_url('/mod/glossary/view.php', array('g' => $concept->glossaryid
, 'mode' => 'cat', 'hook' => $concept->id
));
104 'class' => 'glossary autolink category glossaryid' . $concept->glossaryid
);
106 } else { // Link to entry or alias
107 $title = $glossaries[$concept->glossaryid
] . ': ' . $concept->concept
;
108 // Hardcoding dictionary format in the URL rather than defaulting
109 // to the current glossary format which may not work in a popup.
110 // for example "entry list" means the popup would only contain
111 // a link that opens another popup.
112 $link = new moodle_url('/mod/glossary/showentry.php', array('eid' => $concept->id
, 'displayformat' => 'dictionary'));
115 'title' => str_replace('&', '&', $title), // Undo the s() mangling.
116 'class' => 'glossary autolink concept glossaryid' . $concept->glossaryid
);
118 // This flag is optionally set by resource_pluginfile()
119 // if processing an embedded file use target to prevent getting nested Moodles.
120 if (!empty($CFG->embeddedsoforcelinktarget
)) {
121 $attributes['target'] = '_top';
123 $href_tag_begin = html_writer
::start_tag('a', $attributes);
125 $conceptlist[] = new filterobject($concept->concept
, $href_tag_begin, '</a>',
126 $concept->casesensitive
, $concept->fullmatch
);
130 usort($conceptlist, 'filter_glossary::sort_entries_by_length');
133 // Do not cache the excluded list here, it is used once per page only.
134 $this->cacheuserid
= $USER->id
;
135 $this->cachecourseid
= $courseid;
136 $this->cacheconceptlist
= $conceptlist;
139 if (empty($conceptlist)) {
142 return filter_phrases($text, $conceptlist); // Actually search for concepts!
145 private static function sort_entries_by_length($entry0, $entry1) {
146 $len0 = strlen($entry0->phrase
);
147 $len1 = strlen($entry1->phrase
);
151 } else if ($len0 > $len1) {