3 * Global Search Engine for Moodle
4 * add-on 1.8+ : Valery Fremaux [valery.fremaux@club-internet.fr]
7 * document handling for techproject activity module
9 /* see wiki_document.php for descriptions */
11 require_once("$CFG->dirroot/search/documents/document.php");
12 require_once("$CFG->dirroot/mod/techproject/lib.php");
15 * a class for representing searchable information
18 class TechprojectEntrySearchDocument
extends SearchDocument
{
24 public function __construct(&$entry, $course_id, $context_id) {
25 // generic information
26 $doc->docid
= $entry['id'];
27 $doc->documenttype
= SEARCH_TYPE_TECHPROJECT
;
28 $doc->itemtype
= $entry['entry_type'];
29 $doc->contextid
= $context_id;
32 $doc->title
= $entry['abstract'];
33 $doc->author
= ($entry['userid']) ?
$entry['author'] : '';
34 $doc->contents
= strip_tags($entry['description']);
37 $doc->url
= techproject_make_link($entry['projectid'], $entry['id'], $entry['entry_type'], $entry['groupid']);
39 // module specific information
40 $data->techproject
= $entry['projectid'];
42 parent
::__construct($doc, $data, $course_id, $entry['groupid'], $entry['userid'], PATH_FOR_SEARCH_TYPE_TECHPROJECT
);
44 } //TechprojectEntrySearchDocument
47 * constructs a valid link to a description detail
50 function techproject_make_link($techproject_id, $entry_id, $entry_type, $group_id) {
52 return $CFG->wwwroot
.'/mod/techproject/view.php?view=view_detail&id='.$techproject_id.'&objectId='.$entry_id.'&objectClass='.$entry_type.'&group='.$group_id;
53 } //techproject_make_link
59 function techproject_iterator() {
60 $techprojects = get_records('techproject');
62 } //techproject_iterator
66 * @param techproject a techproject instance
67 * @return an array of collected searchable documents
69 function techproject_get_content_for_index(&$techproject) {
71 if (!$techproject) return $documents;
73 $requirements = techproject_get_entries($techproject->id
, 'requirement');
74 $specifications = techproject_get_entries($techproject->id
, 'specification');
75 $tasks = techproject_get_tasks($techproject->id
);
76 $milestones = techproject_get_entries($techproject->id
, 'milestone');
77 $deliverables = techproject_get_entries($techproject->id
, 'deliverable');
78 $coursemodule = get_field('modules', 'id', 'name', 'techproject');
79 $cm = get_record('course_modules', 'course', $techproject->course
, 'module', $coursemodule, 'instance', $techproject->id
);
80 $context = get_context_instance(CONTEXT_MODULE
, $cm->id
);
82 $entries = array_merge($requirements, $specifications, $milestones, $deliverables);
83 foreach($entries as $anEntry) {
85 if (strlen($anEntry->description
) > 0) {
86 $documents[] = new TechprojectEntrySearchDocument(get_object_vars($anEntry), $techproject->course
, $context->id
);
91 foreach($tasks as $aTask) {
93 if (strlen($aTask->description
) > 0) {
94 if ($aTask->assignee
){
95 $user = get_record('user', 'id', $aTask->assignee
);
96 $aTask->author
= $user->firstname
.' '.$user->lastname
;
98 $documents[] = new TechprojectEntrySearchDocument(get_object_vars($aTask), $techproject->course
, $context->id
);
103 } //techproject_get_content_for_index
106 * returns a single techproject search document based on a techproject_entry id and itemtype
109 function techproject_single_document($id, $itemtype) {
112 $entry = get_record('techproject_requirement', 'id', $id);
115 case 'specification':{
116 $entry = get_record('techproject_specification', 'id', $id);
120 $entry = get_record('techproject_milestone', 'id', $id);
124 $entry = get_record('techproject_deliverable', 'id', $id);
128 $entry = get_record('techproject_task', 'id', $id);
129 if ($entry->assignee
){
130 $user = get_record('user', 'id', $entry->assignee
);
131 $entry->author
= $user->firstname
.' '.$user->lastname
;
136 $techprojet_course = get_field('techproject', 'course', 'id', $entry->projectid
);
137 $coursemodule = get_field('modules', 'id', 'name', 'techproject');
138 $cm = get_record('course_modules', 'course', $techproject_course, 'module', $coursemodule, 'instance', $entry->projectid
);
139 $context = get_context_instance(CONTEXT_MODULE
, $cm->id
);
140 $entry->type
= $itemtype;
141 $techproject = get_record('techproject', 'id', $requirement->projectid
);
142 return new TechprojectEntrySearchDocument(get_object_vars($anEntry), $techproject->course
, $context->id
);
143 } //techproject_single_document
146 * dummy delete function that packs id with itemtype.
147 * this was here for a reason, but I can't remember it at the moment.
150 function techproject_delete($info, $itemtype) {
152 $object->itemtype
= $itemtype;
154 } //techproject_delete
157 * returns the var names needed to build a sql query for addition/deletions
160 // TODO : what should we do there ?
161 function techproject_db_names() {
162 //[primary id], [table name], [time created field name], [time modified field name]
164 array('id', 'techproject_requirement', 'created', 'modified', 'requirement'),
165 array('id', 'techproject_specification', 'created', 'modified', 'specification'),
166 array('id', 'techproject_task', 'created', 'modified', 'task'),
167 array('id', 'techproject_milestone', 'created', 'modified', 'milestone'),
168 array('id', 'techproject_deliverable', 'created', 'modified', 'deliverable')
170 } //techproject_db_names
173 * get a complete list of entries of one particular type
174 * @param techprojectId the project instance
175 * @param type the entity type
176 * @return an array of records
178 function techproject_get_entries($techproject_id, $type) {
189 '$type' AS entry_type
191 {$CFG->prefix}techproject_{$type} AS e
193 e.projectid = '{$techproject_id}'
195 return get_records_sql($query);
196 } //techproject_get_entries
199 * get the task list for a project instance
200 * @param techprojectId the project
201 * @return an array of records that represent tasks
203 function techproject_get_tasks($techproject_id) {
218 {$CFG->prefix}techproject_task AS t
220 {$CFG->prefix}user AS u
224 t.projectid = '{$techproject_id}'
228 return get_records_sql($query);
229 } //techproject_get_tasks
232 * this function handles the access policy to contents indexed as searchable documents. If this
233 * function does not exist, the search engine assumes access is allowed.
234 * When this point is reached, we already know that :
235 * - user is legitimate in the surrounding context
236 * - user may be guest and guest access is allowed to the module
237 * - the function may perform local checks within the module information logic
238 * @param path the access path to the module script code
239 * @param entry_type the information subclassing (usefull for complex modules, defaults to 'standard')
240 * @param this_id the item id within the information class denoted by entry_type. In techprojects, this id
241 * points to the techproject instance in which all resources are indexed.
242 * @param user the user record denoting the user who searches
243 * @param group_id the current group used by the user when searching
244 * @return true if access is allowed, false elsewhere
246 function techproject_check_text_access($path, $entry_type, $this_id, $user, $group_id, $context_id){
249 include_once("{$CFG->dirroot}/{$path}/lib.php");
251 // get the techproject object and all related stuff
252 $techproject = get_record('techproject', 'id', $this_id);
253 $course = get_record('course', 'id', $techproject->course
);
254 $module_context = get_record('context', 'id', $context_id);
255 $cm = get_record('course_modules', 'id', $module_context->instance
);
256 if (!$cm->visible
and !has_capability('moodle/course:viewhiddenactivities', $module_context)) return false;
258 //group consistency check : checks the following situations about groups
259 // if user is guest check access capabilities for guests :
260 // guests can see default project, and other records if groups are liberal
261 // TODO : change guestsallowed in a capability
262 if (isguest() && $techproject->guestsallowed
){
263 if ($group_id && groupmode($course, $cm) == SEPARATEGROUPS
)
268 // trap if user is not same group and groups are separated
269 $current_group = get_current_group($course->id
);
270 if ((groupmode($course) == SEPARATEGROUPS
) && $group_id != $current_group && $group_id) return false;
272 //trap if ungroupedsees is off in strict access mode and user is not teacher
273 if ((groupmode($course) == SEPARATEGROUPS
) && !$techproject->ungroupedsees
&& !$group_id && isteacher($user->id
)) return false;
276 } //techproject_check_text_access