match qtype: MDL-21701 / OU bug 9551 Fix display of response history.
[moodle.git] / mod / resource / lib.php
CommitLineData
28f672b2 1<?php
2
3// This file is part of Moodle - http://moodle.org/
4//
5// Moodle is free software: you can redistribute it and/or modify
6// it under the terms of the GNU General Public License as published by
7// the Free Software Foundation, either version 3 of the License, or
8// (at your option) any later version.
9//
10// Moodle is distributed in the hope that it will be useful,
11// but WITHOUT ANY WARRANTY; without even the implied warranty of
12// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13// GNU General Public License for more details.
14//
15// You should have received a copy of the GNU General Public License
16// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
2a439ba7 17
28f672b2 18/**
19 * @package mod-resource
aa54ed7b 20 * @copyright 2009 Petr Skoda (http://skodak.org)
28f672b2 21 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
22 */
23
aa54ed7b 24/**
25 * List of features supported in Resource module
26 * @param string $feature FEATURE_xx constant for requested feature
27 * @return mixed True if module supports feature, false if not, null if doesn't know
28 */
29function resource_supports($feature) {
30 switch($feature) {
31 case FEATURE_MOD_ARCHETYPE: return MOD_ARCHETYPE_RESOURCE;
32 case FEATURE_GROUPS: return false;
33 case FEATURE_GROUPINGS: return false;
34 case FEATURE_GROUPMEMBERSONLY: return true;
35 case FEATURE_MOD_INTRO: return true;
36 case FEATURE_COMPLETION_TRACKS_VIEWS: return true;
37 case FEATURE_GRADE_HAS_GRADE: return false;
38 case FEATURE_GRADE_OUTCOMES: return false;
214b1cf7 39
aa54ed7b 40 default: return null;
41 }
42}
713d78ea 43
28f672b2 44/**
aa54ed7b 45 * Returns all other caps used in module
46 * @return array
28f672b2 47 */
aa54ed7b 48function resource_get_extra_capabilities() {
49 return array('moodle/site:accessallgroups');
50}
3d30a455 51
d18830fe 52/**
aa54ed7b 53 * This function is used by the reset_course_userdata function in moodlelib.
54 * @param $data the data submitted from the reset course.
55 * @return array status array
28f672b2 56 */
aa54ed7b 57function resource_reset_userdata($data) {
58 return array();
59}
65634a81 60
aa54ed7b 61/**
62 * List of view style log actions
63 * @return array
64 */
65function resource_get_view_actions() {
66 return array('view','view all');
67}
65634a81 68
aa54ed7b 69/**
70 * List of update style log actions
71 * @return array
72 */
73function resource_get_post_actions() {
74 return array('update', 'add');
75}
65634a81 76
aa54ed7b 77/**
78 * Add resource instance.
79 * @param object $data
80 * @param object $mform
81 * @return int new resoruce instance id
82 */
83function resource_add_instance($data, $mform) {
84 global $CFG, $DB;
85 require_once("$CFG->libdir/resourcelib.php");
65634a81 86
aa54ed7b 87 $cmid = $data->coursemodule;
88 $draftitemid = $data->files;
d18830fe 89
aa54ed7b 90 $data->timemodified = time();
91 $displayoptions = array();
92 if ($data->display == RESOURCELIB_DISPLAY_POPUP) {
93 $displayoptions['popupwidth'] = $data->popupwidth;
94 $displayoptions['popupheight'] = $data->popupheight;
65634a81 95 }
aa54ed7b 96 if (in_array($data->display, array(RESOURCELIB_DISPLAY_AUTO, RESOURCELIB_DISPLAY_EMBED, RESOURCELIB_DISPLAY_FRAME))) {
97 $displayoptions['printheading'] = (int)!empty($data->printheading);
98 $displayoptions['printintro'] = (int)!empty($data->printintro);
ec81373f 99 }
aa54ed7b 100 $data->displayoptions = serialize($displayoptions);
d18830fe 101
aa54ed7b 102 $data->id = $DB->insert_record('resource', $data);
d18830fe 103
aa54ed7b 104 // we need to use context now, so we need to make sure all needed info is already in db
105 $DB->set_field('course_modules', 'instance', $data->id, array('id'=>$cmid));
106 $context = get_context_instance(CONTEXT_MODULE, $cmid);
d2b23346 107
aa54ed7b 108 if ($draftitemid) {
109 file_save_draft_area_files($draftitemid, $context->id, 'resource_content', 0, array('subdirs'=>true));
65634a81 110 }
0440482f 111
aa54ed7b 112 $fs = get_file_storage();
113 $files = $fs->get_area_files($context->id, 'resource_content', 0, '', false);
114 if (count($files) == 1) {
115 $file = reset($files);
116 $path = $file->get_filepath().$file->get_filename();
117 if ($path !== $data->mainfile) {
118 $data->mainfile = $path;
119 $DB->set_field('resource', 'mainfile', $path, array('id'=>$data->id));
f58fcc82 120 }
65634a81 121 }
2a439ba7 122
aa54ed7b 123 return $data->id;
124}
cccb016a 125
aa54ed7b 126/**
127 * Update resource instance.
128 * @param object $data
129 * @param object $mform
130 * @return bool true
131 */
132function resource_update_instance($data, $mform) {
133 global $CFG, $DB;
134 require_once("$CFG->libdir/resourcelib.php");
2a439ba7 135
aa54ed7b 136 $cmid = $data->coursemodule;
137 $draftitemid = $data->files;
cccb016a 138
aa54ed7b 139 $data->timemodified = time();
140 $data->id = $data->instance;
141 $data->revision++;
2a439ba7 142
aa54ed7b 143 $displayoptions = array();
144 if ($data->display == RESOURCELIB_DISPLAY_POPUP) {
145 $displayoptions['popupwidth'] = $data->popupwidth;
146 $displayoptions['popupheight'] = $data->popupheight;
65634a81 147 }
aa54ed7b 148 if (in_array($data->display, array(RESOURCELIB_DISPLAY_AUTO, RESOURCELIB_DISPLAY_EMBED, RESOURCELIB_DISPLAY_FRAME))) {
149 $displayoptions['printheading'] = (int)!empty($data->printheading);
150 $displayoptions['printintro'] = (int)!empty($data->printintro);
3efe78df 151 }
aa54ed7b 152 $data->displayoptions = serialize($displayoptions);
3efe78df 153
aa54ed7b 154 $DB->update_record('resource', $data);
3efe78df 155
aa54ed7b 156 $context = get_context_instance(CONTEXT_MODULE, $cmid);
157 if ($draftitemid) {
158 file_save_draft_area_files($draftitemid, $context->id, 'resource_content', 0, array('subdirs'=>true));
ffcfd8a7 159 }
160
aa54ed7b 161 $fs = get_file_storage();
162 $files = $fs->get_area_files($context->id, 'resource_content', 0, '', false);
163 if (count($files) == 1) {
164 $file = reset($files);
165 $path = $file->get_filepath().$file->get_filename();
166 if ($path !== $data->mainfile) {
167 $data->mainfile = $path;
168 $DB->set_field('resource', 'mainfile', $path, array('id'=>$data->id));
169 }
ffcfd8a7 170 }
171
aa54ed7b 172 return true;
d18830fe 173}
174
28f672b2 175/**
aa54ed7b 176 * Delete resource instance.
28f672b2 177 * @param int $id
aa54ed7b 178 * @return bool true
28f672b2 179 */
d18830fe 180function resource_delete_instance($id) {
aa54ed7b 181 global $DB;
ec81373f 182
aa54ed7b 183 if (!$resource = $DB->get_record('resource', array('id'=>$id))) {
d18830fe 184 return false;
185 }
79035d46 186
aa54ed7b 187 // note: all context files are deleted automatically
ec81373f 188
aa54ed7b 189 $DB->delete_records('resource', array('id'=>$resource->id));
d18830fe 190
aa54ed7b 191 return true;
d18830fe 192}
193
28f672b2 194/**
aa54ed7b 195 * Return use outline
28f672b2 196 * @param object $course
197 * @param object $user
198 * @param object $mod
199 * @param object $resource
200 * @return object|null
201 */
2a439ba7 202function resource_user_outline($course, $user, $mod, $resource) {
5f5cd33c 203 global $DB;
204
aa54ed7b 205 if ($logs = $DB->get_records('log', array('userid'=>$user->id, 'module'=>'resource',
206 'action'=>'view', 'info'=>$resource->id), 'time ASC')) {
2a439ba7 207
208 $numviews = count($logs);
209 $lastlog = array_pop($logs);
210
9f741612 211 $result = new object();
aa54ed7b 212 $result->info = get_string('numviews', '', $numviews);
2a439ba7 213 $result->time = $lastlog->time;
214
215 return $result;
216 }
217 return NULL;
218}
219
28f672b2 220/**
aa54ed7b 221 * Return use complete
28f672b2 222 * @param object $course
223 * @param object $user
224 * @param object $mod
225 * @param object $resource
226 */
2a439ba7 227function resource_user_complete($course, $user, $mod, $resource) {
5f5cd33c 228 global $CFG, $DB;
2a439ba7 229
aa54ed7b 230 if ($logs = $DB->get_records('log', array('userid'=>$user->id, 'module'=>'resource',
231 'action'=>'view', 'info'=>$resource->id), 'time ASC')) {
2a439ba7 232 $numviews = count($logs);
233 $lastlog = array_pop($logs);
234
aa54ed7b 235 $strmostrecently = get_string('mostrecently');
236 $strnumviews = get_string('numviews', '', $numviews);
2a439ba7 237
238 echo "$strnumviews - $strmostrecently ".userdate($lastlog->time);
239
240 } else {
aa54ed7b 241 print_string('neverseen', 'resource');
2a439ba7 242 }
243}
244
28f672b2 245/**
246 * Returns the users with data in one resource
28f672b2 247 *
248 * @param int $resourceid
249 * @return bool false
250 */
84caf038 251function resource_get_participants($resourceid) {
84caf038 252 return false;
253}
2a439ba7 254
28f672b2 255/**
256 * Given a course_module object, this function returns any
257 * "extra" information that may be needed when printing
258 * this activity in a course listing.
259 *
260 * See {@link get_array_of_activities()} in course/lib.php
261 *
28f672b2 262 * @param object $coursemodule
263 * @return object info
264 */
8dddba42 265function resource_get_coursemodule_info($coursemodule) {
aa54ed7b 266 global $CFG, $DB;
267 require_once("$CFG->libdir/filelib.php");
268 require_once("$CFG->dirroot/mod/resource/locallib.php");
ec81373f 269
aa54ed7b 270 if (!$resource = $DB->get_record('resource', array('id'=>$coursemodule->instance), 'id, name, display, displayoptions, tobemigrated, mainfile, revision')) {
271 return NULL;
af65e103 272 }
ec81373f 273
aa54ed7b 274 $info = new object();
275 $info->name = $resource->name;
8dddba42 276
aa54ed7b 277 if ($resource->tobemigrated) {
278 $info->icon ='i/cross_red_big';
279 return $info;
af65e103 280 }
8dddba42 281
aa54ed7b 282 $info->icon = str_replace(array('.gif', '.png'), '', file_extension_icon($resource->mainfile));
d18830fe 283
aa54ed7b 284 $display = resource_get_final_display_type($resource);
9f741612 285
aa54ed7b 286 if ($display == RESOURCELIB_DISPLAY_POPUP) {
287 $fullurl = "$CFG->wwwroot/mod/resource/view.php?id=$coursemodule->id&amp;redirect=1";
288 $options = empty($resource->displayoptions) ? array() : unserialize($resource->displayoptions);
289 $width = empty($options['popupwidth']) ? 620 : $options['popupwidth'];
290 $height = empty($options['popupheight']) ? 450 : $options['popupheight'];
291 $wh = "width=$width,height=$height,toolbar=no,location=no,menubar=no,copyhistory=no,status=no,directories=no,scrollbars=yes,resizable=yes";
9a9012dc 292 $info->extra = "onclick=\"window.open('$fullurl', '', '$wh'); return false;\"";
6da4b261 293
aa54ed7b 294 } else if ($display == RESOURCELIB_DISPLAY_NEW) {
295 $fullurl = "$CFG->wwwroot/mod/resource/view.php?id=$coursemodule->id&amp;redirect=1";
9a9012dc 296 $info->extra = "onclick=\"window.open('$fullurl'); return false;\"";
6da4b261 297
aa54ed7b 298 } else if ($display == RESOURCELIB_DISPLAY_OPEN) {
299 $fullurl = "$CFG->wwwroot/mod/resource/view.php?id=$coursemodule->id&amp;redirect=1";
9a9012dc 300 $info->extra = "onclick=\"window.location.href ='$fullurl';return false;\"";
aa54ed7b 301
302 } else if ($display == RESOURCELIB_DISPLAY_DOWNLOAD) {
303 // do not open any window because it would be left there after download
304 $context = get_context_instance(CONTEXT_MODULE, $coursemodule->id);
305 $path = '/'.$context->id.'/resource_content/'.$resource->revision.$resource->mainfile;
306 $fullurl = addslashes_js(file_encode_url($CFG->wwwroot.'/pluginfile.php', $path, true));
9a9012dc 307 $info->extra = "onclick=\"window.open('$fullurl'); return false;\"";
6da4b261 308 }
89bfeee0 309
aa54ed7b 310 return $info;
6da4b261 311}
f3221af9 312
f3221af9 313
28f672b2 314/**
aa54ed7b 315 * Lists all browsable file areas
316 * @param object $course
317 * @param object $cm
318 * @param object $context
28f672b2 319 * @return array
320 */
aa54ed7b 321function resource_get_file_areas($course, $cm, $context) {
322 $areas = array();
323 if (has_capability('moodle/course:managefiles', $context)) {
324 $areas['resource_content'] = get_string('resourcecontent', 'resource');
325 }
326 return $areas;
f3221af9 327}
328
28f672b2 329/**
aa54ed7b 330 * File browsing support for resource module ontent area.
331 * @param object $browser
332 * @param object $areas
28f672b2 333 * @param object $course
aa54ed7b 334 * @param object $cm
335 * @param object $context
336 * @param string $filearea
337 * @param int $itemid
338 * @param string $filepath
339 * @param string $filename
340 * @return object file_info instance or null if not found
28f672b2 341 */
aa54ed7b 342function resource_get_file_info($browser, $areas, $course, $cm, $context, $filearea, $itemid, $filepath, $filename) {
343 global $CFG;
a69be0d8 344
aa54ed7b 345 $canwrite = has_capability('moodle/course:managefiles', $context);
a69be0d8 346
aa54ed7b 347 $fs = get_file_storage();
a69be0d8 348
aa54ed7b 349 if ($filearea === 'resource_content') {
350 $filepath = is_null($filepath) ? '/' : $filepath;
351 $filename = is_null($filename) ? '.' : $filename;
a69be0d8 352
aa54ed7b 353 $urlbase = $CFG->wwwroot.'/pluginfile.php';
354 if (!$storedfile = $fs->get_file($context->id, $filearea, 0, $filepath, $filename)) {
355 if ($filepath === '/' and $filename === '.') {
356 $storedfile = new virtual_root_file($context->id, $filearea, 0);
a69be0d8 357 } else {
aa54ed7b 358 // not found
359 return null;
a69be0d8 360 }
361 }
aa54ed7b 362 require_once("$CFG->dirroot/mod/resource/locallib.php");
363 return new resource_content_file_info($browser, $context, $storedfile, $urlbase, $areas[$filearea], true, true, $canwrite, false);
a69be0d8 364 }
365
aa54ed7b 366 // note: resource_intro handled in file_browser automatically
a69be0d8 367
aa54ed7b 368 return null;
a69be0d8 369}
370
0b5a80a1 371/**
aa54ed7b 372 * Serves the resource files.
373 * @param object $course
374 * @param object $cminfo
375 * @param object $context
376 * @param string $filearea
377 * @param array $args
378 * @param bool $forcedownload
379 * @return bool false if file not found, does not return if found - justsend the file
28f672b2 380 */
aa54ed7b 381function resource_pluginfile($course, $cminfo, $context, $filearea, $args, $forcedownload) {
382 global $CFG, $DB;
383 require_once("$CFG->libdir/resourcelib.php");
0d06b6fd 384
aa54ed7b 385 if (!$cminfo->uservisible) {
386 return false;
3efe78df 387 }
388
aa54ed7b 389 if ($filearea !== 'resource_content') {
390 // intro is handled automatically in pluginfile.php
391 return false;
3efe78df 392 }
393
aa54ed7b 394 if (!$cm = get_coursemodule_from_instance('resource', $cminfo->instance, $course->id)) {
395 return false;
3efe78df 396 }
397
aa54ed7b 398 require_course_login($course, true, $cm);
3efe78df 399
aa54ed7b 400 array_shift($args); // ignore revision - designed to prevent caching problems only
3efe78df 401
aa54ed7b 402 $fs = get_file_storage();
403 $relativepath = '/'.implode('/', $args);
404 $fullpath = $context->id.$filearea.'0'.$relativepath;
405 if (!$file = $fs->get_file_by_hash(sha1($fullpath)) or $file->is_directory()) {
406 $resource = $DB->get_record('resource', array('id'=>$cminfo->instance), 'id, legacyfiles', MUST_EXIST);
407 if ($resource->legacyfiles != RESOURCELIB_LEGACYFILES_ACTIVE) {
3efe78df 408 return false;
409 }
aa54ed7b 410 require_once("$CFG->dirroot/mod/resource/db/upgradelib.php");
411 if (!$file = resource_try_file_migration($relativepath, $cminfo->id, $cminfo->course, 'resource_content', 0)) {
412 return false;
0d06b6fd 413 }
aa54ed7b 414 // file migrate - update flag
415 $resource->legacyfileslast = time();
416 $DB->update_record('resource', $resource);
ffcfd8a7 417 }
418
aa54ed7b 419 // should we apply filters?
420 $mimetype = $file->get_mimetype();
421 if ($mimetype = 'text/html' or $mimetype = 'text/plain') {
422 $filter = $DB->get_field('resource', 'filterfiles', array('id'=>$cminfo->instance));
423 } else {
424 $filter = 0;
18a2a0cb 425 }
18a2a0cb 426
aa54ed7b 427 // finally send the file
428 send_stored_file($file, 86400, $filter, $forcedownload);
13ca1e06 429}