Merge branch 'MDL-41152-26' of git://github.com/FMCorz/moodle into MOODLE_26_STABLE
[moodle.git] / group / autogroup_form.php
blobc31ade15df5827e7c3f415c12be6a61245e9653b
1 <?php
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.
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/>.
18 /**
19 * Auto group form
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
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');
32 /**
33 * Auto group form class
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
39 class autogroup_form extends moodleform {
41 /**
42 * Form Definition
44 function definition() {
45 global $CFG, $COURSE;
47 $mform =& $this->_form;
49 $mform->addElement('header', 'autogroup', get_string('general'));
51 $mform->addElement('text', 'namingscheme', get_string('namingscheme', 'group'));
52 $mform->addHelpButton('namingscheme', 'namingscheme', 'group');
53 $mform->addRule('namingscheme', get_string('required'), 'required', null, 'client');
54 $mform->setType('namingscheme', PARAM_TEXT);
55 // There must not be duplicate group names in course.
56 $template = get_string('grouptemplate', 'group');
57 $gname = groups_parse_name($template, 0);
58 if (!groups_get_group_by_name($COURSE->id, $gname)) {
59 $mform->setDefault('namingscheme', $template);
62 $options = array('groups' => get_string('numgroups', 'group'),
63 'members' => get_string('nummembers', 'group'));
64 $mform->addElement('select', 'groupby', get_string('groupby', 'group'), $options);
66 $mform->addElement('text', 'number', get_string('number', 'group'),'maxlength="4" size="4"');
67 $mform->setType('number', PARAM_INT);
68 $mform->addRule('number', null, 'numeric', null, 'client');
69 $mform->addRule('number', get_string('required'), 'required', null, 'client');
71 $mform->addElement('header', 'groupmembershdr', get_string('groupmembers', 'group'));
72 $mform->setExpanded('groupmembershdr', true);
74 $options = array(0=>get_string('all'));
75 $options += $this->_customdata['roles'];
76 $mform->addElement('select', 'roleid', get_string('selectfromrole', 'group'), $options);
78 $student = get_archetype_roles('student');
79 $student = reset($student);
81 if ($student and array_key_exists($student->id, $options)) {
82 $mform->setDefault('roleid', $student->id);
85 $context = context_course::instance($COURSE->id);
86 if (has_capability('moodle/cohort:view', $context)) {
87 $options = cohort_get_visible_list($COURSE);
88 if ($options) {
89 $options = array(0=>get_string('anycohort', 'cohort')) + $options;
90 $mform->addElement('select', 'cohortid', get_string('selectfromcohort', 'cohort'), $options);
91 $mform->setDefault('cohortid', '0');
92 } else {
93 $mform->addElement('hidden','cohortid');
94 $mform->setType('cohortid', PARAM_INT);
95 $mform->setConstant('cohortid', '0');
97 } else {
98 $mform->addElement('hidden','cohortid');
99 $mform->setType('cohortid', PARAM_INT);
100 $mform->setConstant('cohortid', '0');
102 $options = array('no' => get_string('noallocation', 'group'),
103 'random' => get_string('random', 'group'),
104 'firstname' => get_string('byfirstname', 'group'),
105 'lastname' => get_string('bylastname', 'group'),
106 'idnumber' => get_string('byidnumber', 'group'));
107 $mform->addElement('select', 'allocateby', get_string('allocateby', 'group'), $options);
108 $mform->setDefault('allocateby', 'random');
110 $mform->addElement('checkbox', 'nosmallgroups', get_string('nosmallgroups', 'group'));
111 $mform->disabledIf('nosmallgroups', 'groupby', 'noteq', 'members');
113 $mform->addElement('header', 'groupinghdr', get_string('grouping', 'group'));
115 $options = array('0' => get_string('nogrouping', 'group'),
116 '-1'=> get_string('newgrouping', 'group'));
117 if ($groupings = groups_get_all_groupings($COURSE->id)) {
118 foreach ($groupings as $grouping) {
119 $options[$grouping->id] = strip_tags(format_string($grouping->name));
122 $mform->addElement('select', 'grouping', get_string('createingrouping', 'group'), $options);
123 if ($groupings) {
124 $mform->setDefault('grouping', '-1');
127 $mform->addElement('text', 'groupingname', get_string('groupingname', 'group'), $options);
128 $mform->setType('groupingname', PARAM_TEXT);
129 $mform->disabledIf('groupingname', 'grouping', 'noteq', '-1');
131 $mform->addElement('hidden','courseid');
132 $mform->setType('courseid', PARAM_INT);
134 $mform->addElement('hidden','seed');
135 $mform->setType('seed', PARAM_INT);
137 $buttonarray = array();
138 $buttonarray[] = &$mform->createElement('submit', 'preview', get_string('preview'), 'xx');
139 $buttonarray[] = &$mform->createElement('submit', 'submitbutton', get_string('submit'));
140 $buttonarray[] = &$mform->createElement('cancel');
141 $mform->addGroup($buttonarray, 'buttonar', '', array(' '), false);
142 $mform->closeHeaderBefore('buttonar');
146 * Performs validation of the form information
148 * @param array $data
149 * @param array $files
150 * @return array $errors An array of $errors
152 function validation($data, $files) {
153 global $CFG, $COURSE;
154 $errors = parent::validation($data, $files);
156 if ($data['allocateby'] != 'no') {
157 if (!$users = groups_get_potential_members($data['courseid'], $data['roleid'], $data['cohortid'])) {
158 $errors['roleid'] = get_string('nousersinrole', 'group');
161 /// Check the number entered is sane
162 if ($data['groupby'] == 'groups') {
163 $usercnt = count($users);
165 if ($data['number'] > $usercnt || $data['number'] < 1) {
166 $errors['number'] = get_string('toomanygroups', 'group', $usercnt);
171 //try to detect group name duplicates
172 $name = groups_parse_name(trim($data['namingscheme']), 0);
173 if (groups_get_group_by_name($COURSE->id, $name)) {
174 $errors['namingscheme'] = get_string('groupnameexists', 'group', $name);
177 // check grouping name duplicates
178 if ( isset($data['grouping']) && $data['grouping'] == '-1') {
179 $name = trim($data['groupingname']);
180 if (empty($name)) {
181 $errors['groupingname'] = get_string('required');
182 } else if (groups_get_grouping_by_name($COURSE->id, $name)) {
183 $errors['groupingname'] = get_string('groupingnameexists', 'group', $name);
187 /// Check the naming scheme
188 if ($data['groupby'] == 'groups' and $data['number'] == 1) {
189 // we can use the name as is because there will be only one group max
190 } else {
191 $matchcnt = preg_match_all('/[#@]{1,1}/', $data['namingscheme'], $matches);
192 if ($matchcnt != 1) {
193 $errors['namingscheme'] = get_string('badnamingscheme', 'group');
197 return $errors;