3 // This file is part of Moodle - http://moodle.org/
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.
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.
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/>.
19 * Defines the base class form used by blocks/edit.php to edit block instance configuration.
21 * It works with the {@link block_edit_form} class, or rather the particular
22 * subclass defined by this block, to do the editing.
26 * @copyright 2009 Tim Hunt
27 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
30 if (!defined('MOODLE_INTERNAL')) {
31 die('Direct access to this script is forbidden.'); /// It must be included from a Moodle page
34 require_once($CFG->libdir
. '/formslib.php');
35 require_once($CFG->libdir
. '/blocklib.php');
38 * The base class form used by blocks/edit.php to edit block instance configuration.
40 * @copyright 2009 Tim Hunt
41 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
43 class block_edit_form
extends moodleform
{
45 * The block instance we are editing.
50 * The page we are editing this block in association with.
55 function __construct($actionurl, $block, $page) {
57 $this->block
= $block;
59 parent
::moodleform($actionurl);
62 function definition() {
63 $mform =& $this->_form
;
65 // First show fields specific to this type of block.
66 $this->specific_definition($mform);
68 // Then show the fields about where this block appears.
69 $mform->addElement('header', 'whereheader', get_string('wherethisblockappears', 'block'));
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();
74 if ($blockweight < -block_manager
::MAX_WEIGHT
) {
75 $weightoptions[$blockweight] = $blockweight;
77 for ($i = -block_manager
::MAX_WEIGHT
; $i <= block_manager
::MAX_WEIGHT
; $i++
) {
78 $weightoptions[$i] = $i;
80 if ($blockweight > block_manager
::MAX_WEIGHT
) {
81 $weightoptions[$blockweight] = $blockweight;
83 $first = reset($weightoptions);
84 $weightoptions[$first] = get_string('bracketfirst', 'block', $first);
85 $last = end($weightoptions);
86 $weightoptions[$last] = get_string('bracketlast', 'block', $last);
88 $regionoptions = $this->page
->theme
->get_all_block_regions();
90 $parentcontext = get_context_instance_by_id($this->block
->instance
->parentcontextid
);
91 $mform->addElement('hidden', 'bui_parentcontextid', $parentcontext->id
);
93 $mform->addElement('static', 'bui_homecontext', get_string('createdat', 'block'), print_context_name($parentcontext));
94 $mform->addHelpButton('bui_homecontext', 'createdat', 'block');
96 // parse pagetype patterns
97 $bits = explode('-', $this->page
->pagetype
);
99 $contextoptions = array();
100 if ( ($parentcontext->contextlevel
== CONTEXT_COURSE
&& $parentcontext->instanceid
== SITEID
) ||
101 ($parentcontext->contextlevel
== CONTEXT_SYSTEM
)) { // Home page
102 if ($bits[0] == 'tag') {
103 // tag always use system context, the contexts options don't make differences, so we use
104 // page type patterns only
105 $mform->addElement('hidden', 'bui_contexts', BUI_CONTEXTS_ENTIRE_SITE
);
107 $contextoptions[BUI_CONTEXTS_FRONTPAGE_ONLY
] = get_string('showonfrontpageonly', 'block');
108 $contextoptions[BUI_CONTEXTS_FRONTPAGE_SUBS
] = get_string('showonfrontpageandsubs', 'block');
109 $contextoptions[BUI_CONTEXTS_ENTIRE_SITE
] = get_string('showonentiresite', 'block');
110 $mform->addElement('select', 'bui_contexts', get_string('contexts', 'block'), $contextoptions);
111 $mform->addHelpButton('bui_contexts', 'contexts', 'block');
113 } else if ($parentcontext->contextlevel
== CONTEXT_COURSE
) {
114 // 0 means display on current context only, not child contexts
115 // but if course managers select mod-* as pagetype patterns, block system will overwrite this option
116 // to 1 (display on current context and child contexts)
117 $mform->addElement('hidden', 'bui_contexts', BUI_CONTEXTS_CURRENT
);
118 } else if ($parentcontext->contextlevel
== CONTEXT_MODULE
or $parentcontext->contextlevel
== CONTEXT_USER
) {
119 // module context doesn't have child contexts, so display in current context only
120 $mform->addElement('hidden', 'bui_contexts', BUI_CONTEXTS_CURRENT
);
122 $parentcontextname = print_context_name($parentcontext);
123 $contextoptions[BUI_CONTEXTS_CURRENT
] = get_string('showoncontextonly', 'block', $parentcontextname);
124 $contextoptions[BUI_CONTEXTS_CURRENT_SUBS
] = get_string('showoncontextandsubs', 'block', $parentcontextname);
125 $mform->addElement('select', 'bui_contexts', get_string('contexts', 'block'), $contextoptions);
128 if ($this->page
->pagetype
== 'site-index') { // No need for pagetype list on home page
129 $pagetypelist = array('*'=>get_string('page-x', 'pagetype'));
131 // Generate pagetype patterns by callbacks
132 $pagetypelist = generate_page_type_patterns($this->page
->pagetype
, $parentcontext, $this->page
->context
);
133 if (!array_key_exists($this->block
->instance
->pagetypepattern
, $pagetypelist)) {
134 // Pushing block's existing page type pattern
135 $pagetypestringname = 'page-'.str_replace('*', 'x', $this->block
->instance
->pagetypepattern
);
136 if (get_string_manager()->string_exists($pagetypestringname, 'pagetype')) {
137 $pagetyelist[$this->block
->instance
->pagetypepattern
] = get_string($pagetypestringname, 'pagetype');
142 // hide page type pattern select box if there is only one choice
143 if (count($pagetypelist) > 1) {
144 $mform->addElement('select', 'bui_pagetypepattern', get_string('restrictpagetypes', 'block'), $pagetypelist);
146 $value = array_pop(array_keys($pagetypelist));
147 $mform->addElement('hidden', 'bui_pagetypepattern', $value);
150 if ($this->page
->subpage
) {
151 if ($parentcontext->contextlevel
== CONTEXT_USER
) {
152 $mform->addElement('hidden', 'bui_subpagepattern', '%@NULL@%');
154 $subpageoptions = array(
155 '%@NULL@%' => get_string('anypagematchingtheabove', 'block'),
156 $this->page
->subpage
=> get_string('thisspecificpage', 'block', $this->page
->subpage
),
158 $mform->addElement('select', 'bui_subpagepattern', get_string('subpages', 'block'), $subpageoptions);
162 $defaultregionoptions = $regionoptions;
163 $defaultregion = $this->block
->instance
->defaultregion
;
164 if (!array_key_exists($defaultregion, $defaultregionoptions)) {
165 $defaultregionoptions[$defaultregion] = $defaultregion;
167 $mform->addElement('select', 'bui_defaultregion', get_string('defaultregion', 'block'), $defaultregionoptions);
168 $mform->addHelpButton('bui_defaultregion', 'defaultregion', 'block');
170 $mform->addElement('select', 'bui_defaultweight', get_string('defaultweight', 'block'), $weightoptions);
171 $mform->addHelpButton('bui_defaultweight', 'defaultweight', 'block');
173 // Where this block is positioned on this page.
174 $mform->addElement('header', 'whereheader', get_string('onthispage', 'block'));
176 $mform->addElement('selectyesno', 'bui_visible', get_string('visible', 'block'));
178 $blockregion = $this->block
->instance
->region
;
179 if (!array_key_exists($blockregion, $regionoptions)) {
180 $regionoptions[$blockregion] = $blockregion;
182 $mform->addElement('select', 'bui_region', get_string('region', 'block'), $regionoptions);
184 $mform->addElement('select', 'bui_weight', get_string('weight', 'block'), $weightoptions);
186 $pagefields = array('bui_visible', 'bui_region', 'bui_weight');
187 if (!$this->block
->user_can_edit()) {
188 $mform->hardFreezeAllVisibleExcept($pagefields);
190 if (!$this->page
->user_can_edit_blocks()) {
191 $mform->hardFreeze($pagefields);
194 $this->add_action_buttons();
197 function set_data($defaults) {
198 // Prefix bui_ on all the core field names.
199 $blockfields = array('showinsubcontexts', 'pagetypepattern', 'subpagepattern', 'parentcontextid',
200 'defaultregion', 'defaultweight', 'visible', 'region', 'weight');
201 foreach ($blockfields as $field) {
202 $newname = 'bui_' . $field;
203 $defaults->$newname = $defaults->$field;
206 // Copy block config into config_ fields.
207 if (!empty($this->block
->config
)) {
208 foreach ($this->block
->config
as $field => $value) {
209 $configfield = 'config_' . $field;
210 $defaults->$configfield = $value;
214 // Munge ->subpagepattern becuase HTML selects don't play nicely with NULLs.
215 if (empty($defaults->bui_subpagepattern
)) {
216 $defaults->bui_subpagepattern
= '%@NULL@%';
219 $systemcontext = get_context_instance(CONTEXT_SYSTEM
);
220 if ($defaults->parentcontextid
== $systemcontext->id
) {
221 $defaults->bui_contexts
= BUI_CONTEXTS_ENTIRE_SITE
; // System-wide and sticky
223 $defaults->bui_contexts
= $defaults->bui_showinsubcontexts
;
226 parent
::set_data($defaults);
230 * Override this to create any form fields specific to this type of block.
231 * @param object $mform the form being built.
233 protected function specific_definition($mform) {
234 // By default, do nothing.