File:  [LON-CAPA] / loncom / enrollment / Autoenroll.pl
Revision 1.10: download - view: text, annotated - select for diffs
Wed Jun 9 17:01:56 2004 UTC (20 years ago) by raeburn
Branches: MAIN
CVS tags: HEAD
Fix bug #3062

    1: #!/usr/bin/perl
    2: #
    3: #Automated Enrollment script
    4: # $Id: Autoenroll.pl,v 1.10 2004/06/09 17:01:56 raeburn 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: 
   29:     use strict;
   30:     use lib '/home/httpd/lib/perl';
   31:     use localenroll;
   32:     use LONCAPA::Configuration;
   33:     use LONCAPA::Enrollment;
   34:     use Apache::lonnet;
   35:     use Apache::loncoursedata;
   36:     use Apache::lonmsg;
   37:     use HTML::Entities;
   38: 
   39: #only run if configured to
   40:     if (! &localenroll::run()) { exit; }
   41: 
   42: # Determine the library server's domain and hostID
   43:     my $perlvarref = LONCAPA::Configuration::read_conf('loncapa.conf');
   44:     my $dom = $$perlvarref{'lonDefDomain'};
   45:     my $logfile = $$perlvarref{'lonDaemons'}.'/logs/autoenroll.log';
   46:     $ENV{'user.domain'} = $dom;
   47:     my $hostid = $$perlvarref{'lonHostID'};
   48: 
   49: # Determine the present time;
   50:     my $timenow = time();
   51: 
   52: # Determine the courses
   53:     my %courses = &Apache::lonnet::courseiddump($dom,'.',1,$hostid); 
   54:     my %affiliates = ();
   55:     my %enrollvar = ();
   56:     my %reply = ();
   57:     my %LC_code = ();
   58:     foreach my $key (sort keys %courses) {
   59:         my $crs;
   60:         if ($key =~ m/^($dom)_(\w+)$/) {
   61:             $crs = $2;
   62:         }
   63: 
   64: # Get course settings
   65:         my %settings = &Apache::lonnet::dump('environment',$dom,$crs);
   66:         %{$enrollvar{$crs}} = ();
   67:         @{$affiliates{$crs}} = ();
   68:         %{$LC_code{$crs}} = ();
   69:         foreach my $item (keys %settings) {
   70:             if ($item =~ m/^internal\.(.+)$/) {
   71:                 $enrollvar{$crs}{$1} = $settings{$item};
   72:             } elsif ($item eq 'description') {
   73:                 $enrollvar{$crs}{$item} = &HTML::Entities::decode($settings{$item});  
   74:             }
   75:         }
   76:         if (($enrollvar{$crs}{autostart} <= $timenow) && ( ($enrollvar{$crs}{autoend} > $timenow) || ($enrollvar{$crs}{autoend} == 0) ) ) {
   77:             if ( ($enrollvar{$crs}{autoadds} == 1) || ($enrollvar{$crs}{autodrops} == 1) ) {
   78: # Add to list of classes for retrieval
   79:                 $enrollvar{$crs}{sectionnums} =~ s/ //g;
   80:                 $enrollvar{$crs}{crosslistings} =~ s/ //g;
   81:                 my @sections = ();
   82:                 my @crosslistings = ();
   83:                 if ($enrollvar{$crs}{sectionnums} =~ m/,/) {
   84:                     @sections = split/,/,$enrollvar{$crs}{sectionnums};
   85:                 } else {
   86:                     $sections[0] = $enrollvar{$crs}{sectionnums};
   87:                 }
   88:                 if ($enrollvar{$crs}{crosslistings} =~ m/,/) {
   89:                     @crosslistings = split/,/,$enrollvar{$crs}{crosslistings}
   90:                 } else {
   91:                     @crosslistings = $enrollvar{$crs}{crosslistings};
   92:                 }
   93:                 foreach my $sec (@sections) {
   94:                     if ($sec =~ m/^(\w+):(\w*)$/ ) {
   95:                         my $course_id = $enrollvar{$crs}{coursecode}.$1;
   96:                         my $gp = $2;
   97:                         if (!grep/^$course_id$/,@{$affiliates{$crs}}) {
   98:                             push @{$affiliates{$crs}}, $course_id;
   99:                             $LC_code{$crs}{$course_id} = $gp; 
  100:                         }
  101:                     }
  102:                 }
  103:                 foreach my $xlist (@crosslistings) {
  104:                     if ($xlist =~ m/^(\w+):(\w*)$/) {
  105:                         my $course_id = $1;
  106:                         my $gp = $2;
  107:                         if (!grep/^$course_id$/,@{$affiliates{$crs}}) {
  108:                             push @{$affiliates{$crs}}, $course_id;
  109:                             $LC_code{$crs}{$course_id} = $gp;
  110:                         }
  111:                     }
  112:                 }
  113:             }
  114:         }
  115:     }
  116:     my $outcome = &Apache::lonnet::fetch_enrollment_query('automated',$dom,\%affiliates,\%reply);
  117: 
  118: # Now go through classes and perform required enrollment changes.
  119:     open (my $fh,">>$logfile");
  120:     print $fh "********************\n".localtime(time)." Enrollment messages start --\n";
  121:     foreach my $crs (sort keys %enrollvar) {
  122:         my $logmsg = '';
  123:         my $newusermsg = '';
  124:         if ($reply{$crs} > 0) {
  125:             if ( ($enrollvar{$crs}{autostart} < $timenow) && ( ($enrollvar{$crs}{autoend} > $timenow) || ($enrollvar{$crs}{autoend} == 0) ) ) {
  126:                 if (($enrollvar{$crs}{autoadds} == 1) || ($enrollvar{$crs}{autodrops} == 1)) {
  127:                     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');
  128:                     print $fh "Messages start for $crs\n";
  129:                     print $fh "$logmsg\n";
  130:                     print $fh "Messages end for $crs\n";
  131:                     if ($changecount > 0) {
  132:                         unless ($enrollvar{$crs}{notifylist}  eq '') {
  133: # Send message about enrollment changes to notifylist.
  134: # Set $ENV{'user.name'}, $ENV{'user.home'} for use by logging in lonmsg
  135:                             unless ( ($enrollvar{$crs}{'courseowner'} eq '') || (!defined($enrollvar{$crs}{'courseowner'}) )  ) {
  136:                                 $ENV{'user.name'} = $enrollvar{$crs}{'courseowner'};
  137:                                 $ENV{'user.home'} = &Apache::lonnet::homeserver($ENV{'user.name'},$dom);
  138: 
  139:                                 my $subject = "Student enrollment changes in $enrollvar{$crs}{coursecode}";
  140:                                 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;
  141:                                 unless ($newusermsg eq '') 
  142:                                 {
  143:                                    $message .= "\n".$newusermsg;
  144:                                 }
  145:                                 my @to_notify = ();
  146:                                 if ($enrollvar{$crs}{notifylist} =~ m/,/) {
  147:                                     @to_notify = split/,/,$enrollvar{$crs}{notifylist};
  148:                                 } else {
  149:                                     $to_notify[0] = $enrollvar{$crs}{notifylist};
  150:                                 }
  151:                                 foreach my $cc (@to_notify) {
  152:                                     my ($ccname,$ccdom) = split/@/,$cc;
  153:                                     my $status =  &Apache::lonmsg::user_normal_msg($ccname,$ccdom,$subject,$message);
  154:                                 }
  155:                                 if ( ($enrollvar{$crs}{notifylist} eq '') && ($newusermsg ne '') ) {
  156:                                      my $subject = "New user accounts in  $enrollvar{$crs}{'coursecode'}";
  157:                                      my $status =  &Apache::lonmsg::user_normal_msg($ENV{'user.name'},$dom,$subject,$newusermsg);
  158:                                 }
  159:                                 delete($ENV{'user.name'});
  160:                                 delete($ENV{'user.home'});
  161:                             }
  162:                         }
  163:                     }
  164:                 }
  165:             }
  166:         } else {
  167:             if ( ($enrollvar{$crs}{autoadds} == 1) || ($enrollvar{$crs}{autodrops} == 1) ) {
  168:                 if ( ($enrollvar{$crs}{autostart} < $timenow) && ( ($enrollvar{$crs}{autoend} > $timenow) || ($enrollvar{$crs}{autoend} == 0) ) ) {
  169:                     print $fh "No institutional classlist data could be retrieved for $crs\n";
  170:                 } else {
  171:                     print $fh "Not within time window for auto-enrollment in $crs\n";
  172:                 }
  173:             } else {
  174:                 print $fh "Auto-enrollment not currently enabled for $crs\n";
  175:             }
  176:         }
  177:     }
  178:     print $fh "-- ".localtime(time)." Enrollment messages end\n*******************\n\n";
  179:     close($fh);
  180:     delete($ENV{'user.domain'});
  181: 
  182: # Check for photos
  183: 
  184: 1;

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