Annotation of loncom/automation/Autocreate.pl, revision 1.7

1.1       raeburn     1: #!/usr/bin/perl
                      2: #
                      3: # Automated Course Creation script
                      4: #
                      5: # Copyright Michigan State University Board of Trustees
                      6: #
                      7: # This file is part of the LearningOnline Network with CAPA (LON-CAPA).
                      8: #
                      9: # LON-CAPA is free software; you can redistribute it and/or modify
                     10: # it under the terms of the GNU General Public License as published by
                     11: # the Free Software Foundation; either version 2 of the License, or
                     12: # (at your option) any later version.
                     13: #
                     14: # LON-CAPA is distributed in the hope that it will be useful,
                     15: # but WITHOUT ANY WARRANTY; without even the implied warranty of
                     16: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
                     17: # GNU General Public License for more details.
                     18: #
                     19: # You should have received a copy of the GNU General Public License
                     20: # along with LON-CAPA; if not, write to the Free Software
                     21: # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
                     22: #
                     23: # /home/httpd/html/adm/gpl.txt
                     24: #
                     25: # http://www.lon-capa.org/
                     26: #
                     27: # Run as www. Call this from an entry in /etc/cron.d/loncapa
                     28: #
                     29: # www /home/httpd/perl/Autocreate.pl $dom $uname:$udom
                     30: #
                     31: # where $dom is the name of the course domain, $uname and $udom are the 
                     32: # username and domain of a Domain Coordinator in the domain.   
                     33: #
                     34:     use strict;
                     35:     use lib '/home/httpd/lib/perl';
                     36:     use Apache::lonnet;
1.6       raeburn    37:     use Apache::lonlocal;
1.1       raeburn    38:     use LONCAPA::batchcreatecourse;
                     39:     use LONCAPA::Configuration;
                     40: 
                     41:     my $perlvarref = &LONCAPA::Configuration::read_conf('loncapa.conf');
                     42:     my $logfile = $$perlvarref{'lonDaemons'}.'/logs/autocreate.log';
                     43:     my @domains = &Apache::lonnet::current_machine_domains();
                     44:     open (my $fh,">>$logfile");
                     45:     print $fh "********************\n".localtime(time)." Autocreation messages start --\n";
                     46:     if (@ARGV < 2) {
                     47:         print $fh "usage: ./Autocreate <coursedomain  username:domain>.\nPlease provide the username and domain of a Domain Coordinator.\n";
                     48:         exit;  
                     49:     }
                     50: # check if $defdom is a domain hosted on this library server. 
                     51:     my $defdom = $ARGV[0];
                     52:     my ($dcname,$dcdom) = ($ARGV[1] =~ /^([^:]+):([^:]+)$/);
                     53:     if ($defdom eq '' || !grep/^$defdom$/,@domains) {
                     54:         print $fh "The domain you supplied is not a valid domain for this server\n\n";
                     55:         close($fh);
                     56:         exit;
                     57:     }
                     58: # check if user is an active domain coordinator.
                     59:     if (!&check_activedc($dcdom,$dcname,$defdom)) {
                     60:         print $fh "The username you supplied for domain $defdom does not have an active domain coordinator role in the domain\n\n";
                     61:         close($fh);
                     62:         exit;
                     63:     }
1.6       raeburn    64: 
                     65:     # Initialize language handler
                     66:     &Apache::lonlocal::get_language_handle();
                     67: 
1.1       raeburn    68:     my $batchdir = $$perlvarref{'lonDaemons'}.'/tmp/addcourse/'.$defdom.'/auto';
                     69:     opendir(DIR,"$batchdir/pending");
                     70:     my @requests = grep(!/^\.\.?$/,readdir(DIR));
                     71:     closedir(DIR);
                     72:     my %courseids = ();
1.5       raeburn    73:     my @permissions = ('mau','ccc','cin','cta','cep','ccr','cst');
1.3       raeburn    74:     my %permissionflags = ();
                     75:     &set_permissions(\%permissionflags,\@permissions);
1.4       raeburn    76:     $env{'user.name'} = $dcname;
                     77:     $env{'user.domain'} = $dcdom;
