MDL-62384 privacy: Modify user contexts query for auth_oauth2
[moodle.git] / mod / page / lib.php
blob67135ad02c007202fd8950cf8295a65764a5b952
1 <?php
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.
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/>.
18 /**
19 * @package mod_page
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;
26 /**
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) {
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_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;
43 default: return null;
47 /**
48 * Returns all other caps used in module
49 * @return array
51 function page_get_extra_capabilities() {
52 return array('moodle/site:accessallgroups');
55 /**
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) {
62 // Any changes to the list of dates that needs to be rolled should be same during course restore and course reset.
63 // See MDL-9367.
65 return array();
68 /**
69 * List the actions that correspond to a view of this module.
70 * This is used by the participation report.
72 * Note: This is not used by new logging system. Event with
73 * crud = 'r' and edulevel = LEVEL_PARTICIPATING will
74 * be considered as view action.
76 * @return array
78 function page_get_view_actions() {
79 return array('view','view all');
82 /**
83 * List the actions that correspond to a post of this module.
84 * This is used by the participation report.
86 * Note: This is not used by new logging system. Event with
87 * crud = ('c' || 'u' || 'd') and edulevel = LEVEL_PARTICIPATING
88 * will be considered as post action.
90 * @return array
92 function page_get_post_actions() {
93 return array('update', 'add');
96 /**
97 * Add page instance.
98 * @param stdClass $data
99 * @param mod_page_mod_form $mform
100 * @return int new page instance id
102 function page_add_instance($data, $mform = null) {
103 global $CFG, $DB;
104 require_once("$CFG->libdir/resourcelib.php");
106 $cmid = $data->coursemodule;
108 $data->timemodified = time();
109 $displayoptions = array();
110 if ($data->display == RESOURCELIB_DISPLAY_POPUP) {
111 $displayoptions['popupwidth'] = $data->popupwidth;
112 $displayoptions['popupheight'] = $data->popupheight;
114 $displayoptions['printheading'] = $data->printheading;
115 $displayoptions['printintro'] = $data->printintro;
116 $data->displayoptions = serialize($displayoptions);
118 if ($mform) {
119 $data->content = $data->page['text'];
120 $data->contentformat = $data->page['format'];
123 $data->id = $DB->insert_record('page', $data);
125 // we need to use context now, so we need to make sure all needed info is already in db
126 $DB->set_field('course_modules', 'instance', $data->id, array('id'=>$cmid));
127 $context = context_module::instance($cmid);
129 if ($mform and !empty($data->page['itemid'])) {
130 $draftitemid = $data->page['itemid'];
131 $data->content = file_save_draft_area_files($draftitemid, $context->id, 'mod_page', 'content', 0, page_get_editor_options($context), $data->content);
132 $DB->update_record('page', $data);
135 $completiontimeexpected = !empty($data->completionexpected) ? $data->completionexpected : null;
136 \core_completion\api::update_completion_date_event($cmid, 'page', $data->id, $completiontimeexpected);
138 return $data->id;
142 * Update page instance.
143 * @param object $data
144 * @param object $mform
145 * @return bool true
147 function page_update_instance($data, $mform) {
148 global $CFG, $DB;
149 require_once("$CFG->libdir/resourcelib.php");
151 $cmid = $data->coursemodule;
152 $draftitemid = $data->page['itemid'];
154 $data->timemodified = time();
155 $data->id = $data->instance;
156 $data->revision++;
158 $displayoptions = array();
159 if ($data->display == RESOURCELIB_DISPLAY_POPUP) {
160 $displayoptions['popupwidth'] = $data->popupwidth;
161 $displayoptions['popupheight'] = $data->popupheight;
163 $displayoptions['printheading'] = $data->printheading;
164 $displayoptions['printintro'] = $data->printintro;
165 $data->displayoptions = serialize($displayoptions);
167 $data->content = $data->page['text'];
168 $data->contentformat = $data->page['format'];
170 $DB->update_record('page', $data);
172 $context = context_module::instance($cmid);
173 if ($draftitemid) {
174 $data->content = file_save_draft_area_files($draftitemid, $context->id, 'mod_page', 'content', 0, page_get_editor_options($context), $data->content);
175 $DB->update_record('page', $data);
178 $completiontimeexpected = !empty($data->completionexpected) ? $data->completionexpected : null;
179 \core_completion\api::update_completion_date_event($cmid, 'page', $data->id, $completiontimeexpected);
181 return true;
185 * Delete page instance.
186 * @param int $id
187 * @return bool true
189 function page_delete_instance($id) {
190 global $DB;
192 if (!$page = $DB->get_record('page', array('id'=>$id))) {
193 return false;
196 $cm = get_coursemodule_from_instance('page', $id);
197 \core_completion\api::update_completion_date_event($cm->id, 'page', $id, null);
199 // note: all context files are deleted automatically
201 $DB->delete_records('page', array('id'=>$page->id));
203 return true;
207 * Given a course_module object, this function returns any
208 * "extra" information that may be needed when printing
209 * this activity in a course listing.
211 * See {@link get_array_of_activities()} in course/lib.php
213 * @param stdClass $coursemodule
214 * @return cached_cm_info Info to customise main page display
216 function page_get_coursemodule_info($coursemodule) {
217 global $CFG, $DB;
218 require_once("$CFG->libdir/resourcelib.php");
220 if (!$page = $DB->get_record('page', array('id'=>$coursemodule->instance),
221 'id, name, display, displayoptions, intro, introformat')) {
222 return NULL;
225 $info = new cached_cm_info();
226 $info->name = $page->name;
228 if ($coursemodule->showdescription) {
229 // Convert intro to html. Do not filter cached version, filters run at display time.
230 $info->content = format_module_intro('page', $page, $coursemodule->id, false);
233 if ($page->display != RESOURCELIB_DISPLAY_POPUP) {
234 return $info;
237 $fullurl = "$CFG->wwwroot/mod/page/view.php?id=$coursemodule->id&amp;inpopup=1";
238 $options = empty($page->displayoptions) ? array() : unserialize($page->displayoptions);
239 $width = empty($options['popupwidth']) ? 620 : $options['popupwidth'];
240 $height = empty($options['popupheight']) ? 450 : $options['popupheight'];
241 $wh = "width=$width,height=$height,toolbar=no,location=no,menubar=no,copyhistory=no,status=no,directories=no,scrollbars=yes,resizable=yes";
242 $info->onclick = "window.open('$fullurl', '', '$wh'); return false;";
244 return $info;
249 * Lists all browsable file areas
251 * @package mod_page
252 * @category files
253 * @param stdClass $course course object
254 * @param stdClass $cm course module object
255 * @param stdClass $context context object
256 * @return array
258 function page_get_file_areas($course, $cm, $context) {
259 $areas = array();
260 $areas['content'] = get_string('content', 'page');
261 return $areas;
265 * File browsing support for page module content area.
267 * @package mod_page
268 * @category files
269 * @param stdClass $browser file browser instance
270 * @param stdClass $areas file areas
271 * @param stdClass $course course object
272 * @param stdClass $cm course module object
273 * @param stdClass $context context object
274 * @param string $filearea file area
275 * @param int $itemid item ID
276 * @param string $filepath file path
277 * @param string $filename file name
278 * @return file_info instance or null if not found
280 function page_get_file_info($browser, $areas, $course, $cm, $context, $filearea, $itemid, $filepath, $filename) {
281 global $CFG;
283 if (!has_capability('moodle/course:managefiles', $context)) {
284 // students can not peak here!
285 return null;
288 $fs = get_file_storage();
290 if ($filearea === 'content') {
291 $filepath = is_null($filepath) ? '/' : $filepath;
292 $filename = is_null($filename) ? '.' : $filename;
294 $urlbase = $CFG->wwwroot.'/pluginfile.php';
295 if (!$storedfile = $fs->get_file($context->id, 'mod_page', 'content', 0, $filepath, $filename)) {
296 if ($filepath === '/' and $filename === '.') {
297 $storedfile = new virtual_root_file($context->id, 'mod_page', 'content', 0);
298 } else {
299 // not found
300 return null;
303 require_once("$CFG->dirroot/mod/page/locallib.php");
304 return new page_content_file_info($browser, $context, $storedfile, $urlbase, $areas[$filearea], true, true, true, false);
307 // note: page_intro handled in file_browser automatically
309 return null;
313 * Serves the page files.
315 * @package mod_page
316 * @category files
317 * @param stdClass $course course object
318 * @param stdClass $cm course module object
319 * @param stdClass $context context object
320 * @param string $filearea file area
321 * @param array $args extra arguments
322 * @param bool $forcedownload whether or not force download
323 * @param array $options additional options affecting the file serving
324 * @return bool false if file not found, does not return if found - just send the file
326 function page_pluginfile($course, $cm, $context, $filearea, $args, $forcedownload, array $options=array()) {
327 global $CFG, $DB;
328 require_once("$CFG->libdir/resourcelib.php");
330 if ($context->contextlevel != CONTEXT_MODULE) {
331 return false;
334 require_course_login($course, true, $cm);
335 if (!has_capability('mod/page:view', $context)) {
336 return false;
339 if ($filearea !== 'content') {
340 // intro is handled automatically in pluginfile.php
341 return false;
344 // $arg could be revision number or index.html
345 $arg = array_shift($args);
346 if ($arg == 'index.html' || $arg == 'index.htm') {
347 // serve page content
348 $filename = $arg;
350 if (!$page = $DB->get_record('page', array('id'=>$cm->instance), '*', MUST_EXIST)) {
351 return false;
354 // We need to rewrite the pluginfile URLs so the media filters can work.
355 $content = file_rewrite_pluginfile_urls($page->content, 'webservice/pluginfile.php', $context->id, 'mod_page', 'content',
356 $page->revision);
357 $formatoptions = new stdClass;
358 $formatoptions->noclean = true;
359 $formatoptions->overflowdiv = true;
360 $formatoptions->context = $context;
361 $content = format_text($content, $page->contentformat, $formatoptions);
363 // Remove @@PLUGINFILE@@/.
364 $options = array('reverse' => true);
365 $content = file_rewrite_pluginfile_urls($content, 'webservice/pluginfile.php', $context->id, 'mod_page', 'content',
366 $page->revision, $options);
367 $content = str_replace('@@PLUGINFILE@@/', '', $content);
369 send_file($content, $filename, 0, 0, true, true);
370 } else {
371 $fs = get_file_storage();
372 $relativepath = implode('/', $args);
373 $fullpath = "/$context->id/mod_page/$filearea/0/$relativepath";
374 if (!$file = $fs->get_file_by_hash(sha1($fullpath)) or $file->is_directory()) {
375 $page = $DB->get_record('page', array('id'=>$cm->instance), 'id, legacyfiles', MUST_EXIST);
376 if ($page->legacyfiles != RESOURCELIB_LEGACYFILES_ACTIVE) {
377 return false;
379 if (!$file = resourcelib_try_file_migration('/'.$relativepath, $cm->id, $cm->course, 'mod_page', 'content', 0)) {
380 return false;
382 //file migrate - update flag
383 $page->legacyfileslast = time();
384 $DB->update_record('page', $page);
387 // finally send the file
388 send_stored_file($file, null, 0, $forcedownload, $options);
393 * Return a list of page types
394 * @param string $pagetype current page type
395 * @param stdClass $parentcontext Block's parent context
396 * @param stdClass $currentcontext Current context of block
398 function page_page_type_list($pagetype, $parentcontext, $currentcontext) {
399 $module_pagetype = array('mod-page-*'=>get_string('page-mod-page-x', 'page'));
400 return $module_pagetype;
404 * Export page resource contents
406 * @return array of file content
408 function page_export_contents($cm, $baseurl) {
409 global $CFG, $DB;
410 $contents = array();
411 $context = context_module::instance($cm->id);
413 $page = $DB->get_record('page', array('id'=>$cm->instance), '*', MUST_EXIST);
415 // page contents
416 $fs = get_file_storage();
417 $files = $fs->get_area_files($context->id, 'mod_page', 'content', 0, 'sortorder DESC, id ASC', false);
418 foreach ($files as $fileinfo) {
419 $file = array();
420 $file['type'] = 'file';
421 $file['filename'] = $fileinfo->get_filename();
422 $file['filepath'] = $fileinfo->get_filepath();
423 $file['filesize'] = $fileinfo->get_filesize();
424 $file['fileurl'] = file_encode_url("$CFG->wwwroot/" . $baseurl, '/'.$context->id.'/mod_page/content/'.$page->revision.$fileinfo->get_filepath().$fileinfo->get_filename(), true);
425 $file['timecreated'] = $fileinfo->get_timecreated();
426 $file['timemodified'] = $fileinfo->get_timemodified();
427 $file['sortorder'] = $fileinfo->get_sortorder();
428 $file['userid'] = $fileinfo->get_userid();
429 $file['author'] = $fileinfo->get_author();
430 $file['license'] = $fileinfo->get_license();
431 $file['mimetype'] = $fileinfo->get_mimetype();
432 $file['isexternalfile'] = $fileinfo->is_external_file();
433 if ($file['isexternalfile']) {
434 $file['repositorytype'] = $fileinfo->get_repository_type();
436 $contents[] = $file;
439 // page html conent
440 $filename = 'index.html';
441 $pagefile = array();
442 $pagefile['type'] = 'file';
443 $pagefile['filename'] = $filename;
444 $pagefile['filepath'] = '/';
445 $pagefile['filesize'] = 0;
446 $pagefile['fileurl'] = file_encode_url("$CFG->wwwroot/" . $baseurl, '/'.$context->id.'/mod_page/content/' . $filename, true);
447 $pagefile['timecreated'] = null;
448 $pagefile['timemodified'] = $page->timemodified;
449 // make this file as main file
450 $pagefile['sortorder'] = 1;
451 $pagefile['userid'] = null;
452 $pagefile['author'] = null;
453 $pagefile['license'] = null;
454 $contents[] = $pagefile;
456 return $contents;
460 * Register the ability to handle drag and drop file uploads
461 * @return array containing details of the files / types the mod can handle
463 function page_dndupload_register() {
464 return array('types' => array(
465 array('identifier' => 'text/html', 'message' => get_string('createpage', 'page')),
466 array('identifier' => 'text', 'message' => get_string('createpage', 'page'))
471 * Handle a file that has been uploaded
472 * @param object $uploadinfo details of the file / content that has been uploaded
473 * @return int instance id of the newly created mod
475 function page_dndupload_handle($uploadinfo) {
476 // Gather the required info.
477 $data = new stdClass();
478 $data->course = $uploadinfo->course->id;
479 $data->name = $uploadinfo->displayname;
480 $data->intro = '<p>'.$uploadinfo->displayname.'</p>';
481 $data->introformat = FORMAT_HTML;
482 if ($uploadinfo->type == 'text/html') {
483 $data->contentformat = FORMAT_HTML;
484 $data->content = clean_param($uploadinfo->content, PARAM_CLEANHTML);
485 } else {
486 $data->contentformat = FORMAT_PLAIN;
487 $data->content = clean_param($uploadinfo->content, PARAM_TEXT);
489 $data->coursemodule = $uploadinfo->coursemodule;
491 // Set the display options to the site defaults.
492 $config = get_config('page');
493 $data->display = $config->display;
494 $data->popupheight = $config->popupheight;
495 $data->popupwidth = $config->popupwidth;
496 $data->printheading = $config->printheading;
497 $data->printintro = $config->printintro;
499 return page_add_instance($data, null);
503 * Mark the activity completed (if required) and trigger the course_module_viewed event.
505 * @param stdClass $page page object
506 * @param stdClass $course course object
507 * @param stdClass $cm course module object
508 * @param stdClass $context context object
509 * @since Moodle 3.0
511 function page_view($page, $course, $cm, $context) {
513 // Trigger course_module_viewed event.
514 $params = array(
515 'context' => $context,
516 'objectid' => $page->id
519 $event = \mod_page\event\course_module_viewed::create($params);
520 $event->add_record_snapshot('course_modules', $cm);
521 $event->add_record_snapshot('course', $course);
522 $event->add_record_snapshot('page', $page);
523 $event->trigger();
525 // Completion.
526 $completion = new completion_info($course);
527 $completion->set_module_viewed($cm);
531 * Check if the module has any update that affects the current user since a given time.
533 * @param cm_info $cm course module data
534 * @param int $from the time to check updates from
535 * @param array $filter if we need to check only specific updates
536 * @return stdClass an object with the different type of areas indicating if they were updated or not
537 * @since Moodle 3.2
539 function page_check_updates_since(cm_info $cm, $from, $filter = array()) {
540 $updates = course_check_module_updates_since($cm, $from, array('content'), $filter);
541 return $updates;
545 * This function receives a calendar event and returns the action associated with it, or null if there is none.
547 * This is used by block_myoverview in order to display the event appropriately. If null is returned then the event
548 * is not displayed on the block.
550 * @param calendar_event $event
551 * @param \core_calendar\action_factory $factory
552 * @return \core_calendar\local\event\entities\action_interface|null
554 function mod_page_core_calendar_provide_event_action(calendar_event $event,
555 \core_calendar\action_factory $factory) {
556 $cm = get_fast_modinfo($event->courseid)->instances['page'][$event->instance];
558 $completion = new \completion_info($cm->get_course());
560 $completiondata = $completion->get_data($cm, false);
562 if ($completiondata->completionstate != COMPLETION_INCOMPLETE) {
563 return null;
566 return $factory->create_instance(
567 get_string('view'),
568 new \moodle_url('/mod/page/view.php', ['id' => $cm->id]),
570 true