weekly release 3.10.1+
[moodle.git] / group / autogroup_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
18/**
19 * Auto group form
20 *
21 * @package core_group
22 * @copyright 2007 mattc-catalyst (http://moodle.com)
23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
24 */
bfebaf64
MD
25if (!defined('MOODLE_INTERNAL')) {
26 die('Direct access to this script is forbidden.'); /// It must be included from a Moodle page
27}
28
acf000b0 29require_once($CFG->dirroot.'/lib/formslib.php');
92343cd2 30require_once($CFG->dirroot.'/cohort/lib.php');
acf000b0 31
4d8e2417
AG
32/**
33 * Auto group form class
34 *
35 * @package core_group
36 * @copyright 2007 mattc-catalyst (http://moodle.com)
37 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
38 */
acf000b0 39class autogroup_form extends moodleform {
40
4d8e2417
AG
41 /**
42 * Form Definition
43 */
acf000b0 44 function definition() {
e5e5b377
JP
45 global $USER, $COURSE;
46 $coursecontext = context_course::instance($COURSE->id);
acf000b0 47
48 $mform =& $this->_form;
49
5995717e
FM
50 $mform->addElement('header', 'autogroup', get_string('general'));
51
52 $mform->addElement('text', 'namingscheme', get_string('namingscheme', 'group'));
53 $mform->addHelpButton('namingscheme', 'namingscheme', 'group');
54 $mform->addRule('namingscheme', get_string('required'), 'required', null, 'client');
55 $mform->setType('namingscheme', PARAM_TEXT);
56 // There must not be duplicate group names in course.
57 $template = get_string('grouptemplate', 'group');
58 $gname = groups_parse_name($template, 0);
59 if (!groups_get_group_by_name($COURSE->id, $gname)) {
60 $mform->setDefault('namingscheme', $template);
61 }
62
63 $options = array('groups' => get_string('numgroups', 'group'),
64 'members' => get_string('nummembers', 'group'));
65 $mform->addElement('select', 'groupby', get_string('groupby', 'group'), $options);
66
67 $mform->addElement('text', 'number', get_string('number', 'group'),'maxlength="4" size="4"');
68 $mform->setType('number', PARAM_INT);
69 $mform->addRule('number', null, 'numeric', null, 'client');
70 $mform->addRule('number', get_string('required'), 'required', null, 'client');
71
e5e5b377
JP
72 // Enable group messaging for the groups to be auto-created.
73 if (\core_message\api::can_create_group_conversation($USER->id, $coursecontext)) {
74 $mform->addElement('selectyesno', 'enablemessaging', get_string('enablemessaging', 'group'));
75 $mform->addHelpButton('enablemessaging', 'enablemessaging', 'group');
76 }
77
5995717e
FM
78 $mform->addElement('header', 'groupmembershdr', get_string('groupmembers', 'group'));
79 $mform->setExpanded('groupmembershdr', true);
acf000b0 80
f16fa0a3 81 $options = array(0=>get_string('all'));
acf000b0 82 $options += $this->_customdata['roles'];
83 $mform->addElement('select', 'roleid', get_string('selectfromrole', 'group'), $options);
df997f84
PS
84
85 $student = get_archetype_roles('student');
86 $student = reset($student);
87
88 if ($student and array_key_exists($student->id, $options)) {
89 $mform->setDefault('roleid', $student->id);
f16fa0a3 90 }
acf000b0 91
7b96ffb0 92 $coursecontext = context_course::instance($COURSE->id);
5ceb68d1 93 if ($cohorts = cohort_get_available_cohorts($coursecontext, COHORT_WITH_ENROLLED_MEMBERS_ONLY, 0, 0)) {
80f98467
MG
94 $options = array(0 => get_string('anycohort', 'cohort'));
95 foreach ($cohorts as $c) {
96 $options[$c->id] = format_string($c->name, true, context::instance_by_id($c->contextid));
3c35aea2 97 }
80f98467
MG
98 $mform->addElement('select', 'cohortid', get_string('selectfromcohort', 'cohort'), $options);
99 $mform->setDefault('cohortid', '0');
92343cd2
PS
100 } else {
101 $mform->addElement('hidden','cohortid');
102 $mform->setType('cohortid', PARAM_INT);
3c35aea2 103 $mform->setConstant('cohortid', '0');
92343cd2 104 }
e4ebf7ef
GF
105
106 if ($groupings = groups_get_all_groupings($COURSE->id)) {
107 $options = array();
108 $options[0] = get_string('none');
109 foreach ($groupings as $grouping) {
110 $options[$grouping->id] = format_string($grouping->name);
111 }
112 $mform->addElement('select', 'groupingid', get_string('selectfromgrouping', 'group'), $options);
113 $mform->setDefault('groupingid', 0);
114 $mform->disabledIf('groupingid', 'notingroup', 'checked');
115 } else {
116 $mform->addElement('hidden', 'groupingid');
117 $mform->setType('groupingid', PARAM_INT);
118 $mform->setConstant('groupingid', 0);
119 }
120
121 if ($groups = groups_get_all_groups($COURSE->id)) {
122 $options = array();
123 $options[0] = get_string('none');
124 foreach ($groups as $group) {
125 $options[$group->id] = format_string($group->name);
126 }
127 $mform->addElement('select', 'groupid', get_string('selectfromgroup', 'group'), $options);
128 $mform->setDefault('groupid', 0);
129 $mform->disabledIf('groupid', 'notingroup', 'checked');
130 } else {
131 $mform->addElement('hidden', 'groupid');
132 $mform->setType('groupid', PARAM_INT);
133 $mform->setConstant('groupid', 0);
134 }
135
f16fa0a3 136 $options = array('no' => get_string('noallocation', 'group'),
137 'random' => get_string('random', 'group'),
138 'firstname' => get_string('byfirstname', 'group'),
139 'lastname' => get_string('bylastname', 'group'),
140 'idnumber' => get_string('byidnumber', 'group'));
acf000b0 141 $mform->addElement('select', 'allocateby', get_string('allocateby', 'group'), $options);
f16fa0a3 142 $mform->setDefault('allocateby', 'random');
f16fa0a3 143
5995717e
FM
144 $mform->addElement('checkbox', 'nosmallgroups', get_string('nosmallgroups', 'group'));
145 $mform->disabledIf('nosmallgroups', 'groupby', 'noteq', 'members');
146
55c9a150 147 $mform->addElement('checkbox', 'notingroup', get_string('notingroup', 'group'));
e4ebf7ef
GF
148 $mform->disabledIf('notingroup', 'groupingid', 'neq', 0);
149 $mform->disabledIf('notingroup', 'groupid', 'neq', 0);
55c9a150 150
7b96ffb0
NT
151 if (has_capability('moodle/course:viewsuspendedusers', $coursecontext)) {
152 $mform->addElement('checkbox', 'includeonlyactiveenrol', get_string('includeonlyactiveenrol', 'group'), '');
153 $mform->addHelpButton('includeonlyactiveenrol', 'includeonlyactiveenrol', 'group');
154 $mform->setDefault('includeonlyactiveenrol', true);
155 }
156
5995717e 157 $mform->addElement('header', 'groupinghdr', get_string('grouping', 'group'));
f16fa0a3 158
5995717e 159 $options = array('0' => get_string('nogrouping', 'group'),
98da6021
PS
160 '-1'=> get_string('newgrouping', 'group'));
161 if ($groupings = groups_get_all_groupings($COURSE->id)) {
162 foreach ($groupings as $grouping) {
163 $options[$grouping->id] = strip_tags(format_string($grouping->name));
f16fa0a3 164 }
f16fa0a3 165 }
98da6021
PS
166 $mform->addElement('select', 'grouping', get_string('createingrouping', 'group'), $options);
167 if ($groupings) {
168 $mform->setDefault('grouping', '-1');
169 }
170
171 $mform->addElement('text', 'groupingname', get_string('groupingname', 'group'), $options);
071e68f9 172 $mform->setType('groupingname', PARAM_TEXT);
98da6021 173 $mform->disabledIf('groupingname', 'grouping', 'noteq', '-1');
acf000b0 174
175 $mform->addElement('hidden','courseid');
176 $mform->setType('courseid', PARAM_INT);
f16fa0a3 177
acf000b0 178 $mform->addElement('hidden','seed');
179 $mform->setType('seed', PARAM_INT);
f16fa0a3 180
181 $buttonarray = array();
c3c424a0 182 $buttonarray[] = &$mform->createElement('submit', 'preview', get_string('preview'));
acf000b0 183 $buttonarray[] = &$mform->createElement('submit', 'submitbutton', get_string('submit'));
184 $buttonarray[] = &$mform->createElement('cancel');
185 $mform->addGroup($buttonarray, 'buttonar', '', array(' '), false);
186 $mform->closeHeaderBefore('buttonar');
187 }
188
4d8e2417
AG
189 /**
190 * Performs validation of the form information
191 *
192 * @param array $data
193 * @param array $files
194 * @return array $errors An array of $errors
195 */
f16fa0a3 196 function validation($data, $files) {
4454447d 197 global $CFG, $COURSE;
a78890d5 198 $errors = parent::validation($data, $files);
acf000b0 199
f16fa0a3 200 if ($data['allocateby'] != 'no') {
e4ebf7ef
GF
201 $source = array();
202 if ($data['cohortid']) {
203 $source['cohortid'] = $data['cohortid'];
204 }
205 if ($data['groupingid']) {
206 $source['groupingid'] = $data['groupingid'];
207 }
208 if ($data['groupid']) {
209 $source['groupid'] = $data['groupid'];
210 }
211 if (!$users = groups_get_potential_members($data['courseid'], $data['roleid'], $source)) {
f16fa0a3 212 $errors['roleid'] = get_string('nousersinrole', 'group');
213 }
214
215 /// Check the number entered is sane
216 if ($data['groupby'] == 'groups') {
217 $usercnt = count($users);
218
219 if ($data['number'] > $usercnt || $data['number'] < 1) {
4454447d 220 $errors['number'] = get_string('toomanygroups', 'group', $usercnt);
f16fa0a3 221 }
222 }
223 }
224
225 //try to detect group name duplicates
32648682 226 $name = groups_parse_name(trim($data['namingscheme']), 0);
f16fa0a3 227 if (groups_get_group_by_name($COURSE->id, $name)) {
228 $errors['namingscheme'] = get_string('groupnameexists', 'group', $name);
acf000b0 229 }
acf000b0 230
f16fa0a3 231 // check grouping name duplicates
5447d8f0 232 if ( isset($data['grouping']) && $data['grouping'] == '-1') {
32648682 233 $name = trim($data['groupingname']);
f16fa0a3 234 if (empty($name)) {
235 $errors['groupingname'] = get_string('required');
236 } else if (groups_get_grouping_by_name($COURSE->id, $name)) {
237 $errors['groupingname'] = get_string('groupingnameexists', 'group', $name);
acf000b0 238 }
239 }
f16fa0a3 240
241 /// Check the naming scheme
96445d40
PS
242 if ($data['groupby'] == 'groups' and $data['number'] == 1) {
243 // we can use the name as is because there will be only one group max
244 } else {
245 $matchcnt = preg_match_all('/[#@]{1,1}/', $data['namingscheme'], $matches);
246 if ($matchcnt != 1) {
247 $errors['namingscheme'] = get_string('badnamingscheme', 'group');
248 }
acf000b0 249 }
acf000b0 250
f16fa0a3 251 return $errors;
252 }
acf000b0 253}