MDL-14627 new mod/page:view capability
[moodle.git] / mod / page / lib.php
CommitLineData
1ad9ff57 1<?php
2
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.
14//
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/>.
17
18/**
2c49daf7
PS
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
1ad9ff57 23 */
24
2c49daf7
PS
25defined('MOODLE_INTERNAL') || die;
26
1ad9ff57 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
31 */
32function 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;
7db1f6b9 42 case FEATURE_BACKUP_MOODLE2: return true;
1ad9ff57 43
44 default: return null;
45 }
46}
47
48/**
49 * Returns all other caps used in module
50 * @return array
51 */
52function page_get_extra_capabilities() {
53 return array('moodle/site:accessallgroups');
54}
55
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
60 */
61function page_reset_userdata($data) {
62 return array();
63}
64
65/**
66 * List of view style log actions
67 * @return array
68 */
69function page_get_view_actions() {
70 return array('view','view all');
71}
72
73/**
74 * List of update style log actions
75 * @return array
76 */
77function page_get_post_actions() {
78 return array('update', 'add');
79}
80
81/**
82 * Add page instance.
83 * @param object $data
84 * @param object $mform
a4ee8005 85 * @return int new page instance id
1ad9ff57 86 */
87function page_add_instance($data, $mform) {
88 global $CFG, $DB;
89 require_once("$CFG->libdir/resourcelib.php");
90
91 $cmid = $data->coursemodule;
92 $draftitemid = $data->page['itemid'];
93
94 $data->timemodified = time();
95 $displayoptions = array();
96 if ($data->display == RESOURCELIB_DISPLAY_POPUP) {
97 $displayoptions['popupwidth'] = $data->popupwidth;
98 $displayoptions['popupheight'] = $data->popupheight;
99 }
100 $displayoptions['printheading'] = $data->printheading;
101 $displayoptions['printintro'] = $data->printintro;
102 $data->displayoptions = serialize($displayoptions);
103
104 $data->content = $data->page['text'];
105 $data->contentformat = $data->page['format'];
106
107 $data->id = $DB->insert_record('page', $data);
108
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);
112
113 if ($draftitemid) {
64f93798 114 $data->content = file_save_draft_area_files($draftitemid, $context->id, 'mod_page', 'content', 0, page_get_editor_options($context), $data->content);
1ad9ff57 115 $DB->update_record('page', $data);
116 }
117
118 return $data->id;
119}
120
121/**
122 * Update page instance.
123 * @param object $data
124 * @param object $mform
125 * @return bool true
126 */
127function page_update_instance($data, $mform) {
128 global $CFG, $DB;
129 require_once("$CFG->libdir/resourcelib.php");
130
131 $cmid = $data->coursemodule;
132 $draftitemid = $data->page['itemid'];
133
134 $data->timemodified = time();
135 $data->id = $data->instance;
136 $data->revision++;
137
138 $displayoptions = array();
139 if ($data->display == RESOURCELIB_DISPLAY_POPUP) {
140 $displayoptions['popupwidth'] = $data->popupwidth;
141 $displayoptions['popupheight'] = $data->popupheight;
142 }
143 $displayoptions['printheading'] = $data->printheading;
144 $displayoptions['printintro'] = $data->printintro;
145 $data->displayoptions = serialize($displayoptions);
146
147 $data->content = $data->page['text'];
148 $data->contentformat = $data->page['format'];
149
150 $DB->update_record('page', $data);
151
152 $context = get_context_instance(CONTEXT_MODULE, $cmid);
153 if ($draftitemid) {
64f93798 154 $data->content = file_save_draft_area_files($draftitemid, $context->id, 'mod_page', 'content', 0, page_get_editor_options($context), $data->content);
1ad9ff57 155 $DB->update_record('page', $data);
156 }
157
158 return true;
159}
160
161/**
162 * Delete page instance.
163 * @param int $id
164 * @return bool true
165 */
166function page_delete_instance($id) {
167 global $DB;
168
169 if (!$page = $DB->get_record('page', array('id'=>$id))) {
170 return false;
171 }
172
173 // note: all context files are deleted automatically
174
175 $DB->delete_records('page', array('id'=>$page->id));
176
177 return true;
178}
179
180/**
181 * Return use outline
182 * @param object $course
183 * @param object $user
184 * @param object $mod
185 * @param object $page
186 * @return object|null
187 */
188function page_user_outline($course, $user, $mod, $page) {
189 global $DB;
190
191 if ($logs = $DB->get_records('log', array('userid'=>$user->id, 'module'=>'page',
192 'action'=>'view', 'info'=>$page->id), 'time ASC')) {
193
194 $numviews = count($logs);
195 $lastlog = array_pop($logs);
196
39790bd8 197 $result = new stdClass();
1ad9ff57 198 $result->info = get_string('numviews', '', $numviews);
199 $result->time = $lastlog->time;
200
201 return $result;
202 }
203 return NULL;
204}
205
206/**
207 * Return use complete
208 * @param object $course
209 * @param object $user
210 * @param object $mod
211 * @param object $page
212 */
213function page_user_complete($course, $user, $mod, $page) {
214 global $CFG, $DB;
215
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);
220
221 $strmostrecently = get_string('mostrecently');
222 $strnumviews = get_string('numviews', '', $numviews);
223
224 echo "$strnumviews - $strmostrecently ".userdate($lastlog->time);
225
226 } else {
c0d1bc10 227 print_string('neverseen', 'page');
1ad9ff57 228 }
229}
230
231/**
232 * Returns the users with data in one page
233 *
234 * @param int $pageid
235 * @return bool false
236 */
237function page_get_participants($pageid) {
238 return false;
239}
240
241/**
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.
245 *
246 * See {@link get_array_of_activities()} in course/lib.php
247 *
248 * @param object $coursemodule
249 * @return object info
250 */
251function page_get_coursemodule_info($coursemodule) {
252 global $CFG, $DB;
253 require_once("$CFG->libdir/resourcelib.php");
254
255 if (!$page = $DB->get_record('page', array('id'=>$coursemodule->instance), 'id, name, display, displayoptions')) {
256 return NULL;
257 }
258
39790bd8 259 $info = new stdClass();
13fceaa4
SH
260 $info->name = $page->name;
261
1ad9ff57 262 if ($page->display != RESOURCELIB_DISPLAY_POPUP) {
9a9012dc 263 return $info;
1ad9ff57 264 }
265
1ad9ff57 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";
9a9012dc 271 $info->extra = "onclick=\"window.open('$fullurl', '', '$wh'); return false;\"";
1ad9ff57 272
273 return $info;
274}
275
276
277/**
278 * Lists all browsable file areas
279 * @param object $course
280 * @param object $cm
281 * @param object $context
282 * @return array
283 */
284function page_get_file_areas($course, $cm, $context) {
285 $areas = array();
9db7f99b 286 $areas['content'] = get_string('content', 'page');
1ad9ff57 287 return $areas;
288}
289
290/**
2c49daf7 291 * File browsing support for page module content area.
1ad9ff57 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
302 */
303function page_get_file_info($browser, $areas, $course, $cm, $context, $filearea, $itemid, $filepath, $filename) {
304 global $CFG;
305
64f93798
PS
306 if (!has_capability('moodle/course:managefiles', $context)) {
307 // students can not peak here!
308 return null;
309 }
1ad9ff57 310
311 $fs = get_file_storage();
312
64f93798 313 if ($filearea === 'content') {
1ad9ff57 314 $filepath = is_null($filepath) ? '/' : $filepath;
315 $filename = is_null($filename) ? '.' : $filename;
316
317 $urlbase = $CFG->wwwroot.'/pluginfile.php';
64f93798 318 if (!$storedfile = $fs->get_file($context->id, 'mod_page', 'content', 0, $filepath, $filename)) {
1ad9ff57 319 if ($filepath === '/' and $filename === '.') {
64f93798 320 $storedfile = new virtual_root_file($context->id, 'mod_page', 'content', 0);
1ad9ff57 321 } else {
322 // not found
323 return null;
324 }
325 }
326 require_once("$CFG->dirroot/mod/page/locallib.php");
64f93798 327 return new page_content_file_info($browser, $context, $storedfile, $urlbase, $areas[$filearea], true, true, true, false);
1ad9ff57 328 }
329
330 // note: page_intro handled in file_browser automatically
331
332 return null;
333}
334
335/**
336 * Serves the page files.
337 * @param object $course
64f93798 338 * @param object $cm
1ad9ff57 339 * @param object $context
340 * @param string $filearea
341 * @param array $args
342 * @param bool $forcedownload
2c49daf7 343 * @return bool false if file not found, does not return if found - just send the file
1ad9ff57 344 */
64f93798 345function page_pluginfile($course, $cm, $context, $filearea, $args, $forcedownload) {
1ad9ff57 346 global $CFG, $DB;
347 require_once("$CFG->libdir/resourcelib.php");
348
64f93798 349 if ($context->contextlevel != CONTEXT_MODULE) {
1ad9ff57 350 return false;
351 }
352
64f93798 353 require_course_login($course, true, $cm);
0c0484de
PS
354 if (!has_capability('mod/page:view', $context)) {
355 return false;
356 }
1ad9ff57 357
64f93798
PS
358 if ($filearea !== 'content') {
359 // intro is handled automatically in pluginfile.php
1ad9ff57 360 return false;
361 }
362
1ad9ff57 363 array_shift($args); // ignore revision - designed to prevent caching problems only
364
365 $fs = get_file_storage();
64f93798
PS
366 $relativepath = implode('/', $args);
367 $fullpath = "/$context->id/mod_page/$filearea/0/$relativepath";
1ad9ff57 368 if (!$file = $fs->get_file_by_hash(sha1($fullpath)) or $file->is_directory()) {
8cb86e03 369 $page = $DB->get_record('page', array('id'=>$cm->instance), 'id, legacyfiles', MUST_EXIST);
1ad9ff57 370 if ($page->legacyfiles != RESOURCELIB_LEGACYFILES_ACTIVE) {
371 return false;
372 }
8cb86e03 373 if (!$file = resourcelib_try_file_migration('/'.$relativepath, $cm->id, $cm->course, 'mod_page', 'content', 0)) {
1ad9ff57 374 return false;
375 }
376 //file migrate - update flag
377 $page->legacyfileslast = time();
378 $DB->update_record('page', $page);
379 }
380
381 // finally send the file
382 send_stored_file($file, 86400, 0, $forcedownload);
383}
92059c7e
SH
384
385
386/**
2c49daf7 387 * This function extends the global navigation for the site.
92059c7e
SH
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
391 *
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
2c49daf7 395 * @param stdClass $cm The course module instance returned from the DB
92059c7e
SH
396 */
397function page_extend_navigation($navigation, $course, $module, $cm) {
398 /**
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.
402 */
403 $navigation->nodetype = navigation_node::NODETYPE_LEAF;
404}