Moodle release 2.7beta
[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
20 * found inside every Moodle text
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/**
31 * Glossary filtering
32 *
33 * TODO: erase the $GLOSSARY_EXCLUDECONCEPTS global => require format_text()
34 * to be able to pass arbitrary $options['filteroptions']['glossary'] to filter_text()
85a69ce1
EL
35 */
36class filter_glossary extends moodle_text_filter {
37
3ffb7395
EL
38 public function setup($page, $context) {
39 // This only requires execution once per request.
40 static $jsinitialised = false;
41 if (empty($jsinitialised)) {
42 $page->requires->yui_module(
43 'moodle-filter_glossary-autolinker',
44 'M.filter_glossary.init_filter_autolinking',
45 array(array('courseid' => 0)));
cbb6e0d0 46 $page->requires->strings_for_js(array('ok'), 'moodle');
3ffb7395
EL
47 $jsinitialised = true;
48 }
49 }
50
85a69ce1 51 public function filter($text, array $options = array()) {
3ffb7395 52 global $CFG, $DB, $GLOSSARY_EXCLUDECONCEPTS;
85a69ce1
EL
53
54 // Trivial-cache - keyed on $cachedcontextid
55 static $cachedcontextid;
56 static $conceptlist;
57
85a69ce1
EL
58 static $nothingtodo; // To avoid processing if no glossaries / concepts are found
59
b123a543
FM
60 // Try to get current course.
61 $coursectx = $this->context->get_course_context(false);
62 if (!$coursectx) {
85a69ce1 63 $courseid = 0;
b123a543
FM
64 } else {
65 $courseid = $coursectx->instanceid;
85a69ce1
EL
66 }
67
68 // Initialise/invalidate our trivial cache if dealing with a different context
69 if (!isset($cachedcontextid) || $cachedcontextid !== $this->context->id) {
70 $cachedcontextid = $this->context->id;
71 $conceptlist = array();
72 $nothingtodo = false;
73 }
74
c3963fbd 75 if (($nothingtodo === true) || (!has_capability('mod/glossary:view', $this->context))) {
85a69ce1
EL
76 return $text;
77 }
78
79 // Create a list of all the concepts to search for. It may be cached already.
80 if (empty($conceptlist)) {
81
82 // Find all the glossaries we need to examine
83 if (!$glossaries = $DB->get_records_sql_menu('
84 SELECT g.id, g.name
85 FROM {glossary} g, {course_modules} cm, {modules} m
86 WHERE m.name = \'glossary\'
87 AND cm.module = m.id
88 AND cm.visible = 1
89 AND g.id = cm.instance
90 AND g.usedynalink != 0
91 AND (g.course = ? OR g.globalglossary = 1)
92 ORDER BY g.globalglossary, g.id', array($courseid))) {
93 $nothingtodo = true;
94 return $text;
95 }
96
97 // Make a list of glossary IDs for searching
98 $glossarylist = implode(',', array_keys($glossaries));
99
100 // Pull out all the raw data from the database for entries, categories and aliases
101 $entries = $DB->get_records_select('glossary_entries',
102 'glossaryid IN ('.$glossarylist.') AND usedynalink != 0 AND approved != 0 ', null, '',
103 'id,glossaryid, concept, casesensitive, 0 AS category, fullmatch');
104
105 $categories = $DB->get_records_select('glossary_categories',
106 'glossaryid IN ('.$glossarylist.') AND usedynalink != 0', null, '',
107 'id,glossaryid,name AS concept, 1 AS casesensitive, 1 AS category, 1 AS fullmatch');
108
109 $aliases = $DB->get_records_sql('
110 SELECT ga.id, ge.id AS entryid, ge.glossaryid,
111 ga.alias AS concept, ge.concept AS originalconcept,
112 casesensitive, 0 AS category, fullmatch
113 FROM {glossary_alias} ga,
114 {glossary_entries} ge
115 WHERE ga.entryid = ge.id
116 AND ge.glossaryid IN ('.$glossarylist.')
117 AND ge.usedynalink != 0
118 AND ge.approved != 0', null);
119
120 // Combine them into one big list
121 $concepts = array();
122 if ($entries and $categories) {
123 $concepts = array_merge($entries, $categories);
124 } else if ($categories) {
125 $concepts = $categories;
126 } else if ($entries) {
127 $concepts = $entries;
128 }
129
130 if ($aliases) {
131 $concepts = array_merge($concepts, $aliases);
132 }
133
134 if (!empty($concepts)) {
135 foreach ($concepts as $key => $concept) {
136 // Trim empty or unlinkable concepts
137 $currentconcept = trim(strip_tags($concept->concept));
96c3f75a 138
139 // Concept must be HTML-escaped, so do the same as format_string
140 // to turn ampersands into &amp;.
141 $currentconcept = replace_ampersands_not_followed_by_entity($currentconcept);
142
85a69ce1
EL
143 if (empty($currentconcept)) {
144 unset($concepts[$key]);
145 continue;
146 } else {
147 $concepts[$key]->concept = $currentconcept;
148 }
149
150 // Rule out any small integers. See bug 1446
151 $currentint = intval($currentconcept);
152 if ($currentint && (strval($currentint) == $currentconcept) && $currentint < 1000) {
153 unset($concepts[$key]);
154 }
155 }
156 }
157
158 if (empty($concepts)) {
159 $nothingtodo = true;
160 return $text;
161 }
162
163 usort($concepts, 'filter_glossary::sort_entries_by_length');
164
165 $strcategory = get_string('category', 'glossary');
166
167 // Loop through all the concepts, setting up our data structure for the filter
168 $conceptlist = array(); // We will store all the concepts here
169
170 foreach ($concepts as $concept) {
171 $glossaryname = str_replace(':', '-', $glossaries[$concept->glossaryid]);
172 if ($concept->category) { // Link to a category
d307a1a7 173 // TODO: Fix this string usage
85a69ce1 174 $title = strip_tags($glossaryname.': '.$strcategory.' '.$concept->concept);
d307a1a7 175 $href_tag_begin = '<a class="glossary autolink category glossaryid'.$concept->glossaryid.'" title="'.$title.'" '.
85a69ce1
EL
176 'href="'.$CFG->wwwroot.'/mod/glossary/view.php?g='.$concept->glossaryid.
177 '&amp;mode=cat&amp;hook='.$concept->id.'">';
178 } else { // Link to entry or alias
179 if (!empty($concept->originalconcept)) { // We are dealing with an alias (so show and point to original)
96c3f75a 180 $title = str_replace('"', "'", html_entity_decode(
181 strip_tags($glossaryname.': '.$concept->originalconcept)));
85a69ce1
EL
182 $concept->id = $concept->entryid;
183 } else { // This is an entry
96c3f75a 184 // We need to remove entities from the content here because it
185 // will be escaped by html_writer below.
186 $title = str_replace('"', "'", html_entity_decode(
187 strip_tags($glossaryname.': '.$concept->concept)));
85a69ce1
EL
188 }
189 // hardcoding dictionary format in the URL rather than defaulting
190 // to the current glossary format which may not work in a popup.
191 // for example "entry list" means the popup would only contain
192 // a link that opens another popup.
193 $link = new moodle_url('/mod/glossary/showentry.php', array('courseid'=>$courseid, 'eid'=>$concept->id, 'displayformat'=>'dictionary'));
194 $attributes = array(
195 'href' => $link,
196 'title'=> $title,
d307a1a7 197 'class'=> 'glossary autolink concept glossaryid'.$concept->glossaryid);
85a69ce1
EL
198
199 // this flag is optionally set by resource_pluginfile()
200 // if processing an embedded file use target to prevent getting nested Moodles
201 if (isset($CFG->embeddedsoforcelinktarget) && $CFG->embeddedsoforcelinktarget) {
202 $attributes['target'] = '_top';
203 }
204
205 $href_tag_begin = html_writer::start_tag('a', $attributes);
206 }
207 $conceptlist[] = new filterobject($concept->concept, $href_tag_begin, '</a>',
208 $concept->casesensitive, $concept->fullmatch);
209 }
210
211 $conceptlist = filter_remove_duplicates($conceptlist);
85a69ce1
EL
212 }
213
214 if (!empty($GLOSSARY_EXCLUDECONCEPTS)) {
215 $reducedconceptlist=array();
216 foreach($conceptlist as $concept) {
217 if(!in_array($concept->phrase,$GLOSSARY_EXCLUDECONCEPTS)) {
218 $reducedconceptlist[]=$concept;
219 }
220 }
221 return filter_phrases($text, $reducedconceptlist);
222 }
223
224 return filter_phrases($text, $conceptlist); // Actually search for concepts!
225 }
226
227
228 private static function sort_entries_by_length($entry0, $entry1) {
229 $len0 = strlen($entry0->concept);
230 $len1 = strlen($entry1->concept);
231
232 if ($len0 < $len1) {
233 return 1;
234 } else if ($len0 > $len1) {
235 return -1;
236 } else {
237 return 0;
238 }
239 }
240}