Index: lib/moodlelib.php
===================================================================
RCS file: /cvsroot/moodle/moodle/lib/moodlelib.php,v
retrieving revision 1.822
diff -u -r1.822 moodlelib.php
--- lib/moodlelib.php	19 Jan 2007 10:29:04 -0000	1.822
+++ lib/moodlelib.php	20 Jan 2007 10:56:13 -0000
@@ -2936,10 +2936,11 @@
         }
     }
 
-/// Delete any groups, removing members first. TODO: check.
+/// Delete any groups, removing members and grouping/course links first.
+    //TODO: If groups or groupings are to be shared between courses, think again!
     if ($groupids = groups_get_groups($course->id)) {
         foreach ($groupids as $groupid) {
-            if (groups_remove_all_group_members($groupid)) {
+            if (groups_remove_all_members($groupid)) {
                 if ($showfeedback) {
                     notify($strdeleted .' groups_members');
                 }
@@ -2947,7 +2948,7 @@
                 $result = false;
             }
             /// Delete any associated context for this group ??
-            delete_context(CONTEXT_GROUP, $group->id);
+            delete_context(CONTEXT_GROUP, $groupid);
             
             if (groups_delete_group($groupid)) {
                 if ($showfeedback) {
@@ -2958,6 +2959,11 @@
             }
         }
     }
+/// Delete any groupings.
+    $result = groups_delete_all_groupings($course->id);
+    if ($result && $showfeedback) {
+        notify($strdeleted .' groupings');
+    }
 
 /// Delete all related records in other tables that may have a courseid
 /// This array stores the tables that need to be cleared, as
Index: group/lib/basicgrouplib.php
===================================================================
RCS file: /cvsroot/moodle/moodle/group/lib/basicgrouplib.php,v
retrieving revision 1.7
diff -u -r1.7 basicgrouplib.php
--- group/lib/basicgrouplib.php	20 Jan 2007 09:28:26 -0000	1.7
+++ group/lib/basicgrouplib.php	20 Jan 2007 10:56:07 -0000
@@ -55,7 +55,8 @@
 }
 
 /**
- * Return member records, for backup.
+ * Get the user ID and time added for each member of a group, for backup4.
+ * @return array An array of member records.
  */
 function groups_get_member_records($groupid) {
     if (!$groupid) {
@@ -81,6 +82,10 @@
     return $groupids;
 }
 
+/**
+ * Get the groups to which a user belongs in any course on site.
+ * @return array | false An array of the group IDs, or false on error.
+ */
 function groups_get_all_groups_for_user($userid) {
     $groups = get_records('groups_members', 'userid', $userid);
     if (! $groups) {
@@ -358,7 +363,7 @@
     } else {
         $useradded = groups_db_add_member($member->groupid, $member->userid, $member->timeadded);
     }
-    return true;
+    return $useradded;
 }
 
 
@@ -368,7 +373,7 @@
 
 
 /**
- * Deletes a group best effort
+ * Delete a group best effort, 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
  * See comment above on web service autoupdating. 
@@ -379,20 +384,9 @@
     return $groupdeleted;
 }
 
-/*function groups_delete_groups($groupids) {
-    if (! $groupids) {
-        return false;
-    }
-    $success = true;
-    foreach ($groupids as $id) {
-        $success = $success && groups_db_delete_group($id);
-    }
-    return $success;
-}*/
-
 
 /**
- * Deletes the specified user from the specified group
+ * Deletes the link between the specified user and group.
  * @param int $groupid The group to delete the user from
  * @param int $userid The user to delete
  * @return boolean True if deletion was successful, false otherwise
@@ -406,6 +400,11 @@
     return $success;
 }
 
+/**
+ * Removes all users from the specified group.
+ * @param int $groupid The ID of the group.
+ * @return boolean True for success, false otherwise.
+ */
 function groups_remove_all_members($groupid) {
     if (! groups_group_exists($groupid)) {
         //Woops, delete group last!
@@ -423,5 +422,4 @@
     return $success;
 }
 
-
 ?>
\ No newline at end of file
Index: group/lib/groupinglib.php
===================================================================
RCS file: /cvsroot/moodle/moodle/group/lib/groupinglib.php,v
retrieving revision 1.7
diff -u -r1.7 groupinglib.php
--- group/lib/groupinglib.php	20 Jan 2007 09:28:26 -0000	1.7
+++ group/lib/groupinglib.php	20 Jan 2007 10:56:07 -0000
@@ -31,12 +31,6 @@
 
 
 function groups_get_grouping_records($courseid) {
-    /*$groupingids = groups_db_get_groupings($courseid);
-    if (! $groupingids) {
-        return false;
-    }
-    $groupings = groups_groupingids_to_groupings($groupingids);
-*/
     global $CFG;
     if (! $courseid) {
         return false;
@@ -50,7 +44,7 @@
 }
 
 /**
- * Gets a list of the groups in a specified grouping
+ * Gets a list of the group IDs in a specified grouping
  * @param int $groupingid The id of the grouping
  * @return array | false. An array of the ids of the groups, or false if there
  * are none or an error occurred.
@@ -59,6 +53,11 @@
     return groups_db_get_groups_in_grouping($groupingid);
 }
 
+/**
+ * Gets complete group-data for each group in a grouping.
+ * @param int $groupingid The ID of the grouping.
+ * @return array | false An array of group records, or false on error.
+ */
 function groups_get_groups_in_grouping_records($groupingid) {
     if (! $groupingid) {
         return false;
@@ -127,10 +126,7 @@
  */
 function groups_get_groups_not_in_any_grouping($courseid) {
     global $CFG;
-/*Was: $sql = "SELECT g.id FROM {$CFG->prefix}groups AS g
-        WHERE g.id NOT IN 
-        (SELECT groupid FROM {$CFG->prefix}groups_groupings_groups)";
-*/
+
     $join = '';
     $where= '';
     if ($courseid) {
@@ -146,14 +142,7 @@
 
     $records = get_records_sql($sql);
     $groupids = groups_groups_to_groupids($records, $courseid);
-    /*$groupids = array();
-    if ($records) {
-        foreach ($records as $r) {
-            $groupids[] = $r->id;
-        }
-    } else {
-        return false;
-    }*/
+
     return $groupids;
 }
 
@@ -418,7 +407,7 @@
  *****************************/
 
 /** 
- * Removes a specified group from a specified grouping. Note that this does 
+ * Removes a specified group from a grouping. Note that this does 
  * not delete the group. 
  * @param int $groupid The id of the group.
  * @param int $groupingid The id of the grouping
@@ -439,4 +428,25 @@
     
 }
 
+/**
+ * Delete all groupings from a course. Groups MUST be deleted first.
+ * TODO: If groups or groupings are to be shared between courses, think again!
+ * @param $courseid The course ID.
+ * @return boolean True if all deletes were successful, false otherwise.
+ */
+function groups_delete_all_groupings($courseid) {
+    if (! $courseid) {
+        return false;
+    }
+    $groupingids = groups_get_groupings($courseid);
+    if (! $groupingids) {
+        return true;
+    }
+    $success = true;
+    foreach ($groupingids as $gg_id) {
+        $success = $success && groups_db_delete_grouping($gg_id);
+    }
+    return $success;
+}
+
 ?>
\ No newline at end of file
Index: group/db/dbbasicgrouplib.php
===================================================================
RCS file: /cvsroot/moodle/moodle/group/db/dbbasicgrouplib.php,v
retrieving revision 1.8
diff -u -r1.8 dbbasicgrouplib.php
--- group/db/dbbasicgrouplib.php	20 Jan 2007 09:31:05 -0000	1.8
+++ group/db/dbbasicgrouplib.php	20 Jan 2007 10:56:07 -0000
@@ -356,7 +356,7 @@
 
 
 /** 
- * Deletes a specified group
+ * 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
  */
@@ -365,8 +365,7 @@
         $success = false;
     } else {
         $success = true;
-        // Get a list of the users for the group and delete them all from the 
-        // group
+        // Get a list of users for the group and remove them all.
 
         $userids = groups_db_get_members($groupid);
         if ($userids != false) {
@@ -378,7 +377,8 @@
 	        }
         }
 
-        // Remove any groupings that the group belongs to     
+        // 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) {
@@ -390,6 +390,7 @@
 		    }
 	    }
 
+        // Remove links with courses.
 		$results = delete_records('groups_courses_groups', 'groupid', $groupid);
 		if ($results == false) {
 			$success = false;
