File:  [LON-CAPA] / loncom / enrollment / Autoenroll.pl
Revision 1.25: download - view: text, annotated - select for diffs
Fri Mar 2 21:38:18 2007 UTC (17 years, 3 months ago) by raeburn
Branches: MAIN
CVS tags: version_2_3_X, HEAD
Localization for text
 - sent to screen (if enrollment changes resulted from running "Update Roster Now"), or
 - logged to autoenroll.log log file (if changes resulted from running Autoenroll.pl).

    1: #!/usr/bin/perl
    2: #
    3: #Automated Enrollment script
    4: # $Id: Autoenroll.pl,v 1.25 2007/03/02 21:38:18 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:     use strict;
   29:     use lib '/home/httpd/lib/perl';
   30:     use localenroll;
   31:     use LONCAPA::Configuration;
   32:     use LONCAPA::Enrollment;
   33:     use Apache::lonnet;
   34:     use Apache::loncoursedata;
   35:     use Apache::lonmsg;
   36:     use Apache::longroup;
   37:     use Apache::loncommon;
   38:     use Apache::lonlocal;
   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:         # Initialize language handler
   56:         &Apache::lonlocal::get_language_handle();
   57:         # Determine the courses
   58:         my %courses = &Apache::lonnet::courseiddump($dom,'.',1,'.','.','.',1,\@hostids,'Course'); 
   59:         my %affiliates = ();
   60:         my %enrollvar = ();
   61:         my %reply = ();
   62:         my %LC_code = ();
   63:         foreach my $key (sort keys %courses) {
   64:             my $crs;
   65:             if ($key =~ m/^($dom)_(\w+)$/) {
   66:                 $crs = $2;
   67:             }
   68: 
   69: # Get course settings
   70:             my %settings = &Apache::lonnet::dump('environment',$dom,$crs);
   71:             %{$enrollvar{$crs}} = ();
   72:             @{$affiliates{$crs}} = ();
   73:             %{$LC_code{$crs}} = ();
   74:             foreach my $item (keys %settings) {
   75:                 if ($item =~ m/^internal\.(.+)$/) {
   76:                     $enrollvar{$crs}{$1} = $settings{$item};
   77:                 } elsif ($item eq 'description') {
   78:                     $enrollvar{$crs}{$item} = &HTML::Entities::decode($settings{$item});  
   79:                 } elsif ($item eq 'default_enrollment_start_date') {
   80:                     $enrollvar{$crs}{startdate} = $settings{$item};
   81:                 }  elsif ($item eq 'default_enrollment_end_date') {
   82:                     $enrollvar{$crs}{enddate} = $settings{$item};
   83:                 }
   84:             }
   85:             if (($enrollvar{$crs}{autostart} <= $timenow) && ( ($enrollvar{$crs}{autoend} > $timenow) || ($enrollvar{$crs}{autoend} == 0) ) ) {
   86:                 if ( ($enrollvar{$crs}{autoadds} == 1) || ($enrollvar{$crs}{autodrops} == 1) ) {
   87: # Add to list of classes for retrieval
   88:                     $enrollvar{$crs}{sectionnums} =~ s/ //g;
   89:                     $enrollvar{$crs}{crosslistings} =~ s/ //g;
   90:                     my @sections = ();
   91:                     my @crosslistings = ();
   92:                     if ($enrollvar{$crs}{sectionnums} =~ m/,/) {
   93:                         @sections = split/,/,$enrollvar{$crs}{sectionnums};
   94:                     } else {
   95:                         $sections[0] = $enrollvar{$crs}{sectionnums};
   96:                     }
   97:                     if ($enrollvar{$crs}{crosslistings} =~ m/,/) {
   98:                         @crosslistings = split/,/,$enrollvar{$crs}{crosslistings}
   99:                     } else {
  100:                         @crosslistings = $enrollvar{$crs}{crosslistings};
  101:                     }
  102:                     foreach my $sec (@sections) {
  103:                         if ($sec =~ m/^(\w+):(\w*)$/ ) {
  104:                             my $course_id = $enrollvar{$crs}{coursecode}.$1;
  105:                             my $gp = $2;
  106:                             if (!grep/^$course_id$/,@{$affiliates{$crs}}) {
  107:                                 push @{$affiliates{$crs}}, $course_id;
  108:                                 $LC_code{$crs}{$course_id} = $gp; 
  109:                             }
  110:                         }
  111:                     }
  112:                     foreach my $xlist (@crosslistings) {
  113:                         if ($xlist =~ m/^([^:]+):(\w*)$/) {
  114:                             my $course_id = $1;
  115:                             my $gp = $2;
  116:                             if (!grep/^$course_id$/,@{$affiliates{$crs}}) {
  117:                                 push @{$affiliates{$crs}}, $course_id;
  118:                                 $LC_code{$crs}{$course_id} = $gp;
  119:                             }
  120:                         }
  121:                     }
  122:                 }
  123:             }
  124:         }
  125:         my $outcome = &Apache::lonnet::fetch_enrollment_query('automated',\%affiliates,\%reply,$dom);
  126: 
  127: # Now go through classes and perform required enrollment changes.
  128:         open (my $fh,">>$logfile");
  129:         print $fh "********************\n".localtime(time).' '.&mt('Enrollment messages start').' --'."\n";
  130:         print $fh &mt("Response from [_1] was [_2]",'fetch_enrollment_query',$outcome)."\n";
  131:         foreach my $crs (sort keys %enrollvar) {
  132:             my $logmsg = '';
  133:             my $newusermsg = '';
  134:             if ($reply{$crs} > 0) {
  135:                 if ( ($enrollvar{$crs}{autostart} < $timenow) && ( ($enrollvar{$crs}{autoend} > $timenow) || ($enrollvar{$crs}{autoend} == 0) ) ) {
  136:                     if (($enrollvar{$crs}{autoadds} == 1) || ($enrollvar{$crs}{autodrops} == 1)) {
  137:                         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');
  138:                         print $fh &mt('Messages start for [_1]',$crs)."\n";
  139:                         print $fh "$logmsg\n";
  140:                         print $fh &mt('Messages end for [_1]',$crs)."\n";
  141:                         if ($changecount > 0) {
  142:                             unless ($enrollvar{$crs}{notifylist}  eq '') {
  143: # Send message about enrollment changes to notifylist.
  144: # Set $env{'user.name'}, $env{'user.domain'}, $env{'user.home'}
  145: # for use by logging in lonmsg
  146:                                 unless ( ($enrollvar{$crs}{'courseowner'} eq '') || (!defined($enrollvar{$crs}{'courseowner'}) )  ) {
  147:                                     if ($enrollvar{$crs}{'courseowner'} =~ /:/) {
  148:                                         ($env{'user.name'},$env{'user.domain'}) = split(/:/,$enrollvar{$crs}{'courseowner'});
  149:                                     } else {
  150:                                         $env{'user.name'} = $enrollvar{$crs}{'courseowner'};
  151:                                         $env{'user.domain'} = $dom;
  152:                                     }
  153:                                     $env{'user.home'} = &Apache::lonnet::homeserver($env{'user.name'},$env{'user.domain'});
  154: 
  155:                                     my $subject = &mt('Student enrollment changes in [_1]',$enrollvar{$crs}{coursecode});
  156:                                     my $message = &mt('The following [quant,_1,change] occurred in [_2] - [_3] as a result of the automated classlist update:',$changecount,$enrollvar{$crs}{description},$enrollvar{$crs}{coursecode})."\n\n".$response;
  157:                                     unless ($newusermsg eq '') { 
  158:                                         $message .= "\n".$newusermsg;
  159:                                     }
  160:                                     my @to_notify = ();
  161:                                     if ($enrollvar{$crs}{notifylist} =~ m/,/) {
  162:                                         @to_notify = split/,/,$enrollvar{$crs}{notifylist};
  163:                                     } else {
  164:                                         $to_notify[0] = $enrollvar{$crs}{notifylist};
  165:                                     }
  166:                                     foreach my $cc (@to_notify) {
  167:                                         my ($ccname,$ccdom);
  168:                                         if ($cc =~ /:/) {
  169:                                             ($ccname,$ccdom) = split(/:/,$cc);
  170:                                         } elsif ($cc =~ /\@/) {
  171:                                             ($ccname,$ccdom) = split(/\@/,$cc);
  172:                                         }
  173:                                         my $status =  &Apache::lonmsg::user_normal_msg($ccname,$ccdom,$subject,$message);
  174:                                     }
  175:                                     if ( ($enrollvar{$crs}{notifylist} eq '') && ($newusermsg ne '') ) {
  176:                                         my $subject = &mt('New user accounts in [_1]',$enrollvar{$crs}{'coursecode'});
  177:                                         my $status =  &Apache::lonmsg::user_normal_msg($env{'user.name'},$env{'user.domain'},$subject,$newusermsg);
  178:                                     }
  179:                                     delete($env{'user.name'});
  180:                                     delete($env{'user.home'});
  181:                                     $env{'user.domain'} = $dom;
  182:                                 }
  183:                             }
  184:                         }
  185:                     }
  186:                 }
  187:             } else {
  188:                 if ( ($enrollvar{$crs}{autoadds} == 1) || ($enrollvar{$crs}{autodrops} == 1) ) {
  189:                     if ( ($enrollvar{$crs}{autostart} < $timenow) && ( ($enrollvar{$crs}{autoend} > $timenow) || ($enrollvar{$crs}{autoend} == 0) ) ) {
  190:                         print $fh &mt('No institutional classlist data could be retrieved for [_1]',$crs)."\n";
  191:                     } else {
  192:                         print $fh ('Not within time window for auto-enrollment in [_1]',$crs)."\n";
  193:                     }
  194:                 } else {
  195:                     print $fh &mt('Auto-enrollment not currently enabled for [_1]',$crs)."\n";
  196:                 }
  197:             }
  198:         }
  199:         print $fh "-- ".localtime(time).' '.&mt('Enrollment messages end')."\n*******************\n\n";
  200:         close($fh);
  201:         delete($env{'user.domain'});
  202:     }
  203: 
  204: 1;

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