2009-11-01 12:00:47 +00:00
|
|
|
<?php
|
2012-01-05 09:29:55 +08:00
|
|
|
// This file is part of Moodle - http://moodle.org/
|
|
|
|
//
|
|
|
|
// Moodle is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
//
|
|
|
|
// Moodle is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU General Public License
|
|
|
|
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Auto group form
|
|
|
|
*
|
|
|
|
* @package core_group
|
|
|
|
* @copyright 2007 mattc-catalyst (http://moodle.com)
|
|
|
|
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
|
*/
|
2010-05-13 02:02:05 +00:00
|
|
|
if (!defined('MOODLE_INTERNAL')) {
|
|
|
|
die('Direct access to this script is forbidden.'); /// It must be included from a Moodle page
|
|
|
|
}
|
|
|
|
|
2007-09-24 21:55:15 +00:00
|
|
|
require_once($CFG->dirroot.'/lib/formslib.php');
|
2010-04-23 09:15:55 +00:00
|
|
|
require_once($CFG->dirroot.'/cohort/lib.php');
|
2007-09-24 21:55:15 +00:00
|
|
|
|
2012-01-05 09:29:55 +08:00
|
|
|
/**
|
|
|
|
* Auto group form class
|
|
|
|
*
|
|
|
|
* @package core_group
|
|
|
|
* @copyright 2007 mattc-catalyst (http://moodle.com)
|
|
|
|
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
|
*/
|
2007-09-24 21:55:15 +00:00
|
|
|
class autogroup_form extends moodleform {
|
|
|
|
|
2012-01-05 09:29:55 +08:00
|
|
|
/**
|
|
|
|
* Form Definition
|
|
|
|
*/
|
2007-09-24 21:55:15 +00:00
|
|
|
function definition() {
|
|
|
|
global $CFG, $COURSE;
|
|
|
|
|
|
|
|
$mform =& $this->_form;
|
|
|
|
|
|
|
|
$mform->addElement('header', 'autogroup', get_string('autocreategroups', 'group'));
|
|
|
|
|
2007-11-19 20:31:57 +00:00
|
|
|
$options = array(0=>get_string('all'));
|
2007-09-24 21:55:15 +00:00
|
|
|
$options += $this->_customdata['roles'];
|
|
|
|
$mform->addElement('select', 'roleid', get_string('selectfromrole', 'group'), $options);
|
MDL-21782 reworked enrolment framework, the core infrastructure is in place, the basic plugins are all implemented; see the tracker issue for list of unfinished bits, expect more changes and improvements during the next week
AMOS START
MOV [sendcoursewelcomemessage,core_admin],[sendcoursewelcomemessage,enrol_self]
MOV [configsendcoursewelcomemessage,core_admin],[sendcoursewelcomemessage_desc,enrol_self]
MOV [enrolstartdate,core],[enrolstartdate,enrol_self]
MOV [enrolenddate,core],[enrolenddate,enrol_self]
CPY [welcometocourse,core],[welcometocourse,enrol_self]
CPY [welcometocoursetext,core],[welcometocoursetext,enrol_self]
MOV [notenrollable,core],[notenrollable,core_enrol]
MOV [enrolenddaterror,core],[enrolenddaterror,enrol_self]
MOV [enrolmentkeyhint,core],[passwordinvalidhint,enrol_self]
MOV [coursemanager,core_admin],[coursecontact,core_admin]
MOV [configcoursemanager,core_admin],[coursecontact_desc,core_admin]
MOV [enrolledincourserole,core],[enrolledincourserole,enrol_manual]
MOV [enrolme,core],[enrolme,core_enrol]
MOV [unenrol,core],[unenrol,core_enrol]
MOV [unenrolme,core],[unenrolme,core_enrol]
MOV [enrolmentnew,core],[enrolmentnew,core_enrol]
MOV [enrolmentnewuser,core],[enrolmentnewuser,core_enrol]
MOV [enrolments,core],[enrolments,core_enrol]
MOV [enrolperiod,core],[enrolperiod,core_enrol]
MOV [unenrolroleusers,core],[unenrolroleusers,core_enrol]
AMOS END
2010-06-21 15:30:49 +00:00
|
|
|
|
|
|
|
$student = get_archetype_roles('student');
|
|
|
|
$student = reset($student);
|
|
|
|
|
|
|
|
if ($student and array_key_exists($student->id, $options)) {
|
|
|
|
$mform->setDefault('roleid', $student->id);
|
2007-11-19 20:31:57 +00:00
|
|
|
}
|
2007-09-24 21:55:15 +00:00
|
|
|
|
2010-04-23 09:16:53 +00:00
|
|
|
$context = get_context_instance(CONTEXT_COURSE, $COURSE->id);
|
|
|
|
if (has_capability('moodle/cohort:view', $context)) {
|
|
|
|
$options = cohort_get_visible_list($COURSE);
|
|
|
|
if ($options) {
|
|
|
|
$options = array(0=>get_string('anycohort', 'cohort')) + $options;
|
|
|
|
$mform->addElement('select', 'cohortid', get_string('selectfromcohort', 'cohort'), $options);
|
|
|
|
$mform->setDefault('cohortid', '0');
|
|
|
|
} else {
|
|
|
|
$mform->addElement('hidden','cohortid');
|
|
|
|
$mform->setType('cohortid', PARAM_INT);
|
|
|
|
$mform->setConstant('cohortid', '0');
|
|
|
|
}
|
2010-04-23 09:15:55 +00:00
|
|
|
} else {
|
|
|
|
$mform->addElement('hidden','cohortid');
|
|
|
|
$mform->setType('cohortid', PARAM_INT);
|
2010-04-23 09:16:53 +00:00
|
|
|
$mform->setConstant('cohortid', '0');
|
2010-04-23 09:15:55 +00:00
|
|
|
}
|
|
|
|
|
2007-11-19 20:31:57 +00:00
|
|
|
$options = array('groups' => get_string('numgroups', 'group'),
|
|
|
|
'members' => get_string('nummembers', 'group'));
|
2007-09-24 21:55:15 +00:00
|
|
|
$mform->addElement('select', 'groupby', get_string('groupby', 'group'), $options);
|
2007-11-19 20:31:57 +00:00
|
|
|
|
2007-09-24 21:55:15 +00:00
|
|
|
$mform->addElement('text', 'number', get_string('number', 'group'),'maxlength="4" size="4"');
|
|
|
|
$mform->setType('number', PARAM_INT);
|
|
|
|
$mform->addRule('number', null, 'numeric', null, 'client');
|
|
|
|
$mform->addRule('number', get_string('required'), 'required', null, 'client');
|
2007-11-19 20:31:57 +00:00
|
|
|
|
|
|
|
$mform->addElement('checkbox', 'nosmallgroups', get_string('nosmallgroups', 'group'));
|
|
|
|
$mform->disabledIf('nosmallgroups', 'groupby', 'noteq', 'members');
|
|
|
|
$mform->setAdvanced('nosmallgroups');
|
|
|
|
|
|
|
|
$options = array('no' => get_string('noallocation', 'group'),
|
|
|
|
'random' => get_string('random', 'group'),
|
|
|
|
'firstname' => get_string('byfirstname', 'group'),
|
|
|
|
'lastname' => get_string('bylastname', 'group'),
|
|
|
|
'idnumber' => get_string('byidnumber', 'group'));
|
2007-09-24 21:55:15 +00:00
|
|
|
$mform->addElement('select', 'allocateby', get_string('allocateby', 'group'), $options);
|
2007-11-19 20:31:57 +00:00
|
|
|
$mform->setDefault('allocateby', 'random');
|
|
|
|
$mform->setAdvanced('allocateby');
|
|
|
|
|
|
|
|
$mform->addElement('text', 'namingscheme', get_string('namingscheme', 'group'));
|
2010-07-13 15:36:19 +00:00
|
|
|
$mform->addHelpButton('namingscheme', 'namingscheme', 'group');
|
2007-11-19 20:31:57 +00:00
|
|
|
$mform->addRule('namingscheme', get_string('required'), 'required', null, 'client');
|
2012-05-11 15:50:09 +08:00
|
|
|
$mform->setType('namingscheme', PARAM_TEXT);
|
2007-11-19 20:31:57 +00:00
|
|
|
// there must not be duplicate group names in course
|
|
|
|
$template = get_string('grouptemplate', 'group');
|
|
|
|
$gname = groups_parse_name($template, 0);
|
|
|
|
if (!groups_get_group_by_name($COURSE->id, $gname)) {
|
|
|
|
$mform->setDefault('namingscheme', $template);
|
|
|
|
}
|
|
|
|
|
2010-04-07 07:37:12 +00:00
|
|
|
$options = array('0' => get_string('no'),
|
|
|
|
'-1'=> get_string('newgrouping', 'group'));
|
|
|
|
if ($groupings = groups_get_all_groupings($COURSE->id)) {
|
|
|
|
foreach ($groupings as $grouping) {
|
|
|
|
$options[$grouping->id] = strip_tags(format_string($grouping->name));
|
2007-11-19 20:31:57 +00:00
|
|
|
}
|
|
|
|
}
|
2010-04-07 07:37:12 +00:00
|
|
|
$mform->addElement('select', 'grouping', get_string('createingrouping', 'group'), $options);
|
|
|
|
if ($groupings) {
|
|
|
|
$mform->setDefault('grouping', '-1');
|
|
|
|
}
|
|
|
|
|
|
|
|
$mform->addElement('text', 'groupingname', get_string('groupingname', 'group'), $options);
|
2012-05-11 15:50:09 +08:00
|
|
|
$mform->setType('groupingname', PARAM_TEXT);
|
2010-04-07 07:37:12 +00:00
|
|
|
$mform->disabledIf('groupingname', 'grouping', 'noteq', '-1');
|
2007-09-24 21:55:15 +00:00
|
|
|
|
|
|
|
$mform->addElement('hidden','courseid');
|
|
|
|
$mform->setType('courseid', PARAM_INT);
|
2007-11-19 20:31:57 +00:00
|
|
|
|
2007-09-24 21:55:15 +00:00
|
|
|
$mform->addElement('hidden','seed');
|
|
|
|
$mform->setType('seed', PARAM_INT);
|
2007-11-19 20:31:57 +00:00
|
|
|
|
|
|
|
$buttonarray = array();
|
2007-09-24 21:55:15 +00:00
|
|
|
$buttonarray[] = &$mform->createElement('submit', 'preview', get_string('preview'), 'xx');
|
|
|
|
$buttonarray[] = &$mform->createElement('submit', 'submitbutton', get_string('submit'));
|
|
|
|
$buttonarray[] = &$mform->createElement('cancel');
|
|
|
|
$mform->addGroup($buttonarray, 'buttonar', '', array(' '), false);
|
|
|
|
$mform->closeHeaderBefore('buttonar');
|
|
|
|
}
|
|
|
|
|
2012-01-05 09:29:55 +08:00
|
|
|
/**
|
|
|
|
* Performs validation of the form information
|
|
|
|
*
|
|
|
|
* @param array $data
|
|
|
|
* @param array $files
|
|
|
|
* @return array $errors An array of $errors
|
|
|
|
*/
|
2007-11-19 20:31:57 +00:00
|
|
|
function validation($data, $files) {
|
2009-12-16 22:14:17 +00:00
|
|
|
global $CFG, $COURSE;
|
2007-11-23 22:15:07 +00:00
|
|
|
$errors = parent::validation($data, $files);
|
2007-09-24 21:55:15 +00:00
|
|
|
|
2007-11-19 20:31:57 +00:00
|
|
|
if ($data['allocateby'] != 'no') {
|
2010-04-23 09:15:55 +00:00
|
|
|
if (!$users = groups_get_potential_members($data['courseid'], $data['roleid'], $data['cohortid'])) {
|
2007-11-19 20:31:57 +00:00
|
|
|
$errors['roleid'] = get_string('nousersinrole', 'group');
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Check the number entered is sane
|
|
|
|
if ($data['groupby'] == 'groups') {
|
|
|
|
$usercnt = count($users);
|
|
|
|
|
|
|
|
if ($data['number'] > $usercnt || $data['number'] < 1) {
|
2009-12-16 22:14:17 +00:00
|
|
|
$errors['number'] = get_string('toomanygroups', 'group', $usercnt);
|
2007-11-19 20:31:57 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//try to detect group name duplicates
|
2008-06-01 17:53:25 +00:00
|
|
|
$name = groups_parse_name(trim($data['namingscheme']), 0);
|
2007-11-19 20:31:57 +00:00
|
|
|
if (groups_get_group_by_name($COURSE->id, $name)) {
|
|
|
|
$errors['namingscheme'] = get_string('groupnameexists', 'group', $name);
|
2007-09-24 21:55:15 +00:00
|
|
|
}
|
|
|
|
|
2007-11-19 20:31:57 +00:00
|
|
|
// check grouping name duplicates
|
2007-12-20 16:11:10 +00:00
|
|
|
if ( isset($data['grouping']) && $data['grouping'] == '-1') {
|
2008-06-01 17:53:25 +00:00
|
|
|
$name = trim($data['groupingname']);
|
2007-11-19 20:31:57 +00:00
|
|
|
if (empty($name)) {
|
|
|
|
$errors['groupingname'] = get_string('required');
|
|
|
|
} else if (groups_get_grouping_by_name($COURSE->id, $name)) {
|
|
|
|
$errors['groupingname'] = get_string('groupingnameexists', 'group', $name);
|
2007-09-24 21:55:15 +00:00
|
|
|
}
|
|
|
|
}
|
2007-11-19 20:31:57 +00:00
|
|
|
|
|
|
|
/// Check the naming scheme
|
2010-07-13 15:29:49 +00:00
|
|
|
if ($data['groupby'] == 'groups' and $data['number'] == 1) {
|
|
|
|
// we can use the name as is because there will be only one group max
|
|
|
|
} else {
|
|
|
|
$matchcnt = preg_match_all('/[#@]{1,1}/', $data['namingscheme'], $matches);
|
|
|
|
if ($matchcnt != 1) {
|
|
|
|
$errors['namingscheme'] = get_string('badnamingscheme', 'group');
|
|
|
|
}
|
2007-09-24 21:55:15 +00:00
|
|
|
}
|
|
|
|
|
2007-11-19 20:31:57 +00:00
|
|
|
return $errors;
|
|
|
|
}
|
2007-09-24 21:55:15 +00:00
|
|
|
}
|