Example #1
0
 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();
 }
Example #2
0
 /**
  * Handle showing/processing the submission from the block editing form.
  * @return boolean true if the form was submitted and the new config saved. Does not
  *      return if the editing form was displayed. False otherwise.
  */
 public function process_url_move()
 {
     global $CFG, $DB, $PAGE;
     $blockid = optional_param('bui_moveid', null, PARAM_INT);
     if (!$blockid) {
         return false;
     }
     require_sesskey();
     $block = $this->find_instance($blockid);
     if (!$this->page->user_can_edit_blocks()) {
         throw new moodle_exception('nopermissions', '', $this->page->url->out(), get_string('editblock'));
     }
     $newregion = optional_param('bui_newregion', '', PARAM_ALPHANUMEXT);
     $newweight = optional_param('bui_newweight', null, PARAM_FLOAT);
     if (!$newregion || is_null($newweight)) {
         // Don't have a valid target position yet, must be just starting the move.
         $this->movingblock = $blockid;
         $this->page->ensure_param_not_in_url('bui_moveid');
         return false;
     }
     if (!$this->is_known_region($newregion)) {
         throw new moodle_exception('unknownblockregion', '', $this->page->url, $newregion);
     }
     // Move this block. This may involve moving other nearby blocks.
     $blocks = $this->birecordsbyregion[$newregion];
     $maxweight = self::MAX_WEIGHT;
     $minweight = -self::MAX_WEIGHT;
     // Initialise the used weights and spareweights array with the default values
     $spareweights = array();
     $usedweights = array();
     for ($i = $minweight; $i <= $maxweight; $i++) {
         $spareweights[$i] = $i;
         $usedweights[$i] = array();
     }
     // Check each block and sort out where we have used weights
     foreach ($blocks as $bi) {
         if ($bi->weight > $maxweight) {
             // If this statement is true then the blocks weight is more than the
             // current maximum. To ensure that we can get the best block position
             // we will initialise elements within the usedweights and spareweights
             // arrays between the blocks weight (which will then be the new max) and
             // the current max
             $parseweight = $bi->weight;
             while (!array_key_exists($parseweight, $usedweights)) {
                 $usedweights[$parseweight] = array();
                 $spareweights[$parseweight] = $parseweight;
                 $parseweight--;
             }
             $maxweight = $bi->weight;
         } else {
             if ($bi->weight < $minweight) {
                 // As above except this time the blocks weight is LESS than the
                 // the current minimum, so we will initialise the array from the
                 // blocks weight (new minimum) to the current minimum
                 $parseweight = $bi->weight;
                 while (!array_key_exists($parseweight, $usedweights)) {
                     $usedweights[$parseweight] = array();
                     $spareweights[$parseweight] = $parseweight;
                     $parseweight++;
                 }
                 $minweight = $bi->weight;
             }
         }
         if ($bi->id != $block->instance->id) {
             unset($spareweights[$bi->weight]);
             $usedweights[$bi->weight][] = $bi->id;
         }
     }
     // First we find the nearest gap in the list of weights.
     $bestdistance = max(abs($newweight - self::MAX_WEIGHT), abs($newweight + self::MAX_WEIGHT)) + 1;
     $bestgap = null;
     foreach ($spareweights as $spareweight) {
         if (abs($newweight - $spareweight) < $bestdistance) {
             $bestdistance = abs($newweight - $spareweight);
             $bestgap = $spareweight;
         }
     }
     // If there is no gap, we have to go outside -self::MAX_WEIGHT .. self::MAX_WEIGHT.
     if (is_null($bestgap)) {
         $bestgap = self::MAX_WEIGHT + 1;
         while (!empty($usedweights[$bestgap])) {
             $bestgap++;
         }
     }
     // Now we know the gap we are aiming for, so move all the blocks along.
     if ($bestgap < $newweight) {
         $newweight = floor($newweight);
         for ($weight = $bestgap + 1; $weight <= $newweight; $weight++) {
             foreach ($usedweights[$weight] as $biid) {
                 $this->reposition_block($biid, $newregion, $weight - 1);
             }
         }
         $this->reposition_block($block->instance->id, $newregion, $newweight);
     } else {
         $newweight = ceil($newweight);
         for ($weight = $bestgap - 1; $weight >= $newweight; $weight--) {
             if (array_key_exists($weight, $usedweights)) {
                 foreach ($usedweights[$weight] as $biid) {
                     $this->reposition_block($biid, $newregion, $weight + 1);
                 }
             }
         }
         $this->reposition_block($block->instance->id, $newregion, $newweight);
     }
     $this->page->ensure_param_not_in_url('bui_moveid');
     $this->page->ensure_param_not_in_url('bui_newregion');
     $this->page->ensure_param_not_in_url('bui_newweight');
     return true;
 }
