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

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

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