PageRenderTime 53ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 0ms

/search/documents/glossary_document.php

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