8000 moodle/lib/blocklib.php at v2.8.11 · newdominic/moodle · GitHub
[go: up one dir, main page]

Skip to content

Latest commit

 

History

History
2225 lines (1971 loc) · 84.5 KB

blocklib.php

File metadata and controls

2225 lines (1971 loc) · 84.5 KB
< 10000 div class="codeBlobWrapper__v-bhvc" style="padding-top:8px;padding-bottom:8px">
}
}
// Surely other pages like course-report will need this too, they just are not important
// enough now. This will be decided in the coming days. (MDL-27829, MDL-28150)
$this->add_block($blockname, $defaulregion, $weight, false, $pagetypepattern, $subpage);
}
/**
* Convenience method, calls add_block repeatedly for all the blocks in $blocks. Optionally, a starting weight
* can be used to decide the starting point that blocks are added in the region, the weight is passed to {@link add_block}
* and incremented by the position of the block in the $blocks array
*
* @param array $blocks array with array keys the region names, and values an array of block names.
* @param string $pagetypepattern optional. Passed to {@link add_block()}
* @param string $subpagepattern optional. Passed to {@link add_block()}
* @param boolean $showinsubcontexts optional. Passed to {@link add_block()}
* @param integer $weight optional. Determines the starting point that the blocks are added in the region.
*/
public function add_blocks($blocks, $pagetypepattern = NULL, $subpagepattern = NULL, $showinsubcontexts=false, $weight=0) {
$initialweight = $weight;
$this->add_regions(array_keys($blocks), false);
foreach ($blocks as $region => $regionblocks) {
foreach ($regionblocks as $offset => $blockname) {
$weight = $initialweight + $offset;
$this->add_block($blockname, $region, $weight, $showinsubcontexts, $pagetypepattern, $subpagepattern);
}
}
}
/**
* Move a block to a new position on this page.
*
* If this block cannot appear on any other pages, then we change defaultposition/weight
* in the block_instances table. Otherwise we just set the position on this page.
*
* @param $blockinstanceid the block instance id.
* @param $newregion the new region name.
* @param $newweight the new weight.
*/
public function reposition_block($blockinstanceid, $newregion, $newweight) {
global $DB;
$this->check_region_is_known($newregion);
$inst = $this->find_instance($blockinstanceid);
$bi = $inst->instance;
if ($bi->weight == $bi->defaultweight && $bi->region == $bi->defaultregion &&
!$bi->showinsubcontexts && strpos($bi->pagetypepattern, '*') === false &&
(!$this->page->subpage || $bi->subpagepattern)) {
// Set default position
$newbi = new stdClass;
$newbi->id = $bi->id;
$newbi->defaultregion = $newregion;
$newbi->defaultweight = $newweight;
$DB->update_record('block_instances', $newbi);
if ($bi->blockpositionid) {
$bp = new stdClass;
$bp->id = $bi->blockpositionid;
$bp->region = $newregion;
$bp->weight = $newweight;
$DB->update_record('block_positions', $bp);
}
} else {
// Just set position on this page.
$bp = new stdClass;
$bp->region = $newregion;
$bp->weight = $newweight;
if ($bi->blockpositionid) {
$bp->id = $bi->blockpositionid;
$DB->update_record('block_positions', $bp);
} else {
$bp->blockinstanceid = $bi->id;
$bp->contextid = $this->page->context->id;
$bp->pagetype = $this->page->pagetype;
if ($this->page->subpage) {
$bp->subpage = $this->page->subpage;
} else {
$bp->subpage = '';
}
$bp->visible = $bi->visible;
$DB->insert_record('block_positions', $bp);
}
}
}
/**
* Find a given block by its instance id
*
* @param integer $instanceid
* @return block_base
*/
public function find_instance($instanceid) {
foreach ($this->regions as $region => $notused) {
$this->ensure_instances_exist($region);
foreach($this->blockinstances[$region] as $instance) {
if ($instance->instance->id == $instanceid) {
return $instance;
}
}
}
throw new block_not_on_page_exception($instanceid, $this->page);
}
/// Inner workings =============================================================
/**
* Check whether the page blocks have been loaded yet
*
* @return void Throws coding exception if already loaded
*/
protected function check_not_yet_loaded() {
if (!is_null($this->birecordsbyregion)) {
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.');
}
}
/**
* Check whether the page blocks have been loaded yet
*
* Nearly identical to the above function {@link check_not_yet_loaded()} except different message
*
* @return void Throws coding exception if already loaded
*/
protected function check_is_loaded() {
if (is_null($this->birecordsbyregion)) {
throw new coding_exception('block_manager has not yet loaded the blocks, to it is too soon to request the information you asked for.');
}
}
/**
* Check if a block type is known and usable
*
* @param string $blockname The block type name to search for
* @param bool $includeinvisible Include disabled block types in the initial pass
* @return void Coding Exception thrown if unknown or not enabled
*/
protected function check_known_block_type($blockname, $includeinvisible = false) {
if (!$this->is_known_block_type($blockname, $includeinvisible)) {
if ($this->is_known_block_type($blockname, true)) {
throw new coding_exception('Unknown block type ' . $blockname);
} else {
throw new coding_exception('Block type ' . $blockname . ' has been disabled by the administrator.');
}
}
}
/**
* Check if a region is known by its name
*
* @param string $region
* @return void Coding Exception thrown if the region is not known
*/
protected function check_region_is_known($region) {
if (!$this->is_known_region($region)) {
throw new coding_exception('Trying to reference an unknown block region ' . $region);
}
}
/**
* Returns an array of region names as keys and nested arrays for values
*
* @return array an array where the array keys are the region names, and the array
* values are empty arrays.
*/
protected function prepare_per_region_arrays() {
$result = array();
foreach ($this->regions as $region => $notused) {
$result[$region] = array();
}
return $result;
}
/**
* Create a set of new block instance from a record array
*
* @param array $birecords An array of block instance records
* @return array An array of instantiated block_instance objects
*/
protected function create_block_instances($birecords) {
$results = array();
foreach ($birecords as $record) {
if ($blockobject = block_instance($record->blockname, $record, $this->page)) {
$results[] = $blockobject;
}
}
return $results;
}
/**
* Create all the block instances for all the blocks that were loaded by
* load_blocks. This is used, for example, to ensure that all blocks get a
* chance to initialise themselves via the {@link block_base::specialize()}
* method, before any output is done.
*/
public function create_all_block_instances() {
foreach ($this->get_regions() as $region) {
$this->ensure_instances_exist($region);
}
}
/**
* Return an array of content objects from a set of block instances
*
* @param array $instances An array of block instances
* @param renderer_base The renderer to use.
* @param string $region the region name.
* @return array An array of block_content (and possibly block_move_target) objects.
*/
protected function create_block_contents($instances, $output, $region) {
$results = array();
$lastweight = 0;
$lastblock = 0;
if ($this->movingblock) {
$first = reset($instances);
if ($first) {
$lastweight = $first->instance->weight - 2;
}
}
foreach ($instances as $instance) {
$content = $instance->get_content_for_output($output);
if (empty($content)) {
continue;
}
if ($this->movingblock && $lastweight != $instance->instance->weight &&
$content->blockinstanceid != $this->movingblock && $lastblock != $this->movingblock) {
$results[] = new block_move_target($this->get_move_target_url($region, ($lastweight + $instance->instance->weight)/2));
}
if ($content->blockinstanceid == $this->movingblock) {
$content->add_class('beingmoved');
$content->annotation .= get_string('movingthisblockcancel', 'block',
html_writer::link($this->page->url, get_string('cancel')));
}
$results[] = $content;
$lastweight = $instance->instance->weight;
$lastblock = $instance->instance->id;
}
if ($this->movingblock && $lastblock != $this->movingblock) {
$results[] = new block_move_target($this->get_move_target_url($region, $lastweight + 1));
}
return $results;
}
/**
* Ensure block instances exist for a given region
*
2BC5
0