Diff for /loncom/enrollment/Autoenroll.pl between versions 1.9 and 1.26

version 1.9, 2004/06/08 22:09:44 version 1.26, 2007/05/14 17:35:01
Line 25 Line 25
 #  #
 # http://www.lon-capa.org/  # http://www.lon-capa.org/
 #  #
   
     use strict;      use strict;
     use lib '/home/httpd/lib/perl';      use lib '/home/httpd/lib/perl';
     use localenroll;      use localenroll;
Line 34 Line 33
     use Apache::lonnet;      use Apache::lonnet;
     use Apache::loncoursedata;      use Apache::loncoursedata;
     use Apache::lonmsg;      use Apache::lonmsg;
       use Apache::longroup;
       use Apache::loncommon;
       use Apache::lonlocal;
     use HTML::Entities;      use HTML::Entities;
   
 #only run if configured to  
     if (! &localenroll::run()) { exit; }  
   
 # Determine the library server's domain and hostID  # Determine the library server's domain and hostID
     my $perlvarref = LONCAPA::Configuration::read_conf('loncapa.conf');      my $perlvarref = LONCAPA::Configuration::read_conf('loncapa.conf');
     my $dom = $$perlvarref{'lonDefDomain'};  
     my $logfile = $$perlvarref{'lonDaemons'}.'/logs/autoenroll.log';      my $logfile = $$perlvarref{'lonDaemons'}.'/logs/autoenroll.log';
     $ENV{'user.domain'} = $dom;      my @domains = &Apache::lonnet::current_machine_domains();
     my $hostid = $$perlvarref{'lonHostID'};      my @hostids = &Apache::lonnet::current_machine_ids();
   
 # 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,$hostid);       foreach my $dom (@domains) {
     my %affiliates = ();          #only run if configured to
     my %enrollvar = ();          my $run_enroll = 0;
     my %reply = ();          my $settings;
     my %LC_code = ();          my %domconfig =
     foreach my $key (sort keys %courses) {              &Apache::lonnet::get_dom('configuration',['autoenroll'],$dom);
         my $crs;          if (ref($domconfig{'autoenroll'}) eq 'HASH') {
         if ($key =~ m/^($dom)_(\w+)$/) {              $settings = $domconfig{'autoenroll'};
             $crs = $2;              if ($settings->{'run'} eq '1') {
                   $run_enroll = 1;
               }
           } 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 %enrollvar = ();
           my %reply = ();
           my %LC_code = ();
           foreach my $key (sort keys %courses) {
               my $crs;
               if ($key =~ m/^($dom)_(\w+)$/) {
                   $crs = $2;
               }
   
 # Get course settings  # Get course settings
         my %settings = &Apache::lonnet::dump('environment',$dom,$crs);              my %settings = &Apache::lonnet::dump('environment',$dom,$crs);
         %{$enrollvar{$crs}} = ();              %{$enrollvar{$crs}} = ();
         @{$affiliates{$crs}} = ();              @{$affiliates{$crs}} = ();
         %{$LC_code{$crs}} = ();              %{$LC_code{$crs}} = ();
         foreach my $item (keys %settings) {              foreach my $item (keys %settings) {
             if ($item =~ m/^internal\.(.+)$/) {                  if ($item =~ m/^internal\.(.+)$/) {
                 $enrollvar{$crs}{$1} = $settings{$item};                      $enrollvar{$crs}{$1} = $settings{$item};
             } elsif ($item eq 'description') {                  } elsif ($item eq 'description') {
                 $enrollvar{$crs}{$item} = &HTML::Entities::decode($settings{$item});                        $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}{autostart} <= $timenow) && ( ($enrollvar{$crs}{autoend} > $timenow) || ($enrollvar{$crs}{autoend} == 0) ) ) {                  if ( ($enrollvar{$crs}{autoadds} == 1) || ($enrollvar{$crs}{autodrops} == 1) ) {
             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[0] = $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) {                      foreach my $sec (@sections) {
                     if ($sec =~ m/^(\w+):(\w*)$/ ) {                          if ($sec =~ m/^(\w+):(\w*)$/ ) {
                         my $course_id = $enrollvar{$crs}{coursecode}.$1;                              my $course_id = $enrollvar{$crs}{coursecode}.$1;
                         my $gp = $2;                              my $gp = $2;
                         if (!grep/^$course_id$/,@{$affiliates{$crs}}) {                              if (!grep/^$course_id$/,@{$affiliates{$crs}}) {
                             push @{$affiliates{$crs}}, $course_id;                                  push @{$affiliates{$crs}}, $course_id;
                             $LC_code{$crs}{$course_id} = $gp;                                   $LC_code{$crs}{$course_id} = $gp; 
                               }
                         }                          }
                     }                      }
                 }                      foreach my $xlist (@crosslistings) {
                 foreach my $xlist (@crosslistings) {                          if ($xlist =~ m/^([^:]+):(\w*)$/) {
                     if ($xlist =~ m/^(\w+):(\w*)$/) {                              my $course_id = $1;
                         my $course_id = $1;                              my $gp = $2;
                         my $gp = $2;                              if (!grep/^$course_id$/,@{$affiliates{$crs}}) {
                         if (!grep/^$course_id$/,@{$affiliates{$crs}}) {                                  push @{$affiliates{$crs}}, $course_id;
                             push @{$affiliates{$crs}}, $course_id;                                  $LC_code{$crs}{$course_id} = $gp;
                             $LC_code{$crs}{$course_id} = $gp;                              }
                         }                          }
                     }                      }
                 }                  }
             }              }
         }          }
     }          my $outcome = &Apache::lonnet::fetch_enrollment_query('automated',\%affiliates,\%reply,$dom);
     my $outcome = &Apache::lonnet::fetch_enrollment_query($hostid,$dom,\%affiliates,\%reply);  
   
 # Now go through classes and perform required enrollment changes.  # Now go through classes and perform required enrollment changes.
     open (my $fh,">>$logfile");          open (my $fh,">>$logfile");
     print $fh "********************\n".localtime(time)." Enrollment messages start --\n";          print $fh "********************\n".localtime(time).' '.&mt('Enrollment messages start').' --'."\n";
     foreach my $crs (sort keys %enrollvar) {          print $fh &mt("Response from [_1] was [_2]",'fetch_enrollment_query',$outcome)."\n";
         my $logmsg = '';          foreach my $crs (sort keys %enrollvar) {
         my $newusermsg = '';              my $logmsg = '';
         if ($reply{$crs} > 0) {              my $newusermsg = '';
             if ( ($enrollvar{$crs}{autostart} < $timenow) && ( ($enrollvar{$crs}{autoend} > $timenow) || ($enrollvar{$crs}{autoend} == 0) ) ) {              if ($reply{$crs} > 0) {
                 if (($enrollvar{$crs}{autoadds} == 1) || ($enrollvar{$crs}{autodrops} == 1)) {                  if ( ($enrollvar{$crs}{autostart} < $timenow) && ( ($enrollvar{$crs}{autoend} > $timenow) || ($enrollvar{$crs}{autoend} == 0) ) ) {
                     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');                      if (($enrollvar{$crs}{autoadds} == 1) || ($enrollvar{$crs}{autodrops} == 1)) {
                     print $fh "Messages start for $crs\n";                          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');
                     print $fh "$logmsg\n";                          print $fh &mt('Messages start for [_1]',$crs)."\n";
                     print $fh "Messages end for $crs\n";                          print $fh "$logmsg\n";
                     if ($changecount > 0) {                          print $fh &mt('Messages end for [_1]',$crs)."\n";
                         unless ($enrollvar{$crs}{notifylist}  eq '') {                          if ($changecount > 0) {
                               unless ($enrollvar{$crs}{notifylist}  eq '') {
 # Send message about enrollment changes to notifylist.  # Send message about enrollment changes to notifylist.
 # Set $ENV{'user.name'}, $ENV{'user.home'} for use by logging in lonmsg  # Set $env{'user.name'}, $env{'user.domain'}, $env{'user.home'}
                             unless ( ($enrollvar{$crs}{'courseowner'} eq '') || (!defined($enrollvar{$crs}{'courseowner'}) )  ) {  # for use by logging in lonmsg
                                 $ENV{'user.name'} = $enrollvar{$crs}{'courseowner'};                                  unless ( ($enrollvar{$crs}{'courseowner'} eq '') || (!defined($enrollvar{$crs}{'courseowner'}) )  ) {
                                 $ENV{'user.home'} = &Apache::lonnet::homeserver($ENV{'user.name'},$dom);                                      if ($enrollvar{$crs}{'courseowner'} =~ /:/) {
                                           ($env{'user.name'},$env{'user.domain'}) = split(/:/,$enrollvar{$crs}{'courseowner'});
                                 my $subject = "Student enrollment changes in $enrollvar{$crs}{coursecode}";                                      } else {
                                 my $message = "The following $changecount change(s) occurred in $enrollvar{$crs}{description} - $enrollvar{$crs}{coursecode} as a result of the automated classlist update:\n\n".$response;                                          $env{'user.name'} = $enrollvar{$crs}{'courseowner'};
                                 unless ($newusermsg eq '')                                           $env{'user.domain'} = $dom;
                                 {                                      }
                                    $message .= "\n".$newusermsg;                                      $env{'user.home'} = &Apache::lonnet::homeserver($env{'user.name'},$env{'user.domain'});
                                 }  
                                 my @to_notify = ();                                      my $subject = &mt('Student enrollment changes in [_1]',$enrollvar{$crs}{coursecode});
                                 if ($enrollvar{$crs}{notifylist} =~ m/,/) {                                      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;
                                     @to_notify = split/,/,$enrollvar{$crs}{notifylist};                                      unless ($newusermsg eq '') { 
                                 } else {                                          $message .= "\n".$newusermsg;
                                     $to_notify[0] = $enrollvar{$crs}{notifylist};                                      }
                                 }                                      my @to_notify = ();
                                 foreach my $cc (@to_notify) {                                      if ($enrollvar{$crs}{notifylist} =~ m/,/) {
                                     my ($ccname,$ccdom) = split/@/,$cc;                                          @to_notify = split/,/,$enrollvar{$crs}{notifylist};
                                     my $status =  &Apache::lonmsg::user_normal_msg($ccname,$ccdom,$subject,$message);                                      } else {
                                 }                                          $to_notify[0] = $enrollvar{$crs}{notifylist};
                                 if ( ($enrollvar{$crs}{notifylist} eq '') && ($newusermsg ne '') ) {                                      }
                                      my $subject = "New user accounts in  $enrollvar{$crs}{'coursecode'}";                                      foreach my $cc (@to_notify) {
                                      my $status =  &Apache::lonmsg::user_normal_msg($ENV{'user.name'},$dom,$subject,$newusermsg);                                          my ($ccname,$ccdom);
                                           if ($cc =~ /:/) {
                                               ($ccname,$ccdom) = split(/:/,$cc);
                                           } elsif ($cc =~ /\@/) {
                                               ($ccname,$ccdom) = split(/\@/,$cc);
                                           }
                                           my $status =  &Apache::lonmsg::user_normal_msg($ccname,$ccdom,$subject,$message);
                                       }
                                       if ( ($enrollvar{$crs}{notifylist} eq '') && ($newusermsg ne '') ) {
                                           my $subject = &mt('New user accounts in [_1]',$enrollvar{$crs}{'coursecode'});
                                           my $status =  &Apache::lonmsg::user_normal_msg($env{'user.name'},$env{'user.domain'},$subject,$newusermsg);
                                       }
                                       delete($env{'user.name'});
                                       delete($env{'user.home'});
                                       $env{'user.domain'} = $dom;
                                 }                                  }
                                 delete($ENV{'user.name'});  
                                 delete($ENV{'user.home'});  
                             }                              }
                         }                          }
                     }                      }
                 }                  }
             }              } else {
         } else {                  if ( ($enrollvar{$crs}{autoadds} == 1) || ($enrollvar{$crs}{autodrops} == 1) ) {
             if ( ($enrollvar{$crs}{autoadds} == 1) || ($enrollvar{$crs}{autodrops} == 1) ) {                      if ( ($enrollvar{$crs}{autostart} < $timenow) && ( ($enrollvar{$crs}{autoend} > $timenow) || ($enrollvar{$crs}{autoend} == 0) ) ) {
                 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";
                     print $fh "No institutional classlist data could be retrieved for $crs\n";                      } else {
                           print $fh ('Not within time window for auto-enrollment in [_1]',$crs)."\n";
                       }
                 } else {                  } else {
                     print $fh "Not within time window for auto-enrollment in $crs\n";                      print $fh &mt('Auto-enrollment not currently enabled for [_1]',$crs)."\n";
                 }                  }
             } else {  
                 print $fh "Auto-enrollment not currently enabled for $crs\n";  
             }              }
         }          }
           print $fh "-- ".localtime(time).' '.&mt('Enrollment messages end')."\n*******************\n\n";
           close($fh);
           delete($env{'user.domain'});
     }      }
     print $fh "-- ".localtime(time)." Enrollment messages end\n*******************\n\n";  
     close($fh);  
     delete($ENV{'user.domain'});  
   
 # Check for photos  
   
 1;  1;

Removed from v.1.9  
changed lines
  Added in v.1.26


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