|
Moodle
2.2.1
http://www.collinsharper.com
|
00001 <?php 00002 00003 // This file is part of Moodle - http://moodle.org/ 00004 // 00005 // Moodle is free software: you can redistribute it and/or modify 00006 // it under the terms of the GNU General Public License as published by 00007 // the Free Software Foundation, either version 3 of the License, or 00008 // (at your option) any later version. 00009 // 00010 // Moodle is distributed in the hope that it will be useful, 00011 // but WITHOUT ANY WARRANTY; without even the implied warranty of 00012 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00013 // GNU General Public License for more details. 00014 // 00015 // You should have received a copy of the GNU General Public License 00016 // along with Moodle. If not, see <http://www.gnu.org/licenses/>. 00017 00030 if (!defined('MOODLE_INTERNAL')) { 00031 die('Direct access to this script is forbidden.'); 00032 } 00033 00034 require_once($CFG->libdir . '/formslib.php'); 00035 require_once($CFG->libdir . '/blocklib.php'); 00036 00043 class block_edit_form extends moodleform { 00048 public $block; 00053 public $page; 00054 00055 function __construct($actionurl, $block, $page) { 00056 global $CFG; 00057 $this->block = $block; 00058 $this->page = $page; 00059 parent::moodleform($actionurl); 00060 } 00061 00062 function definition() { 00063 $mform =& $this->_form; 00064 00065 // First show fields specific to this type of block. 00066 $this->specific_definition($mform); 00067 00068 // Then show the fields about where this block appears. 00069 $mform->addElement('header', 'whereheader', get_string('wherethisblockappears', 'block')); 00070 00071 // If the current weight of the block is out-of-range, add that option in. 00072 $blockweight = $this->block->instance->weight; 00073 $weightoptions = array(); 00074 if ($blockweight < -block_manager::MAX_WEIGHT) { 00075 $weightoptions[$blockweight] = $blockweight; 00076 } 00077 for ($i = -block_manager::MAX_WEIGHT; $i <= block_manager::MAX_WEIGHT; $i++) { 00078 $weightoptions[$i] = $i; 00079 } 00080 if ($blockweight > block_manager::MAX_WEIGHT) { 00081 $weightoptions[$blockweight] = $blockweight; 00082 } 00083 $first = reset($weightoptions); 00084 $weightoptions[$first] = get_string('bracketfirst', 'block', $first); 00085 $last = end($weightoptions); 00086 $weightoptions[$last] = get_string('bracketlast', 'block', $last); 00087 00088 $regionoptions = $this->page->theme->get_all_block_regions(); 00089 00090 $parentcontext = get_context_instance_by_id($this->block->instance->parentcontextid); 00091 $mform->addElement('hidden', 'bui_parentcontextid', $parentcontext->id); 00092 00093 $mform->addElement('static', 'bui_homecontext', get_string('createdat', 'block'), print_context_name($parentcontext)); 00094 $mform->addHelpButton('bui_homecontext', 'createdat', 'block'); 00095 00096 // For pre-calculated (fixed) pagetype lists 00097 $pagetypelist = array(); 00098 00099 // parse pagetype patterns 00100 $bits = explode('-', $this->page->pagetype); 00101 00102 // First of all, check if we are editing blocks @ front-page or no and 00103 // make some dark magic if so (MDL-30340) because each page context 00104 // implies one (and only one) harcoded page-type that will be set later 00105 // when processing the form data at {@link block_manager::process_url_edit()} 00106 00107 // There are some conditions to check related to contexts 00108 $ctxconditions = $this->page->context->contextlevel == CONTEXT_COURSE && 00109 $this->page->context->instanceid == get_site()->id; 00110 // And also some pagetype conditions 00111 $pageconditions = isset($bits[0]) && isset($bits[1]) && $bits[0] == 'site' && $bits[1] == 'index'; 00112 // So now we can be 100% sure if edition is happening at frontpage 00113 $editingatfrontpage = $ctxconditions && $pageconditions; 00114 00115 // Let the form to know about that, can be useful later 00116 $mform->addElement('hidden', 'bui_editingatfrontpage', (int)$editingatfrontpage); 00117 00118 // Front page, show the page-contexts element and set $pagetypelist to 'any page' (*) 00119 // as unique option. Processign the form will do any change if needed 00120 if ($editingatfrontpage) { 00121 $contextoptions = array(); 00122 $contextoptions[BUI_CONTEXTS_FRONTPAGE_ONLY] = get_string('showonfrontpageonly', 'block'); 00123 $contextoptions[BUI_CONTEXTS_FRONTPAGE_SUBS] = get_string('showonfrontpageandsubs', 'block'); 00124 $contextoptions[BUI_CONTEXTS_ENTIRE_SITE] = get_string('showonentiresite', 'block'); 00125 $mform->addElement('select', 'bui_contexts', get_string('contexts', 'block'), $contextoptions); 00126 $mform->addHelpButton('bui_contexts', 'contexts', 'block'); 00127 $pagetypelist['*'] = '*'; // This is not going to be shown ever, it's an unique option 00128 00129 // Any other system context block, hide the page-contexts element, 00130 // it's always system-wide BUI_CONTEXTS_ENTIRE_SITE 00131 } else if ($parentcontext->contextlevel == CONTEXT_SYSTEM) { 00132 $mform->addElement('hidden', 'bui_contexts', BUI_CONTEXTS_ENTIRE_SITE); 00133 00134 } else if ($parentcontext->contextlevel == CONTEXT_COURSE) { 00135 // 0 means display on current context only, not child contexts 00136 // but if course managers select mod-* as pagetype patterns, block system will overwrite this option 00137 // to 1 (display on current context and child contexts) 00138 $mform->addElement('hidden', 'bui_contexts', BUI_CONTEXTS_CURRENT); 00139 } else if ($parentcontext->contextlevel == CONTEXT_MODULE or $parentcontext->contextlevel == CONTEXT_USER) { 00140 // module context doesn't have child contexts, so display in current context only 00141 $mform->addElement('hidden', 'bui_contexts', BUI_CONTEXTS_CURRENT); 00142 } else { 00143 $parentcontextname = print_context_name($parentcontext); 00144 $contextoptions[BUI_CONTEXTS_CURRENT] = get_string('showoncontextonly', 'block', $parentcontextname); 00145 $contextoptions[BUI_CONTEXTS_CURRENT_SUBS] = get_string('showoncontextandsubs', 'block', $parentcontextname); 00146 $mform->addElement('select', 'bui_contexts', get_string('contexts', 'block'), $contextoptions); 00147 } 00148 00149 // Generate pagetype patterns by callbacks if necessary (has not been set specifically) 00150 if (empty($pagetypelist)) { 00151 $pagetypelist = generate_page_type_patterns($this->page->pagetype, $parentcontext, $this->page->context); 00152 $displaypagetypewarning = false; 00153 if (!array_key_exists($this->block->instance->pagetypepattern, $pagetypelist)) { 00154 // Pushing block's existing page type pattern 00155 $pagetypestringname = 'page-'.str_replace('*', 'x', $this->block->instance->pagetypepattern); 00156 if (get_string_manager()->string_exists($pagetypestringname, 'pagetype')) { 00157 $pagetypelist[$this->block->instance->pagetypepattern] = get_string($pagetypestringname, 'pagetype'); 00158 } else { 00159 //as a last resort we could put the page type pattern in the select box 00160 //however this causes mod-data-view to be added if the only option available is mod-data-* 00161 // so we are just showing a warning to users about their prev setting being reset 00162 $displaypagetypewarning = true; 00163 } 00164 } 00165 } 00166 00167 // hide page type pattern select box if there is only one choice 00168 if (count($pagetypelist) > 1) { 00169 if ($displaypagetypewarning) { 00170 $mform->addElement('static', 'pagetypewarning', '', get_string('pagetypewarning','block')); 00171 } 00172 00173 $mform->addElement('select', 'bui_pagetypepattern', get_string('restrictpagetypes', 'block'), $pagetypelist); 00174 } else { 00175 $value = array_pop(array_keys($pagetypelist)); 00176 $mform->addElement('hidden', 'bui_pagetypepattern', $value); 00177 // Now we are really hiding a lot (both page-contexts and page-type-patterns), 00178 // specially in some systemcontext pages having only one option (my/user...) 00179 // so, until it's decided if we are going to add the 'bring-back' pattern to 00180 // all those pages or no (see MDL-30574), we are going to show the unique 00181 // element statically 00182 // TODO: Revisit this once MDL-30574 has been decided and implemented, although 00183 // perhaps it's not bad to always show this statically when only one pattern is 00184 // available. 00185 if (!$editingatfrontpage) { 00186 // Try to beautify it 00187 $strvalue = $value; 00188 $strkey = 'page-'.str_replace('*', 'x', $strvalue); 00189 if (get_string_manager()->string_exists($strkey, 'pagetype')) { 00190 $strvalue = get_string($strkey, 'pagetype'); 00191 } 00192 // Show as static (hidden has been set already) 00193 $mform->addElement('static', 'bui_staticpagetypepattern', 00194 get_string('restrictpagetypes','block'), $strvalue); 00195 } 00196 } 00197 00198 if ($this->page->subpage) { 00199 if ($parentcontext->contextlevel == CONTEXT_USER) { 00200 $mform->addElement('hidden', 'bui_subpagepattern', '%@NULL@%'); 00201 } else { 00202 $subpageoptions = array( 00203 '%@NULL@%' => get_string('anypagematchingtheabove', 'block'), 00204 $this->page->subpage => get_string('thisspecificpage', 'block', $this->page->subpage), 00205 ); 00206 $mform->addElement('select', 'bui_subpagepattern', get_string('subpages', 'block'), $subpageoptions); 00207 } 00208 } 00209 00210 $defaultregionoptions = $regionoptions; 00211 $defaultregion = $this->block->instance->defaultregion; 00212 if (!array_key_exists($defaultregion, $defaultregionoptions)) { 00213 $defaultregionoptions[$defaultregion] = $defaultregion; 00214 } 00215 $mform->addElement('select', 'bui_defaultregion', get_string('defaultregion', 'block'), $defaultregionoptions); 00216 $mform->addHelpButton('bui_defaultregion', 'defaultregion', 'block'); 00217 00218 $mform->addElement('select', 'bui_defaultweight', get_string('defaultweight', 'block'), $weightoptions); 00219 $mform->addHelpButton('bui_defaultweight', 'defaultweight', 'block'); 00220 00221 // Where this block is positioned on this page. 00222 $mform->addElement('header', 'onthispage', get_string('onthispage', 'block')); 00223 00224 $mform->addElement('selectyesno', 'bui_visible', get_string('visible', 'block')); 00225 00226 $blockregion = $this->block->instance->region; 00227 if (!array_key_exists($blockregion, $regionoptions)) { 00228 $regionoptions[$blockregion] = $blockregion; 00229 } 00230 $mform->addElement('select', 'bui_region', get_string('region', 'block'), $regionoptions); 00231 00232 $mform->addElement('select', 'bui_weight', get_string('weight', 'block'), $weightoptions); 00233 00234 $pagefields = array('bui_visible', 'bui_region', 'bui_weight'); 00235 if (!$this->block->user_can_edit()) { 00236 $mform->hardFreezeAllVisibleExcept($pagefields); 00237 } 00238 if (!$this->page->user_can_edit_blocks()) { 00239 $mform->hardFreeze($pagefields); 00240 } 00241 00242 $this->add_action_buttons(); 00243 } 00244 00245 function set_data($defaults) { 00246 // Prefix bui_ on all the core field names. 00247 $blockfields = array('showinsubcontexts', 'pagetypepattern', 'subpagepattern', 'parentcontextid', 00248 'defaultregion', 'defaultweight', 'visible', 'region', 'weight'); 00249 foreach ($blockfields as $field) { 00250 $newname = 'bui_' . $field; 00251 $defaults->$newname = $defaults->$field; 00252 } 00253 00254 // Copy block config into config_ fields. 00255 if (!empty($this->block->config)) { 00256 foreach ($this->block->config as $field => $value) { 00257 $configfield = 'config_' . $field; 00258 $defaults->$configfield = $value; 00259 } 00260 } 00261 00262 // Munge ->subpagepattern becuase HTML selects don't play nicely with NULLs. 00263 if (empty($defaults->bui_subpagepattern)) { 00264 $defaults->bui_subpagepattern = '%@NULL@%'; 00265 } 00266 00267 $systemcontext = get_context_instance(CONTEXT_SYSTEM); 00268 if ($defaults->parentcontextid == $systemcontext->id) { 00269 $defaults->bui_contexts = BUI_CONTEXTS_ENTIRE_SITE; // System-wide and sticky 00270 } else { 00271 $defaults->bui_contexts = $defaults->bui_showinsubcontexts; 00272 } 00273 00274 parent::set_data($defaults); 00275 } 00276 00281 protected function specific_definition($mform) { 00282 // By default, do nothing. 00283 } 00284 }