3 // This file is part of Moodle - http://moodle.org/
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.
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.
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/>.
20 * @copyright 2009 Petr Skoda (http://skodak.org)
21 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
24 defined('MOODLE_INTERNAL') || die;
27 * List of features supported in Page 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
31 function page_supports($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_MOD_INTRO: return true;
37 case FEATURE_COMPLETION_TRACKS_VIEWS: return true;
38 case FEATURE_GRADE_HAS_GRADE: return false;
39 case FEATURE_GRADE_OUTCOMES: return false;
40 case FEATURE_BACKUP_MOODLE2: return true;
41 case FEATURE_SHOW_DESCRIPTION: return true;
48 * Returns all other caps used in module
51 function page_get_extra_capabilities() {
52 return array('moodle/site:accessallgroups');
56 * This function is used by the reset_course_userdata function in moodlelib.
57 * @param $data the data submitted from the reset course.
58 * @return array status array
60 function page_reset_userdata($data) {
65 * List the actions that correspond to a view of this module.
66 * This is used by the participation report.
68 * Note: This is not used by new logging system. Event with
69 * crud = 'r' and edulevel = LEVEL_PARTICIPATING will
70 * be considered as view action.
74 function page_get_view_actions() {
75 return array('view','view all');
79 * List the actions that correspond to a post of this module.
80 * This is used by the participation report.
82 * Note: This is not used by new logging system. Event with
83 * crud = ('c' || 'u' || 'd') and edulevel = LEVEL_PARTICIPATING
84 * will be considered as post action.
88 function page_get_post_actions() {
89 return array('update', 'add');
94 * @param stdClass $data
95 * @param mod_page_mod_form $mform
96 * @return int new page instance id
98 function page_add_instance($data, $mform = null) {
100 require_once("$CFG->libdir/resourcelib.php");
102 $cmid = $data->coursemodule;
104 $data->timemodified = time();
105 $displayoptions = array();
106 if ($data->display == RESOURCELIB_DISPLAY_POPUP) {
107 $displayoptions['popupwidth'] = $data->popupwidth;
108 $displayoptions['popupheight'] = $data->popupheight;
110 $displayoptions['printheading'] = $data->printheading;
111 $displayoptions['printintro'] = $data->printintro;
112 $data->displayoptions = serialize($displayoptions);
115 $data->content = $data->page['text'];
116 $data->contentformat = $data->page['format'];
119 $data->id = $DB->insert_record('page', $data);
121 // we need to use context now, so we need to make sure all needed info is already in db
122 $DB->set_field('course_modules', 'instance', $data->id, array('id'=>$cmid));
123 $context = context_module::instance($cmid);
125 if ($mform and !empty($data->page['itemid'])) {
126 $draftitemid = $data->page['itemid'];
127 $data->content = file_save_draft_area_files($draftitemid, $context->id, 'mod_page', 'content', 0, page_get_editor_options($context), $data->content);
128 $DB->update_record('page', $data);
131 $completiontimeexpected = !empty($data->completionexpected) ? $data->completionexpected : null;
132 \core_completion\api::update_completion_date_event($cmid, 'page', $data->id, $completiontimeexpected);
138 * Update page instance.
139 * @param object $data
140 * @param object $mform
143 function page_update_instance($data, $mform) {
145 require_once("$CFG->libdir/resourcelib.php");
147 $cmid = $data->coursemodule;
148 $draftitemid = $data->page['itemid'];
150 $data->timemodified = time();
151 $data->id = $data->instance;
154 $displayoptions = array();
155 if ($data->display == RESOURCELIB_DISPLAY_POPUP) {
156 $displayoptions['popupwidth'] = $data->popupwidth;
157 $displayoptions['popupheight'] = $data->popupheight;
159 $displayoptions['printheading'] = $data->printheading;
160 $displayoptions['printintro'] = $data->printintro;
161 $data->displayoptions = serialize($displayoptions);
163 $data->content = $data->page['text'];
164 $data->contentformat = $data->page['format'];
166 $DB->update_record('page', $data);
168 $context = context_module::instance($cmid);
170 $data->content = file_save_draft_area_files($draftitemid, $context->id, 'mod_page', 'content', 0, page_get_editor_options($context), $data->content);
171 $DB->update_record('page', $data);
174 $completiontimeexpected = !empty($data->completionexpected) ? $data->completionexpected : null;
175 \core_completion\api::update_completion_date_event($cmid, 'page', $data->id, $completiontimeexpected);
181 * Delete page instance.
185 function page_delete_instance($id) {
188 if (!$page = $DB->get_record('page', array('id'=>$id))) {
192 $cm = get_coursemodule_from_instance('page', $id);
193 \core_completion\api::update_completion_date_event($cm->id, 'page', $id, null);
195 // note: all context files are deleted automatically
197 $DB->delete_records('page', array('id'=>$page->id));
203 * Given a course_module object, this function returns any
204 * "extra" information that may be needed when printing
205 * this activity in a course listing.
207 * See {@link get_array_of_activities()} in course/lib.php
209 * @param stdClass $coursemodule
210 * @return cached_cm_info Info to customise main page display
212 function page_get_coursemodule_info($coursemodule) {
214 require_once("$CFG->libdir/resourcelib.php");
216 if (!$page = $DB->get_record('page', array('id'=>$coursemodule->instance),
217 'id, name, display, displayoptions, intro, introformat')) {
221 $info = new cached_cm_info();
222 $info->name = $page->name;
224 if ($coursemodule->showdescription) {
225 // Convert intro to html. Do not filter cached version, filters run at display time.
226 $info->content = format_module_intro('page', $page, $coursemodule->id, false);
229 if ($page->display != RESOURCELIB_DISPLAY_POPUP) {
233 $fullurl = "$CFG->wwwroot/mod/page/view.php?id=$coursemodule->id&inpopup=1";
234 $options = empty($page->displayoptions) ? array() : unserialize($page->displayoptions);
235 $width = empty($options['popupwidth']) ? 620 : $options['popupwidth'];
236 $height = empty($options['popupheight']) ? 450 : $options['popupheight'];
237 $wh = "width=$width,height=$height,toolbar=no,location=no,menubar=no,copyhistory=no,status=no,directories=no,scrollbars=yes,resizable=yes";
238 $info->onclick = "window.open('$fullurl', '', '$wh'); return false;";
245 * Lists all browsable file areas
249 * @param stdClass $course course object
250 * @param stdClass $cm course module object
251 * @param stdClass $context context object
254 function page_get_file_areas($course, $cm, $context) {
256 $areas['content'] = get_string('content', 'page');
261 * File browsing support for page module content area.
265 * @param stdClass $browser file browser instance
266 * @param stdClass $areas file areas
267 * @param stdClass $course course object
268 * @param stdClass $cm course module object
269 * @param stdClass $context context object
270 * @param string $filearea file area
271 * @param int $itemid item ID
272 * @param string $filepath file path
273 * @param string $filename file name
274 * @return file_info instance or null if not found
276 function page_get_file_info($browser, $areas, $course, $cm, $context, $filearea, $itemid, $filepath, $filename) {
279 if (!has_capability('moodle/course:managefiles', $context)) {
280 // students can not peak here!
284 $fs = get_file_storage();
286 if ($filearea === 'content') {
287 $filepath = is_null($filepath) ? '/' : $filepath;
288 $filename = is_null($filename) ? '.' : $filename;
290 $urlbase = $CFG->wwwroot.'/pluginfile.php';
291 if (!$storedfile = $fs->get_file($context->id, 'mod_page', 'content', 0, $filepath, $filename)) {
292 if ($filepath === '/' and $filename === '.') {
293 $storedfile = new virtual_root_file($context->id, 'mod_page', 'content', 0);
299 require_once("$CFG->dirroot/mod/page/locallib.php");
300 return new page_content_file_info($browser, $context, $storedfile, $urlbase, $areas[$filearea], true, true, true, false);
303 // note: page_intro handled in file_browser automatically
309 * Serves the page files.
313 * @param stdClass $course course object
314 * @param stdClass $cm course module object
315 * @param stdClass $context context object
316 * @param string $filearea file area
317 * @param array $args extra arguments
318 * @param bool $forcedownload whether or not force download
319 * @param array $options additional options affecting the file serving
320 * @return bool false if file not found, does not return if found - just send the file
322 function page_pluginfile($course, $cm, $context, $filearea, $args, $forcedownload, array $options=array()) {
324 require_once("$CFG->libdir/resourcelib.php");
326 if ($context->contextlevel != CONTEXT_MODULE) {
330 require_course_login($course, true, $cm);
331 if (!has_capability('mod/page:view', $context)) {
335 if ($filearea !== 'content') {
336 // intro is handled automatically in pluginfile.php
340 // $arg could be revision number or index.html
341 $arg = array_shift($args);
342 if ($arg == 'index.html' || $arg == 'index.htm') {
343 // serve page content
346 if (!$page = $DB->get_record('page', array('id'=>$cm->instance), '*', MUST_EXIST)) {
350 // We need to rewrite the pluginfile URLs so the media filters can work.
351 $content = file_rewrite_pluginfile_urls($page->content, 'webservice/pluginfile.php', $context->id, 'mod_page', 'content',
353 $formatoptions = new stdClass;
354 $formatoptions->noclean = true;
355 $formatoptions->overflowdiv = true;
356 $formatoptions->context = $context;
357 $content = format_text($content, $page->contentformat, $formatoptions);
359 // Remove @@PLUGINFILE@@/.
360 $options = array('reverse' => true);
361 $content = file_rewrite_pluginfile_urls($content, 'webservice/pluginfile.php', $context->id, 'mod_page', 'content',
362 $page->revision, $options);
363 $content = str_replace('@@PLUGINFILE@@/', '', $content);
365 send_file($content, $filename, 0, 0, true, true);
367 $fs = get_file_storage();
368 $relativepath = implode('/', $args);
369 $fullpath = "/$context->id/mod_page/$filearea/0/$relativepath";
370 if (!$file = $fs->get_file_by_hash(sha1($fullpath)) or $file->is_directory()) {
371 $page = $DB->get_record('page', array('id'=>$cm->instance), 'id, legacyfiles', MUST_EXIST);
372 if ($page->legacyfiles != RESOURCELIB_LEGACYFILES_ACTIVE) {
375 if (!$file = resourcelib_try_file_migration('/'.$relativepath, $cm->id, $cm->course, 'mod_page', 'content', 0)) {
378 //file migrate - update flag
379 $page->legacyfileslast = time();
380 $DB->update_record('page', $page);
383 // finally send the file
384 send_stored_file($file, null, 0, $forcedownload, $options);
389 * Return a list of page types
390 * @param string $pagetype current page type
391 * @param stdClass $parentcontext Block's parent context
392 * @param stdClass $currentcontext Current context of block
394 function page_page_type_list($pagetype, $parentcontext, $currentcontext) {
395 $module_pagetype = array('mod-page-*'=>get_string('page-mod-page-x', 'page'));
396 return $module_pagetype;
400 * Export page resource contents
402 * @return array of file content
404 function page_export_contents($cm, $baseurl) {
407 $context = context_module::instance($cm->id);
409 $page = $DB->get_record('page', array('id'=>$cm->instance), '*', MUST_EXIST);
412 $fs = get_file_storage();
413 $files = $fs->get_area_files($context->id, 'mod_page', 'content', 0, 'sortorder DESC, id ASC', false);
414 foreach ($files as $fileinfo) {
416 $file['type'] = 'file';
417 $file['filename'] = $fileinfo->get_filename();
418 $file['filepath'] = $fileinfo->get_filepath();
419 $file['filesize'] = $fileinfo->get_filesize();
420 $file['fileurl'] = file_encode_url("$CFG->wwwroot/" . $baseurl, '/'.$context->id.'/mod_page/content/'.$page->revision.$fileinfo->get_filepath().$fileinfo->get_filename(), true);
421 $file['timecreated'] = $fileinfo->get_timecreated();
422 $file['timemodified'] = $fileinfo->get_timemodified();
423 $file['sortorder'] = $fileinfo->get_sortorder();
424 $file['userid'] = $fileinfo->get_userid();
425 $file['author'] = $fileinfo->get_author();
426 $file['license'] = $fileinfo->get_license();
427 $file['mimetype'] = $fileinfo->get_mimetype();
428 $file['isexternalfile'] = $fileinfo->is_external_file();
429 if ($file['isexternalfile']) {
430 $file['repositorytype'] = $fileinfo->get_repository_type();
436 $filename = 'index.html';
438 $pagefile['type'] = 'file';
439 $pagefile['filename'] = $filename;
440 $pagefile['filepath'] = '/';
441 $pagefile['filesize'] = 0;
442 $pagefile['fileurl'] = file_encode_url("$CFG->wwwroot/" . $baseurl, '/'.$context->id.'/mod_page/content/' . $filename, true);
443 $pagefile['timecreated'] = null;
444 $pagefile['timemodified'] = $page->timemodified;
445 // make this file as main file
446 $pagefile['sortorder'] = 1;
447 $pagefile['userid'] = null;
448 $pagefile['author'] = null;
449 $pagefile['license'] = null;
450 $contents[] = $pagefile;
456 * Register the ability to handle drag and drop file uploads
457 * @return array containing details of the files / types the mod can handle
459 function page_dndupload_register() {
460 return array('types' => array(
461 array('identifier' => 'text/html', 'message' => get_string('createpage', 'page')),
462 array('identifier' => 'text', 'message' => get_string('createpage', 'page'))
467 * Handle a file that has been uploaded
468 * @param object $uploadinfo details of the file / content that has been uploaded
469 * @return int instance id of the newly created mod
471 function page_dndupload_handle($uploadinfo) {
472 // Gather the required info.
473 $data = new stdClass();
474 $data->course = $uploadinfo->course->id;
475 $data->name = $uploadinfo->displayname;
476 $data->intro = '<p>'.$uploadinfo->displayname.'</p>';
477 $data->introformat = FORMAT_HTML;
478 if ($uploadinfo->type == 'text/html') {
479 $data->contentformat = FORMAT_HTML;
480 $data->content = clean_param($uploadinfo->content, PARAM_CLEANHTML);
482 $data->contentformat = FORMAT_PLAIN;
483 $data->content = clean_param($uploadinfo->content, PARAM_TEXT);
485 $data->coursemodule = $uploadinfo->coursemodule;
487 // Set the display options to the site defaults.
488 $config = get_config('page');
489 $data->display = $config->display;
490 $data->popupheight = $config->popupheight;
491 $data->popupwidth = $config->popupwidth;
492 $data->printheading = $config->printheading;
493 $data->printintro = $config->printintro;
495 return page_add_instance($data, null);
499 * Mark the activity completed (if required) and trigger the course_module_viewed event.
501 * @param stdClass $page page object
502 * @param stdClass $course course object
503 * @param stdClass $cm course module object
504 * @param stdClass $context context object
507 function page_view($page, $course, $cm, $context) {
509 // Trigger course_module_viewed event.
511 'context' => $context,
512 'objectid' => $page->id
515 $event = \mod_page\event\course_module_viewed::create($params);
516 $event->add_record_snapshot('course_modules', $cm);
517 $event->add_record_snapshot('course', $course);
518 $event->add_record_snapshot('page', $page);
522 $completion = new completion_info($course);
523 $completion->set_module_viewed($cm);
527 * Check if the module has any update that affects the current user since a given time.
529 * @param cm_info $cm course module data
530 * @param int $from the time to check updates from
531 * @param array $filter if we need to check only specific updates
532 * @return stdClass an object with the different type of areas indicating if they were updated or not
535 function page_check_updates_since(cm_info $cm, $from, $filter = array()) {
536 $updates = course_check_module_updates_since($cm, $from, array('content'), $filter);
541 * Handles creating actions for events.
543 * @param calendar_event $event
544 * @param \core_calendar\action_factory $factory
545 * @return \core_calendar\local\event\value_objects\action|\core_calendar\local\interfaces\action_interface|null
547 function mod_page_core_calendar_provide_event_action(calendar_event $event,
548 \core_calendar\action_factory $factory) {
549 $cm = get_fast_modinfo($event->courseid)->instances['page'][$event->instance];
551 $course = new stdClass();
552 $course->id = $event->courseid;
553 $completion = new \completion_info($course);
555 $completiondata = $completion->get_data($cm, false);
557 if ($completiondata->completionstate != COMPLETION_INCOMPLETE) {
561 return $factory->create_instance(
563 new \moodle_url('/mod/page/view.php', ['id' => $cm->id]),