--- loncom/enrollment/Autoenroll.pl 2004/03/18 19:40:39 1.8 +++ loncom/enrollment/Autoenroll.pl 2005/11/15 20:53:50 1.16 @@ -1,7 +1,7 @@ #!/usr/bin/perl # #Automated Enrollment script -# $Id: Autoenroll.pl,v 1.8 2004/03/18 19:40:39 raeburn Exp $ +# $Id: Autoenroll.pl,v 1.16 2005/11/15 20:53:50 albertel Exp $ # # Copyright Michigan State University Board of Trustees # @@ -25,7 +25,9 @@ # # http://www.lon-capa.org/ # - +BEGIN { + eval "use Apache2::compat();"; +}; use strict; use lib '/home/httpd/lib/perl'; use localenroll; @@ -36,148 +38,154 @@ use Apache::lonmsg; use HTML::Entities; -#only run if configured to - if (! &localenroll::run()) { exit; } - -# Determine the library server's domain +# Determine the library server's domain and hostID my $perlvarref = LONCAPA::Configuration::read_conf('loncapa.conf'); - my $dom = $$perlvarref{'lonDefDomain'}; my $logfile = $$perlvarref{'lonDaemons'}.'/logs/autoenroll.log'; - $ENV{'user.domain'} = $dom; + my @domains = &Apache::lonnet::current_machine_domains(); + my @hostids = &Apache::lonnet::current_machine_ids(); # Determine the present time; my $timenow = time(); -# Determine the courses - my %courses = &Apache::lonnet::courseiddump($dom,'.',1); - my %affiliates = (); - my %enrollvar = (); - my %reply = (); - my %LC_code = (); - foreach my $key (sort keys %courses) { - my $crs; - if ($key =~ m/^($dom)_(\w+)$/) { - $crs = $2; - } +# For each domain ...... + foreach my $dom (@domains) { + #only run if configured to + if (! &localenroll::run($dom)) { next; } + $env{'user.domain'} = $dom; + # Determine the courses + my %courses = &Apache::lonnet::courseiddump($dom,'.',1,'.','.','.',1,\@hostids); + 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 - my %settings = &Apache::lonnet::dump('environment',$dom,$crs); - %{$enrollvar{$crs}} = (); - @{$affiliates{$crs}} = (); - %{$LC_code{$crs}} = (); - foreach my $item (keys %settings) { - if ($item =~ m/^internal\.(.+)$/) { - $enrollvar{$crs}{$1} = $settings{$item}; - } elsif ($item eq 'description') { - $enrollvar{$crs}{$item} = &HTML::Entities::decode($settings{$item}); + my %settings = &Apache::lonnet::dump('environment',$dom,$crs); + %{$enrollvar{$crs}} = (); + @{$affiliates{$crs}} = (); + %{$LC_code{$crs}} = (); + foreach my $item (keys %settings) { + if ($item =~ m/^internal\.(.+)$/) { + $enrollvar{$crs}{$1} = $settings{$item}; + } elsif ($item eq 'description') { + $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) ) { + 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 - $enrollvar{$crs}{sectionnums} =~ s/ //g; - $enrollvar{$crs}{crosslistings} =~ s/ //g; - my @sections = (); - my @crosslistings = (); - if ($enrollvar{$crs}{sectionnums} =~ m/,/) { - @sections = split/,/,$enrollvar{$crs}{sectionnums}; - } else { - $sections[0] = $enrollvar{$crs}{sectionnums}; - } - if ($enrollvar{$crs}{crosslistings} =~ m/,/) { - @crosslistings = split/,/,$enrollvar{$crs}{crosslistings} - } else { - @crosslistings = $enrollvar{$crs}{crosslistings}; - } - foreach my $sec (@sections) { - if ($sec =~ m/^(\w+):(\w*)$/ ) { - my $course_id = $enrollvar{$crs}{coursecode}.$1; - my $gp = $2; - if (!grep/^$course_id$/,@{$affiliates{$crs}}) { - push @{$affiliates{$crs}}, $course_id; - $LC_code{$crs}{$course_id} = $gp; + $enrollvar{$crs}{sectionnums} =~ s/ //g; + $enrollvar{$crs}{crosslistings} =~ s/ //g; + my @sections = (); + my @crosslistings = (); + if ($enrollvar{$crs}{sectionnums} =~ m/,/) { + @sections = split/,/,$enrollvar{$crs}{sectionnums}; + } else { + $sections[0] = $enrollvar{$crs}{sectionnums}; + } + if ($enrollvar{$crs}{crosslistings} =~ m/,/) { + @crosslistings = split/,/,$enrollvar{$crs}{crosslistings} + } else { + @crosslistings = $enrollvar{$crs}{crosslistings}; + } + foreach my $sec (@sections) { + if ($sec =~ m/^(\w+):(\w*)$/ ) { + my $course_id = $enrollvar{$crs}{coursecode}.$1; + my $gp = $2; + if (!grep/^$course_id$/,@{$affiliates{$crs}}) { + push @{$affiliates{$crs}}, $course_id; + $LC_code{$crs}{$course_id} = $gp; + } } } - } - foreach my $xlist (@crosslistings) { - if ($xlist =~ m/^(\w+):(\w*)$/) { - my $course_id = $1; - my $gp = $2; - if (!grep/^$course_id$/,@{$affiliates{$crs}}) { - push @{$affiliates{$crs}}, $course_id; - $LC_code{$crs}{$course_id} = $gp; + foreach my $xlist (@crosslistings) { + if ($xlist =~ m/^([^:]+):(\w*)$/) { + my $course_id = $1; + my $gp = $2; + if (!grep/^$course_id$/,@{$affiliates{$crs}}) { + push @{$affiliates{$crs}}, $course_id; + $LC_code{$crs}{$course_id} = $gp; + } } } } } } - } - &localenroll::fetch_enrollment($dom,\%affiliates,\%reply); + my $outcome = &Apache::lonnet::fetch_enrollment_query('automated',\%affiliates,\%reply,$dom); # Now go through classes and perform required enrollment changes. - open (my $fh,">>$logfile"); - print $fh "********************\n".localtime(time)." Enrollment messages start --\n"; - foreach my $crs (sort keys %enrollvar) { - my $logmsg = ''; - 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 ($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 "Messages start for $crs\n"; - print $fh "$logmsg\n"; - print $fh "Messages end for $crs\n"; - if ($changecount > 0) { - unless ($enrollvar{$crs}{notifylist} eq '') { + open (my $fh,">>$logfile"); + print $fh "********************\n".localtime(time)." Enrollment messages start --\n"; + print $fh "Response from fetch_enrollment_query was $outcome\n"; + foreach my $crs (sort keys %enrollvar) { + my $logmsg = ''; + 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 ($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 "Messages start for $crs\n"; + print $fh "$logmsg\n"; + print $fh "Messages end for $crs\n"; + if ($changecount > 0) { + unless ($enrollvar{$crs}{notifylist} eq '') { # Send message about enrollment changes to notifylist. -# Set $ENV{'user.name'}, $ENV{'user.home'} for use by logging in lonmsg - unless ( ($enrollvar{$crs}{'courseowner'} eq '') || (!defined($enrollvar{$crs}{'courseowner'}) ) ) { - $ENV{'user.name'} = $enrollvar{$crs}{'courseowner'}; - $ENV{'user.home'} = &Apache::lonnet::homeserver($ENV{'user.name'},$dom); - - my $subject = "Student enrollment changes in $enrollvar{$crs}{coursecode}"; - 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; - unless ($newusermsg eq '') - { - $message .= "\n".$newusermsg; - } - my @to_notify = (); - if ($enrollvar{$crs}{notifylist} =~ m/,/) { - @to_notify = split/,/,$enrollvar{$crs}{notifylist}; - } else { - $to_notify[0] = $enrollvar{$crs}{notifylist}; +# Set $env{'user.name'}, $env{'user.home'} for use by logging in lonmsg + unless ( ($enrollvar{$crs}{'courseowner'} eq '') || (!defined($enrollvar{$crs}{'courseowner'}) ) ) { + $env{'user.name'} = $enrollvar{$crs}{'courseowner'}; + $env{'user.home'} = &Apache::lonnet::homeserver($env{'user.name'},$dom); + + my $subject = "Student enrollment changes in $enrollvar{$crs}{coursecode}"; + 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; + unless ($newusermsg eq '') { + $message .= "\n".$newusermsg; + } + my @to_notify = (); + if ($enrollvar{$crs}{notifylist} =~ m/,/) { + @to_notify = split/,/,$enrollvar{$crs}{notifylist}; + } else { + $to_notify[0] = $enrollvar{$crs}{notifylist}; + } + foreach my $cc (@to_notify) { + my ($ccname,$ccdom) = split/@/,$cc; + my $status = &Apache::lonmsg::user_normal_msg($ccname,$ccdom,$subject,$message); + } + if ( ($enrollvar{$crs}{notifylist} eq '') && ($newusermsg ne '') ) { + my $subject = "New user accounts in $enrollvar{$crs}{'coursecode'}"; + my $status = &Apache::lonmsg::user_normal_msg($env{'user.name'},$dom,$subject,$newusermsg); + } + delete($env{'user.name'}); + delete($env{'user.home'}); } - foreach my $cc (@to_notify) { - my ($ccname,$ccdom) = split/@/,$cc; - my $status = &Apache::lonmsg::user_normal_msg($ccname,$ccdom,$subject,$message); - } - if ( ($enrollvar{$crs}{notifylist} eq '') && ($newusermsg ne '') ) { - my $subject = "New user accounts in $enrollvar{$crs}{'coursecode'}"; - my $status = &Apache::lonmsg::user_normal_msg($ENV{'user.name'},$dom,$subject,$newusermsg); - } - delete($ENV{'user.name'}); - delete($ENV{'user.home'}); } } } } - } - } 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 "No institutional classlist data could be retrieved for $crs\n"; + } 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 "No institutional classlist data could be retrieved for $crs\n"; + } else { + print $fh "Not within time window for auto-enrollment in $crs\n"; + } } else { - print $fh "Not within time window for auto-enrollment in $crs\n"; + print $fh "Auto-enrollment not currently enabled for $crs\n"; } - } else { - print $fh "Auto-enrollment not currently enabled for $crs\n"; } } - } - print $fh "-- ".localtime(time)." 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;