Format now uses defaultquestion(). Bug #4752
[moodle-linuxchix.git] / blocks / pagedemo.php
blobc6de2ab3c01fdaab53fd664a47c68a61c5e7aae0
1 <?php // $Id$
3 die; //must be fixed before enabling again, see SC#971
5 // All of this is standard Moodle fixtures
7 require_once('../config.php');
8 require_once($CFG->dirroot .'/course/lib.php');
9 require_once($CFG->dirroot .'/lib/blocklib.php');
10 require_once($CFG->dirroot .'/mod/resource/lib.php');
11 require_once($CFG->dirroot .'/mod/forum/lib.php');
13 optional_param('blockaction');
14 optional_param('instanceid', 0, PARAM_INT);
15 optional_param('blockid', 0, PARAM_INT);
17 require_login();
19 // Begin snippet -----------------------------------------------------------------
20 // This snippet should normally be defined in another file, but I 've put it all
21 // in here to keep it simple.
23 // First of all define the string identifier for this "type" of page.
24 define('MOODLE_PAGE_TEST', 'testpage');
26 // Also, define identifiers for any non-standard block positions we want to support.
27 define('BLOCK_POS_CENTERUP', 'cu');
28 define('BLOCK_POS_CENTERDOWN', 'cd');
30 // The actual Page derived class
31 class page_test extends page_base {
33 // Mandatory; should return our identifier.
34 function get_type() {
35 return MOODLE_PAGE_TEST;
38 // For this test page, only admins are going to be allowed editing (for simplicity).
39 function user_allowed_editing() {
40 return isadmin();
43 // Also, admins are considered to have "always on" editing (I wanted to avoid duplicating
44 // the code that turns editing on/off here; you can roll your own or copy course/view.php).
45 function user_is_editing() {
46 return isadmin();
49 // Simple method that accepts one parameter and prints the header. Here we just ignore
50 // the parameter entirely.
51 function print_header($title) {
52 print_header("Page testing page", 'SAMPLE CUSTOM PAGE', 'home');
55 // This should point to the script that displays us; it's straightforward in this case.
56 function url_get_path() {
57 global $CFG;
58 return $CFG->wwwroot .'/blocks/pagedemo.php';
61 // We do not need any special request variables such as ID in this case, so we 're not
62 // going to have to override url_get_parameters() here; the default suits us nicely.
64 // Having defined all identifiers we need, here we declare which block positions we are
65 // going to support.
66 function blocks_get_positions() {
67 return array(BLOCK_POS_LEFT, BLOCK_POS_RIGHT, BLOCK_POS_CENTERUP, BLOCK_POS_CENTERDOWN);
70 // And here we declare where new blocks will appear (arbitrary choice).
71 function blocks_default_position() {
72 return BLOCK_POS_CENTERUP;
75 // Since we 're not going to be creating multiple instances of this "page" (as we do with
76 // courses), we don't need to provide default blocks. Otherwise we 'd need to override
77 // the blocks_get_default() method.
79 // And finally, a little block move logic. Given a block's previous position and where
80 // we want to move it to, return its new position. Pretty self-documenting.
81 function blocks_move_position(&$instance, $move) {
82 if($instance->position == BLOCK_POS_LEFT && $move == BLOCK_MOVE_RIGHT) {
83 return BLOCK_POS_CENTERUP;
84 } else if ($instance->position == BLOCK_POS_RIGHT && $move == BLOCK_MOVE_LEFT) {
85 return BLOCK_POS_CENTERUP;
86 } else if (($instance->position == BLOCK_POS_CENTERUP || $instance->position == BLOCK_POS_CENTERDOWN) && $move == BLOCK_MOVE_LEFT) {
87 return BLOCK_POS_LEFT;
88 } else if (($instance->position == BLOCK_POS_CENTERUP || $instance->position == BLOCK_POS_CENTERDOWN) && $move == BLOCK_MOVE_RIGHT) {
89 return BLOCK_POS_RIGHT;
90 } else if ($instance->position == BLOCK_POS_CENTERUP && $move == BLOCK_MOVE_DOWN) {
91 return BLOCK_POS_CENTERDOWN;
92 } else if ($instance->position == BLOCK_POS_CENTERDOWN && $move == BLOCK_MOVE_UP) {
93 return BLOCK_POS_CENTERUP;
95 return $instance->position;
98 // End snippet -------------------------------------------------------------------
100 /// Bounds for block widths on this page
101 define('BLOCK_L_MIN_WIDTH', 160);
102 define('BLOCK_L_MAX_WIDTH', 210);
103 define('BLOCK_R_MIN_WIDTH', 160);
104 define('BLOCK_R_MAX_WIDTH', 210);
105 define('BLOCK_C_MIN_WIDTH', 250);
106 define('BLOCK_C_MAX_WIDTH', 350);
109 // Before creating our page object, we need to map our page identifier to the actual name
110 // of the class which will be handling its operations. Pretty simple, but essential.
111 page_map_class(MOODLE_PAGE_TEST, 'page_test');
113 // Now, create our page object. The identifier "1" is passed arbitrarily because we don't
114 // have multiple "testpages"; if we did, that would be the "testpageid" from the database.
115 $PAGE = page_create_object(MOODLE_PAGE_TEST, 1);
117 $PAGE->print_header(NULL);
118 $editing = $PAGE->user_is_editing();
120 // That's it! From now on, everything is simply copy-pasted from course/view.php with a few
121 // minor tweaks to display the page layout!
123 // Calculate the preferred width for left, right and center (both center positions will use the same)
124 if (empty($preferred_width_left)) {
125 $preferred_width_left = blocks_preferred_width($pageblocks[BLOCK_POS_LEFT]);
127 if (empty($preferred_width_right)) {
128 $preferred_width_right = blocks_preferred_width($pageblocks[BLOCK_POS_RIGHT]);
130 if (empty($preferred_width_centerup)) {
131 $preferred_width_centerup = blocks_preferred_width($pageblocks[BLOCK_POS_CENTERUP]);
133 if (empty($preferred_width_centerdown)) {
134 $preferred_width_centerdown = blocks_preferred_width($pageblocks[BLOCK_POS_CENTERDOWN]);
136 $preferred_width_left = min($preferred_width_left, BLOCK_L_MAX_WIDTH);
137 $preferred_width_left = max($preferred_width_left, BLOCK_L_MIN_WIDTH);
138 $preferred_width_right = min($preferred_width_right, BLOCK_R_MAX_WIDTH);
139 $preferred_width_right = max($preferred_width_right, BLOCK_R_MIN_WIDTH);
140 $preferred_width_center = max($preferred_width_centerup, $preferred_width_centerdown);
141 $preferred_width_center = min($preferred_width_center, BLOCK_C_MAX_WIDTH);
142 $preferred_width_center = max($preferred_width_center, BLOCK_C_MIN_WIDTH);
144 // Display the blocks and allow blocklib to handle any block action requested
145 $pageblocks = blocks_get_by_page($PAGE);
147 if($editing) {
148 if (!empty($blockaction) && confirm_sesskey()) {
149 if (!empty($blockid)) {
150 blocks_execute_action($PAGE, $pageblocks, strtolower($blockaction), intval($blockid));
153 else if (!empty($instanceid)) {
154 $instance = blocks_find_instance($instanceid, $pageblocks);
155 blocks_execute_action($PAGE, $pageblocks, strtolower($blockaction), $instance);
157 // This re-query could be eliminated by judicious programming in blocks_execute_action(),
158 // but I'm not sure if it's worth the complexity increase...
159 $pageblocks = blocks_get_by_page($PAGE);
163 // The actual display logic is here
164 echo '<table style="width: 100%;"><tr>';
166 if(blocks_have_content($pageblocks,BLOCK_POS_LEFT) || $editing) {
167 echo '<td style="vertical-align: top; width: '.$preferred_width_left.'px;">';
168 blocks_print_group($PAGE, $pageblocks,BLOCK_POS_LEFT);
169 echo '</td>';
172 echo '<td style="border: 1px black solid; width: '.$preferred_width_center.'px;"><p style="text-align: center; padding: 10px; background: black; color: white;">Center-up position:</p>';
173 if(blocks_have_content($pageblocks,BLOCK_POS_CENTERUP) || $editing) {
174 blocks_print_group($PAGE, $pageblocks,BLOCK_POS_CENTERUP);
177 echo '<div style="padding: 10px; background: gold; text-align: center;">Content Here';
179 print_object(make_timestamp(2005, 6, 1, 0, 0, 0));
181 echo '</div>';
183 echo '<p style="text-align: center; padding: 10px; background: black; color: white;">Center-down position:</p>';
184 if(blocks_have_content($pageblocks,BLOCK_POS_CENTERDOWN) || $editing) {
185 blocks_print_group($PAGE, $pageblocks,BLOCK_POS_CENTERDOWN);
187 echo '</td>';
189 if(blocks_have_content($pageblocks,BLOCK_POS_RIGHT) || $editing) {
190 echo '<td style="vertical-align: top; width: '.$preferred_width_right.'px;">';
191 blocks_print_group($PAGE, $pageblocks,BLOCK_POS_RIGHT);
192 if ($editing) {
193 blocks_print_adminblock($PAGE, $pageblocks);
195 echo '</td>';
198 // Finished! :-)
200 echo '</tr></table>';
201 print_footer();