### Eclipse Workspace Patch 1.0
#P moodle
Index: group/simpletest/test_basicgrouplib.php
===================================================================
RCS file: group/simpletest/test_basicgrouplib.php
diff -N group/simpletest/test_basicgrouplib.php
--- group/simpletest/test_basicgrouplib.php	14 Aug 2007 00:50:31 -0000	1.8
+++ /dev/null	1 Jan 1970 00:00:00 -0000
@@ -1,96 +0,0 @@
-<?php
-/**
- * Unit tests for new Moodle Groups - basicgrouplib.php and some of utillib.php.
- * 
- * /admin/report/simpletest/index.php?showpasses=1&showsearch=1&path=course%2Fgroups
- *
- * @copyright &copy; 2006 The Open University
- * @author N.D.Freear AT open.ac.uk
- * @license http://www.gnu.org/copyleft/gpl.html GNU Public License
- * @package groups
- *
- * Window - Preferences - PHPeclipse - PHP - Code Templates
- */
-
-if (!defined('MOODLE_INTERNAL')) {
-    die('Direct access to this script is forbidden.');    ///  It must be included from a Moodle page
-}
-
-require_once($CFG->dirroot . '/group/lib/basicgrouplib.php');
-require_once($CFG->dirroot . '/group/lib/utillib.php');
-
-//TODO: rewrite me
-class basicgrouplib_test /*extends UnitTestCase*/ {
-
-    var $courseid= 0;
-    var $userid  = 0;
-    var $userid_2= 0;
-    var $groupid = 0;
-
-    function __construct() {
-       parent::UnitTestCase();
-    }
-
-    function test_get_user() {
-        $this->assertTrue($user = groups_get_user(2)); //Primary admin.
-        if (isset($user)) {
-            $this->userid = $user->id;
-        }
-        $this->assertTrue($user_2 = groups_get_user(1)); //Guest.
-        if (isset($user_2)) {
-            $this->userid_2 = $user_2->id;
-        }
-    }
-
-    function test_get_course_info() {
-        $this->assertTrue($course = groups_get_course_info(1));
-        if (isset($course)) {
-            $this->courseid = $course->id;   
-        }
-    }
-
-    function test_create_group() {      
-        $this->assertTrue($this->groupid = groups_create_group($this->courseid));
-        $this->assertTrue(groups_group_exists($this->groupid));
-        $this->assertTrue(groups_group_belongs_to_course($this->groupid, $this->courseid));
-        $this->assertTrue($groupids = groups_get_groups($this->courseid));
-        //array...
-        $this->assertTrue($groupinfo = groups_set_default_group_settings());
-        $groupinfo->name = 'Group '. $this->getLabel();  //'Temporary Group Name'
-        $this->assertTrue(groups_set_group_settings($this->groupid, $groupinfo));
-        $this->assertTrue($groupinfo->name == groups_get_group_name($this->groupid));
-        $this->assertTrue($this->courseid == groups_get_course($this->groupid));
-    }
-
-    function test_group_matches(){
-      $groupinfo->name = 'Group Testname:' .  $this->getLabel();
-      $groupinfo->description  = 'Group Test Description:' . $this->getLabel();
-
-      $this->assertTrue($this->groupid = groups_create_group($this->courseid, $groupinfo));
-      $this->assertTrue(groups_group_matches($this->courseid, $groupinfo->name, $groupinfo->description));
-
-    }
-
-    function test_add_member() {
-        // NOTE, interface change on add_member, remove_member. 
-        $this->assertTrue(groups_add_member($this->groupid, $this->userid));
-        $this->assertTrue(groups_is_member($this->groupid, $this->userid));
-        $this->assertTrue($userids = groups_get_members($this->groupid));
-        //...
-        $this->assertTrue($groupids= groups_get_groups_for_user($this->userid, $this->courseid));
-        //...        
-        $this->assertTrue(1 == groups_count_group_members($this->groupid)); //Utillib.
-    }
-
-    function test_remove_member() {
-        $this->assertTrue(groups_remove_member($this->groupid, $this->userid));
-        $this->assertFalse(groups_is_member($this->groupid, $this->userid));
-    }
-
-    function test_delete_group() {
-        $this->assertTrue(groups_delete_group($this->groupid));
-        $this->assertFalse(groups_group_exists($this->groupid));
-    }
-}
-
-?>
Index: group/simpletest/test_groupinglib.php
===================================================================
RCS file: group/simpletest/test_groupinglib.php
diff -N group/simpletest/test_groupinglib.php
--- group/simpletest/test_groupinglib.php	14 Aug 2007 00:50:31 -0000	1.7
+++ /dev/null	1 Jan 1970 00:00:00 -0000
@@ -1,99 +0,0 @@
-<?php
-/**
- * Unit tests for new Moodle Groups - groupinglib.php
- *
- * @copyright &copy; 2006 The Open University
- * @author N.D.Freear AT open.ac.uk
- * @license http://www.gnu.org/copyleft/gpl.html GNU Public License
- * @package groups
- */
-
-if (!defined('MOODLE_INTERNAL')) {
-    die('Direct access to this script is forbidden.');    ///  It must be included from a Moodle page
-}
-
-require_once($CFG->dirroot . '/group/lib/groupinglib.php');
-
-//TODO: rewrite me
-class groupinglib_test /*extends UnitTestCase*/ {
-
-    var $courseid= 0;
-    var $userid  = 0;
-    var $groupid = 0;
-    var $groupingid = 0;
-
-    function __construct() {
-        parent::UnitTestCase();
-        //$this->setUpOnce();
-    }
-    function __destruct() {
-        //$this->tearDownOnce();
-    }
-
-    /**
-     * setUp/tearDown: Better in a constructor/destructor, but PHP4 doesn't do destructors :(
-     */
-    function setUp() {
-        parent::setUp();
-
-        if ($course = groups_get_course_info(1)) {
-            $this->courseid = $course->id;   
-        }
-        if ($user = groups_get_user(2)) { //Primary admin.
-            $this->userid = $user->id;
-        }
-
-        $this->groupid = groups_create_group($this->courseid);
-        $groupinfo = groups_set_default_group_settings();
-        $bok = groups_set_group_settings($this->groupid, $groupinfo);
-        $bok = groups_add_member($this->groupid, $this->userid);
-    }
-
-    function tearDown() {
-        parent::tearDown();
-
-        $bok = groups_remove_member($this->groupid, $this->userid);
-        $bok = groups_delete_group($this->groupid);
-    }
-
-    function test_create_grouping() {      
-        $this->assertTrue($this->groupingid = groups_create_grouping($this->courseid));
-        $this->assertTrue(groups_grouping_exists($this->groupingid));
-        $this->assertTrue(groups_grouping_belongs_to_course($this->groupingid, $this->courseid));
-
-        $this->assertTrue($groupinginfo = groups_set_default_grouping_settings());
-        $groupinginfo->name = 'Grouping '. $this->getLabel();
-        $this->assertTrue(groups_set_grouping_settings($this->groupingid, $groupinginfo));
-    }
-
-    function test_groups_grouping_matches(){
-      $groupinginfo->name = 'Grouping Testname:' .  $this->getLabel();
-      $groupinginfo->description  = 'Grouing Test Description:' . $this->getLabel();
-
-      $this->assertTrue($this->groupingid = groups_create_grouping($this->courseid, $groupinginfo));
-      $this->assertTrue(groups_grouping_matches($this->courseid, $groupinginfo->name, $groupinginfo->description));
-
-    }
-
-    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));
-        //...
-    }
-
-    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));        
-    }
-
-    function test_delete_grouping() {
-        $this->assertTrue(groups_delete_grouping($this->groupingid));
-        $this->assertFalse(groups_grouping_exists($this->groupingid));
-    }
-}
-
-?>
Index: group/assign.php
===================================================================
RCS file: /cvsroot/moodle/moodle/group/assign.php,v
retrieving revision 1.12
diff -u -r1.12 assign.php
--- group/assign.php	14 Aug 2007 06:41:16 -0000	1.12
+++ group/assign.php	15 Aug 2007 13:41:49 -0000
@@ -36,7 +36,7 @@
 
 $strsearch = get_string('search');
 $strshowall = get_string('showall');
-$returnurl = $CFG->wwwroot.'/group/index.php?id='.$courseid.'&group='.$groupid;
+$returnurl = $CFG->wwwroot.'/group/index.php?id='.$courseid.'&group='.$group->id;
 
 $context = get_context_instance(CONTEXT_COURSE, $courseid);
 require_capability('moodle/course:managegroups', $context);
Index: group/lib.php
===================================================================
RCS file: /cvsroot/moodle/moodle/group/lib.php,v
retrieving revision 1.3
diff -u -r1.3 lib.php
--- group/lib.php	22 Feb 2007 15:27:30 -0000	1.3
+++ group/lib.php	15 Aug 2007 13:41:49 -0000
@@ -1,6 +1,6 @@
 <?php
 /**
- * Library including new groups and groupings.
+ * Extra library for groups and groupings.
  *
  * @copyright &copy; 2006 The Open University
  * @author J.White AT open.ac.uk
@@ -8,14 +8,167 @@
  * @package groups
  */
 
-require_once($CFG->dirroot.'/group/lib/basicgrouplib.php');
+/*
+ * INTERNAL FUNCTIONS - to be used by moodle core only
+ * require_once $CFG->dirroot.'/group/lib.php' must be used
+ */
+
+/**
+ * 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
+ */
+function groups_delete_group($groupid) {
+    global $CFG;
+    require_once($CFG->libdir.'/gdlib.php');
+
+    if (empty($groupid)) {
+        return false;
+    }
+
+    //first delete members
+    delete_records('groups_members', 'groupid', $groupid);
+    //then imge
+    delete_profile_image($groupid, 'groups');
+    //group itself last
+    return delete_records('groups', 'id', $groupid);
+}
+
+function groups_delete_group_members($courseid, $showfeedback=false) {
+    global $CFG;
+
+    $sql = "DELETE FROM {$CFG->prefix}groups_members
+             WHERE groupid in (SELECT id FROM {$CFG->prefix}groups g WHERE g.courseid = $courseid)";
+
+    execute_sql($sql, false);
+    if ($showfeedback) {
+        notify(get_string('deleted').' groups_members');
+    }
+
+    return true;
+}
+
+function groups_delete_groups($courseid, $showfeedback=false) {
+    global $CFG;
+    require_once($CFG->libdir.'/gdlib.php');
+
+    // delete any uses of groups
+    $sql = "DELETE FROM {$CFG->prefix}groupings_groups
+             WHERE groupid in (SELECT id FROM {$CFG->prefix}groups g WHERE g.courseid = $courseid)";
+    execute_sql($sql, false);
+
+    groups_delete_group_members($courseid, false);
+
+    // delete group pictures
+    if ($groups = get_records('groups', 'courseid', $courseid)) {
+        foreach($groups as $group) {
+            delete_profile_image($group->id, 'groups');
+        }
+    }
+
+    delete_records('groups', 'courseid', $courseid);
+    if ($showfeedback) {
+        notify(get_string('deleted').' groups');
+    }
+
+    return true;
+}
+
+function groups_delete_groupings($courseid, $showfeedback=false) {
+    global $CFG;
+
+    // delete any uses of groupings
+    $sql = "DELETE FROM {$CFG->prefix}groupings_groups
+             WHERE groupingid in (SELECT id FROM {$CFG->prefix}groupings g WHERE g.courseid = $courseid)";
+    execute_sql($sql, false);
+
+    // remove the default groupingid from course
+    set_field('course', 'defaultgroupingid', 0, 'id', $courseid);
+    // remove the groupingid from all course modules
+    set_field('course_modules', 'groupingid', 0, 'courseid', $courseid);
+
+    delete_records('groupings', 'courseid', $courseid);
+    if ($showfeedback) {
+        notify(get_string('deleted').' groupings');
+    }
+
+    return true;
+}
+
+/* =================================== */
+/* various functions used by groups UI */
+/* =================================== */
+
+/**
+ * Gets the users for a course who are not in a specified group
+ * @param int $groupid The id of the group
+ * @param string searchtext similar to searchtext in role assign, search
+ * @return array An array of the userids of the non-group members,  or false if
+ * an error occurred.
+ * This function was changed to get_users_by_capability style
+ * mostly because of the searchtext requirement
+ */
+function groups_get_users_not_in_group($courseid, $groupid, $searchtext='') {
 
-require_once($CFG->dirroot.'/group/lib/groupinglib.php');
+    global $CFG;
 
-require_once($CFG->dirroot.'/group/lib/utillib.php');
+    $context = get_context_instance(CONTEXT_COURSE, $courseid);
 
-require_once($CFG->dirroot.'/group/lib/modulelib.php');
+    if ($searchtext !== '') {   // Search for a subset of remaining users
+        $LIKE      = sql_ilike();
+        $FULLNAME  = sql_fullname();
+        $wheresearch = " AND u.id IN (SELECT id FROM {$CFG->prefix}user WHERE $FULLNAME $LIKE '%$searchtext%' OR email $LIKE '%$searchtext%' )";
+    } else {
+        $wheresearch = '';
+    }
+
+    $capability = 'moodle/course:view';
+    $doanything = false;
+
+    // find all possible "student" roles
+    if ($possibleroles = get_roles_with_capability($capability, CAP_ALLOW, $context)) {
+        if (!$doanything) {
+            if (!$sitecontext = get_context_instance(CONTEXT_SYSTEM)) {
+                return false;    // Something is seriously wrong
+            }
+            $doanythingroles = get_roles_with_capability('moodle/site:doanything', CAP_ALLOW, $sitecontext);
+        }
+
+        $validroleids = array();
+        foreach ($possibleroles as $possiblerole) {
+            if (!$doanything) {
+                if (isset($doanythingroles[$possiblerole->id])) {  // We don't want these included
+                    continue;
+                }
+            }
+            if ($caps = role_context_capabilities($possiblerole->id, $context, $capability)) { // resolved list
+                if (isset($caps[$capability]) && $caps[$capability] > 0) { // resolved capability > 0
+                    $validroleids[] = $possiblerole->id;
+                }
+            }
+        }
+        if (empty($validroleids)) {
+            return false;
+        }
+        $roleids =  '('.implode(',', $validroleids).')';
+    } else {
+        return false;  // No need to continue, since no roles have this capability set
+    }
+
+/// Construct the main SQL
+    $select = " SELECT u.id, u.firstname, u.lastname";
+    $from   = " FROM {$CFG->prefix}user u
+                INNER JOIN {$CFG->prefix}role_assignments ra ON ra.userid = u.id
+                INNER JOIN {$CFG->prefix}role r ON r.id = ra.roleid";
+    $where  = " WHERE ra.contextid ".get_related_contexts_string($context)."
+                  AND u.deleted = 0
+                  AND ra.roleid in $roleids
+                  AND u.id NOT IN (SELECT userid
+                                   FROM {$CFG->prefix}groups_members
+                                   WHERE groupid = $groupid)
+                  $wheresearch";
 
-require_once($CFG->dirroot.'/group/lib/legacylib.php');
+    return get_records_sql($select.$from.$where);;
+}
 
 ?>
