Index: backup/restore_form.html
===================================================================
RCS file: /cvsroot/moodle/moodle/backup/restore_form.html,v
retrieving revision 1.63
diff -u -r1.63 restore_form.html
--- backup/restore_form.html	5 Dec 2007 16:14:29 -0000	1.63
+++ backup/restore_form.html	5 Dec 2007 17:48:25 -0000
@@ -77,6 +77,14 @@
         $restore_users = 1;
     }
 
+    if (!isset($restore_groups)) {
+        if (empty($CFG->enablegroupings)) {
+            $restore_groups = RESTORE_GROUPS_ONLY;
+        } else {
+            $restore_groups = RESTORE_GROUPS_GROUPINGS;
+        }
+    }
+
     if (!isset($restore_logs)) {
         $restore_logs = 1;
     }
@@ -411,6 +419,28 @@
         }
         echo "</td></tr>";
 
+        //Now print the Groups tr (assume there is no $info->backup_groups)
+        echo "<tr>";
+        echo "<td align=\"right\" colspan=\"2\"><b>";
+        if (empty($CFG->enablegroupings)) {
+            echo get_string('groups').":";
+            echo "</b></td><td colspan=\"2\">";
+            $group_options[0] = get_string('no');
+            $group_options[RESTORE_GROUPS_ONLY] = get_string('yes');
+        } else {
+            echo get_string('groupsgroupings', 'group').":";
+            echo "</b></td><td colspan=\"2\">";
+            $group_options[0] = get_string('none');
+            $group_options[RESTORE_GROUPINGS_ONLY] = get_string('groupingsonly', 'group');
+            $group_options[RESTORE_GROUPS_GROUPINGS] = get_string('groupsgroupings', 'group'); //all.
+
+        } /*else {
+            echo get_string('none');
+            echo "<input type=\"hidden\" name=\"restore_groups\" value=\"2\" />";
+        }*/
+        choose_from_menu($group_options, 'restore_groups', $restore_groups, '');
+        echo "</td></tr>";
+
         //Now print the Logs tr
         echo "<tr>";
         echo "<td align=\"right\" colspan=\"2\"><b>";
Index: backup/restorelib.php
===================================================================
RCS file: /cvsroot/moodle/moodle/backup/restorelib.php,v
retrieving revision 1.292
diff -u -r1.292 restorelib.php
--- backup/restorelib.php	26 Nov 2007 03:21:46 -0000	1.292
+++ backup/restorelib.php	5 Dec 2007 17:48:30 -0000
@@ -1,6 +1,22 @@
 <?php //$Id: restorelib.php,v 1.292 2007/11/26 03:21:46 toyomoyo Exp $
     //Functions used in restore
 