1.6       raeburn    78:     $env{'request.role.domain'} = $defdom;
1.1       raeburn    79:     my $wwwid=getpwnam('www');
                     80:     if ($wwwid!=$<) {
                     81:         my $emailto=$$perlvarref{'lonAdmEMail'};
                     82:         my $subj="LON: $$perlvarref{'lonHostID'} User ID mismatch";
                     83:         my $requestmail = "To: $emailto\n";
                     84:         $requestmail .= 
                     85:         "Subject: LON: $$perlvarref{'lonHostID'} User ID mismatch\n".
                     86:         "User ID mismatch. Autocreate.pl must be run as user www\n"; 
                     87:         if ($emailto =~ /^[^\@]+\@[^\@]+$/) {
                     88:             if (open(MAIL, "|/usr/lib/sendmail -oi -t -odb")) {
                     89:                 print MAIL $requestmail;
                     90:                 close(MAIL);
                     91:                 print $fh "Autocreate.pl must be run as user www\n\n";
                     92:             } else {
                     93:                 print $fh "Could not send notification e-mail to $emailto\n\n"; 
                     94:             }
                     95:         } else {
                     96:             print $fh "Notification e-mail address for Administrator is not a valid e-mail address\n\n";
                     97:         }
                     98:         close($fh);
                     99:         exit;
                    100:     }
                    101: 
1.3       raeburn   102:     print $fh "Sending to batch - auto,$defdom,$dcname,$dcdom ".join(":",@requests)."\n";
                    103:     my ($result,$logmsg) = &LONCAPA::batchcreatecourse::create_courses(\@requests,\%courseids,'auto',$defdom,$dcname,$dcdom);
1.7     ! raeburn   104:     my $output;
        !           105:     if ($result ne '') {
        !           106:         $output = $result."\n";
        !           107:     }
        !           108:     if ($logmsg ne '') {
        !           109:         $output .= $logmsg."\n";    
        !           110:     }
1.1       raeburn   111: 
                    112: # Copy requests from pending directory to processed directory and unlink.
                    113:   foreach my $request (@requests) {  
                    114:         if ((-e "$batchdir/pending/$request") && $request !~ /\.\./ && $request ne '' &&$request ne './') {
                    115:             open(FILE,"<$batchdir/pending/$request");
                    116:             my @buffer = <FILE>;
                    117:             close(FILE);
1.3       raeburn   118:             if (!-e "$batchdir/processed") {
                    119:                 mkdir("$batchdir/processed", 0755);
                    120:             }
1.1       raeburn   121:             open(FILE,">$batchdir/processed/$request");
                    122:             print FILE @buffer;
                    123:             close(FILE);
                    124:             if (-e "$batchdir/processed/$request") {
                    125:                 unlink("$batchdir/pending/$request");
                    126:             }
                    127:         }
                    128:     }
                    129: 
                    130:     foreach my $key (sort keys %courseids) {
1.3       raeburn   131:         print $fh "created course: $key - $courseids{$key}\n";
                    132:         my $newcourse = &Apache::lonnet::escape($key.':'.$courseids{$key});
                    133:         $output .= $newcourse.':'; 
1.1       raeburn   134:     }
1.3       raeburn   135:     $output =~ s/:$//;
                    136:     print $output;
1.1       raeburn   137: 
1.3       raeburn   138:     &unset_permissions(\%permissionflags);
1.4       raeburn   139:     delete($env{'user.name'});
                    140:     delete($env{'user.domain'});
1.6       raeburn   141:     delete($env{'request.role.domain'});
1.1       raeburn   142:     print $fh "-- ".localtime(time)." Autocreation messages end\n*******************\n\n";
                    143:     close($fh);
                    144: 
                    145: sub check_activedc {
                    146:     my ($dcdom,$dcname,$defdom) = @_;
                    147:     my %dumphash=
                    148:             &Apache::lonnet::dump('roles',$dcdom,$dcname);
                    149:     my $now=time;
                    150:     my $activedc = 0;
                    151:     foreach my $item (keys %dumphash) {
                    152:         my ($domain,$role) = ($item =~ m-^/([^/]+)/[^_]*_(\w+)$-);
                    153:         if ($role eq 'dc' && $domain eq $defdom) {
                    154:             my ($trole,$tend,$tstart)=split(/_/,$dumphash{$item});
                    155:             if (($tend) && ($tend<$now)) { next; }
                    156:             if (($tstart) && ($now<$tstart)) { next; }
                    157:             $activedc = 1;
                    158:             last;
                    159:         }
                    160:     }
                    161:     return $activedc;
                    162: }
1.3       raeburn   163: 
                    164: sub set_permissions {
                    165:     my ($permissionflags,$permissions) = @_;
                    166:     foreach my $allowtype (@{$permissions}) {
1.4       raeburn   167:         unless($env{"allowed.$allowtype"}) {
                    168:             $env{"allowed.$allowtype"} = 'F';
1.3       raeburn   169:             $permissionflags{$allowtype} = 1;
                    170:         }
                    171:     }
                    172: }
                    173: 
                    174: sub unset_permissions {
                    175:     my ($permissionflags) = @_;
                    176:     foreach my $allowtype (keys %{$permissionflags}) {
1.4       raeburn   177:         delete($env{"allowed.$allowtype"});
1.3       raeburn   178:     }
                    179: }

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>