PageRenderTime 50ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/filter/glossary/filter.php

https://github.com/plymouthstate/moodle
PHP | 226 lines | 143 code | 33 blank | 50 comment | 33 complexity | 9db5cb34a204a149aaa9914692a5f247 MD5 | raw file
  1. <?php
  2. // This file is part of Moodle - http://moodle.org/
  3. //
  4. // Moodle is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU General Public License as published by
  6. // the Free Software Foundation, either version 3 of the License, or
  7. // (at your option) any later version.
  8. //
  9. // Moodle is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU General Public License
  15. // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
  16. /**
  17. * This filter provides automatic linking to
  18. * glossary entries, aliases and categories when
  19. * found inside every Moodle text
  20. *
  21. * @package filter
  22. * @subpackage glossary
  23. * @copyright 2004 onwards Eloy Lafuente (stronk7) {@link http://stronk7.com}
  24. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  25. */
  26. defined('MOODLE_INTERNAL') || die();
  27. /**
  28. * Glossary filtering
  29. *
  30. * TODO: erase the $GLOSSARY_EXCLUDECONCEPTS global => require format_text()
  31. * to be able to pass arbitrary $options['filteroptions']['glossary'] to filter_text()
  32. */
  33. class filter_glossary extends moodle_text_filter {
  34. public function filter($text, array $options = array()) {
  35. global $CFG, $DB, $GLOSSARY_EXCLUDECONCEPTS, $PAGE;
  36. // Trivial-cache - keyed on $cachedcontextid
  37. static $cachedcontextid;
  38. static $conceptlist;
  39. static $jsinitialised; // To control unique js init
  40. static $nothingtodo; // To avoid processing if no glossaries / concepts are found
  41. // Try to get current course
  42. if (!$courseid = get_courseid_from_context($this->context)) {
  43. $courseid = 0;
  44. }
  45. // Initialise/invalidate our trivial cache if dealing with a different context
  46. if (!isset($cachedcontextid) || $cachedcontextid !== $this->context->id) {
  47. $cachedcontextid = $this->context->id;
  48. $conceptlist = array();
  49. $nothingtodo = false;
  50. }
  51. if ($nothingtodo === true) {
  52. return $text;
  53. }
  54. // Create a list of all the concepts to search for. It may be cached already.
  55. if (empty($conceptlist)) {
  56. // Find all the glossaries we need to examine
  57. if (!$glossaries = $DB->get_records_sql_menu('
  58. SELECT g.id, g.name
  59. FROM {glossary} g, {course_modules} cm, {modules} m
  60. WHERE m.name = \'glossary\'
  61. AND cm.module = m.id
  62. AND cm.visible = 1
  63. AND g.id = cm.instance
  64. AND g.usedynalink != 0
  65. AND (g.course = ? OR g.globalglossary = 1)
  66. ORDER BY g.globalglossary, g.id', array($courseid))) {
  67. $nothingtodo = true;
  68. return $text;
  69. }
  70. // Make a list of glossary IDs for searching
  71. $glossarylist = implode(',', array_keys($glossaries));
  72. // Pull out all the raw data from the database for entries, categories and aliases
  73. $entries = $DB->get_records_select('glossary_entries',
  74. 'glossaryid IN ('.$glossarylist.') AND usedynalink != 0 AND approved != 0 ', null, '',
  75. 'id,glossaryid, concept, casesensitive, 0 AS category, fullmatch');
  76. $categories = $DB->get_records_select('glossary_categories',
  77. 'glossaryid IN ('.$glossarylist.') AND usedynalink != 0', null, '',
  78. 'id,glossaryid,name AS concept, 1 AS casesensitive, 1 AS category, 1 AS fullmatch');
  79. $aliases = $DB->get_records_sql('
  80. SELECT ga.id, ge.id AS entryid, ge.glossaryid,
  81. ga.alias AS concept, ge.concept AS originalconcept,
  82. casesensitive, 0 AS category, fullmatch
  83. FROM {glossary_alias} ga,
  84. {glossary_entries} ge
  85. WHERE ga.entryid = ge.id
  86. AND ge.glossaryid IN ('.$glossarylist.')
  87. AND ge.usedynalink != 0
  88. AND ge.approved != 0', null);
  89. // Combine them into one big list
  90. $concepts = array();
  91. if ($entries and $categories) {
  92. $concepts = array_merge($entries, $categories);
  93. } else if ($categories) {
  94. $concepts = $categories;
  95. } else if ($entries) {
  96. $concepts = $entries;
  97. }
  98. if ($aliases) {
  99. $concepts = array_merge($concepts, $aliases);
  100. }
  101. if (!empty($concepts)) {
  102. foreach ($concepts as $key => $concept) {
  103. // Trim empty or unlinkable concepts
  104. $currentconcept = trim(strip_tags($concept->concept));
  105. if (empty($currentconcept)) {
  106. unset($concepts[$key]);
  107. continue;
  108. } else {
  109. $concepts[$key]->concept = $currentconcept;
  110. }
  111. // Rule out any small integers. See bug 1446
  112. $currentint = intval($currentconcept);
  113. if ($currentint && (strval($currentint) == $currentconcept) && $currentint < 1000) {
  114. unset($concepts[$key]);
  115. }
  116. }
  117. }
  118. if (empty($concepts)) {
  119. $nothingtodo = true;
  120. return $text;
  121. }
  122. usort($concepts, 'filter_glossary::sort_entries_by_length');
  123. $strcategory = get_string('category', 'glossary');
  124. // Loop through all the concepts, setting up our data structure for the filter
  125. $conceptlist = array(); // We will store all the concepts here
  126. foreach ($concepts as $concept) {
  127. $glossaryname = str_replace(':', '-', $glossaries[$concept->glossaryid]);
  128. if ($concept->category) { // Link to a category
  129. // TODO: Fix this string usage
  130. $title = strip_tags($glossaryname.': '.$strcategory.' '.$concept->concept);
  131. $href_tag_begin = '<a class="glossary autolink category glossaryid'.$concept->glossaryid.'" title="'.$title.'" '.
  132. 'href="'.$CFG->wwwroot.'/mod/glossary/view.php?g='.$concept->glossaryid.
  133. '&amp;mode=cat&amp;hook='.$concept->id.'">';
  134. } else { // Link to entry or alias
  135. if (!empty($concept->originalconcept)) { // We are dealing with an alias (so show and point to original)
  136. $title = str_replace('"', "'", strip_tags($glossaryname.': '.$concept->originalconcept));
  137. $concept->id = $concept->entryid;
  138. } else { // This is an entry
  139. $title = str_replace('"', "'", strip_tags($glossaryname.': '.$concept->concept));
  140. }
  141. // hardcoding dictionary format in the URL rather than defaulting
  142. // to the current glossary format which may not work in a popup.
  143. // for example "entry list" means the popup would only contain
  144. // a link that opens another popup.
  145. $link = new moodle_url('/mod/glossary/showentry.php', array('courseid'=>$courseid, 'eid'=>$concept->id, 'displayformat'=>'dictionary'));
  146. $attributes = array(
  147. 'href' => $link,
  148. 'title'=> $title,
  149. 'class'=> 'glossary autolink concept 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';
  154. }
  155. $href_tag_begin = html_writer::start_tag('a', $attributes);
  156. }
  157. $conceptlist[] = new filterobject($concept->concept, $href_tag_begin, '</a>',
  158. $concept->casesensitive, $concept->fullmatch);
  159. }
  160. $conceptlist = filter_remove_duplicates($conceptlist);
  161. if (empty($jsinitialised)) {
  162. // Add a JavaScript event to open popup's here. This only ever need to be
  163. // added once!
  164. $PAGE->requires->yui_module(
  165. 'moodle-filter_glossary-autolinker',
  166. 'M.filter_glossary.init_filter_autolinking',
  167. array(array('courseid' => $courseid)));
  168. $jsinitialised = true;
  169. }
  170. }
  171. if (!empty($GLOSSARY_EXCLUDECONCEPTS)) {
  172. $reducedconceptlist=array();
  173. foreach($conceptlist as $concept) {
  174. if(!in_array($concept->phrase,$GLOSSARY_EXCLUDECONCEPTS)) {
  175. $reducedconceptlist[]=$concept;
  176. }
  177. }
  178. return filter_phrases($text, $reducedconceptlist);
  179. }
  180. return filter_phrases($text, $conceptlist); // Actually search for concepts!
  181. }
  182. private static function sort_entries_by_length($entry0, $entry1) {
  183. $len0 = strlen($entry0->concept);
  184. $len1 = strlen($entry1->concept);
  185. if ($len0 < $len1) {
  186. return 1;
  187. } else if ($len0 > $len1) {
  188. return -1;
  189. } else {
  190. return 0;
  191. }
  192. }
  193. }