--- loncom/automation/Autocreate.pl	2005/03/04 15:09:06	1.1
+++ loncom/automation/Autocreate.pl	2008/03/18 20:51:12	1.10
@@ -2,6 +2,8 @@
 #
 # Automated Course Creation script
 #
+# $Id: Autocreate.pl,v 1.10 2008/03/18 20:51:12 raeburn Exp $
+#
 # Copyright Michigan State University Board of Trustees
 #
 # This file is part of the LearningOnline Network with CAPA (LON-CAPA).
@@ -34,6 +36,7 @@
     use strict;
     use lib '/home/httpd/lib/perl';
     use Apache::lonnet;
+    use Apache::lonlocal;
     use LONCAPA::batchcreatecourse;
     use LONCAPA::Configuration;
 
@@ -60,17 +63,21 @@
         close($fh);
         exit;
     }
-                                                   
+
+    # Initialize language handler
+    &Apache::lonlocal::get_language_handle();
+
     my $batchdir = $$perlvarref{'lonDaemons'}.'/tmp/addcourse/'.$defdom.'/auto';
     opendir(DIR,"$batchdir/pending");
     my @requests = grep(!/^\.\.?$/,readdir(DIR));
     closedir(DIR);
     my %courseids = ();
-    my $cccflag = 0;
-    unless ($ENV{'allowed.ccc'}) {
-        $ENV{'allowed.ccc'} = 'F';
-        $cccflag = 1;
-    }
+    my @permissions = ('mau','ccc','cin','cta','cep','ccr','cst');
+    my %permissionflags = ();
+    &set_permissions(\%permissionflags,\@permissions);
+    $env{'user.name'} = $dcname;
+    $env{'user.domain'} = $dcdom;
+    $env{'request.role.domain'} = $defdom;
     my $wwwid=getpwnam('www');
     if ($wwwid!=$<) {
         my $emailto=$$perlvarref{'lonAdmEMail'};
@@ -94,14 +101,27 @@
         exit;
     }
 
-    my ($output,$logmsg) = &LONCAPA::batchcreatecourse::create_courses(\@requests,\%courseids,'auto',$defdom,$dcname,$dcdom);
+    print $fh "Sending to batch - auto,$defdom,$dcname,$dcdom ".join(":",@requests)."\n";
+    my ($result,$logmsg) = &LONCAPA::batchcreatecourse::create_courses(\@requests,\%courseids,'auto',$defdom,$dcname,$dcdom);
+    my $outcome;
+    if ($result ne '') {
+        $outcome = $result."\n";
+    }
+    if ($logmsg ne '') {
+        $outcome .= $logmsg."\n";    
+    }
+    print $fh $outcome;
 
+    my $output;
 # Copy requests from pending directory to processed directory and unlink.
-  foreach my $request (@requests) {  
+    foreach my $request (@requests) {  
         if ((-e "$batchdir/pending/$request") && $request !~ /\.\./ && $request ne '' &&$request ne './') {
             open(FILE,"<$batchdir/pending/$request");
             my @buffer = <FILE>;
             close(FILE);
+            if (!-e "$batchdir/processed") {
+                mkdir("$batchdir/processed", 0755);
+            }
             open(FILE,">$batchdir/processed/$request");
             print FILE @buffer;
             close(FILE);
@@ -112,12 +132,17 @@
     }
 
     foreach my $key (sort keys %courseids) {
-        print $fh "created course: $key\n";
-    }
-
-    if ($cccflag) {
-        delete($ENV{'allowed.ccc'});
-    }
+        print $fh "created course: $key - $courseids{$key}\n";
+        my $newcourse = &Apache::lonnet::escape($key.':'.$courseids{$key});
+        $output .= $newcourse.':'; 
+    }
+    $output =~ s/:$//;
+    print $output;
+
+    &unset_permissions(\%permissionflags);
+    delete($env{'user.name'});
+    delete($env{'user.domain'});
+    delete($env{'request.role.domain'});
     print $fh "-- ".localtime(time)." Autocreation messages end\n*******************\n\n";
     close($fh);
 
@@ -139,3 +164,20 @@ sub check_activedc {
     }
     return $activedc;
 }
+
+sub set_permissions {
+    my ($permissionflags,$permissions) = @_;
+    foreach my $allowtype (@{$permissions}) {
+        unless($env{"allowed.$allowtype"}) {
+            $env{"allowed.$allowtype"} = 'F';
+            $permissionflags{$allowtype} = 1;
+        }
+    }
+}
+
+sub unset_permissions {
+    my ($permissionflags) = @_;
+    foreach my $allowtype (keys %{$permissionflags}) {
+        delete($env{"allowed.$allowtype"});
+    }
+}