webservice MDL-21127 add IP address for every web service logs.
[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);
0aed347f
MD
85 $mform->addElement('hidden', 'bui_parentcontextid', $parentcontext->id);
86
87 $contextoptions = array();
88 if ( ($parentcontext->contextlevel == CONTEXT_COURSE && $parentcontext->instanceid == SITEID) ||
89 ($parentcontext->contextlevel == CONTEXT_SYSTEM)) {
90 $contextoptions[0] = get_string('showonfrontpageonly', 'block');
91 $contextoptions[1] = get_string('showonfrontpageandsubs', 'block');
92 $contextoptions[2] = get_string('showonentiresite', 'block');
02ba576c 93 } else {
0aed347f
MD
94 $parentcontextname = print_context_name($parentcontext);
95 $contextoptions[0] = get_string('showoncontextonly', 'block', $parentcontextname);
96 $contextoptions[1] = get_string('showoncontextandsubs', 'block', $parentcontextname);
02ba576c 97 }
0aed347f 98 $mform->addElement('select', 'bui_contexts', get_string('contexts', 'block'), $contextoptions);
1d13c75c 99
100 $pagetypeoptions = matching_page_type_patterns($this->page->pagetype);
101 $pagetypeoptions = array_combine($pagetypeoptions, $pagetypeoptions);
a19f419d 102 $mform->addElement('select', 'bui_pagetypepattern', get_string('pagetypes', 'block'), $pagetypeoptions);
1d13c75c 103
104 if ($this->page->subpage) {
105 $subpageoptions = array(
106 '%@NULL@%' => get_string('anypagematchingtheabove', 'block'),
107 $this->page->subpage => get_string('thisspecificpage', 'block', $this->page->subpage),
108 );
a19f419d 109 $mform->addElement('select', 'bui_subpagepattern', get_string('subpages', 'block'), $subpageoptions);
1d13c75c 110 }
111
112 $defaultregionoptions = $regionoptions;
113 $defaultregion = $this->block->instance->defaultregion;
114 if (!array_key_exists($defaultregion, $defaultregionoptions)) {
115 $defaultregionoptions[$defaultregion] = $defaultregion;
116 }
a19f419d 117 $mform->addElement('select', 'bui_defaultregion', get_string('defaultregion', 'block'), $defaultregionoptions);
1d13c75c 118
a19f419d 119 $mform->addElement('select', 'bui_defaultweight', get_string('defaultweight', 'block'), $weightoptions);
1d13c75c 120
121 // Where this block is positioned on this page.
122 $mform->addElement('header', 'whereheader', get_string('onthispage', 'block'));
123
a19f419d 124 $mform->addElement('selectyesno', 'bui_visible', get_string('visible', 'block'));
1d13c75c 125
126 $blockregion = $this->block->instance->region;
127 if (!array_key_exists($blockregion, $regionoptions)) {
128 $regionoptions[$blockregion] = $blockregion;
129 }
a19f419d 130 $mform->addElement('select', 'bui_region', get_string('region', 'block'), $regionoptions);
1d13c75c 131
a19f419d 132 $mform->addElement('select', 'bui_weight', get_string('weight', 'block'), $weightoptions);
1d13c75c 133
d14edf06 134 $pagefields = array('bui_visible', 'bui_region', 'bui_weight');
135 if (!$this->block->user_can_edit()) {
136 $mform->hardFreezeAllVisibleExcept($pagefields);
137 }
138 if (!$this->page->user_can_edit_blocks()) {
139 $mform->hardFreeze($pagefields);
140 }
141
1d13c75c 142 $this->add_action_buttons();
143 }
144
145 function set_data($defaults) {
a23bbaa3 146 // Prefix bui_ on all the core field names.
02ba576c 147 $blockfields = array('showinsubcontexts', 'pagetypepattern', 'subpagepattern', 'parentcontextid',
a23bbaa3 148 'defaultregion', 'defaultweight', 'visible', 'region', 'weight');
149 foreach ($blockfields as $field) {
150 $newname = 'bui_' . $field;
151 $defaults->$newname = $defaults->$field;
152 }
153
1d13c75c 154 // Copy block config into config_ fields.
155 if (!empty($this->block->config)) {
156 foreach ($this->block->config as $field => $value) {
157 $configfield = 'config_' . $field;
158 $defaults->$configfield = $value;
159 }
160 }
161
162 // Munge ->subpagepattern becuase HTML selects don't play nicely with NULLs.
a23bbaa3 163 if (empty($defaults->bui_subpagepattern)) {
164 $defaults->bui_subpagepattern = '%@NULL@%';
1d13c75c 165 }
166
0aed347f
MD
167 $systemcontext = get_context_instance(CONTEXT_SYSTEM);
168 if ($defaults->parentcontextid == $systemcontext->id) {
169 $defaults->bui_contexts = 2; // System-wide and sticky
170 } else {
171 $defaults->bui_contexts = $defaults->bui_showinsubcontexts;
172 }
173
1d13c75c 174 parent::set_data($defaults);
175 }
176
177 /**
178 * Override this to create any form fields specific to this type of block.
179 * @param object $mform the form being built.
180 */
181 protected function specific_definition($mform) {
182 // By default, do nothing.
183 }
02ba576c 184}