MDL-35669 gravatar Provide default image URL to Gravatar
[moodle.git] / lib / blocklib.php
blobe9c133ffed6ec78787927934d6488fb1e3b4f79e
1 <?php
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.
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/>.
18 /**
19 * Block Class and Functions
21 * This file defines the {@link block_manager} class,
23 * @package core
24 * @subpackage block
25 * @copyright 1999 onwards Martin Dougiamas http://dougiamas.com
26 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
29 defined('MOODLE_INTERNAL') || die();
31 /**#@+
32 * @deprecated since Moodle 2.0. No longer used.
34 define('BLOCK_MOVE_LEFT', 0x01);
35 define('BLOCK_MOVE_RIGHT', 0x02);
36 define('BLOCK_MOVE_UP', 0x04);
37 define('BLOCK_MOVE_DOWN', 0x08);
38 define('BLOCK_CONFIGURE', 0x10);
39 /**#@-*/
41 /**#@+
42 * Default names for the block regions in the standard theme.
44 define('BLOCK_POS_LEFT', 'side-pre');
45 define('BLOCK_POS_RIGHT', 'side-post');
46 /**#@-*/
48 /**#@+
49 * @deprecated since Moodle 2.0. No longer used.
51 define('BLOCKS_PINNED_TRUE',0);
52 define('BLOCKS_PINNED_FALSE',1);
53 define('BLOCKS_PINNED_BOTH',2);
54 /**#@-*/
56 define('BUI_CONTEXTS_FRONTPAGE_ONLY', 0);
57 define('BUI_CONTEXTS_FRONTPAGE_SUBS', 1);
58 define('BUI_CONTEXTS_ENTIRE_SITE', 2);
60 define('BUI_CONTEXTS_CURRENT', 0);
61 define('BUI_CONTEXTS_CURRENT_SUBS', 1);
63 /**
64 * Exception thrown when someone tried to do something with a block that does
65 * not exist on a page.
67 * @copyright 2009 Tim Hunt
68 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
69 * @since Moodle 2.0
71 class block_not_on_page_exception extends moodle_exception {
72 /**
73 * Constructor
74 * @param int $instanceid the block instance id of the block that was looked for.
75 * @param object $page the current page.
77 public function __construct($instanceid, $page) {
78 $a = new stdClass;
79 $a->instanceid = $instanceid;
80 $a->url = $page->url->out();
81 parent::__construct('blockdoesnotexistonpage', '', $page->url->out(), $a);
85 /**
86 * This class keeps track of the block that should appear on a moodle_page.
88 * The page to work with as passed to the constructor.
90 * @copyright 2009 Tim Hunt
91 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
92 * @since Moodle 2.0
94 class block_manager {
95 /**
96 * The UI normally only shows block weights between -MAX_WEIGHT and MAX_WEIGHT,
97 * although other weights are valid.
99 const MAX_WEIGHT = 10;
101 /// Field declarations =========================================================
104 * the moodle_page we are managing blocks for.
105 * @var moodle_page
107 protected $page;
109 /** @var array region name => 1.*/
110 protected $regions = array();
112 /** @var string the region where new blocks are added.*/
113 protected $defaultregion = null;
115 /** @var array will be $DB->get_records('blocks') */
116 protected $allblocks = null;
119 * @var array blocks that this user can add to this page. Will be a subset
120 * of $allblocks, but with array keys block->name. Access this via the
121 * {@link get_addable_blocks()} method to ensure it is lazy-loaded.
123 protected $addableblocks = null;
126 * Will be an array region-name => array(db rows loaded in load_blocks);
127 * @var array
129 protected $birecordsbyregion = null;
132 * array region-name => array(block objects); populated as necessary by
133 * the ensure_instances_exist method.
134 * @var array
136 protected $blockinstances = array();
139 * array region-name => array(block_contents objects) what actually needs to
140 * be displayed in each region.
141 * @var array
143 protected $visibleblockcontent = array();
146 * array region-name => array(block_contents objects) extra block-like things
147 * to be displayed in each region, before the real blocks.
148 * @var array
150 protected $extracontent = array();
153 * Used by the block move id, to track whether a block is currently being moved.
155 * When you click on the move icon of a block, first the page needs to reload with
156 * extra UI for choosing a new position for a particular block. In that situation
157 * this field holds the id of the block being moved.
159 * @var integer|null
161 protected $movingblock = null;
164 * Show only fake blocks
166 protected $fakeblocksonly = false;
168 /// Constructor ================================================================
171 * Constructor.
172 * @param object $page the moodle_page object object we are managing the blocks for,
173 * or a reasonable faxilimily. (See the comment at the top of this class
174 * and {@link http://en.wikipedia.org/wiki/Duck_typing})
176 public function __construct($page) {
177 $this->page = $page;
180 /// Getter methods =============================================================
183 * Get an array of all region names on this page where a block may appear
185 * @return array the internal names of the regions on this page where block may appear.
187 public function get_regions() {
188 if (is_null($this->defaultregion)) {
189 $this->page->initialise_theme_and_output();
191 return array_keys($this->regions);
195 * Get the region name of the region blocks are added to by default
197 * @return string the internal names of the region where new blocks are added
198 * by default, and where any blocks from an unrecognised region are shown.
199 * (Imagine that blocks were added with one theme selected, then you switched
200 * to a theme with different block positions.)
202 public function get_default_region() {
203 $this->page->initialise_theme_and_output();
204 return $this->defaultregion;
208 * The list of block types that may be added to this page.
210 * @return array block name => record from block table.
212 public function get_addable_blocks() {
213 $this->check_is_loaded();
215 if (!is_null($this->addableblocks)) {
216 return $this->addableblocks;
219 // Lazy load.
220 $this->addableblocks = array();
222 $allblocks = blocks_get_record();
223 if (empty($allblocks)) {
224 return $this->addableblocks;
227 $pageformat = $this->page->pagetype;
228 foreach($allblocks as $block) {
229 if (!$bi = block_instance($block->name)) {
230 continue;
232 if ($block->visible &&
233 ($bi->instance_allow_multiple() || !$this->is_block_present($block->name)) &&
234 blocks_name_allowed_in_format($block->name, $pageformat) &&
235 $bi->user_can_addto($this->page)) {
236 $this->addableblocks[$block->name] = $block;
240 return $this->addableblocks;
244 * Given a block name, find out of any of them are currently present in the page
246 * @param string $blockname - the basic name of a block (eg "navigation")
247 * @return boolean - is there one of these blocks in the current page?
249 public function is_block_present($blockname) {
250 if (empty($this->blockinstances)) {
251 return false;
254 foreach ($this->blockinstances as $region) {
255 foreach ($region as $instance) {
256 if (empty($instance->instance->blockname)) {
257 continue;
259 if ($instance->instance->blockname == $blockname) {
260 return true;
264 return false;
268 * Find out if a block type is known by the system
270 * @param string $blockname the name of the type of block.
271 * @param boolean $includeinvisible if false (default) only check 'visible' blocks, that is, blocks enabled by the admin.
272 * @return boolean true if this block in installed.
274 public function is_known_block_type($blockname, $includeinvisible = false) {
275 $blocks = $this->get_installed_blocks();
276 foreach ($blocks as $block) {
277 if ($block->name == $blockname && ($includeinvisible || $block->visible)) {
278 return true;
281 return false;
285 * Find out if a region exists on a page
287 * @param string $region a region name
288 * @return boolean true if this region exists on this page.
290 public function is_known_region($region) {
291 return array_key_exists($region, $this->regions);
295 * Get an array of all blocks within a given region
297 * @param string $region a block region that exists on this page.
298 * @return array of block instances.
300 public function get_blocks_for_region($region) {
301 $this->check_is_loaded();
302 $this->ensure_instances_exist($region);
303 return $this->blockinstances[$region];
307 * Returns an array of block content objects that exist in a region
309 * @param string $region a block region that exists on this page.
310 * @return array of block block_contents objects for all the blocks in a region.
312 public function get_content_for_region($region, $output) {
313 $this->check_is_loaded();
314 $this->ensure_content_created($region, $output);
315 return $this->visibleblockcontent[$region];
319 * Helper method used by get_content_for_region.
320 * @param string $region region name
321 * @param float $weight weight. May be fractional, since you may want to move a block
322 * between ones with weight 2 and 3, say ($weight would be 2.5).
323 * @return string URL for moving block $this->movingblock to this position.
325 protected function get_move_target_url($region, $weight) {
326 return new moodle_url($this->page->url, array('bui_moveid' => $this->movingblock,
327 'bui_newregion' => $region, 'bui_newweight' => $weight, 'sesskey' => sesskey()));
331 * Determine whether a region contains anything. (Either any real blocks, or
332 * the add new block UI.)
334 * (You may wonder why the $output parameter is required. Unfortunately,
335 * because of the way that blocks work, the only reliable way to find out
336 * if a block will be visible is to get the content for output, and to
337 * get the content, you need a renderer. Fortunately, this is not a
338 * performance problem, because we cache the output that is generated, and
339 * in almost every case where we call region_has_content, we are about to
340 * output the blocks anyway, so we are not doing wasted effort.)
342 * @param string $region a block region that exists on this page.
343 * @param object $output a core_renderer. normally the global $OUTPUT.
344 * @return boolean Whether there is anything in this region.
346 public function region_has_content($region, $output) {
348 if (!$this->is_known_region($region)) {
349 return false;
351 $this->check_is_loaded();
352 $this->ensure_content_created($region, $output);
353 // if ($this->page->user_is_editing() && $this->page->user_can_edit_blocks()) {
354 // Mark Nielsen's patch - part 1
355 if ($this->page->user_is_editing() && $this->page->user_can_edit_blocks() && $this->movingblock) {
356 // If editing is on, we need all the block regions visible, for the
357 // move blocks UI.
358 return true;
360 return !empty($this->visibleblockcontent[$region]) || !empty($this->extracontent[$region]);
364 * Get an array of all of the installed blocks.
366 * @return array contents of the block table.
368 public function get_installed_blocks() {
369 global $DB;
370 if (is_null($this->allblocks)) {
371 $this->allblocks = $DB->get_records('block');
373 return $this->allblocks;
376 /// Setter methods =============================================================
379 * Add a region to a page
381 * @param string $region add a named region where blocks may appear on the
382 * current page. This is an internal name, like 'side-pre', not a string to
383 * display in the UI.
385 public function add_region($region) {
386 $this->check_not_yet_loaded();
387 $this->regions[$region] = 1;
391 * Add an array of regions
392 * @see add_region()
394 * @param array $regions this utility method calls add_region for each array element.
396 public function add_regions($regions) {
397 foreach ($regions as $region) {
398 $this->add_region($region);
403 * Set the default region for new blocks on the page
405 * @param string $defaultregion the internal names of the region where new
406 * blocks should be added by default, and where any blocks from an
407 * unrecognised region are shown.
409 public function set_default_region($defaultregion) {
410 $this->check_not_yet_loaded();
411 if ($defaultregion) {
412 $this->check_region_is_known($defaultregion);
414 $this->defaultregion = $defaultregion;
418 * Add something that looks like a block, but which isn't an actual block_instance,
419 * to this page.
421 * @param block_contents $bc the content of the block-like thing.
422 * @param string $region a block region that exists on this page.
424 public function add_fake_block($bc, $region) {
425 $this->page->initialise_theme_and_output();
426 if (!$this->is_known_region($region)) {
427 $region = $this->get_default_region();
429 if (array_key_exists($region, $this->visibleblockcontent)) {
430 throw new coding_exception('block_manager has already prepared the blocks in region ' .
431 $region . 'for output. It is too late to add a fake block.');
433 $this->extracontent[$region][] = $bc;
437 * When the block_manager class was created, the {@link add_fake_block()}
438 * was called add_pretend_block, which is inconsisted with
439 * {@link show_only_fake_blocks()}. To fix this inconsistency, this method
440 * was renamed to add_fake_block. Please update your code.
441 * @param block_contents $bc the content of the block-like thing.
442 * @param string $region a block region that exists on this page.
444 public function add_pretend_block($bc, $region) {
445 debugging(DEBUG_DEVELOPER, 'add_pretend_block has been renamed to add_fake_block. Please rename the method call in your code.');
446 $this->add_fake_block($bc, $region);
450 * Checks to see whether all of the blocks within the given region are docked
452 * @see region_uses_dock
453 * @param string $region
454 * @return bool True if all of the blocks within that region are docked
456 public function region_completely_docked($region, $output) {
457 if (!$this->page->theme->enable_dock) {
458 return false;
461 // Do not dock the region when the user attemps to move a block.
462 if ($this->movingblock) {
463 return false;
466 $this->check_is_loaded();
467 $this->ensure_content_created($region, $output);
468 foreach($this->visibleblockcontent[$region] as $instance) {
469 if (!empty($instance->content) && !get_user_preferences('docked_block_instance_'.$instance->blockinstanceid, 0)) {
470 return false;
473 return true;
477 * Checks to see whether any of the blocks within the given regions are docked
479 * @see region_completely_docked
480 * @param array|string $regions array of regions (or single region)
481 * @return bool True if any of the blocks within that region are docked
483 public function region_uses_dock($regions, $output) {
484 if (!$this->page->theme->enable_dock) {
485 return false;
487 $this->check_is_loaded();
488 foreach((array)$regions as $region) {
489 $this->ensure_content_created($region, $output);
490 foreach($this->visibleblockcontent[$region] as $instance) {
491 if(!empty($instance->content) && get_user_preferences('docked_block_instance_'.$instance->blockinstanceid, 0)) {
492 return true;
496 return false;
499 /// Actions ====================================================================
502 * This method actually loads the blocks for our page from the database.
504 * @param boolean|null $includeinvisible
505 * null (default) - load hidden blocks if $this->page->user_is_editing();
506 * true - load hidden blocks.
507 * false - don't load hidden blocks.
509 public function load_blocks($includeinvisible = null) {
510 global $DB, $CFG;
512 if (!is_null($this->birecordsbyregion)) {
513 // Already done.
514 return;
517 if ($CFG->version < 2009050619) {
518 // Upgrade/install not complete. Don't try too show any blocks.
519 $this->birecordsbyregion = array();
520 return;
523 // Ensure we have been initialised.
524 if (is_null($this->defaultregion)) {
525 $this->page->initialise_theme_and_output();
526 // If there are still no block regions, then there are no blocks on this page.
527 if (empty($this->regions)) {
528 $this->birecordsbyregion = array();
529 return;
533 // Check if we need to load normal blocks
534 if ($this->fakeblocksonly) {
535 $this->birecordsbyregion = $this->prepare_per_region_arrays();
536 return;
539 if (is_null($includeinvisible)) {
540 $includeinvisible = $this->page->user_is_editing();
542 if ($includeinvisible) {
543 $visiblecheck = '';
544 } else {
545 $visiblecheck = 'AND (bp.visible = 1 OR bp.visible IS NULL)';
548 $context = $this->page->context;
549 $contexttest = 'bi.parentcontextid = :contextid2';
550 $parentcontextparams = array();
551 $parentcontextids = get_parent_contexts($context);
552 if ($parentcontextids) {
553 list($parentcontexttest, $parentcontextparams) =
554 $DB->get_in_or_equal($parentcontextids, SQL_PARAMS_NAMED, 'parentcontext');
555 $contexttest = "($contexttest OR (bi.showinsubcontexts = 1 AND bi.parentcontextid $parentcontexttest))";
558 $pagetypepatterns = matching_page_type_patterns($this->page->pagetype);
559 list($pagetypepatterntest, $pagetypepatternparams) =
560 $DB->get_in_or_equal($pagetypepatterns, SQL_PARAMS_NAMED, 'pagetypepatterntest');
562 list($ccselect, $ccjoin) = context_instance_preload_sql('bi.id', CONTEXT_BLOCK, 'ctx');
564 $params = array(
565 'subpage1' => $this->page->subpage,
566 'subpage2' => $this->page->subpage,
567 'contextid1' => $context->id,
568 'contextid2' => $context->id,
569 'pagetype' => $this->page->pagetype,
571 if ($this->page->subpage === '') {
572 $params['subpage1'] = $DB->sql_empty();
573 $params['subpage2'] = $DB->sql_empty();
575 $sql = "SELECT
576 bi.id,
577 bp.id AS blockpositionid,
578 bi.blockname,
579 bi.parentcontextid,
580 bi.showinsubcontexts,
581 bi.pagetypepattern,
582 bi.subpagepattern,
583 bi.defaultregion,
584 bi.defaultweight,
585 COALESCE(bp.visible, 1) AS visible,
586 COALESCE(bp.region, bi.defaultregion) AS region,
587 COALESCE(bp.weight, bi.defaultweight) AS weight,
588 bi.configdata
589 $ccselect
591 FROM {block_instances} bi
592 JOIN {block} b ON bi.blockname = b.name
593 LEFT JOIN {block_positions} bp ON bp.blockinstanceid = bi.id
594 AND bp.contextid = :contextid1
595 AND bp.pagetype = :pagetype
596 AND bp.subpage = :subpage1
597 $ccjoin
599 WHERE
600 $contexttest
601 AND bi.pagetypepattern $pagetypepatterntest
602 AND (bi.subpagepattern IS NULL OR bi.subpagepattern = :subpage2)
603 $visiblecheck
604 AND b.visible = 1
606 ORDER BY
607 COALESCE(bp.region, bi.defaultregion),
608 COALESCE(bp.weight, bi.defaultweight),
609 bi.id";
610 $blockinstances = $DB->get_recordset_sql($sql, $params + $parentcontextparams + $pagetypepatternparams);
612 $this->birecordsbyregion = $this->prepare_per_region_arrays();
613 $unknown = array();
614 foreach ($blockinstances as $bi) {
615 context_instance_preload($bi);
616 if ($this->is_known_region($bi->region)) {
617 $this->birecordsbyregion[$bi->region][] = $bi;
618 } else {
619 $unknown[] = $bi;
623 // Pages don't necessarily have a defaultregion. The one time this can
624 // happen is when there are no theme block regions, but the script itself
625 // has a block region in the main content area.
626 if (!empty($this->defaultregion)) {
627 $this->birecordsbyregion[$this->defaultregion] =
628 array_merge($this->birecordsbyregion[$this->defaultregion], $unknown);
633 * Add a block to the current page, or related pages. The block is added to
634 * context $this->page->contextid. If $pagetypepattern $subpagepattern
636 * @param string $blockname The type of block to add.
637 * @param string $region the block region on this page to add the block to.
638 * @param integer $weight determines the order where this block appears in the region.
639 * @param boolean $showinsubcontexts whether this block appears in subcontexts, or just the current context.
640 * @param string|null $pagetypepattern which page types this block should appear on. Defaults to just the current page type.
641 * @param string|null $subpagepattern which subpage this block should appear on. NULL = any (the default), otherwise only the specified subpage.
643 public function add_block($blockname, $region, $weight, $showinsubcontexts, $pagetypepattern = NULL, $subpagepattern = NULL) {
644 global $DB;
645 // Allow invisible blocks because this is used when adding default page blocks, which
646 // might include invisible ones if the user makes some default blocks invisible
647 $this->check_known_block_type($blockname, true);
648 $this->check_region_is_known($region);
650 if (empty($pagetypepattern)) {
651 $pagetypepattern = $this->page->pagetype;
654 $blockinstance = new stdClass;
655 $blockinstance->blockname = $blockname;
656 $blockinstance->parentcontextid = $this->page->context->id;
657 $blockinstance->showinsubcontexts = !empty($showinsubcontexts);
658 $blockinstance->pagetypepattern = $pagetypepattern;
659 $blockinstance->subpagepattern = $subpagepattern;
660 $blockinstance->defaultregion = $region;
661 $blockinstance->defaultweight = $weight;
662 $blockinstance->configdata = '';
663 $blockinstance->id = $DB->insert_record('block_instances', $blockinstance);
665 // Ensure the block context is created.
666 context_block::instance($blockinstance->id);
668 // If the new instance was created, allow it to do additional setup
669 if ($block = block_instance($blockname, $blockinstance)) {
670 $block->instance_create();
674 public function add_block_at_end_of_default_region($blockname) {
675 $defaulregion = $this->get_default_region();
677 $lastcurrentblock = end($this->birecordsbyregion[$defaulregion]);
678 if ($lastcurrentblock) {
679 $weight = $lastcurrentblock->weight + 1;
680 } else {
681 $weight = 0;
684 if ($this->page->subpage) {
685 $subpage = $this->page->subpage;
686 } else {
687 $subpage = null;
690 // Special case. Course view page type include the course format, but we
691 // want to add the block non-format-specifically.
692 $pagetypepattern = $this->page->pagetype;
693 if (strpos($pagetypepattern, 'course-view') === 0) {
694 $pagetypepattern = 'course-view-*';
697 // We should end using this for ALL the blocks, making always the 1st option
698 // the default one to be used. Until then, this is one hack to avoid the
699 // 'pagetypewarning' message on blocks initial edition (MDL-27829) caused by
700 // non-existing $pagetypepattern set. This way at least we guarantee one "valid"
701 // (the FIRST $pagetypepattern will be set)
703 // We are applying it to all blocks created in mod pages for now and only if the
704 // default pagetype is not one of the available options
705 if (preg_match('/^mod-.*-/', $pagetypepattern)) {
706 $pagetypelist = generate_page_type_patterns($this->page->pagetype, null, $this->page->context);
707 // Only go for the first if the pagetype is not a valid option
708 if (is_array($pagetypelist) && !array_key_exists($pagetypepattern, $pagetypelist)) {
709 $pagetypepattern = key($pagetypelist);
712 // Surely other pages like course-report will need this too, they just are not important
713 // enough now. This will be decided in the coming days. (MDL-27829, MDL-28150)
715 $this->add_block($blockname, $defaulregion, $weight, false, $pagetypepattern, $subpage);
719 * Convenience method, calls add_block repeatedly for all the blocks in $blocks.
721 * @param array $blocks array with array keys the region names, and values an array of block names.
722 * @param string $pagetypepattern optional. Passed to @see add_block()
723 * @param string $subpagepattern optional. Passed to @see add_block()
725 public function add_blocks($blocks, $pagetypepattern = NULL, $subpagepattern = NULL, $showinsubcontexts=false, $weight=0) {
726 $this->add_regions(array_keys($blocks));
727 foreach ($blocks as $region => $regionblocks) {
728 $weight = 0;
729 foreach ($regionblocks as $blockname) {
730 $this->add_block($blockname, $region, $weight, $showinsubcontexts, $pagetypepattern, $subpagepattern);
731 $weight += 1;
737 * Move a block to a new position on this page.
739 * If this block cannot appear on any other pages, then we change defaultposition/weight
740 * in the block_instances table. Otherwise we just set the position on this page.
742 * @param $blockinstanceid the block instance id.
743 * @param $newregion the new region name.
744 * @param $newweight the new weight.
746 public function reposition_block($blockinstanceid, $newregion, $newweight) {
747 global $DB;
749 $this->check_region_is_known($newregion);
750 $inst = $this->find_instance($blockinstanceid);
752 $bi = $inst->instance;
753 if ($bi->weight == $bi->defaultweight && $bi->region == $bi->defaultregion &&
754 !$bi->showinsubcontexts && strpos($bi->pagetypepattern, '*') === false &&
755 (!$this->page->subpage || $bi->subpagepattern)) {
757 // Set default position
758 $newbi = new stdClass;
759 $newbi->id = $bi->id;
760 $newbi->defaultregion = $newregion;
761 $newbi->defaultweight = $newweight;
762 $DB->update_record('block_instances', $newbi);
764 if ($bi->blockpositionid) {
765 $bp = new stdClass;
766 $bp->id = $bi->blockpositionid;
767 $bp->region = $newregion;
768 $bp->weight = $newweight;
769 $DB->update_record('block_positions', $bp);
772 } else {
773 // Just set position on this page.
774 $bp = new stdClass;
775 $bp->region = $newregion;
776 $bp->weight = $newweight;
778 if ($bi->blockpositionid) {
779 $bp->id = $bi->blockpositionid;
780 $DB->update_record('block_positions', $bp);
782 } else {
783 $bp->blockinstanceid = $bi->id;
784 $bp->contextid = $this->page->context->id;
785 $bp->pagetype = $this->page->pagetype;
786 if ($this->page->subpage) {
787 $bp->subpage = $this->page->subpage;
788 } else {
789 $bp->subpage = '';
791 $bp->visible = $bi->visible;
792 $DB->insert_record('block_positions', $bp);
798 * Find a given block by its instance id
800 * @param integer $instanceid
801 * @return object
803 public function find_instance($instanceid) {
804 foreach ($this->regions as $region => $notused) {
805 $this->ensure_instances_exist($region);
806 foreach($this->blockinstances[$region] as $instance) {
807 if ($instance->instance->id == $instanceid) {
808 return $instance;
812 throw new block_not_on_page_exception($instanceid, $this->page);
815 /// Inner workings =============================================================
818 * Check whether the page blocks have been loaded yet
820 * @return void Throws coding exception if already loaded
822 protected function check_not_yet_loaded() {
823 if (!is_null($this->birecordsbyregion)) {
824 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.');
829 * Check whether the page blocks have been loaded yet
831 * Nearly identical to the above function {@link check_not_yet_loaded()} except different message
833 * @return void Throws coding exception if already loaded
835 protected function check_is_loaded() {
836 if (is_null($this->birecordsbyregion)) {
837 throw new coding_exception('block_manager has not yet loaded the blocks, to it is too soon to request the information you asked for.');
842 * Check if a block type is known and usable
844 * @param string $blockname The block type name to search for
845 * @param bool $includeinvisible Include disabled block types in the initial pass
846 * @return void Coding Exception thrown if unknown or not enabled
848 protected function check_known_block_type($blockname, $includeinvisible = false) {
849 if (!$this->is_known_block_type($blockname, $includeinvisible)) {
850 if ($this->is_known_block_type($blockname, true)) {
851 throw new coding_exception('Unknown block type ' . $blockname);
852 } else {
853 throw new coding_exception('Block type ' . $blockname . ' has been disabled by the administrator.');
859 * Check if a region is known by its name
861 * @param string $region
862 * @return void Coding Exception thrown if the region is not known
864 protected function check_region_is_known($region) {
865 if (!$this->is_known_region($region)) {
866 throw new coding_exception('Trying to reference an unknown block region ' . $region);
871 * Returns an array of region names as keys and nested arrays for values
873 * @return array an array where the array keys are the region names, and the array
874 * values are empty arrays.
876 protected function prepare_per_region_arrays() {
877 $result = array();
878 foreach ($this->regions as $region => $notused) {
879 $result[$region] = array();
881 return $result;
885 * Create a set of new block instance from a record array
887 * @param array $birecords An array of block instance records
888 * @return array An array of instantiated block_instance objects
890 protected function create_block_instances($birecords) {
891 $results = array();
892 foreach ($birecords as $record) {
893 if ($blockobject = block_instance($record->blockname, $record, $this->page)) {
894 $results[] = $blockobject;
897 return $results;
901 * Create all the block instances for all the blocks that were loaded by
902 * load_blocks. This is used, for example, to ensure that all blocks get a
903 * chance to initialise themselves via the {@link block_base::specialize()}
904 * method, before any output is done.
906 public function create_all_block_instances() {
907 foreach ($this->get_regions() as $region) {
908 $this->ensure_instances_exist($region);
913 * Return an array of content objects from a set of block instances
915 * @param array $instances An array of block instances
916 * @param renderer_base The renderer to use.
917 * @param string $region the region name.
918 * @return array An array of block_content (and possibly block_move_target) objects.
920 protected function create_block_contents($instances, $output, $region) {
921 $results = array();
923 $lastweight = 0;
924 $lastblock = 0;
925 if ($this->movingblock) {
926 $first = reset($instances);
927 if ($first) {
928 $lastweight = $first->instance->weight - 2;
931 $strmoveblockhere = get_string('moveblockhere', 'block');
934 foreach ($instances as $instance) {
935 $content = $instance->get_content_for_output($output);
936 if (empty($content)) {
937 continue;
940 if ($this->movingblock && $lastweight != $instance->instance->weight &&
941 $content->blockinstanceid != $this->movingblock && $lastblock != $this->movingblock) {
942 $results[] = new block_move_target($strmoveblockhere, $this->get_move_target_url($region, ($lastweight + $instance->instance->weight)/2));
945 if ($content->blockinstanceid == $this->movingblock) {
946 $content->add_class('beingmoved');
947 $content->annotation .= get_string('movingthisblockcancel', 'block',
948 html_writer::link($this->page->url, get_string('cancel')));
951 $results[] = $content;
952 $lastweight = $instance->instance->weight;
953 $lastblock = $instance->instance->id;
956 if ($this->movingblock && $lastblock != $this->movingblock) {
957 $results[] = new block_move_target($strmoveblockhere, $this->get_move_target_url($region, $lastweight + 1));
959 return $results;
963 * Ensure block instances exist for a given region
965 * @param string $region Check for bi's with the instance with this name
967 protected function ensure_instances_exist($region) {
968 $this->check_region_is_known($region);
969 if (!array_key_exists($region, $this->blockinstances)) {
970 $this->blockinstances[$region] =
971 $this->create_block_instances($this->birecordsbyregion[$region]);
976 * Ensure that there is some content within the given region
978 * @param string $region The name of the region to check
980 protected function ensure_content_created($region, $output) {
981 $this->ensure_instances_exist($region);
982 if (!array_key_exists($region, $this->visibleblockcontent)) {
983 $contents = array();
984 if (array_key_exists($region, $this->extracontent)) {
985 $contents = $this->extracontent[$region];
987 $contents = array_merge($contents, $this->create_block_contents($this->blockinstances[$region], $output, $region));
988 if ($region == $this->defaultregion) {
989 $addblockui = block_add_block_ui($this->page, $output);
990 if ($addblockui) {
991 $contents[] = $addblockui;
994 $this->visibleblockcontent[$region] = $contents;
998 /// Process actions from the URL ===============================================
1001 * Get the appropriate list of editing icons for a block. This is used
1002 * to set {@link block_contents::$controls} in {@link block_base::get_contents_for_output()}.
1004 * @param $output The core_renderer to use when generating the output. (Need to get icon paths.)
1005 * @return an array in the format for {@link block_contents::$controls}
1007 public function edit_controls($block) {
1008 global $CFG;
1010 if (!isset($CFG->undeletableblocktypes) || (!is_array($CFG->undeletableblocktypes) && !is_string($CFG->undeletableblocktypes))) {
1011 $undeletableblocktypes = array('navigation','settings');
1012 } else if (is_string($CFG->undeletableblocktypes)) {
1013 $undeletableblocktypes = explode(',', $CFG->undeletableblocktypes);
1014 } else {
1015 $undeletableblocktypes = $CFG->undeletableblocktypes;
1018 $controls = array();
1019 $actionurl = $this->page->url->out(false, array('sesskey'=> sesskey()));
1021 if ($this->page->user_can_edit_blocks()) {
1022 // Move icon.
1023 $controls[] = array('url' => $actionurl . '&bui_moveid=' . $block->instance->id,
1024 'icon' => 't/move', 'caption' => get_string('move'), 'class' => 'editing_move');
1027 if ($this->page->user_can_edit_blocks() || $block->user_can_edit()) {
1028 // Edit config icon - always show - needed for positioning UI.
1029 $controls[] = array('url' => $actionurl . '&bui_editid=' . $block->instance->id,
1030 'icon' => 't/edit', 'caption' => get_string('configuration'), 'class' => 'editing_edit');
1033 if ($this->page->user_can_edit_blocks() && $block->user_can_edit() && $block->user_can_addto($this->page)) {
1034 if (!in_array($block->instance->blockname, $undeletableblocktypes)
1035 || !in_array($block->instance->pagetypepattern, array('*', 'site-index'))
1036 || $block->instance->parentcontextid != SITEID) {
1037 // Delete icon.
1038 $controls[] = array('url' => $actionurl . '&bui_deleteid=' . $block->instance->id,
1039 'icon' => 't/delete', 'caption' => get_string('delete'), 'class' => 'editing_delete');
1043 if ($this->page->user_can_edit_blocks() && $block->instance_can_be_hidden()) {
1044 // Show/hide icon.
1045 if ($block->instance->visible) {
1046 $controls[] = array('url' => $actionurl . '&bui_hideid=' . $block->instance->id,
1047 'icon' => 't/hide', 'caption' => get_string('hide'), 'class' => 'editing_hide');
1048 } else {
1049 $controls[] = array('url' => $actionurl . '&bui_showid=' . $block->instance->id,
1050 'icon' => 't/show', 'caption' => get_string('show'), 'class' => 'editing_show');
1054 // Assign roles icon.
1055 if (has_capability('moodle/role:assign', $block->context)) {
1056 //TODO: please note it is sloppy to pass urls through page parameters!!
1057 // it is shortened because some web servers (e.g. IIS by default) give
1058 // a 'security' error if you try to pass a full URL as a GET parameter in another URL.
1059 $return = $this->page->url->out(false);
1060 $return = str_replace($CFG->wwwroot . '/', '', $return);
1062 $controls[] = array('url' => $CFG->wwwroot . '/' . $CFG->admin .
1063 '/roles/assign.php?contextid=' . $block->context->id . '&returnurl=' . urlencode($return),
1064 'icon' => 'i/roles', 'caption' => get_string('assignroles', 'role'), 'class' => 'editing_roles');
1067 return $controls;
1071 * Process any block actions that were specified in the URL.
1073 * @return boolean true if anything was done. False if not.
1075 public function process_url_actions() {
1076 if (!$this->page->user_is_editing()) {
1077 return false;
1079 return $this->process_url_add() || $this->process_url_delete() ||
1080 $this->process_url_show_hide() || $this->process_url_edit() ||
1081 $this->process_url_move();
1085 * Handle adding a block.
1086 * @return boolean true if anything was done. False if not.
1088 public function process_url_add() {
1089 $blocktype = optional_param('bui_addblock', null, PARAM_PLUGIN);
1090 if (!$blocktype) {
1091 return false;
1094 require_sesskey();
1096 if (!$this->page->user_can_edit_blocks()) {
1097 throw new moodle_exception('nopermissions', '', $this->page->url->out(), get_string('addblock'));
1100 if (!array_key_exists($blocktype, $this->get_addable_blocks())) {
1101 throw new moodle_exception('cannotaddthisblocktype', '', $this->page->url->out(), $blocktype);
1104 $this->add_block_at_end_of_default_region($blocktype);
1106 // If the page URL was a guess, it will contain the bui_... param, so we must make sure it is not there.
1107 $this->page->ensure_param_not_in_url('bui_addblock');
1109 return true;
1113 * Handle deleting a block.
1114 * @return boolean true if anything was done. False if not.
1116 public function process_url_delete() {
1117 global $CFG, $PAGE, $OUTPUT;
1119 $blockid = optional_param('bui_deleteid', null, PARAM_INT);
1120 $confirmdelete = optional_param('bui_confirm', null, PARAM_INT);
1122 if (!$blockid) {
1123 return false;
1126 require_sesskey();
1127 $block = $this->page->blocks->find_instance($blockid);
1128 if (!$block->user_can_edit() || !$this->page->user_can_edit_blocks() || !$block->user_can_addto($this->page)) {
1129 throw new moodle_exception('nopermissions', '', $this->page->url->out(), get_string('deleteablock'));
1132 if (!$confirmdelete) {
1133 $deletepage = new moodle_page();
1134 $deletepage->set_pagelayout('admin');
1135 $deletepage->set_course($this->page->course);
1136 $deletepage->set_context($this->page->context);
1137 if ($this->page->cm) {
1138 $deletepage->set_cm($this->page->cm);
1141 $deleteurlbase = str_replace($CFG->wwwroot . '/', '/', $this->page->url->out_omit_querystring());
1142 $deleteurlparams = $this->page->url->params();
1143 $deletepage->set_url($deleteurlbase, $deleteurlparams);
1144 $deletepage->set_block_actions_done();
1145 // At this point we are either going to redirect, or display the form, so
1146 // overwrite global $PAGE ready for this. (Formslib refers to it.)
1147 $PAGE = $deletepage;
1148 //some functions like MoodleQuickForm::addHelpButton use $OUTPUT so we need to replace that too
1149 $output = $deletepage->get_renderer('core');
1150 $OUTPUT = $output;
1152 $site = get_site();
1153 $blocktitle = $block->get_title();
1154 $strdeletecheck = get_string('deletecheck', 'block', $blocktitle);
1155 $message = get_string('deleteblockcheck', 'block', $blocktitle);
1157 $PAGE->navbar->add($strdeletecheck);
1158 $PAGE->set_title($blocktitle . ': ' . $strdeletecheck);
1159 $PAGE->set_heading($site->fullname);
1160 echo $OUTPUT->header();
1161 $confirmurl = new moodle_url($deletepage->url, array('sesskey' => sesskey(), 'bui_deleteid' => $block->instance->id, 'bui_confirm' => 1));
1162 $cancelurl = new moodle_url($deletepage->url);
1163 $yesbutton = new single_button($confirmurl, get_string('yes'));
1164 $nobutton = new single_button($cancelurl, get_string('no'));
1165 echo $OUTPUT->confirm($message, $yesbutton, $nobutton);
1166 echo $OUTPUT->footer();
1167 // Make sure that nothing else happens after we have displayed this form.
1168 exit;
1169 } else {
1170 blocks_delete_instance($block->instance);
1171 // bui_deleteid and bui_confirm should not be in the PAGE url.
1172 $this->page->ensure_param_not_in_url('bui_deleteid');
1173 $this->page->ensure_param_not_in_url('bui_confirm');
1174 return true;
1179 * Handle showing or hiding a block.
1180 * @return boolean true if anything was done. False if not.
1182 public function process_url_show_hide() {
1183 if ($blockid = optional_param('bui_hideid', null, PARAM_INT)) {
1184 $newvisibility = 0;
1185 } else if ($blockid = optional_param('bui_showid', null, PARAM_INT)) {
1186 $newvisibility = 1;
1187 } else {
1188 return false;
1191 require_sesskey();
1193 $block = $this->page->blocks->find_instance($blockid);
1195 if (!$this->page->user_can_edit_blocks()) {
1196 throw new moodle_exception('nopermissions', '', $this->page->url->out(), get_string('hideshowblocks'));
1197 } else if (!$block->instance_can_be_hidden()) {
1198 return false;
1201 blocks_set_visibility($block->instance, $this->page, $newvisibility);
1203 // If the page URL was a guses, it will contain the bui_... param, so we must make sure it is not there.
1204 $this->page->ensure_param_not_in_url('bui_hideid');
1205 $this->page->ensure_param_not_in_url('bui_showid');
1207 return true;
1211 * Handle showing/processing the submission from the block editing form.
1212 * @return boolean true if the form was submitted and the new config saved. Does not
1213 * return if the editing form was displayed. False otherwise.
1215 public function process_url_edit() {
1216 global $CFG, $DB, $PAGE, $OUTPUT;
1218 $blockid = optional_param('bui_editid', null, PARAM_INT);
1219 if (!$blockid) {
1220 return false;
1223 require_sesskey();
1224 require_once($CFG->dirroot . '/blocks/edit_form.php');
1226 $block = $this->find_instance($blockid);
1228 if (!$block->user_can_edit() && !$this->page->user_can_edit_blocks()) {
1229 throw new moodle_exception('nopermissions', '', $this->page->url->out(), get_string('editblock'));
1232 $editpage = new moodle_page();
1233 $editpage->set_pagelayout('admin');
1234 $editpage->set_course($this->page->course);
1235 //$editpage->set_context($block->context);
1236 $editpage->set_context($this->page->context);
1237 if ($this->page->cm) {
1238 $editpage->set_cm($this->page->cm);
1240 $editurlbase = str_replace($CFG->wwwroot . '/', '/', $this->page->url->out_omit_querystring());
1241 $editurlparams = $this->page->url->params();
1242 $editurlparams['bui_editid'] = $blockid;
1243 $editpage->set_url($editurlbase, $editurlparams);
1244 $editpage->set_block_actions_done();
1245 // At this point we are either going to redirect, or display the form, so
1246 // overwrite global $PAGE ready for this. (Formslib refers to it.)
1247 $PAGE = $editpage;
1248 //some functions like MoodleQuickForm::addHelpButton use $OUTPUT so we need to replace that to
1249 $output = $editpage->get_renderer('core');
1250 $OUTPUT = $output;
1252 $formfile = $CFG->dirroot . '/blocks/' . $block->name() . '/edit_form.php';
1253 if (is_readable($formfile)) {
1254 require_once($formfile);
1255 $classname = 'block_' . $block->name() . '_edit_form';
1256 if (!class_exists($classname)) {
1257 $classname = 'block_edit_form';
1259 } else {
1260 $classname = 'block_edit_form';
1263 $mform = new $classname($editpage->url, $block, $this->page);
1264 $mform->set_data($block->instance);
1266 if ($mform->is_cancelled()) {
1267 redirect($this->page->url);
1269 } else if ($data = $mform->get_data()) {
1270 $bi = new stdClass;
1271 $bi->id = $block->instance->id;
1272 $bi->pagetypepattern = $data->bui_pagetypepattern;
1273 if (empty($data->bui_subpagepattern) || $data->bui_subpagepattern == '%@NULL@%') {
1274 $bi->subpagepattern = null;
1275 } else {
1276 $bi->subpagepattern = $data->bui_subpagepattern;
1279 $systemcontext = context_system::instance();
1280 $frontpagecontext = context_course::instance(SITEID);
1281 $parentcontext = context::instance_by_id($data->bui_parentcontextid);
1283 // Updating stickiness and contexts. See MDL-21375 for details.
1284 if (has_capability('moodle/site:manageblocks', $parentcontext)) { // Check permissions in destination
1286 // Explicitly set the default context
1287 $bi->parentcontextid = $parentcontext->id;
1289 if ($data->bui_editingatfrontpage) { // The block is being edited on the front page
1291 // The interface here is a special case because the pagetype pattern is
1292 // totally derived from the context menu. Here are the excpetions. MDL-30340
1294 switch ($data->bui_contexts) {
1295 case BUI_CONTEXTS_ENTIRE_SITE:
1296 // The user wants to show the block across the entire site
1297 $bi->parentcontextid = $systemcontext->id;
1298 $bi->showinsubcontexts = true;
1299 $bi->pagetypepattern = '*';
1300 break;
1301 case BUI_CONTEXTS_FRONTPAGE_SUBS:
1302 // The user wants the block shown on the front page and all subcontexts
1303 $bi->parentcontextid = $frontpagecontext->id;
1304 $bi->showinsubcontexts = true;
1305 $bi->pagetypepattern = '*';
1306 break;
1307 case BUI_CONTEXTS_FRONTPAGE_ONLY:
1308 // The user want to show the front page on the frontpage only
1309 $bi->parentcontextid = $frontpagecontext->id;
1310 $bi->showinsubcontexts = false;
1311 $bi->pagetypepattern = 'site-index';
1312 // This is the only relevant page type anyway but we'll set it explicitly just
1313 // in case the front page grows site-index-* subpages of its own later
1314 break;
1319 $bits = explode('-', $bi->pagetypepattern);
1320 // hacks for some contexts
1321 if (($parentcontext->contextlevel == CONTEXT_COURSE) && ($parentcontext->instanceid != SITEID)) {
1322 // For course context
1323 // is page type pattern is mod-*, change showinsubcontext to 1
1324 if ($bits[0] == 'mod' || $bi->pagetypepattern == '*') {
1325 $bi->showinsubcontexts = 1;
1326 } else {
1327 $bi->showinsubcontexts = 0;
1329 } else if ($parentcontext->contextlevel == CONTEXT_USER) {
1330 // for user context
1331 // subpagepattern should be null
1332 if ($bits[0] == 'user' or $bits[0] == 'my') {
1333 // we don't need subpagepattern in usercontext
1334 $bi->subpagepattern = null;
1338 $bi->defaultregion = $data->bui_defaultregion;
1339 $bi->defaultweight = $data->bui_defaultweight;
1340 $DB->update_record('block_instances', $bi);
1342 if (!empty($block->config)) {
1343 $config = clone($block->config);
1344 } else {
1345 $config = new stdClass;
1347 foreach ($data as $configfield => $value) {
1348 if (strpos($configfield, 'config_') !== 0) {
1349 continue;
1351 $field = substr($configfield, 7);
1352 $config->$field = $value;
1354 $block->instance_config_save($config);
1356 $bp = new stdClass;
1357 $bp->visible = $data->bui_visible;
1358 $bp->region = $data->bui_region;
1359 $bp->weight = $data->bui_weight;
1360 $needbprecord = !$data->bui_visible || $data->bui_region != $data->bui_defaultregion ||
1361 $data->bui_weight != $data->bui_defaultweight;
1363 if ($block->instance->blockpositionid && !$needbprecord) {
1364 $DB->delete_records('block_positions', array('id' => $block->instance->blockpositionid));
1366 } else if ($block->instance->blockpositionid && $needbprecord) {
1367 $bp->id = $block->instance->blockpositionid;
1368 $DB->update_record('block_positions', $bp);
1370 } else if ($needbprecord) {
1371 $bp->blockinstanceid = $block->instance->id;
1372 $bp->contextid = $this->page->context->id;
1373 $bp->pagetype = $this->page->pagetype;
1374 if ($this->page->subpage) {
1375 $bp->subpage = $this->page->subpage;
1376 } else {
1377 $bp->subpage = '';
1379 $DB->insert_record('block_positions', $bp);
1382 redirect($this->page->url);
1384 } else {
1385 $strheading = get_string('blockconfiga', 'moodle', $block->get_title());
1386 $editpage->set_title($strheading);
1387 $editpage->set_heading($strheading);
1388 $bits = explode('-', $this->page->pagetype);
1389 if ($bits[0] == 'tag' && !empty($this->page->subpage)) {
1390 // better navbar for tag pages
1391 $editpage->navbar->add(get_string('tags'), new moodle_url('/tag/'));
1392 $tag = tag_get('id', $this->page->subpage, '*');
1393 // tag search page doesn't have subpageid
1394 if ($tag) {
1395 $editpage->navbar->add($tag->name, new moodle_url('/tag/index.php', array('id'=>$tag->id)));
1398 $editpage->navbar->add($block->get_title());
1399 $editpage->navbar->add(get_string('configuration'));
1400 echo $output->header();
1401 echo $output->heading($strheading, 2);
1402 $mform->display();
1403 echo $output->footer();
1404 exit;
1409 * Handle showing/processing the submission from the block editing form.
1410 * @return boolean true if the form was submitted and the new config saved. Does not
1411 * return if the editing form was displayed. False otherwise.
1413 public function process_url_move() {
1414 global $CFG, $DB, $PAGE;
1416 $blockid = optional_param('bui_moveid', null, PARAM_INT);
1417 if (!$blockid) {
1418 return false;
1421 require_sesskey();
1423 $block = $this->find_instance($blockid);
1425 if (!$this->page->user_can_edit_blocks()) {
1426 throw new moodle_exception('nopermissions', '', $this->page->url->out(), get_string('editblock'));
1429 $newregion = optional_param('bui_newregion', '', PARAM_ALPHANUMEXT);
1430 $newweight = optional_param('bui_newweight', null, PARAM_FLOAT);
1431 if (!$newregion || is_null($newweight)) {
1432 // Don't have a valid target position yet, must be just starting the move.
1433 $this->movingblock = $blockid;
1434 $this->page->ensure_param_not_in_url('bui_moveid');
1435 return false;
1438 if (!$this->is_known_region($newregion)) {
1439 throw new moodle_exception('unknownblockregion', '', $this->page->url, $newregion);
1442 // Move this block. This may involve moving other nearby blocks.
1443 $blocks = $this->birecordsbyregion[$newregion];
1445 $maxweight = self::MAX_WEIGHT;
1446 $minweight = -self::MAX_WEIGHT;
1448 // Initialise the used weights and spareweights array with the default values
1449 $spareweights = array();
1450 $usedweights = array();
1451 for ($i = $minweight; $i <= $maxweight; $i++) {
1452 $spareweights[$i] = $i;
1453 $usedweights[$i] = array();
1456 // Check each block and sort out where we have used weights
1457 foreach ($blocks as $bi) {
1458 if ($bi->weight > $maxweight) {
1459 // If this statement is true then the blocks weight is more than the
1460 // current maximum. To ensure that we can get the best block position
1461 // we will initialise elements within the usedweights and spareweights
1462 // arrays between the blocks weight (which will then be the new max) and
1463 // the current max
1464 $parseweight = $bi->weight;
1465 while (!array_key_exists($parseweight, $usedweights)) {
1466 $usedweights[$parseweight] = array();
1467 $spareweights[$parseweight] = $parseweight;
1468 $parseweight--;
1470 $maxweight = $bi->weight;
1471 } else if ($bi->weight < $minweight) {
1472 // As above except this time the blocks weight is LESS than the
1473 // the current minimum, so we will initialise the array from the
1474 // blocks weight (new minimum) to the current minimum
1475 $parseweight = $bi->weight;
1476 while (!array_key_exists($parseweight, $usedweights)) {
1477 $usedweights[$parseweight] = array();
1478 $spareweights[$parseweight] = $parseweight;
1479 $parseweight++;
1481 $minweight = $bi->weight;
1483 if ($bi->id != $block->instance->id) {
1484 unset($spareweights[$bi->weight]);
1485 $usedweights[$bi->weight][] = $bi->id;
1489 // First we find the nearest gap in the list of weights.
1490 $bestdistance = max(abs($newweight - self::MAX_WEIGHT), abs($newweight + self::MAX_WEIGHT)) + 1;
1491 $bestgap = null;
1492 foreach ($spareweights as $spareweight) {
1493 if (abs($newweight - $spareweight) < $bestdistance) {
1494 $bestdistance = abs($newweight - $spareweight);
1495 $bestgap = $spareweight;
1499 // If there is no gap, we have to go outside -self::MAX_WEIGHT .. self::MAX_WEIGHT.
1500 if (is_null($bestgap)) {
1501 $bestgap = self::MAX_WEIGHT + 1;
1502 while (!empty($usedweights[$bestgap])) {
1503 $bestgap++;
1507 // Now we know the gap we are aiming for, so move all the blocks along.
1508 if ($bestgap < $newweight) {
1509 $newweight = floor($newweight);
1510 for ($weight = $bestgap + 1; $weight <= $newweight; $weight++) {
1511 foreach ($usedweights[$weight] as $biid) {
1512 $this->reposition_block($biid, $newregion, $weight - 1);
1515 $this->reposition_block($block->instance->id, $newregion, $newweight);
1516 } else {
1517 $newweight = ceil($newweight);
1518 for ($weight = $bestgap - 1; $weight >= $newweight; $weight--) {
1519 if (array_key_exists($weight, $usedweights)) {
1520 foreach ($usedweights[$weight] as $biid) {
1521 $this->reposition_block($biid, $newregion, $weight + 1);
1525 $this->reposition_block($block->instance->id, $newregion, $newweight);
1528 $this->page->ensure_param_not_in_url('bui_moveid');
1529 $this->page->ensure_param_not_in_url('bui_newregion');
1530 $this->page->ensure_param_not_in_url('bui_newweight');
1531 return true;
1535 * Turns the display of normal blocks either on or off.
1537 * @param bool $setting
1539 public function show_only_fake_blocks($setting = true) {
1540 $this->fakeblocksonly = $setting;
1544 /// Helper functions for working with block classes ============================
1547 * Call a class method (one that does not require a block instance) on a block class.
1549 * @param string $blockname the name of the block.
1550 * @param string $method the method name.
1551 * @param array $param parameters to pass to the method.
1552 * @return mixed whatever the method returns.
1554 function block_method_result($blockname, $method, $param = NULL) {
1555 if(!block_load_class($blockname)) {
1556 return NULL;
1558 return call_user_func(array('block_'.$blockname, $method), $param);
1562 * Creates a new instance of the specified block class.
1564 * @param string $blockname the name of the block.
1565 * @param $instance block_instances DB table row (optional).
1566 * @param moodle_page $page the page this block is appearing on.
1567 * @return block_base the requested block instance.
1569 function block_instance($blockname, $instance = NULL, $page = NULL) {
1570 if(!block_load_class($blockname)) {
1571 return false;
1573 $classname = 'block_'.$blockname;
1574 $retval = new $classname;
1575 if($instance !== NULL) {
1576 if (is_null($page)) {
1577 global $PAGE;
1578 $page = $PAGE;
1580 $retval->_load_instance($instance, $page);
1582 return $retval;
1586 * Load the block class for a particular type of block.
1588 * @param string $blockname the name of the block.
1589 * @return boolean success or failure.
1591 function block_load_class($blockname) {
1592 global $CFG;
1594 if(empty($blockname)) {
1595 return false;
1598 $classname = 'block_'.$blockname;
1600 if(class_exists($classname)) {
1601 return true;
1604 $blockpath = $CFG->dirroot.'/blocks/'.$blockname.'/block_'.$blockname.'.php';
1606 if (file_exists($blockpath)) {
1607 require_once($CFG->dirroot.'/blocks/moodleblock.class.php');
1608 include_once($blockpath);
1609 }else{
1610 //debugging("$blockname code does not exist in $blockpath", DEBUG_DEVELOPER);
1611 return false;
1614 return class_exists($classname);
1618 * Given a specific page type, return all the page type patterns that might
1619 * match it.
1621 * @param string $pagetype for example 'course-view-weeks' or 'mod-quiz-view'.
1622 * @return array an array of all the page type patterns that might match this page type.
1624 function matching_page_type_patterns($pagetype) {
1625 $patterns = array($pagetype);
1626 $bits = explode('-', $pagetype);
1627 if (count($bits) == 3 && $bits[0] == 'mod') {
1628 if ($bits[2] == 'view') {
1629 $patterns[] = 'mod-*-view';
1630 } else if ($bits[2] == 'index') {
1631 $patterns[] = 'mod-*-index';
1634 while (count($bits) > 0) {
1635 $patterns[] = implode('-', $bits) . '-*';
1636 array_pop($bits);
1638 $patterns[] = '*';
1639 return $patterns;
1643 * Given a specific page type, parent context and currect context, return all the page type patterns
1644 * that might be used by this block.
1646 * @param string $pagetype for example 'course-view-weeks' or 'mod-quiz-view'.
1647 * @param stdClass $parentcontext Block's parent context
1648 * @param stdClass $currentcontext Current context of block
1649 * @return array an array of all the page type patterns that might match this page type.
1651 function generate_page_type_patterns($pagetype, $parentcontext = null, $currentcontext = null) {
1652 global $CFG;
1654 $bits = explode('-', $pagetype);
1656 $core = get_core_subsystems();
1657 $plugins = get_plugin_types();
1659 //progressively strip pieces off the page type looking for a match
1660 $componentarray = null;
1661 for ($i = count($bits); $i > 0; $i--) {
1662 $possiblecomponentarray = array_slice($bits, 0, $i);
1663 $possiblecomponent = implode('', $possiblecomponentarray);
1665 // Check to see if the component is a core component
1666 if (array_key_exists($possiblecomponent, $core) && !empty($core[$possiblecomponent])) {
1667 $libfile = $CFG->dirroot.'/'.$core[$possiblecomponent].'/lib.php';
1668 if (file_exists($libfile)) {
1669 require_once($libfile);
1670 $function = $possiblecomponent.'_page_type_list';
1671 if (function_exists($function)) {
1672 if ($patterns = $function($pagetype, $parentcontext, $currentcontext)) {
1673 break;
1679 //check the plugin directory and look for a callback
1680 if (array_key_exists($possiblecomponent, $plugins) && !empty($plugins[$possiblecomponent])) {
1682 //We've found a plugin type. Look for a plugin name by getting the next section of page type
1683 if (count($bits) > $i) {
1684 $pluginname = $bits[$i];
1685 $directory = get_plugin_directory($possiblecomponent, $pluginname);
1686 if (!empty($directory)){
1687 $libfile = $directory.'/lib.php';
1688 if (file_exists($libfile)) {
1689 require_once($libfile);
1690 $function = $possiblecomponent.'_'.$pluginname.'_page_type_list';
1691 if (!function_exists($function)) {
1692 $function = $pluginname.'_page_type_list';
1694 if (function_exists($function)) {
1695 if ($patterns = $function($pagetype, $parentcontext, $currentcontext)) {
1696 break;
1703 //we'll only get to here if we still don't have any patterns
1704 //the plugin type may have a callback
1705 $directory = get_plugin_directory($possiblecomponent, null);
1706 if (!empty($directory)){
1707 $libfile = $directory.'/lib.php';
1708 if (file_exists($libfile)) {
1709 require_once($libfile);
1710 $function = $possiblecomponent.'_page_type_list';
1711 if (function_exists($function)) {
1712 if ($patterns = $function($pagetype, $parentcontext, $currentcontext)) {
1713 break;
1721 if (empty($patterns)) {
1722 $patterns = default_page_type_list($pagetype, $parentcontext, $currentcontext);
1725 // Ensure that the * pattern is always available if editing block 'at distance', so
1726 // we always can 'bring back' it to the original context. MDL-30340
1727 if ((!isset($currentcontext) or !isset($parentcontext) or $currentcontext->id != $parentcontext->id) && !isset($patterns['*'])) {
1728 // TODO: We could change the string here, showing its 'bring back' meaning
1729 $patterns['*'] = get_string('page-x', 'pagetype');
1732 return $patterns;
1736 * Generates a default page type list when a more appropriate callback cannot be decided upon.
1738 * @param string $pagetype
1739 * @param stdClass $parentcontext
1740 * @param stdClass $currentcontext
1741 * @return array
1743 function default_page_type_list($pagetype, $parentcontext = null, $currentcontext = null) {
1744 // Generate page type patterns based on current page type if
1745 // callbacks haven't been defined
1746 $patterns = array($pagetype => $pagetype);
1747 $bits = explode('-', $pagetype);
1748 while (count($bits) > 0) {
1749 $pattern = implode('-', $bits) . '-*';
1750 $pagetypestringname = 'page-'.str_replace('*', 'x', $pattern);
1751 // guessing page type description
1752 if (get_string_manager()->string_exists($pagetypestringname, 'pagetype')) {
1753 $patterns[$pattern] = get_string($pagetypestringname, 'pagetype');
1754 } else {
1755 $patterns[$pattern] = $pattern;
1757 array_pop($bits);
1759 $patterns['*'] = get_string('page-x', 'pagetype');
1760 return $patterns;
1764 * Generates the page type list for the my moodle page
1766 * @param string $pagetype
1767 * @param stdClass $parentcontext
1768 * @param stdClass $currentcontext
1769 * @return array
1771 function my_page_type_list($pagetype, $parentcontext = null, $currentcontext = null) {
1772 return array('my-index' => get_string('page-my-index', 'pagetype'));
1776 * Generates the page type list for a module by either locating and using the modules callback
1777 * or by generating a default list.
1779 * @param string $pagetype
1780 * @param stdClass $parentcontext
1781 * @param stdClass $currentcontext
1782 * @return array
1784 function mod_page_type_list($pagetype, $parentcontext = null, $currentcontext = null) {
1785 $patterns = plugin_page_type_list($pagetype, $parentcontext, $currentcontext);
1786 if (empty($patterns)) {
1787 // if modules don't have callbacks
1788 // generate two default page type patterns for modules only
1789 $bits = explode('-', $pagetype);
1790 $patterns = array($pagetype => $pagetype);
1791 if ($bits[2] == 'view') {
1792 $patterns['mod-*-view'] = get_string('page-mod-x-view', 'pagetype');
1793 } else if ($bits[2] == 'index') {
1794 $patterns['mod-*-index'] = get_string('page-mod-x-index', 'pagetype');
1797 return $patterns;
1799 /// Functions update the blocks if required by the request parameters ==========
1802 * Return a {@link block_contents} representing the add a new block UI, if
1803 * this user is allowed to see it.
1805 * @return block_contents an appropriate block_contents, or null if the user
1806 * cannot add any blocks here.
1808 function block_add_block_ui($page, $output) {
1809 global $CFG, $OUTPUT;
1810 if (!$page->user_is_editing() || !$page->user_can_edit_blocks()) {
1811 return null;
1814 $bc = new block_contents();
1815 $bc->title = get_string('addblock');
1816 $bc->add_class('block_adminblock');
1818 $missingblocks = $page->blocks->get_addable_blocks();
1819 if (empty($missingblocks)) {
1820 $bc->content = get_string('noblockstoaddhere');
1821 return $bc;
1824 $menu = array();
1825 foreach ($missingblocks as $block) {
1826 $blockobject = block_instance($block->name);
1827 if ($blockobject !== false && $blockobject->user_can_addto($page)) {
1828 $menu[$block->name] = $blockobject->get_title();
1831 collatorlib::asort($menu);
1833 $actionurl = new moodle_url($page->url, array('sesskey'=>sesskey()));
1834 $select = new single_select($actionurl, 'bui_addblock', $menu, null, array(''=>get_string('adddots')), 'add_block');
1835 $bc->content = $OUTPUT->render($select);
1836 return $bc;
1839 // Functions that have been deprecated by block_manager =======================
1842 * @deprecated since Moodle 2.0 - use $page->blocks->get_addable_blocks();
1844 * This function returns an array with the IDs of any blocks that you can add to your page.
1845 * Parameters are passed by reference for speed; they are not modified at all.
1847 * @param $page the page object.
1848 * @param $blockmanager Not used.
1849 * @return array of block type ids.
1851 function blocks_get_missing(&$page, &$blockmanager) {
1852 debugging('blocks_get_missing is deprecated. Please use $page->blocks->get_addable_blocks() instead.', DEBUG_DEVELOPER);
1853 $blocks = $page->blocks->get_addable_blocks();
1854 $ids = array();
1855 foreach ($blocks as $block) {
1856 $ids[] = $block->id;
1858 return $ids;
1862 * Actually delete from the database any blocks that are currently on this page,
1863 * but which should not be there according to blocks_name_allowed_in_format.
1865 * @todo Write/Fix this function. Currently returns immediately
1866 * @param $course
1868 function blocks_remove_inappropriate($course) {
1869 // TODO
1870 return;
1872 $blockmanager = blocks_get_by_page($page);
1874 if (empty($blockmanager)) {
1875 return;
1878 if (($pageformat = $page->pagetype) == NULL) {
1879 return;
1882 foreach($blockmanager as $region) {
1883 foreach($region as $instance) {
1884 $block = blocks_get_record($instance->blockid);
1885 if(!blocks_name_allowed_in_format($block->name, $pageformat)) {
1886 blocks_delete_instance($instance->instance);
1893 * Check that a given name is in a permittable format
1895 * @param string $name
1896 * @param string $pageformat
1897 * @return bool
1899 function blocks_name_allowed_in_format($name, $pageformat) {
1900 $accept = NULL;
1901 $maxdepth = -1;
1902 if (!$bi = block_instance($name)) {
1903 return false;
1906 $formats = $bi->applicable_formats();
1907 if (!$formats) {
1908 $formats = array();
1910 foreach ($formats as $format => $allowed) {
1911 $formatregex = '/^'.str_replace('*', '[^-]*', $format).'.*$/';
1912 $depth = substr_count($format, '-');
1913 if (preg_match($formatregex, $pageformat) && $depth > $maxdepth) {
1914 $maxdepth = $depth;
1915 $accept = $allowed;
1918 if ($accept === NULL) {
1919 $accept = !empty($formats['all']);
1921 return $accept;
1925 * Delete a block, and associated data.
1927 * @param object $instance a row from the block_instances table
1928 * @param bool $nolongerused legacy parameter. Not used, but kept for backwards compatibility.
1929 * @param bool $skipblockstables for internal use only. Makes @see blocks_delete_all_for_context() more efficient.
1931 function blocks_delete_instance($instance, $nolongerused = false, $skipblockstables = false) {
1932 global $DB;
1934 if ($block = block_instance($instance->blockname, $instance)) {
1935 $block->instance_delete();
1937 delete_context(CONTEXT_BLOCK, $instance->id);
1939 if (!$skipblockstables) {
1940 $DB->delete_records('block_positions', array('blockinstanceid' => $instance->id));
1941 $DB->delete_records('block_instances', array('id' => $instance->id));
1942 $DB->delete_records_list('user_preferences', 'name', array('block'.$instance->id.'hidden','docked_block_instance_'.$instance->id));
1947 * Delete all the blocks that belong to a particular context.
1949 * @param int $contextid the context id.
1951 function blocks_delete_all_for_context($contextid) {
1952 global $DB;
1953 $instances = $DB->get_recordset('block_instances', array('parentcontextid' => $contextid));
1954 foreach ($instances as $instance) {
1955 blocks_delete_instance($instance, true);
1957 $instances->close();
1958 $DB->delete_records('block_instances', array('parentcontextid' => $contextid));
1959 $DB->delete_records('block_positions', array('contextid' => $contextid));
1963 * Set a block to be visible or hidden on a particular page.
1965 * @param object $instance a row from the block_instances, preferably LEFT JOINed with the
1966 * block_positions table as return by block_manager.
1967 * @param moodle_page $page the back to set the visibility with respect to.
1968 * @param integer $newvisibility 1 for visible, 0 for hidden.
1970 function blocks_set_visibility($instance, $page, $newvisibility) {
1971 global $DB;
1972 if (!empty($instance->blockpositionid)) {
1973 // Already have local information on this page.
1974 $DB->set_field('block_positions', 'visible', $newvisibility, array('id' => $instance->blockpositionid));
1975 return;
1978 // Create a new block_positions record.
1979 $bp = new stdClass;
1980 $bp->blockinstanceid = $instance->id;
1981 $bp->contextid = $page->context->id;
1982 $bp->pagetype = $page->pagetype;
1983 if ($page->subpage) {
1984 $bp->subpage = $page->subpage;
1986 $bp->visible = $newvisibility;
1987 $bp->region = $instance->defaultregion;
1988 $bp->weight = $instance->defaultweight;
1989 $DB->insert_record('block_positions', $bp);
1993 * @deprecated since 2.0
1994 * Delete all the blocks from a particular page.
1996 * @param string $pagetype the page type.
1997 * @param integer $pageid the page id.
1998 * @return bool success or failure.
2000 function blocks_delete_all_on_page($pagetype, $pageid) {
2001 global $DB;
2003 debugging('Call to deprecated function blocks_delete_all_on_page. ' .
2004 'This function cannot work any more. Doing nothing. ' .
2005 'Please update your code to use a block_manager method $PAGE->blocks->....', DEBUG_DEVELOPER);
2006 return false;
2010 * Get the block record for a particular blockid - that is, a particular type os block.
2012 * @param $int blockid block type id. If null, an array of all block types is returned.
2013 * @param bool $notusedanymore No longer used.
2014 * @return array|object row from block table, or all rows.
2016 function blocks_get_record($blockid = NULL, $notusedanymore = false) {
2017 global $PAGE;
2018 $blocks = $PAGE->blocks->get_installed_blocks();
2019 if ($blockid === NULL) {
2020 return $blocks;
2021 } else if (isset($blocks[$blockid])) {
2022 return $blocks[$blockid];
2023 } else {
2024 return false;
2029 * Find a given block by its blockid within a provide array
2031 * @param int $blockid
2032 * @param array $blocksarray
2033 * @return bool|object Instance if found else false
2035 function blocks_find_block($blockid, $blocksarray) {
2036 if (empty($blocksarray)) {
2037 return false;
2039 foreach($blocksarray as $blockgroup) {
2040 if (empty($blockgroup)) {
2041 continue;
2043 foreach($blockgroup as $instance) {
2044 if($instance->blockid == $blockid) {
2045 return $instance;
2049 return false;
2052 // Functions for programatically adding default blocks to pages ================
2055 * Parse a list of default blocks. See config-dist for a description of the format.
2057 * @param string $blocksstr
2058 * @return array
2060 function blocks_parse_default_blocks_list($blocksstr) {
2061 $blocks = array();
2062 $bits = explode(':', $blocksstr);
2063 if (!empty($bits)) {
2064 $leftbits = trim(array_shift($bits));
2065 if ($leftbits != '') {
2066 $blocks[BLOCK_POS_LEFT] = explode(',', $leftbits);
2069 if (!empty($bits)) {
2070 $rightbits =trim(array_shift($bits));
2071 if ($rightbits != '') {
2072 $blocks[BLOCK_POS_RIGHT] = explode(',', $rightbits);
2075 return $blocks;
2079 * @return array the blocks that should be added to the site course by default.
2081 function blocks_get_default_site_course_blocks() {
2082 global $CFG;
2084 if (!empty($CFG->defaultblocks_site)) {
2085 return blocks_parse_default_blocks_list($CFG->defaultblocks_site);
2086 } else {
2087 return array(
2088 BLOCK_POS_LEFT => array('site_main_menu'),
2089 BLOCK_POS_RIGHT => array('course_summary', 'calendar_month')
2095 * Add the default blocks to a course.
2097 * @param object $course a course object.
2099 function blocks_add_default_course_blocks($course) {
2100 global $CFG;
2102 if (!empty($CFG->defaultblocks_override)) {
2103 $blocknames = blocks_parse_default_blocks_list($CFG->defaultblocks_override);
2105 } else if ($course->id == SITEID) {
2106 $blocknames = blocks_get_default_site_course_blocks();
2108 } else {
2109 $defaultblocks = 'defaultblocks_' . $course->format;
2110 if (!empty($CFG->$defaultblocks)) {
2111 $blocknames = blocks_parse_default_blocks_list($CFG->$defaultblocks);
2113 } else {
2114 $formatconfig = $CFG->dirroot.'/course/format/'.$course->format.'/config.php';
2115 $format = array(); // initialize array in external file
2116 if (is_readable($formatconfig)) {
2117 include($formatconfig);
2119 if (!empty($format['defaultblocks'])) {
2120 $blocknames = blocks_parse_default_blocks_list($format['defaultblocks']);
2122 } else if (!empty($CFG->defaultblocks)){
2123 $blocknames = blocks_parse_default_blocks_list($CFG->defaultblocks);
2125 } else {
2126 $blocknames = array(
2127 BLOCK_POS_LEFT => array(),
2128 BLOCK_POS_RIGHT => array('search_forums', 'news_items', 'calendar_upcoming', 'recent_activity')
2134 if ($course->id == SITEID) {
2135 $pagetypepattern = 'site-index';
2136 } else {
2137 $pagetypepattern = 'course-view-*';
2139 $page = new moodle_page();
2140 $page->set_course($course);
2141 $page->blocks->add_blocks($blocknames, $pagetypepattern);
2145 * Add the default system-context blocks. E.g. the admin tree.
2147 function blocks_add_default_system_blocks() {
2148 global $DB;
2150 $page = new moodle_page();
2151 $page->set_context(context_system::instance());
2152 $page->blocks->add_blocks(array(BLOCK_POS_LEFT => array('navigation', 'settings')), '*', null, true);
2153 $page->blocks->add_blocks(array(BLOCK_POS_LEFT => array('admin_bookmarks')), 'admin-*', null, null, 2);
2155 if ($defaultmypage = $DB->get_record('my_pages', array('userid'=>null, 'name'=>'__default', 'private'=>1))) {
2156 $subpagepattern = $defaultmypage->id;
2157 } else {
2158 $subpagepattern = null;
2161 $page->blocks->add_blocks(array(BLOCK_POS_RIGHT => array('private_files', 'online_users'), 'content' => array('course_overview')), 'my-index', $subpagepattern, false);