\ No newline at end of file
Index: group/index.php
===================================================================
RCS file: /cvsroot/moodle/moodle/group/index.php,v
retrieving revision 1.27
diff -u -r1.27 index.php
--- group/index.php	14 Aug 2007 00:50:02 -0000	1.27
+++ group/index.php	15 Aug 2007 13:41:49 -0000
@@ -216,5 +216,37 @@
 
     print_footer($course);
 
+/**
+ * Returns the first button action with the given prefix, taken from
+ * POST or GET, otherwise returns false.
+ * See /lib/moodlelib.php function optional_param.
+ * @param $prefix 'act_' as in 'action'.
+ * @return string The action without the prefix, or false if no action found.
+ */
+function groups_param_action($prefix = 'act_') {
+    $action = false;
+//($_SERVER['QUERY_STRING'] && preg_match("/$prefix(.+?)=(.+)/", $_SERVER['QUERY_STRING'], $matches)) { //b_(.*?)[&;]{0,1}/
+
+    if ($_POST) {
+        $form_vars = $_POST;
+    }
+    elseif ($_GET) {
+        $form_vars = $_GET; 
+    }
+    if ($form_vars) {
+        foreach ($form_vars as $key => $value) {
+            if (preg_match("/$prefix(.+)/", $key, $matches)) {
+                $action = $matches[1];
+                break;
+            }
+        }
+    }
+    if ($action && !preg_match('/^\w+$/', $action)) {
+        $action = false;
+        error('Action had wrong type.');
+    }
+    ///if (debugging()) echo 'Debug: '.$action;
+    return $action;
+}
 
 ?>
\ No newline at end of file
Index: group/edit.php
===================================================================
RCS file: /cvsroot/moodle/moodle/group/edit.php,v
retrieving revision 1.10
diff -u -r1.10 edit.php
--- group/edit.php	14 Aug 2007 00:50:02 -0000	1.10
+++ group/edit.php	15 Aug 2007 13:41:49 -0000
@@ -26,11 +26,6 @@
     error('Course ID was incorrect');
 }
 
-/// Delete action should not be called without a group id
-if ($delete && !$id) {
-    error(get_string('errorinvalidgroup'));
-}
-
 /// basic access control checks
 if (! $course = get_record('course', 'id', $courseid)) {
     error("Incorrect course id ");
@@ -53,7 +48,6 @@
 }
 
 if ($id and $delete) {
-
     if (!$confirm) {
         print_header(get_string('deleteselectedgroup', 'group'), get_string('deleteselectedgroup', 'group'));
         $optionsyes = array('id'=>$id, 'delete'=>1, 'courseid'=>$courseid, 'sesskey'=>sesskey(), 'confirm'=>1);
@@ -61,7 +55,7 @@
         if (!$group = get_record('groups', 'id', $id)) {
             error('Group ID was incorrect');
         }
-        notice_yesno(get_string('deletegroupconfirm', 'group', $group->name), 'edit.php', 'index.php', $optionsyes, $optionsno, 'post', 'get');
+        notice_yesno(get_string('deletegroupconfirm', 'group', $group->name), 'edit.php', 'index.php', $optionsyes, $optionsno, 'get', 'get');
         print_footer();
         die;
 
@@ -74,7 +68,7 @@
             events_trigger('group_deleted', $eventdata);
             redirect('index.php?id='.$course->id);
         } else {
-            print_error('erroreditgroup', 'group', groups_home_url($course->id));
+            print_error('erroreditgroup', 'group', $returnurl);
         }
     }
 }
Index: group/printgrouping.php
===================================================================
RCS file: group/printgrouping.php
diff -N group/printgrouping.php
--- group/printgrouping.php	14 Aug 2007 00:50:04 -0000	1.3
+++ /dev/null	1 Jan 1970 00:00:00 -0000
@@ -1,93 +0,0 @@
-<?php
-/**
- * Print groups in groupings, and members of groups.
- *
- * @copyright &copy; 2006 The Open University
- * @author J.White AT open.ac.uk
- * @license http://www.gnu.org/copyleft/gpl.html GNU Public License
- * @package groups
- */
-require_once('../config.php');
-require_once('lib.php');
-
-//TODO: fix me
-die;die;die;
-$success = true;
-
-$courseid   = required_param('courseid', PARAM_INT);
-$groupingid = required_param('groupingid', PARAM_INT);
-
-// Get the course information so we can print the header and
-// check the course id is valid
-$course = groups_get_course_info($courseid);
-if (! $course) {
-    $success = false;
-    print_error('invalidcourse');
-}
-
-
-if ($success) {
-    // Make sure that the user has permissions to manage groups.
-    require_login($courseid);
-
-    $context = get_context_instance(CONTEXT_COURSE, $courseid);
-    if (! has_capability('moodle/course:managegroups', $context)) {
-        redirect();
-    }
-
-    //( confirm_sesskey checks that this is a POST request.)
-
-    // Print the page and form
-    $strgroups = get_string('groups');
-    $strparticipants = get_string('participants');
-    print_header("$course->shortname: $strgroups", $course->fullname,
-                 "<a href=\"$CFG->wwwroot/course/view.php?id=$courseid\">$course->shortname</a> ".
-                 "-> <a href=\"$CFG->wwwroot/user/index.php?id=$courseid\">$strparticipants</a> ".
-                 "-> <a href=\"$CFG->wwwroot/group/index.php?id=$courseid\">$strgroups</a>".
-                 "-> ".get_string('printerfriendly', 'group'), "", "", true, '', user_login_string($course, $USER));
-
-    $groupingname = groups_get_grouping_name($groupingid);
-    if (! $groupingname) {
-        print_error('errorinvalidgrouping', 'group', groups_home_url($courseid));
-    } else {
-       // Print the name of the grouping
-        if (!empty($CFG->enablegroupings)) {
-            // NO GROUPINGS YET!
-           echo "<h1>$groupingname</h1>\n";
-        }
-    }
-
-    // Get the groups and group members for the grouping.
-    if (GROUP_NOT_IN_GROUPING == $groupingid) {
-        $groupids = groups_get_groups_not_in_any_grouping($courseid);
-    } else {
-        $groupids = groups_get_groups_in_grouping($groupingid);
-    }
-
-    if ($groupids) {
-        // Make sure the groups are in the right order
-        $group_names = groups_groupids_to_group_names($groupids);
-
-        // Go through each group in turn and print the group name and then the members
-        foreach ($group_names as $group) {
-
-            echo "<h2>{$group->name}</h2>\n";
-            $userids = groups_get_members($group->id);
-            if ($userids != false) {
-                // Make sure the users are in the right order
-                $user_names = groups_userids_to_user_names($userids, $courseid);
-
-                echo "<ol>\n";
-                foreach ($user_names as $user) {
-
-                    echo "<li>{$user->name}</li>\n";
-                }
-                echo "</ol>\n";
-            }
-        }
-    }
-
-    print_footer($course);
-}
-
-?>
Index: group/grouping.php
===================================================================
RCS file: /cvsroot/moodle/moodle/group/grouping.php,v
retrieving revision 1.10
diff -u -r1.10 grouping.php
--- group/grouping.php	14 Aug 2007 00:50:00 -0000	1.10
+++ group/grouping.php	15 Aug 2007 13:41:49 -0000
@@ -14,7 +14,6 @@
 
 require_once('../config.php');
 require_once('lib.php');
-require_once($CFG->libdir.'/moodlelib.php');
 require_once('grouping_edit_form.php');
 
 $courseid   = required_param('courseid', PARAM_INT);         
@@ -27,6 +26,8 @@
     error('No groupings yet');
 }
 
+$returnurl = $CFG->wwwroot.'/group/index.php?id='.$courseid;
+
 // Get the course information so we can print the header and
 // check the course id is valid
 $course = groups_get_course_info($courseid);
@@ -35,7 +36,7 @@
     print_error('invalidcourse'); //'The course ID is invalid'
 }
 if (GROUP_NOT_IN_GROUPING == $id) {
-    print_error('errornotingroupingedit', 'group', groups_home_url($courseid), get_string('notingrouping', 'group'));
+    print_error('errornotingroupingedit', 'group', $returnurl, get_string('notingrouping', 'group'));
 }
 
 /// basic access control checks
@@ -58,14 +59,14 @@
 // preprocess data
 if ($delete) {
     if (groups_delete_grouping($id)) {
-        redirect(groups_home_url($course->id));
+        redirect($returnurl);
     } else {
-        print_error('erroreditgrouping', 'group', groups_home_url($course->id));
+        print_error('erroreditgrouping', 'group', $returnurl);
     }
 }
 
 if ($editform->is_cancelled()) {
-    redirect(groups_home_url($courseid, false, $id, false));
+    redirect($returnurl);
 } elseif ($data = $editform->get_data()) {
     $success = true;
     
@@ -83,9 +84,9 @@
     }
     
     if ($success) {
-        redirect(groups_home_url($courseid, false, $id, false));
+        redirect($returnurl);
     } else {
-        print_error('erroreditgrouping', 'group', groups_home_url($courseid));
+        print_error('erroreditgrouping', 'group', $returnurl);
     }
 
 } else { // Prepare and output form
@@ -101,7 +102,7 @@
                  $course->fullname, 
                  "<a href=\"$CFG->wwwroot/course/view.php?id=$courseid\">$course->shortname</a> ".
                  "-> <a href=\"$CFG->wwwroot/user/index.php?id=$courseid\">$strparticipants</a> ".
-                 '-> <a href="' .format_string(groups_home_url($courseid, false, $id, false)) . "\">$strgroups</a>".
+                 "-> <a href=\"$returnurl\">$strgroups</a>".
                  "-> $strheading", '', '', true, '', user_login_string($course, $USER));
     print_heading($strheading);
     $editform->display();
Index: blocks/admin/block_admin.php
===================================================================
RCS file: /cvsroot/moodle/moodle/blocks/admin/block_admin.php,v
retrieving revision 1.98
diff -u -r1.98 block_admin.php
--- blocks/admin/block_admin.php	12 Aug 2007 16:09:57 -0000	1.98
+++ blocks/admin/block_admin.php	15 Aug 2007 13:41:49 -0000
@@ -102,7 +102,7 @@
 
         if (($course->groupmode || !$course->groupmodeforce) && has_capability('moodle/course:managegroups', $context) && ($course->id!==SITEID)) {
             $strgroups = get_string('groups');
-            $this->content->items[]='<a title="'.$strgroups.'" href="'.groups_home_url($this->instance->pageid).'">'.$strgroups.'</a>';
+            $this->content->items[]='<a title="'.$strgroups.'" href="'.$CFG->wwwroot.'/group/index.php?id='.$this->instance->pageid.'">'.$strgroups.'</a>';
             $this->content->icons[]='<img src="'.$CFG->pixpath.'/i/group.gif" class="icon" alt="" />';
         }
 
Index: mod/quiz/locallib.php
===================================================================
RCS file: /cvsroot/moodle/moodle/mod/quiz/locallib.php,v
retrieving revision 1.118
diff -u -r1.118 locallib.php
--- mod/quiz/locallib.php	14 Aug 2007 04:22:06 -0000	1.118
+++ mod/quiz/locallib.php	15 Aug 2007 13:41:58 -0000
@@ -881,7 +881,7 @@
     // check for notifications required
     $notifyfields = 'u.id, u.username, u.firstname, u.lastname, u.email, u.emailstop, u.lang, u.timezone, u.mailformat, u.maildisplay';
     $userstonotify = get_users_by_capability($context, 'mod/quiz:emailnotifysubmission',
-            $notifyfields, '', '', '', groups_m_get_groups_for_user($cm, $USER->id),
+            $notifyfields, '', '', '', groups_get_all_groups_for_user($USER->id),
             $notifyexcludeusers, false, false, true);
 
     // if something to send, then build $a
