File:  [LON-CAPA] / loncom / enrollment / Autoenroll.pl
Revision 1.26: download - view: text, annotated - select for diffs
Mon May 14 17:35:01 2007 UTC (17 years, 1 month ago) by raeburn
Branches: MAIN
CVS tags: HEAD
Running of Autoenroll.pl and display of "Automated Enrollment Manager" link on ENRL menu now controlled by domain configuration setting.

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

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