Merge branch 'install_21_STABLE' of git://git.moodle.cz/moodle-install into MOODLE_21...
[moodle.git] / mod / glossary / filter.php
blob85090f4f2fcf8efed362e3125719012a90d7f428
1 <?php
3 /**
5 * @global moodle_database $DB
6 * @global moodle_page $PAGE
7 * @param int $courseid
8 * @param string $text
9 * @return string
11 function glossary_filter($courseid, $text) {
12 global $CFG, $DB, $GLOSSARY_EXCLUDECONCEPTS, $PAGE;
14 // Trivial-cache - keyed on $cachedcourseid
15 static $nothingtodo;
16 static $conceptlist;
17 static $cachedcourseid;
18 static $jsinitialised;
20 if (empty($courseid)) {
21 $courseid = SITEID;
24 // Initialise/invalidate our trivial cache if dealing with a different course
25 if (!isset($cachedcourseid) || $cachedcourseid !== (int)$courseid) {
26 $conceptlist = array();
27 $nothingtodo = false;
29 $cachedcourseid = (int)$courseid;
31 if ($nothingtodo === true) {
32 return $text;
35 /// Create a list of all the concepts to search for. It may be cached already.
37 if (empty($conceptlist)) {
39 /// Find all the glossaries we need to examine
40 if (!$glossaries = $DB->get_records_sql_menu('SELECT g.id, g.name
41 FROM {glossary} g, {course_modules} cm, {modules} m
42 WHERE m.name = \'glossary\' AND
43 cm.module = m.id AND
44 cm.visible = 1 AND
45 g.id = cm.instance AND
46 g.usedynalink <> 0 AND
47 (g.course = ? OR g.globalglossary = 1)
48 ORDER BY g.globalglossary, g.id', array($courseid))) {
49 $nothingtodo = true;
50 return $text;
53 /// Make a list of glossary IDs for searching
54 $glossarylist = implode(',', array_keys($glossaries));
57 /// Pull out all the raw data from the database for entries, categories and aliases
58 $entries = $DB->get_records_select('glossary_entries',
59 'glossaryid IN ('.$glossarylist.') AND usedynalink != 0 AND approved != 0 ', null, '',
60 'id,glossaryid, concept, casesensitive, 0 AS category, fullmatch');
62 $categories = $DB->get_records_select('glossary_categories',
63 'glossaryid IN ('.$glossarylist.') AND usedynalink != 0', null, '',
64 'id,glossaryid,name AS concept, 1 AS casesensitive, 1 AS category, 1 AS fullmatch');
66 $aliases = $DB->get_records_sql('SELECT ga.id, ge.id AS entryid, ge.glossaryid, ga.alias AS concept, ge.concept AS originalconcept,
67 casesensitive, 0 AS category, fullmatch
68 FROM {glossary_alias} ga,
69 {glossary_entries} ge
70 WHERE ga.entryid = ge.id
71 AND ge.glossaryid IN ('.$glossarylist.')
72 AND ge.usedynalink != 0
73 AND ge.approved != 0', null);
76 /// Combine them into one big list
77 $concepts = array();
78 if ($entries and $categories) {
79 $concepts = array_merge($entries, $categories);
80 } else if ($categories) {
81 $concepts = $categories;
82 } else if ($entries) {
83 $concepts = $entries;
86 if ($aliases) {
87 $concepts = array_merge($concepts, $aliases);
90 if (!empty($concepts)) {
91 foreach ($concepts as $key => $concept) {
92 /// Trim empty or unlinkable concepts
93 $currentconcept = trim(strip_tags($concept->concept));
94 if (empty($currentconcept)) {
95 unset($concepts[$key]);
96 continue;
97 } else {
98 $concepts[$key]->concept = $currentconcept;
101 /// Rule out any small integers. See bug 1446
102 $currentint = intval($currentconcept);
103 if ($currentint && (strval($currentint) == $currentconcept) && $currentint < 1000) {
104 unset($concepts[$key]);
109 if (empty($concepts)) {
110 $nothingtodo = true;
111 return $text;
114 usort($concepts, 'glossary_sort_entries_by_length');
116 $strcategory = get_string('category', 'glossary');
119 /// Loop through all the concepts, setting up our data structure for the filter
121 $conceptlist = array(); /// We will store all the concepts here
123 foreach ($concepts as $concept) {
125 $glossaryname = str_replace(':', '-', $glossaries[$concept->glossaryid]);
127 if ($concept->category) { // Link to a category
128 $title = strip_tags($glossaryname.': '.$strcategory.' '.$concept->concept);
129 $href_tag_begin = '<a class="glossary autolink glossaryid'.$concept->glossaryid.'" title="'.$title.'" '.
130 'href="'.$CFG->wwwroot.'/mod/glossary/view.php?g='.$concept->glossaryid.
131 '&amp;mode=cat&amp;hook='.$concept->id.'">';
133 } else { // Link to entry or alias
134 if (!empty($concept->originalconcept)) { // We are dealing with an alias (so show and point to original)
135 $title = str_replace('"', "'", strip_tags($glossaryname.': '.$concept->originalconcept));
136 $concept->id = $concept->entryid;
137 } else { // This is an entry
138 $title = str_replace('"', "'", strip_tags($glossaryname.': '.$concept->concept));
140 // hardcoding dictionary format in the URL rather than defaulting
141 // to the current glossary format which may not work in a popup.
142 // for example "entry list" means the popup would only contain
143 // a link that opens another popup.
144 $link = new moodle_url('/mod/glossary/showentry.php', array('courseid'=>$courseid, 'eid'=>$concept->id, 'displayformat'=>'dictionary'));
145 $attributes = array(
146 'href' => $link,
147 'title'=> $title,
148 'class'=> 'glossary autolink glossaryid'.$concept->glossaryid);
150 // this flag is optionally set by resource_pluginfile()
151 // if processing an embedded file use target to prevent getting nested Moodles
152 if (isset($CFG->embeddedsoforcelinktarget) && $CFG->embeddedsoforcelinktarget) {
153 $attributes['target'] = '_top';
156 $href_tag_begin = html_writer::start_tag('a', $attributes);
158 $conceptlist[] = new filterobject($concept->concept, $href_tag_begin, '</a>',
159 $concept->casesensitive, $concept->fullmatch);
162 $conceptlist = filter_remove_duplicates($conceptlist);
164 if (empty($jsinitialised)) {
165 // Add a JavaScript event to open popup's here. This only ever need to be
166 // added once!
167 $PAGE->requires->yui_module('moodle-mod_glossary-autolinker', 'M.mod_glossary.init_filter_autolinking', array(array('courseid'=>$courseid)));
168 $jsinitialised = true;
172 if(!empty($GLOSSARY_EXCLUDECONCEPTS)) {
173 $reducedconceptlist=array();
174 foreach($conceptlist as $concept) {
175 if(!in_array($concept->phrase,$GLOSSARY_EXCLUDECONCEPTS)) {
176 $reducedconceptlist[]=$concept;
179 return filter_phrases($text, $reducedconceptlist);
182 return filter_phrases($text, $conceptlist); // Actually search for concepts!
186 function glossary_sort_entries_by_length ($entry0, $entry1) {
187 $len0 = strlen($entry0->concept);
188 $len1 = strlen($entry1->concept);
190 if ($len0 < $len1) {
191 return 1;
192 } else if ($len0 > $len1) {
193 return -1;
194 } else {
195 return 0;