Index: group/db/dbcleanup.php
===================================================================
RCS file: group/db/dbcleanup.php
diff -N group/db/dbcleanup.php
--- group/db/dbcleanup.php	8 May 2007 23:44:31 -0000	1.2
+++ /dev/null	1 Jan 1970 00:00:00 -0000
@@ -1,59 +0,0 @@
-<?php
-
-// @@@ TO DO Need to write these functions because they are needed by parts of
-// the current moodle code where courses are deleted etc. 
-
-/**
- * @param int $courseid If false, removes the user from all groups for all 
- * course
- */
-function groups_remove_user_from_all_groups($userid, $courseid) {
-    // @@@ TO DO 
-}
-
-function groups_remove_all_group_members($courseid, $showfeedback) {
-    // @@@ TO DO 
-}
-
-function groups_remove_all_groups($courseid, $removemembers, $showfeedback) {
-    // @@@ TO DO 
-}
-
-/**
- * Cleans up all the groups and groupings in a course (it does this best-effort 
- * i.e. if one deletion fails, it still attempts further deletions).
- * IMPORTANT: Note that if the groups and groupings are used by other courses 
- * somehow, they will still be deleted - it doesn't protect against this. 
- * @param int $courseid The id of the course
- * @return boolean True if the clean up was successful, false otherwise. 
- */
-function groups_cleanup_groups($courseid) {
-    $success = true;
-
-    // Delete all the groupings 
-    $groupings = groups_get_groupings($courseid);
-    if ($groupings != false) {
-        foreach($groupings as $groupingid) {
-            $groupingdeleted = groups_delete_grouping($groupingid);
-            if (!$groupingdeleted) {
-                $success = false;
-            }
-        }
-    }
-
-    // Delete all the groups
-    $groupids = groups_get_groups($courseid);
-    if ($groupids != false) {
-        foreach($groupids as $groupid) {
-            $groupdeleted = groups_delete_group($groupid);
-
-            if (!$groupdeleted) {
-                $success = false;
-            }
-        }
-    }
-
-    return $success;
-}
-
-?>
Index: group/db/dbmodulelib.php
===================================================================
RCS file: group/db/dbmodulelib.php
diff -N group/db/dbmodulelib.php
--- group/db/dbmodulelib.php	8 May 2007 23:44:32 -0000	1.3
+++ /dev/null	1 Jan 1970 00:00:00 -0000
@@ -1,46 +0,0 @@
-<?php
-/*******************************************************************************
- * modulelib.php
- * 
- * This file contains functions to be used by modules to support groups. More
- * documentation is available on the Developer's Notes section of the Moodle 
- * wiki. 
- * 
- * For queries, suggestions for improvements etc. please post on the Groups 
- * forum on the moodle.org site.
- ******************************************************************************/
-// @@@ Lots TO DO in this file
-
-
-/**
- * Gets the groupingid for a particular course module instance returning
- * false if it is null. 
- * @param int  $cmid The id of the module instance.
- * @return int The grouping id (or false if it is null or an error occurred)
- */
-function groups___db_m_get_groupingid($cmid) {
-    // @@@ Check nulls are turned into false
-    $query = get_record('course_modules', 'groupingid', $userid);
-    return $query;
-}
-
-/**
- * Gets the groupingid for a particular course module instance 
- */
-function groups_db_m_set_groupingid($cmid) {
-    // @@@ TO DO
-}
-
-
-/**
- * Gets the group object associated with a group id. This group object can be 
- * used to get information such as the name of the group and the file for the 
- * group icon if it exists. (Look at the groups table in the database to see
- * the fields). 
- * @param int $groupid The id of the group
- * @return group The group object 
- */
-function groups_db_m_get_group($groupid) {
-}
-
-?>
Index: group/db/dbbasicgrouplib.php
===================================================================
RCS file: /cvsroot/moodle/moodle/group/db/dbbasicgrouplib.php,v
retrieving revision 1.16
diff -u -r1.16 dbbasicgrouplib.php
--- group/db/dbbasicgrouplib.php	14 Aug 2007 00:50:14 -0000	1.16
+++ group/db/dbbasicgrouplib.php	15 Aug 2007 13:41:49 -0000
@@ -9,9 +9,6 @@
  * @license http://www.gnu.org/copyleft/gpl.html GNU Public License
  * @package groups
  */
-require_once($CFG->libdir.'/datalib.php');
-require_once($CFG->dirroot.'/group/lib.php');
-
 
 /*******************************************************************************
  * Utility functions
@@ -140,30 +137,6 @@
 
 }
 
-/**
- * Given two users, determines if there exists a group to which they both belong
- * @param int $userid1 The id of the first user
- * @param int $userid2 The id of the second user
- * @return boolean True if the users are in a common group, false otherwise or 
- * if an error occurred. 
- */
-function groups_db_users_in_common_group($userid1, $userid2) {
-    global $CFG;
-    $havecommongroup = false;
-    $sql = "SELECT gm1.groupid, 1 FROM {$CFG->prefix}groups_members gm1 " .
-        "INNER JOIN {$CFG->prefix}groups_members gm2 " .
-        "ON gm1.groupid = gm2.groupid" .
-        "WHERE gm1.userid = '$userid1' AND gm2.userid = '$userid2'";
-    $commongroups = get_record_sql($sql);
-    if ($commongroups) {
-        $havecommongroup = true;
-    }
-
-    return $havecommongroup;           
-}
-
-
-
 /******************************************************************************* 
    Membership functions  
  ******************************************************************************/
@@ -187,27 +160,6 @@
 
 
 /**
- * Determine if a course ID, group name and description match a group in the database.
- *   For backup/restorelib.php
- * @return mixed A group-like object with $group->id, or false.
- */
-function groups_db_group_matches($courseid, $grp_name, $grp_description) {
-//$gro_db->id; $gro_db = get_record("groups","courseid",$restore->course_id,"name",$gro->name,"description",$gro->description);    
-    global $CFG;
-    $sql = "SELECT g.id, g.name, g.description
-        FROM {$CFG->prefix}groups g
-        WHERE g.name = '$grp_name'
-        AND g.description = '$grp_description'
-        AND g.courseid = '$courseid'";
-    $records = get_records_sql($sql);
-    $group = false;
-    if ($records) {
-        $group = array_shift($records);
-    } 
-    return $group;
-}
-
-/**
  * Determine if a course ID, and group name match a group in the database.
  * @return mixed A group-like object with $group->id, or false.
  */
@@ -399,54 +351,6 @@
 }
 
 
-/** 
- * 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; 
-                }
-            }
-        }
-        
-        // 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;
-}
-
 /**
  * Internal function to set the time a group was modified.
  */
@@ -455,49 +359,4 @@
 }
 
 
-/******************************************************************************
- * Groups SQL clauses for modules and core.
- */
-
-/**
- * Returns the table in which group members are stored, with a prefix 'gm'.
- * @return SQL string.
- */
-function groups_members_from_sql() {
-    global $CFG;
-    return " {$CFG->prefix}groups_members gm ";
-}
-
-/**
- * Returns a join testing user.id against member's user ID.
- * Relies on 'user' table being included as 'user u'.
- * Used in Quiz module reports.
- * @param group ID, optional to include a test for this in the SQL.
- * @return SQL string.
- */
-function groups_members_join_sql($groupid=false) {    
-    $sql = ' JOIN '.groups_members_from_sql().' ON u.id = gm.userid ';
-    if ($groupid) {
-        $sql = "AND gm.groupid = '$groupid' ";
-    }
-    return $sql;
-    //return ' INNER JOIN '.$CFG->prefix.'role_assignments ra ON u.id=ra.userid'.
-    //       ' INNER JOIN '.$CFG->prefix.'context c ON ra.contextid=c.id AND c.contextlevel='.CONTEXT_GROUP.' AND c.instanceid='.$groupid;
-}
-
-/**
- * Returns SQL for a WHERE clause testing the group ID.
- * Optionally test the member's ID against another table's user ID column. 
- * @param groupid
- * @param userid_sql Optional user ID column selector, example "mdl_user.id", or false.
- * @return SQL string.
- */
-function groups_members_where_sql($groupid, $userid_sql=false) {
-    $sql = " gm.groupid = '$groupid' ";
-    if ($userid_sql) {
-        $sql .= "AND $userid_sql = gm.userid ";
-    }
-    return $sql;
-}
-
 ?>
Index: group/db/dbgroupinglib.php
===================================================================
RCS file: /cvsroot/moodle/moodle/group/db/dbgroupinglib.php,v
retrieving revision 1.9
diff -u -r1.9 dbgroupinglib.php
--- group/db/dbgroupinglib.php	14 Aug 2007 00:50:17 -0000	1.9
+++ group/db/dbgroupinglib.php	15 Aug 2007 13:41:49 -0000
@@ -70,34 +70,6 @@
     return $groupids;
 }
 
-
-/*
- * Gets the groupings that a group belongs to 
- * @param int $groupid The id of the group
- * @return array An array of the ids of the groupings that the group belongs to, 
- * or false if there are none or if an error occurred. 
- */
-function groups_db_get_groupings_for_group($groupid) {
-    if (!$groupid) {
-        $groupingids = false;
-    } else {
-        $groupings = get_records('groupings_groups', 'groupid ', 
-            $groupid, '', $fields='id, groupingid');
-        if (!$groupings) {
-            $groupingids = false;
-        } else {
-            // Put the results into an array
-            $groupingids = array();
-            foreach ($groupings as $grouping) {
-                array_push($groupingids, $grouping->groupingid);
-            }
-        } 
-    }
-
-    return $groupingids;
-}
-
-
 /**
  * Gets the information about a specified grouping
  * @param int $groupingid
@@ -118,28 +90,6 @@
     return $groupingsettings;
 }
 
-/**
- * Gets the grouping to use for a particular instance of a module in a course
- * @param int $coursemoduleid The id of the instance of the module in the course
- * @return int The id of the grouping or false if there is no such id recorded 
- * or if an error occurred. 
- */
-function groups_db_get_grouping_for_coursemodule($cm) {
-    if (is_object($cm) and isset($cm->course) and isset($cm->groupingid)) {
-        //Do NOT rely on cm->module!
-        return $cm->groupingid;
-    } elseif (is_numeric($cm)) {
-        // Treat param as the course module ID.
-        $coursemoduleid = $cm;
-        $record = get_record('course_modules', 'id', $coursemoduleid, 'id, groupingid');
-        if ($record and isset($record->groupingid)) {
-            return $record->groupingid;
-        }
-    }
-    return false;
-}
-
-
 /*******************************************************************************
         Membership functions  
  ******************************************************************************/
@@ -161,25 +111,6 @@
     return $exists;
 }
 
