4 * This file contains the parent class for moodle pages, page_base,
5 * as well as the page_course subclass.
6 * A page is defined by its page type (ie. course, blog, activity) and its page id
7 * (courseid, blogid, activity id, etc).
9 * @author Jon Papaioannou
11 * @license http://www.gnu.org/copyleft/gpl.html GNU Public License
15 function page_import_types($path) {
18 static $types = array();
20 if(substr($path, -1) != '/') {
24 $path = clean_param($path, PARAM_PATH
);
26 if(isset($types[$path])) {
30 $file = $CFG->dirroot
.'/'.$path.'pagelib.php';
34 if(!isset($DEFINEDPAGES)) {
35 error('Imported '.$file.' but found no page classes');
37 return $types[$path] = $DEFINEDPAGES;
44 * Factory function page_create_object(). Called with a numeric ID for a page, it autodetects
45 * the page type, constructs the correct object and returns it.
48 function page_create_instance($instance) {
49 page_id_and_class($id, $class);
50 return page_create_object($id, $instance);
54 * Factory function page_create_object(). Called with a pagetype identifier and possibly with
55 * its numeric ID. Returns a fully constructed page_base subclass you can work with.
58 function page_create_object($type, $id = NULL) {
62 $data->pagetype
= $type;
65 $classname = page_map_class($type);
67 $object = &new $classname;
68 // TODO: subclassing check here
70 if ($object->get_type() !== $type) {
71 // Somehow somewhere someone made a mistake
72 debugging('Page object\'s type ('. $object->get_type() .') does not match requested type ('. $type .')');
75 $object->init_quick($data);
80 * Function page_map_class() is the way for your code to define its own page subclasses and let Moodle recognize them.
81 * Use it to associate the textual identifier of your Page with the actual class name that has to be instantiated.
84 function page_map_class($type, $classname = NULL) {
87 static $mappings = NULL;
89 if ($mappings === NULL) {
91 PAGE_COURSE_VIEW
=> 'page_course'
95 if (!empty($type) && !empty($classname)) {
96 $mappings[$type] = $classname;
99 if (!isset($mappings[$type])) {
100 debugging('Page class mapping requested for unknown type: '.$type);
103 if (empty($classname) && !class_exists($mappings[$type])) {
104 debugging('Page class mapping for id "'.$type.'" exists but class "'.$mappings[$type].'" is not defined');
107 return $mappings[$type];
111 * Parent class from which all Moodle page classes derive
113 * @author Jon Papaioannou
115 * @todo This parent class is very messy still. Please for the moment ignore it and move on to the derived class page_course to see the comments there.
120 * The string identifier for the type of page being described.
126 * The numeric identifier of the page being described.
132 * Class bool to determine if the instance's full initialization has been completed.
133 * @var boolean $full_init_done
135 var $full_init_done = false;
138 * The class attribute that Moodle has to assign to the BODY tag for this page.
139 * @var string $body_class
141 var $body_class = NULL;
144 * The id attribute that Moodle has to assign to the BODY tag for this page.
145 * @var string $body_id
153 // A whole battery of functions to allow standardized-name constructors in all versions of PHP.
154 // The constructor is actually called construct()
155 function page_base() {
159 function __construct() {
163 function construct() {
164 page_id_and_class($this->body_id
, $this->body_class
);
167 // USER-RELATED THINGS
169 // By default, no user is editing anything and none CAN edit anything. Developers
170 // will have to override these settings to let Moodle know when it should grant
171 // editing rights to the user viewing the page.
172 function user_allowed_editing() {
173 trigger_error('Page class does not implement method <strong>user_allowed_editing()</strong>', E_USER_WARNING
);
176 function user_is_editing() {
177 trigger_error('Page class does not implement method <strong>user_is_editing()</strong>', E_USER_WARNING
);
181 // HTML OUTPUT SECTION
183 // We have absolutely no idea what derived pages are all about
184 function print_header($title, $morenavlinks) {
185 trigger_error('Page class does not implement method <strong>print_header()</strong>', E_USER_WARNING
);
189 // BLOCKS RELATED SECTION
191 // By default, pages don't have any blocks. Override this in your derived class if you need blocks.
192 function blocks_get_positions() {
196 // Thus there is no default block position. If you override the above you should override this one too.
197 // Because this makes sense only if blocks_get_positions() is overridden and because these two should
198 // be overridden as a group or not at all, this one issues a warning. The sneaky part is that this warning
199 // will only be seen if you override blocks_get_positions() but NOT blocks_default_position().
200 function blocks_default_position() {
201 trigger_error('Page class does not implement method <strong>blocks_default_position()</strong>', E_USER_WARNING
);
205 // If you don't override this, newly constructed pages of this kind won't have any blocks.
206 function blocks_get_default() {
210 // If you don't override this, your blocks will not be able to change positions
211 function blocks_move_position(&$instance, $move) {
212 return $instance->position
;
215 // SELF-REPORTING SECTION
217 // Derived classes HAVE to define their "home url"
218 function url_get_path() {
219 trigger_error('Page class does not implement method <strong>url_get_path()</strong>', E_USER_WARNING
);
223 // It's not always required to pass any arguments to the home url, so this doesn't trigger any errors (sensible default)
224 function url_get_parameters() {
228 // This should actually NEVER be overridden unless you have GOOD reason. Works fine as it is.
229 function url_get_full($extraparams = array()) {
230 $path = $this->url_get_path();
235 $params = $this->url_get_parameters();
236 if (!empty($params)) {
237 $params = array_merge($params, $extraparams);
239 $params = $extraparams;
248 foreach($params as $var => $value) {
249 $path .= $first?
'?' : '&';
250 $path .= $var .'='. urlencode($value);
257 // This forces implementers to actually hardwire their page identification constant in the class.
258 // Good thing, if you ask me. That way we can later auto-detect "installed" page types by querying
259 // the classes themselves in the future.
260 function get_type() {
261 trigger_error('Page class does not implement method <strong>get_type()</strong>', E_USER_ERROR
);
265 // Simple stuff, do not override this.
270 // "Sensible default" case here. Take it from the body id.
271 function get_format_name() {
272 return $this->body_id
;
275 // Returns $this->body_class
276 function get_body_class() {
277 return $this->body_class
;
280 // Returns $this->body_id
281 function get_body_id() {
282 return $this->body_id
;
285 // Initialize the data members of the parent class
286 function init_quick($data) {
287 $this->type
= $data->pagetype
;
288 $this->id
= $data->pageid
;
291 function init_full() {
292 $this->full_init_done
= true;
296 // is this page always editable, regardless of anything else?
297 function edit_always() {
298 return (has_capability('moodle/site:manageblocks', get_context_instance(CONTEXT_SYSTEM
)) && defined('ADMIN_STICKYBLOCKS'));
304 * Class that models the behavior of a moodle course
306 * @author Jon Papaioannou
310 class page_course
extends page_base
{
312 // Any data we might need to store specifically about ourself should be declared here.
313 // After init_full() is called for the first time, ALL of these variables should be
314 // initialized correctly and ready for use.
315 var $courserecord = NULL;
317 // Do any validation of the officially recognized bits of the data and forward to parent.
318 // Do NOT load up "expensive" resouces (e.g. SQL data) here!
319 function init_quick($data) {
320 if(empty($data->pageid
) && !defined('ADMIN_STICKYBLOCKS')) {
321 error('Cannot quickly initialize page: empty course id');
323 parent
::init_quick($data);
326 // Here you should load up all heavy-duty data for your page. Basically everything that
327 // does not NEED to be loaded for the class to make basic decisions should NOT be loaded
328 // in init_quick() and instead deferred here. Of course this function had better recognize
329 // $this->full_init_done to prevent wasteful multiple-time data retrieval.
330 function init_full() {
332 if($this->full_init_done
) {
335 if (empty($this->id
)) {
336 $this->id
= 0; // avoid db errors
338 if ($this->id
== $COURSE->id
) {
339 $this->courserecord
= $COURSE;
341 $this->courserecord
= get_record('course', 'id', $this->id
);
344 if(empty($this->courserecord
) && !defined('ADMIN_STICKYBLOCKS')) {
345 error('Cannot fully initialize page: invalid course id '. $this->id
);
347 $this->full_init_done
= true;
350 // USER-RELATED THINGS
352 // Can user edit the course page or "sticky page"?
353 // This is also about editting of blocks BUT mainly activities in course page layout, see
354 // update_course_icon() has very similar checks - it must use the same capabilities
355 function user_allowed_editing() {
358 if (has_capability('moodle/site:manageblocks', get_context_instance(CONTEXT_SYSTEM
)) && defined('ADMIN_STICKYBLOCKS')) {
361 return editcourseallowed($this->id
);
364 // Is the user actually editing this course page or "sticky page" right now?
365 function user_is_editing() {
366 if (has_capability('moodle/site:manageblocks', get_context_instance(CONTEXT_SYSTEM
)) && defined('ADMIN_STICKYBLOCKS')) {
367 //always in edit mode on sticky page
370 return isediting($this->id
);
373 // HTML OUTPUT SECTION
375 // This function prints out the common part of the page's header.
376 // You should NEVER print the header "by hand" in other code.
377 function print_header($title, $morenavlinks=NULL, $meta='', $bodytags='') {
381 $replacements = array(
382 '%fullname%' => $this->courserecord
->fullname
384 foreach($replacements as $search => $replace) {
385 $title = str_replace($search, $replace, $title);
390 if(!empty($morenavlinks)) {
391 $navlinks = array_merge($navlinks, $morenavlinks);
394 $navigation = build_navigation($navlinks);
396 // The "Editing On" button will be appearing only in the "main" course screen
397 // (i.e., no breadcrumbs other than the default one added inside this function)
398 $buttons = switchroles_form($this->courserecord
->id
) . update_course_icon($this->courserecord
->id
);
399 $buttons = empty($morenavlinks) ?
$buttons : ' ';
401 print_header($title, $this->courserecord
->fullname
, $navigation,
402 '', $meta, true, $buttons, user_login_string($this->courserecord
, $USER), false, $bodytags);
404 echo '<div class="accesshide"><a href="#startofcontent">'.get_string('skiptomaincontent').'</a></div>';
407 // SELF-REPORTING SECTION
409 // This is hardwired here so the factory function page_create_object() can be sure there was no mistake.
410 // Also, it doubles as a way to let others inquire about our type.
411 function get_type() {
412 return PAGE_COURSE_VIEW
;
415 // This is like the "category" of a page of this "type". For example, if the type is PAGE_COURSE_VIEW
416 // the format_name is the actual name of the course format. If the type were PAGE_ACTIVITY_VIEW, then
417 // the format_name might be that activity's name etc.
418 function get_format_name() {
420 if (defined('ADMIN_STICKYBLOCKS')) {
421 return PAGE_COURSE_VIEW
;
423 if($this->id
== SITEID
) {
424 return parent
::get_format_name();
426 // This needs to reflect the path hierarchy under Moodle root.
427 return 'course-view-'.$this->courserecord
->format
;
430 // This should return a fully qualified path to the URL which is responsible for displaying us.
431 function url_get_path() {
433 if (defined('ADMIN_STICKYBLOCKS')) {
434 return $CFG->wwwroot
.'/'.$CFG->admin
.'/stickyblocks.php';
436 if($this->id
== SITEID
) {
437 return $CFG->wwwroot
.'/index.php';
440 return $CFG->wwwroot
.'/course/view.php';
444 // This should return an associative array of any GET/POST parameters that are needed by the URL
445 // which displays us to make it work. If none are needed, return an empty array.
446 function url_get_parameters() {
447 if (defined('ADMIN_STICKYBLOCKS')) {
448 return array('pt' => ADMIN_STICKYBLOCKS
);
450 if($this->id
== SITEID
) {
454 return array('id' => $this->id
);
458 // BLOCKS RELATED SECTION
460 // Which are the positions in this page which support blocks? Return an array containing their identifiers.
461 // BE CAREFUL, ORDER DOES MATTER! In textual representations, lists of blocks in a page use the ':' character
462 // to delimit different positions in the page. The part before the first ':' in such a representation will map
463 // directly to the first item of the array you return here, the second to the next one and so on. This way,
464 // you can add more positions in the future without interfering with legacy textual representations.
465 function blocks_get_positions() {
466 return array(BLOCK_POS_LEFT
, BLOCK_POS_RIGHT
);
469 // When a new block is created in this page, which position should it go to?
470 function blocks_default_position() {
471 return BLOCK_POS_RIGHT
;
474 // When we are creating a new page, use the data at your disposal to provide a textual representation of the
475 // blocks that are going to get added to this new page. Delimit block names with commas (,) and use double
476 // colons (:) to delimit between block positions in the page. See blocks_get_positions() for additional info.
477 function blocks_get_default() {
482 if($this->id
== SITEID
) {
484 if (!empty($CFG->defaultblocks_site
)) {
485 $blocknames = $CFG->defaultblocks_site
;
487 /// Failsafe - in case nothing was defined.
489 $blocknames = 'site_main_menu,admin_tree:course_summary,calendar_month';
492 // It's a normal course, so do it according to the course format
494 $pageformat = $this->courserecord
->format
;
495 if (!empty($CFG->{'defaultblocks_'. $pageformat})) {
496 $blocknames = $CFG->{'defaultblocks_'. $pageformat};
499 $format_config = $CFG->dirroot
.'/course/format/'.$pageformat.'/config.php';
500 if (@is_file
($format_config) && is_readable($format_config)) {
501 require($format_config);
503 if (!empty($format['defaultblocks'])) {
504 $blocknames = $format['defaultblocks'];
506 else if (!empty($CFG->defaultblocks
)){
507 $blocknames = $CFG->defaultblocks
;
509 /// Failsafe - in case nothing was defined.
511 $blocknames = 'participants,activity_modules,search_forums,admin,course_list:news_items,calendar_upcoming,recent_activity';
519 // Given an instance of a block in this page and the direction in which we want to move it, where is
520 // it going to go? Return the identifier of the instance's new position. This allows us to tell blocklib
521 // how we want the blocks to move around in this page in an arbitrarily complex way. If the move as given
522 // does not make sense, make sure to return the instance's original position.
524 // Since this is going to get called a LOT, pass the instance by reference purely for speed. Do **NOT**
525 // modify its data in any way, this will actually confuse blocklib!!!
526 function blocks_move_position(&$instance, $move) {
527 if($instance->position
== BLOCK_POS_LEFT
&& $move == BLOCK_MOVE_RIGHT
) {
528 return BLOCK_POS_RIGHT
;
529 } else if ($instance->position
== BLOCK_POS_RIGHT
&& $move == BLOCK_MOVE_LEFT
) {
530 return BLOCK_POS_LEFT
;
532 return $instance->position
;
537 * Class that models the common parts of all activity modules
539 * @author Jon Papaioannou
543 class page_generic_activity
extends page_base
{
544 var $activityname = NULL;
545 var $courserecord = NULL;
546 var $modulerecord = NULL;
547 var $activityrecord = NULL;
549 function init_full() {
550 if($this->full_init_done
) {
553 if(empty($this->activityname
)) {
554 error('Page object derived from page_generic_activity but did not define $this->activityname');
556 $module = get_record('modules', 'name', $this->activityname
);
557 $this->modulerecord
= get_record('course_modules', 'module', $module->id
, 'instance', $this->id
);
558 if(empty($this->modulerecord
)) {
559 error('Cannot fully initialize page: invalid '.$this->activityname
.' instance id '. $this->id
);
561 $this->courserecord
= get_record('course', 'id', $this->modulerecord
->course
);
562 if(empty($this->courserecord
)) {
563 error('Cannot fully initialize page: invalid course id '. $this->modulerecord
->course
);
565 $this->activityrecord
= get_record($this->activityname
, 'id', $this->id
);
566 if(empty($this->courserecord
)) {
567 error('Cannot fully initialize page: invalid '.$this->activityname
.' id '. $this->id
);
569 $this->full_init_done
= true;
572 function user_allowed_editing() {
574 // Yu: I think this is wrong, should be checking manageactivities instead
575 //return has_capability('moodle/site:manageblocks', get_context_instance(CONTEXT_COURSE, $this->modulerecord->course));
576 return has_capability('moodle/course:manageactivities', get_context_instance(CONTEXT_MODULE
, $this->modulerecord
->id
));
579 function user_is_editing() {
581 return isediting($this->modulerecord
->course
);
584 function url_get_path() {
586 return $CFG->wwwroot
.'/mod/'.$this->activityname
.'/view.php';
589 function url_get_parameters() {
591 return array('id' => $this->modulerecord
->id
);
594 function blocks_get_positions() {
595 return array(BLOCK_POS_LEFT
);
598 function blocks_default_position() {
599 return BLOCK_POS_LEFT
;
602 function print_header($title, $morenavlinks = NULL, $bodytags = '', $meta = '') {
606 $replacements = array(
607 '%fullname%' => format_string($this->activityrecord
->name
)
609 foreach ($replacements as $search => $replace) {
610 $title = str_replace($search, $replace, $title);
614 $navlinks[] = array('name' => get_string('modulenameplural', $this->activityname
), 'link' => $CFG->wwwroot
."/mod/{$this->activityname}/index.php?id={$this->courserecord->id}", 'type' => 'activity');
615 $navlinks[] = array('name' => format_string($this->activityrecord
->name
), 'link' => $CFG->wwwroot
."/mod/{$this->activityname}/view.php?id={$this->modulerecord->id}", 'type' => 'activityinstance');
617 if (!empty($morenavlinks)) {
618 $navlinks = array_merge($navlinks, $morenavlinks);
621 if (empty($morenavlinks) && $this->user_allowed_editing()) {
622 $buttons = '<table><tr><td>'.update_module_button($this->modulerecord
->id
, $this->courserecord
->id
, get_string('modulename', $this->activityname
)).'</td>';
623 if (!empty($CFG->showblocksonmodpages
)) {
624 $buttons .= '<td><form target="'.$CFG->framename
.'" method="get" action="view.php">'.
625 '<input type="hidden" name="id" value="'.$this->modulerecord
->id
.'" />'.
626 '<input type="hidden" name="edit" value="'.($this->user_is_editing()?
'off':'on').'" />'.
627 '<input type="submit" value="'.get_string($this->user_is_editing()?
'blockseditoff':'blocksediton').'" /></form></td>';
629 $buttons .= '</tr></table>';
634 $navigation = build_navigation($navlinks);
636 print_header($title, $this->courserecord
->fullname
, $navigation, '', $meta, true, $buttons, navmenu($this->courserecord
, $this->modulerecord
), false, $bodytags);