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/>.
20 * @copyright 2009 Petr Skoda (http://skodak.org)
21 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
25 * List of features supported in Page module
26 * @param string $feature FEATURE_xx constant for requested feature
27 * @return mixed True if module supports feature, false if not, null if doesn't know
29 function page_supports($feature) {
31 case FEATURE_MOD_ARCHETYPE: return MOD_ARCHETYPE_RESOURCE;
32 case FEATURE_GROUPS: return false;
33 case FEATURE_GROUPINGS: return false;
34 case FEATURE_GROUPMEMBERSONLY: return true;
35 case FEATURE_MOD_INTRO: return true;
36 case FEATURE_COMPLETION_TRACKS_VIEWS: return true;
37 case FEATURE_GRADE_HAS_GRADE: return false;
38 case FEATURE_GRADE_OUTCOMES: return false;
39 case FEATURE_BACKUP_MOODLE2: return true;
46 * Returns all other caps used in module
49 function page_get_extra_capabilities() {
50 return array('moodle/site:accessallgroups');
54 * This function is used by the reset_course_userdata function in moodlelib.
55 * @param $data the data submitted from the reset course.
56 * @return array status array
58 function page_reset_userdata($data) {
63 * List of view style log actions
66 function page_get_view_actions() {
67 return array('view','view all');
71 * List of update style log actions
74 function page_get_post_actions() {
75 return array('update', 'add');
81 * @param object $mform
82 * @return int new resoruce instance id
84 function page_add_instance($data, $mform) {
86 require_once("$CFG->libdir/resourcelib.php");
88 $cmid = $data->coursemodule;
89 $draftitemid = $data->page['itemid'];
91 $data->timemodified = time();
92 $displayoptions = array();
93 if ($data->display == RESOURCELIB_DISPLAY_POPUP) {
94 $displayoptions['popupwidth'] = $data->popupwidth;
95 $displayoptions['popupheight'] = $data->popupheight;
97 $displayoptions['printheading'] = $data->printheading;
98 $displayoptions['printintro'] = $data->printintro;
99 $data->displayoptions = serialize($displayoptions);
101 $data->content = $data->page['text'];
102 $data->contentformat = $data->page['format'];
104 $data->id = $DB->insert_record('page', $data);
106 // we need to use context now, so we need to make sure all needed info is already in db
107 $DB->set_field('course_modules', 'instance', $data->id, array('id'=>$cmid));
108 $context = get_context_instance(CONTEXT_MODULE, $cmid);
111 $data->content = file_save_draft_area_files($draftitemid, $context->id, 'mod_page', 'content', 0, page_get_editor_options($context), $data->content);
112 $DB->update_record('page', $data);
119 * Update page instance.
120 * @param object $data
121 * @param object $mform
124 function page_update_instance($data, $mform) {
126 require_once("$CFG->libdir/resourcelib.php");
128 $cmid = $data->coursemodule;
129 $draftitemid = $data->page['itemid'];
131 $data->timemodified = time();
132 $data->id = $data->instance;
135 $displayoptions = array();
136 if ($data->display == RESOURCELIB_DISPLAY_POPUP) {
137 $displayoptions['popupwidth'] = $data->popupwidth;
138 $displayoptions['popupheight'] = $data->popupheight;
140 $displayoptions['printheading'] = $data->printheading;
141 $displayoptions['printintro'] = $data->printintro;
142 $data->displayoptions = serialize($displayoptions);
144 $data->content = $data->page['text'];
145 $data->contentformat = $data->page['format'];
147 $DB->update_record('page', $data);
149 $context = get_context_instance(CONTEXT_MODULE, $cmid);
151 $data->content = file_save_draft_area_files($draftitemid, $context->id, 'mod_page', 'content', 0, page_get_editor_options($context), $data->content);
152 $DB->update_record('page', $data);
159 * Delete page instance.
163 function page_delete_instance($id) {
166 if (!$page = $DB->get_record('page', array('id'=>$id))) {
170 // note: all context files are deleted automatically
172 $DB->delete_records('page', array('id'=>$page->id));
179 * @param object $course
180 * @param object $user
182 * @param object $page
183 * @return object|null
185 function page_user_outline($course, $user, $mod, $page) {
188 if ($logs = $DB->get_records('log', array('userid'=>$user->id, 'module'=>'page',
189 'action'=>'view', 'info'=>$page->id), 'time ASC')) {
191 $numviews = count($logs);
192 $lastlog = array_pop($logs);
194 $result = new object();
195 $result->info = get_string('numviews', '', $numviews);
196 $result->time = $lastlog->time;
204 * Return use complete
205 * @param object $course
206 * @param object $user
208 * @param object $page
210 function page_user_complete($course, $user, $mod, $page) {
213 if ($logs = $DB->get_records('log', array('userid'=>$user->id, 'module'=>'page',
214 'action'=>'view', 'info'=>$page->id), 'time ASC')) {
215 $numviews = count($logs);
216 $lastlog = array_pop($logs);
218 $strmostrecently = get_string('mostrecently');
219 $strnumviews = get_string('numviews', '', $numviews);
221 echo "$strnumviews - $strmostrecently ".userdate($lastlog->time);
224 print_string('neverseen', 'page');
229 * Returns the users with data in one page
234 function page_get_participants($pageid) {
239 * Given a course_module object, this function returns any
240 * "extra" information that may be needed when printing
241 * this activity in a course listing.
243 * See {@link get_array_of_activities()} in course/lib.php
245 * @param object $coursemodule
246 * @return object info
248 function page_get_coursemodule_info($coursemodule) {
250 require_once("$CFG->libdir/resourcelib.php");
252 if (!$page = $DB->get_record('page', array('id'=>$coursemodule->instance), 'id, name, display, displayoptions')) {
256 $info = new object();
257 $info->name = $page->name;
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->extra = "onclick=\"window.open('$fullurl', '', '$wh'); return false;\"";
275 * Lists all browsable file areas
276 * @param object $course
278 * @param object $context
281 function page_get_file_areas($course, $cm, $context) {
283 $areas['content'] = get_string('content', 'page');
288 * File browsing support for page module ontent area.
289 * @param object $browser
290 * @param object $areas
291 * @param object $course
293 * @param object $context
294 * @param string $filearea
296 * @param string $filepath
297 * @param string $filename
298 * @return object file_info instance or null if not found
300 function page_get_file_info($browser, $areas, $course, $cm, $context, $filearea, $itemid, $filepath, $filename) {
303 if (!has_capability('moodle/course:managefiles', $context)) {
304 // students can not peak here!
308 $fs = get_file_storage();
310 if ($filearea === 'content') {
311 $filepath = is_null($filepath) ? '/' : $filepath;
312 $filename = is_null($filename) ? '.' : $filename;
314 $urlbase = $CFG->wwwroot.'/pluginfile.php';
315 if (!$storedfile = $fs->get_file($context->id, 'mod_page', 'content', 0, $filepath, $filename)) {
316 if ($filepath === '/' and $filename === '.') {
317 $storedfile = new virtual_root_file($context->id, 'mod_page', 'content', 0);
323 require_once("$CFG->dirroot/mod/page/locallib.php");
324 return new page_content_file_info($browser, $context, $storedfile, $urlbase, $areas[$filearea], true, true, true, false);
327 // note: page_intro handled in file_browser automatically
333 * Serves the page files.
334 * @param object $course
336 * @param object $context
337 * @param string $filearea
339 * @param bool $forcedownload
340 * @return bool false if file not found, does not return if found - justsend the file
342 function page_pluginfile($course, $cm, $context, $filearea, $args, $forcedownload) {
344 require_once("$CFG->libdir/resourcelib.php");
346 if ($context->contextlevel != CONTEXT_MODULE) {
350 require_course_login($course, true, $cm);
352 if ($filearea !== 'content') {
353 // intro is handled automatically in pluginfile.php
357 array_shift($args); // ignore revision - designed to prevent caching problems only
359 $fs = get_file_storage();
360 $relativepath = implode('/', $args);
361 $fullpath = "/$context->id/mod_page/$filearea/0/$relativepath";
362 if (!$file = $fs->get_file_by_hash(sha1($fullpath)) or $file->is_directory()) {
363 $page = $DB->get_record('page', array('id'=>$cminfo->instance), 'id, legacyfiles', MUST_EXIST);
364 if ($page->legacyfiles != RESOURCELIB_LEGACYFILES_ACTIVE) {
367 if (!$file = resourcelib_try_file_migration('/'.$relativepath, $cminfo->id, $cminfo->course, 'mod_page', 'content', 0)) {
370 //file migrate - update flag
371 $page->legacyfileslast = time();
372 $DB->update_record('page', $page);
375 // finally send the file
376 send_stored_file($file, 86400, 0, $forcedownload);
381 * This function extends the global navigaiton for the site.
382 * It is important to note that you should not rely on PAGE objects within this
383 * body of code as there is no guarantee that during an AJAX request they are
386 * @param navigation_node $navigation The page node within the global navigation
387 * @param stdClass $course The course object returned from the DB
388 * @param stdClass $module The module object returned from the DB
389 * @param stdClass $cm The course module isntance returned from the DB
391 function page_extend_navigation($navigation, $course, $module, $cm) {
393 * This is currently just a stub so that it can be easily expanded upon.
394 * When expanding just remove this comment and the line below and then add
397 $navigation->nodetype = navigation_node::NODETYPE_LEAF;