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

/search/documents/glossary_document.php

https://github.com/kpike/moodle
PHP | 287 lines | 140 code | 38 blank | 109 comment | 22 complexity | 0fd1be2cc281b16a4a2ef855993120ba MD5 | raw file
  1. <?php
  2. /**
  3. * Global Search Engine for Moodle
  4. *
  5. * @package search
  6. * @category core
  7. * @subpackage document_wrappers
  8. * @author Michael Campanis (mchampan) [cynnical@gmail.com], Valery Fremaux [valery.fremaux@club-internet.fr] > 1.8
  9. * @contributor Tatsuva Shirai 20090530
  10. * @date 2008/03/31
  11. * @license http://www.gnu.org/copyleft/gpl.html GNU Public License
  12. * @version Moodle 2.0
  13. *
  14. * document handling for glossary activity module
  15. * This file contains a mapping between a glossary entry and it's indexable counterpart,
  16. *
  17. * Functions for iterating and retrieving the necessary records are now also included
  18. * in this file, rather than mod/glossary/lib.php
  19. *
  20. */
  21. /**
  22. * includes and requires
  23. */
  24. require_once($CFG->dirroot.'/search/documents/document.php');
  25. /**
  26. * a class for representing searchable information
  27. *
  28. */
  29. class GlossarySearchDocument extends SearchDocument {
  30. /**
  31. * document constructor
  32. *
  33. */
  34. public function __construct(&$entry, $course_id, $context_id) {
  35. global $DB;
  36. // generic information; required
  37. $doc->docid = $entry['id'];
  38. $doc->documenttype = SEARCH_TYPE_GLOSSARY;
  39. $doc->itemtype = 'standard';
  40. $doc->contextid = $context_id;
  41. $doc->title = $entry['concept'];
  42. $doc->date = $entry['timecreated'];
  43. if ($entry['userid'])
  44. $user = $DB->get_record('user', array('id' => $entry['userid']));
  45. $doc->author = ($user ) ? $user->firstname.' '.$user->lastname : '' ;
  46. $doc->contents = strip_tags($entry['definition']);
  47. $doc->url = glossary_make_link($entry['id']);
  48. // module specific information; optional
  49. $data->glossary = $entry['glossaryid'];
  50. // construct the parent class
  51. parent::__construct($doc, $data, $course_id, -1, $entry['userid'], 'mod/'.SEARCH_TYPE_GLOSSARY);
  52. }
  53. }
  54. /**
  55. * a class for representing searchable information
  56. *
  57. */
  58. class GlossaryCommentSearchDocument extends SearchDocument {
  59. /**
  60. * document constructor
  61. * @uses $DB
  62. */
  63. public function __construct(&$entry, $glossary_id, $course_id, $context_id) {
  64. global $DB;
  65. // generic information; required
  66. $doc->docid = $entry['itemid'];
  67. $doc->documenttype = SEARCH_TYPE_GLOSSARY;
  68. $doc->itemtype = 'comment';
  69. $doc->contextid = $context_id;
  70. $doc->title = get_string('commenton', 'search') . ' ' . $entry['concept'];
  71. $doc->date = $entry['timecreated'];
  72. if ($entry['userid'])
  73. $user = $DB->get_record('user', array('id' => $entry['userid']));
  74. $doc->author = ($user ) ? $user->firstname.' '.$user->lastname : '' ;
  75. $doc->contents = strip_tags($entry['content']);
  76. $doc->url = glossary_make_link($entry['itemid']);
  77. // module specific information; optional
  78. $data->glossary = $glossary_id;
  79. // construct the parent class
  80. parent::__construct($doc, $data, $course_id, -1, $entry['userid'], 'mod/'.SEARCH_TYPE_GLOSSARY);
  81. }
  82. }
  83. /**
  84. * constructs valid access links to information
  85. * @uses $CFG
  86. * @param int $entry_id the id of the glossary entry
  87. * @return a full featured link element as a string
  88. */
  89. function glossary_make_link($entry_id) {
  90. global $CFG;
  91. require_once($CFG->dirroot.'/search/querylib.php');
  92. //links directly to entry
  93. // return $CFG->wwwroot.'/mod/glossary/showentry.php?eid='.$entry_id;
  94. // TOO LONG URL
  95. // Suggestion : bounce on popup within the glossarie's showentry page
  96. // preserve glossary pop-up, be careful where you place your ' and "s
  97. //this function is meant to return a url that is placed between href='[url here]'
  98. $jsondata = array('url'=>'/mod/glossary/showentry.php?eid='.$entry_id,'name'=>'entry','options'=>DEFAULT_POPUP_SETTINGS);
  99. $jsondata = json_encode($jsondata);
  100. return "$CFG->wwwroot/mod/glossary/showentry.php?eid=$entry_id' onclick='return openpopup(null, $jsondata);";
  101. }
  102. /**
  103. * part of search engine API
  104. *
  105. */
  106. function glossary_iterator() {
  107. global $DB;
  108. $glossaries = $DB->get_records('glossary');
  109. return $glossaries;
  110. }
  111. /**
  112. * part of search engine API
  113. * @uses $DB
  114. * @param object $glossary a glossary instance
  115. * @return an array of searchable documents
  116. */
  117. function glossary_get_content_for_index(&$glossary) {
  118. global $DB;
  119. // get context
  120. $coursemodule = $DB->get_field('modules', 'id', array('name' => 'glossary'));
  121. $cm = $DB->get_record('course_modules', array('course' => $glossary->course, 'module' => $coursemodule, 'instance' => $glossary->id));
  122. $context = get_context_instance(CONTEXT_MODULE, $cm->id);
  123. $documents = array();
  124. $entryIds = array();
  125. // index entries
  126. $entries = $DB->get_records('glossary_entries', array('glossaryid' => $glossary->id));
  127. if ($entries){
  128. foreach($entries as $entry) {
  129. $concepts[$entry->id] = $entry->concept;
  130. if (strlen($entry->definition) > 0) {
  131. $entryIds[] = $entry->id;
  132. $documents[] = new GlossarySearchDocument(get_object_vars($entry), $glossary->course, $context->id);
  133. }
  134. }
  135. }
  136. // index comments
  137. if (count($entryIds)){
  138. list($entryidssql, $params) = $DB->get_in_or_equal($entryIds, SQL_PARAMS_NAMED);
  139. $params['ctxid'] = $context->id;
  140. $sql = "SELECT *
  141. FROM {comments}
  142. WHERE contextid = :ctxid
  143. AND itemid $entryidssql";
  144. $comments = $DB->get_recordset_sql($sql, $params);
  145. if ($comments){
  146. foreach($comments as $comment) {
  147. if (strlen($comment->entrycomment) > 0) {
  148. $comment->concept = $concepts[$comment->entryid];
  149. $documents[] = new GlossaryCommentSearchDocument(get_object_vars($comment), $glossary->id, $glossary->course, $context->id);
  150. }
  151. }
  152. }
  153. }
  154. return $documents;
  155. }
  156. /**
  157. * part of search engine API
  158. * @uses $DB
  159. * @param int $id the glossary entry identifier
  160. * @param string $itemtype the type of information
  161. * @return a single search document based on a glossary entry
  162. */
  163. function glossary_single_document($id, $itemtype) {
  164. global $DB;
  165. if ($itemtype == 'standard'){
  166. $entry = $DB->get_record('glossary_entries', array('id' => $id));
  167. }
  168. elseif ($itemtype == 'comment'){
  169. $comment = $DB->get_record('glossary_comments', array('id' => $id));
  170. $entry = $DB->get_record('glossary_entries', array('id' => $comment->entryid));
  171. }
  172. $glossary_course = $DB->get_field('glossary', 'course', array('id' => $entry->glossaryid));
  173. $coursemodule = $DB->get_field('modules', 'id', array('name' => 'glossary'));
  174. $cm = $DB->get_record('course_modules', array('course' => $glossary_course, 'module' => $coursemodule, 'instance' => $entry->glossaryid));
  175. $context = get_context_instance(CONTEXT_MODULE, $cm->id);
  176. if ($itemtype == 'standard'){
  177. return new GlossarySearchDocument(get_object_vars($entry), $glossary_course, $context->id);
  178. }
  179. elseif ($itemtype == 'comment'){
  180. return new GlossaryCommentSearchDocument(get_object_vars($comment), $entry->glossaryid, $glossary_course, $context->id);
  181. }
  182. }
  183. /**
  184. * dummy delete function that packs id with itemtype.
  185. * this was here for a reason, but I can't remember it at the moment.
  186. *
  187. */
  188. function glossary_delete($info, $itemtype) {
  189. $object->id = $info;
  190. $object->itemtype = $itemtype;
  191. return $object;
  192. }
  193. /**
  194. * returns the var names needed to build a sql query for addition/deletions
  195. *
  196. */
  197. function glossary_db_names() {
  198. //[primary id], [table name], [time created field name], [time modified field name]
  199. return array(
  200. array('id', 'glossary_entries', 'timecreated', 'timemodified', 'standard'),
  201. array('id', 'comments', 'timecreated', 'timecreated', 'comment')
  202. );
  203. }
  204. /**
  205. * this function handles the access policy to contents indexed as searchable documents. If this
  206. * function does not exist, the search engine assumes access is allowed.
  207. * When this point is reached, we already know that :
  208. * - user is legitimate in the surrounding context
  209. * - user may be guest and guest access is allowed to the module
  210. * - the function may perform local checks within the module information logic
  211. * @uses $CFG, $DB
  212. * @param string $path the access path to the module script code
  213. * @param string $itemtype the information subclassing (usefull for complex modules, defaults to 'standard')
  214. * @param int $this_id the item id within the information class denoted by itemtype. In glossaries, this id
  215. * points out the indexed glossary item.
  216. * @param object $user the user record denoting the user who searches
  217. * @param int $group_id the current group used by the user when searching
  218. * @param int $context_id the current group used by the user when searching
  219. * @return true if access is allowed, false elsewhere
  220. */
  221. function glossary_check_text_access($path, $itemtype, $this_id, $user, $group_id, $context_id){
  222. global $CFG, $DB;
  223. // get the glossary object and all related stuff
  224. $entry = $DB->get_record('glossary_entries', array('id' => $this_id));
  225. $glossary = $DB->get_record('glossary', array('id' => $entry->glossaryid));
  226. $context = $DB->get_record('context', array('id' => $context_id));
  227. $cm = $DB->get_record('course_modules', array('id' => $context->instanceid));
  228. if (empty($cm)) return false; // Shirai 20090530 - MDL19342 - course module might have been delete
  229. if (!$cm->visible && !has_capability('moodle/course:viewhiddenactivities', $context)) {
  230. return false;
  231. }
  232. //approval check : entries should be approved for being viewed, or belongs to the user unless the viewer can approve them or manage them
  233. if (!$entry->approved && $user != $entry->userid && !has_capability('mod/glossary:approve', $context) && !has_capability('mod/glossary:manageentries', $context)) {
  234. return false;
  235. }
  236. return true;
  237. }
  238. /**
  239. * post processes the url for cleaner output.
  240. * @param string $title
  241. */
  242. function glossary_link_post_processing($title){
  243. global $CFG;
  244. if ($CFG->block_search_utf8dir){
  245. return mb_convert_encoding($title, 'UTF-8', 'auto');
  246. }
  247. return mb_convert_encoding($title, 'auto', 'UTF-8');
  248. }
  249. ?>