dcb1bd3c |
1 | <?php //$Id$ |
0f3fe4b6 |
2 | |
e8c8cee9 |
3 | /////////////////////////////////////////////////////////////////////////// |
4 | // // |
5 | // NOTICE OF COPYRIGHT // |
6 | // // |
7 | // Moodle - Modular Object-Oriented Dynamic Learning Environment // |
8 | // http://moodle.org // |
9 | // // |
10 | // Copyright (C) 1999 onwards Martin Dougiamas http://dougiamas.com // |
11 | // // |
12 | // This program is free software; you can redistribute it and/or modify // |
13 | // it under the terms of the GNU General Public License as published by // |
14 | // the Free Software Foundation; either version 2 of the License, or // |
15 | // (at your option) any later version. // |
16 | // // |
17 | // This program is distributed in the hope that it will be useful, // |
18 | // but WITHOUT ANY WARRANTY; without even the implied warranty of // |
19 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // |
20 | // GNU General Public License for more details: // |
21 | // // |
22 | // http://www.gnu.org/copyleft/gpl.html // |
23 | // // |
24 | /////////////////////////////////////////////////////////////////////////// |
25 | |
26 | /** |
27 | * This library includes all the necessary stuff to use blocks on pages in Moodle. |
28 | * |
29 | * @license http://www.gnu.org/copyleft/gpl.html GNU Public License |
30 | * @package pages |
31 | */ |
0f3fe4b6 |
32 | |
0f3fe4b6 |
33 | define('BLOCK_MOVE_LEFT', 0x01); |
34 | define('BLOCK_MOVE_RIGHT', 0x02); |
35 | define('BLOCK_MOVE_UP', 0x04); |
36 | define('BLOCK_MOVE_DOWN', 0x08); |
9b4b78fd |
37 | define('BLOCK_CONFIGURE', 0x10); |
0f3fe4b6 |
38 | |
9b4b78fd |
39 | define('BLOCK_POS_LEFT', 'l'); |
40 | define('BLOCK_POS_RIGHT', 'r'); |
0e9af917 |
41 | |
ee6055eb |
42 | define('BLOCKS_PINNED_TRUE',0); |
43 | define('BLOCKS_PINNED_FALSE',1); |
44 | define('BLOCKS_PINNED_BOTH',2); |
45 | |
f032aa7a |
46 | require_once($CFG->libdir.'/pagelib.php'); |
61abc65b |
47 | |
86b5ea0f |
48 | /** |
49 | * This class keeps track of the block that should appear on a moodle_page. |
50 | * The page to work with as passed to the constructor. |
51 | * The only fields of moodle_page that is uses are ->context, ->pagetype and |
52 | * ->subpage, so instead of passing a full moodle_page object, you may also |
53 | * pass a stdClass object with those three fields. These field values are read |
54 | * only at the point that the load_blocks() method is called. It is the caller's |
55 | * responsibility to ensure that those fields do not subsequently change. |
56 | */ |
57 | class block_manager { |
58 | /**#@+ Tracks the where we are in the generation of the page. */ |
59 | const STATE_BLOCKS_NOT_LOADED = 0; |
60 | const STATE_BLOCKS_LOADED = 1; |
61 | /**#@-*/ |
62 | |
63 | /// Field declarations ========================================================= |
64 | |
65 | protected $loaded = self::STATE_BLOCKS_NOT_LOADED; |
66 | |
67 | protected $page; |
68 | |
69 | protected $regions = array(); |
70 | |
71 | protected $defaultregion; |
72 | |
73 | /// Constructor ================================================================ |
74 | |
75 | /** |
76 | * Constructor. |
77 | * @param object $page the moodle_page object object we are managing the blocks for, |
78 | * or a reasonable faxilimily. (See the comment at the top of this classe |
79 | * and http://en.wikipedia.org/wiki/Duck_typing) |
80 | */ |
81 | public function __construct($page) { |
82 | $this->page = $page; |
83 | } |
84 | |
85 | /// Getter methods ============================================================= |
86 | |
87 | /** |
88 | * @return array the internal names of the regions on this page where block may appear. |
89 | */ |
90 | public function get_regions() { |
91 | return array_keys($this->regions); |
92 | } |
93 | |
94 | /** |
95 | * @return string the internal names of the region where new blocks are added |
96 | * by default, and where any blocks from an unrecognised region are shown. |
97 | * (Imagine that blocks were added with one theme selected, then you switched |
98 | * to a theme with different block positions.) |
99 | */ |
100 | public function get_default_region() { |
101 | return $this->defaultregion; |
102 | } |
103 | |
104 | /// Setter methods ============================================================= |
105 | |
106 | /** |
107 | * @param string $region add a named region where blocks may appear on the |
108 | * current page. This is an internal name, like 'side-pre', not a string to |
109 | * display in the UI. |
110 | */ |
111 | public function add_region($region) { |
112 | $this->check_not_yet_loaded(); |
113 | $this->regions[$region] = 1; |
114 | } |
115 | |
116 | /** |
117 | * @param array $regions this utility method calls add_region for each array element. |
118 | */ |
119 | public function add_regions($regions) { |
120 | foreach ($regions as $region) { |
121 | $this->add_region($region); |
122 | } |
123 | } |
124 | |
125 | /** |
126 | * @param string $defaultregion the internal names of the region where new |
127 | * blocks should be added by default, and where any blocks from an |
128 | * unrecognised region are shown. |
129 | */ |
130 | public function set_default_region($defaultregion) { |
131 | $this->check_not_yet_loaded(); |
132 | if (!array_key_exists($defaultregion, $this->regions)) { |
133 | throw new coding_exception('Trying to set an unknown block region as the default.'); |
134 | } |
135 | $this->defaultregion = $defaultregion; |
136 | } |
137 | |
138 | /// Inner workings ============================================================= |
139 | |
140 | protected function check_not_yet_loaded() { |
141 | if ($this->loaded) { |
142 | throw new coding_exception('block_manager has already loaded the blocks, to it is too late to change things that might affect which blocks are visible.'); |
143 | } |
144 | } |
145 | |
146 | protected function mark_loaded() { |
147 | $this->loaded = self::STATE_BLOCKS_LOADED; |
148 | } |
149 | } |
150 | |
0f3fe4b6 |
151 | //This function retrieves a method-defined property of a class WITHOUT instantiating an object |
11306331 |
152 | function block_method_result($blockname, $method, $param = NULL) { |
0f3fe4b6 |
153 | if(!block_load_class($blockname)) { |
154 | return NULL; |
155 | } |
11306331 |
156 | return call_user_func(array('block_'.$blockname, $method), $param); |
0f3fe4b6 |
157 | } |
158 | |
159 | //This function creates a new object of the specified block class |
9b4b78fd |
160 | function block_instance($blockname, $instance = NULL) { |
0f3fe4b6 |
161 | if(!block_load_class($blockname)) { |
162 | return false; |
163 | } |
e89d741a |
164 | $classname = 'block_'.$blockname; |
f032aa7a |
165 | $retval = new $classname; |
9b4b78fd |
166 | if($instance !== NULL) { |
1345403a |
167 | $retval->_load_instance($instance); |
9b4b78fd |
168 | } |
169 | return $retval; |
0f3fe4b6 |
170 | } |
171 | |
172 | //This function loads the necessary class files for a block |
173 | //Whenever you want to load a block, use this first |
174 | function block_load_class($blockname) { |
175 | global $CFG; |
176 | |
a9033ad5 |
177 | if(empty($blockname)) { |
c7a9e293 |
178 | return false; |
179 | } |
180 | |
e89d741a |
181 | $classname = 'block_'.$blockname; |
a9033ad5 |
182 | |
183 | if(class_exists($classname)) { |
184 | return true; |
185 | } |
186 | |
187 | require_once($CFG->dirroot.'/blocks/moodleblock.class.php'); |
e9a20759 |
188 | @include_once($CFG->dirroot.'/blocks/'.$blockname.'/block_'.$blockname.'.php'); // do not throw errors if block code not present |
0f3fe4b6 |
189 | |
0f3fe4b6 |
190 | return class_exists($classname); |
191 | } |
192 | |
c8e0b579 |
193 | // This function returns an array with the IDs of any blocks that you can add to your page. |
194 | // Parameters are passed by reference for speed; they are not modified at all. |
195 | function blocks_get_missing(&$page, &$pageblocks) { |
f032aa7a |
196 | |
197 | $missingblocks = array(); |
198 | $allblocks = blocks_get_record(); |
d529807a |
199 | $pageformat = $page->pagetype; |
f032aa7a |
200 | |
201 | if(!empty($allblocks)) { |
202 | foreach($allblocks as $block) { |
203 | if($block->visible && (!blocks_find_block($block->id, $pageblocks) || $block->multiple)) { |
204 | // And if it's applicable for display in this format... |
5bbbe0be |
205 | if(blocks_name_allowed_in_format($block->name, $pageformat)) { |
8a47e075 |
206 | // ...add it to the missing blocks |
f032aa7a |
207 | $missingblocks[] = $block->id; |
208 | } |
209 | } |
210 | } |
211 | } |
212 | return $missingblocks; |
213 | } |
214 | |
215 | function blocks_remove_inappropriate($page) { |
216 | $pageblocks = blocks_get_by_page($page); |
217 | |
218 | if(empty($pageblocks)) { |
219 | return; |
220 | } |
221 | |
d529807a |
222 | if(($pageformat = $page->pagetype) == NULL) { |
f032aa7a |
223 | return; |
224 | } |
225 | |
226 | foreach($pageblocks as $position) { |
227 | foreach($position as $instance) { |
228 | $block = blocks_get_record($instance->blockid); |
5bbbe0be |
229 | if(!blocks_name_allowed_in_format($block->name, $pageformat)) { |
8a47e075 |
230 | blocks_delete_instance($instance); |
f032aa7a |
231 | } |
232 | } |
233 | } |
234 | } |
235 | |
5bbbe0be |
236 | function blocks_name_allowed_in_format($name, $pageformat) { |
cd2bc3c9 |
237 | $accept = NULL; |
238 | $maxdepth = -1; |
239 | $formats = block_method_result($name, 'applicable_formats'); |
240 | if (!$formats) { |
241 | $formats = array(); |
242 | } |
243 | foreach ($formats as $format => $allowed) { |
244 | $formatregex = '/^'.str_replace('*', '[^-]*', $format).'.*$/'; |
245 | $depth = substr_count($format, '-'); |
246 | if (preg_match($formatregex, $pageformat) && $depth > $maxdepth) { |
247 | $maxdepth = $depth; |
248 | $accept = $allowed; |
5bbbe0be |
249 | } |
250 | } |
cd2bc3c9 |
251 | if ($accept === NULL) { |
5bbbe0be |
252 | $accept = !empty($formats['all']); |
253 | } |
254 | return $accept; |
255 | } |
256 | |
0d6b9d4f |
257 | function blocks_delete_instance($instance,$pinned=false) { |
58ff964f |
258 | global $DB; |
f032aa7a |
259 | |
e9a20759 |
260 | // Get the block object and call instance_delete() if possible |
7fa944ca |
261 | if($record = blocks_get_record($instance->blockid)) { |
262 | if($obj = block_instance($record->name, $instance)) { |
e9a20759 |
263 | // Return value ignored |
264 | $obj->instance_delete(); |
265 | } |
b33dd23a |
266 | } |
267 | |
0d6b9d4f |
268 | if (!empty($pinned)) { |
66b10689 |
269 | $DB->delete_records('block_pinned_old', array('id'=>$instance->id)); |
0d6b9d4f |
270 | // And now, decrement the weight of all blocks after this one |
66b10689 |
271 | $sql = "UPDATE {block_pinned_old} |
58ff964f |
272 | SET weight = weight - 1 |
273 | WHERE pagetype = ? AND position = ? AND weight > ?"; |
274 | $params = array($instance->pagetype, $instance->position, $instance->weight); |
275 | $DB->execute($sql, $params); |
0d6b9d4f |
276 | } else { |
277 | // Now kill the db record; |
66b10689 |
278 | $DB->delete_records('block_instance_old', array('oldid'=>$instance->id)); |
19f5b2db |
279 | delete_context(CONTEXT_BLOCK, $instance->id); |
0d6b9d4f |
280 | // And now, decrement the weight of all blocks after this one |
66b10689 |
281 | $sql = "UPDATE {block_instance_old} |
58ff964f |
282 | SET weight = weight - 1 |
283 | WHERE pagetype = ? AND pageid = ? |
284 | AND position = ? AND weight > ?"; |
285 | $params = array($instance->pagetype, $instance->pageid, $instance->position, $instance->weight); |
286 | $DB->execute($sql, $params); |
0d6b9d4f |
287 | } |
2e477369 |
288 | return true; |
f032aa7a |
289 | } |
290 | |
c8e0b579 |
291 | // Accepts an array of block instances and checks to see if any of them have content to display |
292 | // (causing them to calculate their content in the process). Returns true or false. Parameter passed |
293 | // by reference for speed; the array is actually not modified. |
dffd4bb9 |
294 | function blocks_have_content(&$pageblocks, $position) { |
0d6b9d4f |
295 | |
296 | if (empty($pageblocks) || !is_array($pageblocks) || !array_key_exists($position,$pageblocks)) { |
297 | return false; |
298 | } |
66c7e47b |
299 | // use a for() loop to get references to the array elements |
300 | // foreach() cannot fetch references in PHP v4.x |
301 | for ($n=0; $n<count($pageblocks[$position]);$n++) { |
302 | $instance = &$pageblocks[$position][$n]; |
6b726eb4 |
303 | if (empty($instance->visible)) { |
7933cc6b |
304 | continue; |
305 | } |
3ef642d9 |
306 | if(!$record = blocks_get_record($instance->blockid)) { |
7933cc6b |
307 | continue; |
308 | } |
3ef642d9 |
309 | if(!$obj = block_instance($record->name, $instance)) { |
9b4b78fd |
310 | continue; |
311 | } |
3ef642d9 |
312 | if(!$obj->is_empty()) { |
afd1ec02 |
313 | // cache rec and obj |
66c7e47b |
314 | // for blocks_print_group() |
b1a308be |
315 | $instance->rec = $record; |
afd1ec02 |
316 | $instance->obj = $obj; |
3ef642d9 |
317 | return true; |
0f3fe4b6 |
318 | } |
319 | } |
9b4b78fd |
320 | |
0f3fe4b6 |
321 | return false; |
322 | } |
323 | |
c8e0b579 |
324 | // This function prints one group of blocks in a page |
325 | // Parameters passed by reference for speed; they are not modified. |
66492322 |
326 | function blocks_print_group(&$page, &$pageblocks, $position) { |
6ad71d66 |
327 | global $COURSE, $CFG, $USER; |
66492322 |
328 | |
6b726eb4 |
329 | if (empty($pageblocks[$position])) { |
330 | $groupblocks = array(); |
b35fc182 |
331 | $maxweight = 0; |
6b726eb4 |
332 | } else { |
333 | $groupblocks = $pageblocks[$position]; |
334 | $maxweight = max(array_keys($groupblocks)); |
9b4b78fd |
335 | } |
3cdc4597 |
336 | |
6b726eb4 |
337 | |
338 | foreach ($groupblocks as $instance) { |
4374ee2c |
339 | if (!empty($instance->pinned)) { |
340 | $maxweight--; |
341 | } |
342 | } |
0f3fe4b6 |
343 | |
f032aa7a |
344 | $isediting = $page->user_is_editing(); |
6b726eb4 |
345 | |
346 | |
347 | foreach($groupblocks as $instance) { |
348 | |
afd1ec02 |
349 | |
66c7e47b |
350 | // $instance may have ->rec and ->obj |
351 | // cached from when we walked $pageblocks |
352 | // in blocks_have_content() |
353 | if (empty($instance->rec)) { |
6b726eb4 |
354 | if (empty($instance->blockid)) { |
355 | continue; // Can't do anything |
356 | } |
66c7e47b |
357 | $block = blocks_get_record($instance->blockid); |
358 | } else { |
359 | $block = $instance->rec; |
360 | } |
f809df70 |
361 | |
362 | if (empty($block)) { |
363 | // Block doesn't exist! We should delete this instance! |
364 | continue; |
365 | } |
366 | |
6b726eb4 |
367 | if (empty($block->visible)) { |
9b4b78fd |
368 | // Disabled by the admin |
369 | continue; |
370 | } |
afd1ec02 |
371 | |
66c7e47b |
372 | if (empty($instance->obj)) { |
373 | if (!$obj = block_instance($block->name, $instance)) { |
374 | // Invalid block |
375 | continue; |
376 | } |
377 | } else { |
378 | $obj = $instance->obj; |
ab0e4dd4 |
379 | } |
0f3fe4b6 |
380 | |
d7f688d4 |
381 | $editalways = false; |
0d6b9d4f |
382 | |
6b726eb4 |
383 | |
0d6b9d4f |
384 | if (($isediting && empty($instance->pinned)) || !empty($editalways)) { |
9b4b78fd |
385 | $options = 0; |
f032aa7a |
386 | // The block can be moved up if it's NOT the first one in its position. If it is, we look at the OR clause: |
387 | // the first block might still be able to move up if the page says so (i.e., it will change position) |
388 | $options |= BLOCK_MOVE_UP * ($instance->weight != 0 || ($page->blocks_move_position($instance, BLOCK_MOVE_UP) != $instance->position)); |
389 | // Same thing for downward movement |
390 | $options |= BLOCK_MOVE_DOWN * ($instance->weight != $maxweight || ($page->blocks_move_position($instance, BLOCK_MOVE_DOWN) != $instance->position)); |
391 | // For left and right movements, it's up to the page to tell us whether they are allowed |
392 | $options |= BLOCK_MOVE_RIGHT * ($page->blocks_move_position($instance, BLOCK_MOVE_RIGHT) != $instance->position); |
393 | $options |= BLOCK_MOVE_LEFT * ($page->blocks_move_position($instance, BLOCK_MOVE_LEFT ) != $instance->position); |
394 | // Finally, the block can be configured if the block class either allows multiple instances, or if it specifically |
395 | // allows instance configuration (multiple instances override that one). It doesn't have anything to do with what the |
396 | // administrator has allowed for this block in the site admin options. |
397 | $options |= BLOCK_CONFIGURE * ( $obj->instance_allow_multiple() || $obj->instance_allow_config() ); |
1345403a |
398 | $obj->_add_edit_controls($options); |
9b4b78fd |
399 | } |
0f3fe4b6 |
400 | |
0a0bb380 |
401 | if (!$instance->visible && empty($COURSE->javascriptportal)) { |
402 | if ($isediting) { |
1345403a |
403 | $obj->_print_shadow(); |
0f3fe4b6 |
404 | } |
0a0bb380 |
405 | } else { |
4a1a14c9 |
406 | global $COURSE; |
d23157d8 |
407 | if(!empty($COURSE->javascriptportal)) { |
408 | $COURSE->javascriptportal->currentblocksection = $position; |
409 | } |
1345403a |
410 | $obj->_print_block(); |
9b4b78fd |
411 | } |
d23157d8 |
412 | if (!empty($COURSE->javascriptportal) |
413 | && (empty($instance->pinned) || !$instance->pinned)) { |
afd1ec02 |
414 | $COURSE->javascriptportal->block_add('inst'.$instance->id, !$instance->visible); |
d23157d8 |
415 | } |
416 | } // End foreach |
417 | |
282c1695 |
418 | // Check if |
419 | // we are on the default position/side AND |
420 | // we're editing the page AND |
421 | // ( |
422 | // we have the capability to manage blocks OR |
423 | // we are in myMoodle page AND have the capibility to manage myMoodle blocks |
424 | // ) |
6ad71d66 |
425 | |
426 | // for constant PAGE_MY_MOODLE |
427 | include_once($CFG->dirroot.'/my/pagelib.php'); |
afd1ec02 |
428 | |
6ad71d66 |
429 | $coursecontext = get_context_instance(CONTEXT_COURSE, $COURSE->id); |
d529807a |
430 | $myownblogpage = (isset($page->filtertype) && isset($page->filterselect) && $page->pagetype=='blog-view' && $page->filtertype=='user' && $page->filterselect == $USER->id); |
afd1ec02 |
431 | |
6ad71d66 |
432 | $managecourseblocks = has_capability('moodle/site:manageblocks', $coursecontext); |
d529807a |
433 | $editmymoodle = $page->pagetype == PAGE_MY_MOODLE && has_capability('moodle/my:manageblocks', $coursecontext); |
afd1ec02 |
434 | |
86b5ea0f |
435 | if ($page->blocks->get_default_region() == $position && |
0be6f678 |
436 | $page->user_is_editing() && |
ee1efe0b |
437 | ($managecourseblocks || $editmymoodle || $myownblogpage || defined('ADMIN_STICKYBLOCKS'))) { |
6ad71d66 |
438 | |
66492322 |
439 | blocks_print_adminblock($page, $pageblocks); |
440 | } |
0f3fe4b6 |
441 | } |
442 | |
c8e0b579 |
443 | // This iterates over an array of blocks and calculates the preferred width |
444 | // Parameter passed by reference for speed; it's not modified. |
445 | function blocks_preferred_width(&$instances) { |
0f3fe4b6 |
446 | $width = 0; |
447 | |
9b4b78fd |
448 | if(empty($instances) || !is_array($instances)) { |
0f3fe4b6 |
449 | return 0; |
450 | } |
ff6191b7 |
451 | |
452 | $blocks = blocks_get_record(); |
453 | |
9b4b78fd |
454 | foreach($instances as $instance) { |
455 | if(!$instance->visible) { |
fe78a3dc |
456 | continue; |
0c9c6363 |
457 | } |
ff6191b7 |
458 | |
f809df70 |
459 | if (!array_key_exists($instance->blockid, $blocks)) { |
460 | // Block doesn't exist! We should delete this instance! |
461 | continue; |
462 | } |
463 | |
ff6191b7 |
464 | if(!$blocks[$instance->blockid]->visible) { |
9b687320 |
465 | continue; |
466 | } |
ff6191b7 |
467 | $pref = block_method_result($blocks[$instance->blockid]->name, 'preferred_width'); |
9b4b78fd |
468 | if($pref === NULL) { |
469 | continue; |
470 | } |
471 | if($pref > $width) { |
472 | $width = $pref; |
0f3fe4b6 |
473 | } |
474 | } |
475 | return $width; |
476 | } |
477 | |
9b4b78fd |
478 | function blocks_get_record($blockid = NULL, $invalidate = false) { |
58ff964f |
479 | global $DB; |
480 | |
9b4b78fd |
481 | static $cache = NULL; |
0f3fe4b6 |
482 | |
9b4b78fd |
483 | if($invalidate || empty($cache)) { |
58ff964f |
484 | $cache = $DB->get_records('block'); |
9b4b78fd |
485 | } |
0f3fe4b6 |
486 | |
9b4b78fd |
487 | if($blockid === NULL) { |
488 | return $cache; |
489 | } |
0f3fe4b6 |
490 | |
9b4b78fd |
491 | return (isset($cache[$blockid])? $cache[$blockid] : false); |
492 | } |
493 | |
494 | function blocks_find_block($blockid, $blocksarray) { |
0d6b9d4f |
495 | if (empty($blocksarray)) { |
496 | return false; |
497 | } |
9b4b78fd |
498 | foreach($blocksarray as $blockgroup) { |
0d6b9d4f |
499 | if (empty($blockgroup)) { |
500 | continue; |
501 | } |
9b4b78fd |
502 | foreach($blockgroup as $instance) { |
503 | if($instance->blockid == $blockid) { |
504 | return $instance; |
505 | } |
506 | } |
507 | } |
508 | return false; |
509 | } |
510 | |
511 | function blocks_find_instance($instanceid, $blocksarray) { |
512 | foreach($blocksarray as $subarray) { |
513 | foreach($subarray as $instance) { |
514 | if($instance->id == $instanceid) { |
515 | return $instance; |
516 | } |
517 | } |
518 | } |
519 | return false; |
520 | } |
521 | |
ec79d3e4 |
522 | // Simple entry point for anyone that wants to use blocks |
ee6055eb |
523 | function blocks_setup(&$PAGE,$pinned=BLOCKS_PINNED_FALSE) { |
524 | switch ($pinned) { |
525 | case BLOCKS_PINNED_TRUE: |
526 | $pageblocks = blocks_get_pinned($PAGE); |
527 | break; |
528 | case BLOCKS_PINNED_BOTH: |
529 | $pageblocks = blocks_get_by_page_pinned($PAGE); |
530 | break; |
531 | case BLOCKS_PINNED_FALSE: |
532 | default: |
533 | $pageblocks = blocks_get_by_page($PAGE); |
534 | break; |
535 | } |
536 | blocks_execute_url_action($PAGE, $pageblocks,($pinned==BLOCKS_PINNED_TRUE)); |
ec79d3e4 |
537 | return $pageblocks; |
538 | } |
539 | |
b1631fef |
540 | function blocks_execute_action($page, &$pageblocks, $blockaction, $instanceorid, $pinned=false, $redirect=true) { |
58ff964f |
541 | global $CFG, $USER, $DB; |
9b4b78fd |
542 | |
a9c75a9c |
543 | if (is_int($instanceorid)) { |
9b4b78fd |
544 | $blockid = $instanceorid; |
a9c75a9c |
545 | } else if (is_object($instanceorid)) { |
9b4b78fd |
546 | $instance = $instanceorid; |
547 | } |
0f3fe4b6 |
548 | |
549 | switch($blockaction) { |
9b4b78fd |
550 | case 'config': |
9b4b78fd |
551 | $block = blocks_get_record($instance->blockid); |
e82d6cac |
552 | // Hacky hacky tricky stuff to get the original human readable block title, |
553 | // even if the block has configured its title to be something else. |
afd1ec02 |
554 | // Create the object WITHOUT instance data. |
e82d6cac |
555 | $blockobject = block_instance($block->name); |
9b4b78fd |
556 | if ($blockobject === false) { |
11306331 |
557 | break; |
558 | } |
afd1ec02 |
559 | |
11306331 |
560 | // First of all check to see if the block wants to be edited |
561 | if(!$blockobject->user_can_edit()) { |
562 | break; |
9b4b78fd |
563 | } |
11306331 |
564 | |
e82d6cac |
565 | // Now get the title and AFTER that load up the instance |
566 | $blocktitle = $blockobject->get_title(); |
567 | $blockobject->_load_instance($instance); |
afd1ec02 |
568 | |
27ec21a0 |
569 | // Define the data we're going to silently include in the instance config form here, |
9b4b78fd |
570 | // so we can strip them from the submitted data BEFORE serializing it. |
571 | $hiddendata = array( |
19f5b2db |
572 | 'sesskey' => sesskey(), |
9b4b78fd |
573 | 'instanceid' => $instance->id, |
574 | 'blockaction' => 'config' |
575 | ); |
f032aa7a |
576 | |
577 | // To this data, add anything the page itself needs to display |
ad52c04f |
578 | $hiddendata = $page->url->params($hiddendata); |
9b4b78fd |
579 | |
294ce987 |
580 | if ($data = data_submitted()) { |
9b4b78fd |
581 | $remove = array_keys($hiddendata); |
582 | foreach($remove as $item) { |
583 | unset($data->$item); |
0f3fe4b6 |
584 | } |
58ff964f |
585 | if(!$blockobject->instance_config_save($data, $pinned)) { |
e49ef64a |
586 | print_error('cannotsaveblock'); |
0f3fe4b6 |
587 | } |
9b4b78fd |
588 | // And nothing more, continue with displaying the page |
0f3fe4b6 |
589 | } |
9b4b78fd |
590 | else { |
f032aa7a |
591 | // We need to show the config screen, so we highjack the display logic and then die |
e82d6cac |
592 | $strheading = get_string('blockconfiga', 'moodle', $blocktitle); |
edb42f09 |
593 | $page->print_header(get_string('pageheaderconfigablock', 'moodle'), array($strheading => '')); |
b9709905 |
594 | |
595 | echo '<div class="block-config" id="'.$block->name.'">'; /// Make CSS easier |
0be6f678 |
596 | |
edb42f09 |
597 | print_heading($strheading); |
ad52c04f |
598 | echo '<form method="post" name="block-config" action="'. $page->url->out(false) .'">'; |
9b4b78fd |
599 | echo '<p>'; |
600 | foreach($hiddendata as $name => $val) { |
27ec21a0 |
601 | echo '<input type="hidden" name="'. $name .'" value="'. $val .'" />'; |
0f3fe4b6 |
602 | } |
9b4b78fd |
603 | echo '</p>'; |
604 | $blockobject->instance_config_print(); |
605 | echo '</form>'; |
b9709905 |
606 | |
607 | echo '</div>'; |
ad5d5997 |
608 | $PAGE->set_pagetype('blocks-' . $block->name); |
9b4b78fd |
609 | print_footer(); |
f032aa7a |
610 | die(); // Do not go on with the other page-related stuff |
0f3fe4b6 |
611 | } |
612 | break; |
9b4b78fd |
613 | case 'toggle': |
614 | if(empty($instance)) { |
e49ef64a |
615 | print_error('invalidblockinstance', '', '', $blockaction); |
0f3fe4b6 |
616 | } |
9b4b78fd |
617 | $instance->visible = ($instance->visible) ? 0 : 1; |
0d6b9d4f |
618 | if (!empty($pinned)) { |
66b10689 |
619 | $DB->update_record('block_pinned_old', $instance); |
0d6b9d4f |
620 | } else { |
66b10689 |
621 | $DB->update_record('block_instance_old', $instance); |
0d6b9d4f |
622 | } |
9b4b78fd |
623 | break; |
624 | case 'delete': |
625 | if(empty($instance)) { |
e49ef64a |
626 | print_error('invalidblockinstance', '', '', $blockaction); |
0f3fe4b6 |
627 | } |
0d6b9d4f |
628 | blocks_delete_instance($instance, $pinned); |
0f3fe4b6 |
629 | break; |
630 | case 'moveup': |
9b4b78fd |
631 | if(empty($instance)) { |
e49ef64a |
632 | print_error('invalidblockinstance', '', '', $blockaction); |
9b4b78fd |
633 | } |
f032aa7a |
634 | |
635 | if($instance->weight == 0) { |
636 | // The block is the first one, so a move "up" probably means it changes position |
637 | // Where is the instance going to be moved? |
638 | $newpos = $page->blocks_move_position($instance, BLOCK_MOVE_UP); |
6b853ff4 |
639 | $newweight = (empty($pageblocks[$newpos]) ? 0 : max(array_keys($pageblocks[$newpos])) + 1); |
f032aa7a |
640 | |
0d6b9d4f |
641 | blocks_execute_repositioning($instance, $newpos, $newweight, $pinned); |
89a5baab |
642 | } |
f032aa7a |
643 | else { |
644 | // The block is just moving upwards in the same position. |
645 | // This configuration will make sure that even if somehow the weights |
646 | // become not continuous, block move operations will eventually bring |
647 | // the situation back to normal without printing any warnings. |
648 | if(!empty($pageblocks[$instance->position][$instance->weight - 1])) { |
649 | $other = $pageblocks[$instance->position][$instance->weight - 1]; |
650 | } |
651 | if(!empty($other)) { |
652 | ++$other->weight; |
0d6b9d4f |
653 | if (!empty($pinned)) { |
66b10689 |
654 | $DB->update_record('block_pinned_old', $other); |
0d6b9d4f |
655 | } else { |
66b10689 |
656 | $DB->update_record('block_instance_old', $other); |
afd1ec02 |
657 | } |
f032aa7a |
658 | } |
659 | --$instance->weight; |
0d6b9d4f |
660 | if (!empty($pinned)) { |
66b10689 |
661 | $DB->update_record('block_pinned_old', $instance); |
0d6b9d4f |
662 | } else { |
66b10689 |
663 | $DB->update_record('block_instance_old', $instance); |
0d6b9d4f |
664 | } |
0f3fe4b6 |
665 | } |
666 | break; |
667 | case 'movedown': |
9b4b78fd |
668 | if(empty($instance)) { |
e49ef64a |
669 | print_error('invalidblockinstance', '', '', $blockaction); |
9b4b78fd |
670 | } |
f032aa7a |
671 | |
672 | if($instance->weight == max(array_keys($pageblocks[$instance->position]))) { |
673 | // The block is the last one, so a move "down" probably means it changes position |
674 | // Where is the instance going to be moved? |
675 | $newpos = $page->blocks_move_position($instance, BLOCK_MOVE_DOWN); |
6b853ff4 |
676 | $newweight = (empty($pageblocks[$newpos]) ? 0 : max(array_keys($pageblocks[$newpos])) + 1); |
f032aa7a |
677 | |
0d6b9d4f |
678 | blocks_execute_repositioning($instance, $newpos, $newweight, $pinned); |
89a5baab |
679 | } |
f032aa7a |
680 | else { |
681 | // The block is just moving downwards in the same position. |
682 | // This configuration will make sure that even if somehow the weights |
683 | // become not continuous, block move operations will eventually bring |
684 | // the situation back to normal without printing any warnings. |
685 | if(!empty($pageblocks[$instance->position][$instance->weight + 1])) { |
686 | $other = $pageblocks[$instance->position][$instance->weight + 1]; |
687 | } |
688 | if(!empty($other)) { |
689 | --$other->weight; |
0d6b9d4f |
690 | if (!empty($pinned)) { |
66b10689 |
691 | $DB->update_record('block_pinned_old', $other); |
0d6b9d4f |
692 | } else { |
66b10689 |
693 | $DB->update_record('block_instance_old', $other); |
0d6b9d4f |
694 | } |
f032aa7a |
695 | } |
696 | ++$instance->weight; |
0d6b9d4f |
697 | if (!empty($pinned)) { |
66b10689 |
698 | $DB->update_record('block_pinned_old', $instance); |
0d6b9d4f |
699 | } else { |
66b10689 |
700 | $DB->update_record('block_instance_old', $instance); |
0d6b9d4f |
701 | } |
0f3fe4b6 |
702 | } |
703 | break; |
9b4b78fd |
704 | case 'moveleft': |
705 | if(empty($instance)) { |
e49ef64a |
706 | print_error('invalidblockinstance', '', '', $blockaction); |
9b4b78fd |
707 | } |
f032aa7a |
708 | |
709 | // Where is the instance going to be moved? |
710 | $newpos = $page->blocks_move_position($instance, BLOCK_MOVE_LEFT); |
6b853ff4 |
711 | $newweight = (empty($pageblocks[$newpos]) ? 0 : max(array_keys($pageblocks[$newpos])) + 1); |
f032aa7a |
712 | |
0d6b9d4f |
713 | blocks_execute_repositioning($instance, $newpos, $newweight, $pinned); |
0f3fe4b6 |
714 | break; |
9b4b78fd |
715 | case 'moveright': |
716 | if(empty($instance)) { |
e49ef64a |
717 | print_error('invalidblockinstance', '', '', $blockaction); |
9b4b78fd |
718 | } |
f032aa7a |
719 | |
720 | // Where is the instance going to be moved? |
721 | $newpos = $page->blocks_move_position($instance, BLOCK_MOVE_RIGHT); |
6b853ff4 |
722 | $newweight = (empty($pageblocks[$newpos]) ? 0 : max(array_keys($pageblocks[$newpos])) + 1); |
f032aa7a |
723 | |
0d6b9d4f |
724 | blocks_execute_repositioning($instance, $newpos, $newweight, $pinned); |
9b4b78fd |
725 | break; |
726 | case 'add': |
727 | // Add a new instance of this block, if allowed |
728 | $block = blocks_get_record($blockid); |
0f3fe4b6 |
729 | |
3cacefda |
730 | if(empty($block) || !$block->visible) { |
731 | // Only allow adding if the block exists and is enabled |
11306331 |
732 | break; |
9b4b78fd |
733 | } |
0f3fe4b6 |
734 | |
89a5baab |
735 | if(!$block->multiple && blocks_find_block($blockid, $pageblocks) !== false) { |
736 | // If no multiples are allowed and we already have one, return now |
11306331 |
737 | break; |
738 | } |
739 | |
740 | if(!block_method_result($block->name, 'user_can_addto', $page)) { |
741 | // If the block doesn't want to be added... |
742 | break; |
89a5baab |
743 | } |
744 | |
86b5ea0f |
745 | $newpos = $page->blocks->get_default_region(); |
0d6b9d4f |
746 | if (!empty($pinned)) { |
58ff964f |
747 | $sql = "SELECT 1, MAX(weight) + 1 AS nextfree |
66b10689 |
748 | FROM {block_pinned_old} |
58ff964f |
749 | WHERE pagetype = ? AND position = ?"; |
f230ce19 |
750 | $params = array($page->pagetype, $newpos); |
58ff964f |
751 | |
0d6b9d4f |
752 | } else { |
58ff964f |
753 | $sql = "SELECT 1, MAX(weight) + 1 AS nextfree |
66b10689 |
754 | FROM {block_instance_old} |
58ff964f |
755 | WHERE pageid = ? AND pagetype = ? AND position = ?"; |
f230ce19 |
756 | $params = array($page->get_id(), $page->pagetype, $newpos); |
0d6b9d4f |
757 | } |
58ff964f |
758 | $weight = $DB->get_record_sql($sql, $params); |
9b4b78fd |
759 | |
760 | $newinstance = new stdClass; |
761 | $newinstance->blockid = $blockid; |
0d6b9d4f |
762 | if (empty($pinned)) { |
763 | $newinstance->pageid = $page->get_id(); |
764 | } |
f230ce19 |
765 | $newinstance->pagetype = $page->pagetype; |
f032aa7a |
766 | $newinstance->position = $newpos; |
ff679897 |
767 | $newinstance->weight = empty($weight->nextfree) ? 0 : $weight->nextfree; |
9b4b78fd |
768 | $newinstance->visible = 1; |
769 | $newinstance->configdata = ''; |
0d6b9d4f |
770 | if (!empty($pinned)) { |
66b10689 |
771 | $newinstance->id = $DB->insert_record('block_pinned_old', $newinstance); |
0d6b9d4f |
772 | } else { |
66b10689 |
773 | $newinstance->id = $DB->insert_record('block_instance_old', $newinstance); |
0d6b9d4f |
774 | } |
b33dd23a |
775 | |
776 | // If the new instance was created, allow it to do additional setup |
777 | if($newinstance && ($obj = block_instance($block->name, $newinstance))) { |
778 | // Return value ignored |
779 | $obj->instance_create(); |
780 | } |
781 | |
9b4b78fd |
782 | break; |
783 | } |
f032aa7a |
784 | |
b1631fef |
785 | if ($redirect) { |
786 | // In order to prevent accidental duplicate actions, redirect to a page with a clean url |
ad52c04f |
787 | redirect($page->url->out()); |
b1631fef |
788 | } |
f032aa7a |
789 | } |
790 | |
da71112b |
791 | // You can use this to get the blocks to respond to URL actions without much hassle |
0d6b9d4f |
792 | function blocks_execute_url_action(&$PAGE, &$pageblocks,$pinned=false) { |
02cc05a7 |
793 | $blockaction = optional_param('blockaction', '', PARAM_ALPHA); |
da71112b |
794 | |
3edc57e1 |
795 | if (empty($blockaction) || !$PAGE->user_allowed_editing() || !confirm_sesskey()) { |
da71112b |
796 | return; |
797 | } |
798 | |
799 | $instanceid = optional_param('instanceid', 0, PARAM_INT); |
800 | $blockid = optional_param('blockid', 0, PARAM_INT); |
afd1ec02 |
801 | |
da71112b |
802 | if (!empty($blockid)) { |
0d6b9d4f |
803 | blocks_execute_action($PAGE, $pageblocks, strtolower($blockaction), $blockid, $pinned); |
da71112b |
804 | |
805 | } |
806 | else if (!empty($instanceid)) { |
807 | $instance = blocks_find_instance($instanceid, $pageblocks); |
0d6b9d4f |
808 | blocks_execute_action($PAGE, $pageblocks, strtolower($blockaction), $instance, $pinned); |
da71112b |
809 | } |
810 | } |
811 | |
f032aa7a |
812 | // This shouldn't be used externally at all, it's here for use by blocks_execute_action() |
813 | // in order to reduce code repetition. |
29ca8b88 |
814 | function blocks_execute_repositioning(&$instance, $newpos, $newweight, $pinned=false) { |
58ff964f |
815 | global $DB; |
f032aa7a |
816 | |
c4308cfa |
817 | // If it's staying where it is, don't do anything, unless overridden |
29ca8b88 |
818 | if ($newpos == $instance->position) { |
f032aa7a |
819 | return; |
820 | } |
821 | |
822 | // Close the weight gap we 'll leave behind |
0d6b9d4f |
823 | if (!empty($pinned)) { |
66b10689 |
824 | $sql = "UPDATE {block_instance_old} |
58ff964f |
825 | SET weight = weight - 1 |
826 | WHERE pagetype = ? AND position = ? AND weight > ?"; |
827 | $params = array($instance->pagetype, $instance->position, $instance->weight); |
828 | |
0d6b9d4f |
829 | } else { |
66b10689 |
830 | $sql = "UPDATE {block_instance_old} |
58ff964f |
831 | SET weight = weight - 1 |
832 | WHERE pagetype = ? AND pageid = ? |
833 | AND position = ? AND weight > ?"; |
834 | $params = array($instance->pagetype, $instance->pageid, $instance->position, $instance->weight); |
0d6b9d4f |
835 | } |
58ff964f |
836 | $DB->execute($sql, $params); |
f032aa7a |
837 | |
838 | $instance->position = $newpos; |
839 | $instance->weight = $newweight; |
840 | |
0d6b9d4f |
841 | if (!empty($pinned)) { |
66b10689 |
842 | $DB->update_record('block_pinned_old', $instance); |
0d6b9d4f |
843 | } else { |
66b10689 |
844 | $DB->update_record('block_instance_old', $instance); |
0d6b9d4f |
845 | } |
846 | } |
847 | |
29ca8b88 |
848 | |
849 | /** |
850 | * Moves a block to the new position (column) and weight (sort order). |
851 | * @param $instance - The block instance to be moved. |
852 | * @param $destpos - BLOCK_POS_LEFT or BLOCK_POS_RIGHT. The destination column. |
853 | * @param $destweight - The destination sort order. If NULL, we add to the end |
854 | * of the destination column. |
855 | * @param $pinned - Are we moving pinned blocks? We can only move pinned blocks |
856 | * to a new position withing the pinned list. Likewise, we |
857 | * can only moved non-pinned blocks to a new position within |
858 | * the non-pinned list. |
859 | * @return boolean (success or failure). |
860 | */ |
861 | function blocks_move_block($page, &$instance, $destpos, $destweight=NULL, $pinned=false) { |
58ff964f |
862 | global $CFG, $DB; |
afd1ec02 |
863 | |
29ca8b88 |
864 | if ($pinned) { |
865 | $blocklist = blocks_get_pinned($page); |
866 | } else { |
867 | $blocklist = blocks_get_by_page($page); |
868 | } |
afd1ec02 |
869 | |
29ca8b88 |
870 | if ($blocklist[$instance->position][$instance->weight]->id != $instance->id) { |
871 | // The source block instance is not where we think it is. |
c4308cfa |
872 | return false; |
d23157d8 |
873 | } |
afd1ec02 |
874 | |
29ca8b88 |
875 | // First we close the gap that will be left behind when we take out the |
876 | // block from it's current column. |
877 | if ($pinned) { |
66b10689 |
878 | $closegapsql = "UPDATE {block_instance_old} |
afd1ec02 |
879 | SET weight = weight - 1 |
58ff964f |
880 | WHERE weight > ? AND position = ? AND pagetype = ?"; |
881 | $params = array($instance->weight, $instance->position, $instance->pagetype); |
e028ed34 |
882 | } else { |
66b10689 |
883 | $closegapsql = "UPDATE {block_instance_old} |
afd1ec02 |
884 | SET weight = weight - 1 |
58ff964f |
885 | WHERE weight > ? AND position = ? |
886 | AND pagetype = ? AND pageid = ?"; |
887 | $params = array($instance->weight, $instance->position, $instance->pagetype, $instance->pageid); |
29ca8b88 |
888 | } |
58ff964f |
889 | if (!$DB->execute($closegapsql, $params)) { |
29ca8b88 |
890 | return false; |
77e65ff7 |
891 | } |
afd1ec02 |
892 | |
29ca8b88 |
893 | // Now let's make space for the block being moved. |
894 | if ($pinned) { |
66b10689 |
895 | $opengapsql = "UPDATE {block_instance_old} |
afd1ec02 |
896 | SET weight = weight + 1 |
58ff964f |
897 | WHERE weight >= ? AND position = ? AND pagetype = ?"; |
898 | $params = array($destweight, $destpos, $instance->pagetype); |
d23157d8 |
899 | } else { |
66b10689 |
900 | $opengapsql = "UPDATE {block_instance_old} |
58ff964f |
901 | SET weight = weight + 1 |
902 | WHERE weight >= ? AND position = ? |
903 | AND pagetype = ? AND pageid = ?"; |
904 | $params = array($destweight, $destpos, $instance->pagetype, $instance->pageid); |
29ca8b88 |
905 | } |
655b09ca |
906 | if (!$DB->execute($opengapsql, $params)) { |
29ca8b88 |
907 | return false; |
c4308cfa |
908 | } |
afd1ec02 |
909 | |
29ca8b88 |
910 | // Move the block. |
911 | $instance->position = $destpos; |
912 | $instance->weight = $destweight; |
e028ed34 |
913 | |
29ca8b88 |
914 | if ($pinned) { |
66b10689 |
915 | $table = 'block_pinned_old'; |
29ca8b88 |
916 | } else { |
66b10689 |
917 | $table = 'block_instance_old'; |
29ca8b88 |
918 | } |
58ff964f |
919 | return $DB->update_record($table, $instance); |
e028ed34 |
920 | } |
921 | |
d23157d8 |
922 | |
923 | /** |
924 | * Returns an array consisting of 2 arrays: |
925 | * 1) Array of pinned blocks for position BLOCK_POS_LEFT |
926 | * 2) Array of pinned blocks for position BLOCK_POS_RIGHT |
927 | */ |
0d6b9d4f |
928 | function blocks_get_pinned($page) { |
58ff964f |
929 | global $DB; |
afd1ec02 |
930 | |
0d6b9d4f |
931 | $visible = true; |
58ff964f |
932 | $select = "pagetype = ?"; |
f230ce19 |
933 | $params = array($page->pagetype); |
58ff964f |
934 | |
935 | if ($visible) { |
936 | $select .= " AND visible = 1"; |
937 | } |
938 | |
66b10689 |
939 | $blocks = $DB->get_records_select('block_pinned_old', $select, $params, 'position, weight'); |
0d6b9d4f |
940 | |
86b5ea0f |
941 | $positions = $page->blocks->get_regions(); |
0d6b9d4f |
942 | $arr = array(); |
943 | |
944 | foreach($positions as $key => $position) { |
945 | $arr[$position] = array(); |
946 | } |
947 | |
948 | if(empty($blocks)) { |
949 | return $arr; |
950 | } |
951 | |
952 | foreach($blocks as $block) { |
953 | $block->pinned = true; // so we know we can't move it. |
ddf1935f |
954 | // make up an instanceid if we can.. |
955 | $block->pageid = $page->get_id(); |
0d6b9d4f |
956 | $arr[$block->position][$block->weight] = $block; |
957 | } |
958 | |
afd1ec02 |
959 | return $arr; |
0d6b9d4f |
960 | } |
961 | |
962 | |
d23157d8 |
963 | /** |
964 | * Similar to blocks_get_by_page(), except that, the array returned includes |
965 | * pinned blocks as well. Pinned blocks are always appended before normal |
966 | * block instances. |
967 | */ |
0d6b9d4f |
968 | function blocks_get_by_page_pinned($page) { |
969 | $pinned = blocks_get_pinned($page); |
970 | $user = blocks_get_by_page($page); |
afd1ec02 |
971 | |
0d6b9d4f |
972 | $weights = array(); |
973 | |
974 | foreach ($pinned as $pos => $arr) { |
975 | $weights[$pos] = count($arr); |
976 | } |
977 | |
978 | foreach ($user as $pos => $blocks) { |
979 | if (!array_key_exists($pos,$pinned)) { |
980 | $pinned[$pos] = array(); |
981 | } |
982 | if (!array_key_exists($pos,$weights)) { |
983 | $weights[$pos] = 0; |
984 | } |
77e65ff7 |
985 | foreach ($blocks as $block) { |
0d6b9d4f |
986 | $pinned[$pos][$weights[$pos]] = $block; |
987 | $weights[$pos]++; |
988 | } |
989 | } |
990 | return $pinned; |
0f3fe4b6 |
991 | } |
992 | |
d23157d8 |
993 | |
994 | /** |
995 | * Returns an array of blocks for the page. Pinned blocks are excluded. |
996 | */ |
9b4b78fd |
997 | function blocks_get_by_page($page) { |
58ff964f |
998 | global $DB; |
999 | |
66b10689 |
1000 | $blocks = $DB->get_records_select('block_instance_old', "pageid = ? AND ? LIKE (" . $DB->sql_concat('pagetype', "'%'") . ")", |
f230ce19 |
1001 | array($page->get_id(), $page->pagetype), 'position, weight'); |
f032aa7a |
1002 | |
86b5ea0f |
1003 | $positions = $page->blocks->get_regions(); |
f032aa7a |
1004 | $arr = array(); |
1005 | foreach($positions as $key => $position) { |
1006 | $arr[$position] = array(); |
1007 | } |
0f3fe4b6 |
1008 | |
9b4b78fd |
1009 | if(empty($blocks)) { |
1010 | return $arr; |
1011 | } |
0f3fe4b6 |
1012 | |
77e65ff7 |
1013 | foreach($blocks as $block) { |
9b4b78fd |
1014 | $arr[$block->position][$block->weight] = $block; |
0f3fe4b6 |
1015 | } |
d23157d8 |
1016 | return $arr; |
9b4b78fd |
1017 | } |
0f3fe4b6 |
1018 | |
d23157d8 |
1019 | |
9b4b78fd |
1020 | //This function prints the block to admin blocks as necessary |
c1d8705f |
1021 | function blocks_print_adminblock(&$page, &$pageblocks) { |
9b4b78fd |
1022 | global $USER; |
0f3fe4b6 |
1023 | |
c1d8705f |
1024 | $missingblocks = blocks_get_missing($page, $pageblocks); |
1025 | |
9b4b78fd |
1026 | if (!empty($missingblocks)) { |
74fd9ff9 |
1027 | $strblocks = '<div class="title"><h2>'; |
303fe9f4 |
1028 | $strblocks .= get_string('blocks'); |
552adc27 |
1029 | $strblocks .= '</h2></div>'; |
c1d8705f |
1030 | $stradd = get_string('add'); |
9b4b78fd |
1031 | foreach ($missingblocks as $blockid) { |
1032 | $block = blocks_get_record($blockid); |
9b4b78fd |
1033 | $blockobject = block_instance($block->name); |
1034 | if ($blockobject === false) { |
1035 | continue; |
1036 | } |
11306331 |
1037 | if(!$blockobject->user_can_addto($page)) { |
1038 | continue; |
1039 | } |
9b4b78fd |
1040 | $menu[$block->id] = $blockobject->get_title(); |
1041 | } |
4a4c30d2 |
1042 | asort($menu); |
0f3fe4b6 |
1043 | |
ad52c04f |
1044 | $target = $page->url->out(array('sesskey' => sesskey(), 'blockaction' => 'add')); |
f032aa7a |
1045 | $content = popup_form($target.'&blockid=', $menu, 'add_block', '', $stradd .'...', '', '', true); |
afd8402c |
1046 | print_side_block($strblocks, $content, NULL, NULL, NULL, array('class' => 'block_adminblock')); |
0f3fe4b6 |
1047 | } |
0f3fe4b6 |
1048 | } |
1049 | |
19f5b2db |
1050 | /** |
1051 | * Delete all the blocks from a particular page. |
1052 | * |
1053 | * @param string $pagetype the page type. |
1054 | * @param integer $pageid the page id. |
1055 | * @return success of failure. |
1056 | */ |
1057 | function blocks_delete_all_on_page($pagetype, $pageid) { |
1058 | global $DB; |
66b10689 |
1059 | if ($instances = $DB->get_records('block_instance_old', array('pageid' => $pageid, 'pagetype' => $pagetype))) { |
19f5b2db |
1060 | foreach ($instances as $instance) { |
1061 | delete_context(CONTEXT_BLOCK, $instance->id); // Ingore any failures here. |
1062 | } |
1063 | } |
66b10689 |
1064 | return $DB->delete_records('block_instance_old', array('pageid' => $pageid, 'pagetype' => $pagetype)); |
19f5b2db |
1065 | } |
1066 | |
1067 | // Dispite what this function is called, it seems to be mostly used to populate |
1068 | // the default blocks when a new course (or whatever) is created. |
9b4b78fd |
1069 | function blocks_repopulate_page($page) { |
7e0489f4 |
1070 | global $CFG, $DB; |
5b224948 |
1071 | |
9b4b78fd |
1072 | $allblocks = blocks_get_record(); |
1073 | |
1074 | if(empty($allblocks)) { |
e49ef64a |
1075 | print_error('cannotgetblock'); |
9b4b78fd |
1076 | } |
1077 | |
f032aa7a |
1078 | // Assemble the information to correlate block names to ids |
9b4b78fd |
1079 | $idforname = array(); |
1080 | foreach($allblocks as $block) { |
1081 | $idforname[$block->name] = $block->id; |
1082 | } |
1083 | |
f032aa7a |
1084 | /// If the site override has been defined, it is the only valid one. |
1085 | if (!empty($CFG->defaultblocks_override)) { |
1086 | $blocknames = $CFG->defaultblocks_override; |
1087 | } |
1088 | else { |
1089 | $blocknames = $page->blocks_get_default(); |
1090 | } |
afd1ec02 |
1091 | |
86b5ea0f |
1092 | $positions = $page->blocks->get_regions(); |
f032aa7a |
1093 | $posblocks = explode(':', $blocknames); |
1094 | |
1095 | // Now one array holds the names of the positions, and the other one holds the blocks |
1096 | // that are going to go in each position. Luckily for us, both arrays are numerically |
1097 | // indexed and the indexes match, so we can work straight away... but CAREFULLY! |
9b4b78fd |
1098 | |
f032aa7a |
1099 | // Ready to start creating block instances, but first drop any existing ones |
f230ce19 |
1100 | blocks_delete_all_on_page($page->pagetype, $page->get_id()); |
f032aa7a |
1101 | |
1102 | // Here we slyly count $posblocks and NOT $positions. This can actually make a difference |
1103 | // if the textual representation has undefined slots in the end. So we only work with as many |
1104 | // positions were retrieved, not with all the page says it has available. |
1105 | $numpositions = count($posblocks); |
1106 | for($i = 0; $i < $numpositions; ++$i) { |
1107 | $position = $positions[$i]; |
1108 | $blocknames = explode(',', $posblocks[$i]); |
9b4b78fd |
1109 | $weight = 0; |
1110 | foreach($blocknames as $blockname) { |
1111 | $newinstance = new stdClass; |
1112 | $newinstance->blockid = $idforname[$blockname]; |
f032aa7a |
1113 | $newinstance->pageid = $page->get_id(); |
f230ce19 |
1114 | $newinstance->pagetype = $page->pagetype; |
9b4b78fd |
1115 | $newinstance->position = $position; |
1116 | $newinstance->weight = $weight; |
1117 | $newinstance->visible = 1; |
1118 | $newinstance->configdata = ''; |
3cacefda |
1119 | |
1120 | if(!empty($newinstance->blockid)) { |
1121 | // Only add block if it was recognized |
66b10689 |
1122 | $DB->insert_record('block_instance_old', $newinstance); |
3cacefda |
1123 | ++$weight; |
1124 | } |
0f3fe4b6 |
1125 | } |
1126 | } |
9b4b78fd |
1127 | |
1128 | return true; |
0f3fe4b6 |
1129 | } |
1130 | |
f10306b9 |
1131 | ?> |