function test_add_group_to_grouping() { $this->assertTrue(groups_add_group_to_grouping($this->groupid, $this->groupingid)); $this->assertTrue(groups_belongs_to_grouping($this->groupid, $this->groupingid)); $this->assertTrue($groupings = groups_get_groupings_for_group($this->groupid)); //array... $this->assertTrue($groups = groups_get_groups_in_grouping($this->groupingid)); //... }
/** * Distributes students into groups randomly and creates a grouping with those * groups. * * You need to call groups_seed_random_number_generator() at some point in your * script before calling this function. * * Note that this function does not distribute teachers into groups - this still * needs to be done manually. * * @param int $courseid The id of the course that the grouping should belong to * @param int $nostudentspergroup The number of students to put in each group - * this can be set to false if you prefer to specify the number of groups * instead * @param int $nogroups The number of groups - this can be set to false if you * prefer to specify the number of student in each group. If both are specified * then $nostudentspergroup takes precedence. If neither is * specified then the function does nothing and returns false. * @param boolean $distribevenly If $noofstudentspergroup is specified, then * if this is set to true, any leftover students are distributed evenly among * the groups, whereas if it is set to false then they are put in a separate * group. * @param object $groupsettings The default settings to give each group. * This should contain prefix and defaultgroupdescription fields. The groups * are named with the prefix followed by 1, 2, etc. and given the * default group description set. * @param int $groupid If this is not set to false, then only students in the * specified group are distributed into groups, not all the students enrolled on * the course. * @param boolean $alphabetical If this is set to true, then the students are * not distributed randomly but in alphabetical order of last name. * @return int The id of the grouping */ function groups_create_automatic_grouping($courseid, $nostudentspergroup, $nogroups, $distribevenly, $groupingsettings, $groupid = false, $alphabetical = false) { if (!$nostudentspergroup and !$noteacherspergroup and !$nogroups) { $groupingid = false; } else { // Set $userids to the list of students that we want to put into groups // in the grouping if (!$groupid) { $users = get_course_students($courseid); $userids = groups_users_to_userids($users); } else { $userids = groups_get_members($groupid); } // Distribute the users into sets according to the parameters specified $userarrays = groups_distribute_in_random_sets($userids, $nostudentspergroup, $nogroups, $distribevenly, !$alphabetical); if (!$userarrays) { $groupingid = false; } else { // Create the grouping that the groups we create will go into $groupingid = groups_create_grouping($courseid, $groupingsettings); // Get the prefix for the names of each group and default group // description to give each group if (!$groupingsettings->prefix) { $prefix = get_string('defaultgroupprefix', 'groups'); } else { $prefix = $groupingsettings->prefix; } if (!$groupingsettings->defaultgroupdescription) { $defaultgroupdescription = ''; } else { $defaultgroupdescription = $groupingsettings->defaultgroupdescription; } // Now create a group for each set of students, add the group to the // grouping and then add the students $i = 1; foreach ($userarrays as $userids) { $groupsettings->name = $prefix . ' ' . $i; $groupsettings->description = $defaultgroupdescription; $i++; $groupid = groups_create_group($courseid, $groupsettings); $groupadded = groups_add_group_to_grouping($groupid, $groupingid); if (!$groupid or !$groupadded) { $groupingid = false; } else { if ($userids) { foreach ($userids as $userid) { $usersadded = groups_add_member($groupid, $userid); // If unsuccessful just carry on I guess } } } } } } return $groupingid; }
if (groups_group_name_exists($courseid, $data->name)) { $error = get_string('groupnameexists', 'group', $data->name); $success = false; } elseif (!($id = groups_create_group($course->id, $data))) { print_error('erroreditgroup'); } else { $success = (bool) $id; $data->id = $id; if ($groupingid) { $success = $success && groups_add_group_to_grouping($id, $groupingid); } } } elseif ($groupingid != $newgrouping) { // Moving group to new grouping $success = $success && groups_remove_group_from_grouping($id, $groupingid); $success = $success && groups_add_group_to_grouping($id, $newgrouping); } else { // Updating group $group = groups_get_group($data->id); if (groups_group_name_exists($courseid, $data->name) && $group->name != $data->name) { $error = get_string('groupnameexists', 'group', $data->name); $success = false; } elseif (!groups_update_group($data, $course->id)) { print_error('groupnotupdated'); } } // Handle file upload if ($success) { require_once "{$CFG->libdir}/gdlib.php"; if (save_profile_image($id, $editform->_upload_manager, 'groups')) { $data->picture = 1;
function restore_create_groupings_groups($grouping_id, $info, $restore) { global $CFG; $status = true; //Get the members array $members = $info['GROUPING']['#']['GROUPS']['0']['#']['GROUP']; //Iterate over members for ($i = 0; $i < sizeof($members); $i++) { $mem_info = $members[$i]; //Now, build the GROUPINGS_GROUPS record structure $gro_member = new Object(); $gro_member->groupingid = $grouping_id; $gro_member->groupid = backup_todb($mem_info['#']['GROUPID']['0']['#']); $gro_member->timeadded = backup_todb($mem_info['#']['TIMEADDED']['0']['#']); //We have to recode the userid field ///$user = backup_getid($restore->backup_unique_code,"user",$group_member->userid); $group = backup_getid($restore->backup_unique_code, "group", $gro_member->groupid); if ($group) { $gro_member->groupid = $group->new_id; } //The structure is equal to the db, so link the groups to the groupings. TODO: RESTORE. $newid = groups_add_group_to_grouping($gro_member->groupid, $gro_member->groupingid); //Do some output if (($i + 1) % 50 == 0) { if (!defined('RESTORE_SILENTLY')) { echo "."; if (($i + 1) % 1000 == 0) { echo "<br />"; } } backup_flush(300); } if (!$newid) { $status = false; } } return $status; }