5 * @global moodle_database $DB
6 * @global moodle_page $PAGE
11 function glossary_filter($courseid, $text) {
12 global $CFG, $DB, $GLOSSARY_EXCLUDECONCEPTS, $PAGE;
14 // Trivial-cache - keyed on $cachedcourseid
17 static $cachedcourseid;
18 static $jsinitialised;
20 if (empty($courseid)) {
24 // Initialise/invalidate our trivial cache if dealing with a different course
25 if (!isset($cachedcourseid) ||
$cachedcourseid !== (int)$courseid) {
26 $conceptlist = array();
29 $cachedcourseid = (int)$courseid;
31 if ($nothingtodo === true) {
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
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))) {
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,
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
78 if ($entries and $categories) {
79 $concepts = array_merge($entries, $categories);
80 } else if ($categories) {
81 $concepts = $categories;
82 } else if ($entries) {
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]);
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)) {
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 '&mode=cat&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'));
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
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
);
192 } else if ($len0 > $len1) {