PageRenderTime 26ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/filter/glossary/filter.php

https://bitbucket.org/ngmares/moodle
PHP | 227 lines | 145 code | 33 blank | 49 comment | 34 complexity | c12372317ae6cf9c286363cbd68b7a23 MD5 | raw file
Possible License(s): LGPL-2.1, AGPL-3.0, MPL-2.0-no-copyleft-exception, GPL-3.0, Apache-2.0, BSD-3-Clause
  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 setup($page, $context) {
  35. // This only requires execution once per request.
  36. static $jsinitialised = false;
  37. if (empty($jsinitialised)) {
  38. $page->requires->yui_module(
  39. 'moodle-filter_glossary-autolinker',
  40. 'M.filter_glossary.init_filter_autolinking',
  41. array(array('courseid' => 0)));
  42. $jsinitialised = true;
  43. }
  44. }
  45. public function filter($text, array $options = array()) {
  46. global $CFG, $DB, $GLOSSARY_EXCLUDECONCEPTS;
  47. // Trivial-cache - keyed on $cachedcontextid
  48. static $cachedcontextid;
  49. static $conceptlist;
  50. static $nothingtodo; // To avoid processing if no glossaries / concepts are found
  51. // Try to get current course
  52. if (!$courseid = get_courseid_from_context($this->context)) {
  53. $courseid = 0;
  54. }
  55. // Initialise/invalidate our trivial cache if dealing with a different context
  56. if (!isset($cachedcontextid) || $cachedcontextid !== $this->context->id) {
  57. $cachedcontextid = $this->context->id;
  58. $conceptlist = array();
  59. $nothingtodo = false;
  60. }
  61. if (($nothingtodo === true) || (!has_capability('mod/glossary:view', $this->context))) {
  62. return $text;
  63. }
  64. // Create a list of all the concepts to search for. It may be cached already.
  65. if (empty($conceptlist)) {
  66. // Find all the glossaries we need to examine
  67. if (!$glossaries = $DB->get_records_sql_menu('
  68. SELECT g.id, g.name
  69. FROM {glossary} g, {course_modules} cm, {modules} m
  70. WHERE m.name = \'glossary\'
  71. AND cm.module = m.id
  72. AND cm.visible = 1
  73. AND g.id = cm.instance
  74. AND g.usedynalink != 0
  75. AND (g.course = ? OR g.globalglossary = 1)
  76. ORDER BY g.globalglossary, g.id', array($courseid))) {
  77. $nothingtodo = true;
  78. return $text;
  79. }
  80. // Make a list of glossary IDs for searching
  81. $glossarylist = implode(',', array_keys($glossaries));
  82. // Pull out all the raw data from the database for entries, categories and aliases
  83. $entries = $DB->get_records_select('glossary_entries',
  84. 'glossaryid IN ('.$glossarylist.') AND usedynalink != 0 AND approved != 0 ', null, '',
  85. 'id,glossaryid, concept, casesensitive, 0 AS category, fullmatch');
  86. $categories = $DB->get_records_select('glossary_categories',
  87. 'glossaryid IN ('.$glossarylist.') AND usedynalink != 0', null, '',
  88. 'id,glossaryid,name AS concept, 1 AS casesensitive, 1 AS category, 1 AS fullmatch');
  89. $aliases = $DB->get_records_sql('
  90. SELECT ga.id, ge.id AS entryid, ge.glossaryid,
  91. ga.alias AS concept, ge.concept AS originalconcept,
  92. casesensitive, 0 AS category, fullmatch
  93. FROM {glossary_alias} ga,
  94. {glossary_entries} ge
  95. WHERE ga.entryid = ge.id
  96. AND ge.glossaryid IN ('.$glossarylist.')
  97. AND ge.usedynalink != 0
  98. AND ge.approved != 0', null);
  99. // Combine them into one big list
  100. $concepts = array();
  101. if ($entries and $categories) {
  102. $concepts = array_merge($entries, $categories);
  103. } else if ($categories) {
  104. $concepts = $categories;
  105. } else if ($entries) {
  106. $concepts = $entries;
  107. }
  108. if ($aliases) {
  109. $concepts = array_merge($concepts, $aliases);
  110. }
  111. if (!empty($concepts)) {
  112. foreach ($concepts as $key => $concept) {
  113. // Trim empty or unlinkable concepts
  114. $currentconcept = trim(strip_tags($concept->concept));
  115. if (empty($currentconcept)) {
  116. unset($concepts[$key]);
  117. continue;
  118. } else {
  119. $concepts[$key]->concept = $currentconcept;
  120. }
  121. // Rule out any small integers. See bug 1446
  122. $currentint = intval($currentconcept);
  123. if ($currentint && (strval($currentint) == $currentconcept) && $currentint < 1000) {
  124. unset($concepts[$key]);
  125. }
  126. }
  127. }
  128. if (empty($concepts)) {
  129. $nothingtodo = true;
  130. return $text;
  131. }
  132. usort($concepts, 'filter_glossary::sort_entries_by_length');
  133. $strcategory = get_string('category', 'glossary');
  134. // Loop through all the concepts, setting up our data structure for the filter
  135. $conceptlist = array(); // We will store all the concepts here
  136. foreach ($concepts as $concept) {
  137. $glossaryname = str_replace(':', '-', $glossaries[$concept->glossaryid]);
  138. if ($concept->category) { // Link to a category
  139. // TODO: Fix this string usage
  140. $title = strip_tags($glossaryname.': '.$strcategory.' '.$concept->concept);
  141. $href_tag_begin = '<a class="glossary autolink category glossaryid'.$concept->glossaryid.'" title="'.$title.'" '.
  142. 'href="'.$CFG->wwwroot.'/mod/glossary/view.php?g='.$concept->glossaryid.
  143. '&amp;mode=cat&amp;hook='.$concept->id.'">';
  144. } else { // Link to entry or alias
  145. if (!empty($concept->originalconcept)) { // We are dealing with an alias (so show and point to original)
  146. $title = str_replace('"', "'", strip_tags($glossaryname.': '.$concept->originalconcept));
  147. $concept->id = $concept->entryid;
  148. } else { // This is an entry
  149. $title = str_replace('"', "'", strip_tags($glossaryname.': '.$concept->concept));
  150. }
  151. // hardcoding dictionary format in the URL rather than defaulting
  152. // to the current glossary format which may not work in a popup.
  153. // for example "entry list" means the popup would only contain
  154. // a link that opens another popup.
  155. $link = new moodle_url('/mod/glossary/showentry.php', array('courseid'=>$courseid, 'eid'=>$concept->id, 'displayformat'=>'dictionary'));
  156. $attributes = array(
  157. 'href' => $link,
  158. 'title'=> $title,
  159. 'class'=> 'glossary autolink concept glossaryid'.$concept->glossaryid);
  160. // this flag is optionally set by resource_pluginfile()
  161. // if processing an embedded file use target to prevent getting nested Moodles
  162. if (isset($CFG->embeddedsoforcelinktarget) && $CFG->embeddedsoforcelinktarget) {
  163. $attributes['target'] = '_top';
  164. }
  165. $href_tag_begin = html_writer::start_tag('a', $attributes);
  166. }
  167. $conceptlist[] = new filterobject($concept->concept, $href_tag_begin, '</a>',
  168. $concept->casesensitive, $concept->fullmatch);
  169. }
  170. $conceptlist = filter_remove_duplicates($conceptlist);
  171. }
  172. if (!empty($GLOSSARY_EXCLUDECONCEPTS)) {
  173. $reducedconceptlist=array();
  174. foreach($conceptlist as $concept) {
  175. if(!in_array($concept->phrase,$GLOSSARY_EXCLUDECONCEPTS)) {
  176. $reducedconceptlist[]=$concept;
  177. }
  178. }
  179. return filter_phrases($text, $reducedconceptlist);
  180. }
  181. return filter_phrases($text, $conceptlist); // Actually search for concepts!
  182. }
  183. private static function sort_entries_by_length($entry0, $entry1) {
  184. $len0 = strlen($entry0->concept);
  185. $len1 = strlen($entry1->concept);
  186. if ($len0 < $len1) {
  187. return 1;
  188. } else if ($len0 > $len1) {
  189. return -1;
  190. } else {
  191. return 0;
  192. }
  193. }
  194. }