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
14 * document handling for all resources
15 * This file contains the mapping between a resource and it's indexable counterpart,
17 * Functions for iterating and retrieving the necessary records are now also included
18 * in this file, rather than mod/resource/lib.php
22 * requires and includes
24 require_once($CFG->dirroot
.'/search/documents/document.php');
25 require_once($CFG->dirroot
.'/mod/resource/lib.php');
28 * a class for representing searchable information
31 class LabelSearchDocument
extends SearchDocument
{
32 public function __construct(&$label, $context_id) {
33 // generic information; required
34 $doc->docid
= $label['id'];
35 $doc->documenttype
= SEARCH_TYPE_LABEL
;
36 $doc->itemtype
= 'label';
37 $doc->contextid
= $context_id;
39 $doc->title
= strip_tags($label['name']);
40 $doc->date
= $label['timemodified'];
42 $doc->contents
= strip_tags($label['intro']);
43 $doc->url
= label_make_link($label['course']);
45 // module specific information; optional
48 // construct the parent class
49 parent
::__construct($doc, $data, $label['course'], 0, 0, 'mod/'.SEARCH_TYPE_LABEL
);
54 * constructs valid access links to information
55 * @param int $resourceId the of the resource
56 * @return a full featured link element as a string
58 function label_make_link($course_id) {
61 return $CFG->wwwroot
.'/course/view.php?id='.$course_id;
65 * part of standard API
68 function label_iterator() {
71 //trick to leave search indexer functionality intact, but allow
72 //this document to only use the below function to return info
74 $labels = $DB->get_records('label');
79 * part of standard API
80 * this function does not need a content iterator, returns all the info
82 * @param $label notneeded to comply API, remember to fake the iterator array though
84 * @return an array of searchable documents
86 function label_get_content_for_index(&$label) {
89 // starting with Moodle native resources
92 $coursemodule = $DB->get_field('modules', 'id', array('name' => 'label'));
93 $cm = $DB->get_record('course_modules', array('course' => $label->course
, 'module' => $coursemodule, 'instance' => $label->id
));
94 $context = get_context_instance(CONTEXT_MODULE
, $cm->id
);
96 $documents[] = new LabelSearchDocument(get_object_vars($label), $context->id
);
98 mtrace("finished label {$label->id}");
103 * part of standard API.
104 * returns a single resource search document based on a label id
106 * @param int $id the id of the accessible document
107 * @param string $itemtype the nature of the information making the document
108 * @return a searchable object or null if failure
110 function label_single_document($id, $itemtype) {
113 $label = $DB->get_record('label', array('id' => $id));
116 $coursemodule = $DB->get_field('modules', 'id', array('name' => 'label'));
117 $cm = $DB->get_record('course_modules', array('id' => $label->id
));
118 $context = get_context_instance(CONTEXT_MODULE
, $cm->id
);
119 return new LabelSearchDocument(get_object_vars($label), $context->id
);
125 * dummy delete function that aggregates id with itemtype.
126 * this was here for a reason, but I can't remember it at the moment.
129 function label_delete($info, $itemtype) {
131 $object->itemtype
= $itemtype;
136 * returns the var names needed to build a sql query for addition/deletions
139 function label_db_names() {
140 //[primary id], [table name], [time created field name], [time modified field name], [docsubtype], [additional where conditions for sql]
141 return array(array('id', 'label', 'timemodified', 'timemodified', 'label', ''));
145 * this function handles the access policy to contents indexed as searchable documents. If this
146 * function does not exist, the search engine assumes access is allowed.
148 * @param string $path the access path to the module script code
149 * @param string $itemtype the information subclassing (usefull for complex modules, defaults to 'standard')
150 * @param int $this_id the item id within the information class denoted by itemtype. In resources, this id
151 * points to the resource record and not to the module that shows it.
152 * @param object $user the user record denoting the user who searches
153 * @param int $group_id the current group used by the user when searching
154 * @return true if access is allowed, false elsewhere
156 function label_check_text_access($path, $itemtype, $this_id, $user, $group_id, $context_id){
159 $r = $DB->get_record('label', array('id' => $this_id));
160 $module_context = $DB->get_record('context', array('id' => $context_id));
161 $cm = $DB->get_record('course_modules', array('id' => $module_context->instanceid
));
163 if (empty($cm)) return false; // Shirai 20090530 - MDL19342 - course module might have been delete
165 $course_context = get_context_instance(CONTEXT_COURSE
, $r->course
);
167 //check if englobing course is visible
168 if (!is_enrolled($course_context) and !is_viewing($course_context)) {
172 //check if found course module is visible
173 if (!$cm->visible
and !has_capability('moodle/course:viewhiddenactivities', $module_context)){
181 * post processes the url for cleaner output.
182 * @param string $title
184 function label_link_post_processing($title){
187 if ($CFG->block_search_utf8dir
){
188 return mb_convert_encoding("(".shorten_text(clean_text($title), 60)."...) ", 'UTF-8', 'auto');
190 return mb_convert_encoding("(".shorten_text(clean_text($title), 60)."...) ", 'auto', 'UTF-8');