--- loncom/automation/Autocreate.pl	2011/03/06 21:44:14	1.18
+++ loncom/automation/Autocreate.pl	2020/07/18 00:16:00	1.20.2.1
@@ -2,7 +2,7 @@
 #
 # Automated Course Creation script
 #
-# $Id: Autocreate.pl,v 1.18 2011/03/06 21:44:14 raeburn Exp $
+# $Id: Autocreate.pl,v 1.20.2.1 2020/07/18 00:16:00 raeburn Exp $
 #
 # Copyright Michigan State University Board of Trustees
 #
@@ -110,15 +110,14 @@
                 close($fh);
                 exit;
             }
-            $env{'user.name'} = $dcname;
-            $env{'user.domain'} = $dcdom;
-            $env{'request.role.domain'} = $defdom;
+            &set_dc_env($dcname,$dcdom,$defdom);
             my @permissions = ('mau','ccc','cin','cta','cep','ccr','cst');
             my %permissionflags = ();
             &set_permissions(\%permissionflags,\@permissions);
             my $output = &process_xml($fh,$defdom,$dcname,$dcdom);
-            print $output;
             &unset_permissions(\%permissionflags);
+            &unset_dc_env();
+            print $output;
         }
     } else {
         my @permissions = ('mau','ccc','cin','cta','cep','ccr','cst');
@@ -149,7 +148,9 @@
                     }
                 }
                 if ($settings->{'req'}) {
+                    &set_dc_env($dcname,$dcdom);
                     my $output = &Apache::loncoursequeueadmin::process_official_reqs('auto',$dom,$dcname,$dcdom);
+                    &unset_dc_env();
                     if ($output) {
                         print $fh $output;
                     }
@@ -164,10 +165,7 @@
 
 sub process_xml {
     my ($fh,$dom,$dcname,$dcdom) = @_;
-    $env{'user.name'} = $dcname;
-    $env{'user.domain'} = $dcdom;
-    $env{'request.role.domain'} = $dom;
-
+    &set_dc_env($dcname,$dcdom,$dom);
     # Initialize language handler
     &Apache::lonlocal::get_language_handle();
 
@@ -177,7 +175,8 @@ sub process_xml {
     closedir(DIR);
     my %courseids = ();
     print $fh "Sending to batch - auto,$dom,$dcname,$dcdom ".join(":",@requests)."\n";
-    my ($result,$logmsg) = &LONCAPA::batchcreatecourse::create_courses(\@requests,\%courseids,'auto',$dom,$dcname,$dcdom);
+    my ($result,$logmsg,$clonemsg,$keysmsg,$codesref,$instcodesref) =
+        &LONCAPA::batchcreatecourse::create_courses(\@requests,\%courseids,'auto',$dom,$dcname,$dcdom);
     my $outcome;
     if ($result ne '') {
         $outcome = $result."\n";
@@ -185,6 +184,9 @@ sub process_xml {
     if ($logmsg ne '') {
         $outcome .= $logmsg."\n";    
     }
+    if ($clonemsg ne '') {
+        $outcome .= $clonemsg."\n";
+    }
     print $fh $outcome;
 
     my $output;
@@ -211,9 +213,7 @@ sub process_xml {
         $output .= $newcourse.':';
     }
     $output =~ s/:$//;
-    delete($env{'user.name'});
-    delete($env{'user.domain'});
-    delete($env{'request.role.domain'});
+    &unset_dc_env();
     return $output;
 }
 
@@ -243,3 +243,24 @@ sub unset_permissions {
         delete($env{"allowed.$allowtype"});
     }
 }
+
+sub set_dc_env {
+    my ($dcname,$dcdom,$defdom) = @_;
+    $env{'user.name'} = $dcname;
+    $env{'user.domain'} = $dcdom;
+    $env{'user.home'} = &Apache::lonnet::homeserver($dcname,$dcdom);
+    if ($defdom ne '') {
+        $env{'request.role.domain'} = $defdom;
+    }
+    return;
+}
+
+sub unset_dc_env {
+    delete($env{'user.name'});
+    delete($env{'user.domain'});
+    delete($env{'user.home'});
+    if ($env{'request.role.domain'}) {
+        delete($env{'request.role.domain'});
+    }
+    return;
+}