--- loncom/enrollment/Autoenroll.pl 2007/12/05 02:04:02 1.28 +++ loncom/enrollment/Autoenroll.pl 2021/06/15 20:52:27 1.35 @@ -1,7 +1,7 @@ #!/usr/bin/perl # #Automated Enrollment script -# $Id: Autoenroll.pl,v 1.28 2007/12/05 02:04:02 raeburn Exp $ +# $Id: Autoenroll.pl,v 1.35 2021/06/15 20:52:27 raeburn Exp $ # # Copyright Michigan State University Board of Trustees # @@ -52,14 +52,17 @@ foreach my $dom (@domains) { #only run if configured to my $run_enroll = 0; - my $settings; + my ($domsettings,$autofailsafe); my %domconfig = - &Apache::lonnet::get_dom('configuration',['autoenroll'],$dom); + &Apache::lonnet::get_dom('configuration',['autoenroll','coursedefaults'],$dom); if (ref($domconfig{'autoenroll'}) eq 'HASH') { - $settings = $domconfig{'autoenroll'}; - if ($settings->{'run'} eq '1') { + $domsettings = $domconfig{'autoenroll'}; + if ($domsettings->{'run'} eq '1') { $run_enroll = 1; } + if ($domsettings->{'autofailsafe'} ne '') { + $autofailsafe = $domsettings->{'autofailsafe'}; + } } else { $run_enroll = &localenroll::run($dom); } @@ -71,18 +74,27 @@ # Determine the courses my %courses = &Apache::lonnet::courseiddump($dom,'.',1,'.','.','.',1,\@hostids,'Course'); my %affiliates = (); + my %unclutteredsec = (); + my %unclutteredlcsec = (); my %enrollvar = (); my %reply = (); my %LC_code = (); + my ($showcredits,$domdefcredits); + if ((ref($domconfig{'coursedefaults'}) eq 'HASH') && + (ref($domconfig{'coursedefaults'}{'coursecredits'}) eq 'HASH')) { + if ($domconfig{'coursedefaults'}{'coursecredits'}{'official'}) { + $showcredits = 1; + $domdefcredits = $domconfig{'coursedefaults'}{'coursecredits'}{'official'}; + } + } foreach my $key (sort keys %courses) { my %args = ( one_time => 1, ); - my %coursehash = &Apache::lonnet::coursedescription($key,\%args); - my $crs = $coursehash{'num'}; - next if ($coursehash{'domain'} ne $dom); # Get course settings - my %settings = &Apache::lonnet::dump('environment',$dom,$crs); + my %settings = &Apache::lonnet::coursedescription($key,\%args); + my $crs = $settings{'num'}; + next if ($settings{'domain'} ne $dom); %{$enrollvar{$crs}} = (); @{$affiliates{$crs}} = (); %{$LC_code{$crs}} = (); @@ -93,7 +105,7 @@ $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') { + } elsif ($item eq 'default_enrollment_end_date') { $enrollvar{$crs}{enddate} = $settings{$item}; } } @@ -110,17 +122,21 @@ $sections[0] = $enrollvar{$crs}{sectionnums}; } if ($enrollvar{$crs}{crosslistings} =~ m/,/) { - @crosslistings = split/,/,$enrollvar{$crs}{crosslistings} + @crosslistings = split/,/,$enrollvar{$crs}{crosslistings}; } else { @crosslistings = $enrollvar{$crs}{crosslistings}; } + my $crscode = $enrollvar{$crs}{coursecode}; + my $crskey = $crs.':'.$crscode; + @{$unclutteredsec{$crskey}} = (); + @{$unclutteredlcsec{$crskey}} = (); foreach my $sec (@sections) { if ($sec =~ m/^(\w+):(\w*)$/ ) { - my $course_id = $enrollvar{$crs}{coursecode}.$1; + my $instsec = $1; my $gp = $2; - if (!grep/^$course_id$/,@{$affiliates{$crs}}) { - push @{$affiliates{$crs}}, $course_id; - $LC_code{$crs}{$course_id} = $gp; + unless (grep/^\Q$instsec\E$/,@{$unclutteredsec{$crskey}}) { + push(@{$unclutteredsec{$crskey}},$instsec); + push(@{$unclutteredlcsec{$crskey}},$gp); } } } @@ -128,7 +144,7 @@ if ($xlist =~ m/^([^:]+):(\w*)$/) { my $course_id = $1; my $gp = $2; - if (!grep/^$course_id$/,@{$affiliates{$crs}}) { + if (!grep/^\Q$course_id\E$/,@{$affiliates{$crs}}) { push @{$affiliates{$crs}}, $course_id; $LC_code{$crs}{$course_id} = $gp; } @@ -137,6 +153,19 @@ } } } + my %formattedsec = &Apache::lonnet::auto_instsec_reformat($dom,'clutter',\%unclutteredsec); + foreach my $crskey (keys(%formattedsec)) { + my ($crs,$instcode) = split(/:/,$crskey); + if ((ref($formattedsec{$crskey}) eq 'ARRAY') && (ref($unclutteredlcsec{$crskey}) eq 'ARRAY')) { + for (my $i=0; $i<@{$formattedsec{$crskey}}; $i++) { + my $course_id = $instcode.$formattedsec{$crskey}[$i]; + unless (grep/^\Q$course_id\E$/,@{$affiliates{$crs}}) { + push(@{$affiliates{$crs}},$course_id); + $LC_code{$crs}{$course_id} = $unclutteredlcsec{$crskey}[$i]; + } + } + } + } my $outcome = &Apache::lonnet::fetch_enrollment_query('automated',\%affiliates,\%reply,$dom); # Now go through classes and perform required enrollment changes. @@ -149,7 +178,15 @@ 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'); + my $defaultcredits = $domdefcredits; + if ($enrollvar{$crs}{defaultcredits}) { + $defaultcredits = $enrollvar{$crs}{defaultcredits}; + } + my $failsafe = $autofailsafe; + if ($enrollvar{$crs}{'autodropfailsafe'}) { + $failsafe = $enrollvar{$crs}{'autodropfailsafe'}; + } + 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},$showcredits,$defaultcredits,$failsafe,$affiliates{$crs},$LC_code{$crs},\$logmsg,\$newusermsg,'automated'); print $fh &mt('Messages start for [_1]',$crs)."\n"; print $fh "$logmsg\n"; print $fh &mt('Messages end for [_1]',$crs)."\n"; @@ -167,13 +204,14 @@ $ownerdom = $dom; } } - if (($settings->{'sender_uname'} ne '') && - ($settings->{'sender_domain'} ne '')) { - $env{'user.name'} = $settings->{'sender_uname'}; - $env{'user.domain'} = $settings->{'sender_domain'}; - } else { - $env{'user.name'} = $ownername; - $env{'user.domain'} = $ownerdom; + $env{'user.name'} = $ownername; + $env{'user.domain'} = $ownerdom; + if (ref($domsettings) eq 'HASH') { + if (($domsettings->{'sender_uname'} ne '') && + ($domsettings->{'sender_domain'} ne '')) { + $env{'user.name'} = $domsettings->{'sender_uname'}; + $env{'user.domain'} = $domsettings->{'sender_domain'}; + } } if ($enrollvar{$crs}{notifylist} eq '') { if ($newusermsg ne '') { @@ -184,6 +222,7 @@ } } } else { + $env{'form.can_reply'} = 'N'; my $msgcc; # Send message about enrollment changes to notifylist. $env{'user.home'} = &Apache::lonnet::homeserver($env{'user.name'},$env{'user.domain'}); @@ -223,6 +262,7 @@ my $status = &Apache::lonmsg::user_normal_msg($ccname,$ccdom,$subject,$message,undef,undef,undef,undef,\%sentmessage,undef,undef,undef,1,$recipid); } + delete($env{'form.can_reply'}); } delete($env{'user.name'}); delete($env{'user.home'}); @@ -236,7 +276,7 @@ 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"; } else { - print $fh ('Not within time window for auto-enrollment in [_1]',$crs)."\n"; + print $fh &mt('Not within time window for auto-enrollment in [_1]',$crs)."\n"; } } else { print $fh &mt('Auto-enrollment not currently enabled for [_1]',$crs)."\n";