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/>.
22 * @copyright 2007 mattc-catalyst (http://moodle.com)
23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
25 if (!defined('MOODLE_INTERNAL')) {
26 die('Direct access to this script is forbidden.'); /// It must be included from a Moodle page
29 require_once($CFG->dirroot
.'/lib/formslib.php');
30 require_once($CFG->dirroot
.'/cohort/lib.php');
33 * Auto group form class
36 * @copyright 2007 mattc-catalyst (http://moodle.com)
37 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
39 class autogroup_form
extends moodleform
{
44 function definition() {
47 $mform =& $this->_form
;
49 $mform->addElement('header', 'autogroup', get_string('autocreategroups', 'group'));
51 $options = array(0=>get_string('all'));
52 $options +
= $this->_customdata
['roles'];
53 $mform->addElement('select', 'roleid', get_string('selectfromrole', 'group'), $options);
55 $student = get_archetype_roles('student');
56 $student = reset($student);
58 if ($student and array_key_exists($student->id
, $options)) {
59 $mform->setDefault('roleid', $student->id
);
62 $context = context_course
::instance($COURSE->id
);
63 if (has_capability('moodle/cohort:view', $context)) {
64 $options = cohort_get_visible_list($COURSE);
66 $options = array(0=>get_string('anycohort', 'cohort')) +
$options;
67 $mform->addElement('select', 'cohortid', get_string('selectfromcohort', 'cohort'), $options);
68 $mform->setDefault('cohortid', '0');
70 $mform->addElement('hidden','cohortid');
71 $mform->setType('cohortid', PARAM_INT
);
72 $mform->setConstant('cohortid', '0');
75 $mform->addElement('hidden','cohortid');
76 $mform->setType('cohortid', PARAM_INT
);
77 $mform->setConstant('cohortid', '0');
80 $options = array('groups' => get_string('numgroups', 'group'),
81 'members' => get_string('nummembers', 'group'));
82 $mform->addElement('select', 'groupby', get_string('groupby', 'group'), $options);
84 $mform->addElement('text', 'number', get_string('number', 'group'),'maxlength="4" size="4"');
85 $mform->setType('number', PARAM_INT
);
86 $mform->addRule('number', null, 'numeric', null, 'client');
87 $mform->addRule('number', get_string('required'), 'required', null, 'client');
89 $mform->addElement('checkbox', 'nosmallgroups', get_string('nosmallgroups', 'group'));
90 $mform->disabledIf('nosmallgroups', 'groupby', 'noteq', 'members');
91 $mform->setAdvanced('nosmallgroups');
93 $options = array('no' => get_string('noallocation', 'group'),
94 'random' => get_string('random', 'group'),
95 'firstname' => get_string('byfirstname', 'group'),
96 'lastname' => get_string('bylastname', 'group'),
97 'idnumber' => get_string('byidnumber', 'group'));
98 $mform->addElement('select', 'allocateby', get_string('allocateby', 'group'), $options);
99 $mform->setDefault('allocateby', 'random');
100 $mform->setAdvanced('allocateby');
102 $mform->addElement('text', 'namingscheme', get_string('namingscheme', 'group'));
103 $mform->addHelpButton('namingscheme', 'namingscheme', 'group');
104 $mform->addRule('namingscheme', get_string('required'), 'required', null, 'client');
105 $mform->setType('namingscheme', PARAM_TEXT
);
106 // there must not be duplicate group names in course
107 $template = get_string('grouptemplate', 'group');
108 $gname = groups_parse_name($template, 0);
109 if (!groups_get_group_by_name($COURSE->id
, $gname)) {
110 $mform->setDefault('namingscheme', $template);
113 $options = array('0' => get_string('no'),
114 '-1'=> get_string('newgrouping', 'group'));
115 if ($groupings = groups_get_all_groupings($COURSE->id
)) {
116 foreach ($groupings as $grouping) {
117 $options[$grouping->id
] = strip_tags(format_string($grouping->name
));
120 $mform->addElement('select', 'grouping', get_string('createingrouping', 'group'), $options);
122 $mform->setDefault('grouping', '-1');
125 $mform->addElement('text', 'groupingname', get_string('groupingname', 'group'), $options);
126 $mform->setType('groupingname', PARAM_TEXT
);
127 $mform->disabledIf('groupingname', 'grouping', 'noteq', '-1');
129 $mform->addElement('hidden','courseid');
130 $mform->setType('courseid', PARAM_INT
);
132 $mform->addElement('hidden','seed');
133 $mform->setType('seed', PARAM_INT
);
135 $buttonarray = array();
136 $buttonarray[] = &$mform->createElement('submit', 'preview', get_string('preview'), 'xx');
137 $buttonarray[] = &$mform->createElement('submit', 'submitbutton', get_string('submit'));
138 $buttonarray[] = &$mform->createElement('cancel');
139 $mform->addGroup($buttonarray, 'buttonar', '', array(' '), false);
140 $mform->closeHeaderBefore('buttonar');
144 * Performs validation of the form information
147 * @param array $files
148 * @return array $errors An array of $errors
150 function validation($data, $files) {
151 global $CFG, $COURSE;
152 $errors = parent
::validation($data, $files);
154 if ($data['allocateby'] != 'no') {
155 if (!$users = groups_get_potential_members($data['courseid'], $data['roleid'], $data['cohortid'])) {
156 $errors['roleid'] = get_string('nousersinrole', 'group');
159 /// Check the number entered is sane
160 if ($data['groupby'] == 'groups') {
161 $usercnt = count($users);
163 if ($data['number'] > $usercnt ||
$data['number'] < 1) {
164 $errors['number'] = get_string('toomanygroups', 'group', $usercnt);
169 //try to detect group name duplicates
170 $name = groups_parse_name(trim($data['namingscheme']), 0);
171 if (groups_get_group_by_name($COURSE->id
, $name)) {
172 $errors['namingscheme'] = get_string('groupnameexists', 'group', $name);
175 // check grouping name duplicates
176 if ( isset($data['grouping']) && $data['grouping'] == '-1') {
177 $name = trim($data['groupingname']);
179 $errors['groupingname'] = get_string('required');
180 } else if (groups_get_grouping_by_name($COURSE->id
, $name)) {
181 $errors['groupingname'] = get_string('groupingnameexists', 'group', $name);
185 /// Check the naming scheme
186 if ($data['groupby'] == 'groups' and $data['number'] == 1) {
187 // we can use the name as is because there will be only one group max
189 $matchcnt = preg_match_all('/[#@]{1,1}/', $data['namingscheme'], $matches);
190 if ($matchcnt != 1) {
191 $errors['namingscheme'] = get_string('badnamingscheme', 'group');