MDL-30340 Trivial change in PHP doc block - the function does not accept any params
[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
90 $parentcontext = get_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
96 // parse pagetype patterns
97 $bits = explode('-', $this->page->pagetype);
98
0aed347f
MD
99 $contextoptions = array();
100 if ( ($parentcontext->contextlevel == CONTEXT_COURSE && $parentcontext->instanceid == SITEID) ||
b1627a92 101 ($parentcontext->contextlevel == CONTEXT_SYSTEM)) { // Home page
2f9bdadf
AD
102 if ($bits[0] == 'tag' || $bits[0] == 'admin') {
103 // tag and admin pages always use system context
800688bd 104 // the contexts options don't make differences, so we use
b1627a92
DC
105 // page type patterns only
106 $mform->addElement('hidden', 'bui_contexts', BUI_CONTEXTS_ENTIRE_SITE);
107 } else {
108 $contextoptions[BUI_CONTEXTS_FRONTPAGE_ONLY] = get_string('showonfrontpageonly', 'block');
109 $contextoptions[BUI_CONTEXTS_FRONTPAGE_SUBS] = get_string('showonfrontpageandsubs', 'block');
110 $contextoptions[BUI_CONTEXTS_ENTIRE_SITE] = get_string('showonentiresite', 'block');
111 $mform->addElement('select', 'bui_contexts', get_string('contexts', 'block'), $contextoptions);
112 $mform->addHelpButton('bui_contexts', 'contexts', 'block');
113 }
114 } else if ($parentcontext->contextlevel == CONTEXT_COURSE) {
115 // 0 means display on current context only, not child contexts
116 // but if course managers select mod-* as pagetype patterns, block system will overwrite this option
117 // to 1 (display on current context and child contexts)
118 $mform->addElement('hidden', 'bui_contexts', BUI_CONTEXTS_CURRENT);
119 } else if ($parentcontext->contextlevel == CONTEXT_MODULE or $parentcontext->contextlevel == CONTEXT_USER) {
120 // module context doesn't have child contexts, so display in current context only
121 $mform->addElement('hidden', 'bui_contexts', BUI_CONTEXTS_CURRENT);
02ba576c 122 } else {
0aed347f 123 $parentcontextname = print_context_name($parentcontext);
b1627a92
DC
124 $contextoptions[BUI_CONTEXTS_CURRENT] = get_string('showoncontextonly', 'block', $parentcontextname);
125 $contextoptions[BUI_CONTEXTS_CURRENT_SUBS] = get_string('showoncontextandsubs', 'block', $parentcontextname);
126 $mform->addElement('select', 'bui_contexts', get_string('contexts', 'block'), $contextoptions);
02ba576c 127 }
1d13c75c 128
d097ad56 129 $displaypagetypewarning = false;
35e06465 130 if ($this->page->pagetype == 'site-index') { // No need for pagetype list on home page
b1627a92 131 $pagetypelist = array('*'=>get_string('page-x', 'pagetype'));
35e06465 132 } else {
b1627a92
DC
133 // Generate pagetype patterns by callbacks
134 $pagetypelist = generate_page_type_patterns($this->page->pagetype, $parentcontext, $this->page->context);
135 if (!array_key_exists($this->block->instance->pagetypepattern, $pagetypelist)) {
136 // Pushing block's existing page type pattern
137 $pagetypestringname = 'page-'.str_replace('*', 'x', $this->block->instance->pagetypepattern);
138 if (get_string_manager()->string_exists($pagetypestringname, 'pagetype')) {
096928c7
AD
139 $pagetypelist[$this->block->instance->pagetypepattern] = get_string($pagetypestringname, 'pagetype');
140 } else {
d097ad56
AD
141 //as a last resort we could put the page type pattern in the select box
142 //however this causes mod-data-view to be added if the only option available is mod-data-*
143 // so we are just showing a warning to users about their prev setting being reset
144 $displaypagetypewarning = true;
b1627a92 145 }
35e06465
MD
146 }
147 }
b1627a92
DC
148
149 // hide page type pattern select box if there is only one choice
150 if (count($pagetypelist) > 1) {
d097ad56
AD
151 if ($displaypagetypewarning) {
152 $mform->addElement('static', 'pagetypewarning', '', get_string('pagetypewarning','block'));
153 }
154
b1627a92
DC
155 $mform->addElement('select', 'bui_pagetypepattern', get_string('restrictpagetypes', 'block'), $pagetypelist);
156 } else {
157 $value = array_pop(array_keys($pagetypelist));
158 $mform->addElement('hidden', 'bui_pagetypepattern', $value);
159 }
1d13c75c 160
161 if ($this->page->subpage) {
b1627a92
DC
162 if ($parentcontext->contextlevel == CONTEXT_USER) {
163 $mform->addElement('hidden', 'bui_subpagepattern', '%@NULL@%');
164 } else {
165 $subpageoptions = array(
166 '%@NULL@%' => get_string('anypagematchingtheabove', 'block'),
167 $this->page->subpage => get_string('thisspecificpage', 'block', $this->page->subpage),
168 );
169 $mform->addElement('select', 'bui_subpagepattern', get_string('subpages', 'block'), $subpageoptions);
170 }
1d13c75c 171 }
172
173 $defaultregionoptions = $regionoptions;
174 $defaultregion = $this->block->instance->defaultregion;
175 if (!array_key_exists($defaultregion, $defaultregionoptions)) {
176 $defaultregionoptions[$defaultregion] = $defaultregion;
177 }
a19f419d 178 $mform->addElement('select', 'bui_defaultregion', get_string('defaultregion', 'block'), $defaultregionoptions);
b1627a92 179 $mform->addHelpButton('bui_defaultregion', 'defaultregion', 'block');
1d13c75c 180
a19f419d 181 $mform->addElement('select', 'bui_defaultweight', get_string('defaultweight', 'block'), $weightoptions);
b1627a92 182 $mform->addHelpButton('bui_defaultweight', 'defaultweight', 'block');
1d13c75c 183
184 // Where this block is positioned on this page.
185 $mform->addElement('header', 'whereheader', get_string('onthispage', 'block'));
186
a19f419d 187 $mform->addElement('selectyesno', 'bui_visible', get_string('visible', 'block'));
1d13c75c 188
189 $blockregion = $this->block->instance->region;
190 if (!array_key_exists($blockregion, $regionoptions)) {
191 $regionoptions[$blockregion] = $blockregion;
192 }
a19f419d 193 $mform->addElement('select', 'bui_region', get_string('region', 'block'), $regionoptions);
1d13c75c 194
a19f419d 195 $mform->addElement('select', 'bui_weight', get_string('weight', 'block'), $weightoptions);
1d13c75c 196
d14edf06 197 $pagefields = array('bui_visible', 'bui_region', 'bui_weight');
198 if (!$this->block->user_can_edit()) {
199 $mform->hardFreezeAllVisibleExcept($pagefields);
200 }
201 if (!$this->page->user_can_edit_blocks()) {
202 $mform->hardFreeze($pagefields);
203 }
204
1d13c75c 205 $this->add_action_buttons();
206 }
207
208 function set_data($defaults) {
a23bbaa3 209 // Prefix bui_ on all the core field names.
02ba576c 210 $blockfields = array('showinsubcontexts', 'pagetypepattern', 'subpagepattern', 'parentcontextid',
a23bbaa3 211 'defaultregion', 'defaultweight', 'visible', 'region', 'weight');
212 foreach ($blockfields as $field) {
213 $newname = 'bui_' . $field;
214 $defaults->$newname = $defaults->$field;
215 }
216
1d13c75c 217 // Copy block config into config_ fields.
218 if (!empty($this->block->config)) {
219 foreach ($this->block->config as $field => $value) {
220 $configfield = 'config_' . $field;
221 $defaults->$configfield = $value;
222 }
223 }
224
225 // Munge ->subpagepattern becuase HTML selects don't play nicely with NULLs.
a23bbaa3 226 if (empty($defaults->bui_subpagepattern)) {
227 $defaults->bui_subpagepattern = '%@NULL@%';
1d13c75c 228 }
229
0aed347f
MD
230 $systemcontext = get_context_instance(CONTEXT_SYSTEM);
231 if ($defaults->parentcontextid == $systemcontext->id) {
b1627a92 232 $defaults->bui_contexts = BUI_CONTEXTS_ENTIRE_SITE; // System-wide and sticky
0aed347f
MD
233 } else {
234 $defaults->bui_contexts = $defaults->bui_showinsubcontexts;
235 }
236
1d13c75c 237 parent::set_data($defaults);
238 }
239
240 /**
241 * Override this to create any form fields specific to this type of block.
242 * @param object $mform the form being built.
243 */
244 protected function specific_definition($mform) {
245 // By default, do nothing.
246 }
02ba576c 247}