Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 3.9.x will end* 10 May 2021 (12 months).
  • Bug fixes for security issues in 3.9.x will end* 8 May 2023 (36 months).
  • PHP version: minimum PHP 7.2.0 Note: minimum PHP version has increased since Moodle 3.8. PHP 7.3.x and 7.4.x are supported too.
   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.
  13  //
  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/>.
  16  
  17  /**
  18   * Role add/reset selection form.
  19   *
  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
  23   */
  24  
  25  defined('MOODLE_INTERNAL') || die();
  26  
  27  require_once("$CFG->libdir/formslib.php");
  28  
  29  
  30  /**
  31   * Role add/reset selection form.
  32   *
  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
  36   */
  37  class core_role_preset_form extends moodleform {
  38  
  39      /**
  40       * Definition of this form.
  41       */
  42      protected function definition() {
  43          $mform = $this->_form;
  44  
  45          $data = $this->_customdata;
  46          $options = array();
  47  
  48          $group = get_string('other');
  49          $options[$group] = array();
  50          $options[$group][0] = get_string('norole', 'core_role');
  51  
  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;
  57          }
  58  
  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');
  63          }
  64  
  65          $mform->addElement('header', 'presetheader', get_string('roleresetdefaults', 'core_role'));
  66  
  67          $mform->addElement('selectgroups', 'resettype', get_string('roleresetrole', 'core_role'), $options);
  68  
  69          $mform->addElement('filepicker', 'rolepreset', get_string('rolerepreset', 'core_role'));
  70  
  71          if ($data['roleid']) {
  72              $mform->addElement('header', 'resetheader', get_string('resetrole', 'core_role'));
  73  
  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'));
  84          }
  85  
  86          $mform->addElement('hidden', 'roleid');
  87          $mform->setType('roleid', PARAM_INT);
  88  
  89          $mform->addElement('hidden', 'action');
  90          $mform->setType('action', PARAM_ALPHA);
  91  
  92          $mform->addElement('hidden', 'return');
  93          $mform->setType('return', PARAM_ALPHA);
  94  
  95          $this->add_action_buttons(true, get_string('continue', 'core'));
  96  
  97          $this->set_data($data);
  98      }
  99  
 100      /**
 101       * Validate this form.
 102       *
 103       * @param array $data submitted data
 104       * @param array $files not used
 105       * @return array errors
 106       */
 107      public function validation($data, $files) {
 108          $errors = parent::validation($data, $files);
 109  
 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');
 116              }
 117          }
 118  
 119          return $errors;
 120      }
 121  }