+//define('RESTORE_GROUPS_NONE', 0);
+/**
+ * Group backup/restore constants, 1.
+ */
+define('RESTORE_GROUPS_ONLY', 1);
+
+/**
+ * Group backup/restore constants, 2.
+ */
+define('RESTORE_GROUPINGS_ONLY', 2);
+
+/**
+ * Group backup/restore constants, course/all.
+ */
+define('RESTORE_GROUPS_GROUPINGS', 3);
+
     //This function unzips a zip file in the same directory that it is
     //It automatically uses pclzip or command line unzip
     function restore_unzip ($file) {
@@ -459,6 +475,17 @@
             $tab[$elem][0] = "<b>".get_string("users").":</b>";
             $tab[$elem][1] = get_string($info->backup_users);
             $elem++;
+            //Groups info
+            if (empty($CFG->enablegroupings)) {
+                $tab[$elem][0] = "<b>".get_string('groups').":</b>";
+            } else {
+                $tab[$elem][0] = "<b>".get_string('groupsgroupings','group').":</b>";
+            }
+            if (!isset($info->backup_groups)) {  //Backwards compatibility.
+                $info->backup_groups = 'course';
+            }
+            $tab[$elem][1] = get_string($info->backup_groups);
+            $elem++;
             //Logs info
             $tab[$elem][0] = "<b>".get_string("logs").":</b>";
             if ($info->backup_logs == "true") {
@@ -905,6 +932,7 @@
                             backup_putid ($restore->backup_unique_code,"block_instance",$oldid,$instance->id);
                         }
                         $restore->blockinstanceids[] = $newid;
+                           //TODO: $newid undefined.
                     } else {
                         $status = false;
                         break;
@@ -1038,7 +1066,8 @@
                                         $course_module->indent = $mod->indent;
                                         $course_module->visible = $mod->visible;
                                         $course_module->groupmode = $mod->groupmode;
-                                        if ($mod->groupingid and $grouping = backup_getid($restore->backup_unique_code,"groupings",$mod->groupingid)) {
+                                        if( ($restore->groups == RESTORE_GROUPINGS_ONLY or $restore->groups == RESTORE_GROUPS_GROUPINGS)
+                                            and $mod->groupingid and $grouping = backup_getid($restore->backup_unique_code,"groupings",$mod->groupingid) ) {
                                             $course_module->groupingid = $grouping->new_id;
                                         } else {
                                             $course_module->groupingid = 0;
@@ -2840,6 +2869,20 @@
         return $status;
     }
 
+    /**
+     * Recode group ID field, and set group ID based on restore options.
+     * @return object Group object with new_id field.
+     */
+    function restore_group_getid($restore, $groupid) {
+        //We have to recode the groupid field
+        $group = backup_getid($restore->backup_unique_code, 'groups', $groupid);
+        
+        if ($restore->groups == 0 or $restore->groups == RESTORE_GROUPINGS_ONLY) {
+            $group->new_id = 0;
+        }
+        return $group;
+    }
+
     //This function creates all the groups
     function restore_create_groups($restore,$xml_file) {
 
@@ -3018,7 +3061,14 @@
                 $gro = new Object();
                 ///$gro->id = backup_todb($info['GROUPING']['#']['ID']['0']['#']);
                 $gro->courseid    = $restore->course_id;
-                $gro->name        = backup_todb($info['GROUPING']['#']['NAME']['0']['#']);
+                $oldname          = backup_todb($info['GROUPING']['#']['NAME']['0']['#']);
+                $oldshortname     = $restore->course_shortname;
+                if (false===strpos($oldname, $oldshortname)) {
+                    $gro->name = $oldname;
+                }
+                elseif ($shortname = get_field('course', 'shortname', 'id', $restore->course_id)) {
+                    $gro->name = preg_replace("/$oldshortname/", $shortname, $oldname);
+                }
                 $gro->description = backup_todb($info['GROUPING']['#']['DESCRIPTION']['0']['#']);
                 $gro->configdata  = backup_todb($info['GROUPING']['#']['CONFIGDATA']['0']['#']);
                 $gro->timecreated = backup_todb($info['GROUPING']['#']['TIMECREATED']['0']['#']);
@@ -6940,7 +6990,7 @@
 
 
         //Now create groups as needed
-        if ($status) {
+        if ($status and ($restore->groups == RESTORE_GROUPS_ONLY or $restore->groups == RESTORE_GROUPS_GROUPINGS)) {
             if (!defined('RESTORE_SILENTLY')) {
                 echo "<li>".get_string("creatinggroups");
             }
@@ -6958,7 +7008,7 @@
         }
 
         //Now create groupings as needed
-        if ($status) {
+        if ($status and ($restore->groups == RESTORE_GROUPINGS_ONLY or $restore->groups == RESTORE_GROUPS_GROUPINGS)) {
             if (!defined('RESTORE_SILENTLY')) {
                 echo "<li>".get_string("creatinggroupings");
             }
@@ -6976,7 +7026,7 @@
         }
 
         //Now create groupingsgroups as needed
-        if ($status) {
+        if ($status and $restore->groups == RESTORE_GROUPS_GROUPINGS) {
             if (!defined('RESTORE_SILENTLY')) {
                 echo "<li>".get_string("creatinggroupingsgroups");
             }
Index: backup/restore_check.html
===================================================================
RCS file: /cvsroot/moodle/moodle/backup/restore_check.html,v
retrieving revision 1.48
diff -u -r1.48 restore_check.html
--- backup/restore_check.html	20 Sep 2007 15:37:36 -0000	1.48
+++ backup/restore_check.html	5 Dec 2007 17:48:25 -0000
@@ -84,6 +84,8 @@
         $restore_metacourse = required_param('restore_metacourse');
         //restore_users
         $restore_users = required_param('restore_users');
+
+        $restore_groups = required_param('restore_groups');
         //restore_logs
         $restore_logs = required_param('restore_logs');
         //restore_user_files
@@ -126,6 +128,7 @@
         $restore->restoreto=$restore_restoreto;
         $restore->metacourse=$restore_metacourse;
         $restore->users=$restore_users;
+        $restore->groups=$restore_groups;
         $restore->logs=$restore_logs;
         $restore->user_files=$restore_user_files;
         $restore->course_files=$restore_course_files;
Index: lang/en_utf8/group.php
===================================================================
RCS file: /cvsroot/moodle/moodle/lang/en_utf8/group.php,v
retrieving revision 1.24
diff -u -r1.24 group.php
--- lang/en_utf8/group.php	19 Nov 2007 20:31:59 -0000	1.24
+++ lang/en_utf8/group.php	5 Dec 2007 17:48:30 -0000
@@ -128,4 +128,7 @@
 $string['nogroupsassigned'] = 'No groups assigned';
 $string['evenallocation'] = 'Note: To keep group allocation even, the actual number of members per group differs from the number you specified.';
 
+$string['groupsgroupings'] = 'Groups &amp; groupings';
+$string['groupingsonly'] = 'Groupings only';
+
 ?>
Index: lang/en_utf8/moodle.php
===================================================================
RCS file: /cvsroot/moodle/moodle/lang/en_utf8/moodle.php,v
retrieving revision 1.152
diff -u -r1.152 moodle.php
--- lang/en_utf8/moodle.php	26 Nov 2007 04:10:12 -0000	1.152
+++ lang/en_utf8/moodle.php	5 Dec 2007 17:48:31 -0000
@@ -1591,6 +1591,7 @@
 $string['writinggradebookinfo'] = 'Writing gradebook info';
 $string['writinggroupsinfo'] = 'Writing groups info';
 $string['writinggroupingsinfo'] = 'Writing groupings info';
+$string['writinggroupingsgroupsinfo'] = 'Writing groupings-groups info';
 $string['writingheader'] = 'Writing header';
 $string['writingloginfo'] = 'Writing logs info';
 $string['writingmessagesinfo'] = 'Writing messages info';
Index: mod/forum/restorelib.php
===================================================================
RCS file: /cvsroot/moodle/moodle/mod/forum/restorelib.php,v
retrieving revision 1.60
diff -u -r1.60 restorelib.php
--- mod/forum/restorelib.php	19 Dec 2006 07:00:14 -0000	1.60
+++ mod/forum/restorelib.php	5 Dec 2007 17:48:32 -0000
@@ -272,6 +272,7 @@
             $olduserid = backup_todb($dis_info['#']['USERID']['0']['#']);
 
             //Now, build the FORUM_DISCUSSIONS record structure
+            $discussion = new object();
             $discussion->forum = $forum_id;
             $discussion->course = $restore->course_id;
             $discussion->name = backup_todb($dis_info['#']['NAME']['0']['#']);
@@ -293,7 +294,7 @@
             }
 
             //We have to recode the groupid field
-            $group = backup_getid($restore->backup_unique_code, 'groups', $discussion->groupid);
+            $group = restore_group_getid($restore, $discussion->groupid);
             if ($group) {
                 $discussion->groupid = $group->new_id;
             }
Index: mod/data/restorelib.php
===================================================================
RCS file: /cvsroot/moodle/moodle/mod/data/restorelib.php,v
retrieving revision 1.27
diff -u -r1.27 restorelib.php
--- mod/data/restorelib.php	17 Sep 2007 21:58:55 -0000	1.27
+++ mod/data/restorelib.php	5 Dec 2007 17:48:31 -0000
@@ -232,7 +232,7 @@
         $record -> timemodified = backup_todb($rec_info['#']['TIMEMODIFIED']['0']['#']);
         $record -> approved = backup_todb($rec_info['#']['APPROVED']['0']['#']);
         $user = backup_getid($restore->backup_unique_code,"user",$record->userid);
-        $group= backup_getid($restore->backup_unique_code,"groups",$record->groupid);
+        $group= restore_group_getid($restore, $record->groupid);
 
         if ($user) {
             $record->userid = $user->new_id;
Index: mod/wiki/restorelib.php
===================================================================
RCS file: /cvsroot/moodle/moodle/mod/wiki/restorelib.php,v
retrieving revision 1.17
diff -u -r1.17 restorelib.php
--- mod/wiki/restorelib.php	17 Oct 2006 10:05:42 -0000	1.17
+++ mod/wiki/restorelib.php	5 Dec 2007 17:48:32 -0000
@@ -108,6 +108,7 @@
             $oldid = backup_todb($ent_info['#']['ID']['0']['#']);
 
             //Now, build the wiki_ENTRIES record structure
+            $entry = new object();
             $entry->wikiid = $new_wiki_id;
             $entry->course = $restore->course_id;
             $entry->userid = backup_todb($ent_info['#']['USERID']['0']['#']);
@@ -121,7 +122,7 @@
                 $entry->userid = $user->new_id;
             }
             //We have to recode the groupid field
-            $group = backup_getid($restore->backup_unique_code, 'groups', $entry->groupid);
+            $group = restore_group_getid($restore, $entry->groupid);
             if ($group) {
                 $entry->groupid = $group->new_id;
             }
Index: mod/chat/restorelib.php
===================================================================
RCS file: /cvsroot/moodle/moodle/mod/chat/restorelib.php,v
retrieving revision 1.22
diff -u -r1.22 restorelib.php
--- mod/chat/restorelib.php	26 Oct 2006 20:08:52 -0000	1.22
+++ mod/chat/restorelib.php	5 Dec 2007 17:48:31 -0000
@@ -102,6 +102,7 @@
             $olduserid = backup_todb($mes_info['#']['USERID']['0']['#']);
 
             //Now, build the CHAT_MESSAGES record structure
+            $message = new object();
             $message->chatid = $new_chat_id;
             $message->userid = backup_todb($mes_info['#']['USERID']['0']['#']);
             $message->groupid = backup_todb($mes_info['#']['GROUPID']['0']['#']);
@@ -116,7 +117,7 @@
             }
 
             //We have to recode the groupid field
-            $group = backup_getid($restore->backup_unique_code, 'groups', $message->groupid);
+            $group = restore_group_getid($restore, $message->groupid);
             if ($group) {
                 $message->groupid = $group->new_id;
             }
