Moodle 1.9.19 release
[moodle.git] / group / autogroup_form.php
bloba97a176fa1335fc4acb190c9b282accceedd17dd
1 <?php // $Id$
3 require_once($CFG->dirroot.'/lib/formslib.php');
5 /// get url variables
6 class autogroup_form extends moodleform {
8 // Define the form
9 function definition() {
10 global $CFG, $COURSE;
12 $mform =& $this->_form;
14 $mform->addElement('header', 'autogroup', get_string('autocreategroups', 'group'));
16 $options = array(0=>get_string('all'));
17 $options += $this->_customdata['roles'];
18 $mform->addElement('select', 'roleid', get_string('selectfromrole', 'group'), $options);
19 if (!empty($COURSE->defaultrole) and array_key_exists($COURSE->defaultrole, $options)) {
20 $mform->setDefault('roleid', $COURSE->defaultrole);
21 } else if (!empty($CFG->defaultcourseroleid) and array_key_exists($CFG->defaultcourseroleid, $options)) {
22 $mform->setDefault('roleid', $CFG->defaultcourseroleid);
25 $options = array('groups' => get_string('numgroups', 'group'),
26 'members' => get_string('nummembers', 'group'));
27 $mform->addElement('select', 'groupby', get_string('groupby', 'group'), $options);
29 $mform->addElement('text', 'number', get_string('number', 'group'),'maxlength="4" size="4"');
30 $mform->setType('number', PARAM_INT);
31 $mform->addRule('number', null, 'numeric', null, 'client');
32 $mform->addRule('number', get_string('required'), 'required', null, 'client');
34 $mform->addElement('checkbox', 'nosmallgroups', get_string('nosmallgroups', 'group'));
35 $mform->disabledIf('nosmallgroups', 'groupby', 'noteq', 'members');
36 $mform->setAdvanced('nosmallgroups');
38 $options = array('no' => get_string('noallocation', 'group'),
39 'random' => get_string('random', 'group'),
40 'firstname' => get_string('byfirstname', 'group'),
41 'lastname' => get_string('bylastname', 'group'),
42 'idnumber' => get_string('byidnumber', 'group'));
43 $mform->addElement('select', 'allocateby', get_string('allocateby', 'group'), $options);
44 $mform->setDefault('allocateby', 'random');
45 $mform->setAdvanced('allocateby');
47 $mform->addElement('text', 'namingscheme', get_string('namingscheme', 'group'));
48 $mform->setHelpButton('namingscheme', array(false, get_string('namingschemehelp', 'group'),
49 false, true, false, get_string('namingschemehelp', 'group')));
50 $mform->addRule('namingscheme', get_string('required'), 'required', null, 'client');
51 $mform->setType('namingscheme', PARAM_MULTILANG);
52 // there must not be duplicate group names in course
53 $template = get_string('grouptemplate', 'group');
54 $gname = groups_parse_name($template, 0);
55 if (!groups_get_group_by_name($COURSE->id, $gname)) {
56 $mform->setDefault('namingscheme', $template);
59 if (!empty($CFG->enablegroupings)) {
60 $options = array('0' => get_string('no'),
61 '-1'=> get_string('newgrouping', 'group'));
62 if ($groupings = groups_get_all_groupings($COURSE->id)) {
63 foreach ($groupings as $grouping) {
64 $options[$grouping->id] = strip_tags(format_string($grouping->name));
67 $mform->addElement('select', 'grouping', get_string('createingrouping', 'group'), $options);
68 if ($groupings) {
69 $mform->setDefault('grouping', '-1');
72 $mform->addElement('text', 'groupingname', get_string('groupingname', 'group'), $options);
73 $mform->setType('groupingname', PARAM_MULTILANG);
74 $mform->disabledIf('groupingname', 'grouping', 'noteq', '-1');
77 $mform->addElement('hidden','courseid');
78 $mform->setType('courseid', PARAM_INT);
80 $mform->addElement('hidden','seed');
81 $mform->setType('seed', PARAM_INT);
83 $buttonarray = array();
84 $buttonarray[] = &$mform->createElement('submit', 'preview', get_string('preview'), 'xx');
85 $buttonarray[] = &$mform->createElement('submit', 'submitbutton', get_string('submit'));
86 $buttonarray[] = &$mform->createElement('cancel');
87 $mform->addGroup($buttonarray, 'buttonar', '', array(' '), false);
88 $mform->closeHeaderBefore('buttonar');
92 function validation($data, $files) {
93 global $CFG, $COURSE;
94 $errors = parent::validation($data, $files);
96 if ($data['allocateby'] != 'no') {
97 if (!$users = groups_get_potential_members($data['courseid'], $data['roleid'])) {
98 $errors['roleid'] = get_string('nousersinrole', 'group');
101 /// Check the number entered is sane
102 if ($data['groupby'] == 'groups') {
103 $usercnt = count($users);
105 if ($data['number'] > $usercnt || $data['number'] < 1) {
106 $errors['number'] = get_string('toomanygroups', 'group', $usercnt);
111 //try to detect group name duplicates
112 $name = groups_parse_name(stripslashes(trim($data['namingscheme'])), 0);
113 if (groups_get_group_by_name($COURSE->id, $name)) {
114 $errors['namingscheme'] = get_string('groupnameexists', 'group', $name);
117 // check grouping name duplicates
118 if ( isset($data['grouping']) && $data['grouping'] == '-1') {
119 $name = trim(stripslashes($data['groupingname']));
120 if (empty($name)) {
121 $errors['groupingname'] = get_string('required');
122 } else if (groups_get_grouping_by_name($COURSE->id, $name)) {
123 $errors['groupingname'] = get_string('groupingnameexists', 'group', $name);
127 /// Check the naming scheme
128 $matchcnt = preg_match_all('/[#@]{1,1}/', $data['namingscheme'], $matches);
130 if ($matchcnt != 1) {
131 $errors['namingscheme'] = get_string('badnamingscheme', 'group');
134 return $errors;