Index: group/version.php
===================================================================
RCS file: group/version.php
diff -N group/version.php
--- group/version.php	29 Jan 2007 15:03:25 -0000	1.6
+++ /dev/null	1 Jan 1970 00:00:00 -0000
@@ -1,11 +0,0 @@
-<?php
-
-////////////////////////////////////////////////////////////////////////////////
-//  Code fragment to define the group version etc.
-//  This fragment is called by /admin/index.php
-////////////////////////////////////////////////////////////////////////////////
-
-$group_version  = 2007012400;
-//$module->requires = 2006120400;  // Requires this Moodle version
-
-?>
Index: lib/db/upgradelib.php
===================================================================
RCS file: /cvsroot/moodle/moodle/lib/db/upgradelib.php,v
retrieving revision 1.1
diff -u -r1.1 upgradelib.php
--- lib/db/upgradelib.php	22 Jul 2007 21:43:51 -0000	1.1
+++ lib/db/upgradelib.php	27 Jul 2007 12:09:46 -0000
@@ -173,4 +173,221 @@
     return true;
 }
 
+
+
+/**
+ * Create new groupings tables for upgrade from 1.7.*|1.6.* and so on.
+ */
+function upgrade_17_groups() {
+    $result = true;
+
+/// Define table groupings to be created
+    $table = new XMLDBTable('groupings');
+
+/// Adding fields to table groupings
+    $table->addFieldInfo('id', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, XMLDB_SEQUENCE, null, null, null);
+    $table->addFieldInfo('courseid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, '0');
+    $table->addFieldInfo('name', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, null, null, null);
+    $table->addFieldInfo('description', XMLDB_TYPE_TEXT, 'small', null, null, null, null, null, null);
+    $table->addFieldInfo('exclusivegroups', XMLDB_TYPE_INTEGER, '1', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, '0');
+    $table->addFieldInfo('maxgroupsize', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, '0');
+    $table->addFieldInfo('timecreated', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, '0');
+    $table->addFieldInfo('timemodified', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, '0');
+
+/// Adding keys to table groupings
+    $table->addKeyInfo('primary', XMLDB_KEY_PRIMARY, array('id'));
+    $table->addKeyInfo('courseid', XMLDB_KEY_FOREIGN, array('courseid'), 'course', array('id'));
+
+/// Launch create table for groupings
+    $result = $result && create_table($table);
+
+// ==========================================
+
+/// Define table groupings_groups to be created
+    $table = new XMLDBTable('groupings_groups');
+
+/// Adding fields to table groupings_groups
+    $table->addFieldInfo('id', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, XMLDB_SEQUENCE, null, null, null);
+    $table->addFieldInfo('groupingid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, '0');
+    $table->addFieldInfo('groupid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, '0');
+    $table->addFieldInfo('timeadded', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, '0');
+
+/// Adding keys to table groupings_groups
+    $table->addKeyInfo('primary', XMLDB_KEY_PRIMARY, array('id'));
+    $table->addKeyInfo('groupingid', XMLDB_KEY_FOREIGN, array('groupingid'), 'groupings', array('id'));
+    $table->addKeyInfo('groupid', XMLDB_KEY_FOREIGN, array('groupid'), 'groups', array('id'));
+
+/// Launch create table for groupings_groups
+    $result = $result && create_table($table);
+
+    return $result;
+}
+
+/**
+ * Drop, add fields and rename tables for groups upgrade from 1.8.*
+ * @param XMLDBTable $table 'groups_groupings' table object.
+ */
+function upgrade_18_groups($table) {
+    global $CFG;
+    $result = true;
+
+/// Delete columns 'viewowngroup', etc.
+    $field = new XMLDBField('viewowngroup');
+    $field->setAttributes(XMLDB_TYPE_INTEGER, '1', null, XMLDB_NOTNULL, null, null, null, '0', $prev='timecreated');
+    $fields_r = array('viewowngroup', 'viewallgroupsmembers', 'viewallgroupsactivities', 
+            'teachersgroupmark', 'teachersgroupview', 'teachersoverride', 'teacherdeletable');
+    $result = $result && upgrade_drop_fields($table, $field, $fields_r);
+
+/// Add columns/key 'courseid', exclusivegroups, maxgroupsize, timemodified.
+    $field = new XMLDBField('courseid');
+    $field->setAttributes(XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null, null, '0', $prev='id');
+    $result = $result && add_field($table, $field);
+
+    $key = new XMLDBKey('courseid');
+    $key->setAttributes(XMLDB_KEY_FOREIGN, array('courseid'), 'course', array('id'));
+    $result = $result && add_key($table, $key);
+
+    $field = new XMLDBField('exclusivegroups');
+    $field->setAttributes(XMLDB_TYPE_INTEGER, '1', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, '0', $prev='description');
+    $result = $result && add_field($table, $field);
+
+    $field = new XMLDBField('maxgroupsize');
+    $field->setAttributes(XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, '0', $prev='exclusivegroups');
+    $result = $result && add_field($table, $field);
+
+    $field = new XMLDBField('timemodified');
+    $field->setAttributes(XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, '0', $prev='timecreated');
+    $result = $result && add_field($table, $field);
+
+/// Rename to 'groupings'.
+    $result = $result && rename_table($table, 'groupings');
+
+
+/// Now, rename 'groups_groupings_groups' to 'groupings_groups' - confused yet?!
+    $result = $result && rename_table($table = new XMLDBTable('groups_groupings_groups'), 'groupings_groups');
+
+
+/// Transfer course ID from 'mdl_groups_courses_groups' to 'mdl_groups'.
+    if (!$rs = get_recordset('groups_courses_groups')) {
+        //ERROR: strange - did we already remove the tables?
+        return;
+    }
+    $db->debug = false;
+    if ($rs->RecordCount() > 0) {
+        while ($group = rs_fetch_next_record($rs)) {
+            //Update record, overwrite the 'id' (not useful) with group ID.
+            $group->id = $group->groupid;
+            $result = $result && update_record('groups', $group);
+        }
+    }
+    rs_close($rs);
+    $db->debug = true;
+
+    return $result;
+}
+
+/**
+ * Drop keys & indexes for groups upgrade from 1.8.*
+ */
+function upgrade_drop_keys_indexes_groups() {
+    $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;
+}
+
+/** This might be useful in ddllib.php.
+ * Example:
+ * $field_seed = new XMLDBField($field_name);
+ * $field_seed->setAttributes(XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null, null, '0', $previous);
+ * $result = $result && upgrade_drop_fields($table, $field_seed, $fields);
+ *
+ * @param $table XMLDBTable object.
+ * @param $field_seed XMLDBField object with attributes including 'previous' set for first field.
+ * @param $fields array of names of fields to be deleted, including the seed name.
+ */
+function upgrade_drop_fields($table, $field_seed, $fields) {
+    $result = true;
+    $field = $field_seed;
+    foreach ($fields as $field_name) {
+        $field->setName($field_name);
+        $result = $result && drop_field($table, $field);
+        $field->setPrevious($field->getName());
+    }
+    return $result;
+}
+
 ?>
Index: lib/db/install.xml
===================================================================
RCS file: /cvsroot/moodle/moodle/lib/db/install.xml,v
retrieving revision 1.94
diff -u -r1.94 install.xml
--- lib/db/install.xml	25 Jul 2007 03:14:20 -0000	1.94
+++ lib/db/install.xml	27 Jul 2007 12:09:46 -0000
@@ -55,8 +55,9 @@
         <FIELD NAME="visible" TYPE="int" LENGTH="1" NOTNULL="true" UNSIGNED="true" DEFAULT="1" SEQUENCE="false" ENUM="false" PREVIOUS="showreports" NEXT="hiddensections"/>
         <FIELD NAME="hiddensections" TYPE="int" LENGTH="2" NOTNULL="true" UNSIGNED="true" DEFAULT="0" SEQUENCE="false" ENUM="false" PREVIOUS="visible" NEXT="groupmode"/>
         <FIELD NAME="groupmode" TYPE="int" LENGTH="4" NOTNULL="true" UNSIGNED="true" DEFAULT="0" SEQUENCE="false" ENUM="false" PREVIOUS="hiddensections" NEXT="groupmodeforce"/>
-        <FIELD NAME="groupmodeforce" TYPE="int" LENGTH="4" NOTNULL="true" UNSIGNED="true" DEFAULT="0" SEQUENCE="false" ENUM="false" PREVIOUS="groupmode" NEXT="lang"/>
-        <FIELD NAME="lang" TYPE="char" LENGTH="30" NOTNULL="true" SEQUENCE="false" ENUM="false" PREVIOUS="groupmodeforce" NEXT="theme"/>
+        <FIELD NAME="groupmodeforce" TYPE="int" LENGTH="4" NOTNULL="true" UNSIGNED="true" DEFAULT="0" SEQUENCE="false" ENUM="false" PREVIOUS="groupmode" NEXT="groupingid"/>
+        <FIELD NAME="groupingid" TYPE="int" LENGTH="10" NOTNULL="true" UNSIGNED="true" DEFAULT="0" SEQUENCE="false" ENUM="false" PREVIOUS="groupmodeforce" NEXT="lang"/>
+        <FIELD NAME="lang" TYPE="char" LENGTH="30" NOTNULL="true" SEQUENCE="false" ENUM="false" PREVIOUS="groupingid" NEXT="theme"/>
         <FIELD NAME="theme" TYPE="char" LENGTH="50" NOTNULL="true" SEQUENCE="false" ENUM="false" PREVIOUS="lang" NEXT="cost"/>
         <FIELD NAME="cost" TYPE="char" LENGTH="10" NOTNULL="true" SEQUENCE="false" ENUM="false" PREVIOUS="theme" NEXT="currency"/>
         <FIELD NAME="currency" TYPE="char" LENGTH="3" NOTNULL="true" DEFAULT="USD" SEQUENCE="false" ENUM="false" PREVIOUS="cost" NEXT="timecreated"/>
@@ -75,7 +76,8 @@
         <FIELD NAME="defaultrole" TYPE="int" LENGTH="10" NOTNULL="true" UNSIGNED="true" DEFAULT="0" SEQUENCE="false" ENUM="false" COMMENT="The default role given to participants who self-enrol" PREVIOUS="enrol"/>
       </FIELDS>
       <KEYS>
-        <KEY NAME="primary" TYPE="primary" FIELDS="id" COMMENT="Primary key for course"/>
+        <KEY NAME="primary" TYPE="primary" FIELDS="id" COMMENT="Primary key for course" NEXT="groupingid"/>
+        <KEY NAME="groupingid" TYPE="foreign" FIELDS="groupingid" REFTABLE="groupings" REFFIELDS="id" PREVIOUS="primary"/>
       </KEYS>
       <INDEXES>
         <INDEX NAME="category" UNIQUE="false" FIELDS="category" NEXT="idnumber"/>
@@ -143,10 +145,13 @@
         <FIELD NAME="indent" TYPE="int" LENGTH="5" NOTNULL="true" UNSIGNED="true" DEFAULT="0" SEQUENCE="false" ENUM="false" PREVIOUS="score" NEXT="visible"/>
         <FIELD NAME="visible" TYPE="int" LENGTH="1" NOTNULL="true" UNSIGNED="false" DEFAULT="1" SEQUENCE="false" ENUM="false" PREVIOUS="indent" NEXT="visibleold"/>
         <FIELD NAME="visibleold" TYPE="int" LENGTH="1" NOTNULL="true" UNSIGNED="false" DEFAULT="1" SEQUENCE="false" ENUM="false" PREVIOUS="visible" NEXT="groupmode"/>
-        <FIELD NAME="groupmode" TYPE="int" LENGTH="4" NOTNULL="true" UNSIGNED="false" DEFAULT="0" SEQUENCE="false" ENUM="false" PREVIOUS="visibleold"/>
+        <FIELD NAME="groupmode" TYPE="int" LENGTH="4" NOTNULL="true" UNSIGNED="false" DEFAULT="0" SEQUENCE="false" ENUM="false" PREVIOUS="visibleold" NEXT="groupingid"/>
+        <FIELD NAME="groupingid" TYPE="int" LENGTH="10" NOTNULL="true" UNSIGNED="true" DEFAULT="0" SEQUENCE="false" ENUM="false" PREVIOUS="groupmode" NEXT="grouponly"/>
+        <FIELD NAME="grouponly" TYPE="int" LENGTH="4" NOTNULL="true" UNSIGNED="true" SEQUENCE="false" ENUM="false" PREVIOUS="groupingid"/>
       </FIELDS>
       <KEYS>
-        <KEY NAME="primary" TYPE="primary" FIELDS="id" COMMENT="Primary key for course_modules"/>
+        <KEY NAME="primary" TYPE="primary" FIELDS="id" COMMENT="Primary key for course_modules" NEXT="groupingid"/>
+        <KEY NAME="groupingid" TYPE="foreign" FIELDS="groupingid" REFTABLE="groupings" REFFIELDS="id" PREVIOUS="primary"/>
       </KEYS>
       <INDEXES>
         <INDEX NAME="visible" UNIQUE="false" FIELDS="visible" NEXT="course"/>
@@ -1579,7 +1584,7 @@
         <KEY NAME="primary" TYPE="primary" FIELDS="id" COMMENT="primary key of the table, please edit me"/>
       </KEYS>
     </TABLE>
-    <TABLE NAME="grade_import_values" COMMENT="Temporary table for importing grades" PREVIOUS="grade_import_newitem">
+    <TABLE NAME="grade_import_values" COMMENT="Temporary table for importing grades" PREVIOUS="grade_import_newitem" NEXT="groups">
       <FIELDS>
         <FIELD NAME="id" TYPE="int" LENGTH="10" NOTNULL="true" UNSIGNED="true" SEQUENCE="true" ENUM="false" COMMENT="id of the table, please edit me" NEXT="itemid"/>
         <FIELD NAME="itemid" TYPE="int" LENGTH="10" NOTNULL="false" UNSIGNED="true" SEQUENCE="false" ENUM="false" COMMENT="if set, this points to existing grade_items id" PREVIOUS="id" NEXT="newgradeitem"/>
@@ -1595,6 +1600,67 @@
         <KEY NAME="newgradeitem" TYPE="foreign" FIELDS="newgradeitem" REFTABLE="grade_import_newitem" REFFIELDS="id" PREVIOUS="itemid"/>
       </KEYS>
     </TABLE>
+    <TABLE NAME="groups" COMMENT="Each record represents a group." PREVIOUS="grade_import_values" NEXT="groupings">
+      <FIELDS>
+        <FIELD NAME="id" TYPE="int" LENGTH="10" NOTNULL="true" UNSIGNED="true" SEQUENCE="true" ENUM="false" NEXT="courseid"/>
+        <FIELD NAME="courseid" TYPE="int" LENGTH="10" NOTNULL="true" UNSIGNED="true" SEQUENCE="false" ENUM="false" PREVIOUS="id" NEXT="name"/>
+        <FIELD NAME="name" TYPE="char" LENGTH="254" NOTNULL="true" SEQUENCE="false" ENUM="false" COMMENT="Short human readable unique name for the group." PREVIOUS="courseid" NEXT="description"/>
+        <FIELD NAME="description" TYPE="text" LENGTH="small" NOTNULL="false" SEQUENCE="false" ENUM="false" PREVIOUS="name" NEXT="enrolmentkey"/>
+        <FIELD NAME="enrolmentkey" TYPE="char" LENGTH="50" NOTNULL="true" SEQUENCE="false" ENUM="false" PREVIOUS="description" NEXT="lang"/>
+        <FIELD NAME="lang" TYPE="char" LENGTH="30" NOTNULL="true" DEFAULT="en" SEQUENCE="false" ENUM="false" PREVIOUS="enrolmentkey" NEXT="theme"/>
+        <FIELD NAME="theme" TYPE="char" LENGTH="50" NOTNULL="true" SEQUENCE="false" ENUM="false" PREVIOUS="lang" NEXT="picture"/>
+        <FIELD NAME="picture" TYPE="int" LENGTH="10" NOTNULL="true" UNSIGNED="true" DEFAULT="0" SEQUENCE="false" ENUM="false" PREVIOUS="theme" NEXT="hidepicture"/>
+        <FIELD NAME="hidepicture" TYPE="int" LENGTH="1" NOTNULL="true" UNSIGNED="true" DEFAULT="0" SEQUENCE="false" ENUM="false" PREVIOUS="picture" NEXT="timecreated"/>
+        <FIELD NAME="timecreated" TYPE="int" LENGTH="10" NOTNULL="true" UNSIGNED="true" DEFAULT="0" SEQUENCE="false" ENUM="false" PREVIOUS="hidepicture" NEXT="timemodified"/>
+        <FIELD NAME="timemodified" TYPE="int" LENGTH="10" NOTNULL="true" UNSIGNED="true" DEFAULT="0" SEQUENCE="false" ENUM="false" PREVIOUS="timecreated"/>
+      </FIELDS>
+      <KEYS>
+        <KEY NAME="primary" TYPE="primary" FIELDS="id" COMMENT="Primary key for groups." NEXT="courseid"/>
+        <KEY NAME="courseid" TYPE="foreign" FIELDS="courseid" REFTABLE="course" REFFIELDS="id" PREVIOUS="primary"/>
+      </KEYS>
+    </TABLE>
+    <TABLE NAME="groupings" COMMENT="A grouping is a collection of groups. WAS: groups_groupings" PREVIOUS="groups" NEXT="groups_members">
+      <FIELDS>
+        <FIELD NAME="id" TYPE="int" LENGTH="10" NOTNULL="true" UNSIGNED="false" SEQUENCE="true" ENUM="false" COMMENT="id of the table, please edit me" NEXT="courseid"/>
+        <FIELD NAME="courseid" TYPE="int" LENGTH="10" NOTNULL="true" UNSIGNED="true" DEFAULT="0" SEQUENCE="false" ENUM="false" PREVIOUS="id" NEXT="name"/>
+        <FIELD NAME="name" TYPE="char" LENGTH="255" NOTNULL="true" SEQUENCE="false" ENUM="false" COMMENT="Short human readable unique name for group." PREVIOUS="courseid" NEXT="description"/>
+        <FIELD NAME="description" TYPE="text" LENGTH="small" NOTNULL="false" SEQUENCE="false" ENUM="false" PREVIOUS="name" NEXT="exclusivegroups"/>
+        <FIELD NAME="exclusivegroups" TYPE="int" LENGTH="1" NOTNULL="true" UNSIGNED="true" DEFAULT="0" SEQUENCE="false" ENUM="false" PREVIOUS="description" NEXT="maxgroupsize"/>
+        <FIELD NAME="maxgroupsize" TYPE="int" LENGTH="10" NOTNULL="true" UNSIGNED="true" DEFAULT="0" SEQUENCE="false" ENUM="false" PREVIOUS="exclusivegroups" NEXT="timecreated"/>
+        <FIELD NAME="timecreated" TYPE="int" LENGTH="10" NOTNULL="true" UNSIGNED="true" DEFAULT="0" SEQUENCE="false" ENUM="false" PREVIOUS="maxgroupsize" NEXT="timemodified"/>
+        <FIELD NAME="timemodified" TYPE="int" LENGTH="10" NOTNULL="true" UNSIGNED="true" DEFAULT="0" SEQUENCE="false" ENUM="false" PREVIOUS="timecreated"/>
+      </FIELDS>
+      <KEYS>
+        <KEY NAME="primary" TYPE="primary" FIELDS="id" COMMENT="Primary key for groupings." NEXT="courseid"/>
+        <KEY NAME="courseid" TYPE="foreign" FIELDS="courseid" REFTABLE="course" REFFIELDS="id" PREVIOUS="primary"/>
+      </KEYS>
+    </TABLE>
+    <TABLE NAME="groups_members" COMMENT="Link a user to a group." PREVIOUS="groupings" NEXT="groupings_groups">
+      <FIELDS>
+        <FIELD NAME="id" TYPE="int" LENGTH="10" NOTNULL="true" UNSIGNED="true" SEQUENCE="true" ENUM="false" NEXT="groupid"/>
+        <FIELD NAME="groupid" TYPE="int" LENGTH="10" NOTNULL="true" UNSIGNED="true" DEFAULT="0" SEQUENCE="false" ENUM="false" PREVIOUS="id" NEXT="userid"/>
+        <FIELD NAME="userid" TYPE="int" LENGTH="10" NOTNULL="true" UNSIGNED="true" DEFAULT="0" SEQUENCE="false" ENUM="false" PREVIOUS="groupid" NEXT="timeadded"/>
+        <FIELD NAME="timeadded" TYPE="int" LENGTH="10" NOTNULL="true" UNSIGNED="true" DEFAULT="0" SEQUENCE="false" ENUM="false" PREVIOUS="userid"/>
+      </FIELDS>
+      <KEYS>
+        <KEY NAME="primary" TYPE="primary" FIELDS="id" COMMENT="Primary key for groups_members" NEXT="groupid"/>
+        <KEY NAME="groupid" TYPE="foreign" FIELDS="groupid" REFTABLE="groups" REFFIELDS="id" PREVIOUS="primary" NEXT="userid"/>
+        <KEY NAME="userid" TYPE="foreign" FIELDS="userid" REFTABLE="user" REFFIELDS="id" PREVIOUS="groupid"/>
+      </KEYS>
+    </TABLE>
+    <TABLE NAME="groupings_groups" COMMENT="Link a grouping to a group (note, groups can be in multiple groupings ONLY in a course). WAS: groups_groupings_groups" PREVIOUS="groups_members">
+      <FIELDS>
+        <FIELD NAME="id" TYPE="int" LENGTH="10" NOTNULL="true" UNSIGNED="false" SEQUENCE="true" ENUM="false" COMMENT="id of the table, please edit me" NEXT="groupingid"/>
+        <FIELD NAME="groupingid" TYPE="int" LENGTH="10" NOTNULL="true" UNSIGNED="true" DEFAULT="0" SEQUENCE="false" ENUM="false" PREVIOUS="id" NEXT="groupid"/>
+        <FIELD NAME="groupid" TYPE="int" LENGTH="10" NOTNULL="true" UNSIGNED="true" DEFAULT="0" SEQUENCE="false" ENUM="false" PREVIOUS="groupingid" NEXT="timeadded"/>
+        <FIELD NAME="timeadded" TYPE="int" LENGTH="10" NOTNULL="true" UNSIGNED="true" DEFAULT="0" SEQUENCE="false" ENUM="false" PREVIOUS="groupid"/>
+      </FIELDS>
+      <KEYS>
+        <KEY NAME="primary" TYPE="primary" FIELDS="id" COMMENT="Primary key for groupings_groups" NEXT="groupingid"/>
+        <KEY NAME="groupingid" TYPE="foreign" FIELDS="groupingid" REFTABLE="groupings" REFFIELDS="id" PREVIOUS="primary" NEXT="groupid"/>
+        <KEY NAME="groupid" TYPE="foreign" FIELDS="groupid" REFTABLE="groups" REFFIELDS="id" PREVIOUS="groupingid"/>
+      </KEYS>
+    </TABLE>
   </TABLES>
   <STATEMENTS>
     <STATEMENT NAME="insert mnet_application" TYPE="insert" TABLE="mnet_application" COMMENT="Initial insert of records on table mnet_application" NEXT="insert log_display">
@@ -1621,6 +1687,7 @@
         <SENTENCE TEXT="(module, action, mtable, field) VALUES ('message', 'remove contact', 'user', 'CONCAT(firstname,&quot; &quot;,lastname)')" />
         <SENTENCE TEXT="(module, action, mtable, field) VALUES ('message', 'block contact', 'user', 'CONCAT(firstname,&quot; &quot;,lastname)')" />
         <SENTENCE TEXT="(module, action, mtable, field) VALUES ('message', 'unblock contact', 'user', 'CONCAT(firstname,&quot; &quot;,lastname)')" />
+        <SENTENCE TEXT="(module, action, mtable, field) VALUES ('group', 'view', 'groups', 'name')" />
       </SENTENCES>
     </STATEMENT>
   </STATEMENTS>
Index: lib/db/upgrade.php
===================================================================
RCS file: /cvsroot/moodle/moodle/lib/db/upgrade.php,v
retrieving revision 1.92
diff -u -r1.92 upgrade.php
--- lib/db/upgrade.php	25 Jul 2007 03:14:20 -0000	1.92
+++ lib/db/upgrade.php	27 Jul 2007 12:09:46 -0000
@@ -972,7 +972,7 @@
 
     if ($result && $oldversion < 2007071607) {
         require_once($CFG->dirroot . '/question/upgrade.php');
-        $result = $result && question_remove_rqp_qtype_config_string();
+///        $result = $result && question_remove_rqp_qtype_config_string();
     }
 
 
@@ -1427,11 +1427,11 @@
                 if ($rs->RecordCount() > 0) {
                     while ($course = rs_fetch_next_record($rs)) {
                         // this function uses SQL only, it must not be changed after 1.9 goes stable!!
-                        if (!upgrade_18_gradebook($course->id)) {
+/*                        if (!upgrade_18_gradebook($course->id)) {
                             $result = false;
                             break;
                         }
-                    }
+*/                    }
                 }
                 rs_close($rs);
             }
@@ -1476,6 +1476,90 @@
         }
     }
 */
+
+
+    if ($result && $oldversion < 2007072501) {
+
+    // Flag for use later.
+    $ismoodle_18 = false;
+
+    /// IF 'groups_groupings' table exists, this is for 1.8.* only.
+        if (table_exists($table = new XMLDBTable('groups_groupings'))) {
+            $ismoodle_18 = true;
+
+        /// Delete keys & indexes - they may get in the way.            
+            $result = $result && upgrade_drop_keys_indexes_groups();
+
+            $result = $result && upgrade_18_groups($table);
+        }
+    /// ELSE, 1.7.*/1.6.*/1.5.* - create 'groupings'.
+        else {
+            $result = $result && upgrade_17_groups();
+        }
+    /// For both 1.8.* and 1.7.*/1.6.*..
+
+    /// Add groupingid field/f.key to 'course' table.
+        $table = new XMLDBTable('course');
+
+        $field = new XMLDBField('groupingid');
+        $field->setAttributes(XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, '0', $prev='groupmodeforce');
+        $result = $result && add_field($table, $field);
+
+        $key = new XMLDBKey('groupingid');
+        $key->setAttributes(XMLDB_KEY_FOREIGN, array('groupingid'), 'groupings', array('id'));
+        $result = $result && add_key($table, $key);
+
+
+    /// Add grouping ID, grouponly field/f.key to 'course_modules' table.
+        //ALTER TABLE mdl_course_modules DROP COLUMN groupingid;
+        $table = new XMLDBTable('course_modules');
+
+        $field = new XMLDBField('groupingid');
+        $field->setAttributes(XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, '0', $prev='groupmode');
+        $result = $result && add_field($table, $field);
+
+        $field = new XMLDBField('grouponly');
+        $field->setAttributes(XMLDB_TYPE_INTEGER, '4', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, '0', $prev='groupingid');
+        $result = $result && add_field($table, $field);
+
+        $key = new XMLDBKey('groupingid');
+        $key->setAttributes(XMLDB_KEY_FOREIGN, array('groupingid'), 'groupings', array('id'));
+        $result = $result && add_key($table, $key);
+
+        if ($ismoodle_18) {
+        /// Transfer course ID from 'mdl_groups_courses_groupings' to 'mdl_groupings'.
+            if (!$rs = get_recordset('groups_courses_groupings')) {
+                //ERROR: strange - did we already remove the tables?
+                return;
+            }
+            $db->debug = false;
+            if ($rs->RecordCount() > 0) {
+                while ($course_grouping = rs_fetch_next_record($rs)) {
+                    //Update record, overwrite the 'id' (not useful) with grouping ID.
+                    $course_grouping->id = $course_grouping->groupingid;
+                    $result = $result && update_record('groupings', $course_grouping);
+                    
+                /// And, add grouping IDs to 'course' table.
+                    $course = new object();
+                    $course->id = $course_grouping->courseid;
+                    $course->groupingid = $course_grouping->id;
+                    $result = $result && update_record('course', $course);
+                }
+            }
+            rs_close($rs);
+            $db->debug = true;
+/*
+        ///TODO: Delete old tables - uncomment only AFTER testing!
+            $result = $result && drop_table(new XMLDBTable('groups_courses_groups'));
+            $result = $result && drop_table(new XMLDBTable('groups_courses_groupings'));
+            $result = $result && drop_table(new XMLDBTable('groups_temp'));
+            $result = $result && drop_table(new XMLDBTable('groups_members_temp'));
+        
+            $result = $result && unset_config('group_version');
+*/
+        }
+    }
+
     return $result;
 }
 ?>
