weekly release 3.10.1+
[moodle.git] / group / group_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
77e90955 17
8bdc9cac 18/**
4d8e2417 19 * A form for the creation and editing of groups.
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
SH
24 */
25
e88dd876 26defined('MOODLE_INTERNAL') || die;
bfebaf64 27
77e90955 28require_once($CFG->dirroot.'/lib/formslib.php');
29
4d8e2417
AG
30/**
31 * Group form class
32 *
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
35 * @package core_group
36 */
ddff2fa8 37class group_form extends moodleform {
77e90955 38
4d8e2417
AG
39 /**
40 * Definition of the form
41 */
77e90955 42 function definition () {
43 global $USER, $CFG, $COURSE;
74b714df 44 $coursecontext = context_course::instance($COURSE->id);
77e90955 45
77e90955 46 $mform =& $this->_form;
8bdc9cac 47 $editoroptions = $this->_customdata['editoroptions'];
2524b0f2 48
74b714df
ARN
49 $mform->addElement('header', 'general', get_string('general', 'form'));
50
80efbf98 51 $mform->addElement('text','name', get_string('groupname', 'group'),'maxlength="254" size="50"');
ddff2fa8 52 $mform->addRule('name', get_string('required'), 'required', null, 'client');
071e68f9 53 $mform->setType('name', PARAM_TEXT);
77e90955 54
74b714df
ARN
55 $mform->addElement('text','idnumber', get_string('idnumbergroup'), 'maxlength="100" size="10"');
56 $mform->addHelpButton('idnumber', 'idnumbergroup');
57 $mform->setType('idnumber', PARAM_RAW);
74b714df
ARN
58 if (!has_capability('moodle/course:changeidnumber', $coursecontext)) {
59 $mform->hardFreeze('idnumber');
60 }
61
8bdc9cac
SH
62 $mform->addElement('editor', 'description_editor', get_string('groupdescription', 'group'), null, $editoroptions);
63 $mform->setType('description_editor', PARAM_RAW);
2524b0f2 64
df997f84 65 $mform->addElement('passwordunmask', 'enrolmentkey', get_string('enrolmentkey', 'group'), 'maxlength="254" size="24"', get_string('enrolmentkey', 'group'));
9f38cdce 66 $mform->addHelpButton('enrolmentkey', 'enrolmentkey', 'group');
77e90955 67 $mform->setType('enrolmentkey', PARAM_RAW);
2524b0f2 68
e7f46714 69 // Group conversation messaging.
70 if (\core_message\api::can_create_group_conversation($USER->id, $coursecontext)) {
71 $mform->addElement('selectyesno', 'enablemessaging', get_string('enablemessaging', 'group'));
72 $mform->addHelpButton('enablemessaging', 'enablemessaging', 'group');
73 }
74
d996c620
FM
75 $mform->addElement('static', 'currentpicture', get_string('currentpicture'));
76
77 $mform->addElement('checkbox', 'deletepicture', get_string('delete'));
78 $mform->setDefault('deletepicture', 0);
79
689096bc
PS
80 $options = array(get_string('no'), get_string('yes'));
81 $mform->addElement('select', 'hidepicture', get_string('hidepicture'), $options);
2524b0f2 82
689096bc
PS
83 $mform->addElement('filepicker', 'imagefile', get_string('newpicture', 'group'));
84 $mform->addHelpButton('imagefile', 'newpicture', 'group');
77e90955 85
2524b0f2 86 $mform->addElement('hidden','id');
87 $mform->setType('id', PARAM_INT);
08103c93 88
2524b0f2 89 $mform->addElement('hidden','courseid');
90 $mform->setType('courseid', PARAM_INT);
77e90955 91
ddff2fa8 92 $this->add_action_buttons();
77e90955 93 }
94
d996c620
FM
95 /**
96 * Extend the form definition after the data has been parsed.
97 */
98 public function definition_after_data() {
f0e137c5 99 global $COURSE, $DB, $USER;
d996c620
FM
100
101 $mform = $this->_form;
102 $groupid = $mform->getElementValue('id');
e7f46714 103 $coursecontext = context_course::instance($COURSE->id);
d996c620
FM
104
105 if ($group = $DB->get_record('groups', array('id' => $groupid))) {
e7f46714 106 // If can create group conversation then get if a conversation area exists and it is enabled.
f0e137c5
MN
107 if (\core_message\api::can_create_group_conversation($USER->id, $coursecontext)) {
108 if (\core_message\api::is_conversation_area_enabled('core_group', 'groups', $groupid, $coursecontext->id)) {
e7f46714 109 $mform->getElement('enablemessaging')->setSelected(1);
110 }
111 }
d996c620
FM
112 // Print picture.
113 if (!($pic = print_group_picture($group, $COURSE->id, true, true, false))) {
114 $pic = get_string('none');
115 if ($mform->elementExists('deletepicture')) {
116 $mform->removeElement('deletepicture');
117 }
118 }
119 $imageelement = $mform->getElement('currentpicture');
120 $imageelement->setValue($pic);
121 } else {
122 if ($mform->elementExists('currentpicture')) {
123 $mform->removeElement('currentpicture');
124 }
125 if ($mform->elementExists('deletepicture')) {
126 $mform->removeElement('deletepicture');
127 }
128 }
129
130 }
131
4d8e2417
AG
132 /**
133 * Form validation
134 *
135 * @param array $data
136 * @param array $files
137 * @return array $errors An array of errors
138 */
a78890d5 139 function validation($data, $files) {
82672532 140 global $COURSE, $DB, $CFG;
2524b0f2 141
a78890d5 142 $errors = parent::validation($data, $files);
2524b0f2 143
32648682 144 $name = trim($data['name']);
74b714df
ARN
145 if (isset($data['idnumber'])) {
146 $idnumber = trim($data['idnumber']);
147 } else {
148 $idnumber = '';
149 }
dfdaabd6 150 if ($data['id'] and $group = $DB->get_record('groups', array('id'=>$data['id']))) {
2f1e464a 151 if (core_text::strtolower($group->name) != core_text::strtolower($name)) {
c4e953e6 152 if (groups_get_group_by_name($COURSE->id, $name)) {
ddff2fa8 153 $errors['name'] = get_string('groupnameexists', 'group', $name);
2524b0f2 154 }
155 }
74b714df
ARN
156 if (!empty($idnumber) && $group->idnumber != $idnumber) {
157 if (groups_get_group_by_idnumber($COURSE->id, $idnumber)) {
158 $errors['idnumber']= get_string('idnumbertaken');
159 }
160 }
2524b0f2 161
624458a9 162 if ($data['enrolmentkey'] != '') {
82672532 163 $errmsg = '';
624458a9
FM
164 if (!empty($CFG->groupenrolmentkeypolicy) && $group->enrolmentkey !== $data['enrolmentkey']
165 && !check_password_policy($data['enrolmentkey'], $errmsg)) {
166 // Enforce password policy when the password is changed.
82672532 167 $errors['enrolmentkey'] = $errmsg;
624458a9
FM
168 } else {
169 // Prevent twice the same enrolment key in course groups.
170 $sql = "SELECT id FROM {groups} WHERE id <> :groupid AND courseid = :courseid AND enrolmentkey = :key";
171 $params = array('groupid' => $data['id'], 'courseid' => $COURSE->id, 'key' => $data['enrolmentkey']);
172 if ($DB->record_exists_sql($sql, $params)) {
173 $errors['enrolmentkey'] = get_string('enrolmentkeyalreadyinuse', 'group');
174 }
82672532 175 }
176 }
177
178f9658 178 } else if (groups_get_group_by_name($COURSE->id, $name)) {
ddff2fa8 179 $errors['name'] = get_string('groupnameexists', 'group', $name);
74b714df
ARN
180 } else if (!empty($idnumber) && groups_get_group_by_idnumber($COURSE->id, $idnumber)) {
181 $errors['idnumber']= get_string('idnumbertaken');
624458a9 182 } else if ($data['enrolmentkey'] != '') {
9d4762c9
FM
183 $errmsg = '';
184 if (!empty($CFG->groupenrolmentkeypolicy) && !check_password_policy($data['enrolmentkey'], $errmsg)) {
185 // Enforce password policy.
186 $errors['enrolmentkey'] = $errmsg;
187 } else if ($DB->record_exists('groups', array('courseid' => $COURSE->id, 'enrolmentkey' => $data['enrolmentkey']))) {
188 // Prevent the same enrolment key from being used multiple times in course groups.
624458a9
FM
189 $errors['enrolmentkey'] = get_string('enrolmentkeyalreadyinuse', 'group');
190 }
2524b0f2 191 }
192
a78890d5 193 return $errors;
2524b0f2 194 }
195
4d8e2417
AG
196 /**
197 * Get editor options for this form
198 *
199 * @return array An array of options
200 */
8bdc9cac
SH
201 function get_editor_options() {
202 return $this->_customdata['editoroptions'];
203 }
77e90955 204}