File:  [LON-CAPA] / loncom / enrollment / Autoenroll.pl
Revision 1.29: download - view: text, annotated - select for diffs
Wed Jan 2 03:39:50 2008 UTC (16 years, 4 months ago) by raeburn
Branches: MAIN
CVS tags: version_2_7_X, version_2_7_99_0, version_2_7_1, version_2_7_0, version_2_6_X, version_2_6_99_1, version_2_6_99_0, version_2_6_3, version_2_6_2, version_2_6_1, version_2_6_0, HEAD
Forgot the &mt().

    1: #!/usr/bin/perl
    2: #
    3: #Automated Enrollment script
    4: # $Id: Autoenroll.pl,v 1.29 2008/01/02 03:39:50 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:     use LONCAPA qw(:match);
   41: 
   42: # Determine the library server's domain and hostID
   43:     my $perlvarref = LONCAPA::Configuration::read_conf('loncapa.conf');
   44:     my $logfile = $$perlvarref{'lonDaemons'}.'/logs/autoenroll.log';
   45:     my @domains = &Apache::lonnet::current_machine_domains();
   46:     my @hostids = &Apache::lonnet::current_machine_ids();
   47: 
   48: # Determine the present time;
   49:     my $timenow = time();
   50: 
   51: # For each domain ......
   52:     foreach my $dom (@domains) {
   53:         #only run if configured to
   54:         my $run_enroll = 0;
   55:         my $settings;
   56:         my %domconfig =
   57:             &Apache::lonnet::get_dom('configuration',['autoenroll'],$dom);
   58:         if (ref($domconfig{'autoenroll'}) eq 'HASH') {
   59:             $settings = $domconfig{'autoenroll'};
   60:             if ($settings->{'run'} eq '1') {
   61:                 $run_enroll = 1;
   62:             }
   63:         } else {
   64:             $run_enroll = &localenroll::run($dom);
   65:         }
   66:         next if ($run_enroll != 1);
   67: 
   68:         $env{'user.domain'} = $dom;
   69:         # Initialize language handler
   70:         &Apache::lonlocal::get_language_handle();
   71:         # Determine the courses
   72:         my %courses = &Apache::lonnet::courseiddump($dom,'.',1,'.','.','.',1,\@hostids,'Course'); 
   73:         my %affiliates = ();
   74:         my %enrollvar = ();
   75:         my %reply = ();
   76:         my %LC_code = ();
   77:         foreach my $key (sort keys %courses) {
   78:             my %args = (
   79:                          one_time => 1,
   80:                        );
   81:             my %coursehash = &Apache::lonnet::coursedescription($key,\%args);
   82:             my $crs = $coursehash{'num'};
   83:             next if ($coursehash{'domain'} ne $dom);
   84: # Get course settings
   85:             my %settings = &Apache::lonnet::dump('environment',$dom,$crs);
   86:             %{$enrollvar{$crs}} = ();
   87:             @{$affiliates{$crs}} = ();
   88:             %{$LC_code{$crs}} = ();
   89:             foreach my $item (keys %settings) {
   90:                 if ($item =~ m/^internal\.(.+)$/) {
   91:                     $enrollvar{$crs}{$1} = $settings{$item};
   92:                 } elsif ($item eq 'description') {
   93:                     $enrollvar{$crs}{$item} = &HTML::Entities::decode($settings{$item});  
   94:                 } elsif ($item eq 'default_enrollment_start_date') {
   95:                     $enrollvar{$crs}{startdate} = $settings{$item};
   96:                 }  elsif ($item eq 'default_enrollment_end_date') {
   97:                     $enrollvar{$crs}{enddate} = $settings{$item};
   98:                 }
   99:             }
  100:             if (($enrollvar{$crs}{autostart} <= $timenow) && ( ($enrollvar{$crs}{autoend} > $timenow) || ($enrollvar{$crs}{autoend} == 0) ) ) {
  101:                 if ( ($enrollvar{$crs}{autoadds} == 1) || ($enrollvar{$crs}{autodrops} == 1) ) {
  102: # Add to list of classes for retrieval
  103:                     $enrollvar{$crs}{sectionnums} =~ s/ //g;
  104:                     $enrollvar{$crs}{crosslistings} =~ s/ //g;
  105:                     my @sections = ();
  106:                     my @crosslistings = ();
  107:                     if ($enrollvar{$crs}{sectionnums} =~ m/,/) {
  108:                         @sections = split/,/,$enrollvar{$crs}{sectionnums};
  109:                     } else {
  110:                         $sections[0] = $enrollvar{$crs}{sectionnums};
  111:                     }
  112:                     if ($enrollvar{$crs}{crosslistings} =~ m/,/) {
  113:                         @crosslistings = split/,/,$enrollvar{$crs}{crosslistings}
  114:                     } else {
  115:                         @crosslistings = $enrollvar{$crs}{crosslistings};
  116:                     }
  117:                     foreach my $sec (@sections) {
  118:                         if ($sec =~ m/^(\w+):(\w*)$/ ) {
  119:                             my $course_id = $enrollvar{$crs}{coursecode}.$1;
  120:                             my $gp = $2;
  121:                             if (!grep/^$course_id$/,@{$affiliates{$crs}}) {
  122:                                 push @{$affiliates{$crs}}, $course_id;
  123:                                 $LC_code{$crs}{$course_id} = $gp; 
  124:                             }
  125:                         }
  126:                     }
  127:                     foreach my $xlist (@crosslistings) {
  128:                         if ($xlist =~ m/^([^:]+):(\w*)$/) {
  129:                             my $course_id = $1;
  130:                             my $gp = $2;
  131:                             if (!grep/^$course_id$/,@{$affiliates{$crs}}) {
  132:                                 push @{$affiliates{$crs}}, $course_id;
  133:                                 $LC_code{$crs}{$course_id} = $gp;
  134:                             }
  135:                         }
  136:                     }
  137:                 }
  138:             }
  139:         }
  140:         my $outcome = &Apache::lonnet::fetch_enrollment_query('automated',\%affiliates,\%reply,$dom);
  141: 
  142: # Now go through classes and perform required enrollment changes.
  143:         open (my $fh,">>$logfile");
  144:         print $fh "********************\n".localtime(time).' '.&mt('Enrollment messages start').' --'."\n";
  145:         print $fh &mt("Response from [_1] was [_2]",'fetch_enrollment_query',$outcome)."\n";
  146:         foreach my $crs (sort keys %enrollvar) {
  147:             my $logmsg = '';
  148:             my $newusermsg = '';
  149:             if ($reply{$crs} > 0) {
  150:                 if ( ($enrollvar{$crs}{autostart} < $timenow) && ( ($enrollvar{$crs}{autoend} > $timenow) || ($enrollvar{$crs}{autoend} == 0) ) ) {
  151:                     if (($enrollvar{$crs}{autoadds} == 1) || ($enrollvar{$crs}{autodrops} == 1)) {
  152:                         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');
  153:                         print $fh &mt('Messages start for [_1]',$crs)."\n";
  154:                         print $fh "$logmsg\n";
  155:                         print $fh &mt('Messages end for [_1]',$crs)."\n";
  156:                         if ($changecount > 0) {
  157: # Set $env{'user.name'}, $env{'user.domain'}, $env{'user.home'}
  158: # and $env{'request.course.id'} for use by logging in lonmsg
  159:                             $env{'request.course.id'} = $dom.'_'.$crs;
  160:                             my ($ownername,$ownerdom);
  161:                             if ($enrollvar{$crs}{'courseowner'} ne '') {
  162:                                 if ($enrollvar{$crs}{'courseowner'} =~ /:/) {
  163:                                     ($ownername,$ownerdom) =
  164:                                         split(/:/,$enrollvar{$crs}{'courseowner'});
  165:                                 } else {
  166:                                     $ownername = $enrollvar{$crs}{'courseowner'}; 
  167:                                     $ownerdom = $dom;
  168:                                 }
  169:                             }
  170:                             if (($settings->{'sender_uname'} ne '') && 
  171:                                 ($settings->{'sender_domain'} ne '')) {
  172:                                 $env{'user.name'} = $settings->{'sender_uname'};
  173:                                 $env{'user.domain'} = $settings->{'sender_domain'};
  174:                             } else {
  175:                                 $env{'user.name'} = $ownername;
  176:                                 $env{'user.domain'} = $ownerdom;
  177:                             }
  178:                             if ($enrollvar{$crs}{notifylist}  eq '') {
  179:                                 if ($newusermsg ne '') {
  180:                                     if (($ownername =~ /^$match_username$/) && 
  181:                                         ($ownerdom =~ /^$match_domain$/)) {
  182:                                         my $subject = &mt('New user accounts in [_1]',$enrollvar{$crs}{'coursecode'});
  183:                                         my $status =  &Apache::lonmsg::user_normal_msg($ownername,$ownerdom,$subject,$newusermsg);
  184:                                     }
  185:                                 }
  186:                             } else {
  187:                                 my $msgcc;
  188: # Send message about enrollment changes to notifylist.
  189:                                 $env{'user.home'} = &Apache::lonnet::homeserver($env{'user.name'},$env{'user.domain'});
  190:                                 my $subject = &mt('Student enrollment changes in [_1]',$enrollvar{$crs}{coursecode});
  191:                                 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;
  192:                                 unless ($newusermsg eq '') { 
  193:                                     $message .= "\n".$newusermsg;
  194:                                 }
  195:                                 my @to_notify = split(/,/,$enrollvar{$crs}{notifylist});
  196:                                 my $numsent = 0;
  197:                                 my @recusers;
  198:                                 my @recudoms;
  199:                                 foreach my $cc (@to_notify) {
  200:                                     my ($ccname,$ccdom);
  201:                                     if ($cc =~ /:/) {
  202:                                         ($ccname,$ccdom) = split(/:/,$cc);
  203:                                     } elsif ($cc =~ /\@/) {
  204:                                         ($ccname,$ccdom) = split(/\@/,$cc);
  205:                                     }
  206:                                     push(@recusers,$ccname);
  207:                                     push(@recudoms,$ccdom);
  208:                                     $msgcc->{$ccname.':'.$ccdom}='';
  209:                                     $numsent ++;
  210:                                 }
  211:                                 my %reciphash = (
  212:                                     cc => $msgcc,
  213:                                 );
  214:                                 my %sentmessage;
  215:                                 my $stamp = time;
  216:                                 my $msgcount = &Apache::lonmsg::get_uniq();
  217:                                 &Apache::lonmsg::process_sent_mail($subject,'',$numsent,$stamp,$env{'user.name'},$env{'user.domain'},$msgcount,$crs,$$,$message,\@recusers,\@recudoms);
  218:                                 my ($recipid,$recipstatus) =
  219:                                     &Apache::lonmsg::store_recipients($subject,
  220:                                     $env{'user.name'},$env{'user.domain'},\%reciphash);
  221:                                 foreach my $recip (sort(keys(%{$msgcc}))) {
  222:                                     my ($ccname,$ccdom) = split(/:/,$recip);
  223:                                     my $status =  
  224:                                         &Apache::lonmsg::user_normal_msg($ccname,$ccdom,$subject,$message,undef,undef,undef,undef,\%sentmessage,undef,undef,undef,1,$recipid);
  225:                                 }
  226:                             }
  227:                             delete($env{'user.name'});
  228:                             delete($env{'user.home'});
  229:                             delete($env{'request.course.id'});
  230:                             $env{'user.domain'} = $dom;
  231:                         }
  232:                     }
  233:                 }
  234:             } else {
  235:                 if ( ($enrollvar{$crs}{autoadds} == 1) || ($enrollvar{$crs}{autodrops} == 1) ) {
  236:                     if ( ($enrollvar{$crs}{autostart} < $timenow) && ( ($enrollvar{$crs}{autoend} > $timenow) || ($enrollvar{$crs}{autoend} == 0) ) ) {
  237:                         print $fh &mt('No institutional classlist data could be retrieved for [_1]',$crs)."\n";
  238:                     } else {
  239:                         print $fh &mt('Not within time window for auto-enrollment in [_1]',$crs)."\n";
  240:                     }
  241:                 } else {
  242:                     print $fh &mt('Auto-enrollment not currently enabled for [_1]',$crs)."\n";
  243:                 }
  244:             }
  245:         }
  246:         print $fh "-- ".localtime(time).' '.&mt('Enrollment messages end')."\n*******************\n\n";
  247:         close($fh);
  248:         delete($env{'user.domain'});
  249:     }
  250: 
  251: 1;

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