Search moodle.org's
Developer Documentation


  • Bug fixes for general core bugs in 2.8.x ended 9 November 2015 (12 months).
  • Bug fixes for security issues in 2.8.x ended 9 May 2016 (18 months).
  • minimum PHP 5.4.4 (always use latest PHP 5.4.x or 5.5.x on Windows - http://windows.php.net/download/), PHP 7 is NOT supported
  • Differences Between: [Versions 28 and 35] [Versions 28 and 36] [Versions 28 and 37]

       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', 'permissions', get_string('permissions', 'core_role'));
      83          }
      84  
      85          $mform->addElement('hidden', 'roleid');
      86          $mform->setType('roleid', PARAM_INT);
      87  
      88          $mform->addElement('hidden', 'action');
      89          $mform->setType('action', PARAM_ALPHA);
      90  
      91          $mform->addElement('hidden', 'return');
      92          $mform->setType('return', PARAM_ALPHA);
      93  
      94          $this->add_action_buttons(true, get_string('continue', 'core'));
      95  
      96          $this->set_data($data);
      97      }
      98  
      99      /**
     100       * Validate this form.
     101       *
     102       * @param array $data submitted data
     103       * @param array $files not used
     104       * @return array errors
     105       */
     106      public function validation($data, $files) {
     107          $errors = parent::validation($data, $files);
     108  
     109          if ($files = $this->get_draft_files('rolepreset')) {
     110              /** @var stored_file $file */
     111              $file = reset($files);
     112              $xml = $file->get_content();
     113              if (!core_role_preset::is_valid_preset($xml)) {
     114                  $errors['rolepreset'] = get_string('invalidpresetfile', 'core_role');
     115              }
     116          }
     117  
     118          return $errors;
     119      }
     120  }
    

    Search This Site: