-
Notifications
You must be signed in to change notification settings - Fork 3
/
search_document.php
273 lines (237 loc) · 9.89 KB
/
search_document.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Global Search Engine for Moodle
*
* @package customlabel
* @category mod
* @subpackage document_wrappers
* @author Valery Fremaux [valery.fremaux@gmail.com] > 1.9
* @date 2008/03/31
* @license http://www.gnu.org/copyleft/gpl.html GNU Public License
*
* document handling for all resources
* This file contains the mapping between a resource and it's indexable counterpart,
*
* Functions for iterating and retrieving the necessary records are now also included
* in this file, rather than mod/resource/lib.php
*/
namespace local_search;
use \StdClass;
use \context_module;
use \context_course;
use \moodle_url;
use \context;
defined('MOODLE_INTERNAL') || die();
require_once($CFG->dirroot.'/local/search/documents/document.php');
require_once($CFG->dirroot.'/local/search/documents/document_wrapper.class.php');
require_once($CFG->dirroot.'/mod/customlabel/locallib.php');
require_once($CFG->dirroot.'/mod/customlabel/lib.php');
/**
* constants for document definition
*/
define('X_SEARCH_TYPE_CUSTOMLABEL', 'customlabel');
/**
* a class for representing searchable information
*
*/
class CustomLabelSearchDocument extends SearchDocument {
public function __construct(&$customlabel, &$class, $contextid) {
// Generic information; required.
$doc = new StdClass();
$doc->docid = $customlabel['course'];
$doc->documenttype = X_SEARCH_TYPE_CUSTOMLABEL;
$doc->itemtype = 'customlabel';
$doc->contextid = $contextid;
$doc->title = strip_tags($customlabel['title']);
$doc->date = $customlabel['timemodified'];
$doc->author = '';
$doc->contents = strip_tags($customlabel['processedcontent']);
$doc->url = customlabel_document_wrapper::make_link($customlabel['course'], $contextid);
/*
* module specific information : extract fields from serialized content. Add those who are
* lists as keyfields
*/
$content = json_decode(base64_decode($customlabel['content']));
$additionalkeys = null;
// Scan field and get as much searchable fields.
foreach ($class->fields as $afield) {
if (preg_match("/list$/", $afield->type)) {
if (!isset($afield->multiple)) {
$fieldname = $afield->name;
if (!empty($content->{$fieldname})) {
$additionalkeys[$fieldname] = $content->{$fieldname};
}
}
}
}
parent::__construct($doc, $data, $customlabel['course'], 0, 0, 'mod/'.X_SEARCH_TYPE_CUSTOMLABEL, $additionalkeys);
}
}
class customlabel_document_wrapper extends document_wrapper {
/**
* constructs valid access links to information
* @param resourceId the of the resource
* @return a full featured link element as a string
*/
public static function make_link($courseid, $contextid = null) {
global $DB;
$format = $DB->get_field('course', 'format', ['id' => $courseid]);
if ($format == 'page') {
// We need search on which page an occurrence of the search was found.
$context = context::instance_by_id($contextid);
$params = ['cmid' => $context->instanceid];
$pages = $DB->get_records('format_page_items', $params);
if ($pages) {
$firstitemoccurence = array_shift($pages);
$courseurl = new moodle_url('/course/view.php', array('id' => $courseid, 'page' => $firstitemoccurence->pageid));
}
}
if (empty($courseurl)) {
$courseurl = new moodle_url('/course/view.php', array('id' => $courseid));
}
return $courseurl;
}
/**
* part of standard API
*
*/
public static function get_iterator() {
global $DB;
/*
* trick to leave search indexer functionality intact, but allow
* this document to only use the below function to return info
* to be searched
*/
$labels = $DB->get_records('customlabel');
return $labels;
}
/**
* part of standard API
* this function does not need a content iterator, returns all the info
* itself;
* @param notneeded to comply API, remember to fake the iterator array though
* @uses CFG
* @return an array of searchable documents
*/
public static function get_content_for_index(&$instance) {
global $DB;
// Starting with Moodle native resources.
$documents = array();
$coursemodule = $DB->get_field('modules', 'id', array('name' => 'customlabel'));
$params = array('course' => $instance->course, 'module' => $coursemodule, 'instance' => $instance->id);
$cm = $DB->get_record('course_modules', $params);
$context = context_module::instance($cm->id);
$instance->coursemodule = $cm->id;
$customclass = customlabel_load_class($instance, true);
if ($customclass) {
// Render content.
$cminfo = new StdClass;
$cminfo->id = $cm->id;
$cminfo->instance = $instance->id;
$instance->processedcontent = customlabel_cm_info_dynamic($cminfo);
$arr = get_object_vars($instance);
$documents[] = new CustomLabelSearchDocument($arr, $customclass, $context->id);
mtrace("finished label {$instance->id}");
} else {
mtrace("ignoring unknown label type {$instance->labelclass} instance");
}
return $documents;
}
/**
* part of standard API.
* returns a single resource search document based on a label id
* @param id the id of the accessible document
* @return a searchable object or null if failure
*/
public static function single_document($id, $itemtype) {
global $DB;
$instance = $DB->get_record('customlabel', array('id' => $id));
if ($customlabel) {
$coursemodule = $DB->get_field('modules', 'id', array('name' => 'customlabel'));
$cm = $DB->get_record('course_modules', array('module' => $coursemodule, 'instance' => $instance->id));
$instance->coursemodule = $cm->id;
$customclass = customlabel_load_class($instance, true);
$context = context_module::instance($cm->id);
// Render content.
$cminfo = new StdClass;
$cminfo->id = $cm->id;
$cminfo->instance = $instance->id;
$instance->processedcontent = customlabel_cm_info_dynamic($cminfo);
return new CustomLabelSearchDocument(get_object_vars($instance), $customclass, $context->id);
}
return null;
}
/**
* returns the var names needed to build a sql query for addition/deletions
*
*/
public static function db_names() {
return array(array('id', 'customlabel', 'timemodified', 'timemodified', 'customlabel', ''));
}
/**
* customlabel points actually the complete course content and not the customlabel item
*/
protected static function search_get_objectinfo($itemtype, $thisid, $contextid = null) {
global $DB;
if (!$course = $DB->get_record('course', array('id' => $thisid))) {
return false;
}
if ($contextid) {
// We still need this case for the global search engine being able to operate.
$info->context = $DB->get_record('context', array('id' => $contextid));
$info->cm = $DB->get_record('course_modules', array('id' => $info->context->instanceid));
} else {
// This case IS NOT consistant for extracting object information.
return false;
}
$info->instance = $course;
$info->type = 'mod';
$info->mediatype = 'composite';
$info->contenttype = 'html';
return $info;
}
/**
* handles the access policy to contents indexed as searchable documents. If this
* function does not exist, the search engine assumes access is allowed.
* @param path the access path to the module script code
* @param itemtype the information subclassing (usefull for complex modules, defaults to 'standard')
* @param this_id the item id within the information class denoted by itemtype. In resources, this id
* points to the resource record and not to the module that shows it.
* @param user the user record denoting the user who searches
* @param group_id the current group used by the user when searching
* @return true if access is allowed, false elsewhere
*/
public static function check_text_access($path, $itemtype, $thisid, $user, $groupid, $contextid) {
// This_id binds to $course->id, but course check where already performed.
if (!$info = self::search_get_objectinfo($itemtype, $thisid, $contextid)) {
return false;
}
$cm = $info->cm;
$context = $info->context;
/*
* check if found course module is visible
* we cannot consider a content in hidden labels
*/
if (!$cm->visible && !has_capability('moodle/course:viewhiddenactivities', $context)) {
return false;
}
if (!$cm->uservisible) {
return false;
}
return true;
}
}