weekly release 3.10.1+
[moodle.git] / group / grouping_form.php
CommitLineData
4ca6cfbf 1<?php
4d8e2417
AG
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
8bdc9cac 18/**
4d8e2417 19 * A form for creating and editing groupings.
8bdc9cac 20 *
4d8e2417
AG
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
8bdc9cac 24 */
ddff2fa8 25
bfebaf64
MD
26if (!defined('MOODLE_INTERNAL')) {
27 die('Direct access to this script is forbidden.'); /// It must be included from a Moodle page
28}
29
ddff2fa8 30require_once($CFG->dirroot.'/lib/formslib.php');
31
4d8e2417
AG
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 */
ddff2fa8 39class grouping_form extends moodleform {
40
4d8e2417
AG
41 /**
42 * Form definition
43 */
ddff2fa8 44 function definition () {
45 global $USER, $CFG, $COURSE;
74b714df 46 $coursecontext = context_course::instance($COURSE->id);
ddff2fa8 47
48 $mform =& $this->_form;
8bdc9cac 49 $editoroptions = $this->_customdata['editoroptions'];
ddff2fa8 50
74b714df
ARN
51 $mform->addElement('header', 'general', get_string('general', 'form'));
52
ddff2fa8 53 $mform->addElement('text','name', get_string('groupingname', 'group'),'maxlength="254" size="50"');
54 $mform->addRule('name', get_string('required'), 'required', null, 'server');
071e68f9 55 $mform->setType('name', PARAM_TEXT);
ddff2fa8 56
74b714df
ARN
57 $mform->addElement('text','idnumber', get_string('idnumbergrouping'), 'maxlength="100" size="10"');
58 $mform->addHelpButton('idnumber', 'idnumbergrouping');
59 $mform->setType('idnumber', PARAM_RAW);
74b714df
ARN
60 if (!has_capability('moodle/course:changeidnumber', $coursecontext)) {
61 $mform->hardFreeze('idnumber');
62 }
63
8bdc9cac
SH
64 $mform->addElement('editor', 'description_editor', get_string('groupingdescription', 'group'), null, $editoroptions);
65 $mform->setType('description_editor', PARAM_RAW);
ddff2fa8 66
67 $mform->addElement('hidden','id');
68 $mform->setType('id', PARAM_INT);
69
70 $mform->addElement('hidden', 'courseid');
71 $mform->setType('courseid', PARAM_INT);
72
73 $this->add_action_buttons();
74 }
75
4d8e2417
AG
76 /**
77 * Form validation
78 *
79 * @param array $data
80 * @param array $files
81 * @return array $errors An array of validataion errors for the form.
82 */
a78890d5 83 function validation($data, $files) {
dfdaabd6 84 global $COURSE, $DB;
ddff2fa8 85
a78890d5 86 $errors = parent::validation($data, $files);
ddff2fa8 87
32648682 88 $name = trim($data['name']);
74b714df
ARN
89 if (isset($data['idnumber'])) {
90 $idnumber = trim($data['idnumber']);
91 } else {
92 $idnumber = '';
93 }
dfdaabd6 94 if ($data['id'] and $grouping = $DB->get_record('groupings', array('id'=>$data['id']))) {
2f1e464a 95 if (core_text::strtolower($grouping->name) != core_text::strtolower($name)) {
ddff2fa8 96 if (groups_get_grouping_by_name($COURSE->id, $name)) {
97 $errors['name'] = get_string('groupingnameexists', 'group', $name);
98 }
99 }
74b714df
ARN
100 if (!empty($idnumber) && $grouping->idnumber != $idnumber) {
101 if (groups_get_grouping_by_idnumber($COURSE->id, $idnumber)) {
102 $errors['idnumber']= get_string('idnumbertaken');
103 }
104 }
ddff2fa8 105
106 } else if (groups_get_grouping_by_name($COURSE->id, $name)) {
107 $errors['name'] = get_string('groupingnameexists', 'group', $name);
74b714df
ARN
108 } else if (!empty($idnumber) && groups_get_grouping_by_idnumber($COURSE->id, $idnumber)) {
109 $errors['idnumber']= get_string('idnumbertaken');
ddff2fa8 110 }
111
a78890d5 112 return $errors;
ddff2fa8 113 }
114
115}