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();
33 * TODO: erase the $GLOSSARY_EXCLUDECONCEPTS global => require format_text()
34 * to be able to pass arbitrary $options['filteroptions']['glossary'] to filter_text()
36 class filter_glossary
extends moodle_text_filter
{
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)));
46 $jsinitialised = true;
50 public function filter($text, array $options = array()) {
51 global $CFG, $DB, $GLOSSARY_EXCLUDECONCEPTS;
53 // Trivial-cache - keyed on $cachedcontextid
54 static $cachedcontextid;
57 static $nothingtodo; // To avoid processing if no glossaries / concepts are found
59 // Try to get current course
60 if (!$courseid = get_courseid_from_context($this->context
)) {
64 // Initialise/invalidate our trivial cache if dealing with a different context
65 if (!isset($cachedcontextid) ||
$cachedcontextid !== $this->context
->id
) {
66 $cachedcontextid = $this->context
->id
;
67 $conceptlist = array();
71 if (($nothingtodo === true) ||
(!has_capability('mod/glossary:view', $this->context
))) {
75 // Create a list of all the concepts to search for. It may be cached already.
76 if (empty($conceptlist)) {
78 // Find all the glossaries we need to examine
79 if (!$glossaries = $DB->get_records_sql_menu('
81 FROM {glossary} g, {course_modules} cm, {modules} m
82 WHERE m.name = \'glossary\'
85 AND g.id = cm.instance
86 AND g.usedynalink != 0
87 AND (g.course = ? OR g.globalglossary = 1)
88 ORDER BY g.globalglossary, g.id', array($courseid))) {
93 // Make a list of glossary IDs for searching
94 $glossarylist = implode(',', array_keys($glossaries));
96 // Pull out all the raw data from the database for entries, categories and aliases
97 $entries = $DB->get_records_select('glossary_entries',
98 'glossaryid IN ('.$glossarylist.') AND usedynalink != 0 AND approved != 0 ', null, '',
99 'id,glossaryid, concept, casesensitive, 0 AS category, fullmatch');
101 $categories = $DB->get_records_select('glossary_categories',
102 'glossaryid IN ('.$glossarylist.') AND usedynalink != 0', null, '',
103 'id,glossaryid,name AS concept, 1 AS casesensitive, 1 AS category, 1 AS fullmatch');
105 $aliases = $DB->get_records_sql('
106 SELECT ga.id, ge.id AS entryid, ge.glossaryid,
107 ga.alias AS concept, ge.concept AS originalconcept,
108 casesensitive, 0 AS category, fullmatch
109 FROM {glossary_alias} ga,
110 {glossary_entries} ge
111 WHERE ga.entryid = ge.id
112 AND ge.glossaryid IN ('.$glossarylist.')
113 AND ge.usedynalink != 0
114 AND ge.approved != 0', null);
116 // Combine them into one big list
118 if ($entries and $categories) {
119 $concepts = array_merge($entries, $categories);
120 } else if ($categories) {
121 $concepts = $categories;
122 } else if ($entries) {
123 $concepts = $entries;
127 $concepts = array_merge($concepts, $aliases);
130 if (!empty($concepts)) {
131 foreach ($concepts as $key => $concept) {
132 // Trim empty or unlinkable concepts
133 $currentconcept = trim(strip_tags($concept->concept
));
135 // Concept must be HTML-escaped, so do the same as print_string
136 // to turn ampersands into &.
137 $currentconcept = replace_ampersands_not_followed_by_entity($currentconcept);
139 if (empty($currentconcept)) {
140 unset($concepts[$key]);
143 $concepts[$key]->concept
= $currentconcept;
146 // Rule out any small integers. See bug 1446
147 $currentint = intval($currentconcept);
148 if ($currentint && (strval($currentint) == $currentconcept) && $currentint < 1000) {
149 unset($concepts[$key]);
154 if (empty($concepts)) {
159 usort($concepts, 'filter_glossary::sort_entries_by_length');
161 $strcategory = get_string('category', 'glossary');
163 // Loop through all the concepts, setting up our data structure for the filter
164 $conceptlist = array(); // We will store all the concepts here
166 foreach ($concepts as $concept) {
167 $glossaryname = str_replace(':', '-', $glossaries[$concept->glossaryid
]);
168 if ($concept->category
) { // Link to a category
169 // TODO: Fix this string usage
170 $title = strip_tags($glossaryname.': '.$strcategory.' '.$concept->concept
);
171 $href_tag_begin = '<a class="glossary autolink category glossaryid'.$concept->glossaryid
.'" title="'.$title.'" '.
172 'href="'.$CFG->wwwroot
.'/mod/glossary/view.php?g='.$concept->glossaryid
.
173 '&mode=cat&hook='.$concept->id
.'">';
174 } else { // Link to entry or alias
175 if (!empty($concept->originalconcept
)) { // We are dealing with an alias (so show and point to original)
176 $title = str_replace('"', "'", html_entity_decode(
177 strip_tags($glossaryname.': '.$concept->originalconcept
)));
178 $concept->id
= $concept->entryid
;
179 } else { // This is an entry
180 // We need to remove entities from the content here because it
181 // will be escaped by html_writer below.
182 $title = str_replace('"', "'", html_entity_decode(
183 strip_tags($glossaryname.': '.$concept->concept
)));
185 // hardcoding dictionary format in the URL rather than defaulting
186 // to the current glossary format which may not work in a popup.
187 // for example "entry list" means the popup would only contain
188 // a link that opens another popup.
189 $link = new moodle_url('/mod/glossary/showentry.php', array('courseid'=>$courseid, 'eid'=>$concept->id
, 'displayformat'=>'dictionary'));
193 'class'=> 'glossary autolink concept glossaryid'.$concept->glossaryid
);
195 // this flag is optionally set by resource_pluginfile()
196 // if processing an embedded file use target to prevent getting nested Moodles
197 if (isset($CFG->embeddedsoforcelinktarget
) && $CFG->embeddedsoforcelinktarget
) {
198 $attributes['target'] = '_top';
201 $href_tag_begin = html_writer
::start_tag('a', $attributes);
203 $conceptlist[] = new filterobject($concept->concept
, $href_tag_begin, '</a>',
204 $concept->casesensitive
, $concept->fullmatch
);
207 $conceptlist = filter_remove_duplicates($conceptlist);
210 if (!empty($GLOSSARY_EXCLUDECONCEPTS)) {
211 $reducedconceptlist=array();
212 foreach($conceptlist as $concept) {
213 if(!in_array($concept->phrase
,$GLOSSARY_EXCLUDECONCEPTS)) {
214 $reducedconceptlist[]=$concept;
217 return filter_phrases($text, $reducedconceptlist);
220 return filter_phrases($text, $conceptlist); // Actually search for concepts!
224 private static function sort_entries_by_length($entry0, $entry1) {
225 $len0 = strlen($entry0->concept
);
226 $len1 = strlen($entry1->concept
);
230 } else if ($len0 > $len1) {