MDL-41283 report_participation: Updated report to use logging api
[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/**
11e41f1a 19 * @package mod_resource
702ab58c
PS
20 * @copyright 2009 Petr Skoda {@link http://skodak.org}
21 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
28f672b2 22 */
23
702ab58c
PS
24defined('MOODLE_INTERNAL') || die;
25
aa54ed7b 26/**
27 * List of features supported in Resource module
28 * @param string $feature FEATURE_xx constant for requested feature
29 * @return mixed True if module supports feature, false if not, null if doesn't know
30 */
31function resource_supports($feature) {
32 switch($feature) {
33 case FEATURE_MOD_ARCHETYPE: return MOD_ARCHETYPE_RESOURCE;
34 case FEATURE_GROUPS: return false;
35 case FEATURE_GROUPINGS: return false;
36 case FEATURE_GROUPMEMBERSONLY: return true;
37 case FEATURE_MOD_INTRO: return true;
38 case FEATURE_COMPLETION_TRACKS_VIEWS: return true;
39 case FEATURE_GRADE_HAS_GRADE: return false;
40 case FEATURE_GRADE_OUTCOMES: return false;
a345de6e 41 case FEATURE_BACKUP_MOODLE2: return true;
3e4c2435 42 case FEATURE_SHOW_DESCRIPTION: return true;
214b1cf7 43
aa54ed7b 44 default: return null;
45 }
46}
713d78ea 47
28f672b2 48/**
aa54ed7b 49 * Returns all other caps used in module
50 * @return array
28f672b2 51 */
aa54ed7b 52function resource_get_extra_capabilities() {
53 return array('moodle/site:accessallgroups');
54}
3d30a455 55
d18830fe 56/**
aa54ed7b 57 * This function is used by the reset_course_userdata function in moodlelib.
58 * @param $data the data submitted from the reset course.
59 * @return array status array
28f672b2 60 */
aa54ed7b 61function resource_reset_userdata($data) {
62 return array();
63}
65634a81 64
aa54ed7b 65/**
66 * List of view style log actions
67 * @return array
68 */
69function resource_get_view_actions() {
70 return array('view','view all');
71}
65634a81 72
aa54ed7b 73/**
74 * List of update style log actions
75 * @return array
76 */
77function resource_get_post_actions() {
78 return array('update', 'add');
79}
65634a81 80
aa54ed7b 81/**
82 * Add resource instance.
83 * @param object $data
84 * @param object $mform
c6c9a3bc 85 * @return int new resource instance id
aa54ed7b 86 */
87function resource_add_instance($data, $mform) {
88 global $CFG, $DB;
89 require_once("$CFG->libdir/resourcelib.php");
abed5d65 90 require_once("$CFG->dirroot/mod/resource/locallib.php");
f79321f1 91 $cmid = $data->coursemodule;
aa54ed7b 92 $data->timemodified = time();
a2c57666 93
94 resource_set_display_options($data);
d18830fe 95
aa54ed7b 96 $data->id = $DB->insert_record('resource', $data);
d18830fe 97
aa54ed7b 98 // we need to use context now, so we need to make sure all needed info is already in db
99 $DB->set_field('course_modules', 'instance', $data->id, array('id'=>$cmid));
f79321f1 100 resource_set_mainfile($data);
aa54ed7b 101 return $data->id;
102}
cccb016a 103
aa54ed7b 104/**
105 * Update resource instance.
106 * @param object $data
107 * @param object $mform
108 * @return bool true
109 */
110function resource_update_instance($data, $mform) {
111 global $CFG, $DB;
112 require_once("$CFG->libdir/resourcelib.php");
aa54ed7b 113 $data->timemodified = time();
114 $data->id = $data->instance;
115 $data->revision++;
2a439ba7 116
a2c57666 117 resource_set_display_options($data);
118
119 $DB->update_record('resource', $data);
120 resource_set_mainfile($data);
121 return true;
122}
123
124/**
125 * Updates display options based on form input.
126 *
127 * Shared code used by resource_add_instance and resource_update_instance.
128 *
129 * @param object $data Data object
130 */
131function resource_set_display_options($data) {
aa54ed7b 132 $displayoptions = array();
133 if ($data->display == RESOURCELIB_DISPLAY_POPUP) {
134 $displayoptions['popupwidth'] = $data->popupwidth;
135 $displayoptions['popupheight'] = $data->popupheight;
65634a81 136 }
aa54ed7b 137 if (in_array($data->display, array(RESOURCELIB_DISPLAY_AUTO, RESOURCELIB_DISPLAY_EMBED, RESOURCELIB_DISPLAY_FRAME))) {
aa54ed7b 138 $displayoptions['printintro'] = (int)!empty($data->printintro);
3efe78df 139 }
a2c57666 140 if (!empty($data->showsize)) {
141 $displayoptions['showsize'] = 1;
142 }
143 if (!empty($data->showtype)) {
144 $displayoptions['showtype'] = 1;
145 }
aa54ed7b 146 $data->displayoptions = serialize($displayoptions);
d18830fe 147}
148
28f672b2 149/**
aa54ed7b 150 * Delete resource instance.
28f672b2 151 * @param int $id
aa54ed7b 152 * @return bool true
28f672b2 153 */
d18830fe 154function resource_delete_instance($id) {
aa54ed7b 155 global $DB;
ec81373f 156
aa54ed7b 157 if (!$resource = $DB->get_record('resource', array('id'=>$id))) {
d18830fe 158 return false;
159 }
79035d46 160
aa54ed7b 161 // note: all context files are deleted automatically
ec81373f 162
aa54ed7b 163 $DB->delete_records('resource', array('id'=>$resource->id));
d18830fe 164
aa54ed7b 165 return true;
d18830fe 166}
167
28f672b2 168/**
aa54ed7b 169 * Return use outline
28f672b2 170 * @param object $course
171 * @param object $user
172 * @param object $mod
173 * @param object $resource
174 * @return object|null
175 */
2a439ba7 176function resource_user_outline($course, $user, $mod, $resource) {
5f5cd33c 177 global $DB;
178
aa54ed7b 179 if ($logs = $DB->get_records('log', array('userid'=>$user->id, 'module'=>'resource',
180 'action'=>'view', 'info'=>$resource->id), 'time ASC')) {
2a439ba7 181
182 $numviews = count($logs);
183 $lastlog = array_pop($logs);
184
39790bd8 185 $result = new stdClass();
aa54ed7b 186 $result->info = get_string('numviews', '', $numviews);
2a439ba7 187 $result->time = $lastlog->time;
188
189 return $result;
190 }
191 return NULL;
192}
193
28f672b2 194/**
aa54ed7b 195 * Return use complete
28f672b2 196 * @param object $course
197 * @param object $user
198 * @param object $mod
199 * @param object $resource
200 */
2a439ba7 201function resource_user_complete($course, $user, $mod, $resource) {
5f5cd33c 202 global $CFG, $DB;
2a439ba7 203
aa54ed7b 204 if ($logs = $DB->get_records('log', array('userid'=>$user->id, 'module'=>'resource',
205 'action'=>'view', 'info'=>$resource->id), 'time ASC')) {
2a439ba7 206 $numviews = count($logs);
207 $lastlog = array_pop($logs);
208
aa54ed7b 209 $strmostrecently = get_string('mostrecently');
210 $strnumviews = get_string('numviews', '', $numviews);
2a439ba7 211
212 echo "$strnumviews - $strmostrecently ".userdate($lastlog->time);
213
214 } else {
aa54ed7b 215 print_string('neverseen', 'resource');
2a439ba7 216 }
217}
218
28f672b2 219/**
220 * Given a course_module object, this function returns any
221 * "extra" information that may be needed when printing
222 * this activity in a course listing.
223 *
224 * See {@link get_array_of_activities()} in course/lib.php
225 *
3c620d7f 226 * @param stdClass $coursemodule
3e4c2435 227 * @return cached_cm_info info
28f672b2 228 */
8dddba42 229function resource_get_coursemodule_info($coursemodule) {
aa54ed7b 230 global $CFG, $DB;
231 require_once("$CFG->libdir/filelib.php");
232 require_once("$CFG->dirroot/mod/resource/locallib.php");
516c5eca
PS
233 require_once($CFG->libdir.'/completionlib.php');
234
bf0f06b1 235 $context = context_module::instance($coursemodule->id);
ec81373f 236
3e4c2435 237 if (!$resource = $DB->get_record('resource', array('id'=>$coursemodule->instance),
238 'id, name, display, displayoptions, tobemigrated, revision, intro, introformat')) {
aa54ed7b 239 return NULL;
af65e103 240 }
ec81373f 241
3e4c2435 242 $info = new cached_cm_info();
aa54ed7b 243 $info->name = $resource->name;
3e4c2435 244 if ($coursemodule->showdescription) {
245 // Convert intro to html. Do not filter cached version, filters run at display time.
246 $info->content = format_module_intro('resource', $resource, $coursemodule->id, false);
247 }
8dddba42 248
aa54ed7b 249 if ($resource->tobemigrated) {
c3847443 250 $info->icon ='i/invalid';
aa54ed7b 251 return $info;
af65e103 252 }
f79321f1 253 $fs = get_file_storage();
3d8f1d3a 254 $files = $fs->get_area_files($context->id, 'mod_resource', 'content', 0, 'sortorder DESC, id ASC', false); // TODO: this is not very efficient!!
f79321f1 255 if (count($files) >= 1) {
020088d6 256 $mainfile = reset($files);
72225e18 257 $info->icon = file_file_icon($mainfile, 24);
f79321f1
DC
258 $resource->mainfile = $mainfile->get_filename();
259 }
d18830fe 260
aa54ed7b 261 $display = resource_get_final_display_type($resource);
9f741612 262
aa54ed7b 263 if ($display == RESOURCELIB_DISPLAY_POPUP) {
264 $fullurl = "$CFG->wwwroot/mod/resource/view.php?id=$coursemodule->id&amp;redirect=1";
265 $options = empty($resource->displayoptions) ? array() : unserialize($resource->displayoptions);
266 $width = empty($options['popupwidth']) ? 620 : $options['popupwidth'];
267 $height = empty($options['popupheight']) ? 450 : $options['popupheight'];
268 $wh = "width=$width,height=$height,toolbar=no,location=no,menubar=no,copyhistory=no,status=no,directories=no,scrollbars=yes,resizable=yes";
3e4c2435 269 $info->onclick = "window.open('$fullurl', '', '$wh'); return false;";
6da4b261 270
aa54ed7b 271 } else if ($display == RESOURCELIB_DISPLAY_NEW) {
272 $fullurl = "$CFG->wwwroot/mod/resource/view.php?id=$coursemodule->id&amp;redirect=1";
3e4c2435 273 $info->onclick = "window.open('$fullurl'); return false;";
6da4b261 274
6da4b261 275 }
89bfeee0 276
a2c57666 277 // If any optional extra details are turned on, store in custom data
278 $info->customdata = resource_get_optional_details($resource, $coursemodule);
279
aa54ed7b 280 return $info;
6da4b261 281}
f3221af9 282
a2c57666 283/**
284 * Called when viewing course page. Shows extra details after the link if
285 * enabled.
286 *
287 * @param cm_info $cm Course module information
288 */
289function resource_cm_info_view(cm_info $cm) {
73ee2fda 290 $details = $cm->customdata;
a2c57666 291 if ($details) {
292 $cm->set_after_link(' ' . html_writer::tag('span', $details,
293 array('class' => 'resourcelinkdetails')));
294 }
295}
f3221af9 296
28f672b2 297/**
aa54ed7b 298 * Lists all browsable file areas
d2b7803e
DC
299 *
300 * @package mod_resource
301 * @category files
302 * @param stdClass $course course object
303 * @param stdClass $cm course module object
304 * @param stdClass $context context object
28f672b2 305 * @return array
306 */
aa54ed7b 307function resource_get_file_areas($course, $cm, $context) {
308 $areas = array();
64f93798 309 $areas['content'] = get_string('resourcecontent', 'resource');
aa54ed7b 310 return $areas;
f3221af9 311}
312
28f672b2 313/**
f1b8bcf7 314 * File browsing support for resource module content area.
d2b7803e
DC
315 *
316 * @package mod_resource
317 * @category files
318 * @param stdClass $browser file browser instance
319 * @param stdClass $areas file areas
320 * @param stdClass $course course object
321 * @param stdClass $cm course module object
322 * @param stdClass $context context object
323 * @param string $filearea file area
324 * @param int $itemid item ID
325 * @param string $filepath file path
326 * @param string $filename file name
327 * @return file_info instance or null if not found
28f672b2 328 */
aa54ed7b 329function resource_get_file_info($browser, $areas, $course, $cm, $context, $filearea, $itemid, $filepath, $filename) {
330 global $CFG;
a69be0d8 331
64f93798
PS
332 if (!has_capability('moodle/course:managefiles', $context)) {
333 // students can not peak here!
334 return null;
335 }
a69be0d8 336
aa54ed7b 337 $fs = get_file_storage();
a69be0d8 338
64f93798 339 if ($filearea === 'content') {
aa54ed7b 340 $filepath = is_null($filepath) ? '/' : $filepath;
341 $filename = is_null($filename) ? '.' : $filename;
a69be0d8 342
aa54ed7b 343 $urlbase = $CFG->wwwroot.'/pluginfile.php';
64f93798 344 if (!$storedfile = $fs->get_file($context->id, 'mod_resource', 'content', 0, $filepath, $filename)) {
aa54ed7b 345 if ($filepath === '/' and $filename === '.') {
64f93798 346 $storedfile = new virtual_root_file($context->id, 'mod_resource', 'content', 0);
a69be0d8 347 } else {
aa54ed7b 348 // not found
349 return null;
a69be0d8 350 }
351 }
aa54ed7b 352 require_once("$CFG->dirroot/mod/resource/locallib.php");
64f93798 353 return new resource_content_file_info($browser, $context, $storedfile, $urlbase, $areas[$filearea], true, true, true, false);
a69be0d8 354 }
355
aa54ed7b 356 // note: resource_intro handled in file_browser automatically
a69be0d8 357
aa54ed7b 358 return null;
a69be0d8 359}
360
0b5a80a1 361/**
aa54ed7b 362 * Serves the resource files.
d2b7803e
DC
363 *
364 * @package mod_resource
365 * @category files
366 * @param stdClass $course course object
367 * @param stdClass $cm course module object
368 * @param stdClass $context context object
369 * @param string $filearea file area
370 * @param array $args extra arguments
371 * @param bool $forcedownload whether or not force download
261cbbac 372 * @param array $options additional options affecting the file serving
b4ff85aa 373 * @return bool false if file not found, does not return if found - just send the file
28f672b2 374 */
261cbbac 375function resource_pluginfile($course, $cm, $context, $filearea, $args, $forcedownload, array $options=array()) {
aa54ed7b 376 global $CFG, $DB;
377 require_once("$CFG->libdir/resourcelib.php");
0d06b6fd 378
64f93798 379 if ($context->contextlevel != CONTEXT_MODULE) {
aa54ed7b 380 return false;
3efe78df 381 }
382
64f93798 383 require_course_login($course, true, $cm);
ce459060
PS
384 if (!has_capability('mod/resource:view', $context)) {
385 return false;
386 }
3efe78df 387
64f93798
PS
388 if ($filearea !== 'content') {
389 // intro is handled automatically in pluginfile.php
aa54ed7b 390 return false;
3efe78df 391 }
392
aa54ed7b 393 array_shift($args); // ignore revision - designed to prevent caching problems only
3efe78df 394
aa54ed7b 395 $fs = get_file_storage();
64f93798 396 $relativepath = implode('/', $args);
fe3c5dae
PS
397 $fullpath = rtrim("/$context->id/mod_resource/$filearea/0/$relativepath", '/');
398 do {
399 if (!$file = $fs->get_file_by_hash(sha1($fullpath))) {
9815ccee 400 if ($fs->get_file_by_hash(sha1("$fullpath/."))) {
fe3c5dae
PS
401 if ($file = $fs->get_file_by_hash(sha1("$fullpath/index.htm"))) {
402 break;
403 }
404 if ($file = $fs->get_file_by_hash(sha1("$fullpath/index.html"))) {
405 break;
406 }
407 if ($file = $fs->get_file_by_hash(sha1("$fullpath/Default.htm"))) {
408 break;
409 }
410 }
411 $resource = $DB->get_record('resource', array('id'=>$cm->instance), 'id, legacyfiles', MUST_EXIST);
412 if ($resource->legacyfiles != RESOURCELIB_LEGACYFILES_ACTIVE) {
413 return false;
414 }
415 if (!$file = resourcelib_try_file_migration('/'.$relativepath, $cm->id, $cm->course, 'mod_resource', 'content', 0)) {
416 return false;
417 }
418 // file migrate - update flag
419 $resource->legacyfileslast = time();
420 $DB->update_record('resource', $resource);
0d06b6fd 421 }
fe3c5dae 422 } while (false);
ffcfd8a7 423
aa54ed7b 424 // should we apply filters?
425 $mimetype = $file->get_mimetype();
b4ff85aa 426 if ($mimetype === 'text/html' or $mimetype === 'text/plain') {
64f93798 427 $filter = $DB->get_field('resource', 'filterfiles', array('id'=>$cm->instance));
4eaa964f 428 $CFG->embeddedsoforcelinktarget = true;
aa54ed7b 429 } else {
430 $filter = 0;
18a2a0cb 431 }
18a2a0cb 432
aa54ed7b 433 // finally send the file
0c431257 434 send_stored_file($file, null, $filter, $forcedownload, $options);
13ca1e06 435}
b1627a92
DC
436
437/**
438 * Return a list of page types
439 * @param string $pagetype current page type
440 * @param stdClass $parentcontext Block's parent context
441 * @param stdClass $currentcontext Current context of block
442 */
b38e2e28 443function resource_page_type_list($pagetype, $parentcontext, $currentcontext) {
b1627a92
DC
444 $module_pagetype = array('mod-resource-*'=>get_string('page-mod-resource-x', 'resource'));
445 return $module_pagetype;
446}
ec0d6ea2
DC
447
448/**
449 * Export file resource contents
450 *
451 * @return array of file content
452 */
453function resource_export_contents($cm, $baseurl) {
454 global $CFG, $DB;
455 $contents = array();
bf0f06b1 456 $context = context_module::instance($cm->id);
ec0d6ea2
DC
457 $resource = $DB->get_record('resource', array('id'=>$cm->instance), '*', MUST_EXIST);
458
459 $fs = get_file_storage();
460 $files = $fs->get_area_files($context->id, 'mod_resource', 'content', 0, 'sortorder DESC, id ASC', false);
461
462 foreach ($files as $fileinfo) {
463 $file = array();
464 $file['type'] = 'file';
465 $file['filename'] = $fileinfo->get_filename();
466 $file['filepath'] = $fileinfo->get_filepath();
467 $file['filesize'] = $fileinfo->get_filesize();
468 $file['fileurl'] = file_encode_url("$CFG->wwwroot/" . $baseurl, '/'.$context->id.'/mod_resource/content/'.$resource->revision.$fileinfo->get_filepath().$fileinfo->get_filename(), true);
469 $file['timecreated'] = $fileinfo->get_timecreated();
470 $file['timemodified'] = $fileinfo->get_timemodified();
471 $file['sortorder'] = $fileinfo->get_sortorder();
472 $file['userid'] = $fileinfo->get_userid();
473 $file['author'] = $fileinfo->get_author();
474 $file['license'] = $fileinfo->get_license();
475 $contents[] = $file;
476 }
477
478 return $contents;
479}
32528f94
DS
480
481/**
482 * Register the ability to handle drag and drop file uploads
483 * @return array containing details of the files / types the mod can handle
484 */
6b8b0b2e 485function resource_dndupload_register() {
32528f94
DS
486 return array('files' => array(
487 array('extension' => '*', 'message' => get_string('dnduploadresource', 'mod_resource'))
488 ));
489}
490
491/**
492 * Handle a file that has been uploaded
493 * @param object $uploadinfo details of the file / content that has been uploaded
494 * @return int instance id of the newly created mod
495 */
6b8b0b2e 496function resource_dndupload_handle($uploadinfo) {
abed5d65
DS
497 // Gather the required info.
498 $data = new stdClass();
499 $data->course = $uploadinfo->course->id;
500 $data->name = $uploadinfo->displayname;
5fece0f2 501 $data->intro = '';
abed5d65
DS
502 $data->introformat = FORMAT_HTML;
503 $data->coursemodule = $uploadinfo->coursemodule;
504 $data->files = $uploadinfo->draftitemid;
505
506 // Set the display options to the site defaults.
32528f94 507 $config = get_config('resource');
abed5d65
DS
508 $data->display = $config->display;
509 $data->popupheight = $config->popupheight;
510 $data->popupwidth = $config->popupwidth;
abed5d65 511 $data->printintro = $config->printintro;
10654e32
SH
512 $data->showsize = (isset($config->showsize)) ? $config->showsize : 0;
513 $data->showtype = (isset($config->showtype)) ? $config->showtype : 0;
a274df85 514 $data->filterfiles = $config->filterfiles;
32528f94 515
abed5d65 516 return resource_add_instance($data, null);
32528f94 517}