Merge branch 'MDL-61033-master' of https://github.com/nashtechdev01/moodle
[moodle.git] / mod / page / lib.php
CommitLineData
1ad9ff57 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/>.
17
18/**
96930d5b 19 * @package mod_page
2c49daf7
PS
20 * @copyright 2009 Petr Skoda (http://skodak.org)
21 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
1ad9ff57 22 */
23
2c49daf7
PS
24defined('MOODLE_INTERNAL') || die;
25
1ad9ff57 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
30 */
31function 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;
1ad9ff57 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;
7db1f6b9 40 case FEATURE_BACKUP_MOODLE2: return true;
3e4c2435 41 case FEATURE_SHOW_DESCRIPTION: return true;
1ad9ff57 42
43 default: return null;
44 }
45}
46
47/**
48 * Returns all other caps used in module
49 * @return array
50 */
51function page_get_extra_capabilities() {
52 return array('moodle/site:accessallgroups');
53}
54
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
59 */
60function page_reset_userdata($data) {
0d14fcbc
AA
61
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.
64
1ad9ff57 65 return array();
66}
67
68/**
b2b4ec30
RT
69 * List the actions that correspond to a view of this module.
70 * This is used by the participation report.
71 *
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.
75 *
1ad9ff57 76 * @return array
77 */
78function page_get_view_actions() {
cf051cc4 79 return array('view','view all');
1ad9ff57 80}
81
82/**
b2b4ec30
RT
83 * List the actions that correspond to a post of this module.
84 * This is used by the participation report.
85 *
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.
89 *
1ad9ff57 90 * @return array
91 */
92function page_get_post_actions() {
cf051cc4 93 return array('update', 'add');
1ad9ff57 94}
95
96/**
97 * Add page instance.
6b04fdc0
PS
98 * @param stdClass $data
99 * @param mod_page_mod_form $mform
a4ee8005 100 * @return int new page instance id
1ad9ff57 101 */
6b04fdc0 102function page_add_instance($data, $mform = null) {
1ad9ff57 103 global $CFG, $DB;
104 require_once("$CFG->libdir/resourcelib.php");
105
6b04fdc0 106 $cmid = $data->coursemodule;
1ad9ff57 107
108 $data->timemodified = time();
109 $displayoptions = array();
110 if ($data->display == RESOURCELIB_DISPLAY_POPUP) {
111 $displayoptions['popupwidth'] = $data->popupwidth;
112 $displayoptions['popupheight'] = $data->popupheight;
113 }
846ffe60 114 $displayoptions['printheading'] = $data->printheading;
1ad9ff57 115 $displayoptions['printintro'] = $data->printintro;
116 $data->displayoptions = serialize($displayoptions);
117
6b04fdc0
PS
118 if ($mform) {
119 $data->content = $data->page['text'];
120 $data->contentformat = $data->page['format'];
121 }
1ad9ff57 122
123 $data->id = $DB->insert_record('page', $data);
124
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));
f7562e83 127 $context = context_module::instance($cmid);
1ad9ff57 128
6b04fdc0
PS
129 if ($mform and !empty($data->page['itemid'])) {
130 $draftitemid = $data->page['itemid'];
64f93798 131 $data->content = file_save_draft_area_files($draftitemid, $context->id, 'mod_page', 'content', 0, page_get_editor_options($context), $data->content);
1ad9ff57 132 $DB->update_record('page', $data);
133 }
134
b3bd7a66
MN
135 $completiontimeexpected = !empty($data->completionexpected) ? $data->completionexpected : null;
136 \core_completion\api::update_completion_date_event($cmid, 'page', $data->id, $completiontimeexpected);
137
1ad9ff57 138 return $data->id;
139}
140
141/**
142 * Update page instance.
143 * @param object $data
144 * @param object $mform
145 * @return bool true
146 */
147function page_update_instance($data, $mform) {
148 global $CFG, $DB;
149 require_once("$CFG->libdir/resourcelib.php");
150
151 $cmid = $data->coursemodule;
152 $draftitemid = $data->page['itemid'];
153
154 $data->timemodified = time();
155 $data->id = $data->instance;
156 $data->revision++;
157
158 $displayoptions = array();
159 if ($data->display == RESOURCELIB_DISPLAY_POPUP) {
160 $displayoptions['popupwidth'] = $data->popupwidth;
161 $displayoptions['popupheight'] = $data->popupheight;
162 }
846ffe60 163 $displayoptions['printheading'] = $data->printheading;
1ad9ff57 164 $displayoptions['printintro'] = $data->printintro;
165 $data->displayoptions = serialize($displayoptions);
166
167 $data->content = $data->page['text'];
168 $data->contentformat = $data->page['format'];
169
170 $DB->update_record('page', $data);
171
f7562e83 172 $context = context_module::instance($cmid);
1ad9ff57 173 if ($draftitemid) {
64f93798 174 $data->content = file_save_draft_area_files($draftitemid, $context->id, 'mod_page', 'content', 0, page_get_editor_options($context), $data->content);
1ad9ff57 175 $DB->update_record('page', $data);
176 }
177
b3bd7a66
MN
178 $completiontimeexpected = !empty($data->completionexpected) ? $data->completionexpected : null;
179 \core_completion\api::update_completion_date_event($cmid, 'page', $data->id, $completiontimeexpected);
180
1ad9ff57 181 return true;
182}
183
184/**
185 * Delete page instance.
186 * @param int $id
187 * @return bool true
188 */
189function page_delete_instance($id) {
190 global $DB;
191
192 if (!$page = $DB->get_record('page', array('id'=>$id))) {
193 return false;
194 }
195
b3bd7a66
MN
196 $cm = get_coursemodule_from_instance('page', $id);
197 \core_completion\api::update_completion_date_event($cm->id, 'page', $id, null);
198
1ad9ff57 199 // note: all context files are deleted automatically
200
201 $DB->delete_records('page', array('id'=>$page->id));
202
203 return true;
204}
205
1ad9ff57 206/**
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.
210 *
211 * See {@link get_array_of_activities()} in course/lib.php
212 *
3c620d7f 213 * @param stdClass $coursemodule
3e4c2435 214 * @return cached_cm_info Info to customise main page display
1ad9ff57 215 */
216function page_get_coursemodule_info($coursemodule) {
217 global $CFG, $DB;
218 require_once("$CFG->libdir/resourcelib.php");
219
3e4c2435 220 if (!$page = $DB->get_record('page', array('id'=>$coursemodule->instance),
221 'id, name, display, displayoptions, intro, introformat')) {
1ad9ff57 222 return NULL;
223 }
224
3e4c2435 225 $info = new cached_cm_info();
13fceaa4
SH
226 $info->name = $page->name;
227
3e4c2435 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);
231 }
232
1ad9ff57 233 if ($page->display != RESOURCELIB_DISPLAY_POPUP) {
9a9012dc 234 return $info;
1ad9ff57 235 }
236
1ad9ff57 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";
3e4c2435 242 $info->onclick = "window.open('$fullurl', '', '$wh'); return false;";
1ad9ff57 243
244 return $info;
245}
246
247
248/**
249 * Lists all browsable file areas
d2b7803e
DC
250 *
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
1ad9ff57 256 * @return array
257 */
258function page_get_file_areas($course, $cm, $context) {
259 $areas = array();
9db7f99b 260 $areas['content'] = get_string('content', 'page');
1ad9ff57 261 return $areas;
262}
263
264/**
2c49daf7 265 * File browsing support for page module content area.
d2b7803e
DC
266 *
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
1ad9ff57 279 */
280function page_get_file_info($browser, $areas, $course, $cm, $context, $filearea, $itemid, $filepath, $filename) {
281 global $CFG;
282
64f93798
PS
283 if (!has_capability('moodle/course:managefiles', $context)) {
284 // students can not peak here!
285 return null;
286 }
1ad9ff57 287
288 $fs = get_file_storage();
289
64f93798 290 if ($filearea === 'content') {
1ad9ff57 291 $filepath = is_null($filepath) ? '/' : $filepath;
292 $filename = is_null($filename) ? '.' : $filename;
293
294 $urlbase = $CFG->wwwroot.'/pluginfile.php';
64f93798 295 if (!$storedfile = $fs->get_file($context->id, 'mod_page', 'content', 0, $filepath, $filename)) {
1ad9ff57 296 if ($filepath === '/' and $filename === '.') {
64f93798 297 $storedfile = new virtual_root_file($context->id, 'mod_page', 'content', 0);
1ad9ff57 298 } else {
299 // not found
300 return null;
301 }
302 }
303 require_once("$CFG->dirroot/mod/page/locallib.php");
64f93798 304 return new page_content_file_info($browser, $context, $storedfile, $urlbase, $areas[$filearea], true, true, true, false);
1ad9ff57 305 }
306
307 // note: page_intro handled in file_browser automatically
308
309 return null;
310}
311
312/**
313 * Serves the page files.
d2b7803e
DC
314 *
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
261cbbac 323 * @param array $options additional options affecting the file serving
2c49daf7 324 * @return bool false if file not found, does not return if found - just send the file
1ad9ff57 325 */
261cbbac 326function page_pluginfile($course, $cm, $context, $filearea, $args, $forcedownload, array $options=array()) {
1ad9ff57 327 global $CFG, $DB;
328 require_once("$CFG->libdir/resourcelib.php");
329
64f93798 330 if ($context->contextlevel != CONTEXT_MODULE) {
1ad9ff57 331 return false;
332 }
333
64f93798 334 require_course_login($course, true, $cm);
0c0484de
PS
335 if (!has_capability('mod/page:view', $context)) {
336 return false;
337 }
1ad9ff57 338
64f93798
PS
339 if ($filearea !== 'content') {
340 // intro is handled automatically in pluginfile.php
1ad9ff57 341 return false;
342 }
343
ec0d6ea2
DC
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;
1ad9ff57 349
ec0d6ea2 350 if (!$page = $DB->get_record('page', array('id'=>$cm->instance), '*', MUST_EXIST)) {
1ad9ff57 351 return false;
352 }
ec0d6ea2 353
d329a1ea
JL
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);
ec0d6ea2
DC
357 $formatoptions = new stdClass;
358 $formatoptions->noclean = true;
359 $formatoptions->overflowdiv = true;
360 $formatoptions->context = $context;
361 $content = format_text($content, $page->contentformat, $formatoptions);
362
d329a1ea
JL
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);
368
ec0d6ea2
DC
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;
378 }
379 if (!$file = resourcelib_try_file_migration('/'.$relativepath, $cm->id, $cm->course, 'mod_page', 'content', 0)) {
380 return false;
381 }
382 //file migrate - update flag
383 $page->legacyfileslast = time();
384 $DB->update_record('page', $page);
1ad9ff57 385 }
1ad9ff57 386
ec0d6ea2 387 // finally send the file
0c431257 388 send_stored_file($file, null, 0, $forcedownload, $options);
ec0d6ea2 389 }
1ad9ff57 390}
92059c7e 391
b1627a92
DC
392/**
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
397 */
b38e2e28 398function page_page_type_list($pagetype, $parentcontext, $currentcontext) {
b1627a92
DC
399 $module_pagetype = array('mod-page-*'=>get_string('page-mod-page-x', 'page'));
400 return $module_pagetype;
401}
ec0d6ea2
DC
402
403/**
404 * Export page resource contents
405 *
406 * @return array of file content
407 */
408function page_export_contents($cm, $baseurl) {
409 global $CFG, $DB;
410 $contents = array();
f7562e83 411 $context = context_module::instance($cm->id);
ec0d6ea2
DC
412
413 $page = $DB->get_record('page', array('id'=>$cm->instance), '*', MUST_EXIST);
414
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();
1104a9fa
JL
431 $file['mimetype'] = $fileinfo->get_mimetype();
432 $file['isexternalfile'] = $fileinfo->is_external_file();
433 if ($file['isexternalfile']) {
434 $file['repositorytype'] = $fileinfo->get_repository_type();
435 }
ec0d6ea2
DC
436 $contents[] = $file;
437 }
438
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;
455
456 return $contents;
457}
32528f94
DS
458
459/**
460 * Register the ability to handle drag and drop file uploads
461 * @return array containing details of the files / types the mod can handle
462 */
6b8b0b2e 463function page_dndupload_register() {
32528f94
DS
464 return array('types' => array(
465 array('identifier' => 'text/html', 'message' => get_string('createpage', 'page')),
466 array('identifier' => 'text', 'message' => get_string('createpage', 'page'))
467 ));
468}
469
470/**
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
474 */
6b8b0b2e 475function page_dndupload_handle($uploadinfo) {
abed5d65
DS
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;
32528f94 482 if ($uploadinfo->type == 'text/html') {
abed5d65
DS
483 $data->contentformat = FORMAT_HTML;
484 $data->content = clean_param($uploadinfo->content, PARAM_CLEANHTML);
32528f94 485 } else {
abed5d65
DS
486 $data->contentformat = FORMAT_PLAIN;
487 $data->content = clean_param($uploadinfo->content, PARAM_TEXT);
32528f94 488 }
abed5d65 489 $data->coursemodule = $uploadinfo->coursemodule;
32528f94 490
abed5d65
DS
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;
846ffe60 496 $data->printheading = $config->printheading;
abed5d65 497 $data->printintro = $config->printintro;
32528f94 498
abed5d65 499 return page_add_instance($data, null);
32528f94 500}
caa460ee
JL
501
502/**
503 * Mark the activity completed (if required) and trigger the course_module_viewed event.
504 *
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
510 */
511function page_view($page, $course, $cm, $context) {
512
513 // Trigger course_module_viewed event.
514 $params = array(
515 'context' => $context,
516 'objectid' => $page->id
517 );
518
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();
524
525 // Completion.
526 $completion = new completion_info($course);
527 $completion->set_module_viewed($cm);
528}
02a73d76
JL
529
530/**
531 * Check if the module has any update that affects the current user since a given time.
532 *
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
538 */
539function 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;
542}
b3bd7a66
MN
543
544/**
59391e80
MN
545 * This function receives a calendar event and returns the action associated with it, or null if there is none.
546 *
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.
b3bd7a66 549 *
e1cd93ce 550 * @param calendar_event $event
b3bd7a66 551 * @param \core_calendar\action_factory $factory
01f96180 552 * @return \core_calendar\local\event\entities\action_interface|null
b3bd7a66 553 */
e1cd93ce 554function mod_page_core_calendar_provide_event_action(calendar_event $event,
b3bd7a66
MN
555 \core_calendar\action_factory $factory) {
556 $cm = get_fast_modinfo($event->courseid)->instances['page'][$event->instance];
557
50d845e3 558 $completion = new \completion_info($cm->get_course());
b3bd7a66
MN
559
560 $completiondata = $completion->get_data($cm, false);
561
562 if ($completiondata->completionstate != COMPLETION_INCOMPLETE) {
563 return null;
564 }
565
566 return $factory->create_instance(
567 get_string('view'),
568 new \moodle_url('/mod/page/view.php', ['id' => $cm->id]),
569 1,
570 true
571 );
572}