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