File:  [LON-CAPA] / loncom / automation / Autocreate.pl
Revision 1.6: download - view: text, annotated - select for diffs
Mon Jul 2 03:36:47 2007 UTC (16 years, 11 months ago) by raeburn
Branches: MAIN
CVS tags: version_2_5_1, version_2_5_0, version_2_4_99_0, HEAD
- move clone authorization check from loncreatecourse.pm to loncommon.pm
- more information stored in autocreation log
- include context as argument in loncommon::commit_standardrole and commit_studentrole
  to set appropriate line feeds (with/without <br /> for web/auto context).
- more information displayed on screen after course creation from uploaded attributes file (XML)
- language handler for Autocreate.pl

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

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