Merge branch 'w43_MDL-29964_m22_fixenrolevents' of git://github.com/skodak/moodle
[moodle.git] / group / autogroup_form.php
blob36433f08cf6570e0034933616bee97b5dba9ef93
1 <?php
3 if (!defined('MOODLE_INTERNAL')) {
4 die('Direct access to this script is forbidden.'); /// It must be included from a Moodle page
7 require_once($CFG->dirroot.'/lib/formslib.php');
8 require_once($CFG->dirroot.'/cohort/lib.php');
10 /// get url variables
11 class autogroup_form extends moodleform {
13 // Define the form
14 function definition() {
15 global $CFG, $COURSE;
17 $mform =& $this->_form;
19 $mform->addElement('header', 'autogroup', get_string('autocreategroups', 'group'));
21 $options = array(0=>get_string('all'));
22 $options += $this->_customdata['roles'];
23 $mform->addElement('select', 'roleid', get_string('selectfromrole', 'group'), $options);
25 $student = get_archetype_roles('student');
26 $student = reset($student);
28 if ($student and array_key_exists($student->id, $options)) {
29 $mform->setDefault('roleid', $student->id);
32 $context = get_context_instance(CONTEXT_COURSE, $COURSE->id);
33 if (has_capability('moodle/cohort:view', $context)) {
34 $options = cohort_get_visible_list($COURSE);
35 if ($options) {
36 $options = array(0=>get_string('anycohort', 'cohort')) + $options;
37 $mform->addElement('select', 'cohortid', get_string('selectfromcohort', 'cohort'), $options);
38 $mform->setDefault('cohortid', '0');
39 } else {
40 $mform->addElement('hidden','cohortid');
41 $mform->setType('cohortid', PARAM_INT);
42 $mform->setConstant('cohortid', '0');
44 } else {
45 $mform->addElement('hidden','cohortid');
46 $mform->setType('cohortid', PARAM_INT);
47 $mform->setConstant('cohortid', '0');
50 $options = array('groups' => get_string('numgroups', 'group'),
51 'members' => get_string('nummembers', 'group'));
52 $mform->addElement('select', 'groupby', get_string('groupby', 'group'), $options);
54 $mform->addElement('text', 'number', get_string('number', 'group'),'maxlength="4" size="4"');
55 $mform->setType('number', PARAM_INT);
56 $mform->addRule('number', null, 'numeric', null, 'client');
57 $mform->addRule('number', get_string('required'), 'required', null, 'client');
59 $mform->addElement('checkbox', 'nosmallgroups', get_string('nosmallgroups', 'group'));
60 $mform->disabledIf('nosmallgroups', 'groupby', 'noteq', 'members');
61 $mform->setAdvanced('nosmallgroups');
63 $options = array('no' => get_string('noallocation', 'group'),
64 'random' => get_string('random', 'group'),
65 'firstname' => get_string('byfirstname', 'group'),
66 'lastname' => get_string('bylastname', 'group'),
67 'idnumber' => get_string('byidnumber', 'group'));
68 $mform->addElement('select', 'allocateby', get_string('allocateby', 'group'), $options);
69 $mform->setDefault('allocateby', 'random');
70 $mform->setAdvanced('allocateby');
72 $mform->addElement('text', 'namingscheme', get_string('namingscheme', 'group'));
73 $mform->addHelpButton('namingscheme', 'namingscheme', 'group');
74 $mform->addRule('namingscheme', get_string('required'), 'required', null, 'client');
75 $mform->setType('namingscheme', PARAM_MULTILANG);
76 // there must not be duplicate group names in course
77 $template = get_string('grouptemplate', 'group');
78 $gname = groups_parse_name($template, 0);
79 if (!groups_get_group_by_name($COURSE->id, $gname)) {
80 $mform->setDefault('namingscheme', $template);
83 $options = array('0' => get_string('no'),
84 '-1'=> get_string('newgrouping', 'group'));
85 if ($groupings = groups_get_all_groupings($COURSE->id)) {
86 foreach ($groupings as $grouping) {
87 $options[$grouping->id] = strip_tags(format_string($grouping->name));
90 $mform->addElement('select', 'grouping', get_string('createingrouping', 'group'), $options);
91 if ($groupings) {
92 $mform->setDefault('grouping', '-1');
95 $mform->addElement('text', 'groupingname', get_string('groupingname', 'group'), $options);
96 $mform->setType('groupingname', PARAM_MULTILANG);
97 $mform->disabledIf('groupingname', 'grouping', 'noteq', '-1');
99 $mform->addElement('hidden','courseid');
100 $mform->setType('courseid', PARAM_INT);
102 $mform->addElement('hidden','seed');
103 $mform->setType('seed', PARAM_INT);
105 $buttonarray = array();
106 $buttonarray[] = &$mform->createElement('submit', 'preview', get_string('preview'), 'xx');
107 $buttonarray[] = &$mform->createElement('submit', 'submitbutton', get_string('submit'));
108 $buttonarray[] = &$mform->createElement('cancel');
109 $mform->addGroup($buttonarray, 'buttonar', '', array(' '), false);
110 $mform->closeHeaderBefore('buttonar');
114 function validation($data, $files) {
115 global $CFG, $COURSE;
116 $errors = parent::validation($data, $files);
118 if ($data['allocateby'] != 'no') {
119 if (!$users = groups_get_potential_members($data['courseid'], $data['roleid'], $data['cohortid'])) {
120 $errors['roleid'] = get_string('nousersinrole', 'group');
123 /// Check the number entered is sane
124 if ($data['groupby'] == 'groups') {
125 $usercnt = count($users);
127 if ($data['number'] > $usercnt || $data['number'] < 1) {
128 $errors['number'] = get_string('toomanygroups', 'group', $usercnt);
133 //try to detect group name duplicates
134 $name = groups_parse_name(trim($data['namingscheme']), 0);
135 if (groups_get_group_by_name($COURSE->id, $name)) {
136 $errors['namingscheme'] = get_string('groupnameexists', 'group', $name);
139 // check grouping name duplicates
140 if ( isset($data['grouping']) && $data['grouping'] == '-1') {
141 $name = trim($data['groupingname']);
142 if (empty($name)) {
143 $errors['groupingname'] = get_string('required');
144 } else if (groups_get_grouping_by_name($COURSE->id, $name)) {
145 $errors['groupingname'] = get_string('groupingnameexists', 'group', $name);
149 /// Check the naming scheme
150 if ($data['groupby'] == 'groups' and $data['number'] == 1) {
151 // we can use the name as is because there will be only one group max
152 } else {
153 $matchcnt = preg_match_all('/[#@]{1,1}/', $data['namingscheme'], $matches);
154 if ($matchcnt != 1) {
155 $errors['namingscheme'] = get_string('badnamingscheme', 'group');
159 return $errors;