MDL-78962 core/loadingicon: remove jQuery requirement in the API
[moodle.git] / admin / roles / classes / preset_form.php
blob426c3d8d38fc9fdadfd2192604aec9a9b47020d0
1 <?php
2 // This file is part of Moodle - http://moodle.org/
3 //
4 // Moodle is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
8 //
9 // Moodle is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
14 // You should have received a copy of the GNU General Public License
15 // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
17 /**
18 * Role add/reset selection form.
20 * @package core_role
21 * @copyright 2013 Petr Skoda {@link http://skodak.org}
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
25 defined('MOODLE_INTERNAL') || die();
27 require_once("$CFG->libdir/formslib.php");
30 /**
31 * Role add/reset selection form.
33 * @package core_role
34 * @copyright 2013 Petr Skoda {@link http://skodak.org}
35 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
37 class core_role_preset_form extends moodleform {
39 /**
40 * Definition of this form.
42 protected function definition() {
43 $mform = $this->_form;
45 $data = $this->_customdata;
46 $options = array();
48 $group = get_string('other');
49 $options[$group] = array();
50 $options[$group][0] = get_string('norole', 'core_role');
52 $group = get_string('role', 'core');
53 $options[$group] = array();
54 foreach (role_get_names(null, ROLENAME_BOTH) as $role) {
55 // Allow reset to self too, it may be useful when importing incomplete XML preset.
56 $options[$group][$role->id] = $role->localname;
59 $group = get_string('archetype', 'core_role');
60 $options[$group] = array();
61 foreach (get_role_archetypes() as $type) {
62 $options[$group][$type] = get_string('archetype'.$type, 'core_role');
65 $mform->addElement('header', 'presetheader', get_string('roleresetdefaults', 'core_role'));
67 $mform->addElement('selectgroups', 'resettype', get_string('roleresetrole', 'core_role'), $options);
69 $mform->addElement('filepicker', 'rolepreset', get_string('rolerepreset', 'core_role'));
71 if ($data['roleid']) {
72 $mform->addElement('header', 'resetheader', get_string('resetrole', 'core_role'));
74 $mform->addElement('advcheckbox', 'shortname', get_string('roleshortname', 'core_role'));
75 $mform->addElement('advcheckbox', 'name', get_string('customrolename', 'core_role'));
76 $mform->addElement('advcheckbox', 'description', get_string('customroledescription', 'core_role'));
77 $mform->addElement('advcheckbox', 'archetype', get_string('archetype', 'core_role'));
78 $mform->addElement('advcheckbox', 'contextlevels', get_string('maybeassignedin', 'core_role'));
79 $mform->addElement('advcheckbox', 'allowassign', get_string('allowassign', 'core_role'));
80 $mform->addElement('advcheckbox', 'allowoverride', get_string('allowoverride', 'core_role'));
81 $mform->addElement('advcheckbox', 'allowswitch', get_string('allowswitch', 'core_role'));
82 $mform->addElement('advcheckbox', 'allowview', get_string('allowview', 'core_role'));
83 $mform->addElement('advcheckbox', 'permissions', get_string('permissions', 'core_role'));
86 $mform->addElement('hidden', 'roleid');
87 $mform->setType('roleid', PARAM_INT);
89 $mform->addElement('hidden', 'action');
90 $mform->setType('action', PARAM_ALPHA);
92 $mform->addElement('hidden', 'return');
93 $mform->setType('return', PARAM_ALPHA);
95 $this->add_action_buttons(true, get_string('continue', 'core'));
97 $this->set_data($data);
101 * Validate this form.
103 * @param array $data submitted data
104 * @param array $files not used
105 * @return array errors
107 public function validation($data, $files) {
108 $errors = parent::validation($data, $files);
110 if ($files = $this->get_draft_files('rolepreset')) {
111 /** @var stored_file $file */
112 $file = reset($files);
113 $xml = $file->get_content();
114 if (!core_role_preset::is_valid_preset($xml)) {
115 $errors['rolepreset'] = get_string('invalidpresetfile', 'core_role');
119 return $errors;