Annotation of modules/gci/Autoreport.pl, revision 1.2

1.1       gci         1: #!/usr/bin/perl
                      2: #
                      3: # Automated Report Availability 
                      4: #
1.2     ! gci         5: # $Id: Autoreport.pl,v 1.1 2010/01/11 02:58:15 gci Exp $
1.1       gci         6: # Run as www. Call this from an entry in /etc/cron.d/loncapa
                      7: #
                      8: # www /home/httpd/perl/Autoreport.pl
                      9: #
                     10: 
                     11:     use strict;
                     12:     use lib '/home/httpd/lib/perl';
                     13:     use Apache::lonnet;
                     14:     use Apache::loncommon;
                     15:     use Apache::lonmsg;
                     16:     use Apache::lonlocal;
                     17:     use LONCAPA::Configuration;
                     18:     use LONCAPA qw(:DEFAULT :match);
                     19: 
                     20: # Determine the library server's domain and hostID
                     21:     my $perlvarref = &LONCAPA::Configuration::read_conf('loncapa.conf');
                     22:     my $logfile = $$perlvarref{'lonDaemons'}.'/logs/autoreport.log';
                     23:     my @hostids = &Apache::lonnet::current_machine_ids();
                     24: 
                     25: # Determine the present time;
                     26:     my $now = time();
                     27:     my $output;
                     28:     $env{'user.domain'} = 'gci';
                     29:     # Initialize language handler
                     30:     &Apache::lonlocal::get_language_handle();
                     31:     # Determine the courses
                     32:     my %courses = 
                     33:         &Apache::lonnet::courseiddump('gcitest','.',1,'.','.','.',1,\@hostids,'Course');
                     34:     foreach my $key (sort(keys(%courses))) {
                     35:         my %args = (
                     36:                      one_time => 1,
                     37:                    );
                     38: # Get course settings
                     39:         my %settings = &Apache::lonnet::coursedescription($key,\%args);
                     40:         my $cnum = $settings{'num'};
                     41:         next if ($settings{'domain'} ne 'gcitest');
                     42:         next if ($settings{'internal.autoreportmailsent'});
                     43:         my $courseopt=&Apache::lonnet::get_courseresdata($cnum,'gcitest');
                     44:         my $duedate = $courseopt->{$key.'.0.duedate'};
                     45:         my %courseenv;
                     46:         if ($duedate < $now) {
                     47:             my $numsent = &send_message($settings{'internal.courseowner'},
                     48:                                      $settings{'description'},$cnum,'gcitest');
                     49:             $output .= &mt('[quant,_1,message] sent with statistics link for concept test: [_2] owned by [_3].',$numsent,$settings{'description'}.' ('.$cnum.':gcitest)',$settings{'internal.courseowner'})."\n";
                     50:             $courseenv{'internal.autoreportmailsent'} = 1;
                     51:             my $putresult = &Apache::lonnet::put('environment',\%courseenv,'gcitest',$cnum);
                     52:             if ($putresult eq 'ok') {
                     53:                 $output .= &mt('autoreportmailsent item set to [_1] for [_2].',$courseenv{'internal.autoreportmailsent'},$settings{'description'}.' ('.$cnum.':gcitest)')."\n\n";
                     54:             } else {
                     55:                 $output .= &mt('An error - [_1] occurred storing autoreportmailsent in the course environment for [_2].',$putresult,$settings{'description'}.' ('.$cnum.':gcitest)')."\n\n";
                     56:             }
                     57:         }
                     58:     }
                     59:     delete($env{'user.domain'});
                     60:     if ($output) {
                     61:         if (open (my $fh,">>$logfile")) {
                     62:             print $fh "********************\n".localtime(time)." Autoreport messages start --\n".$output."-- ".localtime(time).' '.&mt('Autoreport messages end')."\n*******************\n\n";
                     63:             close($fh);
                     64:         }
                     65:     }
                     66: 
                     67: sub send_message {
                     68:     my ($owner,$cdesc,$cnum,$dom) = @_;
                     69:     my ($ownername,$ownerdom) = split(':',$owner);
                     70:     my $numsent = 0;
                     71:     if (($ownername =~ /^$match_username$/) &&
                     72:         ($ownerdom =~ /^$match_domain$/)) {
                     73:         $env{'form.can_reply'} = 'N';
                     74:         $env{'user.name'} = $ownername;
                     75:         $env{'user.domain'} = $ownerdom;
                     76:         $env{'user.home'} = &Apache::lonnet::homeserver($env{'user.name'},$env{'user.domain'});
                     77:         $env{'request.course.id'} = $dom.'_'.$cnum;
                     78:         my $subject = &mt('GCI Concept Test Report in [_1]',$cdesc);
                     79:         my $message = &mt('Aggregated test performance data are available from the Overall Statistics Page of your GCI Concept Test: [_1]:',$cdesc).'<br /><br />'."\n\n".
                     80:                       &mt('The following link will take you to the statistics page for the Concept Test: ').'<br /><br />'."\n\n".
1.2     ! gci        81:                       'http://gci.lite.msu.edu/adm/login?username='.$ownername.'&symb=%2fadm%2fstatistics&role=cc.%2f'.$dom.'%2f'.$cnum."\n\n";
1.1       gci        82:         my @recusers;
                     83:         my @recudoms;
                     84:         my %cc_hash = &Apache::lonnet::get_my_roles($cnum,$dom,'','',['cc']);
                     85:         my @to_notify;
                     86:         my $msgcc = {};
                     87:         foreach my $key (keys(%cc_hash)) {
                     88:             if ($key =~ /^($match_username:$match_domain):cc$/) {
                     89:                 my $cc = $1;
                     90:                 unless (grep(/^\Q$cc\E/,@to_notify)) {
                     91:                     push(@to_notify,$cc);
                     92:                 }
                     93:             }
                     94:         }
                     95:         if (@to_notify) {
                     96:             foreach my $cc (@to_notify) {
                     97:                 my ($ccname,$ccdom) = split(/:/,$cc);
                     98:                 unless((grep(/^\Q$ccname\E$/,@recusers)) && (grep(/^\Q$ccdom\E$/,@recudoms))) {
                     99:                     push(@recusers,$ccname);
                    100:                     push(@recudoms,$ccdom);
                    101:                     $msgcc->{$ccname.':'.$ccdom}='';
                    102:                     $numsent ++;
                    103:                 }
                    104:             }
                    105:         }
                    106:         if ($numsent) {
                    107:             my %reciphash = (
                    108:                              cc => $msgcc,
                    109:                             );
                    110:             my %sentmessage;
                    111:             my $stamp = time;
                    112:             my $msgcount = &Apache::lonmsg::get_uniq();
                    113:             &Apache::lonmsg::process_sent_mail($subject,'',$numsent,$stamp,$env{'user.name'},$env{'user.domain'},$msgcount,$cnum,$$,$message,\@recusers,\@recudoms);
                    114:             my ($recipid,$recipstatus) =
                    115:                 &Apache::lonmsg::store_recipients($subject,
                    116:                     $env{'user.name'},$env{'user.domain'},\%reciphash);
                    117:             foreach my $recip (sort(keys(%{$msgcc}))) {
                    118:                 my ($ccname,$ccdom) = split(/:/,$recip);
                    119:                 my $status =
                    120:                     &Apache::lonmsg::user_normal_msg($ccname,$ccdom,$subject,$message,undef,undef,undef,1,\%sentmessage,undef,undef,undef,1,$recipid);
                    121:             }
                    122:             delete($env{'form.can_reply'});
                    123:         }
                    124:         delete($env{'user.name'});
                    125:         delete($env{'user.home'});
                    126:         delete($env{'request.course.id'});
                    127:     }
                    128:     return $numsent;
                    129: }
                    130: 

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