Index: group/version.php
===================================================================
RCS file: /cvsroot/moodle/moodle/group/version.php,v
retrieving revision 1.5
diff -u -r1.5 version.php
--- group/version.php	22 Jan 2007 17:50:42 -0000	1.5
+++ group/version.php	25 Jan 2007 17:52:01 -0000
@@ -5,7 +5,7 @@
 //  This fragment is called by /admin/index.php
 ////////////////////////////////////////////////////////////////////////////////
 
-$group_version  = 2007012200;
+$group_version  = 2007012400;
 //$module->requires = 2006120400;  // Requires this Moodle version
 
 ?>
Index: group/db/dbbasicgrouplib.php
===================================================================
RCS file: /cvsroot/moodle/moodle/group/db/dbbasicgrouplib.php,v
retrieving revision 1.11
diff -u -r1.11 dbbasicgrouplib.php
--- group/db/dbbasicgrouplib.php	23 Jan 2007 19:11:40 -0000	1.11
+++ group/db/dbbasicgrouplib.php	25 Jan 2007 17:52:01 -0000
@@ -278,7 +278,7 @@
             $record2 = new Object();
 	        $record2->courseid = $courseid;
 	        $record2->groupid = $groupid;
-           if ($copytime) {
+            if ($copytime) {
                 $record2->timeadded = $record->timemodified;
             } else {
                 $record2->timeadded = $now;
@@ -292,6 +292,38 @@
     return $groupid;
 }
 
+/** 
+ * Upgrades a group for a specified course. To preserve the group ID we do a raw insert.
+ * @param int $courseid The course to create the group for
+ * @return int The id of the group created or false if the insert failed.
+ */
+function groups_db_upgrade_group($courseid, $group) {
+    global $CFG;
+    // Check we have a valid course id
+    if (!$courseid || !$group || !isset($group->id)) {
+        return false; 
+    }
+
+    $r = $group;
+    $sql = "INSERT INTO {$CFG->prefix}groups
+        (id,name,description, enrolmentkey,lang,theme,picture,hidepicture, timecreated,timemodified)
+        VALUES ('$r->id','$r->name','$r->description', '$r->enrolmentkey','$r->lang',
+        '$r->theme','$r->picture','$r->hidepicture', '$r->timecreated','$r->timemodified')";
+
+    if ($result = execute_sql($sql)) {
+        $record2 = new Object();
+        $record2->courseid = $courseid;
+        $record2->groupid = $group->id;
+        $record2->timeadded = $group->timemodified;
+
+        $groupadded = insert_record('groups_courses_groups', $record2);
+        if (! $groupadded) {
+            $groupid = false;
+        }
+    }
+    return $group->id;
+}
+
 
 /**
  * Adds a specified user to a group
Index: group/db/upgrade.php
===================================================================
RCS file: /cvsroot/moodle/moodle/group/db/upgrade.php,v
retrieving revision 1.9
diff -u -r1.9 upgrade.php
--- group/db/upgrade.php	22 Jan 2007 20:15:14 -0000	1.9
+++ group/db/upgrade.php	25 Jan 2007 17:52:01 -0000
@@ -26,8 +26,8 @@
     $group_version = '';  // Get code versions
     require_once ("$CFG->dirroot/group/version.php");
 
-    if (empty($CFG->group_version)) {  // New groups have never been installed...
-        $status = false;
+    if (empty($CFG->group_version)) {  // New 1.8 groups have never been installed...
+        $status = true;
 
         $strdatabaseupgrades = get_string('databaseupgrades');
         print_header($strdatabaseupgrades, $strdatabaseupgrades, $strdatabaseupgrades, '', 
@@ -39,25 +39,25 @@
 
         //TODO: for testing, revert to 'old' groups.
         if (! get_config('group_version')) {
-            $status = revert_group_db();
+            $status = $status && groups_revert_db();
         }
 
         //... But Moodle is already installed.
         if (table_exists($t_groups = new XMLDBTable('groups'))) {
-            $status = rename_table($t_groups, 'groups_temp');
-            $status = rename_table(new XMLDBTable('groups_members'), 'groups_members_temp');
+            //$status = $status && rename_table($t_groups, 'groups_temp');
+            //$status = $status && rename_table(new XMLDBTable('groups_members'), 'groups_members_temp');
         }
 
     /// Both old .sql files and new install.xml are supported
     /// but we prioritize install.xml (XMLDB) if present
 
         if (file_exists($CFG->dirroot . '/group/db/install.xml')) {
-            $status = install_from_xmldb_file($CFG->dirroot . '/group/db/install.xml'); //New method
+            $status = $status && install_from_xmldb_file($CFG->dirroot . '/group/db/install.xml'); //New method
         } else if (file_exists($CFG->dirroot . '/group/db/' . $CFG->dbtype . '.sql')) {
-            $status = modify_database($CFG->dirroot . '/group/db/' . $CFG->dbtype . '.sql'); //Old method
+            $status = $status && modify_database($CFG->dirroot . '/group/db/' . $CFG->dbtype . '.sql'); //Old method
         }
 
-        $status = transfer_group_db();
+        $status = $status && groups_transfer_db();
 
         $db->debug = false;
     
@@ -146,13 +146,15 @@
     }
 }
 
-
-function transfer_group_db() {
+/**
+ * Transfer data from old 1.7 to new 1.8 groups tables.
+ */
+function groups_transfer_db() {
     $status = true;
     
     if (table_exists($t_groups = new XMLDBTable('groups_temp'))) {
-        $status = (bool)$groups_r = get_records('groups_temp');
-        $status = (bool)$members_r= get_records('groups_members_temp');
+        $status = $status &&(bool)$groups_r = get_records('groups_temp');
+        $status = $status &&(bool)$members_r= get_records('groups_members_temp');
 
         if (! $groups_r) {
             return $status;
@@ -164,29 +166,125 @@
             $group->enrolmentkey = $group->password;
             ///unset($group->password);
             ///unset($group->courseid);
-            $status = (bool)$newgroupid = groups_create_group($group->courseid, $group);
-            debugging('Create group status: '.$status); //TODO: ?
+            $status = $status &&(bool)$newgroupid = groups_db_upgrade_group($group->courseid, $group);
             if ($members_r) {
                 foreach ($members_r as $member) {
                     if ($member->groupid == $group->id) {
-                        $status = (bool)$memberid = groups_add_member($newgroupid, $member->userid);
+                        $status = $status &&(bool)$memberid = groups_add_member($newgroupid, $member->userid);
                     }
                 }
             }
-            debugging('Add member status: '.$status);
         }
-        ///$status = drop_table($t_groups);
-        ///$status = drop_table(new XMLDBTable('groups_members_temp'));
+        ///$status = $status && drop_table($t_groups);
+        ///$status = $status && drop_table(new XMLDBTable('groups_members_temp'));
+    } else {
+        return false;
     }
     return $status;
 }
 
+/**
+ * Rename groups tables, part of upgrade 2007012400 below.
+ */
+function groups_rename_db($suffix) {
+    $status = true;
+    if (empty($suffix)) {
+        return false;
+    }
+
+    $status = $status && rename_table(new XMLDBTable('groups'), 'groups'.$suffix);
+    $status = $status && rename_table(new XMLDBTable('groups_members'), 'groups_members'.$suffix);
+    $status = $status && rename_table(new XMLDBTable('groups_groupings'), 'groups_groupings'.$suffix);
+    $status = $status && rename_table(new XMLDBTable('groups_courses_groups'), 'groups_courses_groups'.$suffix);
+    $status = $status && rename_table(new XMLDBTable('groups_courses_groupings'), 'groups_courses_groupings'.$suffix);
+    $status = $status && rename_table(new XMLDBTable('groups_groupings_groups'), 'groups_groupings_groups'.$suffix);
+    return $status;
+}
+
+function groups_drop_keys_indexes_db() {
+    $result = true;
+    /// Define index groupid-courseid (unique) to be added to groups_members
+        $table = new XMLDBTable('groups_members');
+        $index = new XMLDBIndex('groupid-courseid');
+        $index->setAttributes(XMLDB_INDEX_UNIQUE, array('groupid', 'userid'));
+
+    /// Launch add index groupid-courseid
+        $result = $result && drop_index($table, $index);
+
+    /// Define key courseid (foreign) to be added to groups_courses_groups
+        $table = new XMLDBTable('groups_courses_groups');
+        $key = new XMLDBKey('courseid');
+        $key->setAttributes(XMLDB_KEY_FOREIGN, array('courseid'), 'course', array('id'));
+
+    /// Launch add key courseid
+        $result = $result && drop_key($table, $key);
+
+    /// Define key groupid (foreign) to be added to groups_courses_groups
+        $key = new XMLDBKey('groupid');
+        $key->setAttributes(XMLDB_KEY_FOREIGN, array('groupid'), 'groups', array('id'));
+
+    /// Launch add key groupid
+        $result = $result && drop_key($table, $key);
+
+    /// Define index courseid-groupid (unique) to be added to groups_courses_groups
+        $index = new XMLDBIndex('courseid-groupid');
+        $index->setAttributes(XMLDB_INDEX_UNIQUE, array('courseid', 'groupid'));
+
+    /// Launch add index courseid-groupid
+        $result = $result && drop_index($table, $index);
+
+    /// Define key courseid (foreign) to be added to groups_courses_groupings
+        $table = new XMLDBTable('groups_courses_groupings');
+        $key = new XMLDBKey('courseid');
+        $key->setAttributes(XMLDB_KEY_FOREIGN, array('courseid'), 'course', array('id'));
+
+    /// Launch add key courseid
+        $result = $result && drop_key($table, $key);
+
+    /// Define key groupingid (foreign) to be added to groups_courses_groupings
+        $key = new XMLDBKey('groupingid');
+        $key->setAttributes(XMLDB_KEY_FOREIGN, array('groupingid'), 'groups_groupings', array('id'));
+
+    /// Launch add key groupingid
+        $result = $result && drop_key($table, $key);
+
+    /// Define index courseid-groupingid (unique) to be added to groups_courses_groupings
+        $index = new XMLDBIndex('courseid-groupingid');
+        $index->setAttributes(XMLDB_INDEX_UNIQUE, array('courseid', 'groupingid'));
+
+    /// Launch add index courseid-groupingid
+        $result = $result && drop_index($table, $index);
+
+    /// Define key groupingid (foreign) to be added to groups_groupings_groups
+        $table = new XMLDBTable('groups_groupings_groups');
+        $key = new XMLDBKey('groupingid');
+        $key->setAttributes(XMLDB_KEY_FOREIGN, array('groupingid'), 'groups_groupings', array('id'));
+
+    /// Launch add key groupingid
+        $result = $result && drop_key($table, $key);
+
+    /// Define key groupid (foreign) to be added to groups_groupings_groups
+        $key = new XMLDBKey('groupid');
+        $key->setAttributes(XMLDB_KEY_FOREIGN, array('groupid'), 'groups', array('id'));
+
+    /// Launch add key groupid
+        $result = $result && drop_key($table, $key);
+
+    /// Define index groupingid-groupid (unique) to be added to groups_groupings_groups
+        $index = new XMLDBIndex('groupingid-groupid');
+        $index->setAttributes(XMLDB_INDEX_UNIQUE, array('groupingid', 'groupid'));
+
+    /// Launch add index groupingid-groupid
+        $result = $result && drop_index($table, $index);
+
+    return $result;
+}
 
 /**
  * For testing, it's useful to be able to revert to 'old' groups.
  */
-function revert_group_db() {
-    $status = false;
+function groups_revert_db($renametemp=true) {
+    $status = true;
     //$status = (bool)$rs = delete_records('config', 'name', 'group_version');
     if (!get_config('group_version') && table_exists(new XMLDBTable('groups_groupings'))) { //debugging()
         $status = drop_table(new XMLDBTable('groups'));
@@ -196,16 +294,17 @@
         $status = drop_table(new XMLDBTable('groups_courses_groupings'));
         $status = drop_table(new XMLDBTable('groups_groupings_groups'));
 
-        $status = rename_table(new XMLDBTable('groups_temp'), 'groups');
-        $status = rename_table(new XMLDBTable('groups_members_temp'), 'groups_members');
+        if ($renametemp) {
+            $status = rename_table(new XMLDBTable('groups_temp'), 'groups');
+            $status = rename_table(new XMLDBTable('groups_members_temp'), 'groups_members');
+        }
     }
     return $status;
 }
 
 
 function xmldb_group_upgrade($oldversion=0) {
-
-    //global $CFG, $THEME, $db;
+    global $CFG; //, $THEME, $db;
 
     $result = true;
 
@@ -323,6 +422,24 @@
         $result = $result && add_index($table, $index);
     }
 
+    if ($result && $oldversion < 2007012400) {
+        if( table_exists(new XMLDBTable('groups_temp'))
+            && file_exists($CFG->dirroot.'/group/db/install.xml') ) {
+
+            $suffix = '_temp_18';
+            notify("Sorry, groups tables created in Moodle 1.8 dev are being archived and renamed to 'groups_*$suffix'!", 'green');
+
+        /// Need to drop foreign keys/indexes added in last upgrade, rename 'new' tables, then start again!!
+            $result = $result && groups_drop_keys_indexes_db();
+            $result = $result && groups_rename_db($suffix);
+            $result = $result && install_from_xmldb_file($CFG->dirroot.'/group/db/install.xml');
+            $result = $result && groups_transfer_db();
+        }
+        else {
+            error('Upgrade 2007012400 of groups failed! (Could not update version in config table)');
+        }
+    }
+
     return $result;
 }
 
