Merge branch 'w17_MDL-27253_m20_swfsize' of git://github.com/skodak/moodle into MOODL...
[moodle.git] / mod / page / lib.php
blobce8aef1f278cc133d33de98c245036d8735b782d
1 <?php
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.
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/>.
18 /**
19 * @package mod
20 * @subpackage page
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;
27 /**
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) {
33 switch($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;
44 default: return null;
48 /**
49 * Returns all other caps used in module
50 * @return array
52 function page_get_extra_capabilities() {
53 return array('moodle/site:accessallgroups');
56 /**
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) {
62 return array();
65 /**
66 * List of view style log actions
67 * @return array
69 function page_get_view_actions() {
70 return array('view','view all');
73 /**
74 * List of update style log actions
75 * @return array
77 function page_get_post_actions() {
78 return array('update', 'add');
81 /**
82 * Add page instance.
83 * @param object $data
84 * @param object $mform
85 * @return int new page instance id
87 function page_add_instance($data, $mform) {
88 global $CFG, $DB;
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);
113 if ($draftitemid) {
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);
118 return $data->id;
122 * Update page instance.
123 * @param object $data
124 * @param object $mform
125 * @return bool true
127 function page_update_instance($data, $mform) {
128 global $CFG, $DB;
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;
136 $data->revision++;
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);
153 if ($draftitemid) {
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);
158 return true;
162 * Delete page instance.
163 * @param int $id
164 * @return bool true
166 function page_delete_instance($id) {
167 global $DB;
169 if (!$page = $DB->get_record('page', array('id'=>$id))) {
170 return false;
173 // note: all context files are deleted automatically
175 $DB->delete_records('page', array('id'=>$page->id));
177 return true;
181 * Return use outline
182 * @param object $course
183 * @param object $user
184 * @param object $mod
185 * @param object $page
186 * @return object|null
188 function page_user_outline($course, $user, $mod, $page) {
189 global $DB;
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;
201 return $result;
203 return NULL;
207 * Return use complete
208 * @param object $course
209 * @param object $user
210 * @param object $mod
211 * @param object $page
213 function page_user_complete($course, $user, $mod, $page) {
214 global $CFG, $DB;
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);
226 } else {
227 print_string('neverseen', 'page');
232 * Returns the users with data in one page
234 * @param int $pageid
235 * @return bool false
237 function page_get_participants($pageid) {
238 return false;
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) {
252 global $CFG, $DB;
253 require_once("$CFG->libdir/resourcelib.php");
255 if (!$page = $DB->get_record('page', array('id'=>$coursemodule->instance), 'id, name, display, displayoptions')) {
256 return NULL;
259 $info = new stdClass();
260 $info->name = $page->name;
262 if ($page->display != RESOURCELIB_DISPLAY_POPUP) {
263 return $info;
266 $fullurl = "$CFG->wwwroot/mod/page/view.php?id=$coursemodule->id&amp;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;\"";
273 return $info;
278 * Lists all browsable file areas
279 * @param object $course
280 * @param object $cm
281 * @param object $context
282 * @return array
284 function page_get_file_areas($course, $cm, $context) {
285 $areas = array();
286 $areas['content'] = get_string('content', 'page');
287 return $areas;
291 * File browsing support for page module content area.
292 * @param object $browser
293 * @param object $areas
294 * @param object $course
295 * @param object $cm
296 * @param object $context
297 * @param string $filearea
298 * @param int $itemid
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) {
304 global $CFG;
306 if (!has_capability('moodle/course:managefiles', $context)) {
307 // students can not peak here!
308 return null;
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);
321 } else {
322 // not found
323 return null;
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
332 return null;
336 * Serves the page files.
337 * @param object $course
338 * @param object $cm
339 * @param object $context
340 * @param string $filearea
341 * @param array $args
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) {
346 global $CFG, $DB;
347 require_once("$CFG->libdir/resourcelib.php");
349 if ($context->contextlevel != CONTEXT_MODULE) {
350 return false;
353 require_course_login($course, true, $cm);
354 if (!has_capability('mod/page:view', $context)) {
355 return false;
358 if ($filearea !== 'content') {
359 // intro is handled automatically in pluginfile.php
360 return false;
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) {
371 return false;
373 if (!$file = resourcelib_try_file_migration('/'.$relativepath, $cm->id, $cm->course, 'mod_page', 'content', 0)) {
374 return false;
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
390 * available
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
401 * you content.
403 $navigation->nodetype = navigation_node::NODETYPE_LEAF;