-
- /**
-  * Determines if a group belongs to any grouping for the course that it belongs
-  * to
-  * @param int $groupid The id of the group
-  * @return boolean. True if the group belongs to a grouping, false otherwise or
-  * if an error has occurred.
-  */
- function groups_db_belongs_to_any_grouping($groupid) {
-    if (!$groupid) {
-        $isingrouping = false;
-    } else {
-        $isingrouping = record_exists('groupings_groups', 'groupid', 
-                                  $groupid);
-    }
-    
-    return $isingrouping;
- }
-
  
  /**
   * Determines if a group belongs to a specified grouping
@@ -199,50 +130,6 @@
     return $isingrouping;
  }
  
- 
-  /**
-  * Detemines if a specified user belongs to any group of a specified grouping.
-  * @param int $userid The id of the user
-  * @param int $groupingid The id of the grouping
-  * @return boolean True if the user belongs to some group in the grouping,
-  * false otherwise or if an error occurred. 
-  */
- function groups_db_is_member_of_some_group_in_grouping($userid, $groupingid) {
-    if (!$userid or !$groupingid) {
-        $belongstogroup = false;
-    } else {
-        global $CFG;
-        $sql = "SELECT gm.id
-        FROM {$CFG->prefix}groupings_groups gg
-        INNER JOIN {$CFG->prefix}groups_members gm
-        ON gg.groupid = gm.groupid
-        WHERE gm.userid = '$userid' AND gg.groupingid = '$groupingid'";
-        $belongstogroup = record_exists_sql($sql);
-    }
-    return $belongstogroup;
- }
- 
- 
-  /** 
-  * Determines if a grouping belongs to a specified course
-  * @param int $groupingid The id of the grouping
-  * @param int $courseid The id of the course
-  * @return boolean True if the grouping belongs to the course, false otherwise, 
-  * or if an error occurred. 
-  */
- function groups_db_grouping_belongs_to_course($groupingid, $courseid) {
-    if (!$groupingid or !$courseid) {
-        $belongstocourse = false;
-    } else {
-        $belongstocourse = record_exists('groups_courses_groupings', 
-                                         'groupingid', $groupingid, 'courseid', 
-                                         $courseid);
-    }
-    
-    return $belongstocourse;
- }
-
-
 
 
 /*******************************************************************************
@@ -266,7 +153,6 @@
         $groupingid = false; 
     } else {
         // Replace any empty groupsettings
-        $groupingsettings = groups_set_default_grouping_settings($groupingsettings);
         $record = $groupingsettings;
         $record->timecreated = time();
 
@@ -286,113 +172,12 @@
     return $groupingid;
 }
 
-
-/**
- * Adds a specified group to a specified grouping. 
- * @param int $groupid The id of the group
- * @param int $groupingid The id of the grouping
- * @return boolean True if the group was added successfully, false otherwise
- */
-function groups_db_add_group_to_grouping($groupid, $groupingid) {
-   if (!$groupid or !$groupingid or !groups_db_group_exists($groupid) 
-       or !groups_db_grouping_exists($groupingid)) {
-        $success = false;
-    } else {
-        $success = true;
-        $record = new Object();
-        $record->groupingid = $groupingid;
-        $record->groupid = $groupid;
-        $record->timeadded = time();
-
-        $results = insert_record('groupings_groups', $record);
-        if (!$results) {
-            $success = false;
-        }
-    }
-    
-    return $groupingid;   
-}
-
-
-/**
- * Set information about a grouping
- * @param int $groupingid The grouping to update the info for.
- * @param object $groupingsettings 
- */
-function groups_db_set_grouping_settings($groupingid, $groupingsettings) {
-    $success = true;
-    if (!$groupingid or !$groupingsettings 
-        or !groups_db_grouping_exists($groupingid)) {
-        $success = false; 
-    } else {
-        // Replace any empty group settings. 
-        $record = $groupingsettings;
-        $record->id = $groupingid;
-        $record->timemodified = time();
-        $result = update_record('groups_groupings', $record);
-        if (!$result) {
-            $success = false;
-        }
-    }
-    
-    return $success;
-}
-
-
-/**
- * Sets a grouping to use for a particular instance of a module in a course
- * @param int $groupingid The id of the grouping
- * @param int $coursemoduleid The id of the instance of the module in the course
- * @return boolean True if the operation was successful, false otherwise
- */
-function groups_db_set_grouping_for_coursemodule($groupingid, $coursemoduleid) {
-    $success = true;
-    if (!$groupingid or !$coursemoduleid) {
-        $success = false;
-    } else {
-        $record = new Object();
-        $record->id = $coursemoduleid;
-        $record->groupingid = $groupingid;
-        $result = update_record('course_modules', $record);
-        if (!$result) {
-            $success = false;
-        } 
-    }
-    return $success;
-}
-
-
 /*******************************************************************************
         Deletion functions  
  ******************************************************************************/
 
 
 /** 
- * Removes a specified group from a specified 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
- * @return boolean True if the deletion was successful, false otherwise. 
- */
-function groups_db_remove_group_from_grouping($groupid, $groupingid) {
-    $success = true;
-    if (!$groupingid or !$groupid) {
-        $success = false;
-    } else {
-        $results = delete_records('groupings_groups', 'groupid', 
-            $groupid, 'groupingid', $groupingid);
-        // 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;
-}
-
-
-/** 
  * Removes a grouping from a course - note that this function removes but does 
  * not delete any of the groups in the grouping.  
  * @param int $groupingid The id of the grouping
Index: group/lib/modulelib.php
===================================================================
RCS file: group/lib/modulelib.php
diff -N group/lib/modulelib.php
--- group/lib/modulelib.php	9 May 2007 00:06:30 -0000	1.6
+++ /dev/null	1 Jan 1970 00:00:00 -0000
@@ -1,429 +0,0 @@
-<?php
-/**
- * modulelib.php
- * 
- * This file contains functions to be used by modules to support groups. More
- * documentation is available on the Developer's Notes section of the Moodle 
- * wiki. 
- * 
- * For queries, suggestions for improvements etc. please post on the Groups 
- * forum on the moodle.org site.
- *
- * @copyright &copy; 2006 The Open University
- * @author J.White AT open.ac.uk
- * @license http://www.gnu.org/copyleft/gpl.html GNU Public License
- * @package groups
- */ 
-
-/*
- * (OLD) Permission types
- * 
- * There are six types of permission that a user can hold for a particular
- * group - 'student view', 'student contribute', 'teacher view', 
- * 'teacher contribute', 'view members list' and 'view group existence'. 
- * 
- * A particular user need not to be a member of the group to have a specific 
- * permission and may have more than one permission type. The permission that a 
- * particular user has for a group used by an particular instance of the module 
- * depends on whether the student is a teacher or student for the course and on 
- * the settings for the set of groups (the 'grouping') being used by the 
- * instance of the module  
- * 
- * It is up to each module to decide how to interpret the different permission 
- * types. The only exception is with 'view members list' and 'view group 
- * existence'. The former means that the user can view the members of the group 
- * while the latter means that the user can view information such as the group 
- * name and description. It is possible that a user may have 'view members list' 
- * permission without 'view group existence' permission - the members would just 
- * appear as the other users on the course. 
- * 
- * Permission types can be combined with boolean expressions where they are used 
- * if necessary. 
- * 
- * @name GROUPS_STUDENT Either 'student view' or 'student contribute' permission
- * @name GROUPS_TEACHER Either 'teacher view' or 'teacher contribute' permission
- * @name GROUPS_VIEW Either 'teacher view' or 'student view' permission
- * @name GROUPS_CONTRIBUTE Either 'teacher contribute' or 'student contribute' 
- * permission
- * @name GROUPS_VIEW_GROUP_EXISTENCE 'view group existence' permission
- * @name GROUPS_VIEW_MEMBERS_LIST 'view members list' permission
- * @name GROUPS_STUDENT_VIEW 'student view' permission
- * @name GROUPS_STUDENT_CONTRIBUTE 'student contribute' permission
- * @name GROUPS_TEACHER_VIEW 'teacher view' permission
- * @name GROUPS_TEACHER_CONTRIBUTE 'teacher contribute' permission
- */
-define('GROUPS_STUDENT', 1);
-define('GROUPS_TEACHER', 2);
-define('GROUPS_VIEW', 4);
-define('GROUPS_CONTRIBUTE', 8);
-define('GROUPS_VIEW_GROUP_EXISTENCE', 16);
-define('GROUPS_VIEW_MEMBERS_LIST', 48);
-define('GROUPS_STUDENT_VIEW', 5);
-define('GROUPS_STUDENT_CONTRIBUTE', 9);
-define('GROUPS_TEACHER_VIEW', 6);
-define('GROUPS_TEACHER_CONTRIBUTE', 10);
-
-/**
- * Indicates if the instance of the module has been set up by an editor of the 
- * course to use groups. This functionality can also be obtained using the 
- * groups_m_get_groups() function, however it is sufficiently commonly needed 
- * that this separate function has been provided and should be used instead. 
- * @param int $cmid The id of the module instance
- * @return boolean True if the instance is set up to use groups, false otherwise
- */
-function groups_m_uses_groups($cmid) {
-    $usesgroups = false;
-    $groupingid = groups_db_get_groupingid($cmid);
-    if (!$groupingid) {
-        $usesgroups = true;
-    }
-
-    return $usesgroups;
-}
-
-/**
- * Prints a dropdown box to enable a user to select between the groups for the 
- * module instance of which they are a member. If a user belongs to 0 or 1 
- * groups, no form is printed. The dropdown box belongs to a form and when a 
- * user clicks on the box this form is automatically submitted so that the page 
- * knows about the change. 
- * @param int $cmid The id of the module instance
- * @param string $urlroot The url of the page - this is necessary so the form 
- * can submit to the correct page. 
- * @param int $permissiontype - see note on permissiontypes above. 
- * @return boolean True unless an error occurred or the module instance does not 
- * use groups in which case returns false. 
- */
-function groups_m_print_group_selector($cmid, $urlroot, $permissiontype) {
-    // Get the groups for the cmid
-    // Produce an array to put into the $groupsmenu array. 
-    // Add an all option if necessary. 
-    $groupids = groups_module_get_groups_for_current_user($cmid, $permissiontype);
-
-    // Need a line to check if current group selected. 
-    if ($groupids) {
-        $currentgroup = groups_module_get_current_group($cmid);
-        if ($allgroupsoption) {
-            $groupsmenu[0] = get_string('allparticipants');
-        }
-
-        foreach ($groupids as $groupid) {
-            $groupsmenu[$groupid] = groups_get_group_name($groupid);
-            popup_form($urlroot.'&amp;group=', $groupsmenu, 'selectgroup', 
-                $currentgroup, '', '', '', false, 'self');
-        }
-    }
-}
-
-/**
- * Gets the group that a student has selected from the drop-down menu printed
- * by groups_m_print_group_selector and checks that the student has the 
- * specified permission for the group and that the group is one of the groups
- * assigned for this module instance.
- * 
- * Groups selected are saved between page changes within the module instance but 
- * not necessarily if the user leaves the instance e.g. returns to the main 
- * course page. If the selector has not been printed anywhere during the user's 
- * 'visit' to the module instance, then the function returns false. This means 
- * that you need to be particularly careful about pages that might be 
- * bookmarked by the user.  
- * 
- * @uses $USER   
- * @param int $cmid The id of the module instance
- * @param int $permissiontype The permission type - see note on permission types 
- * above
- * @param int $userid The id of the user, defaults to the current user 
- * @return boolean True if no error occurred, false otherwise.
- * 
- * TO DO - make this and other functions default to current user  
- */
-function groups_m_get_selected_group($cmid, $permissiontype, $userid) {
-    $currentgroup = optional_param('group');
-    if (!$currentgroup) {
-        $groupids = groups_get_groups_for_user();
-    }
-    // Get it from the session variable, otherwise get it from the form, otherwise
-    // Get it from the database as the first group. 
-    // Then set the  group in the session variable to make it easier to get next time.
-}
-
-/**
- * Gets an array of the group IDs of all groups for the user in this course module.
- * @uses $USER     
- * @param object $cm The course-module object.
- * @param int $userid The ID of the user.
- * @return array An array of group IDs, or false. 
- */
-function groups_m_get_groups_for_user($cm, $userid) {
-//echo 'User'; print_object($cm);
-    $groupingid = groups_get_grouping_for_coursemodule($cm);
-    if (!$groupingid || GROUP_NOT_IN_GROUPING == $groupingid) {
-        return false;
-    }
-    if (!isset($cm->course) || !groupmode($cm->course, $cm)) {
-        return false;
-    }
-    elseif (GROUP_ANY_GROUPING == $groupingid) {
-        return groups_get_groups_for_user($userid, $cm->course);
-    }
-    return groups_get_groups_for_user_in_grouping($userid, $groupingid);
-} 
-
-
-/**
- * Get the ID of the first group for the global $USER in the course module.
- * Replaces legacylib function 'mygroupid'.
- * @uses $USER
- * @param $cm A course module object.
- * @return int A single group ID for this user.
- */ 
-function groups_m_get_my_group($cm) {
-    global $USER;
-    $groupids = groups_m_get_groups_for_user($cm, $USER->id);
-    if (!$groupids || count($groupids) == 0) {
-        return 0;
-    }
-    return array_shift($groupids);
-}
-
-
-/**
- * Indicates if a specified user has a particular type of permission for a 
- * particular group for this module instance.
- * @uses $USER      
- * @param int $cmid The id of the module instance. This is necessary because the 
- * same group can be used in different module instances with different 
- * permission setups. 
- * @param int $groupid The id of the group
- * @param int $permissiontype The permission type - see note on permission types 
- * above
- * @userid int $userid The id of the user, defaults to the current user
- * @return boolean True if the user has the specified permission type, false 
- * otherwise or if an error occurred. 
- */
-function groups_m_has_permission($cm, $groupid, $permissiontype, $userid = null) {
-    if (!$userid) {
-        global $USER;
-        $userid = $USER->id;
-    }
-    $groupingid = groups_get_grouping_for_coursemodule($cm);
-    if (!$groupingid || !is_object($cm) || !isset($cm->course)) {
-        return false;
-    }
-    $courseid = $cm->course;
-    $isstudent = isstudent($courseid, $userid);
-    $isteacher = isteacher($courseid, $userid);
-    $groupmember = groups_is_member($groupid, $userid);
-    $memberofsomegroup = groups_is_member_of_some_group_in_grouping($userid, $groupingid);
-
-    $groupingsettings = groups_get_grouping_settings($groupingid);
-    $viewowngroup = $groupingsettings->viewowngroup;
-    $viewallgroupsmembers = $groupingsettings->viewallgroupmembers;
-    $viewallgroupsactivities = $groupingsettings->viewallgroupsactivities;
-    $teachersgroupsmark = $groupingsettings->teachersgroupsmark;
-    $teachersgroupsview = $groupingsettings->teachersgroupsview;
-    $teachersgroupmark = $groupingsettings->teachersgroupmark;
-    $teachersgroupview = $groupingsettings->teachersgroupview;
-    $teachersoverride = $groupingsettings->teachersoverride;
-
-    $permission = false;
-
-    switch ($permissiontype) {
-    case 'view':
-        if (($isstudent and $groupmember) or 
-            ($isteacher and $groupmember) or 
-            ($isstudent and $viewallgroupsactivities) or 
-            ($isteacher and !$teachersgroupview) or 
-            ($isteacher and !$memberofsomegroup and $teachersoverride)) {
-                $permission = true;
-            } 
-        break;
-
-    case 'studentcontribute':
-        if (($isstudent and $groupmember) or 
-            ($isteacher and $groupmember) or 
-            ($isteacher and !$memberofsomegroup and $teachersoverride)) {
-                $permission = true;
-            } 
-        break;
-    case 'teachermark':
-        if (($isteacher and $groupmember) or 
-            ($isteacher and !$teachersgroupmark) or
-            ($isteacher and !$memberofsomegroup and $teachersoverride)) {
-                $permission = true;
-            }  
-        break;
-
-    case 'viewmembers':
-        if (($isstudent and $groupmember and $viewowngroup) or 
-            ($isstudent and $viewallgroupsmembers) or 
-            ($isteacher and $groupmember) or 
-            ($isteacher and !$teachersgroupview) or 
-            ($isteacher and !$memberofsomegroup and $teachersoverride) or 
-            $isteacheredit) {
-                $permission = true;
-            }  
-        break;
-    }
-    return $permission;
-}
-
-/**
- * Gets an array of members of a group that have a particular permission type 
- * for this instance of the module and that are enrolled on the course that
- * the module instance belongs to. 
- * 
- * @param int $cmid The id of the module instance. This is necessary because the 
- * same group can be used in different module instances with different 
- * permission setups. 
- * @param int $groupid The id of the group
- * @param int $permissiontype The permission type - see note on permission types 
- * above
- * @return array An array containing the ids of the users with the specified 
- * permission. 
- */
-function groups_m_get_members_with_permission($cmid, $groupid, 
-                                              $permissiontype) {
-    // Get all the users as $userid
-    $validuserids = array();
-    foreach($validuserids as $userid) {
-        $haspermission = groups_m_has_permission($cmid, $groupid, 
-            $permissiontype, $userid);
-        if ($haspermission) {
-            array_push($validuserids, $userid);
-        }
-    }
-    return $validuserids;
-                                              }
-
-/**
- * Gets the group object associated with a group id. This group object can be 
- * used to get information such as the name of the group and the file for the 
- * group icon if it exists. (Look at the groups table in the database to see
- * the fields). 
- * @param int $groupid The id of the group
- * @return group The group object 
- */
-function groups_m_get_group($groupid) {
-    return groups_db_m_get_group($groupid);
-}
-
-/**
- * Gets the groups for the module instance. In general, you should use 
- * groups_m_get_groups_for_user, however this function is provided for 
- * circumstances where this function isn't sufficient for some reason. 
- * @param int $cmid The id of the module instance. 
- * @return array An array of the ids of the groups for the module instance 
- */
-function groups_m_get_groups($cmid) {
-    $groupingid = groups_db_get_groupingid($cmid);
-    $groupids = groups_get_groups_in_grouping($groupingid);
-    return $groupids;
-}
-
-/**
- * Gets the members of group that are enrolled on the course that the specified
- * module instance belongs to. 
- * @param int $cmid The id of the module instance
- * @param int $groupid The id of the group
- * @return array An array of the userids of the members. 
- */
-function groups_m_get_members($cmid, $groupid) {
-    $userids = groups_get_members($groupid, $membertype);
-    if (!$userids) {
-        $memberids = false;
-    } else {
-        // Check if each user is enrolled on the course @@@ TO DO 
-    }
-    return $memberids;
-}
-
-/**
- * Stores a current group in the user's session, if not already present.
- *
- * Current group applies to all modules in the current course that share 
- * a grouping (or use no grouping).
- *
- * This function allows the user to change group if they want, but it
- * checks they have permissions to access the new group and calls error()
- * otherwise.
- * @param object $cm Course-module object
- * @param int $groupmode Group mode
- * @param int $changegroup If specified, user wants to change to this group
- * @return Group ID
- */
-function groups_m_get_and_set_current($cm, $groupmode, $changegroup=-1) {
-    // Check group mode is turned on
-    if (!$groupmode) {
-        return false;
-    }
-
-    // Get current group and return it if no change requested
-    $currentgroupid = groups_m_get_current($cm);
-    if ($changegroup<0) {
-        return $currentgroupid;
-    }
-
-    // Check 'all groups' access
-    $context = get_context_instance(CONTEXT_COURSE, $cm->course);
-    $allgroups = has_capability('moodle/site:accessallgroups', $context);
-
-    // 0 is a special case for 'all groups'.
-    if ($changegroup==0) {
-        if ($groupmode!=VISIBLEGROUPS && !$allgroups) {
-            error('You do not have access to view all groups');
-        }
-    } else { // Normal group specified
-        // Check group is in the course...
-        if (!groups_group_belongs_to_course($changegroup, $cm->course)) {
-            error('Requested group is not in this course.');
-        }
-        // ...AND in the right grouping if required...
-        if ($cm->groupingid && !groups_belongs_to_grouping($changegroup, $cm->groupingid)) {
-            print_object($cm);
-            print_object(groups_get_group($changegroup));
-            error('Requested group is not in this grouping.');
-        }
-        // ...AND user has access to all groups, or it's in visible groups mode, or 
-        // user is a member.
-        if (!$allgroups &&
-          $groupmode != VISIBLEGROUPS && !groups_is_member($changegroup)) {
-        }
-    }
-    // OK, now remember this group in session
-    global $SESSION;
-    $SESSION->currentgroupinggroup[$cm->course][$cm->groupingid] = $changegroup;
-    return $changegroup;
-}
-
-/**
- * Obtains the current group (see groups_m_get_and_set_current) either as an ID or object.
- * @param object $cm Course-module object
- * @param bool $full If true, returns group object rather than ID
- * @return mixed Group ID (default) or object
- */
-function groups_m_get_current($cm, $full=false) {
-    global $SESSION;
-    if (isset($SESSION->currentgroupinggroup[$cm->course][$cm->groupingid])) {
-        $currentgroup = $SESSION->currentgroupinggroup[$cm->course][$cm->groupingid];
-    } else {
-        global $USER;
-        if ($cm->groupingid) {
-            $mygroupids=groups_get_groups_for_user_in_grouping($USER->id, $cm->groupingid);
-        } else {
-            $mygroupids=groups_get_groups_for_user($USER->id, $cm->course);
-        }
-        if (!$mygroupids) {
-            return false;
-        }
-        $currentgroup = array_shift($mygroupids);
-        $SESSION->currentgroupinggroup[$cm->course][$cm->groupingid]=$currentgroup;
-    }
-    if ($full) {
-        return groups_groupid_to_group($currentgroup);
-    } else {
-        return $currentgroup;
-    }
-}
-
-?>
Index: group/lib/automaticgroupinglib.php
===================================================================
RCS file: group/lib/automaticgroupinglib.php
diff -N group/lib/automaticgroupinglib.php
--- group/lib/automaticgroupinglib.php	9 May 2007 00:06:30 -0000	1.3
+++ /dev/null	1 Jan 1970 00:00:00 -0000
@@ -1,251 +0,0 @@
-<?PHP  
-
-/************************************
- * Automatic group generataion
- ************************************/
- 
- // @@@ TO DO - Some of this code could be simplified a lot I realised 
- // after I wrote it and got it working! 
- 
-/**
- * Seeds the random number generator used by groups_create_automatic_grouping. 
- * This must be called before using groups_create_automatic_grouping and should 
- * only be called once in each script even if you are calling 
- * groups_create_automatic_grouping more than once.
- */ 
-function groups_seed_random_number_generator() { 
-    $seed = (double)microtime()*1234567 ;
-    srand($seed);
-}
- 
-
-/**
- * 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;
-                                          }
-
-
-/**
- * Takes an array and a set size, puts the elements of the array into an array 
- * of arrays of size $setsize randomly. 
- * @param array $array The array to distribute into random sets
- * @param int $setsize The size of each set - this can be set to false, if you 
- * would prefer to specify the number of sets.  
- * @param int $nosets The number of sets - this can be set to false if you would 
- * prefer to specify the setsize. 
- * If both $setsize and $nosets are set then $setsize takes precedence. If both 
- * are set to false then the function does nothing and returns false.  
- * @param $distribevenly boolean Determines how extra elements are distributed 
- * if $setsize doesn't divide exactly into the number of elements if $setsize is 
- * specified. If it is true then extra elements will be distributed evenly 
- * amongst the sets, whereas if it is set to false then the remaining elements 
- * will be put into a separate set. 
- * @param boolean $randomise If this is true then the elements of array will be 
- * put into the arrays in a random order, otherwise they will be put into the 
- * array in the same order as the original array. 
- * @return array The array of arrays of elements generated. 
- */
-function groups_distribute_in_random_sets($array, $setsize, $nosets, 
-                                          $distribevenly = true, 
-                                          $randomise = true) {
-    $noelements = count($array);    
-
-    // Create a list of the numbers 1,..., $noelements, in either random order 
-    // or in numerical order depending on whether $randomise has been set.    
-    if ($randomise) {
-        $orderarray = groups_random_list($noelements);
-    } else {
-        // Just create the array (1,2,3,....)
-        $orderarray = array();
-        for($i = 0; $i < $noelements; $i++) {
-            array_push($orderarray, $i);
-        }
-    }
-
-    // Now use the ordering in $orderarray to generate the new arrays
-    $arrayofrandomsets = array(); // 
-
-    for ($i = 0; $i < $noelements; $i++) {      
-        $arrayofrandomsets[$arrayno][$i] = $array[$orderarray[$i]];
-        if (groups_last_element_in_set($noelements, $setsize, $nosets, 
-            $distribevenly, $i) 
-            and $i != $noelements - 1) {
-                $arrayno++;
-                $arrayofrandomsets[$arrayno] = array(); 
-            }
-
-    }
-
-    return  $arrayofrandomsets;
-                                          }
-
-/**
- * Returns an array of the numbers 0,..,$size - 1 in random order 
- * @param int $size the number of numbers to return in a random order 
- * @return array The array of numbers in a random order
- */
-function groups_random_list($size) {
-    $orderarray = array();
-    $noelementsset = 0;
-    while($noelementsset != $size) {
-        $newelement = rand() % $size;
-        // If the array doesn't already contain the element, add it.
-        if (array_search($newelement, $orderarray) === false) {
-            array_push($orderarray, $newelement);
-            $noelementsset++;
-        }
-    }
-
-    return $orderarray;
-}
-
-/**
- * A helper function for groups_distribute_in_random_sets(). 
- * When distributing elements into sets, determines if a given element is the 
- * last element in the set. 
- * @param int $totalnoelements The total number of elements being distributed. 
- * @param int $setsize See groups_distribute_in_random_sets()
- * @param int $nosets See groups_distribute_in_random_sets()
- * @param boolean $distribevenly  See groups_distribute_in_random_sets()
- * @param int $elementno The element number that we are considering i.e. if this 
- * is the 15th element then this would be 15. 
- * @return boolean True if the element under consideration would be the last 
- * element in the set, fals otherwise. 
- */
-function groups_last_element_in_set($totalnoelements, $setsize, $nosets, 
-                                    $distribevenly, $elementno) {
-    $lastelement = false;
-    $elementno = $elementno + 1; // Counting from zero is just too confusing! 
-
-    // If $nosets has been specified, make sure $setsize is set to the right 
-    // value, so that we can treat the two cases identically. Then work out how 
-    // many extra elements will be left over. 
-    if (!$setsize) {
-        $setsize = floor($totalnoelements / $nosets);
-        $noextra = $totalnoelements % $nosets;
-    } else {
-        $noextra = $totalnoelements % $setsize;
-    }
-
-    if (!$distribevenly) {
-        // If we're putting our extra elements in a set at the end, everything 
-        // is easy!
-        if ($elementno % $setsize == 0) {
-            $lastelement = true;
-        }
-    } else {
-        // Work out the number of elements that will be in the bigger sets that 
-        // have the leftover elements in 
-        // them.
-        $noinbiggersets = $noextra * ($setsize + 1);
-        // Work out if this element is a last element in a set or not - we need 
-        // to separate the case where the element is one of the ones that goes 
-        // into the bigger sets at the beginning 
-        // and the case where it's one of the elements in the normal sized sets. 
-        if (($elementno <= $noinbiggersets and $elementno % ($setsize + 1) == 0) 
-            or ($elementno > $noinbiggersets  and 
-                ($elementno - $noinbiggersets ) % $setsize == 0) ) {
-                   $lastelement = true;
-        }
-    }
-
-    return $lastelement;
-                                    }
-
-?>
Index: group/lib/utillib.php
===================================================================
RCS file: /cvsroot/moodle/moodle/group/lib/utillib.php,v
retrieving revision 1.15
diff -u -r1.15 utillib.php
--- group/lib/utillib.php	14 Aug 2007 00:50:22 -0000	1.15
+++ group/lib/utillib.php	15 Aug 2007 13:41:51 -0000
@@ -301,123 +301,5 @@
     return false;
 }
 
