--- loncom/enrollment/Autoenroll.pl 2005/04/12 00:20:01 1.14 +++ loncom/enrollment/Autoenroll.pl 2007/12/05 02:04:02 1.28 @@ -1,7 +1,7 @@ #!/usr/bin/perl # #Automated Enrollment script -# $Id: Autoenroll.pl,v 1.14 2005/04/12 00:20:01 raeburn Exp $ +# $Id: Autoenroll.pl,v 1.28 2007/12/05 02:04:02 raeburn Exp $ # # Copyright Michigan State University Board of Trustees # @@ -33,7 +33,11 @@ use Apache::lonnet; use Apache::loncoursedata; use Apache::lonmsg; + use Apache::longroup; + use Apache::loncommon; + use Apache::lonlocal; use HTML::Entities; + use LONCAPA qw(:match); # Determine the library server's domain and hostID my $perlvarref = LONCAPA::Configuration::read_conf('loncapa.conf'); @@ -46,21 +50,37 @@ # For each domain ...... foreach my $dom (@domains) { - #only run if configured to - if (! &localenroll::run($dom)) { next; } + #only run if configured to + my $run_enroll = 0; + my $settings; + my %domconfig = + &Apache::lonnet::get_dom('configuration',['autoenroll'],$dom); + if (ref($domconfig{'autoenroll'}) eq 'HASH') { + $settings = $domconfig{'autoenroll'}; + if ($settings->{'run'} eq '1') { + $run_enroll = 1; + } + } else { + $run_enroll = &localenroll::run($dom); + } + next if ($run_enroll != 1); + $env{'user.domain'} = $dom; - # Determine the courses - my %courses = &Apache::lonnet::courseiddump($dom,'.',1,'.','.','.',1,\@hostids); + # Initialize language handler + &Apache::lonlocal::get_language_handle(); + # Determine the courses + my %courses = &Apache::lonnet::courseiddump($dom,'.',1,'.','.','.',1,\@hostids,'Course'); my %affiliates = (); my %enrollvar = (); my %reply = (); my %LC_code = (); foreach my $key (sort keys %courses) { - my $crs; - if ($key =~ m/^($dom)_(\w+)$/) { - $crs = $2; - } - + 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); %{$enrollvar{$crs}} = (); @@ -105,7 +125,7 @@ } } foreach my $xlist (@crosslistings) { - if ($xlist =~ m/^(\w+):(\w*)$/) { + if ($xlist =~ m/^([^:]+):(\w*)$/) { my $course_id = $1; my $gp = $2; if (!grep/^$course_id$/,@{$affiliates{$crs}}) { @@ -121,8 +141,8 @@ # Now go through classes and perform required enrollment changes. open (my $fh,">>$logfile"); - print $fh "********************\n".localtime(time)." Enrollment messages start --\n"; - print $fh "Response from fetch_enrollment_query was $outcome\n"; + print $fh "********************\n".localtime(time).' '.&mt('Enrollment messages start').' --'."\n"; + print $fh &mt("Response from [_1] was [_2]",'fetch_enrollment_query',$outcome)."\n"; foreach my $crs (sort keys %enrollvar) { my $logmsg = ''; my $newusermsg = ''; @@ -130,59 +150,102 @@ 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 &mt('Messages start for [_1]',$crs)."\n"; print $fh "$logmsg\n"; - print $fh "Messages end for $crs\n"; + print $fh &mt('Messages end for [_1]',$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}; - } - foreach my $cc (@to_notify) { - my ($ccname,$ccdom) = split/@/,$cc; - my $status = &Apache::lonmsg::user_normal_msg($ccname,$ccdom,$subject,$message); +# Set $env{'user.name'}, $env{'user.domain'}, $env{'user.home'} +# and $env{'request.course.id'} for use by logging in lonmsg + $env{'request.course.id'} = $dom.'_'.$crs; + my ($ownername,$ownerdom); + if ($enrollvar{$crs}{'courseowner'} ne '') { + if ($enrollvar{$crs}{'courseowner'} =~ /:/) { + ($ownername,$ownerdom) = + split(/:/,$enrollvar{$crs}{'courseowner'}); + } else { + $ownername = $enrollvar{$crs}{'courseowner'}; + $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; + } + if ($enrollvar{$crs}{notifylist} eq '') { + if ($newusermsg ne '') { + if (($ownername =~ /^$match_username$/) && + ($ownerdom =~ /^$match_domain$/)) { + my $subject = &mt('New user accounts in [_1]',$enrollvar{$crs}{'coursecode'}); + my $status = &Apache::lonmsg::user_normal_msg($ownername,$ownerdom,$subject,$newusermsg); } - 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); + } + } else { + my $msgcc; +# Send message about enrollment changes to notifylist. + $env{'user.home'} = &Apache::lonnet::homeserver($env{'user.name'},$env{'user.domain'}); + my $subject = &mt('Student enrollment changes in [_1]',$enrollvar{$crs}{coursecode}); + my $message = &mt('The following [quant,_1,change] occurred in [_2] - [_3] as a result of the automated classlist update:',$changecount,$enrollvar{$crs}{description},$enrollvar{$crs}{coursecode})."\n\n".$response; + unless ($newusermsg eq '') { + $message .= "\n".$newusermsg; + } + my @to_notify = split(/,/,$enrollvar{$crs}{notifylist}); + my $numsent = 0; + my @recusers; + my @recudoms; + foreach my $cc (@to_notify) { + my ($ccname,$ccdom); + if ($cc =~ /:/) { + ($ccname,$ccdom) = split(/:/,$cc); + } elsif ($cc =~ /\@/) { + ($ccname,$ccdom) = split(/\@/,$cc); } - delete($env{'user.name'}); - delete($env{'user.home'}); + push(@recusers,$ccname); + push(@recudoms,$ccdom); + $msgcc->{$ccname.':'.$ccdom}=''; + $numsent ++; + } + my %reciphash = ( + cc => $msgcc, + ); + my %sentmessage; + my $stamp = time; + my $msgcount = &Apache::lonmsg::get_uniq(); + &Apache::lonmsg::process_sent_mail($subject,'',$numsent,$stamp,$env{'user.name'},$env{'user.domain'},$msgcount,$crs,$$,$message,\@recusers,\@recudoms); + my ($recipid,$recipstatus) = + &Apache::lonmsg::store_recipients($subject, + $env{'user.name'},$env{'user.domain'},\%reciphash); + foreach my $recip (sort(keys(%{$msgcc}))) { + my ($ccname,$ccdom) = split(/:/,$recip); + my $status = + &Apache::lonmsg::user_normal_msg($ccname,$ccdom,$subject,$message,undef,undef,undef,undef,\%sentmessage,undef,undef,undef,1,$recipid); } } + delete($env{'user.name'}); + delete($env{'user.home'}); + delete($env{'request.course.id'}); + $env{'user.domain'} = $dom; } } } } 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"; + 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 $crs\n"; + print $fh ('Not within time window for auto-enrollment in [_1]',$crs)."\n"; } } else { - print $fh "Auto-enrollment not currently enabled for $crs\n"; + print $fh &mt('Auto-enrollment not currently enabled for [_1]',$crs)."\n"; } } } - print $fh "-- ".localtime(time)." Enrollment messages end\n*******************\n\n"; + print $fh "-- ".localtime(time).' '.&mt('Enrollment messages end')."\n*******************\n\n"; close($fh); delete($env{'user.domain'}); -# Check for photos } 1;