Merge branch 'MDL-20367-master' of git://github.com/damyon/moodle
[moodle.git] / blocks / edit_form.php
blob19624eb9ea0c2a7fef8569869cc539ba66e6fffb
1 <?php
2 // This file is part of Moodle - http://moodle.org/
3 //
4 // Moodle is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
8 //
9 // Moodle is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
14 // You should have received a copy of the GNU General Public License
15 // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
17 /**
18 * Defines the base class form used by blocks/edit.php to edit block instance configuration.
20 * It works with the {@link block_edit_form} class, or rather the particular
21 * subclass defined by this block, to do the editing.
23 * @package core_block
24 * @copyright 2009 Tim Hunt
25 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
28 if (!defined('MOODLE_INTERNAL')) {
29 die('Direct access to this script is forbidden.'); /// It must be included from a Moodle page
32 require_once($CFG->libdir . '/formslib.php');
33 require_once($CFG->libdir . '/blocklib.php');
35 /**
36 * The base class form used by blocks/edit.php to edit block instance configuration.
38 * @copyright 2009 Tim Hunt
39 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
41 class block_edit_form extends moodleform {
42 /**
43 * The block instance we are editing.
44 * @var block_base
46 public $block;
47 /**
48 * The page we are editing this block in association with.
49 * @var moodle_page
51 public $page;
53 function __construct($actionurl, $block, $page) {
54 global $CFG;
55 $this->block = $block;
56 $this->page = $page;
57 parent::moodleform($actionurl);
60 function definition() {
61 $mform =& $this->_form;
63 // First show fields specific to this type of block.
64 $this->specific_definition($mform);
66 // Then show the fields about where this block appears.
67 $mform->addElement('header', 'whereheader', get_string('wherethisblockappears', 'block'));
69 // If the current weight of the block is out-of-range, add that option in.
70 $blockweight = $this->block->instance->weight;
71 $weightoptions = array();
72 if ($blockweight < -block_manager::MAX_WEIGHT) {
73 $weightoptions[$blockweight] = $blockweight;
75 for ($i = -block_manager::MAX_WEIGHT; $i <= block_manager::MAX_WEIGHT; $i++) {
76 $weightoptions[$i] = $i;
78 if ($blockweight > block_manager::MAX_WEIGHT) {
79 $weightoptions[$blockweight] = $blockweight;
81 $first = reset($weightoptions);
82 $weightoptions[$first] = get_string('bracketfirst', 'block', $first);
83 $last = end($weightoptions);
84 $weightoptions[$last] = get_string('bracketlast', 'block', $last);
86 $regionoptions = $this->page->theme->get_all_block_regions();
88 $parentcontext = context::instance_by_id($this->block->instance->parentcontextid);
89 $mform->addElement('hidden', 'bui_parentcontextid', $parentcontext->id);
90 $mform->setType('bui_parentcontextid', PARAM_INT);
92 $mform->addElement('static', 'bui_homecontext', get_string('createdat', 'block'), $parentcontext->get_context_name());
93 $mform->addHelpButton('bui_homecontext', 'createdat', 'block');
95 // For pre-calculated (fixed) pagetype lists
96 $pagetypelist = array();
98 // parse pagetype patterns
99 $bits = explode('-', $this->page->pagetype);
101 // First of all, check if we are editing blocks @ front-page or no and
102 // make some dark magic if so (MDL-30340) because each page context
103 // implies one (and only one) harcoded page-type that will be set later
104 // when processing the form data at {@link block_manager::process_url_edit()}
106 // There are some conditions to check related to contexts
107 $ctxconditions = $this->page->context->contextlevel == CONTEXT_COURSE &&
108 $this->page->context->instanceid == get_site()->id;
109 // And also some pagetype conditions
110 $pageconditions = isset($bits[0]) && isset($bits[1]) && $bits[0] == 'site' && $bits[1] == 'index';
111 // So now we can be 100% sure if edition is happening at frontpage
112 $editingatfrontpage = $ctxconditions && $pageconditions;
114 // Let the form to know about that, can be useful later
115 $mform->addElement('hidden', 'bui_editingatfrontpage', (int)$editingatfrontpage);
116 $mform->setType('bui_editingatfrontpage', PARAM_INT);
118 // Front page, show the page-contexts element and set $pagetypelist to 'any page' (*)
119 // as unique option. Processign the form will do any change if needed
120 if ($editingatfrontpage) {
121 $contextoptions = array();
122 $contextoptions[BUI_CONTEXTS_FRONTPAGE_ONLY] = get_string('showonfrontpageonly', 'block');
123 $contextoptions[BUI_CONTEXTS_FRONTPAGE_SUBS] = get_string('showonfrontpageandsubs', 'block');
124 $contextoptions[BUI_CONTEXTS_ENTIRE_SITE] = get_string('showonentiresite', 'block');
125 $mform->addElement('select', 'bui_contexts', get_string('contexts', 'block'), $contextoptions);
126 $mform->addHelpButton('bui_contexts', 'contexts', 'block');
127 $pagetypelist['*'] = '*'; // This is not going to be shown ever, it's an unique option
129 // Any other system context block, hide the page-contexts element,
130 // it's always system-wide BUI_CONTEXTS_ENTIRE_SITE
131 } else if ($parentcontext->contextlevel == CONTEXT_SYSTEM) {
132 $mform->addElement('hidden', 'bui_contexts', BUI_CONTEXTS_ENTIRE_SITE);
134 } else if ($parentcontext->contextlevel == CONTEXT_COURSE) {
135 // 0 means display on current context only, not child contexts
136 // but if course managers select mod-* as pagetype patterns, block system will overwrite this option
137 // to 1 (display on current context and child contexts)
138 $mform->addElement('hidden', 'bui_contexts', BUI_CONTEXTS_CURRENT);
139 } else if ($parentcontext->contextlevel == CONTEXT_MODULE or $parentcontext->contextlevel == CONTEXT_USER) {
140 // module context doesn't have child contexts, so display in current context only
141 $mform->addElement('hidden', 'bui_contexts', BUI_CONTEXTS_CURRENT);
142 } else {
143 $parentcontextname = $parentcontext->get_context_name();
144 $contextoptions[BUI_CONTEXTS_CURRENT] = get_string('showoncontextonly', 'block', $parentcontextname);
145 $contextoptions[BUI_CONTEXTS_CURRENT_SUBS] = get_string('showoncontextandsubs', 'block', $parentcontextname);
146 $mform->addElement('select', 'bui_contexts', get_string('contexts', 'block'), $contextoptions);
148 $mform->setType('bui_contexts', PARAM_INT);
150 // Generate pagetype patterns by callbacks if necessary (has not been set specifically)
151 if (empty($pagetypelist)) {
152 $pagetypelist = generate_page_type_patterns($this->page->pagetype, $parentcontext, $this->page->context);
153 $displaypagetypewarning = false;
154 if (!array_key_exists($this->block->instance->pagetypepattern, $pagetypelist)) {
155 // Pushing block's existing page type pattern
156 $pagetypestringname = 'page-'.str_replace('*', 'x', $this->block->instance->pagetypepattern);
157 if (get_string_manager()->string_exists($pagetypestringname, 'pagetype')) {
158 $pagetypelist[$this->block->instance->pagetypepattern] = get_string($pagetypestringname, 'pagetype');
159 } else {
160 //as a last resort we could put the page type pattern in the select box
161 //however this causes mod-data-view to be added if the only option available is mod-data-*
162 // so we are just showing a warning to users about their prev setting being reset
163 $displaypagetypewarning = true;
168 // hide page type pattern select box if there is only one choice
169 if (count($pagetypelist) > 1) {
170 if ($displaypagetypewarning) {
171 $mform->addElement('static', 'pagetypewarning', '', get_string('pagetypewarning','block'));
174 $mform->addElement('select', 'bui_pagetypepattern', get_string('restrictpagetypes', 'block'), $pagetypelist);
175 } else {
176 $values = array_keys($pagetypelist);
177 $value = array_pop($values);
178 $mform->addElement('hidden', 'bui_pagetypepattern', $value);
179 $mform->setType('bui_pagetypepattern', PARAM_RAW);
180 // Now we are really hiding a lot (both page-contexts and page-type-patterns),
181 // specially in some systemcontext pages having only one option (my/user...)
182 // so, until it's decided if we are going to add the 'bring-back' pattern to
183 // all those pages or no (see MDL-30574), we are going to show the unique
184 // element statically
185 // TODO: Revisit this once MDL-30574 has been decided and implemented, although
186 // perhaps it's not bad to always show this statically when only one pattern is
187 // available.
188 if (!$editingatfrontpage) {
189 // Try to beautify it
190 $strvalue = $value;
191 $strkey = 'page-'.str_replace('*', 'x', $strvalue);
192 if (get_string_manager()->string_exists($strkey, 'pagetype')) {
193 $strvalue = get_string($strkey, 'pagetype');
195 // Show as static (hidden has been set already)
196 $mform->addElement('static', 'bui_staticpagetypepattern',
197 get_string('restrictpagetypes','block'), $strvalue);
201 if ($this->page->subpage) {
202 if ($parentcontext->contextlevel == CONTEXT_USER) {
203 $mform->addElement('hidden', 'bui_subpagepattern', '%@NULL@%');
204 $mform->setType('bui_subpagepattern', PARAM_RAW);
205 } else {
206 $subpageoptions = array(
207 '%@NULL@%' => get_string('anypagematchingtheabove', 'block'),
208 $this->page->subpage => get_string('thisspecificpage', 'block', $this->page->subpage),
210 $mform->addElement('select', 'bui_subpagepattern', get_string('subpages', 'block'), $subpageoptions);
214 $defaultregionoptions = $regionoptions;
215 $defaultregion = $this->block->instance->defaultregion;
216 if (!array_key_exists($defaultregion, $defaultregionoptions)) {
217 $defaultregionoptions[$defaultregion] = $defaultregion;
219 $mform->addElement('select', 'bui_defaultregion', get_string('defaultregion', 'block'), $defaultregionoptions);
220 $mform->addHelpButton('bui_defaultregion', 'defaultregion', 'block');
222 $mform->addElement('select', 'bui_defaultweight', get_string('defaultweight', 'block'), $weightoptions);
223 $mform->addHelpButton('bui_defaultweight', 'defaultweight', 'block');
225 // Where this block is positioned on this page.
226 $mform->addElement('header', 'onthispage', get_string('onthispage', 'block'));
228 $mform->addElement('selectyesno', 'bui_visible', get_string('visible', 'block'));
230 $blockregion = $this->block->instance->region;
231 if (!array_key_exists($blockregion, $regionoptions)) {
232 $regionoptions[$blockregion] = $blockregion;
234 $mform->addElement('select', 'bui_region', get_string('region', 'block'), $regionoptions);
236 $mform->addElement('select', 'bui_weight', get_string('weight', 'block'), $weightoptions);
238 $pagefields = array('bui_visible', 'bui_region', 'bui_weight');
239 if (!$this->block->user_can_edit()) {
240 $mform->hardFreezeAllVisibleExcept($pagefields);
242 if (!$this->page->user_can_edit_blocks()) {
243 $mform->hardFreeze($pagefields);
246 $this->add_action_buttons();
249 function set_data($defaults) {
250 // Prefix bui_ on all the core field names.
251 $blockfields = array('showinsubcontexts', 'pagetypepattern', 'subpagepattern', 'parentcontextid',
252 'defaultregion', 'defaultweight', 'visible', 'region', 'weight');
253 foreach ($blockfields as $field) {
254 $newname = 'bui_' . $field;
255 $defaults->$newname = $defaults->$field;
258 // Copy block config into config_ fields.
259 if (!empty($this->block->config)) {
260 foreach ($this->block->config as $field => $value) {
261 $configfield = 'config_' . $field;
262 $defaults->$configfield = $value;
266 // Munge ->subpagepattern becuase HTML selects don't play nicely with NULLs.
267 if (empty($defaults->bui_subpagepattern)) {
268 $defaults->bui_subpagepattern = '%@NULL@%';
271 $systemcontext = context_system::instance();
272 if ($defaults->parentcontextid == $systemcontext->id) {
273 $defaults->bui_contexts = BUI_CONTEXTS_ENTIRE_SITE; // System-wide and sticky
274 } else {
275 $defaults->bui_contexts = $defaults->bui_showinsubcontexts;
278 parent::set_data($defaults);
282 * Override this to create any form fields specific to this type of block.
283 * @param object $mform the form being built.
285 protected function specific_definition($mform) {
286 // By default, do nothing.