-/**
- * Return the address for the group settings page.
- * (For /user/index.php etc.)
- * @param $courseid
- * @param $groupid
- * @param $groupingid Default false, or optionally a grouping ID.
- * @param $html Default true for HTML pages, eg. on error. False for HTTP redirects.
- * @param $param Extra parameters.
- * @return string An absolute URL.
- */
-function groups_group_edit_url($courseid, $groupid, $groupingid=false, $html=true, $param=false) {
-    global $CFG;
-    $html ? $sep = '&amp;' : $sep = '&';
-    $url = $CFG->wwwroot.'/group/edit.php?courseid='.$courseid;
-    if ($groupid) {
-        $url .= $sep.'id='.$groupid;
-    }
-    if ($groupingid) {
-        $url .= $sep.'grouping='.$groupingid;
-    }
-    if ($param) {
-        $url .= $sep.$param;
-    }
-    return $url;
-}
-
-/** 
- * Return the address for the grouping settings page - Internal group use only.
- * @param $courseid
- * @param $groupingid Default false, or optionally a grouping ID.
- * @param $html Default true for HTML pages, eg. on error. False for HTTP redirects.
- * @param $param Extra parameters.
- * @return string An absolute URL.
- */
-function groups_grouping_edit_url($courseid, $groupingid=false, $html=true, $param=false) {
-    global $CFG;
-    $html ? $sep = '&amp;' : $sep = '&';
-    $url = $CFG->wwwroot.'/group/grouping.php?courseid='.$courseid;
-    if ($groupingid) {
-        $url .= $sep.'id='.$groupingid;
-    }
-    if ($param) {
-        $url .= $sep.$param;
-    }
-    return $url;
-}
-
-/**
- * Return the address for the add/remove users page - Internal group use only.
- * @param $courseid
- * @param $groupid
- * @param $groupingid Default false, or optionally a grouping ID.
- * @param $html Default true for HTML pages, eg. on error. False for HTTP redirects.
- * @return string An absolute URL.
- */
-function groups_members_add_url($courseid, $groupid, $groupingid=false, $html=true) {
-    global $CFG;
-    $html ? $sep = '&amp;' : $sep = '&';
-    $url = $CFG->wwwroot.'/group/assign.php?courseid='.$courseid.$sep.'group='.$groupid;
-    if ($groupingid) {
-        $url .= $sep.'grouping='.$groupingid;
-    }
-    return $url;
-}
-
-/**
- * Return the address for the main group management page. (For admin block etc.)
- * @param $courseid
- * @param $groupid Default false, or optionally a group ID.
- * @param $groupingid Default false, or optionally a grouping ID.
- * @param $html Default true for HTML pages, eg. on error. False for HTTP redirects.
- * @return string An absolute URL.
- */
-function groups_home_url($courseid, $groupid=false, $groupingid=false, $html=true) {
-    global $CFG;
-    $html ? $sep = '&amp;' : $sep = '&';
-    $url = $CFG->wwwroot.'/group/index.php?id='.$courseid;
-    if ($groupid) {
-        $url .= $sep.'group='.$groupid;
-    }
-    if ($groupingid) {
-        $url .= $sep.'grouping='.$groupingid;
-    }
-    return $url;
-}
-
-/**
- * Returns the first button action with the given prefix, taken from
- * POST or GET, otherwise returns false.
- * See /lib/moodlelib.php function optional_param.
- * @param $prefix 'act_' as in 'action'.
- * @return string The action without the prefix, or false if no action found.
- */
-function groups_param_action($prefix = 'act_') {
-    $action = false;
-//($_SERVER['QUERY_STRING'] && preg_match("/$prefix(.+?)=(.+)/", $_SERVER['QUERY_STRING'], $matches)) { //b_(.*?)[&;]{0,1}/
-
-    if ($_POST) {
-        $form_vars = $_POST;
-    }
-    elseif ($_GET) {
-        $form_vars = $_GET; 
-    }
-    if ($form_vars) {
-        foreach ($form_vars as $key => $value) {
-            if (preg_match("/$prefix(.+)/", $key, $matches)) {
-                $action = $matches[1];
-                break;
-            }
-        }
-    }
-    if ($action && !preg_match('/^\w+$/', $action)) {
-        $action = false;
-        error('Action had wrong type.');
-    }
-    ///if (debugging()) echo 'Debug: '.$action;
-    return $action;
-}
 
 ?>
