/** * Delete a specified group, first removing members and links with courses and groupings. * @param int $groupid The group to delete * @return boolean True if deletion was successful, false otherwise */ function groups_db_delete_group($groupid) { if (!$groupid) { $success = false; } else { $success = true; // Get a list of users for the group and remove them all. $userids = groups_db_get_members($groupid); if ($userids != false) { foreach ($userids as $userid) { $userdeleted = groups_db_remove_member($userid, $groupid); if (!$userdeleted) { $success = false; } } } // Remove any links with groupings to which the group belongs. //TODO: dbgroupinglib also seems to delete these links - duplication? $groupingids = groups_get_groupings_for_group($groupid); if ($groupingids != false) { foreach ($groupingids as $groupingid) { $groupremoved = groups_remove_group_from_grouping($groupid, $groupingid); if (!$groupremoved) { $success = false; } } } // Remove links with courses. $results = delete_records('groups_courses_groups', 'groupid', $groupid); if ($results == false) { $success = false; } // Delete the group itself $results = delete_records($table = 'groups', $field1 = 'id', $value1 = $groupid); // delete_records returns an array of the results from the sql call, // not a boolean, so we have to set our return variable if ($results == false) { $success = false; } } return $success; }
// First check if this group name doesn't already exist 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')) {
function test_remove_group_from_grouping() { $this->assertTrue(groups_remove_group_from_grouping($this->groupid, $this->groupingid)); $this->assertFalse(groups_belongs_to_grouping($this->groupid, $this->groupingid)); }