MDL-38788 behat: Generic form elements interaction compatibile with JS disabled
[moodle.git] / blocks / edit_form.php
CommitLineData
1d13c75c 1<?php
2
4ca6cfbf
PS
3// This file is part of Moodle - http://moodle.org/
4//
1d13c75c 5// Moodle is free software: you can redistribute it and/or modify
6// it under the terms of the GNU General Public License as published by
7// the Free Software Foundation, either version 3 of the License, or
8// (at your option) any later version.
9//
10// Moodle is distributed in the hope that it will be useful,
11// but WITHOUT ANY WARRANTY; without even the implied warranty of
12// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13// GNU General Public License for more details.
4ca6cfbf 14//
1d13c75c 15// You should have received a copy of the GNU General Public License
16// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
17
18/**
19 * Defines the base class form used by blocks/edit.php to edit block instance configuration.
20 *
21 * It works with the {@link block_edit_form} class, or rather the particular
22 * subclass defined by this block, to do the editing.
23 *
78bfb562
PS
24 * @package core
25 * @subpackage block
26 * @copyright 2009 Tim Hunt
27 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
1d13c75c 28 */
29
bfebaf64
MD
30if (!defined('MOODLE_INTERNAL')) {
31 die('Direct access to this script is forbidden.'); /// It must be included from a Moodle page
32}
33
1d13c75c 34require_once($CFG->libdir . '/formslib.php');
b1627a92 35require_once($CFG->libdir . '/blocklib.php');
1d13c75c 36
37/**
38 * The base class form used by blocks/edit.php to edit block instance configuration.
39 *
40 * @copyright 2009 Tim Hunt
41 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
42 */
43class block_edit_form extends moodleform {
1d13c75c 44 /**
45 * The block instance we are editing.
46 * @var block_base
47 */
48 public $block;
49 /**
50 * The page we are editing this block in association with.
51 * @var moodle_page
52 */
53 public $page;
54
a19f419d 55 function __construct($actionurl, $block, $page) {
1d13c75c 56 global $CFG;
57 $this->block = $block;
58 $this->page = $page;
a19f419d 59 parent::moodleform($actionurl);
1d13c75c 60 }
61
62 function definition() {
63 $mform =& $this->_form;
64
65 // First show fields specific to this type of block.
66 $this->specific_definition($mform);
67
68 // Then show the fields about where this block appears.
69 $mform->addElement('header', 'whereheader', get_string('wherethisblockappears', 'block'));
70
71 // If the current weight of the block is out-of-range, add that option in.
72 $blockweight = $this->block->instance->weight;
73 $weightoptions = array();
2cdb8d84 74 if ($blockweight < -block_manager::MAX_WEIGHT) {
1d13c75c 75 $weightoptions[$blockweight] = $blockweight;
76 }
2cdb8d84 77 for ($i = -block_manager::MAX_WEIGHT; $i <= block_manager::MAX_WEIGHT; $i++) {
1d13c75c 78 $weightoptions[$i] = $i;
79 }
2cdb8d84 80 if ($blockweight > block_manager::MAX_WEIGHT) {
1d13c75c 81 $weightoptions[$blockweight] = $blockweight;
82 }
83 $first = reset($weightoptions);
84 $weightoptions[$first] = get_string('bracketfirst', 'block', $first);
85 $last = end($weightoptions);
86 $weightoptions[$last] = get_string('bracketlast', 'block', $last);
87
88 $regionoptions = $this->page->theme->get_all_block_regions();
89
d197ea43 90 $parentcontext = context::instance_by_id($this->block->instance->parentcontextid);
0aed347f
MD
91 $mform->addElement('hidden', 'bui_parentcontextid', $parentcontext->id);
92
b1627a92
DC
93 $mform->addElement('static', 'bui_homecontext', get_string('createdat', 'block'), print_context_name($parentcontext));
94 $mform->addHelpButton('bui_homecontext', 'createdat', 'block');
95
d4e71a4e
EL
96 // For pre-calculated (fixed) pagetype lists
97 $pagetypelist = array();
98
b1627a92
DC
99 // parse pagetype patterns
100 $bits = explode('-', $this->page->pagetype);
101
d4e71a4e
EL
102 // First of all, check if we are editing blocks @ front-page or no and
103 // make some dark magic if so (MDL-30340) because each page context
104 // implies one (and only one) harcoded page-type that will be set later
105 // when processing the form data at {@link block_manager::process_url_edit()}
106
107 // There are some conditions to check related to contexts
108 $ctxconditions = $this->page->context->contextlevel == CONTEXT_COURSE &&
109 $this->page->context->instanceid == get_site()->id;
110 // And also some pagetype conditions
111 $pageconditions = isset($bits[0]) && isset($bits[1]) && $bits[0] == 'site' && $bits[1] == 'index';
112 // So now we can be 100% sure if edition is happening at frontpage
113 $editingatfrontpage = $ctxconditions && $pageconditions;
114
115 // Let the form to know about that, can be useful later
116 $mform->addElement('hidden', 'bui_editingatfrontpage', (int)$editingatfrontpage);
117
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
128
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);
133
b1627a92
DC
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);
02ba576c 142 } else {
0aed347f 143 $parentcontextname = print_context_name($parentcontext);
b1627a92
DC
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);
02ba576c 147 }
1d13c75c 148
d4e71a4e
EL
149 // Generate pagetype patterns by callbacks if necessary (has not been set specifically)
150 if (empty($pagetypelist)) {
151 $pagetypelist = generate_page_type_patterns($this->page->pagetype, $parentcontext, $this->page->context);
152 $displaypagetypewarning = false;
153 if (!array_key_exists($this->block->instance->pagetypepattern, $pagetypelist)) {
154 // Pushing block's existing page type pattern
155 $pagetypestringname = 'page-'.str_replace('*', 'x', $this->block->instance->pagetypepattern);
156 if (get_string_manager()->string_exists($pagetypestringname, 'pagetype')) {
157 $pagetypelist[$this->block->instance->pagetypepattern] = get_string($pagetypestringname, 'pagetype');
158 } else {
159 //as a last resort we could put the page type pattern in the select box
160 //however this causes mod-data-view to be added if the only option available is mod-data-*
161 // so we are just showing a warning to users about their prev setting being reset
162 $displaypagetypewarning = true;
163 }
35e06465
MD
164 }
165 }
b1627a92
DC
166
167 // hide page type pattern select box if there is only one choice
168 if (count($pagetypelist) > 1) {
d097ad56
AD
169 if ($displaypagetypewarning) {
170 $mform->addElement('static', 'pagetypewarning', '', get_string('pagetypewarning','block'));
171 }
172
b1627a92
DC
173 $mform->addElement('select', 'bui_pagetypepattern', get_string('restrictpagetypes', 'block'), $pagetypelist);
174 } else {
b3aad0a2
PS
175 $values = array_keys($pagetypelist);
176 $value = array_pop($values);
b1627a92 177 $mform->addElement('hidden', 'bui_pagetypepattern', $value);
d4e71a4e
EL
178 // Now we are really hiding a lot (both page-contexts and page-type-patterns),
179 // specially in some systemcontext pages having only one option (my/user...)
180 // so, until it's decided if we are going to add the 'bring-back' pattern to
181 // all those pages or no (see MDL-30574), we are going to show the unique
182 // element statically
183 // TODO: Revisit this once MDL-30574 has been decided and implemented, although
184 // perhaps it's not bad to always show this statically when only one pattern is
185 // available.
186 if (!$editingatfrontpage) {
187 // Try to beautify it
188 $strvalue = $value;
189 $strkey = 'page-'.str_replace('*', 'x', $strvalue);
190 if (get_string_manager()->string_exists($strkey, 'pagetype')) {
191 $strvalue = get_string($strkey, 'pagetype');
192 }
193 // Show as static (hidden has been set already)
194 $mform->addElement('static', 'bui_staticpagetypepattern',
195 get_string('restrictpagetypes','block'), $strvalue);
196 }
b1627a92 197 }
1d13c75c 198
199 if ($this->page->subpage) {
b1627a92
DC
200 if ($parentcontext->contextlevel == CONTEXT_USER) {
201 $mform->addElement('hidden', 'bui_subpagepattern', '%@NULL@%');
202 } else {
203 $subpageoptions = array(
204 '%@NULL@%' => get_string('anypagematchingtheabove', 'block'),
205 $this->page->subpage => get_string('thisspecificpage', 'block', $this->page->subpage),
206 );
207 $mform->addElement('select', 'bui_subpagepattern', get_string('subpages', 'block'), $subpageoptions);
208 }
1d13c75c 209 }
210
211 $defaultregionoptions = $regionoptions;
212 $defaultregion = $this->block->instance->defaultregion;
213 if (!array_key_exists($defaultregion, $defaultregionoptions)) {
214 $defaultregionoptions[$defaultregion] = $defaultregion;
215 }
a19f419d 216 $mform->addElement('select', 'bui_defaultregion', get_string('defaultregion', 'block'), $defaultregionoptions);
b1627a92 217 $mform->addHelpButton('bui_defaultregion', 'defaultregion', 'block');
1d13c75c 218
a19f419d 219 $mform->addElement('select', 'bui_defaultweight', get_string('defaultweight', 'block'), $weightoptions);
b1627a92 220 $mform->addHelpButton('bui_defaultweight', 'defaultweight', 'block');
1d13c75c 221
222 // Where this block is positioned on this page.
986bd835 223 $mform->addElement('header', 'onthispage', get_string('onthispage', 'block'));
1d13c75c 224
a19f419d 225 $mform->addElement('selectyesno', 'bui_visible', get_string('visible', 'block'));
1d13c75c 226
227 $blockregion = $this->block->instance->region;
228 if (!array_key_exists($blockregion, $regionoptions)) {
229 $regionoptions[$blockregion] = $blockregion;
230 }
a19f419d 231 $mform->addElement('select', 'bui_region', get_string('region', 'block'), $regionoptions);
1d13c75c 232
a19f419d 233 $mform->addElement('select', 'bui_weight', get_string('weight', 'block'), $weightoptions);
1d13c75c 234
d14edf06 235 $pagefields = array('bui_visible', 'bui_region', 'bui_weight');
236 if (!$this->block->user_can_edit()) {
237 $mform->hardFreezeAllVisibleExcept($pagefields);
238 }
239 if (!$this->page->user_can_edit_blocks()) {
240 $mform->hardFreeze($pagefields);
241 }
242
1d13c75c 243 $this->add_action_buttons();
244 }
245
246 function set_data($defaults) {
a23bbaa3 247 // Prefix bui_ on all the core field names.
02ba576c 248 $blockfields = array('showinsubcontexts', 'pagetypepattern', 'subpagepattern', 'parentcontextid',
a23bbaa3 249 'defaultregion', 'defaultweight', 'visible', 'region', 'weight');
250 foreach ($blockfields as $field) {
251 $newname = 'bui_' . $field;
252 $defaults->$newname = $defaults->$field;
253 }
254
1d13c75c 255 // Copy block config into config_ fields.
256 if (!empty($this->block->config)) {
257 foreach ($this->block->config as $field => $value) {
258 $configfield = 'config_' . $field;
259 $defaults->$configfield = $value;
260 }
261 }
262
263 // Munge ->subpagepattern becuase HTML selects don't play nicely with NULLs.
a23bbaa3 264 if (empty($defaults->bui_subpagepattern)) {
265 $defaults->bui_subpagepattern = '%@NULL@%';
1d13c75c 266 }
267
fe2fdd11 268 $systemcontext = context_system::instance();
0aed347f 269 if ($defaults->parentcontextid == $systemcontext->id) {
b1627a92 270 $defaults->bui_contexts = BUI_CONTEXTS_ENTIRE_SITE; // System-wide and sticky
0aed347f
MD
271 } else {
272 $defaults->bui_contexts = $defaults->bui_showinsubcontexts;
273 }
274
1d13c75c 275 parent::set_data($defaults);
276 }
277
278 /**
279 * Override this to create any form fields specific to this type of block.
280 * @param object $mform the form being built.
281 */
282 protected function specific_definition($mform) {
283 // By default, do nothing.
284 }
02ba576c 285}