Index: group/lib/basicgrouplib.php
===================================================================
RCS file: /cvsroot/moodle/moodle/group/lib/basicgrouplib.php,v
retrieving revision 1.19
diff -u -r1.19 basicgrouplib.php
--- group/lib/basicgrouplib.php	14 Aug 2007 06:41:21 -0000	1.19
+++ group/lib/basicgrouplib.php	15 Aug 2007 13:41:50 -0000
@@ -149,90 +149,6 @@
     return false;
 }
 
-/**
- * Gets the users for a course who are not in a specified group
- * @param int $groupid The id of the group
- * @param string searchtext similar to searchtext in role assign, search
- * @return array An array of the userids of the non-group members,  or false if 
- * an error occurred.  
- * This function was changed to get_users_by_capability style
- * mostly because of the searchtext requirement
- */
-function groups_get_users_not_in_group($courseid, $groupid, $searchtext='') {
-    
-    global $CFG;
-
-    $context = get_context_instance(CONTEXT_COURSE, $courseid);
-
-    if ($searchtext !== '') {   // Search for a subset of remaining users
-        $LIKE      = sql_ilike();
-        $FULLNAME  = sql_fullname();
-        $wheresearch = " AND u.id IN (SELECT id FROM {$CFG->prefix}user WHERE $FULLNAME $LIKE '%$searchtext%' OR email $LIKE '%$searchtext%' )";
-    } else {
-        $wheresearch = ''; 
-    }
-    
-    $capability = 'moodle/course:view';
-    $doanything = false;
-
-    // find all possible "student" roles
-    if ($possibleroles = get_roles_with_capability($capability, CAP_ALLOW, $context)) {
-        if (!$doanything) {
-            if (!$sitecontext = get_context_instance(CONTEXT_SYSTEM)) {
-                return false;    // Something is seriously wrong
-            }
-            $doanythingroles = get_roles_with_capability('moodle/site:doanything', CAP_ALLOW, $sitecontext);
-        }
-
-        $validroleids = array();
-        foreach ($possibleroles as $possiblerole) {
-            if (!$doanything) {
-                if (isset($doanythingroles[$possiblerole->id])) {  // We don't want these included
-                    continue;
-                }
-            }
-            if ($caps = role_context_capabilities($possiblerole->id, $context, $capability)) { // resolved list
-                if (isset($caps[$capability]) && $caps[$capability] > 0) { // resolved capability > 0
-                    $validroleids[] = $possiblerole->id;
-                }
-            }
-        }
-        if (empty($validroleids)) {
-            return false;
-        }
-        $roleids =  '('.implode(',', $validroleids).')';
-    } else {
-        return false;  // No need to continue, since no roles have this capability set
-    }
-
-/// Construct the main SQL
-    $select = " SELECT u.id, u.firstname, u.lastname";
-    $from   = " FROM {$CFG->prefix}user u
-                INNER JOIN {$CFG->prefix}role_assignments ra ON ra.userid = u.id
-                INNER JOIN {$CFG->prefix}role r ON r.id = ra.roleid";
-    $where  = " WHERE ra.contextid ".get_related_contexts_string($context)."
-                  AND u.deleted = 0
-                  AND ra.roleid in $roleids
-                  AND u.id NOT IN (SELECT userid 
-                                   FROM {$CFG->prefix}groups_members 
-                                   WHERE groupid = $groupid)
-                  $wheresearch";
-
-    return get_records_sql($select.$from.$where);;
-}
-
-/**
- * Given two users, determines if there exists a group to which they both belong
- * @param int $userid1 The id of the first user
- * @param int $userid2 The id of the second user
- * @return boolean True if the users are in a common group, false otherwise or 
- * if an error occurred. 
- */
-function groups_users_in_common_group($userid1, $userid2) {
-    return groups_db_users_in_common_group($userid1, $userid2); 
-}
-
-
 /*****************************
    Membership functions 
  *****************************/
@@ -248,16 +164,6 @@
     return groups_db_group_exists($groupid);
 }
 
-
-/**
- * Determine if a course ID, group name and description match a group in the database.
- *   For backup/restorelib.php
- * @return mixed A group-like object with $group->id, or false.
- */
-function groups_group_matches($courseid, $grp_name, $grp_description) {
-    return groups_db_group_matches($courseid, $grp_name, $grp_description);
-}
-
 /**
  * Determine if a course ID, and group name match a group in the database.
  * @return mixed A group-like object with $group->id, or false.
@@ -425,19 +331,6 @@
 
 
 /**
- * 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. 
- */
-function groups_delete_group($groupid) {
-    $groupdeleted = groups_db_delete_group($groupid);
-
-    return $groupdeleted;
-}
-
-
-/**
  * 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
Index: group/lib/groupinglib.php
===================================================================
RCS file: /cvsroot/moodle/moodle/group/lib/groupinglib.php,v
retrieving revision 1.19
diff -u -r1.19 groupinglib.php
--- group/lib/groupinglib.php	14 Aug 2007 00:50:21 -0000	1.19
+++ group/lib/groupinglib.php	15 Aug 2007 13:41:50 -0000
@@ -31,19 +31,6 @@
 }
 
 
-function groups_get_grouping_records($courseid) {
-    global $CFG;
-    if (! $courseid) {
-        return false;
-    }
-    $sql = "SELECT gg.*
-        FROM {$CFG->prefix}groups_groupings gg
-        INNER JOIN {$CFG->prefix}groups_courses_groupings cg ON gg.id = cg.groupingid
-        WHERE cg.courseid = '$courseid'";
-    $groupings = get_records_sql($sql);
-    return $groupings;
-}
-
 /**
  * Gets a list of the group IDs in a specified grouping
  * @param int $groupingid The id of the grouping
@@ -55,32 +42,6 @@
 }
 
 /**
- * Gets data linking a grouping to each group it contains.
- * @param int $groupingid The ID of the grouping.
- * @return array | false An array of grouping-group records, or false on error.
- */
-function groups_get_groups_in_grouping_records($groupingid) {
-    if (! $groupingid) {
-        return false;
-    }
-    $grouping_groups = get_records('groupings_groups', 'groupingid ', 
-                              $groupingid, '', $fields='id, groupid, timeadded');
-
-    return $grouping_groups;
-}
-
-
-/** 
- * Gets the groupings that a group belongs to 
- * @param int $groupid The id of the group
- * @return array An array of the ids of the groupings that the group belongs to, 
- * or false if there are none or if an error occurred. 
- */
-function groups_get_groupings_for_group($groupid) {
-    return groups_db_get_groupings_for_group($groupid);
-}
-
-/**
  * Gets the information about a specified grouping
  * @param int $groupingid
  * @return object The grouping settings object - properties are name and 
@@ -91,16 +52,6 @@
 }
 
 /**
- * Set information about a grouping
- * @param int $groupingid The grouping to update the info for.
- * @param object $groupingsettings 
- */
-function groups_set_grouping_settings($groupingid, $groupingsettings) {
-    return groups_db_set_grouping_settings($groupingid, $groupingsettings);
-}
-
-
-/**
  * Gets the name of a grouping with a specified ID
  * @param int $groupid The grouping ID.
  * @return string The name of the grouping.
@@ -119,42 +70,6 @@
     return false;
 }
 
-
-/**
- * Get array of group IDs for the user in a grouping.
- * @param int $userid
- * @param int $groupingid
- * @return array If the user has groups an array of group IDs, else false.
- */
-function groups_get_groups_for_user_in_grouping($userid, $groupingid) {
-    global $CFG;
-    $sql = "SELECT gg.groupid
-        FROM {$CFG->prefix}groupings_groups gg
-        INNER JOIN {$CFG->prefix}groups_members gm ON gm.groupid = gg.groupid
-        WHERE gm.userid = '$userid'
-        AND gg.groupingid = '$groupingid'";
-    $records = get_records_sql($sql);
-
-//print_object($records);
-    return groups_groups_to_groupids($records);
-}
-
-/**
- * Gets a list of the groups not in a specified grouping
- * @param int $groupingid The grouping specified
- * @return array An array of the group ids
- */
-function groups_get_groups_not_in_grouping($groupingid, $courseid) {
-    $allgroupids = groups_get_groups($courseid);
-    $groupids = array();
-    foreach($allgroupids as $groupid) {
-        if (!groups_belongs_to_grouping($groupid, $groupingid)) {
-            array_push($groupids, $groupid);
-        }
-    }
-    return $groupids;
-}
-
 /**
  * Gets a list of the groups not in any grouping, but in this course.
  * TODO: move to dbgroupinglib.php
@@ -182,115 +97,6 @@
     return $groupids;
 }
 
-/**
- * Gets the users for the course who are not in any group of a grouping.
- * @param int $courseid The id of the course
- * @param int $groupingid The id of the grouping
- * @param int $groupid Excludes members of a particular group
- * @return array An array of the userids of the users not in any group of 
- * the grouping or false if an error occurred. 
- */
-function groups_get_users_not_in_any_group_in_grouping($courseid, $groupingid, 
-    $groupid = false) {
-        $users = get_course_users($courseid);
-        $userids = groups_users_to_userids($users); 
-        $nongroupmembers = array();
-        if (! $userids) {
-            return $nongroupmembers;
-        }
-        foreach($userids as $userid) {
-            if (!groups_is_member_of_some_group_in_grouping($userid, $groupingid)) {
-                // If a group has been specified don't include members of that group
-                if ($groupid  and !groups_is_member($userid, $groupid)) {
-                    array_push($nongroupmembers, $userid);
-                } else {
-                    ///array_push($nongroupmembers, $userid);
-                }
-            }
-        }
-        return $nongroupmembers;
-    }
-
-
-/**
- * Determines if a user is in more than one group in a grouping
- * @param int $userid The id of the user
- * @param int $groupingid The id of the grouping
- * @return boolean True if the user is in more than one group, false otherwise 
- * or if an error occurred. 
- */
-function groups_user_is_in_multiple_groups($userid, $groupingid) {
-    $inmultiplegroups = false;
-    //TODO: $courseid?
-    $groupids = groups_get_groups_for_user($courseid);
-    if ($groupids != false) {
-        $groupinggroupids = array();
-        foreach($groupids as $groupid) {
-            if (groups_belongs_to_grouping($groupid, $groupingid)) {
-                array_push($groupinggroupids, $groupid);
-            }
-        }
-        if (count($groupinggroupids) > 1) {
-            $inmultiplegroups = true;
-        }
-    }
-    return $inmultiplegroups;
-}
-
-
-/**
- * Returns an object with the default grouping settings values - these can of 
- * course be overridden if desired.
- * Can also be used to set the default for any values not set
- * @return object The grouping settings object. 
- */
-function groups_set_default_grouping_settings($groupingsettings = null) {
-        
-    if (!isset($groupingsettings->name)) {
-        $groupingsettings->name = 'Temporary Grouping Name';
-    }
-
-    if (!isset($groupingsettings->description)) {
-        $groupingsettings->description = '';
-    }
-
-    if (!isset($groupingsettings->viewowngroup)) {
-        $groupingsettings->viewowngroup = 1;
-    }
-
-    if (!isset($groupingsettings->viewallgroupsmembers)) {
-        $groupingsettings->viewallgroupsmembers = 0;
-    }
-
-    if (!isset($groupingsettings->viewallgroupsactivities)) {
-        $groupingsettings->viewallgroupsactivities = 0;
-    }
-
-    if (!isset($groupingsettings->teachersgroupmark)) {
-        $groupingsettings->teachersgroupmark = 0;
-    }  
-
-    if (!isset($groupingsettings->teachersgroupview)) {
-        $groupingsettings->teachersgroupview = 0;
-    }               
-
-    if (!isset($groupingsettings->teachersoverride)) {
-        $groupingsettings->teachersoverride = 1;
-    }  
-
-    return $groupingsettings;
-}
-
-
-/**
- * Gets the grouping ID to use for a particular instance of a module in a course
- * @param int $coursemoduleid The id of the instance of the module in the course
- * @return int The id of the grouping or false if there is no such id recorded 
- * or if an error occurred. 
- */
-function groups_get_grouping_for_coursemodule($coursemodule) {
-    return groups_db_get_grouping_for_coursemodule($coursemodule);
-}
 
 /*****************************
         Membership functions  
@@ -306,26 +112,6 @@
     return groups_db_grouping_exists($groupingid);
 }
 
-/**
- * Determine if a course ID, grouping name and description match a grouping in the database.
- *   For backup/restorelib.php
- * @return mixed A grouping-like object with $grouping->id, or false.
- */
-function groups_grouping_matches($courseid, $gg_name, $gg_description) {
-    global $CFG;
-    $sql = "SELECT gg.id, gg.name, gg.description
-        FROM {$CFG->prefix}groups_groupings gg
-        INNER JOIN {$CFG->prefix}groups_courses_groupings cg ON gg.id = cg.groupingid
-        WHERE gg.name = '$gg_name'
-        AND gg.description = '$gg_description'
-        AND cg.courseid = '$courseid'";
-    $records = get_records_sql($sql);
-    $grouping = false;
-    if ($records) {
-        $grouping = array_shift($records);
-    } 
-    return $grouping;
-}
 
 /**
  * Determines if a group belongs to a specified grouping
@@ -339,29 +125,6 @@
 }
 
 
-/**
- * Detemines if a specified user belongs to any group of a specified grouping.
- * @param int $userid The id of the user
- * @param int $groupingid The id of the grouping
- * @return boolean True if the user belongs to some group in the grouping,
- * false otherwise or if an error occurred. 
- */
-function groups_is_member_of_some_group_in_grouping($userid, $groupingid) {
-    return groups_db_is_member_of_some_group_in_grouping($userid, $groupingid);
-}
-
-/** 
- * Determines if a grouping belongs to a specified course
- * @param int $groupingid The id of the grouping
- * @param int $courseid The id of the course
- * @return boolean True if the grouping belongs to the course, false otherwise, 
- * or if an error occurred. 
- */
-function groups_grouping_belongs_to_course($groupingid, $courseid) {
-    return groups_db_grouping_belongs_to_course($groupingid, $courseid);
-}
-
-
 /*****************************
         Creation functions  
 *****************************/
