MDL-21386 readding require_login() which I accidentally removed during one previous...
[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 *
24 * @package moodlecore
25 * @copyright 2009 Tim Hunt
26 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
27 */
28
29require_once($CFG->libdir . '/formslib.php');
30
31/**
32 * The base class form used by blocks/edit.php to edit block instance configuration.
33 *
34 * @copyright 2009 Tim Hunt
35 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
36 */
37class block_edit_form extends moodleform {
1d13c75c 38 /**
39 * The block instance we are editing.
40 * @var block_base
41 */
42 public $block;
43 /**
44 * The page we are editing this block in association with.
45 * @var moodle_page
46 */
47 public $page;
48
a19f419d 49 function __construct($actionurl, $block, $page) {
1d13c75c 50 global $CFG;
51 $this->block = $block;
52 $this->page = $page;
a19f419d 53 parent::moodleform($actionurl);
1d13c75c 54 }
55
56 function definition() {
57 $mform =& $this->_form;
58
59 // First show fields specific to this type of block.
60 $this->specific_definition($mform);
61
62 // Then show the fields about where this block appears.
63 $mform->addElement('header', 'whereheader', get_string('wherethisblockappears', 'block'));
64
65 // If the current weight of the block is out-of-range, add that option in.
66 $blockweight = $this->block->instance->weight;
67 $weightoptions = array();
2cdb8d84 68 if ($blockweight < -block_manager::MAX_WEIGHT) {
1d13c75c 69 $weightoptions[$blockweight] = $blockweight;
70 }
2cdb8d84 71 for ($i = -block_manager::MAX_WEIGHT; $i <= block_manager::MAX_WEIGHT; $i++) {
1d13c75c 72 $weightoptions[$i] = $i;
73 }
2cdb8d84 74 if ($blockweight > block_manager::MAX_WEIGHT) {
1d13c75c 75 $weightoptions[$blockweight] = $blockweight;
76 }
77 $first = reset($weightoptions);
78 $weightoptions[$first] = get_string('bracketfirst', 'block', $first);
79 $last = end($weightoptions);
80 $weightoptions[$last] = get_string('bracketlast', 'block', $last);
81
82 $regionoptions = $this->page->theme->get_all_block_regions();
83
84 $parentcontext = get_context_instance_by_id($this->block->instance->parentcontextid);
02ba576c
MD
85
86 // Check if the block is on the front page (explictly or as system block): MDL-21375
87 if ($parentcontext->contextlevel == CONTEXT_COURSE && $parentcontext->instanceid == SITEID) {
88 $frontpagecontext = $parentcontext;
89 $systemcontext = get_context_instance(CONTEXT_SYSTEM);
90 } else if ($parentcontext->contextlevel == CONTEXT_SYSTEM) {
91 $systemcontext = $parentcontext;
92 $frontpagecontext = get_context_instance(CONTEXT_COURSE, SITEID);
93 }
94
95 if (!empty($frontpagecontext)) { // This block appears on the front page, offer a choice
96 $contextoptions = array();
97 $contextoptions[$frontpagecontext->id] = print_context_name($frontpagecontext);
98 $contextoptions[$systemcontext->id] = print_context_name($systemcontext);
99 $mform->addElement('select', 'bui_parentcontextid', get_string('thisblockbelongsto', 'block'), $contextoptions);
100 } else {
101 $mform->addElement('static', 'contextname', get_string('thisblockbelongsto', 'block'), print_context_name($parentcontext));
102 }
1d13c75c 103
a19f419d 104 $mform->addElement('selectyesno', 'bui_showinsubcontexts', get_string('appearsinsubcontexts', 'block'));
1d13c75c 105
106 $pagetypeoptions = matching_page_type_patterns($this->page->pagetype);
107 $pagetypeoptions = array_combine($pagetypeoptions, $pagetypeoptions);
a19f419d 108 $mform->addElement('select', 'bui_pagetypepattern', get_string('pagetypes', 'block'), $pagetypeoptions);
1d13c75c 109
110 if ($this->page->subpage) {
111 $subpageoptions = array(
112 '%@NULL@%' => get_string('anypagematchingtheabove', 'block'),
113 $this->page->subpage => get_string('thisspecificpage', 'block', $this->page->subpage),
114 );
a19f419d 115 $mform->addElement('select', 'bui_subpagepattern', get_string('subpages', 'block'), $subpageoptions);
1d13c75c 116 }
117
118 $defaultregionoptions = $regionoptions;
119 $defaultregion = $this->block->instance->defaultregion;
120 if (!array_key_exists($defaultregion, $defaultregionoptions)) {
121 $defaultregionoptions[$defaultregion] = $defaultregion;
122 }
a19f419d 123 $mform->addElement('select', 'bui_defaultregion', get_string('defaultregion', 'block'), $defaultregionoptions);
1d13c75c 124
a19f419d 125 $mform->addElement('select', 'bui_defaultweight', get_string('defaultweight', 'block'), $weightoptions);
1d13c75c 126
127 // Where this block is positioned on this page.
128 $mform->addElement('header', 'whereheader', get_string('onthispage', 'block'));
129
a19f419d 130 $mform->addElement('selectyesno', 'bui_visible', get_string('visible', 'block'));
1d13c75c 131
132 $blockregion = $this->block->instance->region;
133 if (!array_key_exists($blockregion, $regionoptions)) {
134 $regionoptions[$blockregion] = $blockregion;
135 }
a19f419d 136 $mform->addElement('select', 'bui_region', get_string('region', 'block'), $regionoptions);
1d13c75c 137
a19f419d 138 $mform->addElement('select', 'bui_weight', get_string('weight', 'block'), $weightoptions);
1d13c75c 139
d14edf06 140 $pagefields = array('bui_visible', 'bui_region', 'bui_weight');
141 if (!$this->block->user_can_edit()) {
142 $mform->hardFreezeAllVisibleExcept($pagefields);
143 }
144 if (!$this->page->user_can_edit_blocks()) {
145 $mform->hardFreeze($pagefields);
146 }
147
1d13c75c 148 $this->add_action_buttons();
149 }
150
151 function set_data($defaults) {
a23bbaa3 152 // Prefix bui_ on all the core field names.
02ba576c 153 $blockfields = array('showinsubcontexts', 'pagetypepattern', 'subpagepattern', 'parentcontextid',
a23bbaa3 154 'defaultregion', 'defaultweight', 'visible', 'region', 'weight');
155 foreach ($blockfields as $field) {
156 $newname = 'bui_' . $field;
157 $defaults->$newname = $defaults->$field;
158 }
159
1d13c75c 160 // Copy block config into config_ fields.
161 if (!empty($this->block->config)) {
162 foreach ($this->block->config as $field => $value) {
163 $configfield = 'config_' . $field;
164 $defaults->$configfield = $value;
165 }
166 }
167
168 // Munge ->subpagepattern becuase HTML selects don't play nicely with NULLs.
a23bbaa3 169 if (empty($defaults->bui_subpagepattern)) {
170 $defaults->bui_subpagepattern = '%@NULL@%';
1d13c75c 171 }
172
173 parent::set_data($defaults);
174 }
175
176 /**
177 * Override this to create any form fields specific to this type of block.
178 * @param object $mform the form being built.
179 */
180 protected function specific_definition($mform) {
181 // By default, do nothing.
182 }
02ba576c 183}