MDL-11082 Improved groups upgrade performance 1.8x -> 1.9; thanks Eloy for telling...
[moodle-pu.git] / mod / glossary / filter.php
blob90ec18b38bde1e5e2e860c47e26db4d6c208bcc3
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 '.
57 'approved != 0 AND concept != \'\'','',
58 'id,glossaryid, concept,casesensitive,0 AS category,fullmatch');
60 $categories = get_records_select('glossary_categories',
61 'glossaryid IN ('.$glossarylist.') AND usedynalink != 0', '',
62 'id,glossaryid,name AS concept, '.
63 '1 AS casesensitive, 1 AS category, 1 AS fullmatch');
65 $aliases = get_records_sql('SELECT ga.id, ge.glossaryid, ga.alias as concept, ge.concept as originalconcept,
66 casesensitive, 0 AS category, fullmatch
67 FROM '.$CFG->prefix.'glossary_alias ga,
68 '.$CFG->prefix.'glossary_entries ge
69 WHERE ga.entryid = ge.id
70 AND ge.glossaryid IN ('.$glossarylist.')
71 AND ge.usedynalink != 0
72 AND ge.approved != 0
73 AND ge.concept != \'\' ');
76 /// Combine them into one big list
77 if ($entries and $categories) {
78 $concepts = array_merge($entries, $categories);
79 } else if ($categories) {
80 $concepts = $categories;
81 } else if ($entries) {
82 $concepts = $entries;
85 if ($aliases) {
86 $concepts = array_merge($concepts, $aliases);
89 if (!empty($concepts)) {
90 foreach ($concepts as $key => $concept) {
91 /// Trim empty or unlinkable concepts
92 $currentconcept = trim(strip_tags($concept->concept));
93 if (empty($currentconcept)) {
94 unset($concepts[$key]);
95 continue;
96 } else {
97 $concepts[$key]->concept = $currentconcept;
100 /// Rule out any small integers. See bug 1446
101 $currentint = intval($currentconcept);
102 if ($currentint && (strval($currentint) == $currentconcept) && $currentint < 1000) {
103 unset($concepts[$key]);
108 if (empty($concepts)) {
109 $nothingtodo = true;
110 return $text;
113 usort($concepts, 'glossary_sort_entries_by_length');
115 $strcategory = get_string('category', 'glossary');
118 /// Loop through all the concepts, setting up our data structure for the filter
120 $conceptlist = array(); /// We will store all the concepts here
122 foreach ($concepts as $concept) {
124 $glossaryname = $glossaries[$concept->glossaryid];
126 if ($concept->category) { // Link to a category
127 $title = strip_tags($glossaryname.': '.$strcategory.' '.$concept->concept);
128 $href_tag_begin = '<a class="glossary autolink glossaryid'.$concept->glossaryid.'" title="'.$title.'" '.
129 'href="'.$CFG->wwwroot.'/mod/glossary/view.php?g='.$concept->glossaryid.
130 '&amp;mode=cat&amp;hook='.$concept->id.'">';
131 } else {
132 if (!empty($concept->originalconcept)) { // We are dealing with an alias (so show original)
133 $encodedconcept = urlencode($concept->originalconcept);
134 $title = str_replace('"', "'", strip_tags($glossaryname.': '.$concept->originalconcept));
135 } else {
136 $encodedconcept = urlencode($concept->concept);
137 $title = str_replace('"', "'", strip_tags($glossaryname.': '.$concept->concept));
139 $href_tag_begin = '<a class="glossary autolink glossaryid'.$concept->glossaryid.'" title="'.$title.'" '.
140 'href="'.$CFG->wwwroot.'/mod/glossary/showentry.php?courseid='.$courseid.
141 '&amp;concept='.$encodedconcept.'" '.
142 'onclick="return openpopup(\'/mod/glossary/showentry.php?courseid='.$courseid.
143 '\&amp;concept='.$encodedconcept.'\', \'entry\', '.
144 '\'menubar=0,location=0,scrollbars,resizable,width=600,height=450\', 0);">';
148 $conceptlist[] = new filterobject($concept->concept, $href_tag_begin, '</a>',
149 $concept->casesensitive, $concept->fullmatch);
152 $conceptlist = filter_remove_duplicates($conceptlist);
155 return filter_phrases($text, $conceptlist); // Actually search for concepts!
159 function glossary_sort_entries_by_length ($entry0, $entry1) {
160 $len0 = strlen($entry0->concept);
161 $len1 = strlen($entry1->concept);
163 if ($len0 < $len1) {
164 return 1;
165 } else if ($len0 > $len1) {
166 return -1;
167 } else {
168 return 0;