@@ -385,58 +148,6 @@
     return $groupingid;
 }
 
-
-/**
- * Adds a specified group to a specified grouping.
- * @param int $groupid The id of the group
- * @param int $groupingid The id of the grouping
- * @return boolean True if the group was added successfully or the group already 
- * belonged to the grouping, false otherwise. Also returns false if the group 
- * doesn't belong to the same course as the grouping. 
- */
-function groups_add_group_to_grouping($groupid, $groupingid) {
-    if (GROUP_NOT_IN_GROUPING == $groupingid) {
-        return true;
-    }
-    $belongstogrouping = groups_belongs_to_grouping($groupid, $groupingid);
-
-    if (!groups_grouping_exists($groupingid)) {
-        $groupadded = false;
-    } elseif (!$belongstogrouping) {
-        $groupadded = groups_db_add_group_to_grouping($groupid, $groupingid); 
-    } else {
-        $groupadded = true;
-    }
-
-    return $groupadded;  
-}
-
-
-/**
- * Sets the name of a grouping overwriting any current name that the grouping 
- * has
- * @param int $groupingid The id of the grouping specified
- * @param string $name The name to give the grouping
- * @return boolean True if the grouping settings was added successfully, false 
- * otherwise.
- */
-function groups_set_grouping_name($groupingid, $name) {
-    return groups_db_set_grouping_name($groupingid, $name);
-}
-
-
-/**
- * Sets a grouping to use for a particular instance of a module in a course
- * @param int $groupingid The id of the grouping
- * @param int $coursemoduleid The id of the instance of the module in the course
- * @return boolean True if the operation was successful, false otherwise
- */
-function groups_set_grouping_for_coursemodule($groupingid, $coursemoduleid) {
-    return groups_db_set_grouping_for_coursemodule($groupingid, 
-                                                        $coursemoduleid);
-}
-
-
 /*****************************
         Update functions  
  *****************************/
@@ -463,21 +174,6 @@
  *****************************/
 
 /** 
- * 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
- * @return boolean True if the deletion was successful, false otherwise. 
- */
-function groups_remove_group_from_grouping($groupid, $groupingid) {
-    if (GROUP_NOT_IN_GROUPING == $groupingid) {
-        //Quietly ignore.
-        return true;
-    }
-    return groups_db_remove_group_from_grouping($groupid, $groupingid);
-}
-
-/** 
  * Removes a grouping from a course - note that this function does not delete 
  * any of the groups in the grouping. 
  * @param int $groupingid The id of the grouping
@@ -491,25 +187,5 @@
     
 }
 
-/**
- * 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;
-}
 
 ?>
Index: lib/setup.php
===================================================================
RCS file: /cvsroot/moodle/moodle/lib/setup.php,v
retrieving revision 1.203
diff -u -r1.203 setup.php
--- lib/setup.php	29 Jul 2007 20:48:18 -0000	1.203
+++ lib/setup.php	15 Aug 2007 13:41:58 -0000
@@ -194,6 +194,7 @@
     require_once($CFG->libdir .'/deprecatedlib.php');   // Deprecated functions included for backward compatibility
     require_once($CFG->libdir .'/moodlelib.php');       // Other general-purpose functions
     require_once($CFG->libdir .'/eventslib.php');       // Events functions
+    require_once($CFG->libdir .'/grouplib.php');        // Groups functions
     
 /// Disable errors for now - needed for installation when debug enabled in config.php
     if (isset($CFG->debug)) {
Index: lib/gdlib.php
===================================================================
RCS file: /cvsroot/moodle/moodle/lib/gdlib.php,v
retrieving revision 1.13
diff -u -r1.13 gdlib.php
--- lib/gdlib.php	11 Feb 2006 16:17:06 -0000	1.13
+++ lib/gdlib.php	15 Aug 2007 13:41:51 -0000
@@ -1,7 +1,7 @@
 <?php
 
 /**
- * gdlib.php - Collection of routines in Moodle related to 
+ * gdlib.php - Collection of routines in Moodle related to
  * processing images using GD
  *
  * @author ?
@@ -10,7 +10,7 @@
  * @package moodlecore
  */
 
-/** 
+/**
  * short description (optional)
  *
  * long description
@@ -32,58 +32,75 @@
 
     global $CFG;
 
-    if (function_exists('ImageCopyResampled') and $CFG->gdversion >= 2) { 
+    if (function_exists('ImageCopyResampled') and $CFG->gdversion >= 2) {
        return ImageCopyResampled($dst_img, $src_img, $dst_x, $dst_y, $src_x, $src_y,
                                  $dst_w, $dst_h, $src_w, $src_h);
     }
 
     $totalcolors = imagecolorstotal($src_img);
-    for ($i=0; $i<$totalcolors; $i++) { 
+    for ($i=0; $i<$totalcolors; $i++) {
         if ($colors = ImageColorsForIndex($src_img, $i)) {
             ImageColorAllocate($dst_img, $colors['red'], $colors['green'], $colors['blue']);
         }
     }
 
-    $scaleX = ($src_w - 1) / $dst_w; 
-    $scaleY = ($src_h - 1) / $dst_h; 
+    $scaleX = ($src_w - 1) / $dst_w;
+    $scaleY = ($src_h - 1) / $dst_h;
 
-    $scaleX2 = $scaleX / 2.0; 
-    $scaleY2 = $scaleY / 2.0; 
+    $scaleX2 = $scaleX / 2.0;
+    $scaleY2 = $scaleY / 2.0;
 
-    for ($j = 0; $j < $dst_h; $j++) { 
-        $sY = $j * $scaleY; 
+    for ($j = 0; $j < $dst_h; $j++) {
+        $sY = $j * $scaleY;
 
-        for ($i = 0; $i < $dst_w; $i++) { 
-            $sX = $i * $scaleX; 
-
-            $c1 = ImageColorsForIndex($src_img,ImageColorAt($src_img,(int)$sX,(int)$sY+$scaleY2)); 
-            $c2 = ImageColorsForIndex($src_img,ImageColorAt($src_img,(int)$sX,(int)$sY)); 
-            $c3 = ImageColorsForIndex($src_img,ImageColorAt($src_img,(int)$sX+$scaleX2,(int)$sY+$scaleY2)); 
-            $c4 = ImageColorsForIndex($src_img,ImageColorAt($src_img,(int)$sX+$scaleX2,(int)$sY)); 
-
-            $red = (int) (($c1['red'] + $c2['red'] + $c3['red'] + $c4['red']) / 4); 
-            $green = (int) (($c1['green'] + $c2['green'] + $c3['green'] + $c4['green']) / 4); 
-            $blue = (int) (($c1['blue'] + $c2['blue'] + $c3['blue'] + $c4['blue']) / 4); 
-
-            $color = ImageColorClosest ($dst_img, $red, $green, $blue); 
-            ImageSetPixel ($dst_img, $i + $dst_x, $j + $dst_y, $color); 
-        } 
-    } 
+        for ($i = 0; $i < $dst_w; $i++) {
+            $sX = $i * $scaleX;
+
+            $c1 = ImageColorsForIndex($src_img,ImageColorAt($src_img,(int)$sX,(int)$sY+$scaleY2));
+            $c2 = ImageColorsForIndex($src_img,ImageColorAt($src_img,(int)$sX,(int)$sY));
+            $c3 = ImageColorsForIndex($src_img,ImageColorAt($src_img,(int)$sX+$scaleX2,(int)$sY+$scaleY2));
+            $c4 = ImageColorsForIndex($src_img,ImageColorAt($src_img,(int)$sX+$scaleX2,(int)$sY));
+
+            $red = (int) (($c1['red'] + $c2['red'] + $c3['red'] + $c4['red']) / 4);
+            $green = (int) (($c1['green'] + $c2['green'] + $c3['green'] + $c4['green']) / 4);
+            $blue = (int) (($c1['blue'] + $c2['blue'] + $c3['blue'] + $c4['blue']) / 4);
+
+            $color = ImageColorClosest ($dst_img, $red, $green, $blue);
+            ImageSetPixel ($dst_img, $i + $dst_x, $j + $dst_y, $color);
+        }
+    }
+}
+
+/**
+ * Delete profile images associated with user or group
+ * @param int $id user or group id
+ * @param string $dir type of entity - 'groups' or 'users'
+ * @return boolean success
+ */
+function delete_profile_image($id, $dir='users') {
+    global $CFG;
+
+    require_once $CFG->libdir.'/filelib.php';
+    $location = $CFG->dataroot .'/'. $dir .'/'. $id;
+
+    if (file_exists($location)) {
+        return fulldelete($location);
+    }
+
+    return true;
 }
 
