Merge branch 'MDL-61658-33-allcountrycodes' of git://github.com/mudrd8mz/moodle into...
[moodle.git] / filter / glossary / filter.php
blob5c4ba448122d4b668c0e87b4d02241c37161017d
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.
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/>.
17 /**
18 * This filter provides automatic linking to
19 * glossary entries, aliases and categories when
20 * found inside every Moodle text.
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
28 defined('MOODLE_INTERNAL') || die();
30 /**
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);
58 if (!$coursectx) {
59 // Only global glossaries will be linked.
60 $courseid = 0;
61 } else {
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)) {
72 return $text;
74 return filter_phrases($text, $this->cacheconceptlist);
77 list($glossaries, $allconcepts) = \mod_glossary\local\concept_cache::get_concepts($courseid);
79 if (!$allconcepts) {
80 $this->cacheuserid = $USER->id;
81 $this->cachecourseid = $courseid;
82 $this->cacheconcepts = array();
83 return $text;
86 $strcategory = get_string('category', 'glossary');
88 $conceptlist = array();
89 $excluded = false;
91 foreach ($allconcepts as $concepts) {
92 foreach ($concepts as $concept) {
93 if (!empty($GLOSSARY_EXCLUDEENTRY) and $concept->id == $GLOSSARY_EXCLUDEENTRY) {
94 $excluded = true;
95 continue;
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);
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'));
113 $attributes = array(
114 'href' => $link,
115 'title' => str_replace('&amp;', '&', $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');
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;
139 if (empty($conceptlist)) {
140 return $text;
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);
149 if ($len0 < $len1) {
150 return 1;
151 } else if ($len0 > $len1) {
152 return -1;
153 } else {
154 return 0;