Automatic installer.php lang files by installer_builder (20070726)
[moodle-linuxchix.git] / search / documents / glossary_document.php
blobda9e5e78fad2c853354c72777a25fa71a09c9fa5
1 <?php
2 /**
3 * Global Search Engine for Moodle
4 * Michael Champanis (mchampan) [cynnical@gmail.com]
5 * review 1.8+ : Valery Fremaux [valery.fremaux@club-internet.fr]
6 * 2007/08/02
8 * document handling for glossary activity module
9 * This file contains a mapping between a glossary entry and it's indexable counterpart,
11 * Functions for iterating and retrieving the necessary records are now also included
12 * in this file, rather than mod/glossary/lib.php
13 **/
15 require_once("$CFG->dirroot/search/documents/document.php");
17 /*
18 * a class for representing searchable information
20 **/
21 class GlossarySearchDocument extends SearchDocument {
23 /**
24 * document constructor
27 public function __construct(&$entry, $course_id, $context_id) {
28 // generic information; required
29 $doc->docid = $entry['id'];
30 $doc->documenttype = SEARCH_TYPE_GLOSSARY;
31 $doc->itemtype = 'standard';
32 $doc->contextid = $context_id;
34 $doc->title = $entry['concept'];
35 $doc->date = $entry['timecreated'];
37 if ($entry['userid'])
38 $user = get_record('user', 'id', $entry['userid']);
39 $doc->author = ($user ) ? $user->firstname.' '.$user->lastname : '' ;
40 $doc->contents = strip_tags($entry['definition']);
41 $doc->url = glossary_make_link($entry['id']);
43 // module specific information; optional
44 $data->glossary = $entry['glossaryid'];
46 // construct the parent class
47 parent::__construct($doc, $data, $course_id, -1, $entry['userid'], PATH_FOR_SEARCH_TYPE_GLOSSARY);
48 } //constructor
49 } //GlossarySearchDocument
51 /*
52 * a class for representing searchable information
54 **/
55 class GlossaryCommentSearchDocument extends SearchDocument {
57 /**
58 * document constructor
61 public function __construct(&$entry, $glossary_id, $course_id, $context_id) {
62 // generic information; required
63 $doc->docid = $entry['id'];
64 $doc->documenttype = SEARCH_TYPE_GLOSSARY;
65 $doc->itemtype = 'comment';
66 $doc->contextid = $context_id;
68 $doc->title = get_string('commenton', 'search') . ' ' . $entry['concept'];
69 $doc->date = $entry['timemodified'];
71 if ($entry['userid'])
72 $user = get_record('user', 'id', $entry['userid']);
73 $doc->author = ($user ) ? $user->firstname.' '.$user->lastname : '' ;
74 $doc->contents = strip_tags($entry['entrycomment']);
75 $doc->url = glossary_make_link($entry['entryid']);
77 // module specific information; optional
78 $data->glossary = $glossary_id;
80 // construct the parent class
81 parent::__construct($doc, $data, $course_id, -1, $entry['userid'], PATH_FOR_SEARCH_TYPE_GLOSSARY);
82 } //constructor
83 } //GlossaryCommentSearchDocument
85 /**
86 * constructs valid access links to information
87 * @param entry_id the id of the glossary entry
88 * @return a full featured link element as a string
90 function glossary_make_link($entry_id) {
91 global $CFG;
93 //links directly to entry
94 // return $CFG->wwwroot.'/mod/glossary/showentry.php?eid='.$entry_id;
96 // TOO LONG URL
97 // Suggestion : bounce on popup within the glossarie's showentry page
98 // preserve glossary pop-up, be careful where you place your ' and "s
99 //this function is meant to return a url that is placed between href='[url here]'
100 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);";
101 } //glossary_make_link
104 * part of search engine API
107 function glossary_iterator() {
108 $glossaries = get_records('glossary');
109 return $glossaries;
110 } //glossary_iterator
113 * part of search engine API
114 * @glossary a glossary instance
115 * @return an array of searchable documents
117 function glossary_get_content_for_index(&$glossary) {
119 // get context
120 $coursemodule = get_field('modules', 'id', 'name', 'glossary');
121 $cm = get_record('course_modules', 'course', $glossary->course, 'module', $coursemodule, 'instance', $glossary->id);
122 $context = get_context_instance(CONTEXT_MODULE, $cm->id);
124 $documents = array();
125 $entryIds = array();
126 // index entries
127 $entries = get_records('glossary_entries', 'glossaryid', $glossary->id);
128 if ($entries){
129 foreach($entries as $entry) {
130 $concepts[$entry->id] = $entry->concept;
131 if (strlen($entry->definition) > 0) {
132 $entryIds[] = $entry->id;
133 $documents[] = new GlossarySearchDocument(get_object_vars($entry), $glossary->course, $context->id);
138 // index comments
139 if (count($entryIds)){
140 $entryIdList = implode(',', $entryIds);
141 $comments = get_records_list('glossary_comments', 'entryid', $entryIdList);
142 if ($comments){
143 foreach($comments as $comment) {
144 if (strlen($comment->entrycomment) > 0) {
145 $comment->concept = $concepts[$comment->entryid];
146 $documents[] = new GlossaryCommentSearchDocument(get_object_vars($comment), $glossary->id, $glossary->course, $context->id);
151 return $documents;
152 } //glossary_get_content_for_index
155 * part of search engine API
156 * @param id the glossary entry identifier
157 * @itemtype the type of information
158 * @return a single search document based on a glossary entry
160 function glossary_single_document($id, $itemtype) {
161 if ($itemtype == 'standard'){
162 $entry = get_record('glossary_entries', 'id', $id);
164 elseif ($itemtype == 'comment'){
165 $comment = get_record('glossary_comments', 'id', $id);
166 $entry = get_record('glossary_entries', 'id', $comment->entryid);
168 $glossary_course = get_field('glossary', 'course', 'id', $entry->glossaryid);
169 $coursemodule = get_field('modules', 'id', 'name', 'glossary');
170 $cm = get_record('course_modules', 'course', $glossary_course, 'module', $coursemodule, 'instance', $entry->glossaryid);
171 $context = get_context_instance(CONTEXT_MODULE, $cm->id);
172 if ($itemtype == 'standard'){
173 return new GlossarySearchDocument(get_object_vars($entry), $glossary_course, $context->id);
175 elseif ($itemtype == 'comment'){
176 return new GlossaryCommentSearchDocument(get_object_vars($comment), $entry->glossaryid, $glossary_course, $context->id);
178 } //glossary_single_document
181 * dummy delete function that packs id with itemtype.
182 * this was here for a reason, but I can't remember it at the moment.
185 function glossary_delete($info, $itemtype) {
186 $object->id = $info;
187 $object->itemtype = $itemtype;
188 return $object;
189 } //glossary_delete
192 * returns the var names needed to build a sql query for addition/deletions
195 function glossary_db_names() {
196 //[primary id], [table name], [time created field name], [time modified field name]
197 return array(
198 array('id', 'glossary_entries', 'timecreated', 'timemodified', 'standard'),
199 array('id', 'glossary_comments', 'timemodified', 'timemodified', 'comment')
201 } //glossary_db_names
204 * this function handles the access policy to contents indexed as searchable documents. If this
205 * function does not exist, the search engine assumes access is allowed.
206 * When this point is reached, we already know that :
207 * - user is legitimate in the surrounding context
208 * - user may be guest and guest access is allowed to the module
209 * - the function may perform local checks within the module information logic
210 * @param path the access path to the module script code
211 * @param itemtype the information subclassing (usefull for complex modules, defaults to 'standard')
212 * @param this_id the item id within the information class denoted by itemtype. In glossaries, this id
213 * points out the indexed glossary item.
214 * @param user the user record denoting the user who searches
215 * @param group_id the current group used by the user when searching
216 * @return true if access is allowed, false elsewhere
218 function glossary_check_text_access($path, $itemtype, $this_id, $user, $group_id, $context_id){
219 global $CFG;
221 // get the glossary object and all related stuff
222 $entry = get_record('glossary_entries', 'id', $id);
223 $glossary = get_record('glossary', 'id', $entry->glossaryid);
224 $course = get_record('course', 'id', $glossary->course);
225 $module_context = get_record('context', 'id', $context_id);
226 $cm = get_record('course_modules', 'id', $module_context->instance);
227 if (!$cm->visible && !has_capability('moodle/course:viewhiddenactivities', $module_context)) return false;
229 //approval check : entries should be approved for being viewed, or belongs to the user unless the viewer can approve them or manage them
230 if (!$entry->approved && $user != $entry->userid && !has_capability('mod/glossary:approve', $module_context) && !has_capability('mod/glossary:manageentries', $module_context)) return false;
232 return true;
233 } //glossary_check_text_access