-/** 
+/**
  * Given an upload manager with the right settings, this function performs a virus scan, and then scales and crops
  * it and saves it in the right place to be a "user" or "group" image.
  *
- * @uses $CFG
- * @param int $id description?
- * @param object $uploadmanager description?
- * @param string $dir description?
- * @return boolean
- * @todo Finish documenting this function
+ * @param int $id user or group id
+ * @param object $uploadmanager object referencing the image
+ * @param string $dir type of entity - groups, users, ...
+ * @return boolean success
  */
 function save_profile_image($id, $uploadmanager, $dir='users') {
-// 
+//
 
     global $CFG;
 
@@ -117,31 +134,31 @@
     $originalfile = $uploadmanager->get_new_filepath();
 
     $imageinfo = GetImageSize($originalfile);
-    
+
     if (empty($imageinfo)) {
         if (file_exists($originalfile)) {
             unlink($originalfile);
         }
         return false;
     }
-    
+
     $image->width  = $imageinfo[0];
     $image->height = $imageinfo[1];
     $image->type   = $imageinfo[2];
 
     switch ($image->type) {
-        case 1: 
+        case 1:
             if (function_exists('ImageCreateFromGIF')) {
-                $im = ImageCreateFromGIF($originalfile); 
+                $im = ImageCreateFromGIF($originalfile);
             } else {
                 notice('GIF not supported on this server');
                 unlink($originalfile);
                 return false;
             }
             break;
-        case 2: 
+        case 2:
             if (function_exists('ImageCreateFromJPEG')) {
-                $im = ImageCreateFromJPEG($originalfile); 
+                $im = ImageCreateFromJPEG($originalfile);
             } else {
                 notice('JPEG not supported on this server');
                 unlink($originalfile);
@@ -150,14 +167,14 @@
             break;
         case 3:
             if (function_exists('ImageCreateFromPNG')) {
-                $im = ImageCreateFromPNG($originalfile); 
+                $im = ImageCreateFromPNG($originalfile);
             } else {
                 notice('PNG not supported on this server');
                 unlink($originalfile);
                 return false;
             }
             break;
-        default: 
+        default:
             unlink($originalfile);
             return false;
     }
@@ -171,7 +188,7 @@
         $im1 = ImageCreate(100,100);
         $im2 = ImageCreate(35,35);
     }
-    
+
     $cx = $image->width / 2;
     $cy = $image->height / 2;
 
@@ -187,7 +204,7 @@
     if (function_exists('ImageJpeg')) {
         @touch($CFG->dataroot .'/'. $dir .'/'. $id .'/f1.jpg');  // Helps in Safe mode
         @touch($CFG->dataroot .'/'. $dir .'/'. $id .'/f2.jpg');  // Helps in Safe mode
-        if (ImageJpeg($im1, $CFG->dataroot .'/'. $dir .'/'. $id .'/f1.jpg', 90) and 
+        if (ImageJpeg($im1, $CFG->dataroot .'/'. $dir .'/'. $id .'/f1.jpg', 90) and
             ImageJpeg($im2, $CFG->dataroot .'/'. $dir .'/'. $id .'/f2.jpg', 95) ) {
             @chmod($CFG->dataroot .'/'. $dir .'/'. $id .'/f1.jpg', 0666);
             @chmod($CFG->dataroot .'/'. $dir .'/'. $id .'/f2.jpg', 0666);
@@ -199,8 +216,8 @@
     return 0;
 }
 
-/** 
- * Given a user id this function scales and crops the user images to remove 
+/**
+ * Given a user id this function scales and crops the user images to remove
  * the one pixel black border.
  *
  * @uses $CFG
@@ -210,7 +227,7 @@
 function upgrade_profile_image($id, $dir='users') {
     global $CFG;
 
-    $im = ImageCreateFromJPEG($CFG->dataroot .'/'. $dir .'/'. $id .'/f1.jpg'); 
+    $im = ImageCreateFromJPEG($CFG->dataroot .'/'. $dir .'/'. $id .'/f1.jpg');
 
     if (function_exists('ImageCreateTrueColor') and $CFG->gdversion >= 2) {
         $im1 = ImageCreateTrueColor(100,100);
@@ -219,46 +236,44 @@
         $im1 = ImageCreate(100,100);
         $im2 = ImageCreate(35,35);
     }
-    
-    if (function_exists('ImageCopyResampled') and $CFG->gdversion >= 2) { 
+
+    if (function_exists('ImageCopyResampled') and $CFG->gdversion >= 2) {
         ImageCopyBicubic($im1, $im, 0, 0, 2, 2, 100, 100, 96, 96);
     } else {
         imagecopy($im1, $im, 0, 0, 0, 0, 100, 100);
-                $c = ImageColorsForIndex($im1,ImageColorAt($im1,2,2)); 
-                $color = ImageColorClosest ($im1, $c['red'], $c['green'], $c['blue']); 
-                ImageSetPixel ($im1, 0, 0, $color); 
-                $c = ImageColorsForIndex($im1,ImageColorAt($im1,2,97)); 
-                $color = ImageColorClosest ($im1, $c['red'], $c['green'], $c['blue']); 
-                ImageSetPixel ($im1, 0, 99, $color); 
-                $c = ImageColorsForIndex($im1,ImageColorAt($im1,97,2)); 
-                $color = ImageColorClosest ($im1, $c['red'], $c['green'], $c['blue']); 
-                ImageSetPixel ($im1, 99, 0, $color); 
-                $c = ImageColorsForIndex($im1,ImageColorAt($im1,97,97)); 
-                $color = ImageColorClosest ($im1, $c['red'], $c['green'], $c['blue']); 
-                ImageSetPixel ($im1, 99, 99, $color); 
-        for ($x = 1; $x < 99; $x++) { 
-                $c1 = ImageColorsForIndex($im1,ImageColorAt($im,$x,1)); 
-                $color = ImageColorClosest ($im, $c1['red'], $c1['green'], $c1['blue']); 
-                ImageSetPixel ($im1, $x, 0, $color); 
-                $c2 = ImageColorsForIndex($im1,ImageColorAt($im1,$x,98)); 
-                $color = ImageColorClosest ($im1, $red, $green, $blue); 
-                $color = ImageColorClosest ($im, $c2['red'], $c2['green'], $c2['blue']); 
-                ImageSetPixel ($im1, $x, 99, $color); 
-        } 
-        for ($y = 1; $y < 99; $y++) { 
-                $c3 = ImageColorsForIndex($im1,ImageColorAt($im, 1, $y)); 
-                $color = ImageColorClosest ($im, $red, $green, $blue); 
-                $color = ImageColorClosest ($im, $c3['red'], $c3['green'], $c3['blue']); 
-                ImageSetPixel ($im1, 0, $y, $color); 
-                $c4 = ImageColorsForIndex($im1,ImageColorAt($im1, 98, $y)); 
-                $color = ImageColorClosest ($im, $c4['red'], $c4['green'], $c4['blue']); 
-                ImageSetPixel ($im1, 99, $y, $color); 
-        } 
-    } 
+                $c = ImageColorsForIndex($im1,ImageColorAt($im1,2,2));
+                $color = ImageColorClosest ($im1, $c['red'], $c['green'], $c['blue']);
+                ImageSetPixel ($im1, 0, 0, $color);
+                $c = ImageColorsForIndex($im1,ImageColorAt($im1,2,97));
+                $color = ImageColorClosest ($im1, $c['red'], $c['green'], $c['blue']);
+                ImageSetPixel ($im1, 0, 99, $color);
+                $c = ImageColorsForIndex($im1,ImageColorAt($im1,97,2));
+                $color = ImageColorClosest ($im1, $c['red'], $c['green'], $c['blue']);
+                ImageSetPixel ($im1, 99, 0, $color);
+                $c = ImageColorsForIndex($im1,ImageColorAt($im1,97,97));
+                $color = ImageColorClosest ($im1, $c['red'], $c['green'], $c['blue']);
+                ImageSetPixel ($im1, 99, 99, $color);
+        for ($x = 1; $x < 99; $x++) {
+                $c1 = ImageColorsForIndex($im1,ImageColorAt($im,$x,1));
+                $color = ImageColorClosest ($im, $c1['red'], $c1['green'], $c1['blue']);
+                ImageSetPixel ($im1, $x, 0, $color);
+                $c2 = ImageColorsForIndex($im1,ImageColorAt($im1,$x,98));
+                $color = ImageColorClosest ($im, $c2['red'], $c2['green'], $c2['blue']);
+                ImageSetPixel ($im1, $x, 99, $color);
+        }
+        for ($y = 1; $y < 99; $y++) {
+                $c3 = ImageColorsForIndex($im1,ImageColorAt($im, 1, $y));
+                $color = ImageColorClosest ($im, $c3['red'], $c3['green'], $c3['blue']);
+                ImageSetPixel ($im1, 0, $y, $color);
+                $c4 = ImageColorsForIndex($im1,ImageColorAt($im1, 98, $y));
+                $color = ImageColorClosest ($im, $c4['red'], $c4['green'], $c4['blue']);
+                ImageSetPixel ($im1, 99, $y, $color);
+        }
+    }
     ImageCopyBicubic($im2, $im, 0, 0, 2, 2, 35, 35, 96, 96);
 
     if (function_exists('ImageJpeg')) {
-        if (ImageJpeg($im1, $CFG->dataroot .'/'. $dir .'/'. $id .'/f1.jpg', 90) and 
+        if (ImageJpeg($im1, $CFG->dataroot .'/'. $dir .'/'. $id .'/f1.jpg', 90) and
             ImageJpeg($im2, $CFG->dataroot .'/'. $dir .'/'. $id .'/f2.jpg', 95) ) {
             @chmod($CFG->dataroot .'/'. $dir .'/'. $id .'/f1.jpg', 0666);
             @chmod($CFG->dataroot .'/'. $dir .'/'. $id .'/f2.jpg', 0666);
Index: lib/moodlelib.php
===================================================================
RCS file: /cvsroot/moodle/moodle/lib/moodlelib.php,v
retrieving revision 1.919
diff -u -r1.919 moodlelib.php
--- lib/moodlelib.php	13 Aug 2007 13:58:04 -0000	1.919
+++ lib/moodlelib.php	15 Aug 2007 13:41:57 -0000
@@ -3119,33 +3119,9 @@
     }
 
 /// 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_members($groupid)) {
-                if ($showfeedback) {
-                    notify($strdeleted .' groups_members');
-                }
-            } else {
-                $result = false;
-            }
-            /// Delete any associated context for this group ??
-            delete_context(CONTEXT_GROUP, $groupid);
-
-            if (groups_delete_group($groupid)) {
-                if ($showfeedback) {
-                    notify($strdeleted .' groups');
-                }
-            } else {
-                $result = false;
-            }
-        }
-    }
-/// Delete any groupings.
-    $result = groups_delete_all_groupings($course->id);
-    if ($result && $showfeedback) {
-        notify($strdeleted .' groupings');
-    }
+    require_once($CFG->dirroot.'/group/lib.php');
+    groups_delete_groupings($courseid, true);
+    groups_delete_groups($courseid, true);
 
 /// Delete all related records in other tables that may have a courseid
 /// This array stores the tables that need to be cleared, as
@@ -3222,6 +3198,7 @@
 function reset_course_userdata($data, $showfeedback=true) {
 
     global $CFG, $USER, $SESSION;
+    require_once($CFG->dirroot.'/group/lib.php');
 
     $result = true;
 
@@ -3261,18 +3238,8 @@
                 notify($strdeleted .' '.get_string('students'), 'notifysuccess');
             }
 
-            /// Delete group members (but keep the groups) TODO:check.
-            if ($groupids = groups_get_groups($data->courseid)) {
-                foreach ($groupids as $groupid) {
-                    if (groups_remove_all_group_members($groupid)) {
-                        if ($showfeedback) {
-                            notify($strdeleted .' groups_members', 'notifysuccess');
-                        }
-                    } else {
-                        $result = false;
-                    }
-                }
-            }
+            /// Delete group members (but keep the groups)
+            $result = groups_delete_group_members($data->courseid, $showfeedback) && $result;
         }
 
         if (!empty($data->reset_teachers)) {
@@ -3286,17 +3253,8 @@
     }
 
     if (!empty($data->reset_groups)) {
-        if ($groupids = groups_get_groups($data->courseid)) {
-            foreach ($groupids as $groupid) {
-                if (groups_delete_group($groupid)) {
-                    if ($showfeedback) {
-                        notify($strdeleted .' groups', 'notifysuccess');
-                    }
-                } else {
-                    $result = false;
-                }
-            }
-        }
+            $result = groups_delete_groupings($data->courseid, $showfeedback) && $result;
+            $result = groups_delete_groups($data->courseid, $showfeedback) && $result;
     }
 
     if (!empty($data->reset_events)) {
@@ -3326,18 +3284,6 @@
     return $result;
 }
 
-
-require_once($CFG->dirroot.'/group/lib.php');
-/*TODO: functions moved to /group/lib/legacylib.php
-
-ismember
-add_user_to_group
-mygroupid
-groupmode
-set_current_group
-... */
-
-
 function generate_email_processing_address($modid,$modargs) {
     global $CFG;
 
Index: user/index.php
===================================================================
RCS file: /cvsroot/moodle/moodle/user/index.php,v
retrieving revision 1.180
diff -u -r1.180 index.php
--- user/index.php	31 Jul 2007 20:10:05 -0000	1.180
+++ user/index.php	15 Aug 2007 13:41:59 -0000
@@ -252,7 +252,7 @@
                 echo '</td><td class="content">';
                 echo '<h3>'.$group->name;
                 if (has_capability('moodle/course:managegroups', $context)) {
-                    echo '&nbsp;<a title="'.get_string('editgroupprofile').'" href="'.groups_group_edit_url($course->id, $group->id).'">';
+                    echo '&nbsp;<a title="'.get_string('editgroupprofile').'" href="'.$CFG->wwwroot.'/group/edit.php?id='.$group->id.'&amp;courseid='.$group->courseid.'">';
                     echo '<img src="'.$CFG->pixpath.'/t/edit.gif" alt="'.get_string('editgroupprofile').'" />';
                     echo '</a>';
                 }
Index: lib/grouplib.php
===================================================================
RCS file: lib/grouplib.php
diff -N lib/grouplib.php
--- /dev/null	1 Jan 1970 00:00:00 -0000
+++ lib/grouplib.php	1 Jan 1970 00:00:00 -0000
@@ -0,0 +1,64 @@
+<?php  //$Id:$
+
+
+// Include group.lib for now - it should not be here in the final version
+// the reason is to keep the loaded library files minimal
+
+
+
+// folowing files should be removed later
+require_once($CFG->dirroot.'/group/lib/basicgrouplib.php');
+require_once($CFG->dirroot.'/group/lib/groupinglib.php');
+require_once($CFG->dirroot.'/group/lib/utillib.php');
+require_once($CFG->dirroot.'/group/lib/legacylib.php');
+
+
+
+/******************************************************************************
+ * Groups SQL clauses for modules and core.
+ */
+
+/**
+ * Returns the table in which group members are stored, with a prefix 'gm'.
+ * @return SQL string.
+ */
+function groups_members_from_sql() {
+    global $CFG;
+    return " {$CFG->prefix}groups_members gm ";
+}
+
+/**
+ * Returns a join testing user.id against member's user ID.
+ * Relies on 'user' table being included as 'user u'.
+ * Used in Quiz module reports.
+ * @param group ID, optional to include a test for this in the SQL.
+ * @return SQL string.
+ */
+function groups_members_join_sql($groupid=false) {    
+    $sql = ' JOIN '.groups_members_from_sql().' ON u.id = gm.userid ';
+    if ($groupid) {
+        $sql = "AND gm.groupid = '$groupid' ";
+    }
+    return $sql;
+    //return ' INNER JOIN '.$CFG->prefix.'role_assignments ra ON u.id=ra.userid'.
+    //       ' INNER JOIN '.$CFG->prefix.'context c ON ra.contextid=c.id AND c.contextlevel='.CONTEXT_GROUP.' AND c.instanceid='.$groupid;
+}
+
+/**
+ * Returns SQL for a WHERE clause testing the group ID.
+ * Optionally test the member's ID against another table's user ID column. 
+ * @param groupid
+ * @param userid_sql Optional user ID column selector, example "mdl_user.id", or false.
+ * @return SQL string.
+ */
+function groups_members_where_sql($groupid, $userid_sql=false) {
+    $sql = " gm.groupid = '$groupid' ";
+    if ($userid_sql) {
+        $sql .= "AND $userid_sql = gm.userid ";
+    }
+    return $sql;
+}
+
+
+
+?>
