Fix a possible race condition in the PaintWeb DML code.
[moodle/mihaisucan.git] / search / documents / label_document.php
blob9ee02853e0c430634de8d8ac7ac1b19becc7e8d9
1 <?php
2 /**
3 * Global Search Engine for Moodle
5 * @package search
6 * @category core
7 * @subpackage document_wrappers
8 * @author Valery Fremaux [valery.fremaux@club-internet.fr] > 1.9
9 * @contributor Tatsuva Shirai 20090530
10 * @date 2008/03/31
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
20 /**
21 * requires and includes
23 require_once("$CFG->dirroot/search/documents/document.php");
24 require_once("$CFG->dirroot/mod/resource/lib.php");
26 /* *
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'];
40 $doc->author = '';
41 $doc->contents = strip_tags($label['name']);
42 $doc->url = label_make_link($label['course']);
44 // module specific information; optional
45 $data = array();
47 // construct the parent class
48 parent::__construct($doc, $data, $label['course'], 0, 0, 'mod/'.SEARCH_TYPE_LABEL);
49 } //constructor
52 /**
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) {
58 global $CFG;
60 return $CFG->wwwroot.'/course/view.php?id='.$course_id;
63 /**
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
70 //to be searched
71 $labels = get_records('label');
72 return $labels;
75 /**
76 * part of standard API
77 * this function does not need a content iterator, returns all the info
78 * itself;
79 * @param notneeded to comply API, remember to fake the iterator array though
80 * @uses CFG
81 * @return an array of searchable documents
83 function label_get_content_for_index(&$label) {
84 global $CFG;
86 // starting with Moodle native resources
87 $documents = array();
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}");
96 return $documents;
99 /**
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) {
106 global $CFG;
108 $label = get_record('label', 'id', $id);
110 if ($label){
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);
116 return null;
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) {
125 $object->id = $info;
126 $object->itemtype = $itemtype;
127 return $object;
128 } //resource_delete
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){
151 global $CFG;
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)){
164 return false;
167 //check if found course module is visible
168 if (!$cm->visible and !has_capability('moodle/course:viewhiddenactivities', $module_context)){
169 return false;
172 return true;
176 * post processes the url for cleaner output.
177 * @param string $title
179 function label_link_post_processing($title){
180 global $CFG;
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');