forked from moodle/moodle
-
Notifications
You must be signed in to change notification settings - Fork 1
/
autogroup_form.php
197 lines (168 loc) · 8.02 KB
/
autogroup_form.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Auto group form
*
* @package core_group
* @copyright 2007 mattc-catalyst (http://moodle.com)
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
if (!defined('MOODLE_INTERNAL')) {
die('Direct access to this script is forbidden.'); /// It must be included from a Moodle page
}
require_once($CFG->dirroot.'/lib/formslib.php');
require_once($CFG->dirroot.'/cohort/lib.php');
/**
* Auto group form class
*
* @package core_group
* @copyright 2007 mattc-catalyst (http://moodle.com)
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class autogroup_form extends moodleform {
/**
* Form Definition
*/
function definition() {
global $CFG, $COURSE;
$mform =& $this->_form;
$mform->addElement('header', 'autogroup', get_string('autocreategroups', 'group'));
$options = array(0=>get_string('all'));
$options += $this->_customdata['roles'];
$mform->addElement('select', 'roleid', get_string('selectfromrole', 'group'), $options);
$student = get_archetype_roles('student');
$student = reset($student);
if ($student and array_key_exists($student->id, $options)) {
$mform->setDefault('roleid', $student->id);
}
$context = get_context_instance(CONTEXT_COURSE, $COURSE->id);
if (has_capability('moodle/cohort:view', $context)) {
$options = cohort_get_visible_list($COURSE);
if ($options) {
$options = array(0=>get_string('anycohort', 'cohort')) + $options;
$mform->addElement('select', 'cohortid', get_string('selectfromcohort', 'cohort'), $options);
$mform->setDefault('cohortid', '0');
} else {
$mform->addElement('hidden','cohortid');
$mform->setType('cohortid', PARAM_INT);
$mform->setConstant('cohortid', '0');
}
} else {
$mform->addElement('hidden','cohortid');
$mform->setType('cohortid', PARAM_INT);
$mform->setConstant('cohortid', '0');
}
$options = array('groups' => get_string('numgroups', 'group'),
'members' => get_string('nummembers', 'group'));
$mform->addElement('select', 'groupby', get_string('groupby', 'group'), $options);
$mform->addElement('text', 'number', get_string('number', 'group'),'maxlength="4" size="4"');
$mform->setType('number', PARAM_INT);
$mform->addRule('number', null, 'numeric', null, 'client');
$mform->addRule('number', get_string('required'), 'required', null, 'client');
$mform->addElement('checkbox', 'nosmallgroups', get_string('nosmallgroups', 'group'));
$mform->disabledIf('nosmallgroups', 'groupby', 'noteq', 'members');
$mform->setAdvanced('nosmallgroups');
$options = array('no' => get_string('noallocation', 'group'),
'random' => get_string('random', 'group'),
'firstname' => get_string('byfirstname', 'group'),
'lastname' => get_string('bylastname', 'group'),
'idnumber' => get_string('byidnumber', 'group'));
$mform->addElement('select', 'allocateby', get_string('allocateby', 'group'), $options);
$mform->setDefault('allocateby', 'random');
$mform->setAdvanced('allocateby');
$mform->addElement('text', 'namingscheme', get_string('namingscheme', 'group'));
$mform->addHelpButton('namingscheme', 'namingscheme', 'group');
$mform->addRule('namingscheme', get_string('required'), 'required', null, 'client');
$mform->setType('namingscheme', PARAM_MULTILANG);
// there must not be duplicate group names in course
$template = get_string('grouptemplate', 'group');
$gname = groups_parse_name($template, 0);
if (!groups_get_group_by_name($COURSE->id, $gname)) {
$mform->setDefault('namingscheme', $template);
}
$options = array('0' => get_string('no'),
'-1'=> get_string('newgrouping', 'group'));
if ($groupings = groups_get_all_groupings($COURSE->id)) {
foreach ($groupings as $grouping) {
$options[$grouping->id] = strip_tags(format_string($grouping->name));
}
}
$mform->addElement('select', 'grouping', get_string('createingrouping', 'group'), $options);
if ($groupings) {
$mform->setDefault('grouping', '-1');
}
$mform->addElement('text', 'groupingname', get_string('groupingname', 'group'), $options);
$mform->setType('groupingname', PARAM_MULTILANG);
$mform->disabledIf('groupingname', 'grouping', 'noteq', '-1');
$mform->addElement('hidden','courseid');
$mform->setType('courseid', PARAM_INT);
$mform->addElement('hidden','seed');
$mform->setType('seed', PARAM_INT);
$buttonarray = array();
$buttonarray[] = &$mform->createElement('submit', 'preview', get_string('preview'), 'xx');
$buttonarray[] = &$mform->createElement('submit', 'submitbutton', get_string('submit'));
$buttonarray[] = &$mform->createElement('cancel');
$mform->addGroup($buttonarray, 'buttonar', '', array(' '), false);
$mform->closeHeaderBefore('buttonar');
}
/**
* Performs validation of the form information
*
* @param array $data
* @param array $files
* @return array $errors An array of $errors
*/
function validation($data, $files) {
global $CFG, $COURSE;
$errors = parent::validation($data, $files);
if ($data['allocateby'] != 'no') {
if (!$users = groups_get_potential_members($data['courseid'], $data['roleid'], $data['cohortid'])) {
$errors['roleid'] = get_string('nousersinrole', 'group');
}
/// Check the number entered is sane
if ($data['groupby'] == 'groups') {
$usercnt = count($users);
if ($data['number'] > $usercnt || $data['number'] < 1) {
$errors['number'] = get_string('toomanygroups', 'group', $usercnt);
}
}
}
//try to detect group name duplicates
$name = groups_parse_name(trim($data['namingscheme']), 0);
if (groups_get_group_by_name($COURSE->id, $name)) {
$errors['namingscheme'] = get_string('groupnameexists', 'group', $name);
}
// check grouping name duplicates
if ( isset($data['grouping']) && $data['grouping'] == '-1') {
$name = trim($data['groupingname']);
if (empty($name)) {
$errors['groupingname'] = get_string('required');
} else if (groups_get_grouping_by_name($COURSE->id, $name)) {
$errors['groupingname'] = get_string('groupingnameexists', 'group', $name);
}
}
/// Check the naming scheme
if ($data['groupby'] == 'groups' and $data['number'] == 1) {
// we can use the name as is because there will be only one group max
} else {
$matchcnt = preg_match_all('/[#@]{1,1}/', $data['namingscheme'], $matches);
if ($matchcnt != 1) {
$errors['namingscheme'] = get_string('badnamingscheme', 'group');
}
}
return $errors;
}
}