function definition() { $mform =& $this->_form; // First show fields specific to this type of block. $this->specific_definition($mform); // Then show the fields about where this block appears. $mform->addElement('header', 'whereheader', get_string('wherethisblockappears', 'block')); // If the current weight of the block is out-of-range, add that option in. $blockweight = $this->block->instance->weight; $weightoptions = array(); if ($blockweight < -block_manager::MAX_WEIGHT) { $weightoptions[$blockweight] = $blockweight; } for ($i = -block_manager::MAX_WEIGHT; $i <= block_manager::MAX_WEIGHT; $i++) { $weightoptions[$i] = $i; } if ($blockweight > block_manager::MAX_WEIGHT) { $weightoptions[$blockweight] = $blockweight; } $first = reset($weightoptions); $weightoptions[$first] = get_string('bracketfirst', 'block', $first); $last = end($weightoptions); $weightoptions[$last] = get_string('bracketlast', 'block', $last); $regionoptions = $this->page->theme->get_all_block_regions(); $parentcontext = get_context_instance_by_id($this->block->instance->parentcontextid); $mform->addElement('static', 'contextname', get_string('thisblockbelongsto', 'block'), print_context_name($parentcontext)); $mform->addElement('selectyesno', 'bui_showinsubcontexts', get_string('appearsinsubcontexts', 'block')); $pagetypeoptions = matching_page_type_patterns($this->page->pagetype); $pagetypeoptions = array_combine($pagetypeoptions, $pagetypeoptions); $mform->addElement('select', 'bui_pagetypepattern', get_string('pagetypes', 'block'), $pagetypeoptions); if ($this->page->subpage) { $subpageoptions = array('%@NULL@%' => get_string('anypagematchingtheabove', 'block'), $this->page->subpage => get_string('thisspecificpage', 'block', $this->page->subpage)); $mform->addElement('select', 'bui_subpagepattern', get_string('subpages', 'block'), $subpageoptions); } $defaultregionoptions = $regionoptions; $defaultregion = $this->block->instance->defaultregion; if (!array_key_exists($defaultregion, $defaultregionoptions)) { $defaultregionoptions[$defaultregion] = $defaultregion; } $mform->addElement('select', 'bui_defaultregion', get_string('defaultregion', 'block'), $defaultregionoptions); $mform->addElement('select', 'bui_defaultweight', get_string('defaultweight', 'block'), $weightoptions); // Where this block is positioned on this page. $mform->addElement('header', 'whereheader', get_string('onthispage', 'block')); $mform->addElement('selectyesno', 'bui_visible', get_string('visible', 'block')); $blockregion = $this->block->instance->region; if (!array_key_exists($blockregion, $regionoptions)) { $regionoptions[$blockregion] = $blockregion; } $mform->addElement('select', 'bui_region', get_string('region', 'block'), $regionoptions); $mform->addElement('select', 'bui_weight', get_string('weight', 'block'), $weightoptions); $pagefields = array('bui_visible', 'bui_region', 'bui_weight'); if (!$this->block->user_can_edit()) { $mform->hardFreezeAllVisibleExcept($pagefields); } if (!$this->page->user_can_edit_blocks()) { $mform->hardFreeze($pagefields); } $this->add_action_buttons(); }
/** * This method actually loads the blocks for our page from the database. * * @param boolean|null $includeinvisible * null (default) - load hidden blocks if $this->page->user_is_editing(); * true - load hidden blocks. * false - don't load hidden blocks. */ public function load_blocks($includeinvisible = null) { global $DB, $CFG; if (!is_null($this->birecordsbyregion)) { // Already done. return; } if ($CFG->version < 2009050619) { // Upgrade/install not complete. Don't try too show any blocks. $this->birecordsbyregion = array(); return; } // Ensure we have been initialised. if (is_null($this->defaultregion)) { $this->page->initialise_theme_and_output(); // If there are still no block regions, then there are no blocks on this page. if (empty($this->regions)) { $this->birecordsbyregion = array(); return; } } // Check if we need to load normal blocks if ($this->fakeblocksonly) { $this->birecordsbyregion = $this->prepare_per_region_arrays(); return; } if (is_null($includeinvisible)) { $includeinvisible = $this->page->user_is_editing(); } if ($includeinvisible) { $visiblecheck = ''; } else { $visiblecheck = 'AND (bp.visible = 1 OR bp.visible IS NULL)'; } $context = $this->page->context; $contexttest = 'bi.parentcontextid = :contextid2'; $parentcontextparams = array(); $parentcontextids = get_parent_contexts($context); if ($parentcontextids) { list($parentcontexttest, $parentcontextparams) = $DB->get_in_or_equal($parentcontextids, SQL_PARAMS_NAMED, 'parentcontext'); $contexttest = "({$contexttest} OR (bi.showinsubcontexts = 1 AND bi.parentcontextid {$parentcontexttest}))"; } $pagetypepatterns = matching_page_type_patterns($this->page->pagetype); list($pagetypepatterntest, $pagetypepatternparams) = $DB->get_in_or_equal($pagetypepatterns, SQL_PARAMS_NAMED, 'pagetypepatterntest'); list($ccselect, $ccjoin) = context_instance_preload_sql('bi.id', CONTEXT_BLOCK, 'ctx'); $params = array('subpage1' => $this->page->subpage, 'subpage2' => $this->page->subpage, 'contextid1' => $context->id, 'contextid2' => $context->id, 'pagetype' => $this->page->pagetype); if ($this->page->subpage === '') { $params['subpage1'] = $DB->sql_empty(); $params['subpage2'] = $DB->sql_empty(); } $sql = "SELECT\n bi.id,\n bp.id AS blockpositionid,\n bi.blockname,\n bi.parentcontextid,\n bi.showinsubcontexts,\n bi.pagetypepattern,\n bi.subpagepattern,\n bi.defaultregion,\n bi.defaultweight,\n COALESCE(bp.visible, 1) AS visible,\n COALESCE(bp.region, bi.defaultregion) AS region,\n COALESCE(bp.weight, bi.defaultweight) AS weight,\n bi.configdata\n {$ccselect}\n\n FROM {block_instances} bi\n JOIN {block} b ON bi.blockname = b.name\n LEFT JOIN {block_positions} bp ON bp.blockinstanceid = bi.id\n AND bp.contextid = :contextid1\n AND bp.pagetype = :pagetype\n AND bp.subpage = :subpage1\n {$ccjoin}\n\n WHERE\n {$contexttest}\n AND bi.pagetypepattern {$pagetypepatterntest}\n AND (bi.subpagepattern IS NULL OR bi.subpagepattern = :subpage2)\n {$visiblecheck}\n AND b.visible = 1\n\n ORDER BY\n COALESCE(bp.region, bi.defaultregion),\n COALESCE(bp.weight, bi.defaultweight),\n bi.id"; $blockinstances = $DB->get_recordset_sql($sql, $params + $parentcontextparams + $pagetypepatternparams); $this->birecordsbyregion = $this->prepare_per_region_arrays(); $unknown = array(); foreach ($blockinstances as $bi) { context_instance_preload($bi); if ($this->is_known_region($bi->region)) { $this->birecordsbyregion[$bi->region][] = $bi; } else { $unknown[] = $bi; } } // Pages don't necessarily have a defaultregion. The one time this can // happen is when there are no theme block regions, but the script itself // has a block region in the main content area. if (!empty($this->defaultregion)) { $this->birecordsbyregion[$this->defaultregion] = array_merge($this->birecordsbyregion[$this->defaultregion], $unknown); } }
public function test_matching_page_type_patterns() { $this->assertEquals(array('site-index', 'site-index-*', 'site-*', '*'), matching_page_type_patterns('site-index'), '', 0, 10, true); $this->assertEquals(array('mod-quiz-report-overview', 'mod-quiz-report-overview-*', 'mod-quiz-report-*', 'mod-quiz-*', 'mod-*', '*'), matching_page_type_patterns('mod-quiz-report-overview'), '', 0, 10, true); $this->assertEquals(array('mod-forum-view', 'mod-*-view', 'mod-forum-view-*', 'mod-forum-*', 'mod-*', '*'), matching_page_type_patterns('mod-forum-view'), '', 0, 10, true); $this->assertEquals(array('mod-forum-index', 'mod-*-index', 'mod-forum-index-*', 'mod-forum-*', 'mod-*', '*'), matching_page_type_patterns('mod-forum-index'), '', 0, 10, true); }
public function test_matching_page_type_patterns() { $this->assert(new ArraysHaveSameValuesExpectation(array('site-index', 'site-index-*', 'site-*', '*')), matching_page_type_patterns('site-index')); $this->assert(new ArraysHaveSameValuesExpectation(array('mod-quiz-report-overview', 'mod-quiz-report-overview-*', 'mod-quiz-report-*', 'mod-quiz-*', 'mod-*', '*')), matching_page_type_patterns('mod-quiz-report-overview')); $this->assert(new ArraysHaveSameValuesExpectation(array('mod-forum-view', 'mod-*-view', 'mod-forum-view-*', 'mod-forum-*', 'mod-*', '*')), matching_page_type_patterns('mod-forum-view')); $this->assert(new ArraysHaveSameValuesExpectation(array('mod-forum-index', 'mod-*-index', 'mod-forum-index-*', 'mod-forum-*', 'mod-*', '*')), matching_page_type_patterns('mod-forum-index')); }
function definition() { $mform =& $this->_form; // First show fields specific to this type of block. $this->specific_definition($mform); // Then show the fields about where this block appears. $mform->addElement('header', 'whereheader', get_string('wherethisblockappears', 'block')); // If the current weight of the block is out-of-range, add that option in. $blockweight = $this->block->instance->weight; $weightoptions = array(); if ($blockweight < -block_manager::MAX_WEIGHT) { $weightoptions[$blockweight] = $blockweight; } for ($i = -block_manager::MAX_WEIGHT; $i <= block_manager::MAX_WEIGHT; $i++) { $weightoptions[$i] = $i; } if ($blockweight > block_manager::MAX_WEIGHT) { $weightoptions[$blockweight] = $blockweight; } $first = reset($weightoptions); $weightoptions[$first] = get_string('bracketfirst', 'block', $first); $last = end($weightoptions); $weightoptions[$last] = get_string('bracketlast', 'block', $last); $regionoptions = $this->page->theme->get_all_block_regions(); $parentcontext = get_context_instance_by_id($this->block->instance->parentcontextid); $mform->addElement('hidden', 'bui_parentcontextid', $parentcontext->id); $contextoptions = array(); if ($parentcontext->contextlevel == CONTEXT_COURSE && $parentcontext->instanceid == SITEID || $parentcontext->contextlevel == CONTEXT_SYSTEM) { // Home page $contextoptions[0] = get_string('showonfrontpageonly', 'block'); $contextoptions[1] = get_string('showonfrontpageandsubs', 'block'); $contextoptions[2] = get_string('showonentiresite', 'block'); } else { $parentcontextname = print_context_name($parentcontext); $contextoptions[0] = get_string('showoncontextonly', 'block', $parentcontextname); $contextoptions[1] = get_string('showoncontextandsubs', 'block', $parentcontextname); } $mform->addElement('select', 'bui_contexts', get_string('contexts', 'block'), $contextoptions); if ($this->page->pagetype == 'site-index') { // No need for pagetype list on home page $pagetypelist = array('*'); } else { $pagetypelist = matching_page_type_patterns($this->page->pagetype); } $pagetypeoptions = array(); foreach ($pagetypelist as $pagetype) { // Find human-readable names for the pagetypes $pagetypeoptions[$pagetype] = $pagetype; $pagetypestringname = 'page-' . str_replace('*', 'x', $pagetype); // Better names MDL-21375 if (get_string_manager()->string_exists($pagetypestringname, 'pagetype')) { $pagetypeoptions[$pagetype] .= ' (' . get_string($pagetypestringname, 'pagetype') . ')'; } } $mform->addElement('select', 'bui_pagetypepattern', get_string('restrictpagetypes', 'block'), $pagetypeoptions); if ($this->page->subpage) { $subpageoptions = array('%@NULL@%' => get_string('anypagematchingtheabove', 'block'), $this->page->subpage => get_string('thisspecificpage', 'block', $this->page->subpage)); $mform->addElement('select', 'bui_subpagepattern', get_string('subpages', 'block'), $subpageoptions); } $defaultregionoptions = $regionoptions; $defaultregion = $this->block->instance->defaultregion; if (!array_key_exists($defaultregion, $defaultregionoptions)) { $defaultregionoptions[$defaultregion] = $defaultregion; } $mform->addElement('select', 'bui_defaultregion', get_string('defaultregion', 'block'), $defaultregionoptions); $mform->addElement('select', 'bui_defaultweight', get_string('defaultweight', 'block'), $weightoptions); // Where this block is positioned on this page. $mform->addElement('header', 'whereheader', get_string('onthispage', 'block')); $mform->addElement('selectyesno', 'bui_visible', get_string('visible', 'block')); $blockregion = $this->block->instance->region; if (!array_key_exists($blockregion, $regionoptions)) { $regionoptions[$blockregion] = $blockregion; } $mform->addElement('select', 'bui_region', get_string('region', 'block'), $regionoptions); $mform->addElement('select', 'bui_weight', get_string('weight', 'block'), $weightoptions); $pagefields = array('bui_visible', 'bui_region', 'bui_weight'); if (!$this->block->user_can_edit()) { $mform->hardFreezeAllVisibleExcept($pagefields); } if (!$this->page->user_can_edit_blocks()) { $mform->hardFreeze($pagefields); } $this->add_action_buttons(); }
/** * This method actually loads the blocks for our page from the database. * * @param boolean|null $includeinvisible * null (default) - load hidden blocks if $this->page->user_is_editing(); * true - load hidden blocks. * false - don't load hidden blocks. */ public function load_blocks($includeinvisible = null) { global $DB, $CFG; if (!is_null($this->birecordsbyregion)) { // Already done. return; } if ($CFG->version < 2009050619) { // Upgrade/install not complete. Don't try too show any blocks. $this->birecordsbyregion = array(); return; } // Ensure we have been initialised. if (is_null($this->defaultregion)) { $this->page->initialise_theme_and_output(); // If there are still no block regions, then there are no blocks on this page. if (empty($this->regions)) { $this->birecordsbyregion = array(); return; } } // Check if we need to load normal blocks if ($this->fakeblocksonly) { $this->birecordsbyregion = $this->prepare_per_region_arrays(); return; } // Exclude auto created blocks if they are not undeletable in this theme. $undeletable = $this->get_undeletable_block_types(); $undeletablecheck = ''; $undeletableparams = array(); $undeletablenotparams = array(); if (!empty($undeletable)) { list($testsql, $undeletableparams) = $DB->get_in_or_equal($undeletable, SQL_PARAMS_NAMED, 'undeletable'); list($testnotsql, $undeletablenotparams) = $DB->get_in_or_equal($undeletable, SQL_PARAMS_NAMED, 'deletable', false); $undeletablecheck = 'AND ((bi.blockname ' . $testsql . ' AND bi.requiredbytheme = 1) OR ' . ' (bi.blockname ' . $testnotsql . ' AND bi.requiredbytheme = 0))'; } else { $undeletablecheck = 'AND (bi.requiredbytheme = 0)'; } if (is_null($includeinvisible)) { $includeinvisible = $this->page->user_is_editing(); } if ($includeinvisible) { $visiblecheck = ''; } else { $visiblecheck = 'AND (bp.visible = 1 OR bp.visible IS NULL)'; } $context = $this->page->context; $contexttest = 'bi.parentcontextid IN (:contextid2, :contextid3)'; $parentcontextparams = array(); $parentcontextids = $context->get_parent_context_ids(); if ($parentcontextids) { list($parentcontexttest, $parentcontextparams) = $DB->get_in_or_equal($parentcontextids, SQL_PARAMS_NAMED, 'parentcontext'); $contexttest = "($contexttest OR (bi.showinsubcontexts = 1 AND bi.parentcontextid $parentcontexttest))"; } $pagetypepatterns = matching_page_type_patterns($this->page->pagetype); list($pagetypepatterntest, $pagetypepatternparams) = $DB->get_in_or_equal($pagetypepatterns, SQL_PARAMS_NAMED, 'pagetypepatterntest'); $ccselect = ', ' . context_helper::get_preload_record_columns_sql('ctx'); $ccjoin = "LEFT JOIN {context} ctx ON (ctx.instanceid = bi.id AND ctx.contextlevel = :contextlevel)"; $systemcontext = context_system::instance(); $params = array( 'contextlevel' => CONTEXT_BLOCK, 'subpage1' => $this->page->subpage, 'subpage2' => $this->page->subpage, 'contextid1' => $context->id, 'contextid2' => $context->id, 'contextid3' => $systemcontext->id, 'pagetype' => $this->page->pagetype, ); if ($this->page->subpage === '') { $params['subpage1'] = ''; $params['subpage2'] = ''; } $sql = "SELECT bi.id, bp.id AS blockpositionid, bi.blockname, bi.parentcontextid, bi.showinsubcontexts, bi.pagetypepattern, bi.requiredbytheme, bi.subpagepattern, bi.defaultregion, bi.defaultweight, COALESCE(bp.visible, 1) AS visible, COALESCE(bp.region, bi.defaultregion) AS region, COALESCE(bp.weight, bi.defaultweight) AS weight, bi.configdata $ccselect FROM {block_instances} bi JOIN {block} b ON bi.blockname = b.name LEFT JOIN {block_positions} bp ON bp.blockinstanceid = bi.id AND bp.contextid = :contextid1 AND bp.pagetype = :pagetype AND bp.subpage = :subpage1 $ccjoin WHERE $contexttest AND bi.pagetypepattern $pagetypepatterntest AND (bi.subpagepattern IS NULL OR bi.subpagepattern = :subpage2) $visiblecheck AND b.visible = 1 $undeletablecheck ORDER BY COALESCE(bp.region, bi.defaultregion), COALESCE(bp.weight, bi.defaultweight), bi.id"; $allparams = $params + $parentcontextparams + $pagetypepatternparams + $undeletableparams + $undeletablenotparams; $blockinstances = $DB->get_recordset_sql($sql, $allparams); $this->birecordsbyregion = $this->prepare_per_region_arrays(); $unknown = array(); foreach ($blockinstances as $bi) { context_helper::preload_from_record($bi); if ($this->is_known_region($bi->region)) { $this->birecordsbyregion[$bi->region][] = $bi; } else { $unknown[] = $bi; } } // Pages don't necessarily have a defaultregion. The one time this can // happen is when there are no theme block regions, but the script itself // has a block region in the main content area. if (!empty($this->defaultregion)) { $this->birecordsbyregion[$this->defaultregion] = array_merge($this->birecordsbyregion[$this->defaultregion], $unknown); } }