3 function glossary_filter($courseid, $text) {
6 // Trivial-cache - keyed on $cachedcourseid
9 static $cachedcourseid;
11 if (empty($courseid)) {
15 // Initialise/invalidate our trivial cache if dealing with a different course
16 if (!isset($cachedcourseid) ||
$cachedcourseid !== (int)$courseid) {
17 $conceptlist = array();
20 $cachedcourseid = (int)$courseid;
22 if ($nothingtodo === true) {
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
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')) {
46 /// Make a list of glossary IDs for searching
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
75 if ($entries and $categories) {
76 $concepts = array_merge($entries, $categories);
77 } else if ($categories) {
78 $concepts = $categories;
79 } else if ($entries) {
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]);
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)) {
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 '&mode=cat&hook='.$concept->id
.'">';
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
));
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 '&concept='.$encodedconcept.'" '.
140 'onclick="return openpopup(\'/mod/glossary/showentry.php?courseid='.$courseid.
141 '\&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
);
163 } else if ($len0 > $len1) {