See Release Notes
Long Term Support Release
Differences Between: [Versions 39 and 310] [Versions 39 and 311] [Versions 39 and 400] [Versions 39 and 401] [Versions 39 and 402] [Versions 39 and 403]
1 <?php 2 3 // This file is part of Moodle - http://moodle.org/ 4 // 5 // Moodle is free software: you can redistribute it and/or modify 6 // it under the terms of the GNU General Public License as published by 7 // the Free Software Foundation, either version 3 of the License, or 8 // (at your option) any later version. 9 // 10 // Moodle is distributed in the hope that it will be useful, 11 // but WITHOUT ANY WARRANTY; without even the implied warranty of 12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 // GNU General Public License for more details. 14 // 15 // You should have received a copy of the GNU General Public License 16 // along with Moodle. If not, see <http://www.gnu.org/licenses/>. 17 18 /** 19 * Block Class and Functions 20 * 21 * This file defines the {@link block_manager} class, 22 * 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 27 */ 28 29 defined('MOODLE_INTERNAL') || die(); 30 31 /**#@+ 32 * Default names for the block regions in the standard theme. 33 */ 34 define('BLOCK_POS_LEFT', 'side-pre'); 35 define('BLOCK_POS_RIGHT', 'side-post'); 36 /**#@-*/ 37 38 define('BUI_CONTEXTS_FRONTPAGE_ONLY', 0); 39 define('BUI_CONTEXTS_FRONTPAGE_SUBS', 1); 40 define('BUI_CONTEXTS_ENTIRE_SITE', 2); 41 42 define('BUI_CONTEXTS_CURRENT', 0); 43 define('BUI_CONTEXTS_CURRENT_SUBS', 1); 44 45 // Position of "Add block" control, to be used in theme config as a value for $THEME->addblockposition: 46 // - default: as a fake block that is displayed in editing mode 47 // - flatnav: "Add block" item in the flat navigation drawer in editing mode 48 // - custom: none of the above, theme will take care of displaying the control. 49 define('BLOCK_ADDBLOCK_POSITION_DEFAULT', 0); 50 define('BLOCK_ADDBLOCK_POSITION_FLATNAV', 1); 51 define('BLOCK_ADDBLOCK_POSITION_CUSTOM', -1); 52 53 /** 54 * Exception thrown when someone tried to do something with a block that does 55 * not exist on a page. 56 * 57 * @copyright 2009 Tim Hunt 58 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later 59 * @since Moodle 2.0 60 */ 61 class block_not_on_page_exception extends moodle_exception { 62 /** 63 * Constructor 64 * @param int $instanceid the block instance id of the block that was looked for. 65 * @param object $page the current page. 66 */ 67 public function __construct($instanceid, $page) { 68 $a = new stdClass; 69 $a->instanceid = $instanceid; 70 $a->url = $page->url->out(); 71 parent::__construct('blockdoesnotexistonpage', '', $page->url->out(), $a); 72 } 73 } 74 75 /** 76 * This class keeps track of the block that should appear on a moodle_page. 77 * 78 * The page to work with as passed to the constructor. 79 * 80 * @copyright 2009 Tim Hunt 81 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later 82 * @since Moodle 2.0 83 */ 84 class block_manager { 85 /** 86 * The UI normally only shows block weights between -MAX_WEIGHT and MAX_WEIGHT, 87 * although other weights are valid. 88 */ 89 const MAX_WEIGHT = 10; 90 91 /// Field declarations ========================================================= 92 93 /** 94 * the moodle_page we are managing blocks for. 95 * @var moodle_page 96 */ 97 protected $page; 98 99 /** @var array region name => 1.*/ 100 protected $regions = array(); 101 102 /** @var string the region where new blocks are added.*/ 103 protected $defaultregion = null; 104 105 /** @var array will be $DB->get_records('blocks') */ 106 protected $allblocks = null; 107 108 /** 109 * @var array blocks that this user can add to this page. Will be a subset 110 * of $allblocks, but with array keys block->name. Access this via the 111 * {@link get_addable_blocks()} method to ensure it is lazy-loaded. 112 */ 113 protected $addableblocks = null; 114 115 /** 116 * Will be an array region-name => array(db rows loaded in load_blocks); 117 * @var array 118 */ 119 protected $birecordsbyregion = null; 120 121 /** 122 * array region-name => array(block objects); populated as necessary by 123 * the ensure_instances_exist method. 124 * @var array 125 */ 126 protected $blockinstances = array(); 127 128 /** 129 * array region-name => array(block_contents objects) what actually needs to 130 * be displayed in each region. 131 * @var array 132 */ 133 protected $visibleblockcontent = array(); 134 135 /** 136 * array region-name => array(block_contents objects) extra block-like things 137 * to be displayed in each region, before the real blocks. 138 * @var array 139 */ 140 protected $extracontent = array(); 141 142 /** 143 * Used by the block move id, to track whether a block is currently being moved. 144 * 145 * When you click on the move icon of a block, first the page needs to reload with 146 * extra UI for choosing a new position for a particular block. In that situation 147 * this field holds the id of the block being moved. 148 * 149 * @var integer|null 150 */ 151 protected $movingblock = null; 152 153 /** 154 * Show only fake blocks 155 */ 156 protected $fakeblocksonly = false; 157 158 /// Constructor ================================================================ 159 160 /** 161 * Constructor. 162 * @param object $page the moodle_page object object we are managing the blocks for, 163 * or a reasonable faxilimily. (See the comment at the top of this class 164 * and {@link http://en.wikipedia.org/wiki/Duck_typing}) 165 */ 166 public function __construct($page) { 167 $this->page = $page; 168 } 169 170 /// Getter methods ============================================================= 171 172 /** 173 * Get an array of all region names on this page where a block may appear 174 * 175 * @return array the internal names of the regions on this page where block may appear. 176 */ 177 public function get_regions() { 178 if (is_null($this->defaultregion)) { 179 $this->page->initialise_theme_and_output(); 180 } 181 return array_keys($this->regions); 182 } 183 184 /** 185 * Get the region name of the region blocks are added to by default 186 * 187 * @return string the internal names of the region where new blocks are added 188 * by default, and where any blocks from an unrecognised region are shown. 189 * (Imagine that blocks were added with one theme selected, then you switched 190 * to a theme with different block positions.) 191 */ 192 public function get_default_region() { 193 $this->page->initialise_theme_and_output(); 194 return $this->defaultregion; 195 } 196 197 /** 198 * The list of block types that may be added to this page. 199 * 200 * @return array block name => record from block table. 201 */ 202 public function get_addable_blocks() { 203 $this->check_is_loaded(); 204 205 if (!is_null($this->addableblocks)) { 206 return $this->addableblocks; 207 } 208 209 // Lazy load. 210 $this->addableblocks = array(); 211 212 $allblocks = blocks_get_record(); 213 if (empty($allblocks)) { 214 return $this->addableblocks; 215 } 216 217 $unaddableblocks = self::get_undeletable_block_types(); 218 $requiredbythemeblocks = $this->get_required_by_theme_block_types(); 219 $pageformat = $this->page->pagetype; 220 foreach($allblocks as $block) { 221 if (!$bi = block_instance($block->name)) { 222 continue; 223 } 224 if ($block->visible && !in_array($block->name, $unaddableblocks) && 225 !in_array($block->name, $requiredbythemeblocks) && 226 ($bi->instance_allow_multiple() || !$this->is_block_present($block->name)) && 227 blocks_name_allowed_in_format($block->name, $pageformat) && 228 $bi->user_can_addto($this->page)) { 229 $block->title = $bi->get_title(); 230 $this->addableblocks[$block->name] = $block; 231 } 232 } 233 234 core_collator::asort_objects_by_property($this->addableblocks, 'title'); 235 return $this->addableblocks; 236 } 237 238 /** 239 * Given a block name, find out of any of them are currently present in the page 240 241 * @param string $blockname - the basic name of a block (eg "navigation") 242 * @return boolean - is there one of these blocks in the current page? 243 */ 244 public function is_block_present($blockname) { 245 if (empty($this->blockinstances)) { 246 return false; 247 } 248 249 $requiredbythemeblocks = $this->get_required_by_theme_block_types(); 250 foreach ($this->blockinstances as $region) { 251 foreach ($region as $instance) { 252 if (empty($instance->instance->blockname)) { 253 continue; 254 } 255 if ($instance->instance->blockname == $blockname) { 256 if ($instance->instance->requiredbytheme) { 257 if (!in_array($blockname, $requiredbythemeblocks)) { 258 continue; 259 } 260 } 261 return true; 262 } 263 } 264 } 265 return false; 266 } 267 268 /** 269 * Find out if a block type is known by the system 270 * 271 * @param string $blockname the name of the type of block. 272 * @param boolean $includeinvisible if false (default) only check 'visible' blocks, that is, blocks enabled by the admin. 273 * @return boolean true if this block in installed. 274 */ 275 public function is_known_block_type($blockname, $includeinvisible = false) { 276 $blocks = $this->get_installed_blocks(); 277 foreach ($blocks as $block) { 278 if ($block->name == $blockname && ($includeinvisible || $block->visible)) { 279 return true; 280 } 281 } 282 return false; 283 } 284 285 /** 286 * Find out if a region exists on a page 287 * 288 * @param string $region a region name 289 * @return boolean true if this region exists on this page. 290 */ 291 public function is_known_region($region) { 292 if (empty($region)) { 293 return false; 294 } 295 return array_key_exists($region, $this->regions); 296 } 297 298 /** 299 * Get an array of all blocks within a given region 300 * 301 * @param string $region a block region that exists on this page. 302 * @return array of block instances. 303 */ 304 public function get_blocks_for_region($region) { 305 $this->check_is_loaded(); 306 $this->ensure_instances_exist($region); 307 return $this->blockinstances[$region]; 308 } 309 310 /** 311 * Returns an array of block content objects that exist in a region 312 * 313 * @param string $region a block region that exists on this page. 314 * @return array of block block_contents objects for all the blocks in a region. 315 */ 316 public function get_content_for_region($region, $output) { 317 $this->check_is_loaded(); 318 $this->ensure_content_created($region, $output); 319 return $this->visibleblockcontent[$region]; 320 } 321 322 /** 323 * Returns an array of block content objects for all the existings regions 324 * 325 * @param renderer_base $output the rendered to use 326 * @return array of block block_contents objects for all the blocks in all regions. 327 * @since Moodle 3.3 328 */ 329 public function get_content_for_all_regions($output) { 330 $contents = array(); 331 $this->check_is_loaded(); 332 333 foreach ($this->regions as $region => $val) { 334 $this->ensure_content_created($region, $output); 335 $contents[$region] = $this->visibleblockcontent[$region]; 336 } 337 return $contents; 338 } 339 340 /** 341 * Helper method used by get_content_for_region. 342 * @param string $region region name 343 * @param float $weight weight. May be fractional, since you may want to move a block 344 * between ones with weight 2 and 3, say ($weight would be 2.5). 345 * @return string URL for moving block $this->movingblock to this position. 346 */ 347 protected function get_move_target_url($region, $weight) { 348 return new moodle_url($this->page->url, array('bui_moveid' => $this->movingblock, 349 'bui_newregion' => $region, 'bui_newweight' => $weight, 'sesskey' => sesskey())); 350 } 351 352 /** 353 * Determine whether a region contains anything. (Either any real blocks, or 354 * the add new block UI.) 355 * 356 * (You may wonder why the $output parameter is required. Unfortunately, 357 * because of the way that blocks work, the only reliable way to find out 358 * if a block will be visible is to get the content for output, and to 359 * get the content, you need a renderer. Fortunately, this is not a 360 * performance problem, because we cache the output that is generated, and 361 * in almost every case where we call region_has_content, we are about to 362 * output the blocks anyway, so we are not doing wasted effort.) 363 * 364 * @param string $region a block region that exists on this page. 365 * @param core_renderer $output a core_renderer. normally the global $OUTPUT. 366 * @return boolean Whether there is anything in this region. 367 */ 368 public function region_has_content($region, $output) { 369 370 if (!$this->is_known_region($region)) { 371 return false; 372 } 373 $this->check_is_loaded(); 374 $this->ensure_content_created($region, $output); 375 // if ($this->page->user_is_editing() && $this->page->user_can_edit_blocks()) { 376 // Mark Nielsen's patch - part 1 377 if ($this->page->user_is_editing() && $this->page->user_can_edit_blocks() && $this->movingblock) { 378 // If editing is on, we need all the block regions visible, for the 379 // move blocks UI. 380 return true; 381 } 382 return !empty($this->visibleblockcontent[$region]) || !empty($this->extracontent[$region]); 383 } 384 385 /** 386 * Get an array of all of the installed blocks. 387 * 388 * @return array contents of the block table. 389 */ 390 public function get_installed_blocks() { 391 global $DB; 392 if (is_null($this->allblocks)) { 393 $this->allblocks = $DB->get_records('block'); 394 } 395 return $this->allblocks; 396 } 397 398 /** 399 * @return array names of block types that must exist on every page with this theme. 400 */ 401 public function get_required_by_theme_block_types() { 402 $requiredbythemeblocks = false; 403 if (isset($this->page->theme->requiredblocks)) { 404 $requiredbythemeblocks = $this->page->theme->requiredblocks; 405 } 406 407 if ($requiredbythemeblocks === false) { 408 return array('navigation', 'settings'); 409 } else if ($requiredbythemeblocks === '') { 410 return array(); 411 } else if (is_string($requiredbythemeblocks)) { 412 return explode(',', $requiredbythemeblocks); 413 } else { 414 return $requiredbythemeblocks; 415 } 416 } 417 418 /** 419 * Make this block type undeletable and unaddable. 420 * 421 * @param mixed $blockidorname string or int 422 */ 423 public static function protect_block($blockidorname) { 424 global $DB; 425 426 $syscontext = context_system::instance(); 427 428 require_capability('moodle/site:config', $syscontext); 429 430 $block = false; 431 if (is_int($blockidorname)) { 432 $block = $DB->get_record('block', array('id' => $blockidorname), 'id, name', MUST_EXIST); 433 } else { 434 $block = $DB->get_record('block', array('name' => $blockidorname), 'id, name', MUST_EXIST); 435 } 436 $undeletableblocktypes = self::get_undeletable_block_types(); 437 if (!in_array($block->name, $undeletableblocktypes)) { 438 $undeletableblocktypes[] = $block->name; 439 set_config('undeletableblocktypes', implode(',', $undeletableblocktypes)); 440 add_to_config_log('block_protect', "0", "1", $block->name); 441 } 442 } 443 444 /** 445 * Make this block type deletable and addable. 446 * 447 * @param mixed $blockidorname string or int 448 */ 449 public static function unprotect_block($blockidorname) { 450 global $DB; 451 452 $syscontext = context_system::instance(); 453 454 require_capability('moodle/site:config', $syscontext); 455 456 $block = false; 457 if (is_int($blockidorname)) { 458 $block = $DB->get_record('block', array('id' => $blockidorname), 'id, name', MUST_EXIST); 459 } else { 460 $block = $DB->get_record('block', array('name' => $blockidorname), 'id, name', MUST_EXIST); 461 } 462 $undeletableblocktypes = self::get_undeletable_block_types(); 463 if (in_array($block->name, $undeletableblocktypes)) { 464 $undeletableblocktypes = array_diff($undeletableblocktypes, array($block->name)); 465 set_config('undeletableblocktypes', implode(',', $undeletableblocktypes)); 466 add_to_config_log('block_protect', "1", "0", $block->name); 467 } 468 469 } 470 471 /** 472 * Get the list of "protected" blocks via admin block manager ui. 473 * 474 * @return array names of block types that cannot be added or deleted. E.g. array('navigation','settings'). 475 */ 476 public static function get_undeletable_block_types() { 477 global $CFG; 478 $undeletableblocks = false; 479 if (isset($CFG->undeletableblocktypes)) { 480 $undeletableblocks = $CFG->undeletableblocktypes; 481 } 482 483 if (empty($undeletableblocks)) { 484 return array(); 485 } else if (is_string($undeletableblocks)) { 486 return explode(',', $undeletableblocks); 487 } else { 488 return $undeletableblocks; 489 } 490 } 491 492 /// Setter methods ============================================================= 493 494 /** 495 * Add a region to a page 496 * 497 * @param string $region add a named region where blocks may appear on the current page. 498 * This is an internal name, like 'side-pre', not a string to display in the UI. 499 * @param bool $custom True if this is a custom block region, being added by the page rather than the theme layout. 500 */ 501 public function add_region($region, $custom = true) { 502 global $SESSION; 503 $this->check_not_yet_loaded(); 504 if ($custom) { 505 if (array_key_exists($region, $this->regions)) { 506 // This here is EXACTLY why we should not be adding block regions into a page. It should 507 // ALWAYS be done in a theme layout. 508 debugging('A custom region conflicts with a block region in the theme.', DEBUG_DEVELOPER); 509 } 510 // We need to register this custom region against the page type being used. 511 // This allows us to check, when performing block actions, that unrecognised regions can be worked with. 512 $type = $this->page->pagetype; 513 if (!isset($SESSION->custom_block_regions)) { 514 $SESSION->custom_block_regions = array($type => array($region)); 515 } else if (!isset($SESSION->custom_block_regions[$type])) { 516 $SESSION->custom_block_regions[$type] = array($region); 517 } else if (!in_array($region, $SESSION->custom_block_regions[$type])) { 518 $SESSION->custom_block_regions[$type][] = $region; 519 } 520 } 521 $this->regions[$region] = 1; 522 523 // Checking the actual property instead of calling get_default_region as it ends up in a recursive call. 524 if (empty($this->defaultregion)) { 525 $this->set_default_region($region); 526 } 527 } 528 529 /** 530 * Add an array of regions 531 * @see add_region() 532 * 533 * @param array $regions this utility method calls add_region for each array element. 534 */ 535 public function add_regions($regions, $custom = true) { 536 foreach ($regions as $region) { 537 $this->add_region($region, $custom); 538 } 539 } 540 541 /** 542 * Finds custom block regions associated with a page type and registers them with this block manager. 543 * 544 * @param string $pagetype 545 */ 546 public function add_custom_regions_for_pagetype($pagetype) { 547 global $SESSION; 548 if (isset($SESSION->custom_block_regions[$pagetype])) { 549 foreach ($SESSION->custom_block_regions[$pagetype] as $customregion) { 550 $this->add_region($customregion, false); 551 } 552 } 553 } 554 555 /** 556 * Set the default region for new blocks on the page 557 * 558 * @param string $defaultregion the internal names of the region where new 559 * blocks should be added by default, and where any blocks from an 560 * unrecognised region are shown. 561 */ 562 public function set_default_region($defaultregion) { 563 $this->check_not_yet_loaded(); 564 if ($defaultregion) { 565 $this->check_region_is_known($defaultregion); 566 } 567 $this->defaultregion = $defaultregion; 568 } 569 570 /** 571 * Add something that looks like a block, but which isn't an actual block_instance, 572 * to this page. 573 * 574 * @param block_contents $bc the content of the block-like thing. 575 * @param string $region a block region that exists on this page. 576 */ 577 public function add_fake_block($bc, $region) { 578 $this->page->initialise_theme_and_output(); 579 if (!$this->is_known_region($region)) { 580 $region = $this->get_default_region(); 581 } 582 if (array_key_exists($region, $this->visibleblockcontent)) { 583 throw new coding_exception('block_manager has already prepared the blocks in region ' . 584 $region . 'for output. It is too late to add a fake block.'); 585 } 586 if (!isset($bc->attributes['data-block'])) { 587 $bc->attributes['data-block'] = '_fake'; 588 } 589 $bc->attributes['class'] .= ' block_fake'; 590 $this->extracontent[$region][] = $bc; 591 } 592 593 /** 594 * Checks to see whether all of the blocks within the given region are docked 595 * 596 * @see region_uses_dock 597 * @param string $region 598 * @return bool True if all of the blocks within that region are docked 599 * 600 * Return false as from MDL-64506 601 */ 602 public function region_completely_docked($region, $output) { 603 return false; 604 } 605 606 /** 607 * Checks to see whether any of the blocks within the given regions are docked 608 * 609 * @see region_completely_docked 610 * @param array|string $regions array of regions (or single region) 611 * @return bool True if any of the blocks within that region are docked 612 * 613 * Return false as from MDL-64506 614 */ 615 public function region_uses_dock($regions, $output) { 616 return false; 617 } 618 619 /// Actions ==================================================================== 620 621 /** 622 * This method actually loads the blocks for our page from the database. 623 * 624 * @param boolean|null $includeinvisible 625 * null (default) - load hidden blocks if $this->page->user_is_editing(); 626 * true - load hidden blocks. 627 * false - don't load hidden blocks. 628 */ 629 public function load_blocks($includeinvisible = null) { 630 global $DB, $CFG; 631 632 if (!is_null($this->birecordsbyregion)) { 633 // Already done. 634 return; 635 } 636 637 if ($CFG->version < 2009050619) { 638 // Upgrade/install not complete. Don't try too show any blocks. 639 $this->birecordsbyregion = array(); 640 return; 641 } 642 643 // Ensure we have been initialised. 644 if (is_null($this->defaultregion)) { 645 $this->page->initialise_theme_and_output(); 646 // If there are still no block regions, then there are no blocks on this page. 647 if (empty($this->regions)) { 648 $this->birecordsbyregion = array(); 649 return; 650 } 651 } 652 653 // Check if we need to load normal blocks 654 if ($this->fakeblocksonly) { 655 $this->birecordsbyregion = $this->prepare_per_region_arrays(); 656 return; 657 } 658 659 // Exclude auto created blocks if they are not undeletable in this theme. 660 $requiredbytheme = $this->get_required_by_theme_block_types(); 661 $requiredbythemecheck = ''; 662 $requiredbythemeparams = array(); 663 $requiredbythemenotparams = array(); 664 if (!empty($requiredbytheme)) { 665 list($testsql, $requiredbythemeparams) = $DB->get_in_or_equal($requiredbytheme, SQL_PARAMS_NAMED, 'requiredbytheme'); 666 list($testnotsql, $requiredbythemenotparams) = $DB->get_in_or_equal($requiredbytheme, SQL_PARAMS_NAMED, 667 'notrequiredbytheme', false); 668 $requiredbythemecheck = 'AND ((bi.blockname ' . $testsql . ' AND bi.requiredbytheme = 1) OR ' . 669 ' (bi.blockname ' . $testnotsql . ' AND bi.requiredbytheme = 0))'; 670 } else { 671 $requiredbythemecheck = 'AND (bi.requiredbytheme = 0)'; 672 } 673 674 if (is_null($includeinvisible)) { 675 $includeinvisible = $this->page->user_is_editing(); 676 } 677 if ($includeinvisible) { 678 $visiblecheck = ''; 679 } else { 680 $visiblecheck = 'AND (bp.visible = 1 OR bp.visible IS NULL) AND (bs.visible = 1 OR bs.visible IS NULL)'; 681 } 682 683 $context = $this->page->context; 684 $contexttest = 'bi.parentcontextid IN (:contextid2, :contextid3)'; 685 $parentcontextparams = array(); 686 $parentcontextids = $context->get_parent_context_ids(); 687 if ($parentcontextids) { 688 list($parentcontexttest, $parentcontextparams) = 689 $DB->get_in_or_equal($parentcontextids, SQL_PARAMS_NAMED, 'parentcontext'); 690 $contexttest = "($contexttest OR (bi.showinsubcontexts = 1 AND bi.parentcontextid $parentcontexttest))"; 691 } 692 693 $pagetypepatterns = matching_page_type_patterns($this->page->pagetype); 694 list($pagetypepatterntest, $pagetypepatternparams) = 695 $DB->get_in_or_equal($pagetypepatterns, SQL_PARAMS_NAMED, 'pagetypepatterntest'); 696 697 $ccselect = ', ' . context_helper::get_preload_record_columns_sql('ctx'); 698 $ccjoin = "LEFT JOIN {context} ctx ON (ctx.instanceid = bi.id AND ctx.contextlevel = :contextlevel)"; 699 700 $systemcontext = context_system::instance(); 701 $params = array( 702 'contextlevel' => CONTEXT_BLOCK, 703 'subpage1' => $this->page->subpage, 704 'subpage2' => $this->page->subpage, 705 'subpage3' => $this->page->subpage, 706 'contextid1' => $context->id, 707 'contextid2' => $context->id, 708 'contextid3' => $systemcontext->id, 709 'contextid4' => $systemcontext->id, 710 'pagetype' => $this->page->pagetype, 711 'pagetype2' => $this->page->pagetype, 712 ); 713 if ($this->page->subpage === '') { 714 $params['subpage1'] = ''; 715 $params['subpage2'] = ''; 716 $params['subpage3'] = ''; 717 } 718 $sql = "SELECT 719 bi.id, 720 COALESCE(bp.id, bs.id) AS blockpositionid, 721 bi.blockname, 722 bi.parentcontextid, 723 bi.showinsubcontexts, 724 bi.pagetypepattern, 725 bi.requiredbytheme, 726 bi.subpagepattern, 727 bi.defaultregion, 728 bi.defaultweight, 729 COALESCE(bp.visible, bs.visible, 1) AS visible, 730 COALESCE(bp.region, bs.region, bi.defaultregion) AS region, 731 COALESCE(bp.weight, bs.weight, bi.defaultweight) AS weight, 732 bi.configdata 733 $ccselect 734 735 FROM {block_instances} bi 736 JOIN {block} b ON bi.blockname = b.name 737 LEFT JOIN {block_positions} bp ON bp.blockinstanceid = bi.id 738 AND bp.contextid = :contextid1 739 AND bp.pagetype = :pagetype 740 AND bp.subpage = :subpage1 741 LEFT JOIN {block_positions} bs ON bs.blockinstanceid = bi.id 742 AND bs.contextid = :contextid4 743 AND bs.pagetype = :pagetype2 744 AND bs.subpage = :subpage3 745 $ccjoin 746 747 WHERE 748 $contexttest 749 AND bi.pagetypepattern $pagetypepatterntest 750 AND (bi.subpagepattern IS NULL OR bi.subpagepattern = :subpage2) 751 $visiblecheck 752 AND b.visible = 1 753 $requiredbythemecheck 754 755 ORDER BY 756 COALESCE(bp.region, bs.region, bi.defaultregion), 757 COALESCE(bp.weight, bs.weight, bi.defaultweight), 758 bi.id"; 759 760 $allparams = $params + $parentcontextparams + $pagetypepatternparams + $requiredbythemeparams + $requiredbythemenotparams; 761 $blockinstances = $DB->get_recordset_sql($sql, $allparams); 762 763 $this->birecordsbyregion = $this->prepare_per_region_arrays(); 764 $unknown = array(); 765 foreach ($blockinstances as $bi) { 766 context_helper::preload_from_record($bi); 767 if ($this->is_known_region($bi->region)) { 768 $this->birecordsbyregion[$bi->region][] = $bi; 769 } else { 770 $unknown[] = $bi; 771 } 772 } 773 $blockinstances->close(); 774 775 // Pages don't necessarily have a defaultregion. The one time this can 776 // happen is when there are no theme block regions, but the script itself 777 // has a block region in the main content area. 778 if (!empty($this->defaultregion)) { 779 $this->birecordsbyregion[$this->defaultregion] = 780 array_merge($this->birecordsbyregion[$this->defaultregion], $unknown); 781 } 782 } 783 784 /** 785 * Add a block to the current page, or related pages. The block is added to 786 * context $this->page->contextid. If $pagetypepattern $subpagepattern 787 * 788 * @param string $blockname The type of block to add. 789 * @param string $region the block region on this page to add the block to. 790 * @param integer $weight determines the order where this block appears in the region. 791 * @param boolean $showinsubcontexts whether this block appears in subcontexts, or just the current context. 792 * @param string|null $pagetypepattern which page types this block should appear on. Defaults to just the current page type. 793 * @param string|null $subpagepattern which subpage this block should appear on. NULL = any (the default), otherwise only the specified subpage. 794 */ 795 public function add_block($blockname, $region, $weight, $showinsubcontexts, $pagetypepattern = NULL, $subpagepattern = NULL) { 796 global $DB; 797 // Allow invisible blocks because this is used when adding default page blocks, which 798 // might include invisible ones if the user makes some default blocks invisible 799 $this->check_known_block_type($blockname, true); 800 $this->check_region_is_known($region); 801 802 if (empty($pagetypepattern)) { 803 $pagetypepattern = $this->page->pagetype; 804 } 805 806 $blockinstance = new stdClass; 807 $blockinstance->blockname = $blockname; 808 $blockinstance->parentcontextid = $this->page->context->id; 809 $blockinstance->showinsubcontexts = !empty($showinsubcontexts); 810 $blockinstance->pagetypepattern = $pagetypepattern; 811 $blockinstance->subpagepattern = $subpagepattern; 812 $blockinstance->defaultregion = $region; 813 $blockinstance->defaultweight = $weight; 814 $blockinstance->configdata = ''; 815 $blockinstance->timecreated = time(); 816 $blockinstance->timemodified = $blockinstance->timecreated; 817 $blockinstance->id = $DB->insert_record('block_instances', $blockinstance); 818 819 // Ensure the block context is created. 820 context_block::instance($blockinstance->id); 821 822 // If the new instance was created, allow it to do additional setup 823 if ($block = block_instance($blockname, $blockinstance)) { 824 $block->instance_create(); 825 } 826 } 827 828 public function add_block_at_end_of_default_region($blockname) { 829 if (empty($this->birecordsbyregion)) { 830 // No blocks or block regions exist yet. 831 return; 832 } 833 $defaulregion = $this->get_default_region(); 834 835 $lastcurrentblock = end($this->birecordsbyregion[$defaulregion]); 836 if ($lastcurrentblock) { 837 $weight = $lastcurrentblock->weight + 1; 838 } else { 839 $weight = 0; 840 } 841 842 if ($this->page->subpage) { 843 $subpage = $this->page->subpage; 844 } else { 845 $subpage = null; 846 } 847 848 // Special case. Course view page type include the course format, but we 849 // want to add the block non-format-specifically. 850 $pagetypepattern = $this->page->pagetype; 851 if (strpos($pagetypepattern, 'course-view') === 0) { 852 $pagetypepattern = 'course-view-*'; 853 } 854 855 // We should end using this for ALL the blocks, making always the 1st option 856 // the default one to be used. Until then, this is one hack to avoid the 857 // 'pagetypewarning' message on blocks initial edition (MDL-27829) caused by 858 // non-existing $pagetypepattern set. This way at least we guarantee one "valid" 859 // (the FIRST $pagetypepattern will be set) 860 861 // We are applying it to all blocks created in mod pages for now and only if the 862 // default pagetype is not one of the available options 863 if (preg_match('/^mod-.*-/', $pagetypepattern)) { 864 $pagetypelist = generate_page_type_patterns($this->page->pagetype, null, $this->page->context); 865 // Only go for the first if the pagetype is not a valid option 866 if (is_array($pagetypelist) && !array_key_exists($pagetypepattern, $pagetypelist)) { 867 $pagetypepattern = key($pagetypelist); 868 } 869 } 870 // Surely other pages like course-report will need this too, they just are not important 871 // enough now. This will be decided in the coming days. (MDL-27829, MDL-28150) 872 873 $this->add_block($blockname, $defaulregion, $weight, false, $pagetypepattern, $subpage); 874 } 875 876 /** 877 * Convenience method, calls add_block repeatedly for all the blocks in $blocks. Optionally, a starting weight 878 * can be used to decide the starting point that blocks are added in the region, the weight is passed to {@link add_block} 879 * and incremented by the position of the block in the $blocks array 880 * 881 * @param array $blocks array with array keys the region names, and values an array of block names. 882 * @param string $pagetypepattern optional. Passed to {@link add_block()} 883 * @param string $subpagepattern optional. Passed to {@link add_block()} 884 * @param boolean $showinsubcontexts optional. Passed to {@link add_block()} 885 * @param integer $weight optional. Determines the starting point that the blocks are added in the region. 886 */ 887 public function add_blocks($blocks, $pagetypepattern = NULL, $subpagepattern = NULL, $showinsubcontexts=false, $weight=0) { 888 $initialweight = $weight; 889 $this->add_regions(array_keys($blocks), false); 890 foreach ($blocks as $region => $regionblocks) { 891 foreach ($regionblocks as $offset => $blockname) { 892 $weight = $initialweight + $offset; 893 $this->add_block($blockname, $region, $weight, $showinsubcontexts, $pagetypepattern, $subpagepattern); 894 } 895 } 896 } 897 898 /** 899 * Move a block to a new position on this page. 900 * 901 * If this block cannot appear on any other pages, then we change defaultposition/weight 902 * in the block_instances table. Otherwise we just set the position on this page. 903 * 904 * @param $blockinstanceid the block instance id. 905 * @param $newregion the new region name. 906 * @param $newweight the new weight. 907 */ 908 public function reposition_block($blockinstanceid, $newregion, $newweight) { 909 global $DB; 910 911 $this->check_region_is_known($newregion); 912 $inst = $this->find_instance($blockinstanceid); 913 914 $bi = $inst->instance; 915 if ($bi->weight == $bi->defaultweight && $bi->region == $bi->defaultregion && 916 !$bi->showinsubcontexts && strpos($bi->pagetypepattern, '*') === false && 917 (!$this->page->subpage || $bi->subpagepattern)) { 918 919 // Set default position 920 $newbi = new stdClass; 921 $newbi->id = $bi->id; 922 $newbi->defaultregion = $newregion; 923 $newbi->defaultweight = $newweight; 924 $newbi->timemodified = time(); 925 $DB->update_record('block_instances', $newbi); 926 927 if ($bi->blockpositionid) { 928 $bp = new stdClass; 929 $bp->id = $bi->blockpositionid; 930 $bp->region = $newregion; 931 $bp->weight = $newweight; 932 $DB->update_record('block_positions', $bp); 933 } 934 935 } else { 936 // Just set position on this page. 937 $bp = new stdClass; 938 $bp->region = $newregion; 939 $bp->weight = $newweight; 940 941 if ($bi->blockpositionid) { 942 $bp->id = $bi->blockpositionid; 943 $DB->update_record('block_positions', $bp); 944 945 } else { 946 $bp->blockinstanceid = $bi->id; 947 $bp->contextid = $this->page->context->id; 948 $bp->pagetype = $this->page->pagetype; 949 if ($this->page->subpage) { 950 $bp->subpage = $this->page->subpage; 951 } else { 952 $bp->subpage = ''; 953 } 954 $bp->visible = $bi->visible; 955 $DB->insert_record('block_positions', $bp); 956 } 957 } 958 } 959 960 /** 961 * Find a given block by its instance id 962 * 963 * @param integer $instanceid 964 * @return block_base 965 */ 966 public function find_instance($instanceid) { 967 foreach ($this->regions as $region => $notused) { 968 $this->ensure_instances_exist($region); 969 foreach($this->blockinstances[$region] as $instance) { 970 if ($instance->instance->id == $instanceid) { 971 return $instance; 972 } 973 } 974 } 975 throw new block_not_on_page_exception($instanceid, $this->page); 976 } 977 978 /// Inner workings ============================================================= 979 980 /** 981 * Check whether the page blocks have been loaded yet 982 * 983 * @return void Throws coding exception if already loaded 984 */ 985 protected function check_not_yet_loaded() { 986 if (!is_null($this->birecordsbyregion)) { 987 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.'); 988 } 989 } 990 991 /** 992 * Check whether the page blocks have been loaded yet 993 * 994 * Nearly identical to the above function {@link check_not_yet_loaded()} except different message 995 * 996 * @return void Throws coding exception if already loaded 997 */ 998 protected function check_is_loaded() { 999 if (is_null($this->birecordsbyregion)) { 1000 throw new coding_exception('block_manager has not yet loaded the blocks, to it is too soon to request the information you asked for.'); 1001 } 1002 } 1003 1004 /** 1005 * Check if a block type is known and usable 1006 * 1007 * @param string $blockname The block type name to search for 1008 * @param bool $includeinvisible Include disabled block types in the initial pass 1009 * @return void Coding Exception thrown if unknown or not enabled 1010 */ 1011 protected function check_known_block_type($blockname, $includeinvisible = false) { 1012 if (!$this->is_known_block_type($blockname, $includeinvisible)) { 1013 if ($this->is_known_block_type($blockname, true)) { 1014 throw new coding_exception('Unknown block type ' . $blockname); 1015 } else { 1016 throw new coding_exception('Block type ' . $blockname . ' has been disabled by the administrator.'); 1017 } 1018 } 1019 } 1020 1021 /** 1022 * Check if a region is known by its name 1023 * 1024 * @param string $region 1025 * @return void Coding Exception thrown if the region is not known 1026 */ 1027 protected function check_region_is_known($region) { 1028 if (!$this->is_known_region($region)) { 1029 throw new coding_exception('Trying to reference an unknown block region ' . $region); 1030 } 1031 } 1032 1033 /** 1034 * Returns an array of region names as keys and nested arrays for values 1035 * 1036 * @return array an array where the array keys are the region names, and the array 1037 * values are empty arrays. 1038 */ 1039 protected function prepare_per_region_arrays() { 1040 $result = array(); 1041 foreach ($this->regions as $region => $notused) { 1042 $result[$region] = array(); 1043 } 1044 return $result; 1045 } 1046 1047 /** 1048 * Create a set of new block instance from a record array 1049 * 1050 * @param array $birecords An array of block instance records 1051 * @return array An array of instantiated block_instance objects 1052 */ 1053 protected function create_block_instances($birecords) { 1054 $results = array(); 1055 foreach ($birecords as $record) { 1056 if ($blockobject = block_instance($record->blockname, $record, $this->page)) { 1057 $results[] = $blockobject; 1058 } 1059 } 1060 return $results; 1061 } 1062 1063 /** 1064 * Create all the block instances for all the blocks that were loaded by 1065 * load_blocks. This is used, for example, to ensure that all blocks get a 1066 * chance to initialise themselves via the {@link block_base::specialize()} 1067 * method, before any output is done. 1068 * 1069 * It is also used to create any blocks that are "requiredbytheme" by the current theme. 1070 * These blocks that are auto-created have requiredbytheme set on the block instance 1071 * so they are only visible on themes that require them. 1072 */ 1073 public function create_all_block_instances() { 1074 $missing = false; 1075 1076 // If there are any un-removable blocks that were not created - force them. 1077 $requiredbytheme = $this->get_required_by_theme_block_types(); 1078 if (!$this->fakeblocksonly) { 1079 foreach ($requiredbytheme as $forced) { 1080 if (empty($forced)) { 1081 continue; 1082 } 1083 $found = false; 1084 foreach ($this->get_regions() as $region) { 1085 foreach($this->birecordsbyregion[$region] as $instance) { 1086 if ($instance->blockname == $forced) { 1087 $found = true; 1088 } 1089 } 1090 } 1091 if (!$found) { 1092 $this->add_block_required_by_theme($forced); 1093 $missing = true; 1094 } 1095 } 1096 } 1097 1098 if ($missing) { 1099 // Some blocks were missing. Lets do it again. 1100 $this->birecordsbyregion = null; 1101 $this->load_blocks(); 1102 } 1103 foreach ($this->get_regions() as $region) { 1104 $this->ensure_instances_exist($region); 1105 } 1106 1107 } 1108 1109 /** 1110 * Add a block that is required by the current theme but has not been 1111 * created yet. This is a special type of block that only shows in themes that 1112 * require it (by listing it in undeletable_block_types). 1113 * 1114 * @param string $blockname the name of the block type. 1115 */ 1116 protected function add_block_required_by_theme($blockname) { 1117 global $DB; 1118 1119 if (empty($this->birecordsbyregion)) { 1120 // No blocks or block regions exist yet. 1121 return; 1122 } 1123 1124 // Never auto create blocks when we are showing fake blocks only. 1125 if ($this->fakeblocksonly) { 1126 return; 1127 } 1128 1129 // Never add a duplicate block required by theme. 1130 if ($DB->record_exists('block_instances', array('blockname' => $blockname, 'requiredbytheme' => 1))) { 1131 return; 1132 } 1133 1134 $systemcontext = context_system::instance(); 1135 $defaultregion = $this->get_default_region(); 1136 // Add a special system wide block instance only for themes that require it. 1137 $blockinstance = new stdClass; 1138 $blockinstance->blockname = $blockname; 1139 $blockinstance->parentcontextid = $systemcontext->id; 1140 $blockinstance->showinsubcontexts = true; 1141 $blockinstance->requiredbytheme = true; 1142 $blockinstance->pagetypepattern = '*'; 1143 $blockinstance->subpagepattern = null; 1144 $blockinstance->defaultregion = $defaultregion; 1145 $blockinstance->defaultweight = 0; 1146 $blockinstance->configdata = ''; 1147 $blockinstance->timecreated = time(); 1148 $blockinstance->timemodified = $blockinstance->timecreated; 1149 $blockinstance->id = $DB->insert_record('block_instances', $blockinstance); 1150 1151 // Ensure the block context is created. 1152 context_block::instance($blockinstance->id); 1153 1154 // If the new instance was created, allow it to do additional setup. 1155 if ($block = block_instance($blockname, $blockinstance)) { 1156 $block->instance_create(); 1157 } 1158 } 1159 1160 /** 1161 * Return an array of content objects from a set of block instances 1162 * 1163 * @param array $instances An array of block instances 1164 * @param renderer_base The renderer to use. 1165 * @param string $region the region name. 1166 * @return array An array of block_content (and possibly block_move_target) objects. 1167 */ 1168 protected function create_block_contents($instances, $output, $region) { 1169 $results = array(); 1170 1171 $lastweight = 0; 1172 $lastblock = 0; 1173 if ($this->movingblock) { 1174 $first = reset($instances); 1175 if ($first) { 1176 $lastweight = $first->instance->weight - 2; 1177 } 1178 } 1179 1180 foreach ($instances as $instance) { 1181 $content = $instance->get_content_for_output($output); 1182 if (empty($content)) { 1183 continue; 1184 } 1185 1186 if ($this->movingblock && $lastweight != $instance->instance->weight && 1187 $content->blockinstanceid != $this->movingblock && $lastblock != $this->movingblock) { 1188 $results[] = new block_move_target($this->get_move_target_url($region, ($lastweight + $instance->instance->weight)/2)); 1189 } 1190 1191 if ($content->blockinstanceid == $this->movingblock) { 1192 $content->add_class('beingmoved'); 1193 $content->annotation .= get_string('movingthisblockcancel', 'block', 1194 html_writer::link($this->page->url, get_string('cancel'))); 1195 } 1196 1197 $results[] = $content; 1198 $lastweight = $instance->instance->weight; 1199 $lastblock = $instance->instance->id; 1200 } 1201 1202 if ($this->movingblock && $lastblock != $this->movingblock) { 1203 $results[] = new block_move_target($this->get_move_target_url($region, $lastweight + 1)); 1204 } 1205 return $results; 1206 } 1207 1208 /** 1209 * Ensure block instances exist for a given region 1210 * 1211 * @param string $region Check for bi's with the instance with this name 1212 */ 1213 protected function ensure_instances_exist($region) { 1214 $this->check_region_is_known($region); 1215 if (!array_key_exists($region, $this->blockinstances)) { 1216 $this->blockinstances[$region] = 1217 $this->create_block_instances($this->birecordsbyregion[$region]); 1218 } 1219 } 1220 1221 /** 1222 * Ensure that there is some content within the given region 1223 * 1224 * @param string $region The name of the region to check 1225 */ 1226 public function ensure_content_created($region, $output) { 1227 $this->ensure_instances_exist($region); 1228 1229 if (!has_capability('moodle/block:view', $this->page->context) ) { 1230 $this->visibleblockcontent[$region] = []; 1231 return; 1232 } 1233 1234 if (!array_key_exists($region, $this->visibleblockcontent)) { 1235 $contents = array(); 1236 if (array_key_exists($region, $this->extracontent)) { 1237 $contents = $this->extracontent[$region]; 1238 } 1239 $contents = array_merge($contents, $this->create_block_contents($this->blockinstances[$region], $output, $region)); 1240 if (($region == $this->defaultregion) && (!isset($this->page->theme->addblockposition) || 1241 $this->page->theme->addblockposition == BLOCK_ADDBLOCK_POSITION_DEFAULT)) { 1242 $addblockui = block_add_block_ui($this->page, $output); 1243 if ($addblockui) { 1244 $contents[] = $addblockui; 1245 } 1246 } 1247 $this->visibleblockcontent[$region] = $contents; 1248 } 1249 } 1250 1251 /// Process actions from the URL =============================================== 1252 1253 /** 1254 * Get the appropriate list of editing icons for a block. This is used 1255 * to set {@link block_contents::$controls} in {@link block_base::get_contents_for_output()}. 1256 * 1257 * @param $output The core_renderer to use when generating the output. (Need to get icon paths.) 1258 * @return an array in the format for {@link block_contents::$controls} 1259 */ 1260 public function edit_controls($block) { 1261 global $CFG; 1262 1263 $controls = array(); 1264 $actionurl = $this->page->url->out(false, array('sesskey'=> sesskey())); 1265 $blocktitle = $block->title; 1266 if (empty($blocktitle)) { 1267 $blocktitle = $block->arialabel; 1268 } 1269 1270 if ($this->page->user_can_edit_blocks()) { 1271 // Move icon. 1272 $str = new lang_string('moveblock', 'block', $blocktitle); 1273 $controls[] = new action_menu_link_primary( 1274 new moodle_url($actionurl, array('bui_moveid' => $block->instance->id)), 1275 new pix_icon('t/move', $str, 'moodle', array('class' => 'iconsmall', 'title' => '')), 1276 $str, 1277 array('class' => 'editing_move') 1278 ); 1279 1280 } 1281 1282 if ($this->page->user_can_edit_blocks() || $block->user_can_edit()) { 1283 // Edit config icon - always show - needed for positioning UI. 1284 $str = new lang_string('configureblock', 'block', $blocktitle); 1285 $editactionurl = new moodle_url($actionurl, ['bui_editid' => $block->instance->id]); 1286 $editactionurl->remove_params(['sesskey']); 1287 $controls[] = new action_menu_link_secondary( 1288 $editactionurl, 1289 new pix_icon('t/edit', $str, 'moodle', array('class' => 'iconsmall', 'title' => '')), 1290 $str, 1291 array('class' => 'editing_edit') 1292 ); 1293 1294 } 1295 1296 if ($this->page->user_can_edit_blocks() && $block->instance_can_be_hidden()) { 1297 // Show/hide icon. 1298 if ($block->instance->visible) { 1299 $str = new lang_string('hideblock', 'block', $blocktitle); 1300 $url = new moodle_url($actionurl, array('bui_hideid' => $block->instance->id)); 1301 $icon = new pix_icon('t/hide', $str, 'moodle', array('class' => 'iconsmall', 'title' => '')); 1302 $attributes = array('class' => 'editing_hide'); 1303 } else { 1304 $str = new lang_string('showblock', 'block', $blocktitle); 1305 $url = new moodle_url($actionurl, array('bui_showid' => $block->instance->id)); 1306 $icon = new pix_icon('t/show', $str, 'moodle', array('class' => 'iconsmall', 'title' => '')); 1307 $attributes = array('class' => 'editing_show'); 1308 } 1309 $controls[] = new action_menu_link_secondary($url, $icon, $str, $attributes); 1310 } 1311 1312 // Assign roles. 1313 if (get_assignable_roles($block->context, ROLENAME_SHORT)) { 1314 $rolesurl = new moodle_url('/admin/roles/assign.php', array('contextid' => $block->context->id, 1315 'returnurl' => $this->page->url->out_as_local_url())); 1316 $str = new lang_string('assignrolesinblock', 'block', $blocktitle); 1317 $controls[] = new action_menu_link_secondary( 1318 $rolesurl, 1319 new pix_icon('i/assignroles', $str, 'moodle', array('class' => 'iconsmall', 'title' => '')), 1320 $str, array('class' => 'editing_assignroles') 1321 ); 1322 } 1323 1324 // Permissions. 1325 if (has_capability('moodle/role:review', $block->context) or get_overridable_roles($block->context)) { 1326 $rolesurl = new moodle_url('/admin/roles/permissions.php', array('contextid' => $block->context->id, 1327 'returnurl' => $this->page->url->out_as_local_url())); 1328 $str = get_string('permissions', 'role'); 1329 $controls[] = new action_menu_link_secondary( 1330 $rolesurl, 1331 new pix_icon('i/permissions', $str, 'moodle', array('class' => 'iconsmall', 'title' => '')), 1332 $str, array('class' => 'editing_permissions') 1333 ); 1334 } 1335 1336 // Change permissions. 1337 if (has_any_capability(array('moodle/role:safeoverride', 'moodle/role:override', 'moodle/role:assign'), $block->context)) { 1338 $rolesurl = new moodle_url('/admin/roles/check.php', array('contextid' => $block->context->id, 1339 'returnurl' => $this->page->url->out_as_local_url())); 1340 $str = get_string('checkpermissions', 'role'); 1341 $controls[] = new action_menu_link_secondary( 1342 $rolesurl, 1343 new pix_icon('i/checkpermissions', $str, 'moodle', array('class' => 'iconsmall', 'title' => '')), 1344 $str, array('class' => 'editing_checkroles') 1345 ); 1346 } 1347 1348 if ($this->user_can_delete_block($block)) { 1349 // Delete icon. 1350 $str = new lang_string('deleteblock', 'block', $blocktitle); 1351 $deleteactionurl = new moodle_url($actionurl, ['bui_deleteid' => $block->instance->id]); 1352 $deleteactionurl->remove_params(['sesskey']); 1353 $controls[] = new action_menu_link_secondary( 1354 $deleteactionurl, 1355 new pix_icon('t/delete', $str, 'moodle', array('class' => 'iconsmall', 'title' => '')), 1356 $str, 1357 array('class' => 'editing_delete') 1358 ); 1359 } 1360 1361 if (!empty($CFG->contextlocking) && has_capability('moodle/site:managecontextlocks', $block->context)) { 1362 $parentcontext = $block->context->get_parent_context(); 1363 if (empty($parentcontext) || empty($parentcontext->locked)) { 1364 if ($block->context->locked) { 1365 $lockicon = 'i/unlock'; 1366 $lockstring = get_string('managecontextunlock', 'admin'); 1367 } else { 1368 $lockicon = 'i/lock'; 1369 $lockstring = get_string('managecontextlock', 'admin'); 1370 } 1371 $controls[] = new action_menu_link_secondary( 1372 new moodle_url( 1373 '/admin/lock.php', 1374 [ 1375 'id' => $block->context->id, 1376 ] 1377 ), 1378 new pix_icon($lockicon, $lockstring, 'moodle', array('class' => 'iconsmall', 'title' => '')), 1379 $lockstring, 1380 ['class' => 'editing_lock'] 1381 ); 1382 } 1383 } 1384 1385 return $controls; 1386 } 1387 1388 /** 1389 * @param block_base $block a block that appears on this page. 1390 * @return boolean boolean whether the currently logged in user is allowed to delete this block. 1391 */ 1392 protected function user_can_delete_block($block) { 1393 return $this->page->user_can_edit_blocks() && $block->user_can_edit() && 1394 $block->user_can_addto($this->page) && 1395 !in_array($block->instance->blockname, self::get_undeletable_block_types()) && 1396 !in_array($block->instance->blockname, $this->get_required_by_theme_block_types()); 1397 } 1398 1399 /** 1400 * Process any block actions that were specified in the URL. 1401 * 1402 * @return boolean true if anything was done. False if not. 1403 */ 1404 public function process_url_actions() { 1405 if (!$this->page->user_is_editing()) { 1406 return false; 1407 } 1408 return $this->process_url_add() || $this->process_url_delete() || 1409 $this->process_url_show_hide() || $this->process_url_edit() || 1410 $this->process_url_move(); 1411 } 1412 1413 /** 1414 * Handle adding a block. 1415 * @return boolean true if anything was done. False if not. 1416 */ 1417 public function process_url_add() { 1418 global $CFG, $PAGE, $OUTPUT; 1419 1420 $blocktype = optional_param('bui_addblock', null, PARAM_PLUGIN); 1421 if ($blocktype === null) { 1422 return false; 1423 } 1424 1425 require_sesskey(); 1426 1427 if (!$this->page->user_can_edit_blocks()) { 1428 throw new moodle_exception('nopermissions', '', $this->page->url->out(), get_string('addblock')); 1429 } 1430 1431 $addableblocks = $this->get_addable_blocks(); 1432 1433 if ($blocktype === '') { 1434 // Display add block selection. 1435 $addpage = new moodle_page(); 1436 $addpage->set_pagelayout('admin'); 1437 $addpage->blocks->show_only_fake_blocks(true); 1438 $addpage->set_course($this->page->course); 1439 $addpage->set_context($this->page->context); 1440 if ($this->page->cm) { 1441 $addpage->set_cm($this->page->cm); 1442 } 1443 1444 $addpagebase = str_replace($CFG->wwwroot . '/', '/', $this->page->url->out_omit_querystring()); 1445 $addpageparams = $this->page->url->params(); 1446 $addpage->set_url($addpagebase, $addpageparams); 1447 $addpage->set_block_actions_done(); 1448 // At this point we are going to display the block selector, overwrite global $PAGE ready for this. 1449 $PAGE = $addpage; 1450 // Some functions use $OUTPUT so we need to replace that too. 1451 $OUTPUT = $addpage->get_renderer('core'); 1452 1453 $site = get_site(); 1454 $straddblock = get_string('addblock'); 1455 1456 $PAGE->navbar->add($straddblock); 1457 $PAGE->set_title($straddblock); 1458 $PAGE->set_heading($site->fullname); 1459 echo $OUTPUT->header(); 1460 echo $OUTPUT->heading($straddblock); 1461 1462 if (!$addableblocks) { 1463 echo $OUTPUT->box(get_string('noblockstoaddhere')); 1464 echo $OUTPUT->container($OUTPUT->action_link($addpage->url, get_string('back')), 'mx-3 mb-1'); 1465 } else { 1466 $url = new moodle_url($addpage->url, array('sesskey' => sesskey())); 1467 echo $OUTPUT->render_from_template('core/add_block_body', 1468 ['blocks' => array_values($addableblocks), 1469 'url' => '?' . $url->get_query_string(false)]); 1470 echo $OUTPUT->container($OUTPUT->action_link($addpage->url, get_string('cancel')), 'mx-3 mb-1'); 1471 } 1472 1473 echo $OUTPUT->footer(); 1474 // Make sure that nothing else happens after we have displayed this form. 1475 exit; 1476 } 1477 1478 if (!array_key_exists($blocktype, $addableblocks)) { 1479 throw new moodle_exception('cannotaddthisblocktype', '', $this->page->url->out(), $blocktype); 1480 } 1481 1482 $this->add_block_at_end_of_default_region($blocktype); 1483 1484 // If the page URL was a guess, it will contain the bui_... param, so we must make sure it is not there. 1485 $this->page->ensure_param_not_in_url('bui_addblock'); 1486 1487 return true; 1488 } 1489 1490 /** 1491 * Handle deleting a block. 1492 * @return boolean true if anything was done. False if not. 1493 */ 1494 public function process_url_delete() { 1495 global $CFG, $PAGE, $OUTPUT; 1496 1497 $blockid = optional_param('bui_deleteid', null, PARAM_INT); 1498 $confirmdelete = optional_param('bui_confirm', null, PARAM_INT); 1499 1500 if (!$blockid) { 1501 return false; 1502 } 1503 1504 $block = $this->page->blocks->find_instance($blockid); 1505 if (!$this->user_can_delete_block($block)) { 1506 throw new moodle_exception('nopermissions', '', $this->page->url->out(), get_string('deleteablock')); 1507 } 1508 1509 if (!$confirmdelete) { 1510 $deletepage = new moodle_page(); 1511 $deletepage->set_pagelayout('admin'); 1512 $deletepage->blocks->show_only_fake_blocks(true); 1513 $deletepage->set_course($this->page->course); 1514 $deletepage->set_context($this->page->context); 1515 if ($this->page->cm) { 1516 $deletepage->set_cm($this->page->cm); 1517 } 1518 1519 $deleteurlbase = str_replace($CFG->wwwroot . '/', '/', $this->page->url->out_omit_querystring()); 1520 $deleteurlparams = $this->page->url->params(); 1521 $deletepage->set_url($deleteurlbase, $deleteurlparams); 1522 $deletepage->set_block_actions_done(); 1523 // At this point we are either going to redirect, or display the form, so 1524 // overwrite global $PAGE ready for this. (Formslib refers to it.) 1525 $PAGE = $deletepage; 1526 //some functions like MoodleQuickForm::addHelpButton use $OUTPUT so we need to replace that too 1527 $output = $deletepage->get_renderer('core'); 1528 $OUTPUT = $output; 1529 1530 $site = get_site(); 1531 $blocktitle = $block->get_title(); 1532 $strdeletecheck = get_string('deletecheck', 'block', $blocktitle); 1533 $message = get_string('deleteblockcheck', 'block', $blocktitle); 1534 1535 // If the block is being shown in sub contexts display a warning. 1536 if ($block->instance->showinsubcontexts == 1) { 1537 $parentcontext = context::instance_by_id($block->instance->parentcontextid); 1538 $systemcontext = context_system::instance(); 1539 $messagestring = new stdClass(); 1540 $messagestring->location = $parentcontext->get_context_name(); 1541 1542 // Checking for blocks that may have visibility on the front page and pages added on that. 1543 if ($parentcontext->id != $systemcontext->id && is_inside_frontpage($parentcontext)) { 1544 $messagestring->pagetype = get_string('showonfrontpageandsubs', 'block'); 1545 } else { 1546 $pagetypes = generate_page_type_patterns($this->page->pagetype, $parentcontext); 1547 $messagestring->pagetype = $block->instance->pagetypepattern; 1548 if (isset($pagetypes[$block->instance->pagetypepattern])) { 1549 $messagestring->pagetype = $pagetypes[$block->instance->pagetypepattern]; 1550 } 1551 } 1552 1553 $message = get_string('deleteblockwarning', 'block', $messagestring); 1554 } 1555 1556 $PAGE->navbar->add($strdeletecheck); 1557 $PAGE->set_title($blocktitle . ': ' . $strdeletecheck); 1558 $PAGE->set_heading($site->fullname); 1559 echo $OUTPUT->header(); 1560 $confirmurl = new moodle_url($deletepage->url, array('sesskey' => sesskey(), 'bui_deleteid' => $block->instance->id, 'bui_confirm' => 1)); 1561 $cancelurl = new moodle_url($deletepage->url); 1562 $yesbutton = new single_button($confirmurl, get_string('yes')); 1563 $nobutton = new single_button($cancelurl, get_string('no')); 1564 echo $OUTPUT->confirm($message, $yesbutton, $nobutton); 1565 echo $OUTPUT->footer(); 1566 // Make sure that nothing else happens after we have displayed this form. 1567 exit; 1568 } else { 1569 require_sesskey(); 1570 1571 blocks_delete_instance($block->instance); 1572 // bui_deleteid and bui_confirm should not be in the PAGE url. 1573 $this->page->ensure_param_not_in_url('bui_deleteid'); 1574 $this->page->ensure_param_not_in_url('bui_confirm'); 1575 return true; 1576 } 1577 } 1578 1579 /** 1580 * Handle showing or hiding a block. 1581 * @return boolean true if anything was done. False if not. 1582 */ 1583 public function process_url_show_hide() { 1584 if ($blockid = optional_param('bui_hideid', null, PARAM_INT)) { 1585 $newvisibility = 0; 1586 } else if ($blockid = optional_param('bui_showid', null, PARAM_INT)) { 1587 $newvisibility = 1; 1588 } else { 1589 return false; 1590 } 1591 1592 require_sesskey(); 1593 1594 $block = $this->page->blocks->find_instance($blockid); 1595 1596 if (!$this->page->user_can_edit_blocks()) { 1597 throw new moodle_exception('nopermissions', '', $this->page->url->out(), get_string('hideshowblocks')); 1598 } else if (!$block->instance_can_be_hidden()) { 1599 return false; 1600 } 1601 1602 blocks_set_visibility($block->instance, $this->page, $newvisibility); 1603 1604 // If the page URL was a guses, it will contain the bui_... param, so we must make sure it is not there. 1605 $this->page->ensure_param_not_in_url('bui_hideid'); 1606 $this->page->ensure_param_not_in_url('bui_showid'); 1607 1608 return true; 1609 } 1610 1611 /** 1612 * Handle showing/processing the submission from the block editing form. 1613 * @return boolean true if the form was submitted and the new config saved. Does not 1614 * return if the editing form was displayed. False otherwise. 1615 */ 1616 public function process_url_edit() { 1617 global $CFG, $DB, $PAGE, $OUTPUT; 1618 1619 $blockid = optional_param('bui_editid', null, PARAM_INT); 1620 if (!$blockid) { 1621 return false; 1622 } 1623 1624 require_once($CFG->dirroot . '/blocks/edit_form.php'); 1625 1626 $block = $this->find_instance($blockid); 1627 1628 if (!$block->user_can_edit() && !$this->page->user_can_edit_blocks()) { 1629 throw new moodle_exception('nopermissions', '', $this->page->url->out(), get_string('editblock')); 1630 } 1631 1632 $editpage = new moodle_page(); 1633 $editpage->set_pagelayout('admin'); 1634 $editpage->blocks->show_only_fake_blocks(true); 1635 $editpage->set_course($this->page->course); 1636 //$editpage->set_context($block->context); 1637 $editpage->set_context($this->page->context); 1638 if ($this->page->cm) { 1639 $editpage->set_cm($this->page->cm); 1640 } 1641 $editurlbase = str_replace($CFG->wwwroot . '/', '/', $this->page->url->out_omit_querystring()); 1642 $editurlparams = $this->page->url->params(); 1643 $editurlparams['bui_editid'] = $blockid; 1644 $editpage->set_url($editurlbase, $editurlparams); 1645 $editpage->set_block_actions_done(); 1646 // At this point we are either going to redirect, or display the form, so 1647 // overwrite global $PAGE ready for this. (Formslib refers to it.) 1648 $PAGE = $editpage; 1649 //some functions like MoodleQuickForm::addHelpButton use $OUTPUT so we need to replace that to 1650 $output = $editpage->get_renderer('core'); 1651 $OUTPUT = $output; 1652 1653 $formfile = $CFG->dirroot . '/blocks/' . $block->name() . '/edit_form.php'; 1654 if (is_readable($formfile)) { 1655 require_once($formfile); 1656 $classname = 'block_' . $block->name() . '_edit_form'; 1657 if (!class_exists($classname)) { 1658 $classname = 'block_edit_form'; 1659 } 1660 } else { 1661 $classname = 'block_edit_form'; 1662 } 1663 1664 $mform = new $classname($editpage->url, $block, $this->page); 1665 $mform->set_data($block->instance); 1666 1667 if ($mform->is_cancelled()) { 1668 redirect($this->page->url); 1669 1670 } else if ($data = $mform->get_data()) { 1671 $bi = new stdClass; 1672 $bi->id = $block->instance->id; 1673 1674 // This may get overwritten by the special case handling below. 1675 $bi->pagetypepattern = $data->bui_pagetypepattern; 1676 $bi->showinsubcontexts = (bool) $data->bui_contexts; 1677 if (empty($data->bui_subpagepattern) || $data->bui_subpagepattern == '%@NULL@%') { 1678 $bi->subpagepattern = null; 1679 } else { 1680 $bi->subpagepattern = $data->bui_subpagepattern; 1681 } 1682 1683 $systemcontext = context_system::instance(); 1684 $frontpagecontext = context_course::instance(SITEID); 1685 $parentcontext = context::instance_by_id($data->bui_parentcontextid); 1686 1687 // Updating stickiness and contexts. See MDL-21375 for details. 1688 if (has_capability('moodle/site:manageblocks', $parentcontext)) { // Check permissions in destination 1689 1690 // Explicitly set the default context 1691 $bi->parentcontextid = $parentcontext->id; 1692 1693 if ($data->bui_editingatfrontpage) { // The block is being edited on the front page 1694 1695 // The interface here is a special case because the pagetype pattern is 1696 // totally derived from the context menu. Here are the excpetions. MDL-30340 1697 1698 switch ($data->bui_contexts) { 1699 case BUI_CONTEXTS_ENTIRE_SITE: 1700 // The user wants to show the block across the entire site 1701 $bi->parentcontextid = $systemcontext->id; 1702 $bi->showinsubcontexts = true; 1703 $bi->pagetypepattern = '*'; 1704 break; 1705 case BUI_CONTEXTS_FRONTPAGE_SUBS: 1706 // The user wants the block shown on the front page and all subcontexts 1707 $bi->parentcontextid = $frontpagecontext->id; 1708 $bi->showinsubcontexts = true; 1709 $bi->pagetypepattern = '*'; 1710 break; 1711 case BUI_CONTEXTS_FRONTPAGE_ONLY: 1712 // The user want to show the front page on the frontpage only 1713 $bi->parentcontextid = $frontpagecontext->id; 1714 $bi->showinsubcontexts = false; 1715 $bi->pagetypepattern = 'site-index'; 1716 // This is the only relevant page type anyway but we'll set it explicitly just 1717 // in case the front page grows site-index-* subpages of its own later 1718 break; 1719 } 1720 } 1721 } 1722 1723 $bits = explode('-', $bi->pagetypepattern); 1724 // hacks for some contexts 1725 if (($parentcontext->contextlevel == CONTEXT_COURSE) && ($parentcontext->instanceid != SITEID)) { 1726 // For course context 1727 // is page type pattern is mod-*, change showinsubcontext to 1 1728 if ($bits[0] == 'mod' || $bi->pagetypepattern == '*') { 1729 $bi->showinsubcontexts = 1; 1730 } else { 1731 $bi->showinsubcontexts = 0; 1732 } 1733 } else if ($parentcontext->contextlevel == CONTEXT_USER) { 1734 // for user context 1735 // subpagepattern should be null 1736 if ($bits[0] == 'user' or $bits[0] == 'my') { 1737 // we don't need subpagepattern in usercontext 1738 $bi->subpagepattern = null; 1739 } 1740 } 1741 1742 $bi->defaultregion = $data->bui_defaultregion; 1743 $bi->defaultweight = $data->bui_defaultweight; 1744 $bi->timemodified = time(); 1745 $DB->update_record('block_instances', $bi); 1746 1747 if (!empty($block->config)) { 1748 $config = clone($block->config); 1749 } else { 1750 $config = new stdClass; 1751 } 1752 foreach ($data as $configfield => $value) { 1753 if (strpos($configfield, 'config_') !== 0) { 1754 continue; 1755 } 1756 $field = substr($configfield, 7); 1757 $config->$field = $value; 1758 } 1759 $block->instance_config_save($config); 1760 1761 $bp = new stdClass; 1762 $bp->visible = $data->bui_visible; 1763 $bp->region = $data->bui_region; 1764 $bp->weight = $data->bui_weight; 1765 $needbprecord = !$data->bui_visible || $data->bui_region != $data->bui_defaultregion || 1766 $data->bui_weight != $data->bui_defaultweight; 1767 1768 if ($block->instance->blockpositionid && !$needbprecord) { 1769 $DB->delete_records('block_positions', array('id' => $block->instance->blockpositionid)); 1770 1771 } else if ($block->instance->blockpositionid && $needbprecord) { 1772 $bp->id = $block->instance->blockpositionid; 1773 $DB->update_record('block_positions', $bp); 1774 1775 } else if ($needbprecord) { 1776 $bp->blockinstanceid = $block->instance->id; 1777 $bp->contextid = $this->page->context->id; 1778 $bp->pagetype = $this->page->pagetype; 1779 if ($this->page->subpage) { 1780 $bp->subpage = $this->page->subpage; 1781 } else { 1782 $bp->subpage = ''; 1783 } 1784 $DB->insert_record('block_positions', $bp); 1785 } 1786 1787 redirect($this->page->url); 1788 1789 } else { 1790 $strheading = get_string('blockconfiga', 'moodle', $block->get_title()); 1791 $editpage->set_title($strheading); 1792 $editpage->set_heading($strheading); 1793 $bits = explode('-', $this->page->pagetype); 1794 if ($bits[0] == 'tag' && !empty($this->page->subpage)) { 1795 // better navbar for tag pages 1796 $editpage->navbar->add(get_string('tags'), new moodle_url('/tag/')); 1797 $tag = core_tag_tag::get($this->page->subpage); 1798 // tag search page doesn't have subpageid 1799 if ($tag) { 1800 $editpage->navbar->add($tag->get_display_name(), $tag->get_view_url()); 1801 } 1802 } 1803 $editpage->navbar->add($block->get_title()); 1804 $editpage->navbar->add(get_string('configuration')); 1805 echo $output->header(); 1806 echo $output->heading($strheading, 2); 1807 $mform->display(); 1808 echo $output->footer(); 1809 exit; 1810 } 1811 } 1812 1813 /** 1814 * Handle showing/processing the submission from the block editing form. 1815 * @return boolean true if the form was submitted and the new config saved. Does not 1816 * return if the editing form was displayed. False otherwise. 1817 */ 1818 public function process_url_move() { 1819 global $CFG, $DB, $PAGE; 1820 1821 $blockid = optional_param('bui_moveid', null, PARAM_INT); 1822 if (!$blockid) { 1823 return false; 1824 } 1825 1826 require_sesskey(); 1827 1828 $block = $this->find_instance($blockid); 1829 1830 if (!$this->page->user_can_edit_blocks()) { 1831 throw new moodle_exception('nopermissions', '', $this->page->url->out(), get_string('editblock')); 1832 } 1833 1834 $newregion = optional_param('bui_newregion', '', PARAM_ALPHANUMEXT); 1835 $newweight = optional_param('bui_newweight', null, PARAM_FLOAT); 1836 if (!$newregion || is_null($newweight)) { 1837 // Don't have a valid target position yet, must be just starting the move. 1838 $this->movingblock = $blockid; 1839 $this->page->ensure_param_not_in_url('bui_moveid'); 1840 return false; 1841 } 1842 1843 if (!$this->is_known_region($newregion)) { 1844 throw new moodle_exception('unknownblockregion', '', $this->page->url, $newregion); 1845 } 1846 1847 // Move this block. This may involve moving other nearby blocks. 1848 $blocks = $this->birecordsbyregion[$newregion]; 1849 1850 $maxweight = self::MAX_WEIGHT; 1851 $minweight = -self::MAX_WEIGHT; 1852 1853 // Initialise the used weights and spareweights array with the default values 1854 $spareweights = array(); 1855 $usedweights = array(); 1856 for ($i = $minweight; $i <= $maxweight; $i++) { 1857 $spareweights[$i] = $i; 1858 $usedweights[$i] = array(); 1859 } 1860 1861 // Check each block and sort out where we have used weights 1862 foreach ($blocks as $bi) { 1863 if ($bi->weight > $maxweight) { 1864 // If this statement is true then the blocks weight is more than the 1865 // current maximum. To ensure that we can get the best block position 1866 // we will initialise elements within the usedweights and spareweights 1867 // arrays between the blocks weight (which will then be the new max) and 1868 // the current max 1869 $parseweight = $bi->weight; 1870 while (!array_key_exists($parseweight, $usedweights)) { 1871 $usedweights[$parseweight] = array(); 1872 $spareweights[$parseweight] = $parseweight; 1873 $parseweight--; 1874 } 1875 $maxweight = $bi->weight; 1876 } else if ($bi->weight < $minweight) { 1877 // As above except this time the blocks weight is LESS than the 1878 // the current minimum, so we will initialise the array from the 1879 // blocks weight (new minimum) to the current minimum 1880 $parseweight = $bi->weight; 1881 while (!array_key_exists($parseweight, $usedweights)) { 1882 $usedweights[$parseweight] = array(); 1883 $spareweights[$parseweight] = $parseweight; 1884 $parseweight++; 1885 } 1886 $minweight = $bi->weight; 1887 } 1888 if ($bi->id != $block->instance->id) { 1889 unset($spareweights[$bi->weight]); 1890 $usedweights[$bi->weight][] = $bi->id; 1891 } 1892 } 1893 1894 // First we find the nearest gap in the list of weights. 1895 $bestdistance = max(abs($newweight - self::MAX_WEIGHT), abs($newweight + self::MAX_WEIGHT)) + 1; 1896 $bestgap = null; 1897 foreach ($spareweights as $spareweight) { 1898 if (abs($newweight - $spareweight) < $bestdistance) { 1899 $bestdistance = abs($newweight - $spareweight); 1900 $bestgap = $spareweight; 1901 } 1902 } 1903 1904 // If there is no gap, we have to go outside -self::MAX_WEIGHT .. self::MAX_WEIGHT. 1905 if (is_null($bestgap)) { 1906 $bestgap = self::MAX_WEIGHT + 1; 1907 while (!empty($usedweights[$bestgap])) { 1908 $bestgap++; 1909 } 1910 } 1911 1912 // Now we know the gap we are aiming for, so move all the blocks along. 1913 if ($bestgap < $newweight) { 1914 $newweight = floor($newweight); 1915 for ($weight = $bestgap + 1; $weight <= $newweight; $weight++) { 1916 if (array_key_exists($weight, $usedweights)) { 1917 foreach ($usedweights[$weight] as $biid) { 1918 $this->reposition_block($biid, $newregion, $weight - 1); 1919 } 1920 } 1921 } 1922 $this->reposition_block($block->instance->id, $newregion, $newweight); 1923 } else { 1924 $newweight = ceil($newweight); 1925 for ($weight = $bestgap - 1; $weight >= $newweight; $weight--) { 1926 if (array_key_exists($weight, $usedweights)) { 1927 foreach ($usedweights[$weight] as $biid) { 1928 $this->reposition_block($biid, $newregion, $weight + 1); 1929 } 1930 } 1931 } 1932 $this->reposition_block($block->instance->id, $newregion, $newweight); 1933 } 1934 1935 $this->page->ensure_param_not_in_url('bui_moveid'); 1936 $this->page->ensure_param_not_in_url('bui_newregion'); 1937 $this->page->ensure_param_not_in_url('bui_newweight'); 1938 return true; 1939 } 1940 1941 /** 1942 * Turns the display of normal blocks either on or off. 1943 * 1944 * @param bool $setting 1945 */ 1946 public function show_only_fake_blocks($setting = true) { 1947 $this->fakeblocksonly = $setting; 1948 } 1949 } 1950 1951 /// Helper functions for working with block classes ============================ 1952 1953 /** 1954 * Call a class method (one that does not require a block instance) on a block class. 1955 * 1956 * @param string $blockname the name of the block. 1957 * @param string $method the method name. 1958 * @param array $param parameters to pass to the method. 1959 * @return mixed whatever the method returns. 1960 */ 1961 function block_method_result($blockname, $method, $param = NULL) { 1962 if(!block_load_class($blockname)) { 1963 return NULL; 1964 } 1965 return call_user_func(array('block_'.$blockname, $method), $param); 1966 } 1967 1968 /** 1969 * Returns a new instance of the specified block instance id. 1970 * 1971 * @param int $blockinstanceid 1972 * @return block_base the requested block instance. 1973 */ 1974 function block_instance_by_id($blockinstanceid) { 1975 global $DB; 1976 1977 $blockinstance = $DB->get_record('block_instances', ['id' => $blockinstanceid]); 1978 $instance = block_instance($blockinstance->blockname, $blockinstance); 1979 return $instance; 1980 } 1981 1982 /** 1983 * Creates a new instance of the specified block class. 1984 * 1985 * @param string $blockname the name of the block. 1986 * @param $instance block_instances DB table row (optional). 1987 * @param moodle_page $page the page this block is appearing on. 1988 * @return block_base the requested block instance. 1989 */ 1990 function block_instance($blockname, $instance = NULL, $page = NULL) { 1991 if(!block_load_class($blockname)) { 1992 return false; 1993 } 1994 $classname = 'block_'.$blockname; 1995 $retval = new $classname; 1996 if($instance !== NULL) { 1997 if (is_null($page)) { 1998 global $PAGE; 1999 $page = $PAGE; 2000 } 2001 $retval->_load_instance($instance, $page); 2002 } 2003 return $retval; 2004 } 2005 2006 /** 2007 * Load the block class for a particular type of block. 2008 * 2009 * @param string $blockname the name of the block. 2010 * @return boolean success or failure. 2011 */ 2012 function block_load_class($blockname) { 2013 global $CFG; 2014 2015 if(empty($blockname)) { 2016 return false; 2017 } 2018 2019 $classname = 'block_'.$blockname; 2020 2021 if(class_exists($classname)) { 2022 return true; 2023 } 2024 2025 $blockpath = $CFG->dirroot.'/blocks/'.$blockname.'/block_'.$blockname.'.php'; 2026 2027 if (file_exists($blockpath)) { 2028 require_once($CFG->dirroot.'/blocks/moodleblock.class.php'); 2029 include_once($blockpath); 2030 }else{ 2031 //debugging("$blockname code does not exist in $blockpath", DEBUG_DEVELOPER); 2032 return false; 2033 } 2034 2035 return class_exists($classname); 2036 } 2037 2038 /** 2039 * Given a specific page type, return all the page type patterns that might 2040 * match it. 2041 * 2042 * @param string $pagetype for example 'course-view-weeks' or 'mod-quiz-view'. 2043 * @return array an array of all the page type patterns that might match this page type. 2044 */ 2045 function matching_page_type_patterns($pagetype) { 2046 $patterns = array($pagetype); 2047 $bits = explode('-', $pagetype); 2048 if (count($bits) == 3 && $bits[0] == 'mod') { 2049 if ($bits[2] == 'view') { 2050 $patterns[] = 'mod-*-view'; 2051 } else if ($bits[2] == 'index') { 2052 $patterns[] = 'mod-*-index'; 2053 } 2054 } 2055 while (count($bits) > 0) { 2056 $patterns[] = implode('-', $bits) . '-*'; 2057 array_pop($bits); 2058 } 2059 $patterns[] = '*'; 2060 return $patterns; 2061 } 2062 2063 /** 2064 * Give an specific pattern, return all the page type patterns that would also match it. 2065 * 2066 * @param string $pattern the pattern, e.g. 'mod-forum-*' or 'mod-quiz-view'. 2067 * @return array of all the page type patterns matching. 2068 */ 2069 function matching_page_type_patterns_from_pattern($pattern) { 2070 $patterns = array($pattern); 2071 if ($pattern === '*') { 2072 return $patterns; 2073 } 2074 2075 // Only keep the part before the star because we will append -* to all the bits. 2076 $star = strpos($pattern, '-*'); 2077 if ($star !== false) { 2078 $pattern = substr($pattern, 0, $star); 2079 } 2080 2081 $patterns = array_merge($patterns, matching_page_type_patterns($pattern)); 2082 $patterns = array_unique($patterns); 2083 2084 return $patterns; 2085 } 2086 2087 /** 2088 * Given a specific page type, parent context and currect context, return all the page type patterns 2089 * that might be used by this block. 2090 * 2091 * @param string $pagetype for example 'course-view-weeks' or 'mod-quiz-view'. 2092 * @param stdClass $parentcontext Block's parent context 2093 * @param stdClass $currentcontext Current context of block 2094 * @return array an array of all the page type patterns that might match this page type. 2095 */ 2096 function generate_page_type_patterns($pagetype, $parentcontext = null, $currentcontext = null) { 2097 global $CFG; // Required for includes bellow. 2098 2099 $bits = explode('-', $pagetype); 2100 2101 $core = core_component::get_core_subsystems(); 2102 $plugins = core_component::get_plugin_types(); 2103 2104 //progressively strip pieces off the page type looking for a match 2105 $componentarray = null; 2106 for ($i = count($bits); $i > 0; $i--) { 2107 $possiblecomponentarray = array_slice($bits, 0, $i); 2108 $possiblecomponent = implode('', $possiblecomponentarray); 2109 2110 // Check to see if the component is a core component 2111 if (array_key_exists($possiblecomponent, $core) && !empty($core[$possiblecomponent])) { 2112 $libfile = $core[$possiblecomponent].'/lib.php'; 2113 if (file_exists($libfile)) { 2114 require_once($libfile); 2115 $function = $possiblecomponent.'_page_type_list'; 2116 if (function_exists($function)) { 2117 if ($patterns = $function($pagetype, $parentcontext, $currentcontext)) { 2118 break; 2119 } 2120 } 2121 } 2122 } 2123 2124 //check the plugin directory and look for a callback 2125 if (array_key_exists($possiblecomponent, $plugins) && !empty($plugins[$possiblecomponent])) { 2126 2127 //We've found a plugin type. Look for a plugin name by getting the next section of page type 2128 if (count($bits) > $i) { 2129 $pluginname = $bits[$i]; 2130 $directory = core_component::get_plugin_directory($possiblecomponent, $pluginname); 2131 if (!empty($directory)){ 2132 $libfile = $directory.'/lib.php'; 2133 if (file_exists($libfile)) { 2134 require_once($libfile); 2135 $function = $possiblecomponent.'_'.$pluginname.'_page_type_list'; 2136 if (!function_exists($function)) { 2137 $function = $pluginname.'_page_type_list'; 2138 } 2139 if (function_exists($function)) { 2140 if ($patterns = $function($pagetype, $parentcontext, $currentcontext)) { 2141 break; 2142 } 2143 } 2144 } 2145 } 2146 } 2147 2148 //we'll only get to here if we still don't have any patterns 2149 //the plugin type may have a callback 2150 $directory = $plugins[$possiblecomponent]; 2151 $libfile = $directory.'/lib.php'; 2152 if (file_exists($libfile)) { 2153 require_once($libfile); 2154 $function = $possiblecomponent.'_page_type_list'; 2155 if (function_exists($function)) { 2156 if ($patterns = $function($pagetype, $parentcontext, $currentcontext)) { 2157 break; 2158 } 2159 } 2160 } 2161 } 2162 } 2163 2164 if (empty($patterns)) { 2165 $patterns = default_page_type_list($pagetype, $parentcontext, $currentcontext); 2166 } 2167 2168 // Ensure that the * pattern is always available if editing block 'at distance', so 2169 // we always can 'bring back' it to the original context. MDL-30340 2170 if ((!isset($currentcontext) or !isset($parentcontext) or $currentcontext->id != $parentcontext->id) && !isset($patterns['*'])) { 2171 // TODO: We could change the string here, showing its 'bring back' meaning 2172 $patterns['*'] = get_string('page-x', 'pagetype'); 2173 } 2174 2175 return $patterns; 2176 } 2177 2178 /** 2179 * Generates a default page type list when a more appropriate callback cannot be decided upon. 2180 * 2181 * @param string $pagetype 2182 * @param stdClass $parentcontext 2183 * @param stdClass $currentcontext 2184 * @return array 2185 */ 2186 function default_page_type_list($pagetype, $parentcontext = null, $currentcontext = null) { 2187 // Generate page type patterns based on current page type if 2188 // callbacks haven't been defined 2189 $patterns = array($pagetype => $pagetype); 2190 $bits = explode('-', $pagetype); 2191 while (count($bits) > 0) { 2192 $pattern = implode('-', $bits) . '-*'; 2193 $pagetypestringname = 'page-'.str_replace('*', 'x', $pattern); 2194 // guessing page type description 2195 if (get_string_manager()->string_exists($pagetypestringname, 'pagetype')) { 2196 $patterns[$pattern] = get_string($pagetypestringname, 'pagetype'); 2197 } else { 2198 $patterns[$pattern] = $pattern; 2199 } 2200 array_pop($bits); 2201 } 2202 $patterns['*'] = get_string('page-x', 'pagetype'); 2203 return $patterns; 2204 } 2205 2206 /** 2207 * Generates the page type list for the my moodle page 2208 * 2209 * @param string $pagetype 2210 * @param stdClass $parentcontext 2211 * @param stdClass $currentcontext 2212 * @return array 2213 */ 2214 function my_page_type_list($pagetype, $parentcontext = null, $currentcontext = null) { 2215 return array('my-index' => get_string('page-my-index', 'pagetype')); 2216 } 2217 2218 /** 2219 * Generates the page type list for a module by either locating and using the modules callback 2220 * or by generating a default list. 2221 * 2222 * @param string $pagetype 2223 * @param stdClass $parentcontext 2224 * @param stdClass $currentcontext 2225 * @return array 2226 */ 2227 function mod_page_type_list($pagetype, $parentcontext = null, $currentcontext = null) { 2228 $patterns = plugin_page_type_list($pagetype, $parentcontext, $currentcontext); 2229 if (empty($patterns)) { 2230 // if modules don't have callbacks 2231 // generate two default page type patterns for modules only 2232 $bits = explode('-', $pagetype); 2233 $patterns = array($pagetype => $pagetype); 2234 if ($bits[2] == 'view') { 2235 $patterns['mod-*-view'] = get_string('page-mod-x-view', 'pagetype'); 2236 } else if ($bits[2] == 'index') { 2237 $patterns['mod-*-index'] = get_string('page-mod-x-index', 'pagetype'); 2238 } 2239 } 2240 return $patterns; 2241 } 2242 /// Functions update the blocks if required by the request parameters ========== 2243 2244 /** 2245 * Return a {@link block_contents} representing the add a new block UI, if 2246 * this user is allowed to see it. 2247 * 2248 * @return block_contents an appropriate block_contents, or null if the user 2249 * cannot add any blocks here. 2250 */ 2251 function block_add_block_ui($page, $output) { 2252 global $CFG, $OUTPUT; 2253 if (!$page->user_is_editing() || !$page->user_can_edit_blocks()) { 2254 return null; 2255 } 2256 2257 $bc = new block_contents(); 2258 $bc->title = get_string('addblock'); 2259 $bc->add_class('block_adminblock'); 2260 $bc->attributes['data-block'] = 'adminblock'; 2261 2262 $missingblocks = $page->blocks->get_addable_blocks(); 2263 if (empty($missingblocks)) { 2264 $bc->content = get_string('noblockstoaddhere'); 2265 return $bc; 2266 } 2267 2268 $menu = array(); 2269 foreach ($missingblocks as $block) { 2270 $menu[$block->name] = $block->title; 2271 } 2272 2273 $actionurl = new moodle_url($page->url, array('sesskey'=>sesskey())); 2274 $select = new single_select($actionurl, 'bui_addblock', $menu, null, array(''=>get_string('adddots')), 'add_block'); 2275 $select->set_label(get_string('addblock'), array('class'=>'accesshide')); 2276 $bc->content = $OUTPUT->render($select); 2277 return $bc; 2278 } 2279 2280 /** 2281 * Actually delete from the database any blocks that are currently on this page, 2282 * but which should not be there according to blocks_name_allowed_in_format. 2283 * 2284 * @todo Write/Fix this function. Currently returns immediately 2285 * @param $course 2286 */ 2287 function blocks_remove_inappropriate($course) { 2288 // TODO 2289 return; 2290 /* 2291 $blockmanager = blocks_get_by_page($page); 2292 2293 if (empty($blockmanager)) { 2294 return; 2295 } 2296 2297 if (($pageformat = $page->pagetype) == NULL) { 2298 return; 2299 } 2300 2301 foreach($blockmanager as $region) { 2302 foreach($region as $instance) { 2303 $block = blocks_get_record($instance->blockid); 2304 if(!blocks_name_allowed_in_format($block->name, $pageformat)) { 2305 blocks_delete_instance($instance->instance); 2306 } 2307 } 2308 }*/ 2309 } 2310 2311 /** 2312 * Check that a given name is in a permittable format 2313 * 2314 * @param string $name 2315 * @param string $pageformat 2316 * @return bool 2317 */ 2318 function blocks_name_allowed_in_format($name, $pageformat) { 2319 $accept = NULL; 2320 $maxdepth = -1; 2321 if (!$bi = block_instance($name)) { 2322 return false; 2323 } 2324 2325 $formats = $bi->applicable_formats(); 2326 if (!$formats) { 2327 $formats = array(); 2328 } 2329 foreach ($formats as $format => $allowed) { 2330 $formatregex = '/^'.str_replace('*', '[^-]*', $format).'.*$/'; 2331 $depth = substr_count($format, '-'); 2332 if (preg_match($formatregex, $pageformat) && $depth > $maxdepth) { 2333 $maxdepth = $depth; 2334 $accept = $allowed; 2335 } 2336 } 2337 if ($accept === NULL) { 2338 $accept = !empty($formats['all']); 2339 } 2340 return $accept; 2341 } 2342 2343 /** 2344 * Delete a block, and associated data. 2345 * 2346 * @param object $instance a row from the block_instances table 2347 * @param bool $nolongerused legacy parameter. Not used, but kept for backwards compatibility. 2348 * @param bool $skipblockstables for internal use only. Makes @see blocks_delete_all_for_context() more efficient. 2349 */ 2350 function blocks_delete_instance($instance, $nolongerused = false, $skipblockstables = false) { 2351 global $DB; 2352 2353 // Allow plugins to use this block before we completely delete it. 2354 if ($pluginsfunction = get_plugins_with_function('pre_block_delete')) { 2355 foreach ($pluginsfunction as $plugintype => $plugins) { 2356 foreach ($plugins as $pluginfunction) { 2357 $pluginfunction($instance); 2358 } 2359 } 2360 } 2361 2362 if ($block = block_instance($instance->blockname, $instance)) { 2363 $block->instance_delete(); 2364 } 2365 context_helper::delete_instance(CONTEXT_BLOCK, $instance->id); 2366 2367 if (!$skipblockstables) { 2368 $DB->delete_records('block_positions', array('blockinstanceid' => $instance->id)); 2369 $DB->delete_records('block_instances', array('id' => $instance->id)); 2370 $DB->delete_records_list('user_preferences', 'name', array('block'.$instance->id.'hidden','docked_block_instance_'.$instance->id)); 2371 } 2372 } 2373 2374 /** 2375 * Delete multiple blocks at once. 2376 * 2377 * @param array $instanceids A list of block instance ID. 2378 */ 2379 function blocks_delete_instances($instanceids) { 2380 global $DB; 2381 2382 $limit = 1000; 2383 $count = count($instanceids); 2384 $chunks = [$instanceids]; 2385 if ($count > $limit) { 2386 $chunks = array_chunk($instanceids, $limit); 2387 } 2388 2389 // Perform deletion for each chunk. 2390 foreach ($chunks as $chunk) { 2391 $instances = $DB->get_recordset_list('block_instances', 'id', $chunk); 2392 foreach ($instances as $instance) { 2393 blocks_delete_instance($instance, false, true); 2394 } 2395 $instances->close(); 2396 2397 $DB->delete_records_list('block_positions', 'blockinstanceid', $chunk); 2398 $DB->delete_records_list('block_instances', 'id', $chunk); 2399 2400 $preferences = array(); 2401 foreach ($chunk as $instanceid) { 2402 $preferences[] = 'block' . $instanceid . 'hidden'; 2403 $preferences[] = 'docked_block_instance_' . $instanceid; 2404 } 2405 $DB->delete_records_list('user_preferences', 'name', $preferences); 2406 } 2407 } 2408 2409 /** 2410 * Delete all the blocks that belong to a particular context. 2411 * 2412 * @param int $contextid the context id. 2413 */ 2414 function blocks_delete_all_for_context($contextid) { 2415 global $DB; 2416 $instances = $DB->get_recordset('block_instances', array('parentcontextid' => $contextid)); 2417 foreach ($instances as $instance) { 2418 blocks_delete_instance($instance, true); 2419 } 2420 $instances->close(); 2421 $DB->delete_records('block_instances', array('parentcontextid' => $contextid)); 2422 $DB->delete_records('block_positions', array('contextid' => $contextid)); 2423 } 2424 2425 /** 2426 * Set a block to be visible or hidden on a particular page. 2427 * 2428 * @param object $instance a row from the block_instances, preferably LEFT JOINed with the 2429 * block_positions table as return by block_manager. 2430 * @param moodle_page $page the back to set the visibility with respect to. 2431 * @param integer $newvisibility 1 for visible, 0 for hidden. 2432 */ 2433 function blocks_set_visibility($instance, $page, $newvisibility) { 2434 global $DB; 2435 if (!empty($instance->blockpositionid)) { 2436 // Already have local information on this page. 2437 $DB->set_field('block_positions', 'visible', $newvisibility, array('id' => $instance->blockpositionid)); 2438 return; 2439 } 2440 2441 // Create a new block_positions record. 2442 $bp = new stdClass; 2443 $bp->blockinstanceid = $instance->id; 2444 $bp->contextid = $page->context->id; 2445 $bp->pagetype = $page->pagetype; 2446 if ($page->subpage) { 2447 $bp->subpage = $page->subpage; 2448 } 2449 $bp->visible = $newvisibility; 2450 $bp->region = $instance->defaultregion; 2451 $bp->weight = $instance->defaultweight; 2452 $DB->insert_record('block_positions', $bp); 2453 } 2454 2455 /** 2456 * Get the block record for a particular blockid - that is, a particular type os block. 2457 * 2458 * @param $int blockid block type id. If null, an array of all block types is returned. 2459 * @param bool $notusedanymore No longer used. 2460 * @return array|object row from block table, or all rows. 2461 */ 2462 function blocks_get_record($blockid = NULL, $notusedanymore = false) { 2463 global $PAGE; 2464 $blocks = $PAGE->blocks->get_installed_blocks(); 2465 if ($blockid === NULL) { 2466 return $blocks; 2467 } else if (isset($blocks[$blockid])) { 2468 return $blocks[$blockid]; 2469 } else { 2470 return false; 2471 } 2472 } 2473 2474 /** 2475 * Find a given block by its blockid within a provide array 2476 * 2477 * @param int $blockid 2478 * @param array $blocksarray 2479 * @return bool|object Instance if found else false 2480 */ 2481 function blocks_find_block($blockid, $blocksarray) { 2482 if (empty($blocksarray)) { 2483 return false; 2484 } 2485 foreach($blocksarray as $blockgroup) { 2486 if (empty($blockgroup)) { 2487 continue; 2488 } 2489 foreach($blockgroup as $instance) { 2490 if($instance->blockid == $blockid) { 2491 return $instance; 2492 } 2493 } 2494 } 2495 return false; 2496 } 2497 2498 // Functions for programatically adding default blocks to pages ================ 2499 2500 /** 2501 * Parse a list of default blocks. See config-dist for a description of the format. 2502 * 2503 * @param string $blocksstr Determines the starting point that the blocks are added in the region. 2504 * @return array the parsed list of default blocks 2505 */ 2506 function blocks_parse_default_blocks_list($blocksstr) { 2507 $blocks = array(); 2508 $bits = explode(':', $blocksstr); 2509 if (!empty($bits)) { 2510 $leftbits = trim(array_shift($bits)); 2511 if ($leftbits != '') { 2512 $blocks[BLOCK_POS_LEFT] = explode(',', $leftbits); 2513 } 2514 } 2515 if (!empty($bits)) { 2516 $rightbits = trim(array_shift($bits)); 2517 if ($rightbits != '') { 2518 $blocks[BLOCK_POS_RIGHT] = explode(',', $rightbits); 2519 } 2520 } 2521 return $blocks; 2522 } 2523 2524 /** 2525 * @return array the blocks that should be added to the site course by default. 2526 */ 2527 function blocks_get_default_site_course_blocks() { 2528 global $CFG; 2529 2530 if (isset($CFG->defaultblocks_site)) { 2531 return blocks_parse_default_blocks_list($CFG->defaultblocks_site); 2532 } else { 2533 return array( 2534 BLOCK_POS_LEFT => array(), 2535 BLOCK_POS_RIGHT => array() 2536 ); 2537 } 2538 } 2539 2540 /** 2541 * Add the default blocks to a course. 2542 * 2543 * @param object $course a course object. 2544 */ 2545 function blocks_add_default_course_blocks($course) { 2546 global $CFG; 2547 2548 if (isset($CFG->defaultblocks_override)) { 2549 $blocknames = blocks_parse_default_blocks_list($CFG->defaultblocks_override); 2550 2551 } else if ($course->id == SITEID) { 2552 $blocknames = blocks_get_default_site_course_blocks(); 2553 2554 } else if (isset($CFG->{'defaultblocks_' . $course->format})) { 2555 $blocknames = blocks_parse_default_blocks_list($CFG->{'defaultblocks_' . $course->format}); 2556 2557 } else { 2558 require_once($CFG->dirroot. '/course/lib.php'); 2559 $blocknames = course_get_format($course)->get_default_blocks(); 2560 2561 } 2562 2563 if ($course->id == SITEID) { 2564 $pagetypepattern = 'site-index'; 2565 } else { 2566 $pagetypepattern = 'course-view-*'; 2567 } 2568 $page = new moodle_page(); 2569 $page->set_course($course); 2570 $page->blocks->add_blocks($blocknames, $pagetypepattern); 2571 } 2572 2573 /** 2574 * Add the default system-context blocks. E.g. the admin tree. 2575 */ 2576 function blocks_add_default_system_blocks() { 2577 global $DB; 2578 2579 $page = new moodle_page(); 2580 $page->set_context(context_system::instance()); 2581 // We don't add blocks required by the theme, they will be auto-created. 2582 $page->blocks->add_blocks(array(BLOCK_POS_LEFT => array('admin_bookmarks')), 'admin-*', null, null, 2); 2583 2584 if ($defaultmypage = $DB->get_record('my_pages', array('userid' => null, 'name' => '__default', 'private' => 1))) { 2585 $subpagepattern = $defaultmypage->id; 2586 } else { 2587 $subpagepattern = null; 2588 } 2589 2590 $newblocks = array('timeline', 'private_files', 'online_users', 'badges', 'calendar_month', 'calendar_upcoming'); 2591 $newcontent = array('lp', 'recentlyaccessedcourses', 'myoverview'); 2592 $page->blocks->add_blocks(array(BLOCK_POS_RIGHT => $newblocks, 'content' => $newcontent), 'my-index', $subpagepattern); 2593 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body