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['printheading'] = $data->printheading
;
101 $displayoptions['printintro'] = $data->printintro
;
102 $data->displayoptions
= serialize($displayoptions);
105 $data->content
= $data->page
['text'];
106 $data->contentformat
= $data->page
['format'];
109 $data->id
= $DB->insert_record('page', $data);
111 // we need to use context now, so we need to make sure all needed info is already in db
112 $DB->set_field('course_modules', 'instance', $data->id
, array('id'=>$cmid));
113 $context = context_module
::instance($cmid);
115 if ($mform and !empty($data->page
['itemid'])) {
116 $draftitemid = $data->page
['itemid'];
117 $data->content
= file_save_draft_area_files($draftitemid, $context->id
, 'mod_page', 'content', 0, page_get_editor_options($context), $data->content
);
118 $DB->update_record('page', $data);
125 * Update page instance.
126 * @param object $data
127 * @param object $mform
130 function page_update_instance($data, $mform) {
132 require_once("$CFG->libdir/resourcelib.php");
134 $cmid = $data->coursemodule
;
135 $draftitemid = $data->page
['itemid'];
137 $data->timemodified
= time();
138 $data->id
= $data->instance
;
141 $displayoptions = array();
142 if ($data->display
== RESOURCELIB_DISPLAY_POPUP
) {
143 $displayoptions['popupwidth'] = $data->popupwidth
;
144 $displayoptions['popupheight'] = $data->popupheight
;
146 $displayoptions['printheading'] = $data->printheading
;
147 $displayoptions['printintro'] = $data->printintro
;
148 $data->displayoptions
= serialize($displayoptions);
150 $data->content
= $data->page
['text'];
151 $data->contentformat
= $data->page
['format'];
153 $DB->update_record('page', $data);
155 $context = context_module
::instance($cmid);
157 $data->content
= file_save_draft_area_files($draftitemid, $context->id
, 'mod_page', 'content', 0, page_get_editor_options($context), $data->content
);
158 $DB->update_record('page', $data);
165 * Delete page instance.
169 function page_delete_instance($id) {
172 if (!$page = $DB->get_record('page', array('id'=>$id))) {
176 // note: all context files are deleted automatically
178 $DB->delete_records('page', array('id'=>$page->id
));
185 * @param object $course
186 * @param object $user
188 * @param object $page
189 * @return object|null
191 function page_user_outline($course, $user, $mod, $page) {
194 if ($logs = $DB->get_records('log', array('userid'=>$user->id
, 'module'=>'page',
195 'action'=>'view', 'info'=>$page->id
), 'time ASC')) {
197 $numviews = count($logs);
198 $lastlog = array_pop($logs);
200 $result = new stdClass();
201 $result->info
= get_string('numviews', '', $numviews);
202 $result->time
= $lastlog->time
;
210 * Return use complete
211 * @param object $course
212 * @param object $user
214 * @param object $page
216 function page_user_complete($course, $user, $mod, $page) {
219 if ($logs = $DB->get_records('log', array('userid'=>$user->id
, 'module'=>'page',
220 'action'=>'view', 'info'=>$page->id
), 'time ASC')) {
221 $numviews = count($logs);
222 $lastlog = array_pop($logs);
224 $strmostrecently = get_string('mostrecently');
225 $strnumviews = get_string('numviews', '', $numviews);
227 echo "$strnumviews - $strmostrecently ".userdate($lastlog->time
);
230 print_string('neverseen', 'page');
235 * Given a course_module object, this function returns any
236 * "extra" information that may be needed when printing
237 * this activity in a course listing.
239 * See {@link get_array_of_activities()} in course/lib.php
241 * @param cm_info $coursemodule
242 * @return cached_cm_info Info to customise main page display
244 function page_get_coursemodule_info($coursemodule) {
246 require_once("$CFG->libdir/resourcelib.php");
248 if (!$page = $DB->get_record('page', array('id'=>$coursemodule->instance
),
249 'id, name, display, displayoptions, intro, introformat')) {
253 $info = new cached_cm_info();
254 $info->name
= $page->name
;
256 if ($coursemodule->showdescription
) {
257 // Convert intro to html. Do not filter cached version, filters run at display time.
258 $info->content
= format_module_intro('page', $page, $coursemodule->id
, false);
261 if ($page->display
!= RESOURCELIB_DISPLAY_POPUP
) {
265 $fullurl = "$CFG->wwwroot/mod/page/view.php?id=$coursemodule->id&inpopup=1";
266 $options = empty($page->displayoptions
) ?
array() : unserialize($page->displayoptions
);
267 $width = empty($options['popupwidth']) ?
620 : $options['popupwidth'];
268 $height = empty($options['popupheight']) ?
450 : $options['popupheight'];
269 $wh = "width=$width,height=$height,toolbar=no,location=no,menubar=no,copyhistory=no,status=no,directories=no,scrollbars=yes,resizable=yes";
270 $info->onclick
= "window.open('$fullurl', '', '$wh'); return false;";
277 * Lists all browsable file areas
281 * @param stdClass $course course object
282 * @param stdClass $cm course module object
283 * @param stdClass $context context object
286 function page_get_file_areas($course, $cm, $context) {
288 $areas['content'] = get_string('content', 'page');
293 * File browsing support for page module content area.
297 * @param stdClass $browser file browser instance
298 * @param stdClass $areas file areas
299 * @param stdClass $course course object
300 * @param stdClass $cm course module object
301 * @param stdClass $context context object
302 * @param string $filearea file area
303 * @param int $itemid item ID
304 * @param string $filepath file path
305 * @param string $filename file name
306 * @return file_info instance or null if not found
308 function page_get_file_info($browser, $areas, $course, $cm, $context, $filearea, $itemid, $filepath, $filename) {
311 if (!has_capability('moodle/course:managefiles', $context)) {
312 // students can not peak here!
316 $fs = get_file_storage();
318 if ($filearea === 'content') {
319 $filepath = is_null($filepath) ?
'/' : $filepath;
320 $filename = is_null($filename) ?
'.' : $filename;
322 $urlbase = $CFG->wwwroot
.'/pluginfile.php';
323 if (!$storedfile = $fs->get_file($context->id
, 'mod_page', 'content', 0, $filepath, $filename)) {
324 if ($filepath === '/' and $filename === '.') {
325 $storedfile = new virtual_root_file($context->id
, 'mod_page', 'content', 0);
331 require_once("$CFG->dirroot/mod/page/locallib.php");
332 return new page_content_file_info($browser, $context, $storedfile, $urlbase, $areas[$filearea], true, true, true, false);
335 // note: page_intro handled in file_browser automatically
341 * Serves the page files.
345 * @param stdClass $course course object
346 * @param stdClass $cm course module object
347 * @param stdClass $context context object
348 * @param string $filearea file area
349 * @param array $args extra arguments
350 * @param bool $forcedownload whether or not force download
351 * @param array $options additional options affecting the file serving
352 * @return bool false if file not found, does not return if found - just send the file
354 function page_pluginfile($course, $cm, $context, $filearea, $args, $forcedownload, array $options=array()) {
356 require_once("$CFG->libdir/resourcelib.php");
358 if ($context->contextlevel
!= CONTEXT_MODULE
) {
362 require_course_login($course, true, $cm);
363 if (!has_capability('mod/page:view', $context)) {
367 if ($filearea !== 'content') {
368 // intro is handled automatically in pluginfile.php
372 // $arg could be revision number or index.html
373 $arg = array_shift($args);
374 if ($arg == 'index.html' ||
$arg == 'index.htm') {
375 // serve page content
378 if (!$page = $DB->get_record('page', array('id'=>$cm->instance
), '*', MUST_EXIST
)) {
382 // remove @@PLUGINFILE@@/
383 $content = str_replace('@@PLUGINFILE@@/', '', $page->content
);
385 $formatoptions = new stdClass
;
386 $formatoptions->noclean
= true;
387 $formatoptions->overflowdiv
= true;
388 $formatoptions->context
= $context;
389 $content = format_text($content, $page->contentformat
, $formatoptions);
391 send_file($content, $filename, 0, 0, true, true);
393 $fs = get_file_storage();
394 $relativepath = implode('/', $args);
395 $fullpath = "/$context->id/mod_page/$filearea/0/$relativepath";
396 if (!$file = $fs->get_file_by_hash(sha1($fullpath)) or $file->is_directory()) {
397 $page = $DB->get_record('page', array('id'=>$cm->instance
), 'id, legacyfiles', MUST_EXIST
);
398 if ($page->legacyfiles
!= RESOURCELIB_LEGACYFILES_ACTIVE
) {
401 if (!$file = resourcelib_try_file_migration('/'.$relativepath, $cm->id
, $cm->course
, 'mod_page', 'content', 0)) {
404 //file migrate - update flag
405 $page->legacyfileslast
= time();
406 $DB->update_record('page', $page);
409 // finally send the file
410 send_stored_file($file, 86400, 0, $forcedownload, $options);
415 * Return a list of page types
416 * @param string $pagetype current page type
417 * @param stdClass $parentcontext Block's parent context
418 * @param stdClass $currentcontext Current context of block
420 function page_page_type_list($pagetype, $parentcontext, $currentcontext) {
421 $module_pagetype = array('mod-page-*'=>get_string('page-mod-page-x', 'page'));
422 return $module_pagetype;
426 * Export page resource contents
428 * @return array of file content
430 function page_export_contents($cm, $baseurl) {
433 $context = context_module
::instance($cm->id
);
435 $page = $DB->get_record('page', array('id'=>$cm->instance
), '*', MUST_EXIST
);
438 $fs = get_file_storage();
439 $files = $fs->get_area_files($context->id
, 'mod_page', 'content', 0, 'sortorder DESC, id ASC', false);
440 foreach ($files as $fileinfo) {
442 $file['type'] = 'file';
443 $file['filename'] = $fileinfo->get_filename();
444 $file['filepath'] = $fileinfo->get_filepath();
445 $file['filesize'] = $fileinfo->get_filesize();
446 $file['fileurl'] = file_encode_url("$CFG->wwwroot/" . $baseurl, '/'.$context->id
.'/mod_page/content/'.$page->revision
.$fileinfo->get_filepath().$fileinfo->get_filename(), true);
447 $file['timecreated'] = $fileinfo->get_timecreated();
448 $file['timemodified'] = $fileinfo->get_timemodified();
449 $file['sortorder'] = $fileinfo->get_sortorder();
450 $file['userid'] = $fileinfo->get_userid();
451 $file['author'] = $fileinfo->get_author();
452 $file['license'] = $fileinfo->get_license();
457 $filename = 'index.html';
459 $pagefile['type'] = 'file';
460 $pagefile['filename'] = $filename;
461 $pagefile['filepath'] = '/';
462 $pagefile['filesize'] = 0;
463 $pagefile['fileurl'] = file_encode_url("$CFG->wwwroot/" . $baseurl, '/'.$context->id
.'/mod_page/content/' . $filename, true);
464 $pagefile['timecreated'] = null;
465 $pagefile['timemodified'] = $page->timemodified
;
466 // make this file as main file
467 $pagefile['sortorder'] = 1;
468 $pagefile['userid'] = null;
469 $pagefile['author'] = null;
470 $pagefile['license'] = null;
471 $contents[] = $pagefile;
477 * Register the ability to handle drag and drop file uploads
478 * @return array containing details of the files / types the mod can handle
480 function page_dndupload_register() {
481 return array('types' => array(
482 array('identifier' => 'text/html', 'message' => get_string('createpage', 'page')),
483 array('identifier' => 'text', 'message' => get_string('createpage', 'page'))
488 * Handle a file that has been uploaded
489 * @param object $uploadinfo details of the file / content that has been uploaded
490 * @return int instance id of the newly created mod
492 function page_dndupload_handle($uploadinfo) {
493 // Gather the required info.
494 $data = new stdClass();
495 $data->course
= $uploadinfo->course
->id
;
496 $data->name
= $uploadinfo->displayname
;
497 $data->intro
= '<p>'.$uploadinfo->displayname
.'</p>';
498 $data->introformat
= FORMAT_HTML
;
499 if ($uploadinfo->type
== 'text/html') {
500 $data->contentformat
= FORMAT_HTML
;
501 $data->content
= clean_param($uploadinfo->content
, PARAM_CLEANHTML
);
503 $data->contentformat
= FORMAT_PLAIN
;
504 $data->content
= clean_param($uploadinfo->content
, PARAM_TEXT
);
506 $data->coursemodule
= $uploadinfo->coursemodule
;
508 // Set the display options to the site defaults.
509 $config = get_config('page');
510 $data->display
= $config->display
;
511 $data->popupheight
= $config->popupheight
;
512 $data->popupwidth
= $config->popupwidth
;
513 $data->printheading
= $config->printheading
;
514 $data->printintro
= $config->printintro
;
516 return page_add_instance($data, null);