File:  [LON-CAPA] / loncom / enrollment / Autoenroll.pl
Revision 1.33: download - view: text, annotated - select for diffs
Fri Mar 1 05:20:01 2013 UTC (11 years, 2 months ago) by raeburn
Branches: MAIN
CVS tags: version_2_11_1, version_2_11_0_RC3, version_2_11_0_RC2, version_2_11_0_RC1, version_2_11_0, HEAD
- Set institutional status when auto-enrollment adds a new LON-CAPA user.
- Store credits earned for a course in classlist.db for students for whom
  credits are different from course default.
- Credits can be part of XML record for each student in course roster
  retrieved from institutional data course via customized localenroll.pm

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

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