|
Moodle
2.2.1
http://www.collinsharper.com
|
00001 <?php 00002 00003 if (!defined('MOODLE_INTERNAL')) { 00004 die('Direct access to this script is forbidden.'); 00005 } 00006 00007 require_once($CFG->dirroot.'/lib/formslib.php'); 00008 require_once($CFG->dirroot.'/cohort/lib.php'); 00009 00011 class autogroup_form extends moodleform { 00012 00013 // Define the form 00014 function definition() { 00015 global $CFG, $COURSE; 00016 00017 $mform =& $this->_form; 00018 00019 $mform->addElement('header', 'autogroup', get_string('autocreategroups', 'group')); 00020 00021 $options = array(0=>get_string('all')); 00022 $options += $this->_customdata['roles']; 00023 $mform->addElement('select', 'roleid', get_string('selectfromrole', 'group'), $options); 00024 00025 $student = get_archetype_roles('student'); 00026 $student = reset($student); 00027 00028 if ($student and array_key_exists($student->id, $options)) { 00029 $mform->setDefault('roleid', $student->id); 00030 } 00031 00032 $context = get_context_instance(CONTEXT_COURSE, $COURSE->id); 00033 if (has_capability('moodle/cohort:view', $context)) { 00034 $options = cohort_get_visible_list($COURSE); 00035 if ($options) { 00036 $options = array(0=>get_string('anycohort', 'cohort')) + $options; 00037 $mform->addElement('select', 'cohortid', get_string('selectfromcohort', 'cohort'), $options); 00038 $mform->setDefault('cohortid', '0'); 00039 } else { 00040 $mform->addElement('hidden','cohortid'); 00041 $mform->setType('cohortid', PARAM_INT); 00042 $mform->setConstant('cohortid', '0'); 00043 } 00044 } else { 00045 $mform->addElement('hidden','cohortid'); 00046 $mform->setType('cohortid', PARAM_INT); 00047 $mform->setConstant('cohortid', '0'); 00048 } 00049 00050 $options = array('groups' => get_string('numgroups', 'group'), 00051 'members' => get_string('nummembers', 'group')); 00052 $mform->addElement('select', 'groupby', get_string('groupby', 'group'), $options); 00053 00054 $mform->addElement('text', 'number', get_string('number', 'group'),'maxlength="4" size="4"'); 00055 $mform->setType('number', PARAM_INT); 00056 $mform->addRule('number', null, 'numeric', null, 'client'); 00057 $mform->addRule('number', get_string('required'), 'required', null, 'client'); 00058 00059 $mform->addElement('checkbox', 'nosmallgroups', get_string('nosmallgroups', 'group')); 00060 $mform->disabledIf('nosmallgroups', 'groupby', 'noteq', 'members'); 00061 $mform->setAdvanced('nosmallgroups'); 00062 00063 $options = array('no' => get_string('noallocation', 'group'), 00064 'random' => get_string('random', 'group'), 00065 'firstname' => get_string('byfirstname', 'group'), 00066 'lastname' => get_string('bylastname', 'group'), 00067 'idnumber' => get_string('byidnumber', 'group')); 00068 $mform->addElement('select', 'allocateby', get_string('allocateby', 'group'), $options); 00069 $mform->setDefault('allocateby', 'random'); 00070 $mform->setAdvanced('allocateby'); 00071 00072 $mform->addElement('text', 'namingscheme', get_string('namingscheme', 'group')); 00073 $mform->addHelpButton('namingscheme', 'namingscheme', 'group'); 00074 $mform->addRule('namingscheme', get_string('required'), 'required', null, 'client'); 00075 $mform->setType('namingscheme', PARAM_MULTILANG); 00076 // there must not be duplicate group names in course 00077 $template = get_string('grouptemplate', 'group'); 00078 $gname = groups_parse_name($template, 0); 00079 if (!groups_get_group_by_name($COURSE->id, $gname)) { 00080 $mform->setDefault('namingscheme', $template); 00081 } 00082 00083 $options = array('0' => get_string('no'), 00084 '-1'=> get_string('newgrouping', 'group')); 00085 if ($groupings = groups_get_all_groupings($COURSE->id)) { 00086 foreach ($groupings as $grouping) { 00087 $options[$grouping->id] = strip_tags(format_string($grouping->name)); 00088 } 00089 } 00090 $mform->addElement('select', 'grouping', get_string('createingrouping', 'group'), $options); 00091 if ($groupings) { 00092 $mform->setDefault('grouping', '-1'); 00093 } 00094 00095 $mform->addElement('text', 'groupingname', get_string('groupingname', 'group'), $options); 00096 $mform->setType('groupingname', PARAM_MULTILANG); 00097 $mform->disabledIf('groupingname', 'grouping', 'noteq', '-1'); 00098 00099 $mform->addElement('hidden','courseid'); 00100 $mform->setType('courseid', PARAM_INT); 00101 00102 $mform->addElement('hidden','seed'); 00103 $mform->setType('seed', PARAM_INT); 00104 00105 $buttonarray = array(); 00106 $buttonarray[] = &$mform->createElement('submit', 'preview', get_string('preview'), 'xx'); 00107 $buttonarray[] = &$mform->createElement('submit', 'submitbutton', get_string('submit')); 00108 $buttonarray[] = &$mform->createElement('cancel'); 00109 $mform->addGroup($buttonarray, 'buttonar', '', array(' '), false); 00110 $mform->closeHeaderBefore('buttonar'); 00111 } 00112 00113 00114 function validation($data, $files) { 00115 global $CFG, $COURSE; 00116 $errors = parent::validation($data, $files); 00117 00118 if ($data['allocateby'] != 'no') { 00119 if (!$users = groups_get_potential_members($data['courseid'], $data['roleid'], $data['cohortid'])) { 00120 $errors['roleid'] = get_string('nousersinrole', 'group'); 00121 } 00122 00124 if ($data['groupby'] == 'groups') { 00125 $usercnt = count($users); 00126 00127 if ($data['number'] > $usercnt || $data['number'] < 1) { 00128 $errors['number'] = get_string('toomanygroups', 'group', $usercnt); 00129 } 00130 } 00131 } 00132 00133 //try to detect group name duplicates 00134 $name = groups_parse_name(trim($data['namingscheme']), 0); 00135 if (groups_get_group_by_name($COURSE->id, $name)) { 00136 $errors['namingscheme'] = get_string('groupnameexists', 'group', $name); 00137 } 00138 00139 // check grouping name duplicates 00140 if ( isset($data['grouping']) && $data['grouping'] == '-1') { 00141 $name = trim($data['groupingname']); 00142 if (empty($name)) { 00143 $errors['groupingname'] = get_string('required'); 00144 } else if (groups_get_grouping_by_name($COURSE->id, $name)) { 00145 $errors['groupingname'] = get_string('groupingnameexists', 'group', $name); 00146 } 00147 } 00148 00150 if ($data['groupby'] == 'groups' and $data['number'] == 1) { 00151 // we can use the name as is because there will be only one group max 00152 } else { 00153 $matchcnt = preg_match_all('/[#@]{1,1}/', $data['namingscheme'], $matches); 00154 if ($matchcnt != 1) { 00155 $errors['namingscheme'] = get_string('badnamingscheme', 'group'); 00156 } 00157 } 00158 00159 return $errors; 00160 } 00161 }