File:  [LON-CAPA] / loncom / enrollment / Autoenroll.pl
Revision 1.35: download - view: text, annotated - select for diffs
Tue Jun 15 20:52:27 2021 UTC (3 years, 1 month ago) by raeburn
Branches: MAIN
CVS tags: HEAD
- Support customization in localenroll.pm to eliminate ambiguity in extraction
  of institutional section from institutional course section for automated
  enrollment.

    1: #!/usr/bin/perl
    2: #
    3: #Automated Enrollment script
    4: # $Id: Autoenroll.pl,v 1.35 2021/06/15 20:52:27 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 ($domsettings,$autofailsafe);
   56:         my %domconfig =
   57:             &Apache::lonnet::get_dom('configuration',['autoenroll','coursedefaults'],$dom);
   58:         if (ref($domconfig{'autoenroll'}) eq 'HASH') {
   59:             $domsettings = $domconfig{'autoenroll'};
   60:             if ($domsettings->{'run'} eq '1') {
   61:                 $run_enroll = 1;
   62:             }
   63:             if ($domsettings->{'autofailsafe'} ne '') {
   64:                 $autofailsafe = $domsettings->{'autofailsafe'};
   65:             }
   66:         } else {
   67:             $run_enroll = &localenroll::run($dom);
   68:         }
   69:         next if ($run_enroll != 1);
   70: 
   71:         $env{'user.domain'} = $dom;
   72:         # Initialize language handler
   73:         &Apache::lonlocal::get_language_handle();
   74:         # Determine the courses
   75:         my %courses = &Apache::lonnet::courseiddump($dom,'.',1,'.','.','.',1,\@hostids,'Course'); 
   76:         my %affiliates = ();
   77:         my %unclutteredsec = ();
   78:         my %unclutteredlcsec = ();
   79:         my %enrollvar = ();
   80:         my %reply = ();
   81:         my %LC_code = ();
   82:         my ($showcredits,$domdefcredits);
   83:         if ((ref($domconfig{'coursedefaults'}) eq 'HASH') &&
   84:             (ref($domconfig{'coursedefaults'}{'coursecredits'}) eq 'HASH')) {
   85:             if ($domconfig{'coursedefaults'}{'coursecredits'}{'official'}) {
   86:                 $showcredits = 1;
   87:                 $domdefcredits = $domconfig{'coursedefaults'}{'coursecredits'}{'official'};
   88:             }
   89:         }
   90:         foreach my $key (sort keys %courses) {
   91:             my %args = (
   92:                          one_time => 1,
   93:                        );
   94: # Get course settings
   95:             my %settings = &Apache::lonnet::coursedescription($key,\%args);
   96:             my $crs = $settings{'num'};
   97:             next if ($settings{'domain'} ne $dom);
   98:             %{$enrollvar{$crs}} = ();
   99:             @{$affiliates{$crs}} = ();
  100:             %{$LC_code{$crs}} = ();
  101:             foreach my $item (keys %settings) {
  102:                 if ($item =~ m/^internal\.(.+)$/) {
  103:                     $enrollvar{$crs}{$1} = $settings{$item};
  104:                 } elsif ($item eq 'description') {
  105:                     $enrollvar{$crs}{$item} = &HTML::Entities::decode($settings{$item});  
  106:                 } elsif ($item eq 'default_enrollment_start_date') {
  107:                     $enrollvar{$crs}{startdate} = $settings{$item};
  108:                 } elsif ($item eq 'default_enrollment_end_date') {
  109:                     $enrollvar{$crs}{enddate} = $settings{$item};
  110:                 }
  111:             }
  112:             if (($enrollvar{$crs}{autostart} <= $timenow) && ( ($enrollvar{$crs}{autoend} > $timenow) || ($enrollvar{$crs}{autoend} == 0) ) ) {
  113:                 if ( ($enrollvar{$crs}{autoadds} == 1) || ($enrollvar{$crs}{autodrops} == 1) ) {
  114: # Add to list of classes for retrieval
  115:                     $enrollvar{$crs}{sectionnums} =~ s/ //g;
  116:                     $enrollvar{$crs}{crosslistings} =~ s/ //g;
  117:                     my @sections = ();
  118:                     my @crosslistings = ();
  119:                     if ($enrollvar{$crs}{sectionnums} =~ m/,/) {
  120:                         @sections = split/,/,$enrollvar{$crs}{sectionnums};
  121:                     } else {
  122:                         $sections[0] = $enrollvar{$crs}{sectionnums};
  123:                     }
  124:                     if ($enrollvar{$crs}{crosslistings} =~ m/,/) {
  125:                         @crosslistings = split/,/,$enrollvar{$crs}{crosslistings};
  126:                     } else {
  127:                         @crosslistings = $enrollvar{$crs}{crosslistings};
  128:                     }
  129:                     my $crscode = $enrollvar{$crs}{coursecode}; 
  130:                     my $crskey = $crs.':'.$crscode;
  131:                     @{$unclutteredsec{$crskey}} = ();
  132:                     @{$unclutteredlcsec{$crskey}} = (); 
  133:                     foreach my $sec (@sections) {
  134:                         if ($sec =~ m/^(\w+):(\w*)$/ ) {
  135:                             my $instsec = $1; 
  136:                             my $gp = $2;
  137:                             unless (grep/^\Q$instsec\E$/,@{$unclutteredsec{$crskey}}) {
  138:                                 push(@{$unclutteredsec{$crskey}},$instsec);
  139:                                 push(@{$unclutteredlcsec{$crskey}},$gp);
  140:                             }
  141:                         }
  142:                     }
  143:                     foreach my $xlist (@crosslistings) {
  144:                         if ($xlist =~ m/^([^:]+):(\w*)$/) {
  145:                             my $course_id = $1;
  146:                             my $gp = $2;
  147:                             if (!grep/^\Q$course_id\E$/,@{$affiliates{$crs}}) {
  148:                                 push @{$affiliates{$crs}}, $course_id;
  149:                                 $LC_code{$crs}{$course_id} = $gp;
  150:                             }
  151:                         }
  152:                     }
  153:                 }
  154:             }
  155:         }
  156:         my %formattedsec = &Apache::lonnet::auto_instsec_reformat($dom,'clutter',\%unclutteredsec);
  157:         foreach my $crskey (keys(%formattedsec)) {
  158:             my ($crs,$instcode) = split(/:/,$crskey);
  159:             if ((ref($formattedsec{$crskey}) eq 'ARRAY') && (ref($unclutteredlcsec{$crskey}) eq 'ARRAY')) {
  160:                 for (my $i=0; $i<@{$formattedsec{$crskey}}; $i++) {
  161:                     my $course_id = $instcode.$formattedsec{$crskey}[$i]; 
  162:                     unless (grep/^\Q$course_id\E$/,@{$affiliates{$crs}}) {
  163:                         push(@{$affiliates{$crs}},$course_id);
  164:                         $LC_code{$crs}{$course_id} = $unclutteredlcsec{$crskey}[$i];
  165:                     }
  166:                 }
  167:             }
  168:         }
  169:         my $outcome = &Apache::lonnet::fetch_enrollment_query('automated',\%affiliates,\%reply,$dom);
  170: 
  171: # Now go through classes and perform required enrollment changes.
  172:         open (my $fh,">>$logfile");
  173:         print $fh "********************\n".localtime(time).' '.&mt('Enrollment messages start').' --'."\n";
  174:         print $fh &mt("Response from [_1] was [_2]",'fetch_enrollment_query',$outcome)."\n";
  175:         foreach my $crs (sort keys %enrollvar) {
  176:             my $logmsg = '';
  177:             my $newusermsg = '';
  178:             if ($reply{$crs} > 0) {
  179:                 if ( ($enrollvar{$crs}{autostart} < $timenow) && ( ($enrollvar{$crs}{autoend} > $timenow) || ($enrollvar{$crs}{autoend} == 0) ) ) {
  180:                     if (($enrollvar{$crs}{autoadds} == 1) || ($enrollvar{$crs}{autodrops} == 1)) {
  181:                         my $defaultcredits = $domdefcredits;
  182:                         if ($enrollvar{$crs}{defaultcredits}) {
  183:                             $defaultcredits = $enrollvar{$crs}{defaultcredits};
  184:                         }
  185:                         my $failsafe = $autofailsafe;
  186:                         if ($enrollvar{$crs}{'autodropfailsafe'}) {
  187:                             $failsafe = $enrollvar{$crs}{'autodropfailsafe'};
  188:                         }
  189:                         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},$showcredits,$defaultcredits,$failsafe,$affiliates{$crs},$LC_code{$crs},\$logmsg,\$newusermsg,'automated');
  190:                         print $fh &mt('Messages start for [_1]',$crs)."\n";
  191:                         print $fh "$logmsg\n";
  192:                         print $fh &mt('Messages end for [_1]',$crs)."\n";
  193:                         if ($changecount > 0) {
  194: # Set $env{'user.name'}, $env{'user.domain'}, $env{'user.home'}
  195: # and $env{'request.course.id'} for use by logging in lonmsg
  196:                             $env{'request.course.id'} = $dom.'_'.$crs;
  197:                             my ($ownername,$ownerdom);
  198:                             if ($enrollvar{$crs}{'courseowner'} ne '') {
  199:                                 if ($enrollvar{$crs}{'courseowner'} =~ /:/) {
  200:                                     ($ownername,$ownerdom) =
  201:                                         split(/:/,$enrollvar{$crs}{'courseowner'});
  202:                                 } else {
  203:                                     $ownername = $enrollvar{$crs}{'courseowner'}; 
  204:                                     $ownerdom = $dom;
  205:                                 }
  206:                             }
  207:                             $env{'user.name'} = $ownername;
  208:                             $env{'user.domain'} = $ownerdom;
  209:                             if (ref($domsettings) eq 'HASH') {
  210:                                 if (($domsettings->{'sender_uname'} ne '') && 
  211:                                     ($domsettings->{'sender_domain'} ne '')) {
  212:                                     $env{'user.name'} = $domsettings->{'sender_uname'};
  213:                                     $env{'user.domain'} = $domsettings->{'sender_domain'};
  214:                                 }
  215:                             }
  216:                             if ($enrollvar{$crs}{notifylist}  eq '') {
  217:                                 if ($newusermsg ne '') {
  218:                                     if (($ownername =~ /^$match_username$/) && 
  219:                                         ($ownerdom =~ /^$match_domain$/)) {
  220:                                         my $subject = &mt('New user accounts in [_1]',$enrollvar{$crs}{'coursecode'});
  221:                                         my $status =  &Apache::lonmsg::user_normal_msg($ownername,$ownerdom,$subject,$newusermsg);
  222:                                     }
  223:                                 }
  224:                             } else {
  225:                                 $env{'form.can_reply'} = 'N';
  226:                                 my $msgcc;
  227: # Send message about enrollment changes to notifylist.
  228:                                 $env{'user.home'} = &Apache::lonnet::homeserver($env{'user.name'},$env{'user.domain'});
  229:                                 my $subject = &mt('Student enrollment changes in [_1]',$enrollvar{$crs}{coursecode});
  230:                                 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;
  231:                                 unless ($newusermsg eq '') { 
  232:                                     $message .= "\n".$newusermsg;
  233:                                 }
  234:                                 my @to_notify = split(/,/,$enrollvar{$crs}{notifylist});
  235:                                 my $numsent = 0;
  236:                                 my @recusers;
  237:                                 my @recudoms;
  238:                                 foreach my $cc (@to_notify) {
  239:                                     my ($ccname,$ccdom);
  240:                                     if ($cc =~ /:/) {
  241:                                         ($ccname,$ccdom) = split(/:/,$cc);
  242:                                     } elsif ($cc =~ /\@/) {
  243:                                         ($ccname,$ccdom) = split(/\@/,$cc);
  244:                                     }
  245:                                     push(@recusers,$ccname);
  246:                                     push(@recudoms,$ccdom);
  247:                                     $msgcc->{$ccname.':'.$ccdom}='';
  248:                                     $numsent ++;
  249:                                 }
  250:                                 my %reciphash = (
  251:                                     cc => $msgcc,
  252:                                 );
  253:                                 my %sentmessage;
  254:                                 my $stamp = time;
  255:                                 my $msgcount = &Apache::lonmsg::get_uniq();
  256:                                 &Apache::lonmsg::process_sent_mail($subject,'',$numsent,$stamp,$env{'user.name'},$env{'user.domain'},$msgcount,$crs,$$,$message,\@recusers,\@recudoms);
  257:                                 my ($recipid,$recipstatus) =
  258:                                     &Apache::lonmsg::store_recipients($subject,
  259:                                     $env{'user.name'},$env{'user.domain'},\%reciphash);
  260:                                 foreach my $recip (sort(keys(%{$msgcc}))) {
  261:                                     my ($ccname,$ccdom) = split(/:/,$recip);
  262:                                     my $status =  
  263:                                         &Apache::lonmsg::user_normal_msg($ccname,$ccdom,$subject,$message,undef,undef,undef,undef,\%sentmessage,undef,undef,undef,1,$recipid);
  264:                                 }
  265:                                 delete($env{'form.can_reply'});
  266:                             }
  267:                             delete($env{'user.name'});
  268:                             delete($env{'user.home'});
  269:                             delete($env{'request.course.id'});
  270:                             $env{'user.domain'} = $dom;
  271:                         }
  272:                     }
  273:                 }
  274:             } else {
  275:                 if ( ($enrollvar{$crs}{autoadds} == 1) || ($enrollvar{$crs}{autodrops} == 1) ) {
  276:                     if ( ($enrollvar{$crs}{autostart} < $timenow) && ( ($enrollvar{$crs}{autoend} > $timenow) || ($enrollvar{$crs}{autoend} == 0) ) ) {
  277:                         print $fh &mt('No institutional classlist data could be retrieved for [_1]',$crs)."\n";
  278:                     } else {
  279:                         print $fh &mt('Not within time window for auto-enrollment in [_1]',$crs)."\n";
  280:                     }
  281:                 } else {
  282:                     print $fh &mt('Auto-enrollment not currently enabled for [_1]',$crs)."\n";
  283:                 }
  284:             }
  285:         }
  286:         print $fh "-- ".localtime(time).' '.&mt('Enrollment messages end')."\n*******************\n\n";
  287:         close($fh);
  288:         delete($env{'user.domain'});
  289:     }
  290: 
  291: 1;

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