MDL-16221
[moodle-linuxchix.git] / mod / glossary / filter.php
blobba4525a440da27c0dc0376eceb2ba2647ad708fe
1 <?php // $Id$
3 function glossary_filter($courseid, $text) {
4 global $CFG;
6 // Trivial-cache - keyed on $cachedcourseid
7 static $nothingtodo;
8 static $conceptlist;
9 static $cachedcourseid;
11 if (empty($courseid)) {
12 $courseid = SITEID;
15 // Initialise/invalidate our trivial cache if dealing with a different course
16 if (!isset($cachedcourseid) || $cachedcourseid !== (int)$courseid) {
17 $conceptlist = array();
18 $nothingtodo = false;
20 $cachedcourseid = (int)$courseid;
22 if ($nothingtodo === true) {
23 return $text;
26 /// Create a list of all the concepts to search for. It may be cached already.
28 if (empty($conceptlist)) {
30 /// Find all the glossaries we need to examine
31 if (!$glossaries = get_records_sql_menu ('SELECT g.id, g.name
32 FROM '.$CFG->prefix.'glossary g,
33 '.$CFG->prefix.'course_modules cm,
34 '.$CFG->prefix.'modules m
35 WHERE m.name = \'glossary\' AND
36 cm.module = m.id AND
37 cm.visible = 1 AND
38 g.id = cm.instance AND
39 g.usedynalink != 0 AND
40 (g.course = \''.$courseid.'\' OR g.globalglossary = 1)
41 ORDER BY g.globalglossary, g.id')) {
42 $nothingtodo = true;
43 return $text;
46 /// Make a list of glossary IDs for searching
47 $glossarylist = '';
48 foreach ($glossaries as $glossaryid => $glossaryname) {
49 $glossarylist .= $glossaryid.',';
51 $glossarylist = substr($glossarylist,0,-1);
54 /// Pull out all the raw data from the database for entries, categories and aliases
55 $entries = get_records_select('glossary_entries',
56 'glossaryid IN ('.$glossarylist.') AND usedynalink != 0 AND approved != 0 ', '',
57 'id,glossaryid, concept, casesensitive, 0 AS category, fullmatch');
59 $categories = get_records_select('glossary_categories',
60 'glossaryid IN ('.$glossarylist.') AND usedynalink != 0', '',
61 'id,glossaryid,name AS concept, 1 AS casesensitive, 1 AS category, 1 AS fullmatch');
63 $aliases = get_records_sql('SELECT ga.id, ge.glossaryid, ga.alias as concept, ge.concept as originalconcept,
64 casesensitive, 0 AS category, fullmatch
65 FROM '.$CFG->prefix.'glossary_alias ga,
66 '.$CFG->prefix.'glossary_entries ge
67 WHERE ga.entryid = ge.id
68 AND ge.glossaryid IN ('.$glossarylist.')
69 AND ge.usedynalink != 0
70 AND ge.approved != 0');
73 /// Combine them into one big list
74 $concepts = array();
75 if ($entries and $categories) {
76 $concepts = array_merge($entries, $categories);
77 } else if ($categories) {
78 $concepts = $categories;
79 } else if ($entries) {
80 $concepts = $entries;
83 if ($aliases) {
84 $concepts = array_merge($concepts, $aliases);
87 if (!empty($concepts)) {
88 foreach ($concepts as $key => $concept) {
89 /// Trim empty or unlinkable concepts
90 $currentconcept = trim(strip_tags($concept->concept));
91 if (empty($currentconcept)) {
92 unset($concepts[$key]);
93 continue;
94 } else {
95 $concepts[$key]->concept = $currentconcept;
98 /// Rule out any small integers. See bug 1446
99 $currentint = intval($currentconcept);
100 if ($currentint && (strval($currentint) == $currentconcept) && $currentint < 1000) {
101 unset($concepts[$key]);
106 if (empty($concepts)) {
107 $nothingtodo = true;
108 return $text;
111 usort($concepts, 'glossary_sort_entries_by_length');
113 $strcategory = get_string('category', 'glossary');
116 /// Loop through all the concepts, setting up our data structure for the filter
118 $conceptlist = array(); /// We will store all the concepts here
120 foreach ($concepts as $concept) {
122 $glossaryname = $glossaries[$concept->glossaryid];
124 if ($concept->category) { // Link to a category
125 $title = strip_tags($glossaryname.': '.$strcategory.' '.$concept->concept);
126 $href_tag_begin = '<a class="glossary autolink glossaryid'.$concept->glossaryid.'" title="'.$title.'" '.
127 'href="'.$CFG->wwwroot.'/mod/glossary/view.php?g='.$concept->glossaryid.
128 '&amp;mode=cat&amp;hook='.$concept->id.'">';
129 } else {
130 if (!empty($concept->originalconcept)) { // We are dealing with an alias (so show original)
131 $encodedconcept = urlencode($concept->originalconcept);
132 $title = str_replace('"', "'", strip_tags($glossaryname.': '.$concept->originalconcept));
133 } else {
134 $encodedconcept = urlencode($concept->concept);
135 $title = str_replace('"', "'", strip_tags($glossaryname.': '.$concept->concept));
137 $href_tag_begin = '<a class="glossary autolink glossaryid'.$concept->glossaryid.'" title="'.$title.'" '.
138 'href="'.$CFG->wwwroot.'/mod/glossary/showentry.php?courseid='.$courseid.
139 '&amp;concept='.$encodedconcept.'" '.
140 'onclick="return openpopup(\'/mod/glossary/showentry.php?courseid='.$courseid.
141 '\&amp;concept='.$encodedconcept.'\', \'entry\', '.
142 '\'menubar=0,location=0,scrollbars,resizable,width=600,height=450\', 0);">';
146 $conceptlist[] = new filterobject($concept->concept, $href_tag_begin, '</a>',
147 $concept->casesensitive, $concept->fullmatch);
150 $conceptlist = filter_remove_duplicates($conceptlist);
153 return filter_phrases($text, $conceptlist); // Actually search for concepts!
157 function glossary_sort_entries_by_length ($entry0, $entry1) {
158 $len0 = strlen($entry0->concept);
159 $len1 = strlen($entry1->concept);
161 if ($len0 < $len1) {
162 return 1;
163 } else if ($len0 > $len1) {
164 return -1;
165 } else {
166 return 0;