Merge commit 'catalyst/MOODLE_19_STABLE' into mdl19-linuxchix
[moodle-linuxchix.git] / search / documents / glossary_document.php
blob2ec59ef8fd6b8adab350f919adc0a816171e8c64
1 <?php
2 /**
3 * Global Search Engine for Moodle
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
12 * document handling for glossary activity module
13 * This file contains a mapping between a glossary entry and it's indexable counterpart,
15 * Functions for iterating and retrieving the necessary records are now also included
16 * in this file, rather than mod/glossary/lib.php
20 /**
21 * includes and requires
23 require_once("$CFG->dirroot/search/documents/document.php");
25 /**
26 * a class for representing searchable information
29 class GlossarySearchDocument extends SearchDocument {
31 /**
32 * document constructor
35 public function __construct(&$entry, $course_id, $context_id) {
36 // generic information; required
37 $doc->docid = $entry['id'];
38 $doc->documenttype = SEARCH_TYPE_GLOSSARY;
39 $doc->itemtype = 'standard';
40 $doc->contextid = $context_id;
42 $doc->title = $entry['concept'];
43 $doc->date = $entry['timecreated'];
45 if ($entry['userid'])
46 $user = get_record('user', 'id', $entry['userid']);
47 $doc->author = ($user ) ? $user->firstname.' '.$user->lastname : '' ;
48 $doc->contents = strip_tags($entry['definition']);
49 $doc->url = glossary_make_link($entry['id']);
51 // module specific information; optional
52 $data->glossary = $entry['glossaryid'];
54 // construct the parent class
55 parent::__construct($doc, $data, $course_id, -1, $entry['userid'], PATH_FOR_SEARCH_TYPE_GLOSSARY);
59 /**
60 * a class for representing searchable information
63 class GlossaryCommentSearchDocument extends SearchDocument {
65 /**
66 * document constructor
68 public function __construct(&$entry, $glossary_id, $course_id, $context_id) {
69 // generic information; required
70 $doc->docid = $entry['id'];
71 $doc->documenttype = SEARCH_TYPE_GLOSSARY;
72 $doc->itemtype = 'comment';
73 $doc->contextid = $context_id;
75 $doc->title = get_string('commenton', 'search') . ' ' . $entry['concept'];
76 $doc->date = $entry['timemodified'];
78 if ($entry['userid'])
79 $user = get_record('user', 'id', $entry['userid']);
80 $doc->author = ($user ) ? $user->firstname.' '.$user->lastname : '' ;
81 $doc->contents = strip_tags($entry['entrycomment']);
82 $doc->url = glossary_make_link($entry['entryid']);
84 // module specific information; optional
85 $data->glossary = $glossary_id;
87 // construct the parent class
88 parent::__construct($doc, $data, $course_id, -1, $entry['userid'], PATH_FOR_SEARCH_TYPE_GLOSSARY);
92 /**
93 * constructs valid access links to information
94 * @param entry_id the id of the glossary entry
95 * @return a full featured link element as a string
97 function glossary_make_link($entry_id) {
98 global $CFG;
100 //links directly to entry
101 // return $CFG->wwwroot.'/mod/glossary/showentry.php?eid='.$entry_id;
103 // TOO LONG URL
104 // Suggestion : bounce on popup within the glossarie's showentry page
105 // preserve glossary pop-up, be careful where you place your ' and "s
106 //this function is meant to return a url that is placed between href='[url here]'
107 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);";
111 * part of search engine API
114 function glossary_iterator() {
115 $glossaries = get_records('glossary');
116 return $glossaries;
120 * part of search engine API
121 * @glossary a glossary instance
122 * @return an array of searchable documents
124 function glossary_get_content_for_index(&$glossary) {
126 // get context
127 $coursemodule = get_field('modules', 'id', 'name', 'glossary');
128 $cm = get_record('course_modules', 'course', $glossary->course, 'module', $coursemodule, 'instance', $glossary->id);
129 $context = get_context_instance(CONTEXT_MODULE, $cm->id);
131 $documents = array();
132 $entryIds = array();
133 // index entries
134 $entries = get_records('glossary_entries', 'glossaryid', $glossary->id);
135 if ($entries){
136 foreach($entries as $entry) {
137 $concepts[$entry->id] = $entry->concept;
138 if (strlen($entry->definition) > 0) {
139 $entryIds[] = $entry->id;
140 $documents[] = new GlossarySearchDocument(get_object_vars($entry), $glossary->course, $context->id);
145 // index comments
146 if (count($entryIds)){
147 $entryIdList = implode(',', $entryIds);
148 $comments = get_records_list('glossary_comments', 'entryid', $entryIdList);
149 if ($comments){
150 foreach($comments as $comment) {
151 if (strlen($comment->entrycomment) > 0) {
152 $comment->concept = $concepts[$comment->entryid];
153 $documents[] = new GlossaryCommentSearchDocument(get_object_vars($comment), $glossary->id, $glossary->course, $context->id);
158 return $documents;
162 * part of search engine API
163 * @param id the glossary entry identifier
164 * @itemtype the type of information
165 * @return a single search document based on a glossary entry
167 function glossary_single_document($id, $itemtype) {
168 if ($itemtype == 'standard'){
169 $entry = get_record('glossary_entries', 'id', $id);
171 elseif ($itemtype == 'comment'){
172 $comment = get_record('glossary_comments', 'id', $id);
173 $entry = get_record('glossary_entries', 'id', $comment->entryid);
175 $glossary_course = get_field('glossary', 'course', 'id', $entry->glossaryid);
176 $coursemodule = get_field('modules', 'id', 'name', 'glossary');
177 $cm = get_record('course_modules', 'course', $glossary_course, 'module', $coursemodule, 'instance', $entry->glossaryid);
178 $context = get_context_instance(CONTEXT_MODULE, $cm->id);
179 if ($itemtype == 'standard'){
180 return new GlossarySearchDocument(get_object_vars($entry), $glossary_course, $context->id);
182 elseif ($itemtype == 'comment'){
183 return new GlossaryCommentSearchDocument(get_object_vars($comment), $entry->glossaryid, $glossary_course, $context->id);
188 * dummy delete function that packs id with itemtype.
189 * this was here for a reason, but I can't remember it at the moment.
192 function glossary_delete($info, $itemtype) {
193 $object->id = $info;
194 $object->itemtype = $itemtype;
195 return $object;
199 * returns the var names needed to build a sql query for addition/deletions
202 function glossary_db_names() {
203 //[primary id], [table name], [time created field name], [time modified field name]
204 return array(
205 array('id', 'glossary_entries', 'timecreated', 'timemodified', 'standard'),
206 array('id', 'glossary_comments', 'timemodified', 'timemodified', 'comment')
211 * this function handles the access policy to contents indexed as searchable documents. If this
212 * function does not exist, the search engine assumes access is allowed.
213 * When this point is reached, we already know that :
214 * - user is legitimate in the surrounding context
215 * - user may be guest and guest access is allowed to the module
216 * - the function may perform local checks within the module information logic
217 * @param string $path the access path to the module script code
218 * @param string $itemtype the information subclassing (usefull for complex modules, defaults to 'standard')
219 * @param int $this_id the item id within the information class denoted by itemtype. In glossaries, this id
220 * points out the indexed glossary item.
221 * @param object $user the user record denoting the user who searches
222 * @param int $group_id the current group used by the user when searching
223 * @param int $context_id the current group used by the user when searching
224 * @return true if access is allowed, false elsewhere
226 function glossary_check_text_access($path, $itemtype, $this_id, $user, $group_id, $context_id){
227 global $CFG;
229 // get the glossary object and all related stuff
230 $entry = get_record('glossary_entries', 'id', $id);
231 $glossary = get_record('glossary', 'id', $entry->glossaryid);
232 $context = get_record('context', 'id', $context_id);
233 $cm = get_record('course_modules', 'id', $context->instanceid);
234 // $cm = get_coursemodule_from_instance('glossary', $glossary->id, $glossary->course);
235 // $context = get_context_instance(CONTEXT_MODULE, $cm->id);
237 if (!$cm->visible && !has_capability('moodle/course:viewhiddenactivities', $context)) {
238 return false;
241 //approval check : entries should be approved for being viewed, or belongs to the user unless the viewer can approve them or manage them
242 if (!$entry->approved && $user != $entry->userid && !has_capability('mod/glossary:approve', $context) && !has_capability('mod/glossary:manageentries', $context)) {
243 return false;
246 return true;
250 * post processes the url for cleaner output.
251 * @param string $title
253 function glossary_link_post_processing($title){
254 return mb_convert_encoding($title, 'UTF-8', 'auto');