MDL-18011 - removing reference to loancalc block from core
[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/**
19 * @package mod-page
20 * @copyright 2009 Petr Skoda (http://skodak.org)
21 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
22 */
23
24/**
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
28 */
29function page_supports($feature) {
30 switch($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;
7db1f6b9 39 case FEATURE_BACKUP_MOODLE2: return true;
1ad9ff57 40
41 default: return null;
42 }
43}
44
45/**
46 * Returns all other caps used in module
47 * @return array
48 */
49function page_get_extra_capabilities() {
50 return array('moodle/site:accessallgroups');
51}
52
53/**
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
57 */
58function page_reset_userdata($data) {
59 return array();
60}
61
62/**
63 * List of view style log actions
64 * @return array
65 */
66function page_get_view_actions() {
67 return array('view','view all');
68}
69
70/**
71 * List of update style log actions
72 * @return array
73 */
74function page_get_post_actions() {
75 return array('update', 'add');
76}
77
78/**
79 * Add page instance.
80 * @param object $data
81 * @param object $mform
82 * @return int new resoruce instance id
83 */
84function page_add_instance($data, $mform) {
85 global $CFG, $DB;
86 require_once("$CFG->libdir/resourcelib.php");
87
88 $cmid = $data->coursemodule;
89 $draftitemid = $data->page['itemid'];
90
91 $data->timemodified = time();
92 $displayoptions = array();
93 if ($data->display == RESOURCELIB_DISPLAY_POPUP) {
94 $displayoptions['popupwidth'] = $data->popupwidth;
95 $displayoptions['popupheight'] = $data->popupheight;
96 }
97 $displayoptions['printheading'] = $data->printheading;
98 $displayoptions['printintro'] = $data->printintro;
99 $data->displayoptions = serialize($displayoptions);
100
101 $data->content = $data->page['text'];
102 $data->contentformat = $data->page['format'];
103
104 $data->id = $DB->insert_record('page', $data);
105
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);
109
110 if ($draftitemid) {
111 $data->content = file_save_draft_area_files($draftitemid, $context->id, 'page_content', 0, page_get_editor_options($context), $data->content);
112 $DB->update_record('page', $data);
113 }
114
115 return $data->id;
116}
117
118/**
119 * Update page instance.
120 * @param object $data
121 * @param object $mform
122 * @return bool true
123 */
124function page_update_instance($data, $mform) {
125 global $CFG, $DB;
126 require_once("$CFG->libdir/resourcelib.php");
127
128 $cmid = $data->coursemodule;
129 $draftitemid = $data->page['itemid'];
130
131 $data->timemodified = time();
132 $data->id = $data->instance;
133 $data->revision++;
134
135 $displayoptions = array();
136 if ($data->display == RESOURCELIB_DISPLAY_POPUP) {
137 $displayoptions['popupwidth'] = $data->popupwidth;
138 $displayoptions['popupheight'] = $data->popupheight;
139 }
140 $displayoptions['printheading'] = $data->printheading;
141 $displayoptions['printintro'] = $data->printintro;
142 $data->displayoptions = serialize($displayoptions);
143
144 $data->content = $data->page['text'];
145 $data->contentformat = $data->page['format'];
146
147 $DB->update_record('page', $data);
148
149 $context = get_context_instance(CONTEXT_MODULE, $cmid);
150 if ($draftitemid) {
151 $data->content = file_save_draft_area_files($draftitemid, $context->id, 'page_content', 0, page_get_editor_options($context), $data->content);
152 $DB->update_record('page', $data);
153 }
154
155 return true;
156}
157
158/**
159 * Delete page instance.
160 * @param int $id
161 * @return bool true
162 */
163function page_delete_instance($id) {
164 global $DB;
165
166 if (!$page = $DB->get_record('page', array('id'=>$id))) {
167 return false;
168 }
169
170 // note: all context files are deleted automatically
171
172 $DB->delete_records('page', array('id'=>$page->id));
173
174 return true;
175}
176
177/**
178 * Return use outline
179 * @param object $course
180 * @param object $user
181 * @param object $mod
182 * @param object $page
183 * @return object|null
184 */
185function page_user_outline($course, $user, $mod, $page) {
186 global $DB;
187
188 if ($logs = $DB->get_records('log', array('userid'=>$user->id, 'module'=>'page',
189 'action'=>'view', 'info'=>$page->id), 'time ASC')) {
190
191 $numviews = count($logs);
192 $lastlog = array_pop($logs);
193
194 $result = new object();
195 $result->info = get_string('numviews', '', $numviews);
196 $result->time = $lastlog->time;
197
198 return $result;
199 }
200 return NULL;
201}
202
203/**
204 * Return use complete
205 * @param object $course
206 * @param object $user
207 * @param object $mod
208 * @param object $page
209 */
210function page_user_complete($course, $user, $mod, $page) {
211 global $CFG, $DB;
212
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);
217
218 $strmostrecently = get_string('mostrecently');
219 $strnumviews = get_string('numviews', '', $numviews);
220
221 echo "$strnumviews - $strmostrecently ".userdate($lastlog->time);
222
223 } else {
224 print_string('neverseen', 'page');
225 }
226}
227
228/**
229 * Returns the users with data in one page
230 *
231 * @param int $pageid
232 * @return bool false
233 */
234function page_get_participants($pageid) {
235 return false;
236}
237
238/**
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.
242 *
243 * See {@link get_array_of_activities()} in course/lib.php
244 *
245 * @param object $coursemodule
246 * @return object info
247 */
248function page_get_coursemodule_info($coursemodule) {
249 global $CFG, $DB;
250 require_once("$CFG->libdir/resourcelib.php");
251
252 if (!$page = $DB->get_record('page', array('id'=>$coursemodule->instance), 'id, name, display, displayoptions')) {
253 return NULL;
254 }
255
13fceaa4
SH
256 $info = new object();
257 $info->name = $page->name;
258
1ad9ff57 259 if ($page->display != RESOURCELIB_DISPLAY_POPUP) {
9a9012dc 260 return $info;
1ad9ff57 261 }
262
1ad9ff57 263 $fullurl = "$CFG->wwwroot/mod/page/view.php?id=$coursemodule->id&amp;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";
9a9012dc 268 $info->extra = "onclick=\"window.open('$fullurl', '', '$wh'); return false;\"";
1ad9ff57 269
270 return $info;
271}
272
273
274/**
275 * Lists all browsable file areas
276 * @param object $course
277 * @param object $cm
278 * @param object $context
279 * @return array
280 */
281function page_get_file_areas($course, $cm, $context) {
282 $areas = array();
283 if (has_capability('moodle/course:managefiles', $context)) {
284 $areas['page_content'] = get_string('pagecontent', 'page');
285 }
286 return $areas;
287}
288
289/**
290 * File browsing support for page module ontent area.
291 * @param object $browser
292 * @param object $areas
293 * @param object $course
294 * @param object $cm
295 * @param object $context
296 * @param string $filearea
297 * @param int $itemid
298 * @param string $filepath
299 * @param string $filename
300 * @return object file_info instance or null if not found
301 */
302function page_get_file_info($browser, $areas, $course, $cm, $context, $filearea, $itemid, $filepath, $filename) {
303 global $CFG;
304
305 $canwrite = has_capability('moodle/course:managefiles', $context);
306
307 $fs = get_file_storage();
308
309 if ($filearea === 'page_content') {
310 $filepath = is_null($filepath) ? '/' : $filepath;
311 $filename = is_null($filename) ? '.' : $filename;
312
313 $urlbase = $CFG->wwwroot.'/pluginfile.php';
314 if (!$storedfile = $fs->get_file($context->id, $filearea, 0, $filepath, $filename)) {
315 if ($filepath === '/' and $filename === '.') {
316 $storedfile = new virtual_root_file($context->id, $filearea, 0);
317 } else {
318 // not found
319 return null;
320 }
321 }
322 require_once("$CFG->dirroot/mod/page/locallib.php");
323 return new page_content_file_info($browser, $context, $storedfile, $urlbase, $areas[$filearea], true, true, $canwrite, false);
324 }
325
326 // note: page_intro handled in file_browser automatically
327
328 return null;
329}
330
331/**
332 * Serves the page files.
333 * @param object $course
334 * @param object $cminfo
335 * @param object $context
336 * @param string $filearea
337 * @param array $args
338 * @param bool $forcedownload
339 * @return bool false if file not found, does not return if found - justsend the file
340 */
341function page_pluginfile($course, $cminfo, $context, $filearea, $args, $forcedownload) {
342 global $CFG, $DB;
343 require_once("$CFG->libdir/resourcelib.php");
344
345 if (!$cminfo->uservisible) {
346 return false;
347 }
348
349 if ($filearea !== 'page_content') {
350 // intro is handled automatically in pluginfile.php
351 return false;
352 }
353
354 if (!$cm = get_coursemodule_from_instance('page', $cminfo->instance, $course->id)) {
355 return false;
356 }
357
358 require_course_login($course, true, $cm);
359
360 array_shift($args); // ignore revision - designed to prevent caching problems only
361
362 $fs = get_file_storage();
363 $relativepath = '/'.implode('/', $args);
364 $fullpath = $context->id.$filearea.'0'.$relativepath;
365 if (!$file = $fs->get_file_by_hash(sha1($fullpath)) or $file->is_directory()) {
366 $page = $DB->get_record('page', array('id'=>$cminfo->instance), 'id, legacyfiles', MUST_EXIST);
367 if ($page->legacyfiles != RESOURCELIB_LEGACYFILES_ACTIVE) {
368 return false;
369 }
9b56a34f 370 if (!$file = resourcelib_try_file_migration($relativepath, $cminfo->id, $cminfo->course, 'page_content', 0)) {
1ad9ff57 371 return false;
372 }
373 //file migrate - update flag
374 $page->legacyfileslast = time();
375 $DB->update_record('page', $page);
376 }
377
378 // finally send the file
379 send_stored_file($file, 86400, 0, $forcedownload);
380}
92059c7e
SH
381
382
383/**
384 * This function extends the global navigaiton for the site.
385 * It is important to note that you should not rely on PAGE objects within this
386 * body of code as there is no guarantee that during an AJAX request they are
387 * available
388 *
389 * @param navigation_node $navigation The page node within the global navigation
390 * @param stdClass $course The course object returned from the DB
391 * @param stdClass $module The module object returned from the DB
392 * @param stdClass $cm The course module isntance returned from the DB
393 */
394function page_extend_navigation($navigation, $course, $module, $cm) {
395 /**
396 * This is currently just a stub so that it can be easily expanded upon.
397 * When expanding just remove this comment and the line below and then add
398 * you content.
399 */
400 $navigation->nodetype = navigation_node::NODETYPE_LEAF;
401}