2 // This file is part of Moodle - http://moodle.org/
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.
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/>.
19 * A form for the creation and editing of groups.
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
26 defined('MOODLE_INTERNAL') ||
die;
28 require_once($CFG->dirroot
.'/lib/formslib.php');
33 * @copyright 2006 The Open University, N.D.Freear AT open.ac.uk, J.White AT open.ac.uk
34 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
37 class group_form
extends moodleform
{
40 * Definition of the form
42 function definition () {
43 global $USER, $CFG, $COURSE;
44 $coursecontext = context_course
::instance($COURSE->id
);
46 $mform =& $this->_form
;
47 $editoroptions = $this->_customdata
['editoroptions'];
49 $mform->addElement('header', 'general', get_string('general', 'form'));
51 $mform->addElement('text','name', get_string('groupname', 'group'),'maxlength="254" size="50"');
52 $mform->addRule('name', get_string('required'), 'required', null, 'client');
53 $mform->setType('name', PARAM_MULTILANG
);
55 $mform->addElement('text','idnumber', get_string('idnumbergroup'), 'maxlength="100" size="10"');
56 $mform->addHelpButton('idnumber', 'idnumbergroup');
57 $mform->setType('idnumber', PARAM_RAW
);
58 $mform->setAdvanced('idnumber');
59 if (!has_capability('moodle/course:changeidnumber', $coursecontext)) {
60 $mform->hardFreeze('idnumber');
63 $mform->addElement('editor', 'description_editor', get_string('groupdescription', 'group'), null, $editoroptions);
64 $mform->setType('description_editor', PARAM_RAW
);
66 $mform->addElement('passwordunmask', 'enrolmentkey', get_string('enrolmentkey', 'group'), 'maxlength="254" size="24"', get_string('enrolmentkey', 'group'));
67 $mform->addHelpButton('enrolmentkey', 'enrolmentkey', 'group');
68 $mform->setType('enrolmentkey', PARAM_RAW
);
70 if (!empty($CFG->gdversion
)) {
71 $options = array(get_string('no'), get_string('yes'));
72 $mform->addElement('select', 'hidepicture', get_string('hidepicture'), $options);
74 $mform->addElement('filepicker', 'imagefile', get_string('newpicture', 'group'));
75 $mform->addHelpButton('imagefile', 'newpicture', 'group');
78 $mform->addElement('hidden','id');
79 $mform->setType('id', PARAM_INT
);
81 $mform->addElement('hidden','courseid');
82 $mform->setType('courseid', PARAM_INT
);
84 $this->add_action_buttons();
92 * @return array $errors An array of errors
94 function validation($data, $files) {
95 global $COURSE, $DB, $CFG;
97 $errors = parent
::validation($data, $files);
99 $name = trim($data['name']);
100 if (isset($data['idnumber'])) {
101 $idnumber = trim($data['idnumber']);
105 if ($data['id'] and $group = $DB->get_record('groups', array('id'=>$data['id']))) {
106 if (textlib
::strtolower($group->name
) != textlib
::strtolower($name)) {
107 if (groups_get_group_by_name($COURSE->id
, $name)) {
108 $errors['name'] = get_string('groupnameexists', 'group', $name);
111 if (!empty($idnumber) && $group->idnumber
!= $idnumber) {
112 if (groups_get_group_by_idnumber($COURSE->id
, $idnumber)) {
113 $errors['idnumber']= get_string('idnumbertaken');
117 if (!empty($CFG->groupenrolmentkeypolicy
) and $data['enrolmentkey'] != '' and $group->enrolmentkey
!== $data['enrolmentkey']) {
118 // enforce password policy only if changing password
120 if (!check_password_policy($data['enrolmentkey'], $errmsg)) {
121 $errors['enrolmentkey'] = $errmsg;
125 } else if (groups_get_group_by_name($COURSE->id
, $name)) {
126 $errors['name'] = get_string('groupnameexists', 'group', $name);
127 } else if (!empty($idnumber) && groups_get_group_by_idnumber($COURSE->id
, $idnumber)) {
128 $errors['idnumber']= get_string('idnumbertaken');
135 * Get editor options for this form
137 * @return array An array of options
139 function get_editor_options() {
140 return $this->_customdata
['editoroptions'];