File:  [LON-CAPA] / loncom / enrollment / Autoenroll.pl
Revision 1.4: download - view: text, annotated - select for diffs
Thu Dec 11 03:24:20 2003 UTC (20 years, 7 months ago) by raeburn
Branches: MAIN
CVS tags: HEAD
Added e-mailing of usernames and passwords to new accounts if e-mail addresses provided for the new users, or to the course owner via a LON-CAPA message if no e-mail addresses available.

    1: #!/usr/bin/perl
    2: 
    3:     use strict;
    4:     use lib '/home/httpd/lib/perl';
    5:     use localenroll;
    6:     use LONCAPA::Configuration;
    7:     use LONCAPA::Enrollment;
    8:     use Apache::lonnet;
    9:     use Apache::loncoursedata;
   10:     use Apache::lonmsg;
   11:     use HTML::Entities;
   12: 
   13: # Determine the library server's domain
   14:     my $perlvarref = LONCAPA::Configuration::read_conf('loncapa.conf');
   15:     my $dom = $$perlvarref{'lonDefDomain'};
   16:     my $logfile = $$perlvarref{'lonDaemons'}.'/logs/autoenroll.log';
   17:     $ENV{'user.domain'} = $dom;
   18: 
   19: # Determine the present time;
   20:     my $timenow = time();
   21: 
   22: # Determine the courses
   23:     my %courses = &Apache::lonnet::courseiddump($dom,'.',1); 
   24:     my %affiliates = ();
   25:     my %enrollvar = ();
   26:     my %reply = ();
   27:     my %LC_code = ();
   28:     foreach my $key (sort keys %courses) {
   29:         my $crs;
   30:         if ($key =~ m/^($dom)_(\w+)$/) {
   31:             $crs = $2;
   32:         }
   33: 
   34: # Get course settings
   35:         my %settings = &Apache::lonnet::dump('environment',$dom,$crs);
   36:         %{$enrollvar{$crs}} = ();
   37:         @{$affiliates{$crs}} = ();
   38:         %{$LC_code{$crs}} = ();
   39:         foreach my $item (keys %settings) {
   40:             if ($item =~ m/^internal\.(.+)$/) {
   41:                 $enrollvar{$crs}{$1} = $settings{$item};
   42:             } elsif ($item eq 'description') {
   43:                 $enrollvar{$crs}{$item} = &HTML::Entities::decode($settings{$item});  
   44:             }
   45:         }
   46:         if (($enrollvar{$crs}{autostart} <= $timenow) && ($enrollvar{$crs}{autoend} > $timenow)) {
   47:             if ( ($enrollvar{$crs}{autoadds} == 1) || ($enrollvar{$crs}{autodrops} == 1) ) {
   48: # Add to list of classes for retrieval
   49:                 $enrollvar{$crs}{sectionnums} =~ s/ //g;
   50:                 $enrollvar{$crs}{crosslistings} =~ s/ //g;
   51:                 my @sections = ();
   52:                 my @crosslistings = ();
   53:                 if ($enrollvar{$crs}{sectionnums} =~ m/,/) {
   54:                     @sections = split/,/,$enrollvar{$crs}{sectionnums};
   55:                 } else {
   56:                     $sections[0] = $enrollvar{$crs}{sectionnums};
   57:                 }
   58:                 if ($enrollvar{$crs}{crosslistings} =~ m/,/) {
   59:                     @crosslistings = split/,/,$enrollvar{$crs}{crosslistings}
   60:                 } else {
   61:                     @crosslistings = $enrollvar{$crs}{crosslistings};
   62:                 }
   63:                 foreach my $sec (@sections) {
   64:                     if ($sec =~ m/^(\w+):(\w*)$/ ) {
   65:                         my $course_id = $enrollvar{$crs}{coursecode}.$1;
   66:                         my $gp = $2;
   67:                         if (!grep/^$course_id$/,@{$affiliates{$crs}}) {
   68:                             push @{$affiliates{$crs}}, $course_id;
   69:                             $LC_code{$crs}{$course_id} = $gp; 
   70:                         }
   71:                     }
   72:                 }
   73:                 foreach my $xlist (@crosslistings) {
   74:                     if ($xlist =~ m/^(\w+):(\w*)$/) {
   75:                         my $course_id = $1;
   76:                         my $gp = $2;
   77:                         if (!grep/^$course_id$/,@{$affiliates{$crs}}) {
   78:                             push @{$affiliates{$crs}}, $course_id;
   79:                             $LC_code{$crs}{$course_id} = $gp;
   80:                         }
   81:                     }
   82:                 }
   83:             }
   84:         }
   85:     }
   86:     &localenroll::fetch_enrollment($dom,\%affiliates,\%reply);
   87: 
   88: # Now go through classes and perform required enrollment changes.
   89:     open (my $fh,">>$logfile");
   90:     print $fh "********************\n".localtime(time)." Enrollment messages start --\n";
   91:     foreach my $crs (sort keys %enrollvar) {
   92:         my $logmsg = '';
   93:         my $newusermsg = '';
   94:         if ($reply{$crs} > 0) {
   95:             if (($enrollvar{$crs}{autostart} < $timenow) && ($enrollvar{$crs}{autoend} > $timenow)) {
   96:                 if (($enrollvar{$crs}{autoadds} == 1) || ($enrollvar{$crs}{autodrops} == 1)) {
   97:                     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');
   98:                     print $fh "Messages start for $crs\n";
   99:                     print $fh "$logmsg\n";
  100:                     print $fh "Messages end for $crs\n";
  101:                     if ($changecount > 0) {
  102:                         unless ($enrollvar{$crs}{notifylist}  eq '') {
  103: # Send message about enrollment changes to notifylist.
  104: # Set $ENV{'user.name'}, $ENV{'user.home'} for use by logging in lonmsg
  105:                             unless ( ($enrollvar{$crs}{'courseowner'} eq '') || (!defined($enrollvar{$crs}{'courseowner'}) )  ) {
  106:                                 $ENV{'user.name'} = $enrollvar{$crs}{'courseowner'};
  107:                                 $ENV{'user.home'} = &Apache::lonnet::homeserver($ENV{'user.name'},$dom);
  108: 
  109:                                 my $subject = "Student enrollment changes in $enrollvar{$crs}{coursecode}";
  110:                                 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;
  111:                                 unless ($newusermsg eq '') 
  112:                                 {
  113:                                    $message .= "\n".$newusermsg;
  114:                                 }
  115:                                 my @to_notify = ();
  116:                                 if ($enrollvar{$crs}{notifylist} =~ m/,/) {
  117:                                     @to_notify = split/,/,$enrollvar{$crs}{notifylist};
  118:                                 } else {
  119:                                     $to_notify[0] = $enrollvar{$crs}{notifylist};
  120:                                 }
  121:                                 foreach my $cc (@to_notify) {
  122:                                     my ($ccname,$ccdom) = split/@/,$cc;
  123:                                     my $status =  &Apache::lonmsg::user_normal_msg($ccname,$ccdom,$subject,$message);
  124:                                 }
  125:                                 if ( ($enrollvar{$crs}{notifylist} eq '') && ($newusermsg ne '') ) {
  126:                                      my $subject = "New user accounts in  $enrollvar{$crs}{'coursecode'}";
  127:                                      my $status =  &Apache::lonmsg::user_normal_msg($ENV{'user.name'},$dom,$subject,$newusermsg);
  128:                                 }
  129:                                 delete($ENV{'user.name'});
  130:                                 delete($ENV{'user.home'});
  131:                             }
  132:                         }
  133:                     }
  134:                 }
  135:             }
  136:         } else {
  137:             print $fh "No institutional classlist data could be retrieved for $crs\n";
  138:         }
  139:     }
  140:     print $fh "-- ".localtime(time)." Enrollment messages end\n*******************\n\n";
  141:     close($fh);
  142:     delete($ENV{'user.domain'});
  143: 
  144: # Check for photos
  145: 
  146: 1;

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