Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.3.x will end 7 October 2024 (12 months).
  • Bug fixes for security issues in 4.3.x will end 21 April 2025 (18 months).
  • PHP version: minimum PHP 8.0.0 Note: minimum PHP version has increased since Moodle 4.1. PHP 8.2.x is supported too.

Differences Between: [Versions 310 and 403] [Versions 311 and 403] [Versions 39 and 403] [Versions 400 and 403] [Versions 401 and 403] [Versions 402 and 403]

   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  /**
  19   * A form for creating and editing groupings.
  20   *
  21   * @copyright 2006 The Open University, N.D.Freear AT open.ac.uk, J.White AT open.ac.uk
  22   * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23   * @package   core_group
  24   */
  25  
  26  if (!defined('MOODLE_INTERNAL')) {
  27      die('Direct access to this script is forbidden.');    ///  It must be included from a Moodle page
  28  }
  29  
  30  require_once($CFG->dirroot.'/lib/formslib.php');
  31  
  32  /**
  33   * Grouping form class
  34   *
  35   * @copyright 2006 The Open University, N.D.Freear AT open.ac.uk, J.White AT open.ac.uk
  36   * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  37   * @package   core_group
  38   */
  39  class grouping_form extends moodleform {
  40  
  41      /**
  42       * Form definition
  43       */
  44      function definition () {
  45          global $USER, $CFG, $COURSE;
  46          $coursecontext = context_course::instance($COURSE->id);
  47  
  48          $mform =& $this->_form;
  49          $editoroptions = $this->_customdata['editoroptions'];
  50          $grouping = $this->_customdata['grouping'];
  51  
  52          $mform->addElement('header', 'general', get_string('general', 'form'));
  53  
  54          $mform->addElement('text','name', get_string('groupingname', 'group'),'maxlength="254" size="50"');
  55          $mform->addRule('name', get_string('required'), 'required', null, 'server');
  56          $mform->setType('name', PARAM_TEXT);
  57  
  58          $mform->addElement('text','idnumber', get_string('idnumbergrouping'), 'maxlength="100" size="10"');
  59          $mform->addHelpButton('idnumber', 'idnumbergrouping');
  60          $mform->setType('idnumber', PARAM_RAW);
  61          if (!has_capability('moodle/course:changeidnumber', $coursecontext)) {
  62              $mform->hardFreeze('idnumber');
  63          }
  64  
  65          $mform->addElement('editor', 'description_editor', get_string('groupingdescription', 'group'), null, $editoroptions);
  66          $mform->setType('description_editor', PARAM_RAW);
  67  
  68          $handler = \core_group\customfield\grouping_handler::create();
  69          $handler->instance_form_definition($mform, empty($grouping->id) ? 0 : $grouping->id);
  70          $handler->instance_form_before_set_data($grouping);
  71  
  72          $mform->addElement('hidden','id');
  73          $mform->setType('id', PARAM_INT);
  74  
  75          $mform->addElement('hidden', 'courseid');
  76          $mform->setType('courseid', PARAM_INT);
  77  
  78          $this->add_action_buttons();
  79      }
  80  
  81      /**
  82       * Form validation
  83       *
  84       * @param array $data
  85       * @param array $files
  86       * @return array $errors An array of validataion errors for the form.
  87       */
  88      function validation($data, $files) {
  89          global $COURSE, $DB;
  90  
  91          $errors = parent::validation($data, $files);
  92  
  93          $name = trim($data['name']);
  94          if (isset($data['idnumber'])) {
  95              $idnumber = trim($data['idnumber']);
  96          } else {
  97              $idnumber = '';
  98          }
  99          if ($data['id'] and $grouping = $DB->get_record('groupings', array('id'=>$data['id']))) {
 100              if (core_text::strtolower($grouping->name) != core_text::strtolower($name)) {
 101                  if (groups_get_grouping_by_name($COURSE->id,  $name)) {
 102                      $errors['name'] = get_string('groupingnameexists', 'group', $name);
 103                  }
 104              }
 105              if (!empty($idnumber) && $grouping->idnumber != $idnumber) {
 106                  if (groups_get_grouping_by_idnumber($COURSE->id, $idnumber)) {
 107                      $errors['idnumber']= get_string('idnumbertaken');
 108                  }
 109              }
 110  
 111          } else if (groups_get_grouping_by_name($COURSE->id, $name)) {
 112              $errors['name'] = get_string('groupingnameexists', 'group', $name);
 113          } else if (!empty($idnumber) && groups_get_grouping_by_idnumber($COURSE->id, $idnumber)) {
 114              $errors['idnumber']= get_string('idnumbertaken');
 115          }
 116  
 117          $handler = \core_group\customfield\grouping_handler::create();
 118          $errors = array_merge($errors, $handler->instance_form_validation($data, $files));
 119  
 120          return $errors;
 121      }
 122  
 123      /**
 124       *  Apply a logic after data is set.
 125       */
 126      public function definition_after_data() {
 127          $groupid = $this->_form->getElementValue('id');
 128          $handler = \core_group\customfield\grouping_handler::create();
 129          $handler->instance_form_definition_after_data($this->_form, empty($groupid) ? 0 : $groupid);
 130      }
 131  }