Annotation of loncom/enrollment/Autoenroll.pl, revision 1.8

1.2       raeburn     1: #!/usr/bin/perl
1.5       albertel    2: #
                      3: #Automated Enrollment script
1.8     ! raeburn     4: # $Id: Autoenroll.pl,v 1.7 2004/02/08 15:47:45 raeburn Exp $
1.5       albertel    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: #
1.1       raeburn    28: 
1.2       raeburn    29:     use strict;
                     30:     use lib '/home/httpd/lib/perl';
                     31:     use localenroll;
                     32:     use LONCAPA::Configuration;
                     33:     use LONCAPA::Enrollment;
                     34:     use Apache::lonnet;
                     35:     use Apache::loncoursedata;
                     36:     use Apache::lonmsg;
                     37:     use HTML::Entities;
1.6       albertel   38: 
                     39: #only run if configured to
                     40:     if (! &localenroll::run()) { exit; }
1.1       raeburn    41: 
                     42: # Determine the library server's domain
1.2       raeburn    43:     my $perlvarref = LONCAPA::Configuration::read_conf('loncapa.conf');
                     44:     my $dom = $$perlvarref{'lonDefDomain'};
1.4       raeburn    45:     my $logfile = $$perlvarref{'lonDaemons'}.'/logs/autoenroll.log';
1.2       raeburn    46:     $ENV{'user.domain'} = $dom;
1.1       raeburn    47: 
                     48: # Determine the present time;
1.2       raeburn    49:     my $timenow = time();
1.1       raeburn    50: 
                     51: # Determine the courses
1.2       raeburn    52:     my %courses = &Apache::lonnet::courseiddump($dom,'.',1); 
                     53:     my %affiliates = ();
                     54:     my %enrollvar = ();
                     55:     my %reply = ();
                     56:     my %LC_code = ();
                     57:     foreach my $key (sort keys %courses) {
                     58:         my $crs;
                     59:         if ($key =~ m/^($dom)_(\w+)$/) {
                     60:             $crs = $2;
                     61:         }
1.1       raeburn    62: 
                     63: # Get course settings
1.2       raeburn    64:         my %settings = &Apache::lonnet::dump('environment',$dom,$crs);
                     65:         %{$enrollvar{$crs}} = ();
                     66:         @{$affiliates{$crs}} = ();
                     67:         %{$LC_code{$crs}} = ();
                     68:         foreach my $item (keys %settings) {
                     69:             if ($item =~ m/^internal\.(.+)$/) {
                     70:                 $enrollvar{$crs}{$1} = $settings{$item};
                     71:             } elsif ($item eq 'description') {
                     72:                 $enrollvar{$crs}{$item} = &HTML::Entities::decode($settings{$item});  
                     73:             }
                     74:         }
1.7       raeburn    75:         if (($enrollvar{$crs}{autostart} <= $timenow) && ( ($enrollvar{$crs}{autoend} > $timenow) || ($enrollvar{$crs}{autoend} == 0) ) ) {
1.2       raeburn    76:             if ( ($enrollvar{$crs}{autoadds} == 1) || ($enrollvar{$crs}{autodrops} == 1) ) {
1.1       raeburn    77: # Add to list of classes for retrieval
1.2       raeburn    78:                 $enrollvar{$crs}{sectionnums} =~ s/ //g;
                     79:                 $enrollvar{$crs}{crosslistings} =~ s/ //g;
                     80:                 my @sections = ();
                     81:                 my @crosslistings = ();
                     82:                 if ($enrollvar{$crs}{sectionnums} =~ m/,/) {
                     83:                     @sections = split/,/,$enrollvar{$crs}{sectionnums};
                     84:                 } else {
                     85:                     $sections[0] = $enrollvar{$crs}{sectionnums};
                     86:                 }
                     87:                 if ($enrollvar{$crs}{crosslistings} =~ m/,/) {
                     88:                     @crosslistings = split/,/,$enrollvar{$crs}{crosslistings}
                     89:                 } else {
                     90:                     @crosslistings = $enrollvar{$crs}{crosslistings};
                     91:                 }
                     92:                 foreach my $sec (@sections) {
                     93:                     if ($sec =~ m/^(\w+):(\w*)$/ ) {
                     94:                         my $course_id = $enrollvar{$crs}{coursecode}.$1;
                     95:                         my $gp = $2;
                     96:                         if (!grep/^$course_id$/,@{$affiliates{$crs}}) {
                     97:                             push @{$affiliates{$crs}}, $course_id;
                     98:                             $LC_code{$crs}{$course_id} = $gp; 
                     99:                         }
                    100:                     }
                    101:                 }
                    102:                 foreach my $xlist (@crosslistings) {
                    103:                     if ($xlist =~ m/^(\w+):(\w*)$/) {
                    104:                         my $course_id = $1;
                    105:                         my $gp = $2;
                    106:                         if (!grep/^$course_id$/,@{$affiliates{$crs}}) {
                    107:                             push @{$affiliates{$crs}}, $course_id;
                    108:                             $LC_code{$crs}{$course_id} = $gp;
                    109:                         }
                    110:                     }
                    111:                 }
                    112:             }
                    113:         }
                    114:     }
                    115:     &localenroll::fetch_enrollment($dom,\%affiliates,\%reply);
