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

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.8     ! raeburn   104:     my $outcome;
1.7       raeburn   105:     if ($result ne '') {
1.8     ! raeburn   106:         $outcome = $result."\n";
1.7       raeburn   107:     }
                    108:     if ($logmsg ne '') {
1.8     ! raeburn   109:         $outcome .= $logmsg."\n";    
1.7       raeburn   110:     }
1.8     ! raeburn   111:     print $fh $outcome;
1.1       raeburn   112: 
1.8     ! raeburn   113:     my $output;
1.1       raeburn   114: # Copy requests from pending directory to processed directory and unlink.
1.8     ! raeburn   115:     foreach my $request (@requests) {  
1.1       raeburn   116:         if ((-e "$batchdir/pending/$request") && $request !~ /\.\./ && $request ne '' &&$request ne './') {
                    117:             open(FILE,"<$batchdir/pending/$request");
                    118:             my @buffer = <FILE>;
                    119:             close(FILE);
1.3       raeburn   120:             if (!-e "$batchdir/processed") {
                    121:                 mkdir("$batchdir/processed", 0755);
                    122:             }
1.1       raeburn   123:             open(FILE,">$batchdir/processed/$request");
                    124:             print FILE @buffer;
                    125:             close(FILE);
                    126:             if (-e "$batchdir/processed/$request") {
                    127:                 unlink("$batchdir/pending/$request");
                    128:             }
                    129:         }
                    130:     }
                    131: 
                    132:     foreach my $key (sort keys %courseids) {
1.3       raeburn   133:         print $fh "created course: $key - $courseids{$key}\n";
                    134:         my $newcourse = &Apache::lonnet::escape($key.':'.$courseids{$key});
                    135:         $output .= $newcourse.':'; 
1.1       raeburn   136:     }
1.3       raeburn   137:     $output =~ s/:$//;
                    138:     print $output;
1.1       raeburn   139: 
1.3       raeburn   140:     &unset_permissions(\%permissionflags);
1.4       raeburn   141:     delete($env{'user.name'});
                    142:     delete($env{'user.domain'});
1.6       raeburn   143:     delete($env{'request.role.domain'});
1.1       raeburn   144:     print $fh "-- ".localtime(time)." Autocreation messages end\n*******************\n\n";
                    145:     close($fh);
                    146: 
                    147: sub check_activedc {
                    148:     my ($dcdom,$dcname,$defdom) = @_;
                    149:     my %dumphash=
                    150:             &Apache::lonnet::dump('roles',$dcdom,$dcname);
                    151:     my $now=time;
                    152:     my $activedc = 0;
                    153:     foreach my $item (keys %dumphash) {
                    154:         my ($domain,$role) = ($item =~ m-^/([^/]+)/[^_]*_(\w+)$-);
                    155:         if ($role eq 'dc' && $domain eq $defdom) {
                    156:             my ($trole,$tend,$tstart)=split(/_/,$dumphash{$item});
                    157:             if (($tend) && ($tend<$now)) { next; }
                    158:             if (($tstart) && ($now<$tstart)) { next; }
                    159:             $activedc = 1;
                    160:             last;
                    161:         }
                    162:     }
                    163:     return $activedc;
                    164: }
1.3       raeburn   165: 
                    166: sub set_permissions {
                    167:     my ($permissionflags,$permissions) = @_;
                    168:     foreach my $allowtype (@{$permissions}) {
1.4       raeburn   169:         unless($env{"allowed.$allowtype"}) {
                    170:             $env{"allowed.$allowtype"} = 'F';
1.3       raeburn   171:             $permissionflags{$allowtype} = 1;
                    172:         }
                    173:     }
                    174: }
                    175: 
                    176: sub unset_permissions {
                    177:     my ($permissionflags) = @_;
                    178:     foreach my $allowtype (keys %{$permissionflags}) {
1.4       raeburn   179:         delete($env{"allowed.$allowtype"});
1.3       raeburn   180:     }
                    181: }

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