MDL-27120 backup: fixed docblock and code cleanup
[moodle.git] / backup / util / ui / base_moodleform.class.php
1 <?php
3 // This file is part of Moodle - http://moodle.org/
4 //
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.
14 //
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/>.
18 /**
19  * This file contains the generic moodleform bridge for the backup user interface
20  * as well as the individual forms that relate to the different stages the user
21  * interface can exist within.
22  *
23  * @package   moodlecore
24  * @copyright 2010 Sam Hemelryk
25  * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
26  */
28 defined('MOODLE_INTERNAL') || die();
30 require_once($CFG->libdir . '/formslib.php');
32 /**
33  * Backup moodleform bridge
34  *
35  * Ahhh the mighty moodleform bridge! Strong enough to take the weight of 682 full
36  * grown african swallows all of whom have been carring coconuts for several days.
37  * EWWWWW!!!!!!!!!!!!!!!!!!!!!!!!
38  *
39  * @copyright 2010 Sam Hemelryk
40  * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
41  */
42 abstract class base_moodleform extends moodleform {
43     /**
44      * The stage this form belongs to
45      * @var base_ui_stage
46      */
47     protected $uistage = null;
48     /**
49      * True if we have a course div open, false otherwise
50      * @var bool
51      */
52     protected $coursediv = false;
53     /**
54      * True if we have a section div open, false otherwise
55      * @var bool
56      */
57     protected $sectiondiv = false;
58     /**
59      * True if we have an activity div open, false otherwise
60      * @var bool
61      */
62     protected $activitydiv = false;
63     /**
64      * Creates the form
65      *
66      * @param backup_ui_stage $uistage
67      * @param moodle_url|string $action
68      * @param mixed $customdata
69      * @param string $method get|post
70      * @param string $target
71      * @param array $attributes
72      * @param bool $editable
73      */
74     function __construct(base_ui_stage $uistage, $action=null, $customdata=null, $method='post', $target='', $attributes=null, $editable=true) {
75         $this->uistage = $uistage;
76         parent::__construct($action, $customdata, $method, $target, $attributes, $editable);
77     }
78     /**
79      * The standard form definition... obviously not much here
80      */
81     function definition() {
82         $ui = $this->uistage->get_ui();
83         $mform = $this->_form;
84         $stage = $mform->addElement('hidden', 'stage', $this->uistage->get_stage());
85         $stage = $mform->addElement('hidden', $ui->get_name(), $ui->get_uniqueid());
86         $params = $this->uistage->get_params();
87         if (is_array($params) && count($params) > 0) {
88             foreach ($params as $name=>$value) {
89                 $stage = $mform->addElement('hidden', $name, $value);
90             }
91         }
92     }
93     /**
94      * Definition applied after the data is organised.. why's it here? because I want
95      * to add elements on the fly.
96      * @global moodle_page $PAGE
97      */
98     function definition_after_data() {
99         global $PAGE;
100         $buttonarray=array();
101         $buttonarray[] = $this->_form->createElement('submit', 'submitbutton', get_string($this->uistage->get_ui()->get_name().'stage'.$this->uistage->get_stage().'action', 'backup'), array('class'=>'proceedbutton'));
102         if (!$this->uistage->is_first_stage()) {
103             $buttonarray[] = $this->_form->createElement('submit', 'previous', get_string('previousstage','backup'));
104         }
105         $buttonarray[] = $this->_form->createElement('cancel', 'cancel', get_string('cancel'), array('class'=>'confirmcancel'));
106         $this->_form->addGroup($buttonarray, 'buttonar', '', array(' '), false);
107         $this->_form->closeHeaderBefore('buttonar');
109         $config = new stdClass;
110         $config->title = get_string('confirmcancel', 'backup');
111         $config->question = get_string('confirmcancelquestion', 'backup');
112         $config->yesLabel = get_string('confirmcancelyes', 'backup');
113         $config->noLabel = get_string('confirmcancelno', 'backup');
114         $PAGE->requires->yui_module('moodle-backup-confirmcancel', 'M.core_backup.watch_cancel_buttons', array($config));
115     }
116     /**
117      * Closes any open divs
118      */
119     function close_task_divs() {
120         if ($this->activitydiv) {
121             $this->_form->addElement('html', html_writer::end_tag('div'));
122             $this->activitydiv = false;
123         }
124         if ($this->sectiondiv) {
125             $this->_form->addElement('html', html_writer::end_tag('div'));
126             $this->sectiondiv = false;
127         }
128         if ($this->coursediv) {
129             $this->_form->addElement('html', html_writer::end_tag('div'));
130             $this->coursediv = false;
131         }
132     }
133     /**
134      * Adds the backup_setting as a element to the form
135      * @param backup_setting $setting
136      * @return bool
137      */
138     function add_setting(backup_setting $setting, base_task $task=null) {
139         return $this->add_settings(array(array($setting, $task)));
140     }
141     /**
142      * Adds multiple backup_settings as elements to the form
143      * @param array $settingstasks Consists of array($setting, $task) elements
144      * @return bool
145      */
146     public function add_settings(array $settingstasks) {
147         global $OUTPUT;
149         $defaults = array();
150         foreach ($settingstasks as $st) {
151             list($setting, $task) = $st;
152             // If the setting cant be changed or isn't visible then add it as a fixed setting.
153             if (!$setting->get_ui()->is_changeable() || $setting->get_visibility() != backup_setting::VISIBLE) {
154                 $this->add_fixed_setting($setting, $task);
155                 continue;
156             }
158             // First add the formatting for this setting
159             $this->add_html_formatting($setting);
161             // Then call the add method with the get_element_properties array
162             call_user_func_array(array($this->_form, 'addElement'), $setting->get_ui()->get_element_properties($task, $OUTPUT));
163             $defaults[$setting->get_ui_name()] = $setting->get_value();
164             if ($setting->has_help()) {
165                 list($identifier, $component) = $setting->get_help();
166                 $this->_form->addHelpButton($setting->get_ui_name(), $identifier, $component);
167             }
168             $this->_form->addElement('html', html_writer::end_tag('div'));
169         }
170         $this->_form->setDefaults($defaults);
171         return true;
172     }
173     /**
174      * Adds a heading to the form
175      * @param string $name
176      * @param string $text
177      */
178     function add_heading($name , $text) {
179         $this->_form->addElement('header', $name, $text);
180     }
181     /**
182      * Adds HTML formatting for the given backup setting, needed to group/segment
183      * correctly.
184      * @param backup_setting $setting
185      */
186     protected function add_html_formatting(backup_setting $setting) {
187         $mform = $this->_form;
188         $isincludesetting = (strpos($setting->get_name(), '_include')!==false);
189         if ($isincludesetting && $setting->get_level() != backup_setting::ROOT_LEVEL)  {
190             switch ($setting->get_level()) {
191                 case backup_setting::COURSE_LEVEL:
192                     if ($this->activitydiv) {
193                         $this->_form->addElement('html', html_writer::end_tag('div'));
194                         $this->activitydiv = false;
195                     }
196                     if ($this->sectiondiv) {
197                         $this->_form->addElement('html', html_writer::end_tag('div'));
198                         $this->sectiondiv = false;
199                     }
200                     if ($this->coursediv) {
201                         $this->_form->addElement('html', html_writer::end_tag('div'));
202                     }
203                     $mform->addElement('html', html_writer::start_tag('div', array('class'=>'grouped_settings course_level')));
204                     $mform->addElement('html', html_writer::start_tag('div', array('class'=>'include_setting course_level')));
205                     $this->coursediv = true;
206                     break;
207                 case backup_setting::SECTION_LEVEL:
208                     if ($this->activitydiv) {
209                         $this->_form->addElement('html', html_writer::end_tag('div'));
210                         $this->activitydiv = false;
211                     }
212                     if ($this->sectiondiv) {
213                         $this->_form->addElement('html', html_writer::end_tag('div'));
214                     }
215                     $mform->addElement('html', html_writer::start_tag('div', array('class'=>'grouped_settings section_level')));
216                     $mform->addElement('html', html_writer::start_tag('div', array('class'=>'include_setting section_level')));
217                     $this->sectiondiv = true;
218                     break;
219                 case backup_setting::ACTIVITY_LEVEL:
220                     if ($this->activitydiv) {
221                         $this->_form->addElement('html', html_writer::end_tag('div'));
222                     }
223                     $mform->addElement('html', html_writer::start_tag('div', array('class'=>'grouped_settings activity_level')));
224                     $mform->addElement('html', html_writer::start_tag('div', array('class'=>'include_setting activity_level')));
225                     $this->activitydiv = true;
226                     break;
227                 default:
228                     $mform->addElement('html', html_writer::start_tag('div', array('class'=>'normal_setting')));
229                     break;
230             }
231         } else if ($setting->get_level() == backup_setting::ROOT_LEVEL) {
232             $mform->addElement('html', html_writer::start_tag('div', array('class'=>'root_setting')));
233         } else {
234             $mform->addElement('html', html_writer::start_tag('div', array('class'=>'normal_setting')));
235         }
236     }
237     /**
238      * Adds a fixed or static setting to the form
239      * @param backup_setting $setting
240      */
241     function add_fixed_setting(backup_setting $setting, base_task $task) {
242         global $OUTPUT;
243         $settingui = $setting->get_ui();
244         if ($setting->get_visibility() == backup_setting::VISIBLE) {
245             $this->add_html_formatting($setting);
246             switch ($setting->get_status()) {
247                 case backup_setting::LOCKED_BY_PERMISSION:
248                     $icon = ' '.$OUTPUT->pix_icon('i/permissionlock', get_string('lockedbypermission', 'backup'), 'moodle', array('class'=>'smallicon lockedicon permissionlock'));
249                     break;
250                 case backup_setting::LOCKED_BY_CONFIG:
251                     $icon = ' '.$OUTPUT->pix_icon('i/configlock', get_string('lockedbyconfig', 'backup'), 'moodle', array('class'=>'smallicon lockedicon configlock'));
252                     break;
253                 case backup_setting::LOCKED_BY_HIERARCHY:
254                     $icon = ' '.$OUTPUT->pix_icon('i/hierarchylock', get_string('lockedbyhierarchy', 'backup'), 'moodle', array('class'=>'smallicon lockedicon configlock'));
255                     break;
256                 default:
257                     $icon = '';
258                     break;
259             }
260             $label = $settingui->get_label($task);
261             $labelicon = $settingui->get_icon();
262             if (!empty($labelicon)) {
263                 $label .= '&nbsp;'.$OUTPUT->render($labelicon);
264             }
265             $this->_form->addElement('static', 'static_'.$settingui->get_name(), $label, $settingui->get_static_value().$icon);
266             $this->_form->addElement('html', html_writer::end_tag('div'));
267         }
268         $this->_form->addElement('hidden', $settingui->get_name(), $settingui->get_value());
269     }
270     /**
271      * Adds dependencies to the form recursively
272      *
273      * @param backup_setting $setting
274      */
275     function add_dependencies(backup_setting $setting) {
276         $mform = $this->_form;
277         // Apply all dependencies for backup
278         foreach ($setting->get_my_dependency_properties() as $key=>$dependency) {
279             call_user_func_array(array($this->_form, 'disabledIf'), $dependency);
280         }
281     }
282     /**
283      * Returns true if the form was cancelled, false otherwise
284      * @return bool
285      */
286     public function is_cancelled() {
287         return (optional_param('cancel', false, PARAM_BOOL) || parent::is_cancelled());
288     }
290     /**
291      * Removes an element from the form if it exists
292      * @param string $elementname
293      * @return bool
294      */
295     public function remove_element($elementname) {
296         if ($this->_form->elementExists($elementname)) {
297             return $this->_form->removeElement($elementname);
298         } else {
299             return false;
300         }
301     }
303     /**
304      * Gets an element from the form if it exists
305      *
306      * @param string $elementname
307      * @return HTML_QuickForm_input|MoodleQuickForm_group
308      */
309     public function get_element($elementname) {
310         if ($this->_form->elementExists($elementname)) {
311             return $this->_form->getElement($elementname);
312         } else {
313             return false;
314         }
315     }
317     /**
318      * Displays the form
319      */
320     public function display() {
321         $this->require_definition_after_data();
322         parent::display();
323     }
325     /**
326      * Ensures the the definition after data is loaded
327      */
328     public function require_definition_after_data() {
329         if (!$this->_definition_finalized) {
330             $this->_definition_finalized = true;
331             $this->definition_after_data();
332         }
333     }