Index: admin/index.php
===================================================================
RCS file: /cvsroot/moodle/moodle/admin/index.php,v
retrieving revision 1.278
diff -u -r1.278 index.php
--- admin/index.php	6 Jul 2007 17:54:54 -0000	1.278
+++ admin/index.php	27 Jul 2007 12:09:44 -0000
@@ -189,9 +189,8 @@
     /// Continue with the instalation
         $db->debug = false;
         if ($status) {
-            //ugly hack - install new groups: MDL-9217
-            require_once("$CFG->dirroot/group/db/upgrade.php");
-            install_group_db();
+
+            /// Groups install is now in core above.
 
             // Install the roles system.
             moodle_install_roles();
@@ -327,9 +326,9 @@
             /// If successful, continue upgrading roles and setting everything properly
                 if ($status) {
                     if (empty($CFG->rolesactive)) {
-                        //ugly hack - upgrade to new groups (from 1.6) : MDL-9217
-                        require_once("$CFG->dirroot/group/db/upgrade.php");
-                        install_group_db();
+
+                        /// Groups upgrade is now in core above.
+
                         // Upgrade to the roles system.
                         moodle_install_roles();
                         set_config('rolesactive', 1);
@@ -380,9 +379,7 @@
         }
     }
 
-/// ugly hack - convert to new groups if upgrading from 1.7; must be reworked
-    require_once("$CFG->dirroot/group/db/upgrade.php");
-    upgrade_group_db("$CFG->wwwroot/$CFG->admin/index.php");  // Return here afterwards
+/// Groups install/upgrade is now in core above.
 
 
 /// Find and check all main modules and load them up or upgrade them if necessary
Index: group/db/install.xml
===================================================================
RCS file: group/db/install.xml
diff -N group/db/install.xml
--- group/db/install.xml	22 Jan 2007 17:50:44 -0000	1.5
+++ /dev/null	1 Jan 1970 00:00:00 -0000
@@ -1,112 +0,0 @@
-<?xml version="1.0" encoding="UTF-8" ?>
-<XMLDB PATH="group/db" VERSION="20070122" COMMENT="XMLDB file for Moodle groups."
-    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
-    xsi:noNamespaceSchemaLocation="../../lib/xmldb/xmldb.xsd"
->
-  <TABLES>
-    <TABLE NAME="groups" COMMENT="Each record represents a group." NEXT="groups_groupings">
-      <FIELDS>
-        <FIELD NAME="id" TYPE="int" LENGTH="10" NOTNULL="true" UNSIGNED="true" SEQUENCE="true" ENUM="false" NEXT="name"/>
-        <FIELD NAME="name" TYPE="char" LENGTH="254" NOTNULL="true" SEQUENCE="false" ENUM="false" COMMENT="Short human readable unique name for the group." PREVIOUS="id" NEXT="description"/>
-        <FIELD NAME="description" TYPE="text" LENGTH="small" NOTNULL="false" SEQUENCE="false" ENUM="false" PREVIOUS="name" NEXT="enrolmentkey"/>
-        <FIELD NAME="enrolmentkey" TYPE="char" LENGTH="50" NOTNULL="true" SEQUENCE="false" ENUM="false" PREVIOUS="description" NEXT="lang"/>
-        <FIELD NAME="lang" TYPE="char" LENGTH="30" NOTNULL="true" DEFAULT="en" SEQUENCE="false" ENUM="false" PREVIOUS="enrolmentkey" NEXT="theme"/>
-        <FIELD NAME="theme" TYPE="char" LENGTH="50" NOTNULL="true" SEQUENCE="false" ENUM="false" PREVIOUS="lang" NEXT="picture"/>
-        <FIELD NAME="picture" TYPE="int" LENGTH="10" NOTNULL="true" UNSIGNED="true" DEFAULT="0" SEQUENCE="false" ENUM="false" PREVIOUS="theme" NEXT="hidepicture"/>
-        <FIELD NAME="hidepicture" TYPE="int" LENGTH="1" NOTNULL="true" UNSIGNED="true" DEFAULT="0" SEQUENCE="false" ENUM="false" PREVIOUS="picture" NEXT="timecreated"/>
-        <FIELD NAME="timecreated" TYPE="int" LENGTH="10" NOTNULL="true" UNSIGNED="true" DEFAULT="0" SEQUENCE="false" ENUM="false" PREVIOUS="hidepicture" NEXT="timemodified"/>
-        <FIELD NAME="timemodified" TYPE="int" LENGTH="10" NOTNULL="true" UNSIGNED="true" DEFAULT="0" SEQUENCE="false" ENUM="false" PREVIOUS="timecreated"/>
-      </FIELDS>
-      <KEYS>
-        <KEY NAME="primary" TYPE="primary" FIELDS="id" COMMENT="Primary key for groups"/>
-      </KEYS>
-    </TABLE>
-    <TABLE NAME="groups_groupings" COMMENT="A grouping is a collection of groups." PREVIOUS="groups" NEXT="groups_members">
-      <FIELDS>
-        <FIELD NAME="id" TYPE="int" LENGTH="10" NOTNULL="true" UNSIGNED="false" SEQUENCE="true" ENUM="false" COMMENT="id of the table, please edit me" NEXT="name"/>
-        <FIELD NAME="name" TYPE="char" LENGTH="255" NOTNULL="true" SEQUENCE="false" ENUM="false" COMMENT="Short human readable unique name for group." PREVIOUS="id" NEXT="description"/>
-        <FIELD NAME="description" TYPE="text" LENGTH="small" NOTNULL="false" SEQUENCE="false" ENUM="false" PREVIOUS="name" NEXT="timecreated"/>
-        <FIELD NAME="timecreated" TYPE="int" LENGTH="10" NOTNULL="true" UNSIGNED="true" DEFAULT="0" SEQUENCE="false" ENUM="false" PREVIOUS="description" NEXT="viewowngroup"/>
-        <FIELD NAME="viewowngroup" TYPE="int" LENGTH="1" NOTNULL="true" UNSIGNED="true" DEFAULT="1" SEQUENCE="false" ENUM="false" PREVIOUS="timecreated" NEXT="viewallgroupsmembers"/>
-        <FIELD NAME="viewallgroupsmembers" TYPE="int" LENGTH="1" NOTNULL="true" UNSIGNED="true" DEFAULT="0" SEQUENCE="false" ENUM="false" PREVIOUS="viewowngroup" NEXT="viewallgroupsactivities"/>
-        <FIELD NAME="viewallgroupsactivities" TYPE="int" LENGTH="1" NOTNULL="true" UNSIGNED="true" DEFAULT="0" SEQUENCE="false" ENUM="false" PREVIOUS="viewallgroupsmembers" NEXT="teachersgroupmark"/>
-        <FIELD NAME="teachersgroupmark" TYPE="int" LENGTH="1" NOTNULL="true" UNSIGNED="true" DEFAULT="0" SEQUENCE="false" ENUM="false" PREVIOUS="viewallgroupsactivities" NEXT="teachersgroupview"/>
-        <FIELD NAME="teachersgroupview" TYPE="int" LENGTH="1" NOTNULL="true" UNSIGNED="true" DEFAULT="0" SEQUENCE="false" ENUM="false" PREVIOUS="teachersgroupmark" NEXT="teachersoverride"/>
-        <FIELD NAME="teachersoverride" TYPE="int" LENGTH="1" NOTNULL="true" UNSIGNED="true" DEFAULT="0" SEQUENCE="false" ENUM="false" PREVIOUS="teachersgroupview" NEXT="teacherdeletable"/>
-        <FIELD NAME="teacherdeletable" TYPE="int" LENGTH="1" NOTNULL="true" UNSIGNED="true" DEFAULT="0" SEQUENCE="false" ENUM="false" PREVIOUS="teachersoverride"/>
-      </FIELDS>
-      <KEYS>
-        <KEY NAME="primary" TYPE="primary" FIELDS="id" COMMENT="Primary key for groups_groupings"/>
-      </KEYS>
-    </TABLE>
-    <TABLE NAME="groups_members" COMMENT="Link a user to a group." PREVIOUS="groups_groupings" NEXT="groups_courses_groups">
-      <FIELDS>
-        <FIELD NAME="id" TYPE="int" LENGTH="10" NOTNULL="true" UNSIGNED="true" SEQUENCE="true" ENUM="false" NEXT="groupid"/>
-        <FIELD NAME="groupid" TYPE="int" LENGTH="10" NOTNULL="true" UNSIGNED="true" DEFAULT="0" SEQUENCE="false" ENUM="false" PREVIOUS="id" NEXT="userid"/>
-        <FIELD NAME="userid" TYPE="int" LENGTH="10" NOTNULL="true" UNSIGNED="true" DEFAULT="0" SEQUENCE="false" ENUM="false" PREVIOUS="groupid" NEXT="timeadded"/>
-        <FIELD NAME="timeadded" TYPE="int" LENGTH="10" NOTNULL="true" UNSIGNED="true" DEFAULT="0" SEQUENCE="false" ENUM="false" PREVIOUS="userid"/>
-      </FIELDS>
-      <KEYS>
-        <KEY NAME="primary" TYPE="primary" FIELDS="id" COMMENT="Primary key for groups_members" NEXT="groupid"/>
-        <KEY NAME="groupid" TYPE="foreign" FIELDS="groupid" REFTABLE="groups" REFFIELDS="id" PREVIOUS="primary" NEXT="userid"/>
-        <KEY NAME="userid" TYPE="foreign" FIELDS="userid" REFTABLE="user" REFFIELDS="id" PREVIOUS="groupid"/>
-      </KEYS>
-      <INDEXES>
-        <INDEX NAME="groupid-courseid" UNIQUE="true" FIELDS="groupid, userid"/>
-      </INDEXES>
-    </TABLE>
-    <TABLE NAME="groups_courses_groups" COMMENT="Link a group to a course (or the site)." PREVIOUS="groups_members" NEXT="groups_courses_groupings">
-      <FIELDS>
-        <FIELD NAME="id" TYPE="int" LENGTH="10" NOTNULL="true" UNSIGNED="false" SEQUENCE="true" ENUM="false" COMMENT="id of the table, please edit me" NEXT="courseid"/>
-        <FIELD NAME="courseid" TYPE="int" LENGTH="10" NOTNULL="true" UNSIGNED="true" DEFAULT="0" SEQUENCE="false" ENUM="false" PREVIOUS="id" NEXT="groupid"/>
-        <FIELD NAME="groupid" TYPE="int" LENGTH="10" NOTNULL="true" UNSIGNED="true" SEQUENCE="false" ENUM="false" COMMENT="Default comment for the field, please edit me" PREVIOUS="courseid"/>
-      </FIELDS>
-      <KEYS>
-        <KEY NAME="primary" TYPE="primary" FIELDS="id" COMMENT="Primary key for groups_courses_groups" NEXT="courseid"/>
-        <KEY NAME="courseid" TYPE="foreign" FIELDS="courseid" REFTABLE="course" REFFIELDS="id" PREVIOUS="primary" NEXT="groupid"/>
-        <KEY NAME="groupid" TYPE="foreign" FIELDS="groupid" REFTABLE="groups" REFFIELDS="id" PREVIOUS="courseid"/>
-      </KEYS>
-      <INDEXES>
-        <INDEX NAME="courseid-groupid" UNIQUE="true" FIELDS="courseid, groupid"/>
-      </INDEXES>
-    </TABLE>
-    <TABLE NAME="groups_courses_groupings" COMMENT="Link a grouping to a course (or the site)." PREVIOUS="groups_courses_groups" NEXT="groups_groupings_groups">
-      <FIELDS>
-        <FIELD NAME="id" TYPE="int" LENGTH="10" NOTNULL="true" UNSIGNED="false" SEQUENCE="true" ENUM="false" COMMENT="id of the table, please edit me" NEXT="courseid"/>
-        <FIELD NAME="courseid" TYPE="int" LENGTH="10" NOTNULL="true" UNSIGNED="true" DEFAULT="0" SEQUENCE="false" ENUM="false" PREVIOUS="id" NEXT="groupingid"/>
-        <FIELD NAME="groupingid" TYPE="int" LENGTH="10" NOTNULL="true" UNSIGNED="true" SEQUENCE="false" ENUM="false" PREVIOUS="courseid"/>
-      </FIELDS>
-      <KEYS>
-        <KEY NAME="primary" TYPE="primary" FIELDS="id" COMMENT="Primary key for groups_courses_groupings" NEXT="courseid"/>
-        <KEY NAME="courseid" TYPE="foreign" FIELDS="courseid" REFTABLE="course" REFFIELDS="id" PREVIOUS="primary" NEXT="groupingid"/>
-        <KEY NAME="groupingid" TYPE="foreign" FIELDS="groupingid" REFTABLE="groups_groupings" REFFIELDS="id" PREVIOUS="courseid"/>
-      </KEYS>
-      <INDEXES>
-        <INDEX NAME="courseid-groupingid" UNIQUE="true" FIELDS="courseid, groupingid"/>
-      </INDEXES>
-    </TABLE>
-    <TABLE NAME="groups_groupings_groups" COMMENT="Link a group to a grouping." PREVIOUS="groups_courses_groupings">
-      <FIELDS>
-        <FIELD NAME="id" TYPE="int" LENGTH="10" NOTNULL="true" UNSIGNED="false" SEQUENCE="true" ENUM="false" COMMENT="id of the table, please edit me" NEXT="groupingid"/>
-        <FIELD NAME="groupingid" TYPE="int" LENGTH="10" NOTNULL="true" UNSIGNED="true" DEFAULT="0" SEQUENCE="false" ENUM="false" PREVIOUS="id" NEXT="groupid"/>
-        <FIELD NAME="groupid" TYPE="int" LENGTH="10" NOTNULL="true" UNSIGNED="true" SEQUENCE="false" ENUM="false" PREVIOUS="groupingid" NEXT="timeadded"/>
-        <FIELD NAME="timeadded" TYPE="int" LENGTH="10" NOTNULL="true" UNSIGNED="true" DEFAULT="0" SEQUENCE="false" ENUM="false" PREVIOUS="groupid"/>
-      </FIELDS>
-      <KEYS>
-        <KEY NAME="primary" TYPE="primary" FIELDS="id" COMMENT="Primary key for groups_groupings_groups" NEXT="groupingid"/>
-        <KEY NAME="groupingid" TYPE="foreign" FIELDS="groupingid" REFTABLE="groups_groupings" REFFIELDS="id" PREVIOUS="primary" NEXT="groupid"/>
-        <KEY NAME="groupid" TYPE="foreign" FIELDS="groupid" REFTABLE="groups" REFFIELDS="id" PREVIOUS="groupingid"/>
-      </KEYS>
-      <INDEXES>
-        <INDEX NAME="groupingid-groupid" UNIQUE="true" FIELDS="groupingid, groupid"/>
-      </INDEXES>
-    </TABLE>
-  </TABLES>
-  <STATEMENTS>
-    <STATEMENT NAME="insert log_display" TYPE="insert" TABLE="log_display" COMMENT="Initial insert of records on table log_display">
-      <SENTENCES>
-        <SENTENCE TEXT="(module, action, mtable, field) VALUES ('group', 'view', 'groups', 'name')" />
-      </SENTENCES>
-    </STATEMENT>
-  </STATEMENTS>
-</XMLDB>
\ No newline at end of file
Index: group/db/upgrade.php
===================================================================
RCS file: group/db/upgrade.php
diff -N group/db/upgrade.php
--- group/db/upgrade.php	18 May 2007 09:50:58 -0000	1.11.2.5
+++ /dev/null	1 Jan 1970 00:00:00 -0000
@@ -1,405 +0,0 @@
-<?php  //$Id: upgrade.php,v 1.11.2.5 2007/05/18 09:50:58 skodak Exp $
-
-// This file keeps track of upgrades to 
-// groups
-//
-// Sometimes, changes between versions involve
-// alterations to database structures and other
-// major things that may break installations.
-//
-// The upgrade function in this file will attempt
-// to perform all the necessary actions to upgrade
-// your older installtion to the current version.
-//
-// If there's something it cannot do itself, it
-// will tell you what you need to do.
-//
-// The commands in here will all be database-neutral,
-// using the functions defined in lib/ddllib.php
-function install_group_db() {
-    global $CFG, $db;
-
-    $group_version = '';  // Get code version
-    require ("$CFG->dirroot/group/version.php");
-
-    $status = true;
-
-    print_heading('group');
-    $db->debug=true;
-
-    //Moodle is already installed - rename old tables, used during tansfer later
-    if (table_exists($t_groups = new XMLDBTable('groups'))) {
-        $status = $status && rename_table($t_groups, 'groups_temp');
-        $status = $status && rename_table(new XMLDBTable('groups_members'), 'groups_members_temp');
-    }
-
-    // install new groups tables
-    $status = $status && install_from_xmldb_file($CFG->dirroot . '/group/db/install.xml');
-    // convert old groups to new ones
-    $status = $status && groups_transfer_db();
-
-    $db->debug = false;
-
-    if (!$status or !set_config('group_version', $group_version)) {
-        error("Upgrade of group system failed!");
-    }
-
-    notify(get_string('databasesuccess'), 'green');
-    notify(get_string('databaseupgradegroups', '', $group_version), 'green');
-}
-
-function undo_groupings() {
-    global $CFG;
-
-    if (!$rs = get_recordset_sql("
-                    SELECT gpgs.courseid, ggs.groupid 
-                    FROM {$CFG->prefix}groups_courses_groupings gpgs,
-                         {$CFG->prefix}groups_groupings_groups ggs
-                    WHERE gpgs.groupingid = ggs.groupingid")) {
-        //strange - did we already remove the tables?
-        return;
-    }
-
-    $db->debug = false;
-    if ($rs->RecordCount() > 0) {
-        while ($group = rs_fetch_next_record($rs)) {
-            if (!record_exists('groups_courses_groups', 'courseid', $group->courseid, 'groupid', $group->groupid)) {
-                insert_record('groups_courses_groups', $group);
-            }
-        }
-    }
-    rs_close($rs);
-    $db->debug = true;
-
-    delete_records('groups_courses_groupings');
-    delete_records('groups_groupings_groups');
-    delete_records('groups_groupings');
-}
-
-
-function upgrade_group_db($continueto) {
-/// This function upgrades the group tables, if necessary
-/// It's called from admin/index.php.
-
-    global $CFG, $db;
-
-    $group_version = '';  // Get code versions
-    require("$CFG->dirroot/group/version.php");
-
-    if (empty($CFG->group_version)) {  // New 1.8 groups have never been installed...
-        $strdatabaseupgrades = get_string('databaseupgrades');
-        print_header($strdatabaseupgrades, $strdatabaseupgrades, $strdatabaseupgrades, '', 
-                upgrade_get_javascript(), false, "&nbsp;", "&nbsp;");
-
-        upgrade_log_start();
-        //initialize default group settings now
-        install_group_db();
-
-        $adminroot = admin_get_root();
-        print_continue($continueto);
-        print_footer('none');
-        exit;
-    }
-
-/// Upgrading code starts here
-    if ($group_version > $CFG->group_version) {       // Upgrade tables
-        $strdatabaseupgrades = get_string('databaseupgrades');
-        print_header($strdatabaseupgrades, $strdatabaseupgrades, $strdatabaseupgrades, '', upgrade_get_javascript());
-
-        upgrade_log_start();
-        print_heading('group');
-
-        $db->debug = true;
-        $status = xmldb_group_upgrade($CFG->group_version);
-        $db->debug = false;
-
-    /// Now analyze upgrade results
-        if ($status) {    // No upgrading failed
-            if (set_config('group_version', $group_version)) {
-                notify(get_string('databasesuccess'), 'green');
-                notify(get_string('databaseupgradegroups', '', $group_version), 'green');
-                print_continue($continueto);
-                print_footer('none');
-                exit;
-            } else {
-                error("Error: Upgrade of group system failed! (Could not update version in config table)");
-            }
-        } else {
-            error("Error: Upgrade failed! See group/upgrade.php");
-        }
-
-    } else if ($group_version < $CFG->group_version) {
-        error("Error:  The code you are using is OLDER than the version that made these databases!");
-    }
-}
-
-/**
- * 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'))) {
-        $groups_r = get_records('groups_temp');
-        $members_r = get_records('groups_members_temp');
-
-        if (!$groups_r) {
-            // No gropus to upgrade.
-            return true;
-        }
-        foreach ($groups_r as $group) {
-            if (debugging()) {
-                print_object($group);
-            }
-            $group->enrolmentkey = $group->password;
-            $status = $status && ($newgroupid = groups_db_upgrade_group($group->courseid, $group));
-            if ($members_r) {
-                foreach ($members_r as $member) {
-                    if ($member->groupid == $group->id) {
-                        $status = $status && groups_add_member($newgroupid, $member->userid);
-                    }
-                }
-            }
-        }
-    } else {
-        $status = true; //new install - it is ok!
-    }
-    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;
-}
-
-/**
- * Drop 'new' 1.8 groups tables for 200701240 upgrade below.
- * (Also, for testing it's useful to be able to revert to 'old' groups.)
- */
-function groups_revert_db($renametemp=true) {
-    $status = true;
-    ///$status = (bool)$rs = delete_records('config', 'name', 'group_version');
-    if (table_exists(new XMLDBTable('groups_groupings'))) {
-
-        $tables = array('', '_members', '_groupings', '_courses_groups', '_courses_groupings', '_groupings_groups');
-        foreach ($tables as $t_name) {
-            $status = $status && drop_table(new XMLDBTable('groups'.$t_name));
-        }
-        $status = $status && (bool)delete_records('log_display', 'module', 'group');
-
-        if ($renametemp) {
-            $status = $status && rename_table(new XMLDBTable('groups_temp'), 'groups');
-            $status = $status && rename_table(new XMLDBTable('groups_members_temp'), 'groups_members');
-        }
-    }
-    return $status;
-}
-
-
-function xmldb_group_upgrade($oldversion=0) {
-    global $CFG;
-
-    $result = true;
-
-    if ($result && $oldversion < 2007012000) {
-
-    /// Changing nullability of field description on table groups to null
-        $table = new XMLDBTable('groups');
-        $field = new XMLDBField('description');
-        $field->setAttributes(XMLDB_TYPE_TEXT, 'small', null, null, null, null, null, null, 'name');
-
-    /// Launch change of nullability for field description
-        $result = $result && change_field_notnull($table, $field);
-
-    /// Changing nullability of field description on table groups_groupings to null
-        $table = new XMLDBTable('groups_groupings');
-        $field = new XMLDBField('description');
-        $field->setAttributes(XMLDB_TYPE_TEXT, 'small', null, null, null, null, null, null, 'name');
-
-    /// Launch change of nullability for field description
-        $result = $result && change_field_notnull($table, $field);
-    }
-
-    if ($result && $oldversion < 2007012100) {
-
-    /// Changing precision of field lang on table groups to (30)
-        $table = new XMLDBTable('groups');
-        $field = new XMLDBField('lang');
-        $field->setAttributes(XMLDB_TYPE_CHAR, '30', null, XMLDB_NOTNULL, null, null, null, 'en', 'enrolmentkey');
-
-    /// Launch change of precision for field lang
-        $result = $result && change_field_precision($table, $field);
-    }
-
-    /// Adding all the missing FK + Unique indexes (XMLDB will create the underlying indexes)
-    if ($result && $oldversion < 2007012200) {
-
-    /// 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 && add_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 && add_key($table, $key);
-
-    /// Define key groupid (foreign) to be added to groups_courses_groups
-        $table = new XMLDBTable('groups_courses_groups');
-        $key = new XMLDBKey('groupid');
-        $key->setAttributes(XMLDB_KEY_FOREIGN, array('groupid'), 'groups', array('id'));
-
-    /// Launch add key groupid
-        $result = $result && add_key($table, $key);
-
-    /// Define index courseid-groupid (unique) to be added to groups_courses_groups
-        $table = new XMLDBTable('groups_courses_groups');
-        $index = new XMLDBIndex('courseid-groupid');
-        $index->setAttributes(XMLDB_INDEX_UNIQUE, array('courseid', 'groupid'));
-
-    /// Launch add index courseid-groupid
-        $result = $result && add_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 && add_key($table, $key);
-
-    /// Define key groupingid (foreign) to be added to groups_courses_groupings
-        $table = new XMLDBTable('groups_courses_groupings');
-        $key = new XMLDBKey('groupingid');
-        $key->setAttributes(XMLDB_KEY_FOREIGN, array('groupingid'), 'groups_groupings', array('id'));
-
-    /// Launch add key groupingid
-        $result = $result && add_key($table, $key);
-
-    /// Define index courseid-groupingid (unique) to be added to groups_courses_groupings
-        $table = new XMLDBTable('groups_courses_groupings');
-        $index = new XMLDBIndex('courseid-groupingid');
-        $index->setAttributes(XMLDB_INDEX_UNIQUE, array('courseid', 'groupingid'));
-
-    /// Launch add index courseid-groupingid
-        $result = $result && add_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 && add_key($table, $key);
-
-    /// Define key groupid (foreign) to be added to groups_groupings_groups
-        $table = new XMLDBTable('groups_groupings_groups');
-        $key = new XMLDBKey('groupid');
-        $key->setAttributes(XMLDB_KEY_FOREIGN, array('groupid'), 'groups', array('id'));
-
-    /// Launch add key groupid
-        $result = $result && add_key($table, $key);
-
-    /// Define index groupingid-groupid (unique) to be added to groups_groupings_groups
-        $table = new XMLDBTable('groups_groupings_groups');
-        $index = new XMLDBIndex('groupingid-groupid');
-        $index->setAttributes(XMLDB_INDEX_UNIQUE, array('groupingid', 'groupid'));
-
-    /// Launch add index groupingid-groupid
-        $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')) {
-            /// Need to drop foreign keys/indexes added in last upgrade, drop 'new' tables, then start again!!
-            $result = $result && groups_drop_keys_indexes_db();
-            $result = $result && groups_revert_db($renametemp=false);
-            $result = $result && install_from_xmldb_file($CFG->dirroot.'/group/db/install.xml');
-            $result = $result && groups_transfer_db();
-        }
-    }
-
-    return $result;
-}
-
-?>
Index: version.php
===================================================================
RCS file: /cvsroot/moodle/moodle/version.php,v
retrieving revision 1.497
diff -u -r1.497 version.php
--- version.php	25 Jul 2007 03:14:17 -0000	1.497
+++ version.php	27 Jul 2007 12:09:44 -0000
@@ -6,7 +6,7 @@
 // This is compared against the values stored in the database to determine
 // whether upgrades should be performed (see lib/db/*.php)
 
-    $version = 2007072500;  // YYYYMMDD = date
+    $version = 2007072501;  // YYYYMMDD = date
                             //       XY = increments within a single day
 
     $release = '1.9 dev';   // Human-friendly version name
