In addition to the enrollment key for a group "auto"
grouping a student, we have written code to "auto" group students into groups without an enrollment key.
We added a field to allow for selecting during setup of a course whether to turn this on or off.
Here are the directions if anyone else is interested
GROUP AUTO-ASSIGNMENT:
THIS FILE WILL ASSIST IN SETTING UP OF
AUTO-ASSIGNMENT OF STUDENTS INTO GROUPS UPON REGISTRATION.
DB TABLES CHANGED:
mdl_course: added new field groupautoassign
FILES CHANGED:
moodle/enrol/manual/enrol.php
moodle/course/edit_form.php
moodle/lang/en-utf8/moodle.php
NEW FILES:
moodle/lang/en-utf8/help/groupautoassign.html
--------------------------------------------------------------------------------------------------------
1.) Update mdl_course table & add a field after groupmodeforce named:
'groupautoassign'
ALTER TABLE `mdl_course` ADD `groupautoassign` SMALLINT( 4 ) UNSIGNED NOT NULL DEFAULT '0' COMMENT 'Added for group Auto-Assignment' AFTER `groupmodeforce`
--------------------------------------------------------------------------------------------------------
2.) Update moodle/enrol/manual/enrol.php around Line # 72 after:
print_footer();
exit;
} else if (!empty($_GET['confirm'])) {
ADD:
//ADDED NEXT 12 LINES FOR GROUP AUTO-ASSIGNMENT
//Check to see if a group id has already been assigned before auto-assignment procedure
if ($groupid == false) {
if ($course->groupautoassign) {
$groupid = $this->check_group_auto_entry ($course->id);
if ($groupid == false) {
//Set up groups full error string
error(get_string('enrolcoursefull','enrolmax',$group));
}
}
}
//END GROUP AUTO-ASSIGNMENT
-------
ALSO around Line # 83 REPLACE:
if (!enrol_into_course($course, $USER, 'manual')) {
print_error('couldnotassignrole');
}
WITH:
//BEGIN ADDITION FOR AUTO-ASSIGNMENT INTO GROUPS
if (enrol_into_course($course, $USER, 'manual')) {
if ($groupid !== false) {
if (!add_user_to_group($groupid, $USER->id)) {
print_error('couldnotassigngroup');
}
}
} else {
print_error('couldnotassignrole');
}
//END ADDITION FOR AUTO-ASSIGNMENT INTO GROUPS
--------
ALSO around line # 142 after:
$groupid = $this->check_group_entry($course->id, $form->password);
ADD:
//ADDED NEXT 12 LINES FOR GROUP AUTO-ASSIGNMENT
//Check to see if a group id has already been assigned before auto-assignment procedure
if ($groupid == false) {
if ($course->groupautoassign) {
$groupid = $this->check_group_auto_entry ($course->id);
if ($groupid == false) {
//Set up groups full error string
error(get_string('enrolcoursefull','enrolmax',$group));
}
}
}
//END GROUP AUTO-ASSIGNMENT
-------------------------------------------------------------------------------------------------------
3.) Added new function to moodle/enrol/manual/enrol.php to handle assignment of group id to assign around Line # 184 after:
redirect($destination);
} else {
$this->errormsg = get_string('enrolmentkeyhint', '', substr($course->password,0,1));
}
}
ADD:
//BEGIN ADDITION FOR GROUP AUTO-ASSIGNMENT
/**
* Auto Assign students into groups for the given course id
*
* @param courseid the current course id
*/
function check_group_auto_entry ($courseid) {
//Set ingroup to false...No group assigned
$ingroup = false;
//If groups exist in the course id passed then...
if (($groups = get_groups($courseid)) !== false) {
//Spin through each of the groups looking for first available group to assign student to
foreach ($groups as $group) {
//Get the maximum # of students to allow in the group
$maxenrolment=get_field('groups','enrolmax','id',$group->id);
//Get the total # of available seats left in the current group
$availableenrolments=$maxenrolment - count_course_students_bycourseid($courseid,'','','',$group->id,'');
//If students are allowed in group and there are available seats then...
if (($maxenrolment>0) and ($availableenrolments>0)) {
//Assign the current group id to ingroup...this is the group student will be placed into
$ingroup = $group->id;
//Stop the loop and return the group assignment back to the caller
break;
}
}
}
//Pass back the group id the student will be assigned to IF one was returned
return $ingroup;
}
//END ADDITION FOR GROUP AUTO-ASSIGNMENT
------------------------------------------------------------------------------------------------
4.) In moodle/course/edit_form.php around Line # 312 after:
ADD:
//BEGIN ADDITION FOR GROUP AUTO-ASSIGNMENT
$choices = array();
$choices['0'] = get_string('no');
$choices['1'] = get_string('yes');
$mform->addElement('select', 'groupautoassign', get_string('autoassigngroup'), $choices);
$mform->setHelpButton('groupautoassign', array('groupautoassign', get_string('autoassigngroup')), true);
$mform->setDefault('groupautoassign', 0);
//END ADDITION FOR GROUP AUTO-ASSIGNMENT
-------------------------------------------------------------------------------------------------
5.) In moodle/lang/en-utf8/moodle.php around Line # 127 after:
$string['authenticateduserdescription'] = 'All logged in users.';
ADD:
//BEGIN ADDITION FOR GROUP AUTO-ASSIGNMENT
$string['autoassigngroup'] = 'Auto-Assign Groups';
//END ADDITION FOR GROUP AUTO-ASSIGNMENT
-------------------------------------------------------------------------------------------------
6.) Add new file named "groupautoassign.html" in the moodle/lang/en-utf8/help directory:
<h1>Auto-assign Groups</h1>
<p>The group auto-assignment can either be:</p>
<ul>
<li>No - Do not auto-assign students into the groups upon course registration</li>
<li>Yes - Auto-assign students into the groups upon course registration</li>
</ul>
<p>The group auto-assign feature ONLY works if groups exist for the course. Otherwise, this setting is ignored.</p>
--------------------------------------------------------------------------------------------------
7.) Update moodle/lang/en-utf8/help/index.html under Setting Up courses:
ADD:
<li><a href="help.php?file=groupautoassign.html">Auto-Assign Groups</a></li>