Merge branch 'MDL-41595_M25' of git://github.com/lazydaisy/moodle into MOODLE_25_STABLE
[moodle.git] / group / grouping_form.php
blob1f15bc78b5fc30fa73c860d93fc9ac649e70f8d2
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/>.
18 /**
19 * A form for creating and editing groupings.
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
26 if (!defined('MOODLE_INTERNAL')) {
27 die('Direct access to this script is forbidden.'); /// It must be included from a Moodle page
30 require_once($CFG->dirroot.'/lib/formslib.php');
32 /**
33 * Grouping form class
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
39 class grouping_form extends moodleform {
41 /**
42 * Form definition
44 function definition () {
45 global $USER, $CFG, $COURSE;
46 $coursecontext = context_course::instance($COURSE->id);
48 $mform =& $this->_form;
49 $editoroptions = $this->_customdata['editoroptions'];
51 $mform->addElement('header', 'general', get_string('general', 'form'));
53 $mform->addElement('text','name', get_string('groupingname', 'group'),'maxlength="254" size="50"');
54 $mform->addRule('name', get_string('required'), 'required', null, 'server');
55 $mform->setType('name', PARAM_TEXT);
57 $mform->addElement('text','idnumber', get_string('idnumbergrouping'), 'maxlength="100" size="10"');
58 $mform->addHelpButton('idnumber', 'idnumbergrouping');
59 $mform->setType('idnumber', PARAM_RAW);
60 $mform->setAdvanced('idnumber');
61 if (!has_capability('moodle/course:changeidnumber', $coursecontext)) {
62 $mform->hardFreeze('idnumber');
65 $mform->addElement('editor', 'description_editor', get_string('groupingdescription', 'group'), null, $editoroptions);
66 $mform->setType('description_editor', PARAM_RAW);
68 $mform->addElement('hidden','id');
69 $mform->setType('id', PARAM_INT);
71 $mform->addElement('hidden', 'courseid');
72 $mform->setType('courseid', PARAM_INT);
74 $this->add_action_buttons();
77 /**
78 * Form validation
80 * @param array $data
81 * @param array $files
82 * @return array $errors An array of validataion errors for the form.
84 function validation($data, $files) {
85 global $COURSE, $DB;
87 $errors = parent::validation($data, $files);
89 $name = trim($data['name']);
90 if (isset($data['idnumber'])) {
91 $idnumber = trim($data['idnumber']);
92 } else {
93 $idnumber = '';
95 if ($data['id'] and $grouping = $DB->get_record('groupings', array('id'=>$data['id']))) {
96 if (textlib::strtolower($grouping->name) != textlib::strtolower($name)) {
97 if (groups_get_grouping_by_name($COURSE->id, $name)) {
98 $errors['name'] = get_string('groupingnameexists', 'group', $name);
101 if (!empty($idnumber) && $grouping->idnumber != $idnumber) {
102 if (groups_get_grouping_by_idnumber($COURSE->id, $idnumber)) {
103 $errors['idnumber']= get_string('idnumbertaken');
107 } else if (groups_get_grouping_by_name($COURSE->id, $name)) {
108 $errors['name'] = get_string('groupingnameexists', 'group', $name);
109 } else if (!empty($idnumber) && groups_get_grouping_by_idnumber($COURSE->id, $idnumber)) {
110 $errors['idnumber']= get_string('idnumbertaken');
113 return $errors;