Diff for /loncom/enrollment/Autoenroll.pl between versions 1.1 and 1.36

version 1.1, 2003/12/05 17:08:09 version 1.36, 2021/12/28 02:04:35
Line 1 Line 1
 #!/usr/bin/perl -w  #!/usr/bin/perl
   #
   #Automated Enrollment script
   # $Id$
   #
   # Copyright Michigan State University Board of Trustees
   #
   # This file is part of the LearningOnline Network with CAPA (LON-CAPA).
   #
   # LON-CAPA is free software; you can redistribute it and/or modify
   # it under the terms of the GNU General Public License as published by
   # the Free Software Foundation; either version 2 of the License, or
   # (at your option) any later version.
   #
   # LON-CAPA is distributed in the hope that it will be useful,
   # but WITHOUT ANY WARRANTY; without even the implied warranty of
   # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   # GNU General Public License for more details.
   #
   # You should have received a copy of the GNU General Public License
   # along with LON-CAPA; if not, write to the Free Software
   # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
   #
   # /home/httpd/html/adm/gpl.txt
   #
   # http://www.lon-capa.org/
   #
       use strict;
       use lib '/home/httpd/lib/perl';
       use localenroll;
       use LONCAPA::Configuration;
       use LONCAPA::Enrollment;
       use Apache::lonnet;
       use Apache::loncoursedata;
       use Apache::lonmsg;
       use Apache::longroup;
       use Apache::loncommon;
       use Apache::lonlocal;
       use HTML::Entities;
       use LONCAPA qw(:match);
   
   use strict;  # Determine the library server's domain and hostID
   use lib '/home/httpd/lib/perl';      my $perlvarref = LONCAPA::Configuration::read_conf('loncapa.conf');
   use localenroll;      my $logfile = $$perlvarref{'lonDaemons'}.'/logs/autoenroll.log';
   use LONCAPA::Configuration;      my @domains = &Apache::lonnet::current_machine_domains();
   use LONCAPA::Enrollment;      my @hostids = &Apache::lonnet::current_machine_ids();
   use Apache::lonnet;  
   use Apache::loncoursedata;  
   
 # Determine the library server's domain  
   my $perlvarref = LONCAPA::Configuration::read_conf('loncapa.conf');  
   my $dom = $$perlvarref{'lonDefDomain'};  
   print "Domain is $dom\n";  
   
 # Determine the present time;  # Determine the present time;
   my $timenow = time();      my $timenow = time();
   
 # Determine the courses  # For each domain ......
   my %courses = &Apache::lonnet::courseiddump($dom,'.',1);       foreach my $dom (@domains) {
   my %affiliates = ();          #only run if configured to
   my %enrollvar = ();          my $run_enroll = 0;
   my %LC_code = ();          my ($domsettings,$autofailsafe,$autofailsafetype);
   foreach my $key (sort keys %courses) {          my %domconfig =
       my $crs;              &Apache::lonnet::get_dom('configuration',['autoenroll','coursedefaults'],$dom);
       if ($key =~ m/^($dom)_(\w+)$/) {          if (ref($domconfig{'autoenroll'}) eq 'HASH') {
           $crs = $2;              $domsettings = $domconfig{'autoenroll'};
       }              if ($domsettings->{'run'} eq '1') {
                   $run_enroll = 1;
               }
               if ($domsettings->{'autofailsafe'} ne '') {
                   $autofailsafe = $domsettings->{'autofailsafe'};
               }
               if ($domsettings->{'failsafe'} =~ /^(zero|any|off)$/) {
                    $autofailsafetype = $domsettings->{'failsafe'};
               }
           } else {
               $run_enroll = &localenroll::run($dom);
           }
           next if ($run_enroll != 1);
   
           $env{'user.domain'} = $dom;
           # Initialize language handler
           &Apache::lonlocal::get_language_handle();
           # Determine the courses
           my %courses = &Apache::lonnet::courseiddump($dom,'.',1,'.','.','.',1,\@hostids,'Course'); 
           my %affiliates = ();
           my %unclutteredsec = ();
           my %unclutteredlcsec = ();
           my %enrollvar = ();
           my %reply = ();
           my %LC_code = ();
           my ($showcredits,$domdefcredits);
           if ((ref($domconfig{'coursedefaults'}) eq 'HASH') &&
               (ref($domconfig{'coursedefaults'}{'coursecredits'}) eq 'HASH')) {
               if ($domconfig{'coursedefaults'}{'coursecredits'}{'official'}) {
                   $showcredits = 1;
                   $domdefcredits = $domconfig{'coursedefaults'}{'coursecredits'}{'official'};
               }
           }
           foreach my $key (sort keys %courses) {
               my %args = (
                            one_time => 1,
                          );
 # Get course settings  # Get course settings
       my %settings = &Apache::lonnet::dump('environment',$dom,$crs);              my %settings = &Apache::lonnet::coursedescription($key,\%args);
       %{$enrollvar{$crs}} = ();              my $crs = $settings{'num'};
       @{$affiliates{$crs}} = ();              next if ($settings{'domain'} ne $dom);
       %{$LC_code{$crs}} = ();              %{$enrollvar{$crs}} = ();
       foreach my $item (keys %settings) {              @{$affiliates{$crs}} = ();
           if ($item =~ m/^internal\.(.+)$/) {              %{$LC_code{$crs}} = ();
               $enrollvar{$crs}{$1} = $settings{$item};              foreach my $item (keys %settings) {
           }                  if ($item =~ m/^internal\.(.+)$/) {
       }                      $enrollvar{$crs}{$1} = $settings{$item};
       if (($enrollvar{$crs}{autostart} >= $timenow) && ($enrollvar{$crs}{autoend} < $timenow)) {                  } elsif ($item eq 'description') {
           if (($enrollvar{$crs}{autoadds} == 1) || ($enrollvar{$crs}{autodrops} == 1) {                      $enrollvar{$crs}{$item} = &HTML::Entities::decode($settings{$item});  
                   } elsif ($item eq 'default_enrollment_start_date') {
                       $enrollvar{$crs}{startdate} = $settings{$item};
                   } elsif ($item eq 'default_enrollment_end_date') {
                       $enrollvar{$crs}{enddate} = $settings{$item};
                   }
               }
               if (($enrollvar{$crs}{autostart} <= $timenow) && ( ($enrollvar{$crs}{autoend} > $timenow) || ($enrollvar{$crs}{autoend} == 0) ) ) {
                   if ( ($enrollvar{$crs}{autoadds} == 1) || ($enrollvar{$crs}{autodrops} == 1) ) {
 # Add to list of classes for retrieval  # Add to list of classes for retrieval
               $enrollvar{$crs}{sectionnums} =~ s/ //g;                      $enrollvar{$crs}{sectionnums} =~ s/ //g;
               $enrollvar{$crs}{crosslistings} =~ s/ //g;                      $enrollvar{$crs}{crosslistings} =~ s/ //g;
               my @sections = ();                      my @sections = ();
               my @crosslistings = ();                      my @crosslistings = ();
               if ($enrollvar{$crs}{sectionnums} =~ m/,/) {                      if ($enrollvar{$crs}{sectionnums} =~ m/,/) {
                   @sections = split/,/,$enrollvar{$crs}{sectionnums};                          @sections = split/,/,$enrollvar{$crs}{sectionnums};
               } else {                      } else {
                   @$sections = $enrollvar{$crs}{sectionnums};                          $sections[0] = $enrollvar{$crs}{sectionnums};
               }                      }
               if ($enrollvar{$crs}{crosslistings} =~ m/,/) {                      if ($enrollvar{$crs}{crosslistings} =~ m/,/) {
                   @crosslistings = split/,/,$enrollvar{$crs}{crosslistings}                          @crosslistings = split/,/,$enrollvar{$crs}{crosslistings};
               } else {                      } else {
                   @crosslistings = $enrollvar{$crs}{crosslistings};                          @crosslistings = $enrollvar{$crs}{crosslistings};
               }                      }
               foreach my $sec (@sections) {                        my $crscode = $enrollvar{$crs}{coursecode}; 
                   if ($sec =~ m/^(\w+):/(\w*)$) {                      my $crskey = $crs.':'.$crscode;
                       my $course_id = $enrollvar{$crs}{coursecode}.$1;                      @{$unclutteredsec{$crskey}} = ();
                       my $gp = $2;                      @{$unclutteredlcsec{$crskey}} = (); 
                       if (!grep/^$course_id$/,@{$affiliates{$crs}}) {                      foreach my $sec (@sections) {
                           push @{$affiliates{$crs}}, $course_id;                          if ($sec =~ m/^(\w+):(\w*)$/ ) {
                           $LC_code{$crs}{$course_id} = $gp;                               my $instsec = $1; 
                       }                              my $gp = $2;
                   }                              unless (grep/^\Q$instsec\E$/,@{$unclutteredsec{$crskey}}) {
               }                                  push(@{$unclutteredsec{$crskey}},$instsec);
               foreach my $xlist (@crosslistings) {                                  push(@{$unclutteredlcsec{$crskey}},$gp);
                   if ($xlist =~ m/^(\w+):(\w*)$/) {                              }
                       my $course_id = $1;                          }
                       my $gp = $2;                      }
                       if (!grep/^$course_id$/,@{$affiliates{$crs}}) {                      foreach my $xlist (@crosslistings) {
                           push @{$affiliates{$crs}}, $course_id;                          if ($xlist =~ m/^([^:]+):(\w*)$/) {
                           $LC_code{$crs}{$course_id} = $gp;                              my $course_id = $1;
                       }                              my $gp = $2;
                   }                              if (!grep/^\Q$course_id\E$/,@{$affiliates{$crs}}) {
               }                                  push @{$affiliates{$crs}}, $course_id;
           }                                  $LC_code{$crs}{$course_id} = $gp;
       }                              }
       print "Key is $key, Value is $courses{$key}\n";                          }
   }                      }
   &localenroll::fetch_enrollment($dom,\%affiliates,\%reply);                  }
               }
           }
           my %formattedsec = &Apache::lonnet::auto_instsec_reformat($dom,'clutter',\%unclutteredsec);
           foreach my $crskey (keys(%formattedsec)) {
               my ($crs,$instcode) = split(/:/,$crskey);
               if ((ref($formattedsec{$crskey}) eq 'ARRAY') && (ref($unclutteredlcsec{$crskey}) eq 'ARRAY')) {
                   for (my $i=0; $i<@{$formattedsec{$crskey}}; $i++) {
                       my $course_id = $instcode.$formattedsec{$crskey}[$i]; 
                       unless (grep/^\Q$course_id\E$/,@{$affiliates{$crs}}) {
                           push(@{$affiliates{$crs}},$course_id);
                           $LC_code{$crs}{$course_id} = $unclutteredlcsec{$crskey}[$i];
                       }
                   }
               }
           }
           my $outcome = &Apache::lonnet::fetch_enrollment_query('automated',\%affiliates,\%reply,$dom);
   
 # Now go through classes and perform required enrollment changes.  # Now go through classes and perform required enrollment changes.
   foreach my $crs (sort keys %enrollvar) {          open (my $fh,">>$logfile");
       if (($enrollvar{$crs}{autostart} >= $timenow) && ($enrollvar{$crs}{autoend} < $timenow)) {          print $fh "********************\n".localtime(time).' '.&mt('Enrollment messages start').' --'."\n";
           if (($enrollvar{$crs}{autoadds} == 1) || ($enrollvar{$crs}{autodrops} == 1)) {          print $fh &mt("Response from [_1] was [_2]",'fetch_enrollment_query',$outcome)."\n";
               my $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}},"automated");          foreach my $crs (sort keys %enrollvar) {
               unless ($enrollvar{$crs}{notifylist}  eq '') {              my $logmsg = '';
 # Send message about enrollment changes to notifylist.                            my $newusermsg = '';
               }              if ($reply{$crs} > 0) {
           }                  if ( ($enrollvar{$crs}{autostart} < $timenow) && ( ($enrollvar{$crs}{autoend} > $timenow) || ($enrollvar{$crs}{autoend} == 0) ) ) {
       }                      if (($enrollvar{$crs}{autoadds} == 1) || ($enrollvar{$crs}{autodrops} == 1)) {
   }                          my $defaultcredits = $domdefcredits;
                           if ($enrollvar{$crs}{defaultcredits}) {
 # Check for photos                              $defaultcredits = $enrollvar{$crs}{defaultcredits};
                           }
                           my $failsafe = $autofailsafe;
                           if ($enrollvar{$crs}{'autodropfailsafe'}) {
                               $failsafe = $enrollvar{$crs}{'autodropfailsafe'};
                           }
                           my $failsafetype = $autofailsafetype;
                           if ($enrollvar{$crs}{'autodropfailsafetype'}) {
                               $failsafetype = $enrollvar{$crs}{'autodropfailsafetype'};  
                           }
                           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');
                           print $fh &mt('Messages start for [_1]',$crs)."\n";
                           print $fh "$logmsg\n";
                           print $fh &mt('Messages end for [_1]',$crs)."\n";
                           if ($changecount > 0) {
   # Set $env{'user.name'}, $env{'user.domain'}, $env{'user.home'}
   # and $env{'request.course.id'} for use by logging in lonmsg
                               $env{'request.course.id'} = $dom.'_'.$crs;
                               my ($ownername,$ownerdom);
                               if ($enrollvar{$crs}{'courseowner'} ne '') {
                                   if ($enrollvar{$crs}{'courseowner'} =~ /:/) {
                                       ($ownername,$ownerdom) =
                                           split(/:/,$enrollvar{$crs}{'courseowner'});
                                   } else {
                                       $ownername = $enrollvar{$crs}{'courseowner'}; 
                                       $ownerdom = $dom;
                                   }
                               }
                               $env{'user.name'} = $ownername;
                               $env{'user.domain'} = $ownerdom;
                               if (ref($domsettings) eq 'HASH') {
                                   if (($domsettings->{'sender_uname'} ne '') && 
                                       ($domsettings->{'sender_domain'} ne '')) {
                                       $env{'user.name'} = $domsettings->{'sender_uname'};
                                       $env{'user.domain'} = $domsettings->{'sender_domain'};
                                   }
                               }
                               if ($enrollvar{$crs}{notifylist}  eq '') {
                                   if ($newusermsg ne '') {
                                       if (($ownername =~ /^$match_username$/) && 
                                           ($ownerdom =~ /^$match_domain$/)) {
                                           my $subject = &mt('New user accounts in [_1]',$enrollvar{$crs}{'coursecode'});
                                           my $status =  &Apache::lonmsg::user_normal_msg($ownername,$ownerdom,$subject,$newusermsg);
                                       }
                                   }
                               } else {
                                   $env{'form.can_reply'} = 'N';
                                   my $msgcc;
   # Send message about enrollment changes to notifylist.
                                   $env{'user.home'} = &Apache::lonnet::homeserver($env{'user.name'},$env{'user.domain'});
                                   my $subject = &mt('Student enrollment changes in [_1]',$enrollvar{$crs}{coursecode});
                                   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;
                                   unless ($newusermsg eq '') { 
                                       $message .= "\n".$newusermsg;
                                   }
                                   my @to_notify = split(/,/,$enrollvar{$crs}{notifylist});
                                   my $numsent = 0;
                                   my @recusers;
                                   my @recudoms;
                                   foreach my $cc (@to_notify) {
                                       my ($ccname,$ccdom);
                                       if ($cc =~ /:/) {
                                           ($ccname,$ccdom) = split(/:/,$cc);
                                       } elsif ($cc =~ /\@/) {
                                           ($ccname,$ccdom) = split(/\@/,$cc);
                                       }
                                       push(@recusers,$ccname);
                                       push(@recudoms,$ccdom);
                                       $msgcc->{$ccname.':'.$ccdom}='';
                                       $numsent ++;
                                   }
                                   my %reciphash = (
                                       cc => $msgcc,
                                   );
                                   my %sentmessage;
                                   my $stamp = time;
                                   my $msgcount = &Apache::lonmsg::get_uniq();
                                   &Apache::lonmsg::process_sent_mail($subject,'',$numsent,$stamp,$env{'user.name'},$env{'user.domain'},$msgcount,$crs,$$,$message,\@recusers,\@recudoms);
                                   my ($recipid,$recipstatus) =
                                       &Apache::lonmsg::store_recipients($subject,
                                       $env{'user.name'},$env{'user.domain'},\%reciphash);
                                   foreach my $recip (sort(keys(%{$msgcc}))) {
                                       my ($ccname,$ccdom) = split(/:/,$recip);
                                       my $status =  
                                           &Apache::lonmsg::user_normal_msg($ccname,$ccdom,$subject,$message,undef,undef,undef,undef,\%sentmessage,undef,undef,undef,1,$recipid);
                                   }
                                   delete($env{'form.can_reply'});
                               }
                               delete($env{'user.name'});
                               delete($env{'user.home'});
                               delete($env{'request.course.id'});
                               $env{'user.domain'} = $dom;
                           }
                       }
                   }
               } else {
                   if ( ($enrollvar{$crs}{autoadds} == 1) || ($enrollvar{$crs}{autodrops} == 1) ) {
                       if ( ($enrollvar{$crs}{autostart} < $timenow) && ( ($enrollvar{$crs}{autoend} > $timenow) || ($enrollvar{$crs}{autoend} == 0) ) ) {
                           print $fh &mt('No institutional classlist data could be retrieved for [_1]',$crs)."\n";
                       } else {
                           print $fh &mt('Not within time window for auto-enrollment in [_1]',$crs)."\n";
                       }
                   } else {
                       print $fh &mt('Auto-enrollment not currently enabled for [_1]',$crs)."\n";
                   }
               }
           }
           print $fh "-- ".localtime(time).' '.&mt('Enrollment messages end')."\n*******************\n\n";
           close($fh);
           delete($env{'user.domain'});
       }
   
 1;  1;

Removed from v.1.1  
changed lines
  Added in v.1.36


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