"MDL-14932, accessibility imporvement, see tracker"
[moodle-linuxchix.git] / search / documents / techproject_document.php
blob53f5a4a81aea81f038bd2e88cdb1b52f776846d8
1 <?php
2 /**
3 * Global Search Engine for Moodle
5 * @package search
6 * @category core
7 * @subpackage document_wrappers
8 * @author Michael Campanis, 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 techproject activity module
15 /**
16 * requires and includes
18 require_once("$CFG->dirroot/search/documents/document.php");
19 require_once("$CFG->dirroot/mod/techproject/lib.php");
21 /**
22 * a class for representing searchable information
25 class TechprojectEntrySearchDocument extends SearchDocument {
27 /**
28 * constructor
31 public function __construct(&$entry, $course_id, $context_id) {
32 // generic information
33 $doc->docid = $entry['id'];
34 $doc->documenttype = SEARCH_TYPE_TECHPROJECT;
35 $doc->itemtype = $entry['entry_type'];
36 $doc->contextid = $context_id;
39 $doc->title = $entry['abstract'];
40 $doc->author = ($entry['userid']) ? $entry['author'] : '';
41 $doc->contents = strip_tags($entry['description']);
42 $doc->date = '';
44 $doc->url = techproject_make_link($entry['projectid'], $entry['id'], $entry['entry_type'], $entry['groupid']);
46 // module specific information
47 $data->techproject = $entry['projectid'];
49 parent::__construct($doc, $data, $course_id, $entry['groupid'], $entry['userid'], PATH_FOR_SEARCH_TYPE_TECHPROJECT);
50 } //constructor
51 } //TechprojectEntrySearchDocument
53 /**
54 * constructs a valid link to a description detail
57 function techproject_make_link($techproject_id, $entry_id, $entry_type, $group_id) {
58 global $CFG;
59 return $CFG->wwwroot.'/mod/techproject/view.php?view=view_detail&amp;id='.$techproject_id.'&amp;objectId='.$entry_id.'&amp;objectClass='.$entry_type.'&amp;group='.$group_id;
60 } //techproject_make_link
62 /**
63 * search standard API
66 function techproject_iterator() {
67 $techprojects = get_records('techproject');
68 return $techprojects;
69 } //techproject_iterator
71 /**
72 * search standard API
73 * @param techproject a techproject instance
74 * @return an array of collected searchable documents
76 function techproject_get_content_for_index(&$techproject) {
77 $documents = array();
78 if (!$techproject) return $documents;
80 $requirements = techproject_get_entries($techproject->id, 'requirement');
81 $specifications = techproject_get_entries($techproject->id, 'specification');
82 $tasks = techproject_get_tasks($techproject->id);
83 $milestones = techproject_get_entries($techproject->id, 'milestone');
84 $deliverables = techproject_get_entries($techproject->id, 'deliverable');
85 $coursemodule = get_field('modules', 'id', 'name', 'techproject');
86 $cm = get_record('course_modules', 'course', $techproject->course, 'module', $coursemodule, 'instance', $techproject->id);
87 $context = get_context_instance(CONTEXT_MODULE, $cm->id);
89 $entries = @array_merge($requirements, $specifications, $milestones, $deliverables);
90 if ($entries){
91 foreach($entries as $anEntry) {
92 if ($anEntry) {
93 if (strlen($anEntry->description) > 0) {
94 $anEntry->author = '';
95 $documents[] = new TechprojectEntrySearchDocument(get_object_vars($anEntry), $techproject->course, $context->id);
100 if ($tasks){
101 foreach($tasks as $aTask) {
102 if ($aTask) {
103 if (strlen($aTask->description) > 0) {
104 if ($aTask->assignee){
105 $user = get_record('user', 'id', $aTask->assignee);
106 $aTask->author = $user->firstname.' '.$user->lastname;
108 $documents[] = new TechprojectEntrySearchDocument(get_object_vars($aTask), $techproject->course, $context->id);
113 return $documents;
114 } //techproject_get_content_for_index
117 * returns a single techproject search document based on a techproject_entry id and itemtype
120 function techproject_single_document($id, $itemtype) {
121 switch ($itemtype){
122 case 'requirement':{
123 $entry = get_record('techproject_requirement', 'id', $id);
124 $entry->author = '';
125 break;
127 case 'specification':{
128 $entry = get_record('techproject_specification', 'id', $id);
129 $entry->author = '';
130 break;
132 case 'milestone':{
133 $entry = get_record('techproject_milestone', 'id', $id);
134 $entry->author = '';
135 break;
137 case 'deliverable':{
138 $entry = get_record('techproject_deliverable', 'id', $id);
139 $entry->author = '';
140 break;
142 case 'task':{
143 $entry = get_record('techproject_task', 'id', $id);
144 if ($entry->assignee){
145 $user = get_record('user', 'id', $entry->assignee);
146 $entry->author = $user->firstname.' '.$user->lastname;
148 break;
151 $techproject_course = get_field('techproject', 'course', 'id', $entry->projectid);
152 $coursemodule = get_field('modules', 'id', 'name', 'techproject');
153 $cm = get_record('course_modules', 'course', $techproject_course, 'module', $coursemodule, 'instance', $entry->projectid);
154 $context = get_context_instance(CONTEXT_MODULE, $cm->id);
155 $entry->type = $itemtype;
156 $techproject = get_record('techproject', 'id', $requirement->projectid);
157 return new TechprojectEntrySearchDocument(get_object_vars($anEntry), $techproject->course, $context->id);
158 } //techproject_single_document
161 * dummy delete function that packs id with itemtype.
162 * this was here for a reason, but I can't remember it at the moment.
165 function techproject_delete($info, $itemtype) {
166 $object->id = $info;
167 $object->itemtype = $itemtype;
168 return $object;
169 } //techproject_delete
172 * returns the var names needed to build a sql query for addition/deletions
175 // TODO : what should we do there ?
176 function techproject_db_names() {
177 //[primary id], [table name], [time created field name], [time modified field name]
178 return array(
179 array('id', 'techproject_requirement', 'created', 'modified', 'requirement'),
180 array('id', 'techproject_specification', 'created', 'modified', 'specification'),
181 array('id', 'techproject_task', 'created', 'modified', 'task'),
182 array('id', 'techproject_milestone', 'created', 'modified', 'milestone'),
183 array('id', 'techproject_deliverable', 'created', 'modified', 'deliverable')
185 } //techproject_db_names
188 * get a complete list of entries of one particular type
189 * @param techprojectId the project instance
190 * @param type the entity type
191 * @return an array of records
193 function techproject_get_entries($techproject_id, $type) {
194 global $CFG;
196 $query = "
197 SELECT
198 e.id,
199 e.abstract,
200 e.description,
201 e.projectid,
202 e.groupid,
203 e.userid,
204 '$type' AS entry_type
205 FROM
206 {$CFG->prefix}techproject_{$type} AS e
207 WHERE
208 e.projectid = '{$techproject_id}'
210 return get_records_sql($query);
211 } //techproject_get_entries
214 * get the task list for a project instance
215 * @param techprojectId the project
216 * @return an array of records that represent tasks
218 function techproject_get_tasks($techproject_id) {
219 global $CFG;
221 $query = "
222 SELECT
223 t.id,
224 t.abstract,
225 t.description,
226 t.projectid,
227 t.groupid,
228 t.owner as userid,
229 u.firstname,
230 u.lastname,
231 'task' as entry_type
232 FROM
233 {$CFG->prefix}techproject_task AS t
234 LEFT JOIN
235 {$CFG->prefix}user AS u
237 t.owner = u.id
238 WHERE
239 t.projectid = '{$techproject_id}'
240 ORDER BY
241 t.taskstart ASC
243 return get_records_sql($query);
244 } //techproject_get_tasks
247 * this function handles the access policy to contents indexed as searchable documents. If this
248 * function does not exist, the search engine assumes access is allowed.
249 * When this point is reached, we already know that :
250 * - user is legitimate in the surrounding context
251 * - user may be guest and guest access is allowed to the module
252 * - the function may perform local checks within the module information logic
253 * @param path the access path to the module script code
254 * @param entry_type the information subclassing (usefull for complex modules, defaults to 'standard')
255 * @param this_id the item id within the information class denoted by entry_type. In techprojects, this id
256 * points to the techproject instance in which all resources are indexed.
257 * @param user the user record denoting the user who searches
258 * @param group_id the current group used by the user when searching
259 * @return true if access is allowed, false elsewhere
261 function techproject_check_text_access($path, $entry_type, $this_id, $user, $group_id, $context_id){
262 global $CFG;
264 include_once("{$CFG->dirroot}/{$path}/lib.php");
266 // get the techproject object and all related stuff
267 $techproject = get_record('techproject', 'id', $this_id);
268 $course = get_record('course', 'id', $techproject->course);
269 $module_context = get_record('context', 'id', $context_id);
270 $cm = get_record('course_modules', 'id', $module_context->instanceid);
271 if (!$cm->visible and !has_capability('moodle/course:viewhiddenactivities', $module_context)) return false;
273 //group consistency check : checks the following situations about groups
274 // if user is guest check access capabilities for guests :
275 // guests can see default project, and other records if groups are liberal
276 // TODO : change guestsallowed in a capability
277 if (isguest() && $techproject->guestsallowed){
278 if ($group_id && groupmode($course, $cm) == SEPARATEGROUPS)
279 return false;
280 return true;
283 // trap if user is not same group and groups are separated
284 $current_group = get_current_group($course->id);
285 if ((groupmode($course) == SEPARATEGROUPS) && $group_id != $current_group && $group_id) return false;
287 //trap if ungroupedsees is off in strict access mode and user is not teacher
288 if ((groupmode($course) == SEPARATEGROUPS) && !$techproject->ungroupedsees && !$group_id && isteacher($user->id)) return false;
290 return true;
291 } //techproject_check_text_access
294 * this call back is called when displaying the link for some last post processing
297 function techproject_link_post_processing($title){
298 return mb_convert_encoding($title, 'UTF-8', 'auto');