MDL-38192 Backup and restore: Allow selection by activity type
[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         $mform->setDisableShortforms();
85         $stage = $mform->addElement('hidden', 'stage', $this->uistage->get_stage());
86         $mform->setType('stage', PARAM_INT);
87         $stage = $mform->addElement('hidden', $ui->get_name(), $ui->get_uniqueid());
88         $mform->setType($ui->get_name(), PARAM_ALPHANUM);
89         $params = $this->uistage->get_params();
90         if (is_array($params) && count($params) > 0) {
91             foreach ($params as $name=>$value) {
92                 // TODO: Horrible hack, but current backup ui structure does not allow
93                 // to make this easy (only changing params to objects that would be
94                 // possible. MDL-38735.
95                 $intparams = array(
96                         'contextid', 'importid', 'target');
97                 $stage = $mform->addElement('hidden', $name, $value);
98                 if (in_array($name, $intparams)) {
99                     $mform->setType($name, PARAM_INT);
100                 } else {
101                     // Adding setType() to avoid missing setType() warnings.
102                     // MDL-39126: support $mform->setType() for additional backup parameters.
103                     $mform->setType($name, PARAM_RAW);
104                 }
105             }
106         }
107     }
108     /**
109      * Definition applied after the data is organised.. why's it here? because I want
110      * to add elements on the fly.
111      * @global moodle_page $PAGE
112      */
113     function definition_after_data() {
114         $buttonarray=array();
115         $buttonarray[] = $this->_form->createElement('submit', 'submitbutton', get_string($this->uistage->get_ui()->get_name().'stage'.$this->uistage->get_stage().'action', 'backup'), array('class'=>'proceedbutton'));
116         if (!$this->uistage->is_first_stage()) {
117             $buttonarray[] = $this->_form->createElement('submit', 'previous', get_string('previousstage','backup'));
118         }
119         $buttonarray[] = $this->_form->createElement('cancel', 'cancel', get_string('cancel'), array('class'=>'confirmcancel'));
120         $this->_form->addGroup($buttonarray, 'buttonar', '', array(' '), false);
121         $this->_form->closeHeaderBefore('buttonar');
123         $this->_definition_finalized = true;
124     }
126     /**
127      * Closes any open divs
128      */
129     function close_task_divs() {
130         if ($this->activitydiv) {
131             $this->_form->addElement('html', html_writer::end_tag('div'));
132             $this->activitydiv = false;
133         }
134         if ($this->sectiondiv) {
135             $this->_form->addElement('html', html_writer::end_tag('div'));
136             $this->sectiondiv = false;
137         }
138         if ($this->coursediv) {
139             $this->_form->addElement('html', html_writer::end_tag('div'));
140             $this->coursediv = false;
141         }
142     }
143     /**
144      * Adds the backup_setting as a element to the form
145      * @param backup_setting $setting
146      * @return bool
147      */
148     function add_setting(backup_setting $setting, base_task $task=null) {
149         return $this->add_settings(array(array($setting, $task)));
150     }
151     /**
152      * Adds multiple backup_settings as elements to the form
153      * @param array $settingstasks Consists of array($setting, $task) elements
154      * @return bool
155      */
156     public function add_settings(array $settingstasks) {
157         global $OUTPUT;
159         $defaults = array();
160         foreach ($settingstasks as $st) {
161             list($setting, $task) = $st;
162             // If the setting cant be changed or isn't visible then add it as a fixed setting.
163             if (!$setting->get_ui()->is_changeable() || $setting->get_visibility() != backup_setting::VISIBLE) {
164                 $this->add_fixed_setting($setting, $task);
165                 continue;
166             }
168             // First add the formatting for this setting
169             $this->add_html_formatting($setting);
171             // Then call the add method with the get_element_properties array
172             call_user_func_array(array($this->_form, 'addElement'), $setting->get_ui()->get_element_properties($task, $OUTPUT));
173             $this->_form->setType($setting->get_ui_name(), $setting->get_param_validation());
174             $defaults[$setting->get_ui_name()] = $setting->get_value();
175             if ($setting->has_help()) {
176                 list($identifier, $component) = $setting->get_help();
177                 $this->_form->addHelpButton($setting->get_ui_name(), $identifier, $component);
178             }
179             $this->_form->addElement('html', html_writer::end_tag('div'));
180         }
181         $this->_form->setDefaults($defaults);
182         return true;
183     }
184     /**
185      * Adds a heading to the form
186      * @param string $name
187      * @param string $text
188      */
189     function add_heading($name , $text) {
190         $this->_form->addElement('header', $name, $text);
191     }
192     /**
193      * Adds HTML formatting for the given backup setting, needed to group/segment
194      * correctly.
195      * @param backup_setting $setting
196      */
197     protected function add_html_formatting(backup_setting $setting) {
198         $mform = $this->_form;
199         $isincludesetting = (strpos($setting->get_name(), '_include')!==false);
200         if ($isincludesetting && $setting->get_level() != backup_setting::ROOT_LEVEL)  {
201             switch ($setting->get_level()) {
202                 case backup_setting::COURSE_LEVEL:
203                     if ($this->activitydiv) {
204                         $this->_form->addElement('html', html_writer::end_tag('div'));
205                         $this->activitydiv = false;
206                     }
207                     if ($this->sectiondiv) {
208                         $this->_form->addElement('html', html_writer::end_tag('div'));
209                         $this->sectiondiv = false;
210                     }
211                     if ($this->coursediv) {
212                         $this->_form->addElement('html', html_writer::end_tag('div'));
213                     }
214                     $mform->addElement('html', html_writer::start_tag('div', array('class'=>'grouped_settings course_level')));
215                     $mform->addElement('html', html_writer::start_tag('div', array('class'=>'include_setting course_level')));
216                     $this->coursediv = true;
217                     break;
218                 case backup_setting::SECTION_LEVEL:
219                     if ($this->activitydiv) {
220                         $this->_form->addElement('html', html_writer::end_tag('div'));
221                         $this->activitydiv = false;
222                     }
223                     if ($this->sectiondiv) {
224                         $this->_form->addElement('html', html_writer::end_tag('div'));
225                     }
226                     $mform->addElement('html', html_writer::start_tag('div', array('class'=>'grouped_settings section_level')));
227                     $mform->addElement('html', html_writer::start_tag('div', array('class'=>'include_setting section_level')));
228                     $this->sectiondiv = true;
229                     break;
230                 case backup_setting::ACTIVITY_LEVEL:
231                     if ($this->activitydiv) {
232                         $this->_form->addElement('html', html_writer::end_tag('div'));
233                     }
234                     $mform->addElement('html', html_writer::start_tag('div', array('class'=>'grouped_settings activity_level')));
235                     $mform->addElement('html', html_writer::start_tag('div', array('class'=>'include_setting activity_level')));
236                     $this->activitydiv = true;
237                     break;
238                 default:
239                     $mform->addElement('html', html_writer::start_tag('div', array('class'=>'normal_setting')));
240                     break;
241             }
242         } else if ($setting->get_level() == backup_setting::ROOT_LEVEL) {
243             $mform->addElement('html', html_writer::start_tag('div', array('class'=>'root_setting')));
244         } else {
245             $mform->addElement('html', html_writer::start_tag('div', array('class'=>'normal_setting')));
246         }
247     }
248     /**
249      * Adds a fixed or static setting to the form
250      * @param backup_setting $setting
251      */
252     function add_fixed_setting(backup_setting $setting, base_task $task) {
253         global $OUTPUT;
254         $settingui = $setting->get_ui();
255         if ($setting->get_visibility() == backup_setting::VISIBLE) {
256             $this->add_html_formatting($setting);
257             switch ($setting->get_status()) {
258                 case backup_setting::LOCKED_BY_PERMISSION:
259                     $icon = ' '.$OUTPUT->pix_icon('i/permissionlock', get_string('lockedbypermission', 'backup'), 'moodle', array('class'=>'smallicon lockedicon permissionlock'));
260                     break;
261                 case backup_setting::LOCKED_BY_CONFIG:
262                     $icon = ' '.$OUTPUT->pix_icon('i/configlock', get_string('lockedbyconfig', 'backup'), 'moodle', array('class'=>'smallicon lockedicon configlock'));
263                     break;
264                 case backup_setting::LOCKED_BY_HIERARCHY:
265                     $icon = ' '.$OUTPUT->pix_icon('i/hierarchylock', get_string('lockedbyhierarchy', 'backup'), 'moodle', array('class'=>'smallicon lockedicon configlock'));
266                     break;
267                 default:
268                     $icon = '';
269                     break;
270             }
271             $label = $settingui->get_label($task);
272             $labelicon = $settingui->get_icon();
273             if (!empty($labelicon)) {
274                 $label .= '&nbsp;'.$OUTPUT->render($labelicon);
275             }
276             $this->_form->addElement('static', 'static_'.$settingui->get_name(), $label, $settingui->get_static_value().$icon);
277             $this->_form->addElement('html', html_writer::end_tag('div'));
278         }
279         $this->_form->addElement('hidden', $settingui->get_name(), $settingui->get_value());
280         $this->_form->setType($settingui->get_name(), $settingui->get_param_validation());
281     }
282     /**
283      * Adds dependencies to the form recursively
284      *
285      * @param backup_setting $setting
286      */
287     function add_dependencies(backup_setting $setting) {
288         $mform = $this->_form;
289         // Apply all dependencies for backup
290         foreach ($setting->get_my_dependency_properties() as $key=>$dependency) {
291             call_user_func_array(array($this->_form, 'disabledIf'), $dependency);
292         }
293     }
294     /**
295      * Returns true if the form was cancelled, false otherwise
296      * @return bool
297      */
298     public function is_cancelled() {
299         return (optional_param('cancel', false, PARAM_BOOL) || parent::is_cancelled());
300     }
302     /**
303      * Removes an element from the form if it exists
304      * @param string $elementname
305      * @return bool
306      */
307     public function remove_element($elementname) {
308         if ($this->_form->elementExists($elementname)) {
309             return $this->_form->removeElement($elementname);
310         } else {
311             return false;
312         }
313     }
315     /**
316      * Gets an element from the form if it exists
317      *
318      * @param string $elementname
319      * @return HTML_QuickForm_input|MoodleQuickForm_group
320      */
321     public function get_element($elementname) {
322         if ($this->_form->elementExists($elementname)) {
323             return $this->_form->getElement($elementname);
324         } else {
325             return false;
326         }
327     }
329     /**
330      * Displays the form
331      */
332     public function display() {
333         global $PAGE, $COURSE;
335         $this->require_definition_after_data();
337         $config = new stdClass;
338         $config->title = get_string('confirmcancel', 'backup');
339         $config->question = get_string('confirmcancelquestion', 'backup');
340         $config->yesLabel = get_string('confirmcancelyes', 'backup');
341         $config->noLabel = get_string('confirmcancelno', 'backup');
342         $config->closeButtonTitle = get_string('close', 'editor');
343         $PAGE->requires->yui_module('moodle-backup-confirmcancel', 'M.core_backup.watch_cancel_buttons', array($config));
345         // Get list of module types on course.
346         $modinfo = get_fast_modinfo($COURSE);
347         $modnames = $modinfo->get_used_module_names(true);
348         $PAGE->requires->yui_module('moodle-backup-backupselectall', 'M.core_backup.select_all_init',
349                 array($modnames));
350         $PAGE->requires->strings_for_js(array('select', 'all', 'none'), 'moodle');
351         $PAGE->requires->strings_for_js(array('showtypes', 'hidetypes'), 'backup');
353         parent::display();
354     }
356     /**
357      * Ensures the the definition after data is loaded
358      */
359     public function require_definition_after_data() {
360         if (!$this->_definition_finalized) {
361             $this->definition_after_data();
362         }
363     }