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/>.
21 * @copyright 2009 Petr Skoda (http://skodak.org)
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
25 defined('MOODLE_INTERNAL') ||
die;
28 * List of features supported in Page module
29 * @param string $feature FEATURE_xx constant for requested feature
30 * @return mixed True if module supports feature, false if not, null if doesn't know
32 function page_supports($feature) {
34 case FEATURE_MOD_ARCHETYPE
: return MOD_ARCHETYPE_RESOURCE
;
35 case FEATURE_GROUPS
: return false;
36 case FEATURE_GROUPINGS
: return false;
37 case FEATURE_GROUPMEMBERSONLY
: return true;
38 case FEATURE_MOD_INTRO
: return true;
39 case FEATURE_COMPLETION_TRACKS_VIEWS
: return true;
40 case FEATURE_GRADE_HAS_GRADE
: return false;
41 case FEATURE_GRADE_OUTCOMES
: return false;
42 case FEATURE_BACKUP_MOODLE2
: return true;
43 case FEATURE_SHOW_DESCRIPTION
: return true;
50 * Returns all other caps used in module
53 function page_get_extra_capabilities() {
54 return array('moodle/site:accessallgroups');
58 * This function is used by the reset_course_userdata function in moodlelib.
59 * @param $data the data submitted from the reset course.
60 * @return array status array
62 function page_reset_userdata($data) {
67 * List of view style log actions
70 function page_get_view_actions() {
71 return array('view','view all');
75 * List of update style log actions
78 function page_get_post_actions() {
79 return array('update', 'add');
84 * @param stdClass $data
85 * @param mod_page_mod_form $mform
86 * @return int new page instance id
88 function page_add_instance($data, $mform = null) {
90 require_once("$CFG->libdir/resourcelib.php");
92 $cmid = $data->coursemodule
;
94 $data->timemodified
= time();
95 $displayoptions = array();
96 if ($data->display
== RESOURCELIB_DISPLAY_POPUP
) {
97 $displayoptions['popupwidth'] = $data->popupwidth
;
98 $displayoptions['popupheight'] = $data->popupheight
;
100 $displayoptions['printintro'] = $data->printintro
;
101 $data->displayoptions
= serialize($displayoptions);
104 $data->content
= $data->page
['text'];
105 $data->contentformat
= $data->page
['format'];
108 $data->id
= $DB->insert_record('page', $data);
110 // we need to use context now, so we need to make sure all needed info is already in db
111 $DB->set_field('course_modules', 'instance', $data->id
, array('id'=>$cmid));
112 $context = context_module
::instance($cmid);
114 if ($mform and !empty($data->page
['itemid'])) {
115 $draftitemid = $data->page
['itemid'];
116 $data->content
= file_save_draft_area_files($draftitemid, $context->id
, 'mod_page', 'content', 0, page_get_editor_options($context), $data->content
);
117 $DB->update_record('page', $data);
124 * Update page instance.
125 * @param object $data
126 * @param object $mform
129 function page_update_instance($data, $mform) {
131 require_once("$CFG->libdir/resourcelib.php");
133 $cmid = $data->coursemodule
;
134 $draftitemid = $data->page
['itemid'];
136 $data->timemodified
= time();
137 $data->id
= $data->instance
;
140 $displayoptions = array();
141 if ($data->display
== RESOURCELIB_DISPLAY_POPUP
) {
142 $displayoptions['popupwidth'] = $data->popupwidth
;
143 $displayoptions['popupheight'] = $data->popupheight
;
145 $displayoptions['printintro'] = $data->printintro
;
146 $data->displayoptions
= serialize($displayoptions);
148 $data->content
= $data->page
['text'];
149 $data->contentformat
= $data->page
['format'];
151 $DB->update_record('page', $data);
153 $context = context_module
::instance($cmid);
155 $data->content
= file_save_draft_area_files($draftitemid, $context->id
, 'mod_page', 'content', 0, page_get_editor_options($context), $data->content
);
156 $DB->update_record('page', $data);
163 * Delete page instance.
167 function page_delete_instance($id) {
170 if (!$page = $DB->get_record('page', array('id'=>$id))) {
174 // note: all context files are deleted automatically
176 $DB->delete_records('page', array('id'=>$page->id
));
183 * @param object $course
184 * @param object $user
186 * @param object $page
187 * @return object|null
189 function page_user_outline($course, $user, $mod, $page) {
192 if ($logs = $DB->get_records('log', array('userid'=>$user->id
, 'module'=>'page',
193 'action'=>'view', 'info'=>$page->id
), 'time ASC')) {
195 $numviews = count($logs);
196 $lastlog = array_pop($logs);
198 $result = new stdClass();
199 $result->info
= get_string('numviews', '', $numviews);
200 $result->time
= $lastlog->time
;
208 * Return use complete
209 * @param object $course
210 * @param object $user
212 * @param object $page
214 function page_user_complete($course, $user, $mod, $page) {
217 if ($logs = $DB->get_records('log', array('userid'=>$user->id
, 'module'=>'page',
218 'action'=>'view', 'info'=>$page->id
), 'time ASC')) {
219 $numviews = count($logs);
220 $lastlog = array_pop($logs);
222 $strmostrecently = get_string('mostrecently');
223 $strnumviews = get_string('numviews', '', $numviews);
225 echo "$strnumviews - $strmostrecently ".userdate($lastlog->time
);
228 print_string('neverseen', 'page');
233 * Given a course_module object, this function returns any
234 * "extra" information that may be needed when printing
235 * this activity in a course listing.
237 * See {@link get_array_of_activities()} in course/lib.php
239 * @param stdClass $coursemodule
240 * @return cached_cm_info Info to customise main page display
242 function page_get_coursemodule_info($coursemodule) {
244 require_once("$CFG->libdir/resourcelib.php");
246 if (!$page = $DB->get_record('page', array('id'=>$coursemodule->instance
),
247 'id, name, display, displayoptions, intro, introformat')) {
251 $info = new cached_cm_info();
252 $info->name
= $page->name
;
254 if ($coursemodule->showdescription
) {
255 // Convert intro to html. Do not filter cached version, filters run at display time.
256 $info->content
= format_module_intro('page', $page, $coursemodule->id
, false);
259 if ($page->display
!= RESOURCELIB_DISPLAY_POPUP
) {
263 $fullurl = "$CFG->wwwroot/mod/page/view.php?id=$coursemodule->id&inpopup=1";
264 $options = empty($page->displayoptions
) ?
array() : unserialize($page->displayoptions
);
265 $width = empty($options['popupwidth']) ?
620 : $options['popupwidth'];
266 $height = empty($options['popupheight']) ?
450 : $options['popupheight'];
267 $wh = "width=$width,height=$height,toolbar=no,location=no,menubar=no,copyhistory=no,status=no,directories=no,scrollbars=yes,resizable=yes";
268 $info->onclick
= "window.open('$fullurl', '', '$wh'); return false;";
275 * Lists all browsable file areas
279 * @param stdClass $course course object
280 * @param stdClass $cm course module object
281 * @param stdClass $context context object
284 function page_get_file_areas($course, $cm, $context) {
286 $areas['content'] = get_string('content', 'page');
291 * File browsing support for page module content area.
295 * @param stdClass $browser file browser instance
296 * @param stdClass $areas file areas
297 * @param stdClass $course course object
298 * @param stdClass $cm course module object
299 * @param stdClass $context context object
300 * @param string $filearea file area
301 * @param int $itemid item ID
302 * @param string $filepath file path
303 * @param string $filename file name
304 * @return file_info instance or null if not found
306 function page_get_file_info($browser, $areas, $course, $cm, $context, $filearea, $itemid, $filepath, $filename) {
309 if (!has_capability('moodle/course:managefiles', $context)) {
310 // students can not peak here!
314 $fs = get_file_storage();
316 if ($filearea === 'content') {
317 $filepath = is_null($filepath) ?
'/' : $filepath;
318 $filename = is_null($filename) ?
'.' : $filename;
320 $urlbase = $CFG->wwwroot
.'/pluginfile.php';
321 if (!$storedfile = $fs->get_file($context->id
, 'mod_page', 'content', 0, $filepath, $filename)) {
322 if ($filepath === '/' and $filename === '.') {
323 $storedfile = new virtual_root_file($context->id
, 'mod_page', 'content', 0);
329 require_once("$CFG->dirroot/mod/page/locallib.php");
330 return new page_content_file_info($browser, $context, $storedfile, $urlbase, $areas[$filearea], true, true, true, false);
333 // note: page_intro handled in file_browser automatically
339 * Serves the page files.
343 * @param stdClass $course course object
344 * @param stdClass $cm course module object
345 * @param stdClass $context context object
346 * @param string $filearea file area
347 * @param array $args extra arguments
348 * @param bool $forcedownload whether or not force download
349 * @param array $options additional options affecting the file serving
350 * @return bool false if file not found, does not return if found - just send the file
352 function page_pluginfile($course, $cm, $context, $filearea, $args, $forcedownload, array $options=array()) {
354 require_once("$CFG->libdir/resourcelib.php");
356 if ($context->contextlevel
!= CONTEXT_MODULE
) {
360 require_course_login($course, true, $cm);
361 if (!has_capability('mod/page:view', $context)) {
365 if ($filearea !== 'content') {
366 // intro is handled automatically in pluginfile.php
370 // $arg could be revision number or index.html
371 $arg = array_shift($args);
372 if ($arg == 'index.html' ||
$arg == 'index.htm') {
373 // serve page content
376 if (!$page = $DB->get_record('page', array('id'=>$cm->instance
), '*', MUST_EXIST
)) {
380 // remove @@PLUGINFILE@@/
381 $content = str_replace('@@PLUGINFILE@@/', '', $page->content
);
383 $formatoptions = new stdClass
;
384 $formatoptions->noclean
= true;
385 $formatoptions->overflowdiv
= true;
386 $formatoptions->context
= $context;
387 $content = format_text($content, $page->contentformat
, $formatoptions);
389 send_file($content, $filename, 0, 0, true, true);
391 $fs = get_file_storage();
392 $relativepath = implode('/', $args);
393 $fullpath = "/$context->id/mod_page/$filearea/0/$relativepath";
394 if (!$file = $fs->get_file_by_hash(sha1($fullpath)) or $file->is_directory()) {
395 $page = $DB->get_record('page', array('id'=>$cm->instance
), 'id, legacyfiles', MUST_EXIST
);
396 if ($page->legacyfiles
!= RESOURCELIB_LEGACYFILES_ACTIVE
) {
399 if (!$file = resourcelib_try_file_migration('/'.$relativepath, $cm->id
, $cm->course
, 'mod_page', 'content', 0)) {
402 //file migrate - update flag
403 $page->legacyfileslast
= time();
404 $DB->update_record('page', $page);
407 // finally send the file
408 send_stored_file($file, null, 0, $forcedownload, $options);
413 * Return a list of page types
414 * @param string $pagetype current page type
415 * @param stdClass $parentcontext Block's parent context
416 * @param stdClass $currentcontext Current context of block
418 function page_page_type_list($pagetype, $parentcontext, $currentcontext) {
419 $module_pagetype = array('mod-page-*'=>get_string('page-mod-page-x', 'page'));
420 return $module_pagetype;
424 * Export page resource contents
426 * @return array of file content
428 function page_export_contents($cm, $baseurl) {
431 $context = context_module
::instance($cm->id
);
433 $page = $DB->get_record('page', array('id'=>$cm->instance
), '*', MUST_EXIST
);
436 $fs = get_file_storage();
437 $files = $fs->get_area_files($context->id
, 'mod_page', 'content', 0, 'sortorder DESC, id ASC', false);
438 foreach ($files as $fileinfo) {
440 $file['type'] = 'file';
441 $file['filename'] = $fileinfo->get_filename();
442 $file['filepath'] = $fileinfo->get_filepath();
443 $file['filesize'] = $fileinfo->get_filesize();
444 $file['fileurl'] = file_encode_url("$CFG->wwwroot/" . $baseurl, '/'.$context->id
.'/mod_page/content/'.$page->revision
.$fileinfo->get_filepath().$fileinfo->get_filename(), true);
445 $file['timecreated'] = $fileinfo->get_timecreated();
446 $file['timemodified'] = $fileinfo->get_timemodified();
447 $file['sortorder'] = $fileinfo->get_sortorder();
448 $file['userid'] = $fileinfo->get_userid();
449 $file['author'] = $fileinfo->get_author();
450 $file['license'] = $fileinfo->get_license();
455 $filename = 'index.html';
457 $pagefile['type'] = 'file';
458 $pagefile['filename'] = $filename;
459 $pagefile['filepath'] = '/';
460 $pagefile['filesize'] = 0;
461 $pagefile['fileurl'] = file_encode_url("$CFG->wwwroot/" . $baseurl, '/'.$context->id
.'/mod_page/content/' . $filename, true);
462 $pagefile['timecreated'] = null;
463 $pagefile['timemodified'] = $page->timemodified
;
464 // make this file as main file
465 $pagefile['sortorder'] = 1;
466 $pagefile['userid'] = null;
467 $pagefile['author'] = null;
468 $pagefile['license'] = null;
469 $contents[] = $pagefile;
475 * Register the ability to handle drag and drop file uploads
476 * @return array containing details of the files / types the mod can handle
478 function page_dndupload_register() {
479 return array('types' => array(
480 array('identifier' => 'text/html', 'message' => get_string('createpage', 'page')),
481 array('identifier' => 'text', 'message' => get_string('createpage', 'page'))
486 * Handle a file that has been uploaded
487 * @param object $uploadinfo details of the file / content that has been uploaded
488 * @return int instance id of the newly created mod
490 function page_dndupload_handle($uploadinfo) {
491 // Gather the required info.
492 $data = new stdClass();
493 $data->course
= $uploadinfo->course
->id
;
494 $data->name
= $uploadinfo->displayname
;
495 $data->intro
= '<p>'.$uploadinfo->displayname
.'</p>';
496 $data->introformat
= FORMAT_HTML
;
497 if ($uploadinfo->type
== 'text/html') {
498 $data->contentformat
= FORMAT_HTML
;
499 $data->content
= clean_param($uploadinfo->content
, PARAM_CLEANHTML
);
501 $data->contentformat
= FORMAT_PLAIN
;
502 $data->content
= clean_param($uploadinfo->content
, PARAM_TEXT
);
504 $data->coursemodule
= $uploadinfo->coursemodule
;
506 // Set the display options to the site defaults.
507 $config = get_config('page');
508 $data->display
= $config->display
;
509 $data->popupheight
= $config->popupheight
;
510 $data->popupwidth
= $config->popupwidth
;
511 $data->printintro
= $config->printintro
;
513 return page_add_instance($data, null);