Example #3
0
 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();
     foreach ($this->page->blocks->get_regions() as $region) {
         // Make sure to add all custom regions of this particular page too.
         if (!isset($regionoptions[$region])) {
             $regionoptions[$region] = $region;
         }
     }
     $parentcontext = context::instance_by_id($this->block->instance->parentcontextid);
     $mform->addElement('hidden', 'bui_parentcontextid', $parentcontext->id);
     $mform->setType('bui_parentcontextid', PARAM_INT);
     $mform->addElement('static', 'bui_homecontext', get_string('createdat', 'block'), $parentcontext->get_context_name());
     $mform->addHelpButton('bui_homecontext', 'createdat', 'block');
     // For pre-calculated (fixed) pagetype lists
     $pagetypelist = array();
     // parse pagetype patterns
     $bits = explode('-', $this->page->pagetype);
     // First of all, check if we are editing blocks @ front-page or no and
     // make some dark magic if so (MDL-30340) because each page context
     // implies one (and only one) harcoded page-type that will be set later
     // when processing the form data at {@link block_manager::process_url_edit()}
     // There are some conditions to check related to contexts
     $ctxconditions = $this->page->context->contextlevel == CONTEXT_COURSE && $this->page->context->instanceid == get_site()->id;
     // And also some pagetype conditions
     $pageconditions = isset($bits[0]) && isset($bits[1]) && $bits[0] == 'site' && $bits[1] == 'index';
     // So now we can be 100% sure if edition is happening at frontpage
     $editingatfrontpage = $ctxconditions && $pageconditions;
     // Let the form to know about that, can be useful later
     $mform->addElement('hidden', 'bui_editingatfrontpage', (int) $editingatfrontpage);
     $mform->setType('bui_editingatfrontpage', PARAM_INT);
     // Front page, show the page-contexts element and set $pagetypelist to 'any page' (*)
     // as unique option. Processign the form will do any change if needed
     if ($editingatfrontpage) {
         $contextoptions = array();
         $contextoptions[BUI_CONTEXTS_FRONTPAGE_ONLY] = get_string('showonfrontpageonly', 'block');
         $contextoptions[BUI_CONTEXTS_FRONTPAGE_SUBS] = get_string('showonfrontpageandsubs', 'block');
         $contextoptions[BUI_CONTEXTS_ENTIRE_SITE] = get_string('showonentiresite', 'block');
         $mform->addElement('select', 'bui_contexts', get_string('contexts', 'block'), $contextoptions);
         $mform->addHelpButton('bui_contexts', 'contexts', 'block');
         $pagetypelist['*'] = '*';
         // This is not going to be shown ever, it's an unique option
         // Any other system context block, hide the page-contexts element,
         // it's always system-wide BUI_CONTEXTS_ENTIRE_SITE
     } else {
         if ($parentcontext->contextlevel == CONTEXT_SYSTEM) {
             $mform->addElement('hidden', 'bui_contexts', BUI_CONTEXTS_ENTIRE_SITE);
         } else {
             if ($parentcontext->contextlevel == CONTEXT_COURSE) {
                 // 0 means display on current context only, not child contexts
                 // but if course managers select mod-* as pagetype patterns, block system will overwrite this option
                 // to 1 (display on current context and child contexts)
                 $mform->addElement('hidden', 'bui_contexts', BUI_CONTEXTS_CURRENT);
             } else {
                 if ($parentcontext->contextlevel == CONTEXT_MODULE or $parentcontext->contextlevel == CONTEXT_USER) {
                     // module context doesn't have child contexts, so display in current context only
                     $mform->addElement('hidden', 'bui_contexts', BUI_CONTEXTS_CURRENT);
                 } else {
                     $parentcontextname = $parentcontext->get_context_name();
                     $contextoptions[BUI_CONTEXTS_CURRENT] = get_string('showoncontextonly', 'block', $parentcontextname);
                     $contextoptions[BUI_CONTEXTS_CURRENT_SUBS] = get_string('showoncontextandsubs', 'block', $parentcontextname);
                     $mform->addElement('select', 'bui_contexts', get_string('contexts', 'block'), $contextoptions);
                 }
             }
         }
     }
     $mform->setType('bui_contexts', PARAM_INT);
     // Generate pagetype patterns by callbacks if necessary (has not been set specifically)
     if (empty($pagetypelist)) {
         $pagetypelist = generate_page_type_patterns($this->page->pagetype, $parentcontext, $this->page->context);
         $displaypagetypewarning = false;
         if (!array_key_exists($this->block->instance->pagetypepattern, $pagetypelist)) {
             // Pushing block's existing page type pattern
             $pagetypestringname = 'page-' . str_replace('*', 'x', $this->block->instance->pagetypepattern);
             if (get_string_manager()->string_exists($pagetypestringname, 'pagetype')) {
                 $pagetypelist[$this->block->instance->pagetypepattern] = get_string($pagetypestringname, 'pagetype');
             } else {
                 //as a last resort we could put the page type pattern in the select box
                 //however this causes mod-data-view to be added if the only option available is mod-data-*
                 // so we are just showing a warning to users about their prev setting being reset
                 $displaypagetypewarning = true;
             }
         }
     }
     // hide page type pattern select box if there is only one choice
     if (count($pagetypelist) > 1) {
         if ($displaypagetypewarning) {
             $mform->addElement('static', 'pagetypewarning', '', get_string('pagetypewarning', 'block'));
         }
         $mform->addElement('select', 'bui_pagetypepattern', get_string('restrictpagetypes', 'block'), $pagetypelist);
     } else {
         $values = array_keys($pagetypelist);
         $value = array_pop($values);
         $mform->addElement('hidden', 'bui_pagetypepattern', $value);
         $mform->setType('bui_pagetypepattern', PARAM_RAW);
         // Now we are really hiding a lot (both page-contexts and page-type-patterns),
         // specially in some systemcontext pages having only one option (my/user...)
         // so, until it's decided if we are going to add the 'bring-back' pattern to
         // all those pages or no (see MDL-30574), we are going to show the unique
         // element statically
         // TODO: Revisit this once MDL-30574 has been decided and implemented, although
         // perhaps it's not bad to always show this statically when only one pattern is
         // available.
         if (!$editingatfrontpage) {
             // Try to beautify it
             $strvalue = $value;
             $strkey = 'page-' . str_replace('*', 'x', $strvalue);
             if (get_string_manager()->string_exists($strkey, 'pagetype')) {
                 $strvalue = get_string($strkey, 'pagetype');
             }
             // Show as static (hidden has been set already)
             $mform->addElement('static', 'bui_staticpagetypepattern', get_string('restrictpagetypes', 'block'), $strvalue);
         }
     }
     if ($this->page->subpage) {
         if ($parentcontext->contextlevel == CONTEXT_USER) {
             $mform->addElement('hidden', 'bui_subpagepattern', '%@NULL@%');
             $mform->setType('bui_subpagepattern', PARAM_RAW);
         } else {
             $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->addHelpButton('bui_defaultregion', 'defaultregion', 'block');
     $mform->addElement('select', 'bui_defaultweight', get_string('defaultweight', 'block'), $weightoptions);
     $mform->addHelpButton('bui_defaultweight', 'defaultweight', 'block');
     // Where this block is positioned on this page.
     $mform->addElement('header', 'onthispage', 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();
 }
 /**
  * Sets class $edit_controls var with correct block manipulation links.
  *
  * @uses $CFG
  * @uses $USER
  * @param stdObject $options ?
  * @todo complete documenting this function. Define $options.
  */
 function _add_edit_controls($options)
 {
     global $CFG, $USER;
     // TODO - temporary hack to get the block context only if it already exists.
     global $DB;
     if ($DB->record_exists('context', array('contextlevel' => CONTEXT_BLOCK, 'instanceid' => $this->instance->id))) {
         $context = get_context_instance(CONTEXT_BLOCK, $this->instance->id);
     } else {
         $context = get_context_instance(CONTEXT_SYSTEM);
         // pinned blocks do not have own context
     }
     // context for site or course, i.e. participant list etc
     // check to see if user can edit site or course blocks.
     // blocks can appear on other pages such as mod and blog pages...
     if (!$this->page->user_can_edit_blocks()) {
         return null;
     }
     if (!isset($this->str)) {
         $this->str->delete = get_string('delete');
         $this->str->moveup = get_string('moveup');
         $this->str->movedown = get_string('movedown');
         $this->str->moveright = get_string('moveright');
         $this->str->moveleft = get_string('moveleft');
         $this->str->hide = get_string('hide');
         $this->str->show = get_string('show');
         $this->str->configure = get_string('configuration');
         $this->str->assignroles = get_string('assignroles', 'role');
     }
     // RTL support - exchange right and left arrows
     if (right_to_left()) {
         $rightarrow = 'left.gif';
         $leftarrow = 'right.gif';
     } else {
         $rightarrow = 'right.gif';
         $leftarrow = 'left.gif';
     }
     $movebuttons = '<div class="commands">';
     if ($this->instance->visible) {
         $icon = '/t/hide.gif';
         $title = $this->str->hide;
     } else {
         $icon = '/t/show.gif';
         $title = $this->str->show;
     }
     $page = $this->page;
     $script = $page->url->out(false, array('instanceid' => $this->instance->id, 'sesskey' => sesskey()));
     $movebuttons .= '<a class="icon roles" title="' . $this->str->assignroles . '" href="' . $CFG->wwwroot . '/' . $CFG->admin . '/roles/assign.php?contextid=' . $context->id . '">' . '<img src="' . $CFG->pixpath . '/i/roles.gif" alt="' . $this->str->assignroles . '" /></a>';
     // TODO MDL-19010 fix and re-enable.
     if (false && $this->user_can_edit()) {
         $movebuttons .= '<a class="icon hide" title="' . $title . '" href="' . $script . '&amp;blockaction=toggle">' . '<img src="' . $CFG->pixpath . $icon . '" alt="' . $title . '" /></a>';
     }
     if ($options & BLOCK_CONFIGURE && $this->user_can_edit()) {
         $movebuttons .= '<a class="icon edit" title="' . $this->str->configure . '" href="' . $script . '&amp;blockaction=config">' . '<img src="' . $CFG->pixpath . '/t/edit.gif" alt="' . $this->str->configure . '" /></a>';
     }
     if ($this->user_can_addto($page)) {
         $movebuttons .= '<a class="icon delete" title="' . $this->str->delete . '" href="' . $script . '&amp;blockaction=delete">' . '<img src="' . $CFG->pixpath . '/t/delete.gif" alt="' . $this->str->delete . '" /></a>';
     }
     if ($options & BLOCK_MOVE_LEFT) {
         $movebuttons .= '<a class="icon left" title="' . $this->str->moveleft . '" href="' . $script . '&amp;blockaction=moveleft">' . '<img src="' . $CFG->pixpath . '/t/' . $leftarrow . '" alt="' . $this->str->moveleft . '" /></a>';
     }
     if ($options & BLOCK_MOVE_UP) {
         $movebuttons .= '<a class="icon up" title="' . $this->str->moveup . '" href="' . $script . '&amp;blockaction=moveup">' . '<img src="' . $CFG->pixpath . '/t/up.gif" alt="' . $this->str->moveup . '" /></a>';
     }
     if ($options & BLOCK_MOVE_DOWN) {
         $movebuttons .= '<a class="icon down" title="' . $this->str->movedown . '" href="' . $script . '&amp;blockaction=movedown">' . '<img src="' . $CFG->pixpath . '/t/down.gif" alt="' . $this->str->movedown . '" /></a>';
     }
     if ($options & BLOCK_MOVE_RIGHT) {
         $movebuttons .= '<a class="icon right" title="' . $this->str->moveright . '" href="' . $script . '&amp;blockaction=moveright">' . '<img src="' . $CFG->pixpath . '/t/' . $rightarrow . '" alt="' . $this->str->moveright . '" /></a>';
     }
     $movebuttons .= '</div>';
     $this->edit_controls = $movebuttons;
 }
Example #5
0
 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();
 }
 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);
     $mform->addElement('static', 'bui_homecontext', get_string('createdat', 'block'), print_context_name($parentcontext));
     $mform->addHelpButton('bui_homecontext', 'createdat', 'block');
     // parse pagetype patterns
     $bits = explode('-', $this->page->pagetype);
     $contextoptions = array();
     if ($parentcontext->contextlevel == CONTEXT_COURSE && $parentcontext->instanceid == SITEID || $parentcontext->contextlevel == CONTEXT_SYSTEM) {
         // Home page
         if ($bits[0] == 'tag' || $bits[0] == 'admin') {
             // tag and admin pages always use system context
             // the contexts options don't make differences, so we use
             // page type patterns only
             $mform->addElement('hidden', 'bui_contexts', BUI_CONTEXTS_ENTIRE_SITE);
         } else {
             $contextoptions[BUI_CONTEXTS_FRONTPAGE_ONLY] = get_string('showonfrontpageonly', 'block');
             $contextoptions[BUI_CONTEXTS_FRONTPAGE_SUBS] = get_string('showonfrontpageandsubs', 'block');
             $contextoptions[BUI_CONTEXTS_ENTIRE_SITE] = get_string('showonentiresite', 'block');
             $mform->addElement('select', 'bui_contexts', get_string('contexts', 'block'), $contextoptions);
             $mform->addHelpButton('bui_contexts', 'contexts', 'block');
         }
     } else {
         if ($parentcontext->contextlevel == CONTEXT_COURSE) {
             // 0 means display on current context only, not child contexts
             // but if course managers select mod-* as pagetype patterns, block system will overwrite this option
             // to 1 (display on current context and child contexts)
             $mform->addElement('hidden', 'bui_contexts', BUI_CONTEXTS_CURRENT);
         } else {
             if ($parentcontext->contextlevel == CONTEXT_MODULE or $parentcontext->contextlevel == CONTEXT_USER) {
                 // module context doesn't have child contexts, so display in current context only
                 $mform->addElement('hidden', 'bui_contexts', BUI_CONTEXTS_CURRENT);
             } else {
                 $parentcontextname = print_context_name($parentcontext);
                 $contextoptions[BUI_CONTEXTS_CURRENT] = get_string('showoncontextonly', 'block', $parentcontextname);
                 $contextoptions[BUI_CONTEXTS_CURRENT_SUBS] = get_string('showoncontextandsubs', 'block', $parentcontextname);
                 $mform->addElement('select', 'bui_contexts', get_string('contexts', 'block'), $contextoptions);
             }
         }
     }
     $displaypagetypewarning = false;
     if ($this->page->pagetype == 'site-index') {
         // No need for pagetype list on home page
         $pagetypelist = array('*' => get_string('page-x', 'pagetype'));
     } else {
         // Generate pagetype patterns by callbacks
         $pagetypelist = generate_page_type_patterns($this->page->pagetype, $parentcontext, $this->page->context);
         if (!array_key_exists($this->block->instance->pagetypepattern, $pagetypelist)) {
             // Pushing block's existing page type pattern
             $pagetypestringname = 'page-' . str_replace('*', 'x', $this->block->instance->pagetypepattern);
             if (get_string_manager()->string_exists($pagetypestringname, 'pagetype')) {
                 $pagetypelist[$this->block->instance->pagetypepattern] = get_string($pagetypestringname, 'pagetype');
             } else {
                 //as a last resort we could put the page type pattern in the select box
                 //however this causes mod-data-view to be added if the only option available is mod-data-*
                 // so we are just showing a warning to users about their prev setting being reset
                 $displaypagetypewarning = true;
             }
         }
     }
     // hide page type pattern select box if there is only one choice
     if (count($pagetypelist) > 1) {
         if ($displaypagetypewarning) {
             $mform->addElement('static', 'pagetypewarning', '', get_string('pagetypewarning', 'block'));
         }
         $mform->addElement('select', 'bui_pagetypepattern', get_string('restrictpagetypes', 'block'), $pagetypelist);
     } else {
         $value = array_pop(array_keys($pagetypelist));
         $mform->addElement('hidden', 'bui_pagetypepattern', $value);
     }
     if ($this->page->subpage) {
         if ($parentcontext->contextlevel == CONTEXT_USER) {
             $mform->addElement('hidden', 'bui_subpagepattern', '%@NULL@%');
         } else {
             $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->addHelpButton('bui_defaultregion', 'defaultregion', 'block');
     $mform->addElement('select', 'bui_defaultweight', get_string('defaultweight', 'block'), $weightoptions);
     $mform->addHelpButton('bui_defaultweight', 'defaultweight', 'block');
     // 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();
 }