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;
49 * Returns all other caps used in module
52 function page_get_extra_capabilities() {
53 return array('moodle/site:accessallgroups');
57 * This function is used by the reset_course_userdata function in moodlelib.
58 * @param $data the data submitted from the reset course.
59 * @return array status array
61 function page_reset_userdata($data) {
66 * List of view style log actions
69 function page_get_view_actions() {
70 return array('view','view all');
74 * List of update style log actions
77 function page_get_post_actions() {
78 return array('update', 'add');
84 * @param object $mform
85 * @return int new page instance id
87 function page_add_instance($data, $mform) {
89 require_once("$CFG->libdir/resourcelib.php");
91 $cmid = $data->coursemodule
;
92 $draftitemid = $data->page
['itemid'];
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);
104 $data->content
= $data->page
['text'];
105 $data->contentformat
= $data->page
['format'];
107 $data->id
= $DB->insert_record('page', $data);
109 // we need to use context now, so we need to make sure all needed info is already in db
110 $DB->set_field('course_modules', 'instance', $data->id
, array('id'=>$cmid));
111 $context = get_context_instance(CONTEXT_MODULE
, $cmid);
114 $data->content
= file_save_draft_area_files($draftitemid, $context->id
, 'mod_page', 'content', 0, page_get_editor_options($context), $data->content
);
115 $DB->update_record('page', $data);
122 * Update page instance.
123 * @param object $data
124 * @param object $mform
127 function page_update_instance($data, $mform) {
129 require_once("$CFG->libdir/resourcelib.php");
131 $cmid = $data->coursemodule
;
132 $draftitemid = $data->page
['itemid'];
134 $data->timemodified
= time();
135 $data->id
= $data->instance
;
138 $displayoptions = array();
139 if ($data->display
== RESOURCELIB_DISPLAY_POPUP
) {
140 $displayoptions['popupwidth'] = $data->popupwidth
;
141 $displayoptions['popupheight'] = $data->popupheight
;
143 $displayoptions['printheading'] = $data->printheading
;
144 $displayoptions['printintro'] = $data->printintro
;
145 $data->displayoptions
= serialize($displayoptions);
147 $data->content
= $data->page
['text'];
148 $data->contentformat
= $data->page
['format'];
150 $DB->update_record('page', $data);
152 $context = get_context_instance(CONTEXT_MODULE
, $cmid);
154 $data->content
= file_save_draft_area_files($draftitemid, $context->id
, 'mod_page', 'content', 0, page_get_editor_options($context), $data->content
);
155 $DB->update_record('page', $data);
162 * Delete page instance.
166 function page_delete_instance($id) {
169 if (!$page = $DB->get_record('page', array('id'=>$id))) {
173 // note: all context files are deleted automatically
175 $DB->delete_records('page', array('id'=>$page->id
));
182 * @param object $course
183 * @param object $user
185 * @param object $page
186 * @return object|null
188 function page_user_outline($course, $user, $mod, $page) {
191 if ($logs = $DB->get_records('log', array('userid'=>$user->id
, 'module'=>'page',
192 'action'=>'view', 'info'=>$page->id
), 'time ASC')) {
194 $numviews = count($logs);
195 $lastlog = array_pop($logs);
197 $result = new stdClass();
198 $result->info
= get_string('numviews', '', $numviews);
199 $result->time
= $lastlog->time
;
207 * Return use complete
208 * @param object $course
209 * @param object $user
211 * @param object $page
213 function page_user_complete($course, $user, $mod, $page) {
216 if ($logs = $DB->get_records('log', array('userid'=>$user->id
, 'module'=>'page',
217 'action'=>'view', 'info'=>$page->id
), 'time ASC')) {
218 $numviews = count($logs);
219 $lastlog = array_pop($logs);
221 $strmostrecently = get_string('mostrecently');
222 $strnumviews = get_string('numviews', '', $numviews);
224 echo "$strnumviews - $strmostrecently ".userdate($lastlog->time
);
227 print_string('neverseen', 'page');
232 * Returns the users with data in one page
237 function page_get_participants($pageid) {
242 * Given a course_module object, this function returns any
243 * "extra" information that may be needed when printing
244 * this activity in a course listing.
246 * See {@link get_array_of_activities()} in course/lib.php
248 * @param object $coursemodule
249 * @return object info
251 function page_get_coursemodule_info($coursemodule) {
253 require_once("$CFG->libdir/resourcelib.php");
255 if (!$page = $DB->get_record('page', array('id'=>$coursemodule->instance
), 'id, name, display, displayoptions')) {
259 $info = new stdClass();
260 $info->name
= $page->name
;
262 if ($page->display
!= RESOURCELIB_DISPLAY_POPUP
) {
266 $fullurl = "$CFG->wwwroot/mod/page/view.php?id=$coursemodule->id&inpopup=1";
267 $options = empty($page->displayoptions
) ?
array() : unserialize($page->displayoptions
);
268 $width = empty($options['popupwidth']) ?
620 : $options['popupwidth'];
269 $height = empty($options['popupheight']) ?
450 : $options['popupheight'];
270 $wh = "width=$width,height=$height,toolbar=no,location=no,menubar=no,copyhistory=no,status=no,directories=no,scrollbars=yes,resizable=yes";
271 $info->extra
= "onclick=\"window.open('$fullurl', '', '$wh'); return false;\"";
278 * Lists all browsable file areas
279 * @param object $course
281 * @param object $context
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.
292 * @param object $browser
293 * @param object $areas
294 * @param object $course
296 * @param object $context
297 * @param string $filearea
299 * @param string $filepath
300 * @param string $filename
301 * @return object file_info instance or null if not found
303 function page_get_file_info($browser, $areas, $course, $cm, $context, $filearea, $itemid, $filepath, $filename) {
306 if (!has_capability('moodle/course:managefiles', $context)) {
307 // students can not peak here!
311 $fs = get_file_storage();
313 if ($filearea === 'content') {
314 $filepath = is_null($filepath) ?
'/' : $filepath;
315 $filename = is_null($filename) ?
'.' : $filename;
317 $urlbase = $CFG->wwwroot
.'/pluginfile.php';
318 if (!$storedfile = $fs->get_file($context->id
, 'mod_page', 'content', 0, $filepath, $filename)) {
319 if ($filepath === '/' and $filename === '.') {
320 $storedfile = new virtual_root_file($context->id
, 'mod_page', 'content', 0);
326 require_once("$CFG->dirroot/mod/page/locallib.php");
327 return new page_content_file_info($browser, $context, $storedfile, $urlbase, $areas[$filearea], true, true, true, false);
330 // note: page_intro handled in file_browser automatically
336 * Serves the page files.
337 * @param object $course
339 * @param object $context
340 * @param string $filearea
342 * @param bool $forcedownload
343 * @return bool false if file not found, does not return if found - just send the file
345 function page_pluginfile($course, $cm, $context, $filearea, $args, $forcedownload) {
347 require_once("$CFG->libdir/resourcelib.php");
349 if ($context->contextlevel
!= CONTEXT_MODULE
) {
353 require_course_login($course, true, $cm);
354 if (!has_capability('mod/page:view', $context)) {
358 if ($filearea !== 'content') {
359 // intro is handled automatically in pluginfile.php
363 array_shift($args); // ignore revision - designed to prevent caching problems only
365 $fs = get_file_storage();
366 $relativepath = implode('/', $args);
367 $fullpath = "/$context->id/mod_page/$filearea/0/$relativepath";
368 if (!$file = $fs->get_file_by_hash(sha1($fullpath)) or $file->is_directory()) {
369 $page = $DB->get_record('page', array('id'=>$cm->instance
), 'id, legacyfiles', MUST_EXIST
);
370 if ($page->legacyfiles
!= RESOURCELIB_LEGACYFILES_ACTIVE
) {
373 if (!$file = resourcelib_try_file_migration('/'.$relativepath, $cm->id
, $cm->course
, 'mod_page', 'content', 0)) {
376 //file migrate - update flag
377 $page->legacyfileslast
= time();
378 $DB->update_record('page', $page);
381 // finally send the file
382 send_stored_file($file, 86400, 0, $forcedownload);
387 * This function extends the global navigation for the site.
388 * It is important to note that you should not rely on PAGE objects within this
389 * body of code as there is no guarantee that during an AJAX request they are
392 * @param navigation_node $navigation The page node within the global navigation
393 * @param stdClass $course The course object returned from the DB
394 * @param stdClass $module The module object returned from the DB
395 * @param stdClass $cm The course module instance returned from the DB
397 function page_extend_navigation($navigation, $course, $module, $cm) {
399 * This is currently just a stub so that it can be easily expanded upon.
400 * When expanding just remove this comment and the line below and then add
403 $navigation->nodetype
= navigation_node
::NODETYPE_LEAF
;