3 * Global Search Engine for Moodle
7 * @subpackage document_wrappers
8 * @author Valery Fremaux [valery.fremaux@club-internet.fr] > 1.9
9 * @contributor Tatsuva Shirai 20090530
11 * @license http://www.gnu.org/copyleft/gpl.html GNU Public License
13 * document handling for all resources
14 * This file contains the mapping between a resource and it's indexable counterpart,
16 * Functions for iterating and retrieving the necessary records are now also included
17 * in this file, rather than mod/resource/lib.php
21 * requires and includes
23 require_once("$CFG->dirroot/search/documents/document.php");
24 require_once("$CFG->dirroot/mod/resource/lib.php");
27 * a class for representing searchable information
30 class LabelSearchDocument
extends SearchDocument
{
31 public function __construct(&$label, $context_id) {
32 // generic information; required
33 $doc->docid
= $label['id'];
34 $doc->documenttype
= SEARCH_TYPE_LABEL
;
35 $doc->itemtype
= 'label';
36 $doc->contextid
= $context_id;
38 $doc->title
= strip_tags($label['name']);
39 $doc->date
= $label['timemodified'];
41 $doc->contents
= strip_tags($label['name']);
42 $doc->url
= label_make_link($label['course']);
44 // module specific information; optional
47 // construct the parent class
48 parent
::__construct($doc, $data, $label['course'], 0, 0, 'mod/'.SEARCH_TYPE_LABEL
);
53 * constructs valid access links to information
54 * @param resourceId the of the resource
55 * @return a full featured link element as a string
57 function label_make_link($course_id) {
60 return $CFG->wwwroot
.'/course/view.php?id='.$course_id;
64 * part of standard API
67 function label_iterator() {
68 //trick to leave search indexer functionality intact, but allow
69 //this document to only use the below function to return info
71 $labels = get_records('label');
76 * part of standard API
77 * this function does not need a content iterator, returns all the info
79 * @param notneeded to comply API, remember to fake the iterator array though
81 * @return an array of searchable documents
83 function label_get_content_for_index(&$label) {
86 // starting with Moodle native resources
89 $coursemodule = get_field('modules', 'id', 'name', 'label');
90 $cm = get_record('course_modules', 'course', $label->course
, 'module', $coursemodule, 'instance', $label->id
);
91 $context = get_context_instance(CONTEXT_MODULE
, $cm->id
);
93 $documents[] = new LabelSearchDocument(get_object_vars($label), $context->id
);
95 mtrace("finished label {$label->id}");
100 * part of standard API.
101 * returns a single resource search document based on a label id
102 * @param id the id of the accessible document
103 * @return a searchable object or null if failure
105 function label_single_document($id, $itemtype) {
108 $label = get_record('label', 'id', $id);
111 $coursemodule = get_field('modules', 'id', 'name', 'label');
112 $cm = get_record('course_modules', 'id', $label->id
);
113 $context = get_context_instance(CONTEXT_MODULE
, $cm->id
);
114 return new LabelSearchDocument(get_object_vars($label), $context->id
);
120 * dummy delete function that aggregates id with itemtype.
121 * this was here for a reason, but I can't remember it at the moment.
124 function label_delete($info, $itemtype) {
126 $object->itemtype
= $itemtype;
131 * returns the var names needed to build a sql query for addition/deletions
134 function label_db_names() {
135 //[primary id], [table name], [time created field name], [time modified field name], [docsubtype], [additional where conditions for sql]
136 return array(array('id', 'label', 'timemodified', 'timemodified', 'label', ''));
140 * this function handles the access policy to contents indexed as searchable documents. If this
141 * function does not exist, the search engine assumes access is allowed.
142 * @param path the access path to the module script code
143 * @param itemtype the information subclassing (usefull for complex modules, defaults to 'standard')
144 * @param this_id the item id within the information class denoted by itemtype. In resources, this id
145 * points to the resource record and not to the module that shows it.
146 * @param user the user record denoting the user who searches
147 * @param group_id the current group used by the user when searching
148 * @return true if access is allowed, false elsewhere
150 function label_check_text_access($path, $itemtype, $this_id, $user, $group_id, $context_id){
153 // include_once("{$CFG->dirroot}/{$path}/lib.php");
155 $r = get_record('label', 'id', $this_id);
156 $module_context = get_record('context', 'id', $context_id);
157 $cm = get_record('course_modules', 'id', $module_context->instanceid
);
158 if (empty($cm)) return false; // Shirai 20093005 - MDL19342 - course module might have been delete
160 $course_context = get_context_instance(CONTEXT_COURSE
, $r->course
);
162 //check if englobing course is visible
163 if (!has_capability('moodle/course:view', $course_context)){
167 //check if found course module is visible
168 if (!$cm->visible
and !has_capability('moodle/course:viewhiddenactivities', $module_context)){
176 * post processes the url for cleaner output.
177 * @param string $title
179 function label_link_post_processing($title){
182 if ($CFG->block_search_utf8dir
){
183 return mb_convert_encoding("(".shorten_text(clean_text($title), 60)."...) ", 'UTF-8', 'auto');
185 return mb_convert_encoding("(".shorten_text(clean_text($title), 60)."...) ", 'auto', 'UTF-8');