File:  [LON-CAPA] / loncom / enrollment / Autoenroll.pl
Revision 1.16: download - view: text, annotated - select for diffs
Tue Nov 15 20:53:50 2005 UTC (18 years, 6 months ago) by albertel
Branches: MAIN
CVS tags: version_2_1_X, version_2_1_3, version_2_1_2, version_2_1_1, version_2_1_0, version_2_0_99_1, HEAD
- safely try to pull in the compat enviironment where needed

    1: #!/usr/bin/perl
    2: #
    3: #Automated Enrollment script
    4: # $Id: Autoenroll.pl,v 1.16 2005/11/15 20:53:50 albertel Exp $
    5: #
    6: # Copyright Michigan State University Board of Trustees
    7: #
    8: # This file is part of the LearningOnline Network with CAPA (LON-CAPA).
    9: #
   10: # LON-CAPA is free software; you can redistribute it and/or modify
   11: # it under the terms of the GNU General Public License as published by
   12: # the Free Software Foundation; either version 2 of the License, or
   13: # (at your option) any later version.
   14: #
   15: # LON-CAPA is distributed in the hope that it will be useful,
   16: # but WITHOUT ANY WARRANTY; without even the implied warranty of
   17: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   18: # GNU General Public License for more details.
   19: #
   20: # You should have received a copy of the GNU General Public License
   21: # along with LON-CAPA; if not, write to the Free Software
   22: # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
   23: #
   24: # /home/httpd/html/adm/gpl.txt
   25: #
   26: # http://www.lon-capa.org/
   27: #
   28: BEGIN {
   29:     eval "use Apache2::compat();";
   30: };
   31:     use strict;
   32:     use lib '/home/httpd/lib/perl';
   33:     use localenroll;
   34:     use LONCAPA::Configuration;
   35:     use LONCAPA::Enrollment;
   36:     use Apache::lonnet;
   37:     use Apache::loncoursedata;
   38:     use Apache::lonmsg;
   39:     use HTML::Entities;
   40: 
   41: # Determine the library server's domain and hostID
   42:     my $perlvarref = LONCAPA::Configuration::read_conf('loncapa.conf');
   43:     my $logfile = $$perlvarref{'lonDaemons'}.'/logs/autoenroll.log';
   44:     my @domains = &Apache::lonnet::current_machine_domains();
   45:     my @hostids = &Apache::lonnet::current_machine_ids();
   46: 
   47: # Determine the present time;
   48:     my $timenow = time();
   49: 
   50: # For each domain ......
   51:     foreach my $dom (@domains) {
   52:     #only run if configured to
   53:         if (! &localenroll::run($dom)) { next; }
   54:         $env{'user.domain'} = $dom;
   55:     # Determine the courses
   56:         my %courses = &Apache::lonnet::courseiddump($dom,'.',1,'.','.','.',1,\@hostids); 
   57:         my %affiliates = ();
   58:         my %enrollvar = ();
   59:         my %reply = ();
   60:         my %LC_code = ();
   61:         foreach my $key (sort keys %courses) {
   62:             my $crs;
   63:             if ($key =~ m/^($dom)_(\w+)$/) {
   64:                 $crs = $2;
   65:             }
   66: 
   67: # Get course settings
   68:             my %settings = &Apache::lonnet::dump('environment',$dom,$crs);
   69:             %{$enrollvar{$crs}} = ();
   70:             @{$affiliates{$crs}} = ();
   71:             %{$LC_code{$crs}} = ();
   72:             foreach my $item (keys %settings) {
   73:                 if ($item =~ m/^internal\.(.+)$/) {
   74:                     $enrollvar{$crs}{$1} = $settings{$item};
   75:                 } elsif ($item eq 'description') {
   76:                     $enrollvar{$crs}{$item} = &HTML::Entities::decode($settings{$item});  
   77:                 } elsif ($item eq 'default_enrollment_start_date') {
   78:                     $enrollvar{$crs}{startdate} = $settings{$item};
   79:                 }  elsif ($item eq 'default_enrollment_end_date') {
   80:                     $enrollvar{$crs}{enddate} = $settings{$item};
   81:                 }
   82:             }
   83:             if (($enrollvar{$crs}{autostart} <= $timenow) && ( ($enrollvar{$crs}{autoend} > $timenow) || ($enrollvar{$crs}{autoend} == 0) ) ) {
   84:                 if ( ($enrollvar{$crs}{autoadds} == 1) || ($enrollvar{$crs}{autodrops} == 1) ) {
   85: # Add to list of classes for retrieval
   86:                     $enrollvar{$crs}{sectionnums} =~ s/ //g;
   87:                     $enrollvar{$crs}{crosslistings} =~ s/ //g;
   88:                     my @sections = ();
   89:                     my @crosslistings = ();
   90:                     if ($enrollvar{$crs}{sectionnums} =~ m/,/) {
   91:                         @sections = split/,/,$enrollvar{$crs}{sectionnums};
   92:                     } else {
   93:                         $sections[0] = $enrollvar{$crs}{sectionnums};
   94:                     }
   95:                     if ($enrollvar{$crs}{crosslistings} =~ m/,/) {
   96:                         @crosslistings = split/,/,$enrollvar{$crs}{crosslistings}
   97:                     } else {
   98:                         @crosslistings = $enrollvar{$crs}{crosslistings};
   99:                     }
  100:                     foreach my $sec (@sections) {
  101:                         if ($sec =~ m/^(\w+):(\w*)$/ ) {
  102:                             my $course_id = $enrollvar{$crs}{coursecode}.$1;
  103:                             my $gp = $2;
  104:                             if (!grep/^$course_id$/,@{$affiliates{$crs}}) {
  105:                                 push @{$affiliates{$crs}}, $course_id;
  106:                                 $LC_code{$crs}{$course_id} = $gp; 
  107:                             }
  108:                         }
  109:                     }
  110:                     foreach my $xlist (@crosslistings) {
  111:                         if ($xlist =~ m/^([^:]+):(\w*)$/) {
  112:                             my $course_id = $1;
  113:                             my $gp = $2;
  114:                             if (!grep/^$course_id$/,@{$affiliates{$crs}}) {
  115:                                 push @{$affiliates{$crs}}, $course_id;
  116:                                 $LC_code{$crs}{$course_id} = $gp;
  117:                             }
  118:                         }
  119:                     }
  120:                 }
  121:             }
  122:         }
  123:         my $outcome = &Apache::lonnet::fetch_enrollment_query('automated',\%affiliates,\%reply,$dom);
  124: 
  125: # Now go through classes and perform required enrollment changes.
  126:         open (my $fh,">>$logfile");
  127:         print $fh "********************\n".localtime(time)." Enrollment messages start --\n";
  128:         print $fh "Response from fetch_enrollment_query was $outcome\n";
  129:         foreach my $crs (sort keys %enrollvar) {
  130:             my $logmsg = '';
  131:             my $newusermsg = '';
  132:             if ($reply{$crs} > 0) {
  133:                 if ( ($enrollvar{$crs}{autostart} < $timenow) && ( ($enrollvar{$crs}{autoend} > $timenow) || ($enrollvar{$crs}{autoend} == 0) ) ) {
  134:                     if (($enrollvar{$crs}{autoadds} == 1) || ($enrollvar{$crs}{autodrops} == 1)) {
  135:                         my ($changecount,$response) = &LONCAPA::Enrollment::update_LC($dom,$crs,$enrollvar{$crs}{autoadds},$enrollvar{$crs}{autodrops},$enrollvar{$crs}{startdate},$enrollvar{$crs}{enddate},$enrollvar{$crs}{authtype},$enrollvar{$crs}{autharg},\@{$affiliates{$crs}},\%{$LC_code{$crs}},\$logmsg,\$newusermsg,'automated');
  136:                         print $fh "Messages start for $crs\n";
  137:                         print $fh "$logmsg\n";
  138:                         print $fh "Messages end for $crs\n";
  139:                         if ($changecount > 0) {
  140:                             unless ($enrollvar{$crs}{notifylist}  eq '') {
  141: # Send message about enrollment changes to notifylist.
  142: # Set $env{'user.name'}, $env{'user.home'} for use by logging in lonmsg
  143:                                 unless ( ($enrollvar{$crs}{'courseowner'} eq '') || (!defined($enrollvar{$crs}{'courseowner'}) )  ) {
  144:                                     $env{'user.name'} = $enrollvar{$crs}{'courseowner'};
  145:                                     $env{'user.home'} = &Apache::lonnet::homeserver($env{'user.name'},$dom);
  146: 
  147:                                     my $subject = "Student enrollment changes in $enrollvar{$crs}{coursecode}";
  148:                                     my $message = "The following $changecount change(s) occurred in $enrollvar{$crs}{description} - $enrollvar{$crs}{coursecode} as a result of the automated classlist update:\n\n".$response;
  149:                                     unless ($newusermsg eq '') { 
  150:                                         $message .= "\n".$newusermsg;
  151:                                     }
  152:                                     my @to_notify = ();
  153:                                     if ($enrollvar{$crs}{notifylist} =~ m/,/) {
  154:                                         @to_notify = split/,/,$enrollvar{$crs}{notifylist};
  155:                                     } else {
  156:                                         $to_notify[0] = $enrollvar{$crs}{notifylist};
  157:                                     }
  158:                                     foreach my $cc (@to_notify) {
  159:                                         my ($ccname,$ccdom) = split/@/,$cc;
  160:                                         my $status =  &Apache::lonmsg::user_normal_msg($ccname,$ccdom,$subject,$message);
  161:                                     }
  162:                                     if ( ($enrollvar{$crs}{notifylist} eq '') && ($newusermsg ne '') ) {
  163:                                         my $subject = "New user accounts in  $enrollvar{$crs}{'coursecode'}";
  164:                                         my $status =  &Apache::lonmsg::user_normal_msg($env{'user.name'},$dom,$subject,$newusermsg);
  165:                                     }
  166:                                     delete($env{'user.name'});
  167:                                     delete($env{'user.home'});
  168:                                 }
  169:                             }
  170:                         }
  171:                     }
  172:                 }
  173:             } else {
  174:                 if ( ($enrollvar{$crs}{autoadds} == 1) || ($enrollvar{$crs}{autodrops} == 1) ) {
  175:                     if ( ($enrollvar{$crs}{autostart} < $timenow) && ( ($enrollvar{$crs}{autoend} > $timenow) || ($enrollvar{$crs}{autoend} == 0) ) ) {
  176:                         print $fh "No institutional classlist data could be retrieved for $crs\n";
  177:                     } else {
  178:                         print $fh "Not within time window for auto-enrollment in $crs\n";
  179:                     }
  180:                 } else {
  181:                     print $fh "Auto-enrollment not currently enabled for $crs\n";
  182:                 }
  183:             }
  184:         }
  185:         print $fh "-- ".localtime(time)." Enrollment messages end\n*******************\n\n";
  186:         close($fh);
  187:         delete($env{'user.domain'});
  188: # Check for photos
  189:     }
  190: 
  191: 1;

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