File:  [LON-CAPA] / loncom / enrollment / Autoenroll.pl
Revision 1.38: download - view: text, annotated - select for diffs
Mon Oct 2 21:01:22 2023 UTC (7 months ago) by raeburn
Branches: MAIN
CVS tags: version_2_12_X, version_2_11_4_msu, HEAD
- Bug 6979.
  "View As" form in Functions menu includes domain dropdown list if users in
  course are from more than one domain. Use course's environment.db to store
  comma separated list of unique domains to which course users belong.

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

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