1.1       raeburn   116: 
                    117: # Now go through classes and perform required enrollment changes.
1.4       raeburn   118:     open (my $fh,">>$logfile");
                    119:     print $fh "********************\n".localtime(time)." Enrollment messages start --\n";
1.2       raeburn   120:     foreach my $crs (sort keys %enrollvar) {
1.4       raeburn   121:         my $logmsg = '';
                    122:         my $newusermsg = '';
1.2       raeburn   123:         if ($reply{$crs} > 0) {
1.7       raeburn   124:             if ( ($enrollvar{$crs}{autostart} < $timenow) && ( ($enrollvar{$crs}{autoend} > $timenow) || ($enrollvar{$crs}{autoend} == 0) ) ) {
1.2       raeburn   125:                 if (($enrollvar{$crs}{autoadds} == 1) || ($enrollvar{$crs}{autodrops} == 1)) {
1.4       raeburn   126:                     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');
                    127:                     print $fh "Messages start for $crs\n";
                    128:                     print $fh "$logmsg\n";
                    129:                     print $fh "Messages end for $crs\n";
1.2       raeburn   130:                     if ($changecount > 0) {
                    131:                         unless ($enrollvar{$crs}{notifylist}  eq '') {
                    132: # Send message about enrollment changes to notifylist.
                    133: # Set $ENV{'user.name'}, $ENV{'user.home'} for use by logging in lonmsg
                    134:                             unless ( ($enrollvar{$crs}{'courseowner'} eq '') || (!defined($enrollvar{$crs}{'courseowner'}) )  ) {
                    135:                                 $ENV{'user.name'} = $enrollvar{$crs}{'courseowner'};
                    136:                                 $ENV{'user.home'} = &Apache::lonnet::homeserver($ENV{'user.name'},$dom);
                    137: 
                    138:                                 my $subject = "Student enrollment changes in $enrollvar{$crs}{coursecode}";
                    139:                                 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;
1.4       raeburn   140:                                 unless ($newusermsg eq '') 
                    141:                                 {
                    142:                                    $message .= "\n".$newusermsg;
                    143:                                 }
1.2       raeburn   144:                                 my @to_notify = ();
                    145:                                 if ($enrollvar{$crs}{notifylist} =~ m/,/) {
                    146:                                     @to_notify = split/,/,$enrollvar{$crs}{notifylist};
                    147:                                 } else {
                    148:                                     $to_notify[0] = $enrollvar{$crs}{notifylist};
                    149:                                 }
                    150:                                 foreach my $cc (@to_notify) {
                    151:                                     my ($ccname,$ccdom) = split/@/,$cc;
                    152:                                     my $status =  &Apache::lonmsg::user_normal_msg($ccname,$ccdom,$subject,$message);
                    153:                                 }
1.4       raeburn   154:                                 if ( ($enrollvar{$crs}{notifylist} eq '') && ($newusermsg ne '') ) {
                    155:                                      my $subject = "New user accounts in  $enrollvar{$crs}{'coursecode'}";
                    156:                                      my $status =  &Apache::lonmsg::user_normal_msg($ENV{'user.name'},$dom,$subject,$newusermsg);
                    157:                                 }
1.2       raeburn   158:                                 delete($ENV{'user.name'});
                    159:                                 delete($ENV{'user.home'});
                    160:                             }
                    161:                         }
                    162:                     }
                    163:                 }
                    164:             }
                    165:         } else {
1.8     ! raeburn   166:             if ( ($enrollvar{$crs}{autoadds} == 1) || ($enrollvar{$crs}{autodrops} == 1) ) {
        !           167:                 if ( ($enrollvar{$crs}{autostart} < $timenow) && ( ($enrollvar{$crs}{autoend} > $timenow) || ($enrollvar{$crs}{autoend} == 0) ) ) {
        !           168:                     print $fh "No institutional classlist data could be retrieved for $crs\n";
        !           169:                 } else {
        !           170:                     print $fh "Not within time window for auto-enrollment in $crs\n";
        !           171:                 }
        !           172:             } else {
        !           173:                 print $fh "Auto-enrollment not currently enabled for $crs\n";
        !           174:             }
1.2       raeburn   175:         }
                    176:     }
1.4       raeburn   177:     print $fh "-- ".localtime(time)." Enrollment messages end\n*******************\n\n";
                    178:     close($fh);
1.2       raeburn   179:     delete($ENV{'user.domain'});
1.1       raeburn   180: 
                    181: # Check for photos
                    182: 
                    183: 1;

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