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