MDL-22388 Added some checks to kill these scripts dead with an unequivocal notice...
[moodle.git] / group / autogroup_form.php
CommitLineData
4ca6cfbf 1<?php
acf000b0 2
bfebaf64
MD
3if (!defined('MOODLE_INTERNAL')) {
4 die('Direct access to this script is forbidden.'); /// It must be included from a Moodle page
5}
6
acf000b0 7require_once($CFG->dirroot.'/lib/formslib.php');
92343cd2 8require_once($CFG->dirroot.'/cohort/lib.php');
acf000b0 9
10/// get url variables
11class autogroup_form extends moodleform {
12
13 // Define the form
14 function definition() {
15 global $CFG, $COURSE;
16
17 $mform =& $this->_form;
18
19 $mform->addElement('header', 'autogroup', get_string('autocreategroups', 'group'));
20
f16fa0a3 21 $options = array(0=>get_string('all'));
acf000b0 22 $options += $this->_customdata['roles'];
23 $mform->addElement('select', 'roleid', get_string('selectfromrole', 'group'), $options);
f16fa0a3 24 if (!empty($COURSE->defaultrole) and array_key_exists($COURSE->defaultrole, $options)) {
25 $mform->setDefault('roleid', $COURSE->defaultrole);
26 } else if (!empty($CFG->defaultcourseroleid) and array_key_exists($CFG->defaultcourseroleid, $options)) {
27 $mform->setDefault('roleid', $CFG->defaultcourseroleid);
28 }
acf000b0 29
3c35aea2
PS
30 $context = get_context_instance(CONTEXT_COURSE, $COURSE->id);
31 if (has_capability('moodle/cohort:view', $context)) {
32 $options = cohort_get_visible_list($COURSE);
33 if ($options) {
34 $options = array(0=>get_string('anycohort', 'cohort')) + $options;
35 $mform->addElement('select', 'cohortid', get_string('selectfromcohort', 'cohort'), $options);
36 $mform->setDefault('cohortid', '0');
37 } else {
38 $mform->addElement('hidden','cohortid');
39 $mform->setType('cohortid', PARAM_INT);
40 $mform->setConstant('cohortid', '0');
41 }
92343cd2
PS
42 } else {
43 $mform->addElement('hidden','cohortid');
44 $mform->setType('cohortid', PARAM_INT);
3c35aea2 45 $mform->setConstant('cohortid', '0');
92343cd2
PS
46 }
47
f16fa0a3 48 $options = array('groups' => get_string('numgroups', 'group'),
49 'members' => get_string('nummembers', 'group'));
acf000b0 50 $mform->addElement('select', 'groupby', get_string('groupby', 'group'), $options);
f16fa0a3 51
acf000b0 52 $mform->addElement('text', 'number', get_string('number', 'group'),'maxlength="4" size="4"');
53 $mform->setType('number', PARAM_INT);
54 $mform->addRule('number', null, 'numeric', null, 'client');
55 $mform->addRule('number', get_string('required'), 'required', null, 'client');
f16fa0a3 56
57 $mform->addElement('checkbox', 'nosmallgroups', get_string('nosmallgroups', 'group'));
58 $mform->disabledIf('nosmallgroups', 'groupby', 'noteq', 'members');
59 $mform->setAdvanced('nosmallgroups');
60
61 $options = array('no' => get_string('noallocation', 'group'),
62 'random' => get_string('random', 'group'),
63 'firstname' => get_string('byfirstname', 'group'),
64 'lastname' => get_string('bylastname', 'group'),
65 'idnumber' => get_string('byidnumber', 'group'));
acf000b0 66 $mform->addElement('select', 'allocateby', get_string('allocateby', 'group'), $options);
f16fa0a3 67 $mform->setDefault('allocateby', 'random');
68 $mform->setAdvanced('allocateby');
69
70 $mform->addElement('text', 'namingscheme', get_string('namingscheme', 'group'));
f8065dd2 71 $mform->setHelpButton('namingscheme', array('namingscheme', get_string('namingschemehelp', 'group'), 'group'));
f16fa0a3 72 $mform->addRule('namingscheme', get_string('required'), 'required', null, 'client');
73 $mform->setType('namingscheme', PARAM_MULTILANG);
74 // there must not be duplicate group names in course
75 $template = get_string('grouptemplate', 'group');
76 $gname = groups_parse_name($template, 0);
77 if (!groups_get_group_by_name($COURSE->id, $gname)) {
78 $mform->setDefault('namingscheme', $template);
79 }
80
98da6021
PS
81 $options = array('0' => get_string('no'),
82 '-1'=> get_string('newgrouping', 'group'));
83 if ($groupings = groups_get_all_groupings($COURSE->id)) {
84 foreach ($groupings as $grouping) {
85 $options[$grouping->id] = strip_tags(format_string($grouping->name));
f16fa0a3 86 }
f16fa0a3 87 }
98da6021
PS
88 $mform->addElement('select', 'grouping', get_string('createingrouping', 'group'), $options);
89 if ($groupings) {
90 $mform->setDefault('grouping', '-1');
91 }
92
93 $mform->addElement('text', 'groupingname', get_string('groupingname', 'group'), $options);
94 $mform->setType('groupingname', PARAM_MULTILANG);
95 $mform->disabledIf('groupingname', 'grouping', 'noteq', '-1');
acf000b0 96
97 $mform->addElement('hidden','courseid');
98 $mform->setType('courseid', PARAM_INT);
f16fa0a3 99
acf000b0 100 $mform->addElement('hidden','seed');
101 $mform->setType('seed', PARAM_INT);
f16fa0a3 102
103 $buttonarray = array();
acf000b0 104 $buttonarray[] = &$mform->createElement('submit', 'preview', get_string('preview'), 'xx');
105 $buttonarray[] = &$mform->createElement('submit', 'submitbutton', get_string('submit'));
106 $buttonarray[] = &$mform->createElement('cancel');
107 $mform->addGroup($buttonarray, 'buttonar', '', array(' '), false);
108 $mform->closeHeaderBefore('buttonar');
109 }
110
111
f16fa0a3 112 function validation($data, $files) {
4454447d 113 global $CFG, $COURSE;
a78890d5 114 $errors = parent::validation($data, $files);
acf000b0 115
f16fa0a3 116 if ($data['allocateby'] != 'no') {
92343cd2 117 if (!$users = groups_get_potential_members($data['courseid'], $data['roleid'], $data['cohortid'])) {
f16fa0a3 118 $errors['roleid'] = get_string('nousersinrole', 'group');
119 }
120
121 /// Check the number entered is sane
122 if ($data['groupby'] == 'groups') {
123 $usercnt = count($users);
124
125 if ($data['number'] > $usercnt || $data['number'] < 1) {
4454447d 126 $errors['number'] = get_string('toomanygroups', 'group', $usercnt);
f16fa0a3 127 }
128 }
129 }
130
131 //try to detect group name duplicates
32648682 132 $name = groups_parse_name(trim($data['namingscheme']), 0);
f16fa0a3 133 if (groups_get_group_by_name($COURSE->id, $name)) {
134 $errors['namingscheme'] = get_string('groupnameexists', 'group', $name);
acf000b0 135 }
acf000b0 136
f16fa0a3 137 // check grouping name duplicates
5447d8f0 138 if ( isset($data['grouping']) && $data['grouping'] == '-1') {
32648682 139 $name = trim($data['groupingname']);
f16fa0a3 140 if (empty($name)) {
141 $errors['groupingname'] = get_string('required');
142 } else if (groups_get_grouping_by_name($COURSE->id, $name)) {
143 $errors['groupingname'] = get_string('groupingnameexists', 'group', $name);
acf000b0 144 }
145 }
f16fa0a3 146
147 /// Check the naming scheme
148 $matchcnt = preg_match_all('/[#@]{1,1}/', $data['namingscheme'], $matches);
149
acf000b0 150 if ($matchcnt != 1) {
f16fa0a3 151 $errors['namingscheme'] = get_string('badnamingscheme', 'group');
acf000b0 152 }
acf000b0 153
f16fa0a3 154 return $errors;
155 }
acf000b0 156}