Merge branch 'MDL-46867' of git://github.com/timhunt/moodle
[moodle.git] / filter / glossary / filter.php
CommitLineData
85a69ce1
EL
1<?php
2// This file is part of Moodle - http://moodle.org/
3//
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.
8//
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.
13//
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/>.
16
17/**
18 * This filter provides automatic linking to
19 * glossary entries, aliases and categories when
37625e2a 20 * found inside every Moodle text.
85a69ce1
EL
21 *
22 * @package filter
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
26 */
27
28defined('MOODLE_INTERNAL') || die();
29
30/**
37625e2a 31 * Glossary linking filter class.
85a69ce1 32 *
37625e2a 33 * NOTE: multilang glossary entries are not compatible with this filter.
85a69ce1
EL
34 */
35class filter_glossary extends moodle_text_filter {
37625e2a
PS
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;
85a69ce1 42
3ffb7395 43 public function setup($page, $context) {
062d04cf 44 if ($page->requires->should_create_one_time_item_now('filter_glossary_autolinker')) {
3ffb7395
EL
45 $page->requires->yui_module(
46 'moodle-filter_glossary-autolinker',
47 'M.filter_glossary.init_filter_autolinking',
48 array(array('courseid' => 0)));
cbb6e0d0 49 $page->requires->strings_for_js(array('ok'), 'moodle');
3ffb7395
EL
50 }
51 }
52
85a69ce1 53 public function filter($text, array $options = array()) {
37625e2a 54 global $CFG, $USER, $GLOSSARY_EXCLUDEENTRY;
85a69ce1 55
b123a543
FM
56 // Try to get current course.
57 $coursectx = $this->context->get_course_context(false);
58 if (!$coursectx) {
37625e2a 59 // Only global glossaries will be linked.
85a69ce1 60 $courseid = 0;
b123a543 61 } else {
1e83288d 62 $courseid = $coursectx->instanceid;
85a69ce1
EL
63 }
64
1e83288d 65 if ($this->cachecourseid != $courseid or $this->cacheuserid != $USER->id) {
37625e2a
PS
66 // Invalidate the page cache.
67 $this->cacheconceptlist = null;
85a69ce1
EL
68 }
69
37625e2a
PS
70 if (is_array($this->cacheconceptlist) and empty($GLOSSARY_EXCLUDEENTRY)) {
71 if (empty($this->cacheconceptlist)) {
85a69ce1
EL
72 return $text;
73 }
37625e2a
PS
74 return filter_phrases($text, $this->cacheconceptlist);
75 }
85a69ce1 76
1e83288d 77 list($glossaries, $allconcepts) = \mod_glossary\local\concept_cache::get_concepts($courseid);
85a69ce1 78
37625e2a
PS
79 if (!$allconcepts) {
80 $this->cacheuserid = $USER->id;
81 $this->cachecourseid = $courseid;
82 $this->cacheconcepts = array();
83 return $text;
84 }
85a69ce1 85
37625e2a 86 $strcategory = get_string('category', 'glossary');
85a69ce1 87
37625e2a
PS
88 $conceptlist = array();
89 $excluded = false;
85a69ce1 90
37625e2a 91 foreach ($allconcepts as $concepts) {
85a69ce1 92 foreach ($concepts as $concept) {
37625e2a
PS
93 if (!empty($GLOSSARY_EXCLUDEENTRY) and $concept->id == $GLOSSARY_EXCLUDEENTRY) {
94 $excluded = true;
95 continue;
96 }
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));
101 $attributes = array(
102 'href' => $link,
103 'title' => $title,
104 'class' => 'glossary autolink category glossaryid' . $concept->glossaryid);
105
85a69ce1 106 } else { // Link to entry or alias
37625e2a
PS
107 $title = $glossaries[$concept->glossaryid] . ': ' . $concept->concept;
108 // Hardcoding dictionary format in the URL rather than defaulting
85a69ce1
EL
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.
37625e2a 112 $link = new moodle_url('/mod/glossary/showentry.php', array('eid' => $concept->id, 'displayformat' => 'dictionary'));
85a69ce1 113 $attributes = array(
37625e2a
PS
114 'href' => $link,
115 'title' => str_replace('&amp;', '&', $title), // Undo the s() mangling.
116 'class' => 'glossary autolink concept glossaryid' . $concept->glossaryid);
85a69ce1 117 }
37625e2a
PS
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';
122 }
123 $href_tag_begin = html_writer::start_tag('a', $attributes);
124
85a69ce1
EL
125 $conceptlist[] = new filterobject($concept->concept, $href_tag_begin, '</a>',
126 $concept->casesensitive, $concept->fullmatch);
127 }
85a69ce1
EL
128 }
129
37625e2a
PS
130 usort($conceptlist, 'filter_glossary::sort_entries_by_length');
131
132 if (!$excluded) {
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;
85a69ce1
EL
137 }
138
37625e2a
PS
139 if (empty($conceptlist)) {
140 return $text;
141 }
85a69ce1
EL
142 return filter_phrases($text, $conceptlist); // Actually search for concepts!
143 }
144
85a69ce1 145 private static function sort_entries_by_length($entry0, $entry1) {
37625e2a
PS
146 $len0 = strlen($entry0->phrase);
147 $len1 = strlen($entry1->phrase);
85a69ce1
EL
148
149 if ($len0 < $len1) {
150 return 1;
151 } else if ($len0 > $len1) {
152 return -1;
153 } else {
154 return 0;
155 }
156 }
157}