forked from moodle/moodle
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathresource_document.php
354 lines (319 loc) · 12.3 KB
/
resource_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
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
<?php
/**
* Global Search Engine for Moodle
*
* @package search
* @category core
* @subpackage document_wrappers
* @author Michael Campanis (mchampan) [[email protected]], Valery Fremaux [[email protected]] > 1.8
* @contributor Tatsuva Shirai 20090530
* @date 2008/03/31
* @license http://www.gnu.org/copyleft/gpl.html GNU Public License
* @version Moodle 2.0
*
* 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
*/
/**
* requires and includes
*/
require_once($CFG->dirroot.'/search/documents/document.php');
require_once($CFG->dirroot.'/mod/resource/lib.php');
/* *
* a class for representing searchable information
*
*/
class ResourceSearchDocument extends SearchDocument {
public function __construct(&$resource, $context_id) {
// generic information; required
$doc->docid = $resource['trueid'];
$doc->documenttype = SEARCH_TYPE_RESOURCE;
$doc->itemtype = $resource['type'];
$doc->contextid = $context_id;
$doc->title = strip_tags($resource['name']);
$doc->date = $resource['timemodified'];
$doc->author = '';
$doc->contents = strip_tags($resource['summary']).' '.strip_tags($resource['alltext']);
$doc->url = resource_make_link($resource['id']);
// module specific information; optional
$data = array();
// construct the parent class
parent::__construct($doc, $data, $resource['course'], 0, 0, 'mod/'.SEARCH_TYPE_RESOURCE);
}
}
/**
* constructs valid access links to information
* @param resourceId the of the resource
* @return a full featured link element as a string
*/
function resource_make_link($resource_id) {
global $CFG;
return $CFG->wwwroot.'/mod/resource/view.php?id='.$resource_id;
}
/**
* part of standard API
*
*/
function resource_iterator() {
//trick to leave search indexer functionality intact, but allow
//this document to only use the below function to return info
//to be searched
return array(true);
}
/**
* part of standard API
* this function does not need a content iterator, returns all the info
* itself;
* @param void $notneeded to comply API, remember to fake the iterator array though
* @uses $CFG, $DB
* @return an array of searchable documents
*/
function resource_get_content_for_index(&$notneeded) {
global $CFG, $DB;
// starting with Moodle native resources
$documents = array();
$query = "
SELECT
id as trueid,
r.*
FROM
{resource} as r
WHERE
alltext != '' AND
alltext != ' ' AND
alltext != ' ' AND
type != 'file'
";
if ($resources = $DB->get_records_sql($query)){
foreach($resources as $aResource){
$coursemodule = $DB->get_field('modules', 'id', array('name' => 'resource'));
$cm = $DB->get_record('course_modules', array('course' => $aResource->course, 'module' => $coursemodule, 'instance' => $aResource->id));
$context = get_context_instance(CONTEXT_MODULE, $cm->id);
$aResource->id = $cm->id;
$documents[] = new ResourceSearchDocument(get_object_vars($aResource), $context->id);
mtrace("finished $aResource->name");
}
}
// special physical files handling
/**
* this sequence searches for a compatible physical stream handler for getting a text
* equivalence for the content.
*
*/
if (@$CFG->block_search_enable_file_indexing){
$query = "
SELECT
r.id as trueid,
cm.id as id,
r.course as course,
r.name as name,
r.summary as summary,
r.alltext as alltext,
r.reference as reference,
r.type as type,
r.timemodified as timemodified
FROM
{resource} as r,
{course_modules} as cm,
{modules} as m
WHERE
r.type = 'file' AND
cm.instance = r.id AND
cm.course = r.course AND
cm.module = m.id AND
m.name = 'resource'
";
if ($resources = $DB->get_records_sql($query)){
// invokes external content extractor if exists.
foreach($resources as $aResource){
// fetches a physical indexable document and adds it to documents passed by ref
$coursemodule = $DB->get_field('modules', 'id', array('name' => 'resource'));
$cm = $DB->get_record('course_modules', array('id' => $aResource->id));
$context = get_context_instance(CONTEXT_MODULE, $cm->id);
resource_get_physical_file($aResource, $context->id, false, $documents);
}
}
}
return $documents;
}
/**
* get text from a physical file
* @uses $CFG
* @param reference $resource a resource for which to fetch some representative text
* @param int $context_id the context associated with the resource
* @param bool $getsingle if true, returns a single search document, elsewhere return the array
* given as documents increased by one
* @param array $documents the array of documents, by ref, where to add the new document.
* @return a search document when unique or false.
*/
function resource_get_physical_file(&$resource, $context_id, $getsingle, &$documents = null){
global $CFG;
// cannot index empty references
if (empty($resource->reference)){
mtrace("Cannot index, empty reference.");
return false;
}
// cannot index remote resources
if (resource_is_url($resource->reference)){
mtrace("Cannot index remote URLs.");
return false;
}
$fileparts = pathinfo($resource->reference);
// cannot index unknown or masked types
if (empty($fileparts['extension'])) {
mtrace("Cannot index without explicit extension.");
return false;
}
// cannot index non existent file
$file = "{$CFG->dataroot}/{$resource->course}/{$resource->reference}";
if (!file_exists($file)){
mtrace("Missing resource file $file : will not be indexed.");
return false;
}
$ext = strtolower($fileparts['extension']);
// cannot index unallowed or unhandled types
if (!preg_match("/\b$ext\b/i", $CFG->block_search_filetypes)) {
mtrace($fileparts['extension'] . ' is not an allowed extension for indexing');
return false;
}
if (file_exists($CFG->dirroot.'/search/documents/physical_'.$ext.'.php')){
include_once($CFG->dirroot.'/search/documents/physical_'.$ext.'.php');
$function_name = 'get_text_for_indexing_'.$ext;
$resource->alltext = $function_name($resource);
if (!empty($resource->alltext)){
if ($getsingle){
$single = new ResourceSearchDocument(get_object_vars($resource), $context_id);
mtrace("finished file $resource->name as {$resource->reference}");
return $single;
} else {
$documents[] = new ResourceSearchDocument(get_object_vars($resource), $context_id);
}
mtrace("finished file $resource->name as {$resource->reference}");
}
} else {
mtrace("fulltext handler not found for $ext type");
}
return false;
}
/**
* part of standard API.
* returns a single resource search document based on a resource_entry id
* @uses $CFG, $DB
* @param id the id of the accessible document
* @return a searchable object or null if failure
*/
function resource_single_document($id, $itemtype) {
global $CFG, $DB;
// rewriting with legacy moodle databse API
$query = "
SELECT
r.id as trueid,
cm.id as id,
r.course as course,
r.name as name,
r.summary as summary,
r.alltext as alltext,
r.reference as reference,
r.type as type,
r.timemodified as timemodified
FROM
{resource} as r,
{course_modules} as cm,
{modules} as m
WHERE
cm.instance = r.id AND
cm.course = r.course AND
cm.module = m.id AND
m.name = 'resource' AND
((r.type != 'file' AND
r.alltext != '' AND
r.alltext != ' ' AND
r.alltext != ' ') OR
r.type = 'file') AND
r.id = '?'
";
$resource = $DB->get_record_sql($query, array($id));
if ($resource){
$coursemodule = $DB->get_field('modules', 'id', array('name' => 'resource'));
$cm = $DB->get_record('course_modules', array('id' => $resource->id));
$context = get_context_instance(CONTEXT_MODULE, $cm->id);
if ($resource->type == 'file' && @$CFG->block_search_enable_file_indexing){
$document = resource_get_physical_file($resource, true, $context->id);
if (!$document) mtrace("Warning : this document {$resource->name} will not be indexed");
return $document;
} else {
return new ResourceSearchDocument(get_object_vars($resource), $context->id);
}
}
mtrace('null resource');
return null;
}
/**
* dummy delete function that aggregates id with itemtype.
* this was here for a reason, but I can't remember it at the moment.
*
*/
function resource_delete($info, $itemtype) {
$object->id = $info;
$object->itemtype = $itemtype;
return $object;
}
/**
* returns the var names needed to build a sql query for addition/deletions
*
*/
function resource_db_names() {
//[primary id], [table name], [time created field name], [time modified field name], [additional where conditions for sql]
return array(array('id', 'resource', 'timemodified', 'timemodified', 'any', " (alltext != '' AND alltext != ' ' AND alltext != ' ' AND TYPE != 'file') OR TYPE = 'file' "));
}
/**
* this function handles the access policy to contents indexed as searchable documents. If this
* function does not exist, the search engine assumes access is allowed.
* @uses $CFG, $DB
* @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
*/
function resource_check_text_access($path, $itemtype, $this_id, $user, $group_id, $context_id){
global $CFG, $DB;
// include_once("{$CFG->dirroot}/{$path}/lib.php");
$r = $DB->get_record('resource', array('id' => $this_id));
$module_context = $DB->get_record('context', array('id' => $context_id));
$cm = $DB->get_record('course_modules', array('id' => $module_context->instanceid));
if (empty($cm)) return false; // Shirai 20090530 - MDL19342 - course module might have been delete
$course = $DB->get_record('course', array('id' => $r->course));
$course_context = get_context_instance(CONTEXT_COURSE, $r->course);
$course = $DB->get_record('course', array('id' => $r->course));
//check if course is visible
if (!$course->visible && !has_capability('moodle/course:viewhiddencourses', $course_context)) {
return false;
}
//check if user is registered in course or course is open to guests
if (!is_enrolled($course_context) and !is_viewing($course_context)) { //TODO: guest course access is gone, this needs a different solution
return false;
}
//check if found course module is visible
if (!$cm->visible and !has_capability('moodle/course:viewhiddenactivities', $module_context)){
return false;
}
return true;
}
/**
* post processes the url for cleaner output.
* @param string $title
*/
function resource_link_post_processing($title){
global $CFG;
if ($CFG->block_search_utf8dir){
return mb_convert_encoding($title, 'UTF-8', 'auto');
}
return mb_convert_encoding($title, 'auto', 'UTF-8');
}
?>