Annotation of loncom/interface/lonmsg.pm, revision 1.113

1.1       www         1: # The LearningOnline Network with CAPA
1.26      albertel    2: # Routines for messaging
                      3: #
1.113   ! www         4: # $Id: lonmsg.pm,v 1.112 2004/11/09 16:29:32 www Exp $
1.26      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/
1.1       www        27: #
1.75      www        28: 
                     29: 
1.1       www        30: package Apache::lonmsg;
                     31: 
1.58      bowersj2   32: =pod
                     33: 
                     34: =head1 NAME
                     35: 
                     36: Apache::lonmsg: supports internal messaging
                     37: 
                     38: =head1 SYNOPSIS
                     39: 
                     40: lonmsg provides routines for sending messages, receiving messages, and
                     41: a handler to allow users to read, send, and delete messages.
                     42: 
                     43: =head1 OVERVIEW
                     44: 
                     45: =head2 Messaging Overview
                     46: 
                     47: X<messages>LON-CAPA provides an internal messaging system similar to
                     48: email, but customized for LON-CAPA's usage. LON-CAPA implements its
                     49: own messaging system, rather then building on top of email, because of
                     50: the features LON-CAPA messages can offer that conventional e-mail can
                     51: not:
                     52: 
                     53: =over 4
                     54: 
                     55: =item * B<Critical messages>: A message the recipient B<must>
                     56: acknowlegde receipt of before they are allowed to continue using the
                     57: system, preventing a user from claiming they never got a message
                     58: 
                     59: =item * B<Receipts>: LON-CAPA can reliably send reciepts informing the
                     60: sender that it has been read; again, useful for preventing students
                     61: from claiming they did not see a message. (While conventional e-mail
                     62: has some reciept support, it's sporadic, e-mail client-specific, and
                     63: generally the receiver can opt to not send one, making it useless in
                     64: this case.)
                     65: 
                     66: =item * B<Context>: LON-CAPA knows about the sender, such as where
                     67: they are in a course. When a student mails an instructor asking for
                     68: help on the problem, the instructor receives not just the student's
                     69: question, but all submissions the student has made up to that point,
                     70: the user's rendering of the problem, and the complete view the student
                     71: saw of the resource, including discussion up to that point. Finally,
                     72: the instructor is reading all of this inside of LON-CAPA, not their
                     73: email program, so they have full access to LON-CAPA's grading
                     74: interface, or other features they may wish to use in response to the
                     75: student's query.
                     76: 
1.101     raeburn    77: =item * B<Blocking>: LON-CAPA can block display of e-mails that are 
                     78: sent to a student during an online exam. A course coordinator or
                     79: instructor can set an open and close date/time for scheduled online
                     80: exams in a course. If a user uses the LON-CAPA internal messaging 
                     81: system to display e-mails during the scheduled blocking event,  
                     82: display of all e-mail sent during the blocking period will be 
                     83: suppressed, and a message of explanation, including details of the 
                     84: currently active blocking periods will be displayed instead. A user 
                     85: who has a course coordinator or instructor role in a course will be
                     86: unaffected by any blocking periods for the course, unless the user
                     87: also has a student role in the course, AND has selected the student role.
                     88: 
1.58      bowersj2   89: =back
                     90: 
                     91: Users can ask LON-CAPA to forward messages to conventional e-mail
                     92: addresses on their B<PREF> screen, but generally, LON-CAPA messages
                     93: are much more useful then traditional email can be made to be, even
                     94: with HTML support.
                     95: 
                     96: Right now, this document will cover just how to send a message, since
                     97: it is likely you will not need to programmatically read messages,
                     98: since lonmsg already implements that functionality.
                     99: 
                    100: =head1 FUNCTIONS
                    101: 
                    102: =over 4
                    103: 
                    104: =cut
                    105: 
1.1       www       106: use strict;
                    107: use Apache::lonnet();
1.2       www       108: use vars qw($msgcount);
1.47      albertel  109: use HTML::TokeParser();
1.5       www       110: use Apache::Constants qw(:common);
1.47      albertel  111: use Apache::loncommon();
                    112: use Apache::lontexconvert();
                    113: use HTML::Entities();
1.53      www       114: use Mail::Send;
1.67      www       115: use Apache::lonlocal;
1.95      www       116: use Apache::loncommunicate;
1.1       www       117: 
1.65      www       118: # Querystring component with sorting type
                    119: my $sqs;
1.108     www       120: my $startdis;
                    121: my $interdis;
1.65      www       122: 
1.1       www       123: # ===================================================================== Package
                    124: 
1.3       www       125: sub packagemsg {
1.108     www       126:     my ($subject,$message,$citation,$baseurl,$attachmenturl,
                    127: 	$recuser,$recdomain)=@_;
1.96      albertel  128:     $message =&HTML::Entities::encode($message,'<>&"');
                    129:     $citation=&HTML::Entities::encode($citation,'<>&"');
                    130:     $subject =&HTML::Entities::encode($subject,'<>&"');
1.49      albertel  131:     #remove machine specification
                    132:     $baseurl =~ s|^http://[^/]+/|/|;
1.96      albertel  133:     $baseurl =&HTML::Entities::encode($baseurl,'<>&"');
1.51      www       134:     #remove machine specification
                    135:     $attachmenturl =~ s|^http://[^/]+/|/|;
1.96      albertel  136:     $attachmenturl =&HTML::Entities::encode($attachmenturl,'<>&"');
1.51      www       137: 
1.2       www       138:     my $now=time;
                    139:     $msgcount++;
1.6       www       140:     my $partsubj=$subject;
                    141:     $partsubj=&Apache::lonnet::escape($partsubj);
                    142:     my $msgid=&Apache::lonnet::escape(
                    143:            $now.':'.$partsubj.':'.$ENV{'user.name'}.':'.
                    144:            $ENV{'user.domain'}.':'.$msgcount.':'.$$);
1.49      albertel  145:     my $result='<sendername>'.$ENV{'user.name'}.'</sendername>'.
1.1       www       146:            '<senderdomain>'.$ENV{'user.domain'}.'</senderdomain>'.
                    147:            '<subject>'.$subject.'</subject>'.
1.67      www       148: 	   '<time>'.&Apache::lonlocal::locallocaltime($now).'</time>'.
1.1       www       149: 	   '<servername>'.$ENV{'SERVER_NAME'}.'</servername>'.
                    150:            '<host>'.$ENV{'HTTP_HOST'}.'</host>'.
                    151: 	   '<client>'.$ENV{'REMOTE_ADDR'}.'</client>'.
                    152: 	   '<browsertype>'.$ENV{'browser.type'}.'</browsertype>'.
                    153: 	   '<browseros>'.$ENV{'browser.os'}.'</browseros>'.
                    154: 	   '<browserversion>'.$ENV{'browser.version'}.'</browserversion>'.
                    155:            '<browsermathml>'.$ENV{'browser.mathml'}.'</browsermathml>'.
                    156: 	   '<browserraw>'.$ENV{'HTTP_USER_AGENT'}.'</browserraw>'.
                    157: 	   '<courseid>'.$ENV{'request.course.id'}.'</courseid>'.
1.85      www       158: 	   '<coursesec>'.$ENV{'request.course.sec'}.'</coursesec>'.
1.1       www       159: 	   '<role>'.$ENV{'request.role'}.'</role>'.
                    160: 	   '<resource>'.$ENV{'request.filename'}.'</resource>'.
1.2       www       161:            '<msgid>'.$msgid.'</msgid>'.
1.108     www       162: 	   '<recuser>'.$recuser.'</recuser>'.
                    163: 	   '<recdomain>'.$recdomain.'</recdomain>'.
1.49      albertel  164: 	   '<message>'.$message.'</message>';
                    165:     if (defined($citation)) {
                    166: 	$result.='<citation>'.$citation.'</citation>';
                    167:     }
                    168:     if (defined($baseurl)) {
                    169: 	$result.= '<baseurl>'.$baseurl.'</baseurl>';
                    170:     }
1.51      www       171:     if (defined($attachmenturl)) {
1.52      www       172: 	$result.= '<attachmenturl>'.$attachmenturl.'</attachmenturl>';
1.51      www       173:     }
1.49      albertel  174:     return $msgid,$result;
1.1       www       175: }
                    176: 
1.2       www       177: # ================================================== Unpack message into a hash
                    178: 
1.3       www       179: sub unpackagemsg {
1.52      www       180:     my ($message,$notoken)=@_;
1.2       www       181:     my %content=();
                    182:     my $parser=HTML::TokeParser->new(\$message);
                    183:     my $token;
                    184:     while ($token=$parser->get_token) {
                    185:        if ($token->[0] eq 'S') {
                    186: 	   my $entry=$token->[1];
                    187:            my $value=$parser->get_text('/'.$entry);
                    188:            $content{$entry}=$value;
                    189:        }
                    190:     }
1.52      www       191:     if ($content{'attachmenturl'}) {
1.100     albertel  192:        my ($fname)=($content{'attachmenturl'}=~m|/([^/]+)$|);
1.52      www       193:        if ($notoken) {
1.100     albertel  194: 	   $content{'message'}.='<p>'.&mt('Attachment').': <tt>'.$fname.'</tt>';
1.52      www       195:        } else {
1.99      albertel  196: 	   &Apache::lonnet::allowuploaded('/adm/msg',
                    197: 					  $content{'attachmenturl'});
                    198: 	   $content{'message'}.='<p>'.&mt('Attachment').
                    199: 	       ': <a href="'.$content{'attachmenturl'}.'"><tt>'.
1.100     albertel  200: 	       $fname.'</tt></a>';
1.52      www       201:        }
                    202:     }
1.2       www       203:     return %content;
                    204: }
                    205: 
1.6       www       206: # ======================================================= Get info out of msgid
                    207: 
                    208: sub unpackmsgid {
1.106     www       209:     my ($msgid,$folder)=@_;
                    210:     $msgid=&Apache::lonnet::unescape($msgid);
                    211:     my $suffix=&foldersuffix($folder);
1.6       www       212:     my ($sendtime,$shortsubj,$fromname,$fromdomain)=split(/\:/,
1.7       www       213:                           &Apache::lonnet::unescape($msgid));
1.106     www       214:     my %status=&Apache::lonnet::get('email_status'.$suffix,[$msgid]);
1.6       www       215:     if ($status{$msgid}=~/^error\:/) { $status{$msgid}=''; }
                    216:     unless ($status{$msgid}) { $status{$msgid}='new'; }
                    217:     return ($sendtime,$shortsubj,$fromname,$fromdomain,$status{$msgid});
                    218: } 
                    219: 
1.53      www       220: 
                    221: sub sendemail {
                    222:     my ($to,$subject,$body)=@_;
                    223:     $body=
1.67      www       224:     "*** ".&mt('This is an automatic message generated by the LON-CAPA system.')."\n".
                    225:     "*** ".&mt('Please do not reply to this address.')."\n\n".$body;
1.53      www       226:     my $msg = new Mail::Send;
                    227:     $msg->to($to);
                    228:     $msg->subject('[LON-CAPA] '.$subject);
1.103     albertel  229:     my %oldENV=%ENV;
                    230:     undef(%ENV);
1.97      matthew   231:     if (my $fh = $msg->open()) {
1.68      www       232: 	print $fh $body;
                    233: 	$fh->close;
                    234:     }
1.103     albertel  235:     %ENV=%oldENV;
                    236:     undef(%oldENV);
1.53      www       237: }
                    238: 
                    239: # ==================================================== Send notification emails
                    240: 
                    241: sub sendnotification {
                    242:     my ($to,$touname,$toudom,$subj,$crit)=@_;
                    243:     my $sender=$ENV{'environment.firstname'}.' '.$ENV{'environment.lastname'};
                    244:     my $critical=($crit?' critical':'');
                    245:     my $url='http://'.
                    246:       $Apache::lonnet::hostname{&Apache::lonnet::homeserver($touname,$toudom)}.
1.54      www       247:       '/adm/email?username='.$touname.'&domain='.$toudom;
1.53      www       248:     my $body=(<<ENDMSG);
                    249: You received a$critical message from $sender in LON-CAPA. The subject is
                    250: 
                    251:  $subj
                    252: 
                    253: Use
                    254: 
                    255:  $url
                    256: 
                    257: to access this message.
                    258: ENDMSG
                    259:     &sendemail($to,'New'.$critical.' message from '.$sender,$body);
                    260: }
1.40      www       261: # ============================================================= Check for email
                    262: 
                    263: sub newmail {
                    264:     if ((time-$ENV{'user.mailcheck.time'})>300) {
                    265:         my %what=&Apache::lonnet::get('email_status',['recnewemail']);
                    266:         &Apache::lonnet::appenv('user.mailcheck.time'=>time);
                    267:         if ($what{'recnewemail'}>0) { return 1; }
                    268:     }
                    269:     return 0;
                    270: }
                    271: 
1.1       www       272: # =============================== Automated message to the author of a resource
                    273: 
1.58      bowersj2  274: =pod
                    275: 
                    276: =item * B<author_res_msg($filename, $message)>: Sends message $message to the owner
                    277:     of the resource with the URI $filename.
                    278: 
                    279: =cut
                    280: 
1.1       www       281: sub author_res_msg {
                    282:     my ($filename,$message)=@_;
1.2       www       283:     unless ($message) { return 'empty'; }
1.1       www       284:     $filename=&Apache::lonnet::declutter($filename);
1.72      www       285:     my ($domain,$author,@dummy)=split(/\//,$filename);
1.1       www       286:     my $homeserver=&Apache::lonnet::homeserver($author,$domain);
                    287:     if ($homeserver ne 'no_host') {
                    288:        my $id=unpack("%32C*",$message);
1.2       www       289:        my $msgid;
1.72      www       290:        ($msgid,$message)=&packagemsg($filename,$message);
1.3       www       291:        return &Apache::lonnet::reply('put:'.$domain.':'.$author.
1.72      www       292:          ':nohist_res_msgs:'.
                    293:           &Apache::lonnet::escape($filename.'_'.$id).'='.
                    294:           &Apache::lonnet::escape($message),$homeserver);
1.1       www       295:     }
1.2       www       296:     return 'no_host';
1.73      www       297: }
                    298: 
                    299: # =========================================== Retrieve author resource messages
                    300: 
                    301: sub retrieve_author_res_msg {
1.75      www       302:     my $url=shift;
1.73      www       303:     $url=&Apache::lonnet::declutter($url);
1.80      www       304:     my ($domain,$author)=($url=~/^(\w+)\/(\w+)\//);
1.76      www       305:     my %errormsgs=&Apache::lonnet::dump('nohist_res_msgs',$domain,$author);
1.73      www       306:     my $msgs='';
                    307:     foreach (keys %errormsgs) {
1.80      www       308: 	if ($_=~/^\Q$url\E\_\d+$/) {
1.73      www       309: 	    my %content=&unpackagemsg($errormsgs{$_});
1.74      www       310: 	    $msgs.='<p><img src="/adm/lonMisc/bomb.gif" /><b>'.
                    311: 		$content{'time'}.'</b>: '.$content{'message'}.
                    312: 		'<br /></p>';
1.73      www       313: 	}
                    314:     } 
                    315:     return $msgs;     
                    316: }
                    317: 
                    318: 
                    319: # =============================== Delete all author messages related to one URL
                    320: 
                    321: sub del_url_author_res_msg {
1.75      www       322:     my $url=shift;
1.73      www       323:     $url=&Apache::lonnet::declutter($url);
1.77      www       324:     my ($domain,$author)=($url=~/^(\w+)\/(\w+)\//);
                    325:     my @delmsgs=();
                    326:     foreach (&Apache::lonnet::getkeys('nohist_res_msgs',$domain,$author)) {
                    327: 	if ($_=~/^\Q$url\E\_\d+$/) {
                    328: 	    push (@delmsgs,$_);
                    329: 	}
                    330:     }
                    331:     return &Apache::lonnet::del('nohist_res_msgs',\@delmsgs,$domain,$author);
1.73      www       332: }
                    333: 
                    334: # ================= Return hash with URLs for which there is a resource message
                    335: 
                    336: sub all_url_author_res_msg {
                    337:     my ($author,$domain)=@_;
1.75      www       338:     my %returnhash=();
1.76      www       339:     foreach (&Apache::lonnet::getkeys('nohist_res_msgs',$domain,$author)) {
1.75      www       340: 	$_=~/^(.+)\_\d+/;
                    341: 	$returnhash{$1}=1;
                    342:     }
                    343:     return %returnhash;
1.1       www       344: }
                    345: 
                    346: # ================================================== Critical message to a user
                    347: 
1.38      www       348: sub user_crit_msg_raw {
1.24      www       349:     my ($user,$domain,$subject,$message,$sendback)=@_;
1.2       www       350: # Check if allowed missing
                    351:     my $status='';
                    352:     my $msgid='undefined';
                    353:     unless (($message)&&($user)&&($domain)) { $status='empty'; };
                    354:     my $homeserver=&Apache::lonnet::homeserver($user,$domain);
                    355:     if ($homeserver ne 'no_host') {
1.3       www       356:        ($msgid,$message)=&packagemsg($subject,$message);
1.24      www       357:        if ($sendback) { $message.='<sendback>true</sendback>'; }
1.4       www       358:        $status=&Apache::lonnet::critical(
                    359:            'put:'.$domain.':'.$user.':critical:'.
                    360:            &Apache::lonnet::escape($msgid).'='.
                    361:            &Apache::lonnet::escape($message),$homeserver);
1.45      www       362:        if ($ENV{'request.course.id'}) {
                    363:           &user_normal_msg_raw(
                    364:             $ENV{'course.'.$ENV{'request.course.id'}.'.num'},
                    365:             $ENV{'course.'.$ENV{'request.course.id'}.'.domain'},
                    366:             'Critical ['.$user.':'.$domain.']',
                    367: 	    $message);
                    368:        }
1.2       www       369:     } else {
                    370:        $status='no_host';
                    371:     }
1.53      www       372: # Notifications
                    373:     my %userenv = &Apache::lonnet::get('environment',['critnotification'],
                    374:                                        $domain,$user);
                    375:     if ($userenv{'critnotification'}) {
                    376:       &sendnotification($userenv{'critnotification'},$user,$domain,$subject,1);
                    377:     }
                    378: # Log this
1.2       www       379:     &Apache::lonnet::logthis(
1.4       www       380:       'Sending critical email '.$msgid.
1.2       www       381:       ', log status: '.
                    382:       &Apache::lonnet::log($ENV{'user.domain'},$ENV{'user.name'},
                    383:                          $ENV{'user.home'},
                    384:       'Sending critical '.$msgid.' to '.$user.' at '.$domain.' with status: '
1.4       www       385:       .$status));
1.2       www       386:     return $status;
                    387: }
                    388: 
1.38      www       389: # New routine that respects "forward" and calls old routine
                    390: 
1.58      bowersj2  391: =pod
                    392: 
                    393: =item * B<user_crit_msg($user, $domain, $subject, $message, $sendback)>: Sends
                    394:     a critical message $message to the $user at $domain. If $sendback is true,
                    395:     a reciept will be sent to the current user when $user recieves the message.
                    396: 
                    397: =cut
                    398: 
1.38      www       399: sub user_crit_msg {
                    400:     my ($user,$domain,$subject,$message,$sendback)=@_;
                    401:     my $status='';
                    402:     my %userenv = &Apache::lonnet::get('environment',['msgforward'],
                    403:                                        $domain,$user);
                    404:     my $msgforward=$userenv{'msgforward'};
                    405:     if ($msgforward) {
                    406:        foreach (split(/\,/,$msgforward)) {
                    407: 	 my ($forwuser,$forwdomain)=split(/\:/,$_);
                    408:          $status.=
                    409: 	   &user_crit_msg_raw($forwuser,$forwdomain,$subject,$message,
                    410:                 $sendback).' ';
                    411:        }
                    412:     } else { 
                    413: 	$status=&user_crit_msg_raw($user,$domain,$subject,$message,$sendback);
                    414:     }
                    415:     return $status;
                    416: }
                    417: 
1.2       www       418: # =================================================== Critical message received
                    419: 
                    420: sub user_crit_received {
1.12      www       421:     my $msgid=shift;
                    422:     my %message=&Apache::lonnet::get('critical',[$msgid]);
1.52      www       423:     my %contents=&unpackagemsg($message{$msgid},1);
1.24      www       424:     my $status='rec: '.($contents{'sendback'}?
1.5       www       425:      &user_normal_msg($contents{'sendername'},$contents{'senderdomain'},
1.82      www       426:                      &mt('Receipt').': '.$ENV{'user.name'}.' '.&mt('at').' '.$ENV{'user.domain'}.', '.$contents{'subject'},
1.67      www       427:                      &mt('User').' '.$ENV{'user.name'}.' '.&mt('at').' '.$ENV{'user.domain'}.
1.42      www       428:                      ' acknowledged receipt of message'."\n".'   "'.
1.67      www       429:                      $contents{'subject'}.'"'."\n".&mt('dated').' '.
1.42      www       430:                      $contents{'time'}.".\n"
                    431:                      ):'no msg req');
1.5       www       432:     $status.=' trans: '.
1.12      www       433:      &Apache::lonnet::put(
                    434:      'nohist_email',{$contents{'msgid'} => $message{$msgid}});
1.5       www       435:     $status.=' del: '.
1.9       albertel  436:      &Apache::lonnet::del('critical',[$contents{'msgid'}]);
1.5       www       437:     &Apache::lonnet::log($ENV{'user.domain'},$ENV{'user.name'},
                    438:                          $ENV{'user.home'},'Received critical message '.
                    439:                          $contents{'msgid'}.
                    440:                          ', '.$status);
1.12      www       441:     return $status;
1.2       www       442: }
                    443: 
                    444: # ======================================================== Normal communication
                    445: 
1.38      www       446: sub user_normal_msg_raw {
1.51      www       447:     my ($user,$domain,$subject,$message,$citation,$baseurl,$attachmenturl)=@_;
1.2       www       448: # Check if allowed missing
                    449:     my $status='';
                    450:     my $msgid='undefined';
                    451:     unless (($message)&&($user)&&($domain)) { $status='empty'; };
                    452:     my $homeserver=&Apache::lonnet::homeserver($user,$domain);
                    453:     if ($homeserver ne 'no_host') {
1.51      www       454:        ($msgid,$message)=&packagemsg($subject,$message,$citation,$baseurl,
1.108     www       455:                                      $attachmenturl,$user,$domain);
                    456: # Store in user folder
1.4       www       457:        $status=&Apache::lonnet::critical(
                    458:            'put:'.$domain.':'.$user.':nohist_email:'.
                    459:            &Apache::lonnet::escape($msgid).'='.
                    460:            &Apache::lonnet::escape($message),$homeserver);
1.108     www       461: # Save new message received time
1.40      www       462:        &Apache::lonnet::put
                    463:                          ('email_status',{'recnewemail'=>time},$domain,$user);
1.108     www       464: # Into sent-mail folder
                    465:        $status.=' '.&Apache::lonnet::critical(
                    466:            'put:'.$ENV{'user.domain'}.':'.$ENV{'user.name'}.
                    467: 					      ':nohist_email_sent:'.
                    468:            &Apache::lonnet::escape($msgid).'='.
                    469:            &Apache::lonnet::escape($message),$ENV{'user.home'});
1.2       www       470:     } else {
                    471:        $status='no_host';
1.53      www       472:     }
                    473: # Notifications
                    474:     my %userenv = &Apache::lonnet::get('environment',['notification'],
                    475:                                        $domain,$user);
                    476:     if ($userenv{'notification'}) {
                    477: 	&sendnotification($userenv{'notification'},$user,$domain,$subject,0);
1.2       www       478:     }
                    479:     &Apache::lonnet::log($ENV{'user.domain'},$ENV{'user.name'},
                    480:                          $ENV{'user.home'},
                    481:       'Sending '.$msgid.' to '.$user.' at '.$domain.' with status: '.$status);
                    482:     return $status;
                    483: }
1.38      www       484: 
                    485: # New routine that respects "forward" and calls old routine
                    486: 
1.58      bowersj2  487: =pod
                    488: 
                    489: =item * B<user_normal_msg($user, $domain, $subject, $message,
                    490:     $citation, $baseurl, $attachmenturl)>: Sends a message to the
                    491:     $user at $domain, with subject $subject and message $message.
                    492: 
                    493: =cut
                    494: 
1.38      www       495: sub user_normal_msg {
1.52      www       496:     my ($user,$domain,$subject,$message,$citation,$baseurl,$attachmenturl)=@_;
1.38      www       497:     my $status='';
                    498:     my %userenv = &Apache::lonnet::get('environment',['msgforward'],
                    499:                                        $domain,$user);
                    500:     my $msgforward=$userenv{'msgforward'};
                    501:     if ($msgforward) {
                    502:        foreach (split(/\,/,$msgforward)) {
                    503: 	 my ($forwuser,$forwdomain)=split(/\:/,$_);
                    504:          $status.=
                    505: 	  &user_normal_msg_raw($forwuser,$forwdomain,$subject,$message,
1.52      www       506: 			       $citation,$baseurl,$attachmenturl).' ';
1.38      www       507:        }
                    508:     } else { 
1.49      albertel  509: 	$status=&user_normal_msg_raw($user,$domain,$subject,$message,
1.52      www       510: 				     $citation,$baseurl,$attachmenturl);
1.38      www       511:     }
                    512:     return $status;
                    513: }
                    514: 
1.2       www       515: 
1.106     www       516: # ============================================================ List all folders
                    517: 
                    518: sub folderlist {
                    519:     my $folder=shift;
                    520:     my @allfolders=&Apache::lonnet::getkeys('email_folders');
                    521:     if ($allfolders[0]=~/^error:/) { @allfolders=(); }
                    522:     return '<form method="post" action="/adm/email">'.
1.108     www       523: 	&mt('Folder').': '.
1.106     www       524: 	&Apache::loncommon::select_form($folder,'folder',
                    525: 			     ('' => &mt('INBOX'),'trash' => &mt('TRASH'),
1.113   ! www       526:                               'critical' => &mt('Critical'),
1.106     www       527: 			      'sent' => &mt('Sent Messages'),
                    528: 			      map { $_ => $_ } @allfolders)).
1.108     www       529: 			      ' '.&mt('Show').' '.
                    530: 	    &Apache::loncommon::select_form($interdis,'interdis',
                    531: (' 10' => '10', ' 20' => '20', ' 50' => '50', '100' => '100', '200' => '200')).	
                    532:    '<input type="submit" value="'.&mt('View Folder').'" /><br />'.
                    533:    '<input type="submit" name="firstview" value="'.&mt('First').'" />'.
                    534:    '<input type="submit" name="prevview" value="'.&mt('Previous').'" />'.
                    535:    '<input type="text" size="5" name="startdis" value="'.$startdis.'" />'.
                    536:    '<input type="submit" name="nextview" value="'.&mt('Next').'" />'.
                    537:    '<input type="submit" name="lastview" value="'.&mt('Last').'" />'.
                    538: 	'<a href="/adm/email?critical=display'.$sqs.'">'.
1.106     www       539: 	    &mt('View Critical Messages').'</a>'.
1.108     www       540: 	    '</form>';
1.106     www       541: }
1.108     www       542: 
1.106     www       543: # =============================================================== Folder suffix
                    544: 
                    545: sub foldersuffix {
                    546:     my $folder=shift;
                    547:     unless ($folder) { return ''; }
                    548:     return '_'.&Apache::lonnet::escape($folder);
                    549: }
                    550: 
1.7       www       551: # =============================================================== Status Change
                    552: 
                    553: sub statuschange {
1.106     www       554:     my ($msgid,$newstatus,$folder)=@_;
                    555:     my $suffix=&foldersuffix($folder);
                    556:     my %status=&Apache::lonnet::get('email_status'.$suffix,[$msgid]);
1.7       www       557:     if ($status{$msgid}=~/^error\:/) { $status{$msgid}=''; }
                    558:     unless ($status{$msgid}) { $status{$msgid}='new'; }
                    559:     unless (($status{$msgid} eq 'replied') || 
                    560:             ($status{$msgid} eq 'forwarded')) {
1.106     www       561: 	&Apache::lonnet::put('email_status'.$suffix,{$msgid => $newstatus});
1.7       www       562:     }
1.14      www       563:     if (($newstatus eq 'deleted') || ($newstatus eq 'new')) {
1.106     www       564: 	&Apache::lonnet::put('email_status'.$suffix,{$msgid => $newstatus});
1.14      www       565:     }
1.7       www       566: }
1.14      www       567: 
1.106     www       568: # ============================================================= Make new folder
                    569: 
                    570: sub makefolder {
                    571:     my ($newfolder)=@_;
1.113   ! www       572:     if (($newfolder eq 'sent')
        !           573:      || ($newfolder eq 'critical')
        !           574:      || ($newfolder eq 'trash')) { return; }
1.106     www       575:     &Apache::lonnet::put('email_folders',{$newfolder => time});
                    576: }
                    577: 
                    578: # ======================================================== Move between folders
                    579: 
                    580: sub movemsg {
                    581:     my ($msgid,$srcfolder,$trgfolder)=@_;
1.107     www       582:     my $unmsgid=&Apache::lonnet::unescape($msgid);
1.106     www       583:     my $srcsuffix=&foldersuffix($srcfolder);
                    584:     my $trgsuffix=&foldersuffix($trgfolder);
1.107     www       585: 
                    586: # Copy message
                    587:     my %message=&Apache::lonnet::get('nohist_email'.$srcsuffix,[$msgid]);
                    588:     &Apache::lonnet::put('nohist_email'.$trgsuffix,{$msgid => $message{$msgid}});
                    589: 
                    590: # Copy status
                    591:     my %status=&Apache::lonnet::get('email_status'.$srcsuffix,[$unmsgid]);
                    592:     &Apache::lonnet::put('email_status'.$trgsuffix,{$unmsgid => $status{$unmsgid}});
                    593: # See if was deleted -> becomes "read" in trash
1.108     www       594:     my $currentstatus=(&unpackmsgid($status{$unmsgid}),$srcfolder);
1.107     www       595:     if ($currentstatus eq 'deleted') {
                    596: 	&statuschange($msgid,'read',$trgfolder);
                    597:     }
                    598: # Delete orginals
1.106     www       599:     &Apache::lonnet::del('nohist_email'.$srcsuffix,[$msgid]);
1.107     www       600:     &Apache::lonnet::del('email_status'.$srcsuffix,[$unmsgid]);
1.106     www       601: }
                    602: 
1.17      www       603: # ======================================================= Display a course list
                    604: 
                    605: sub discourse {
                    606:     my $r=shift;
1.109     matthew   607:     my $classlist = &Apache::loncoursedata::get_classlist();
1.17      www       608:     my $now=time;
1.67      www       609:     my %lt=&Apache::lonlocal::texthash('cfa' => 'Check for All',
                    610:             'cfs' => 'Check for Section/Group',
                    611:             'cfn' => 'Check for None');
1.17      www       612:     $r->print(<<ENDDISHEADER);
1.92      www       613: <input type="hidden" name="sendmode" value="group" />
1.17      www       614: <script>
                    615:     function checkall() {
                    616: 	for (i=0; i<document.forms.compemail.elements.length; i++) {
                    617:             if 
                    618:           (document.forms.compemail.elements[i].name.indexOf('send_to_')==0) {
                    619: 	      document.forms.compemail.elements[i].checked=true;
                    620:             }
                    621:         }
                    622:     }
                    623: 
1.19      www       624:     function checksec() {
                    625: 	for (i=0; i<document.forms.compemail.elements.length; i++) {
                    626:             if 
                    627:           (document.forms.compemail.elements[i].name.indexOf
                    628:            ('send_to_&&&'+document.forms.compemail.chksec.value)==0) {
                    629: 	      document.forms.compemail.elements[i].checked=true;
                    630:             }
                    631:         }
                    632:     }
                    633: 
1.17      www       634:     function uncheckall() {
                    635: 	for (i=0; i<document.forms.compemail.elements.length; i++) {
                    636:             if 
                    637:           (document.forms.compemail.elements[i].name.indexOf('send_to_')==0) {
                    638: 	      document.forms.compemail.elements[i].checked=false;
                    639:             }
                    640:         }
                    641:     }
                    642: </script>
1.92      www       643: <input type="button" onClick="checkall()" value="$lt{'cfa'}" />&nbsp;
                    644: <input type="button" onClick="checksec()" value="$lt{'cfs'}" />
                    645: <input type="text" size="5" name=chksec />&nbsp;
                    646: <input type="button" onClick="uncheckall()" value="$lt{'cfn'}" />
1.17      www       647: <p>
                    648: ENDDISHEADER
1.109     matthew   649:     my %coursepersonnel=&Apache::lonnet::get_course_adv_roles();
                    650:     $r->print('<table>');
1.61      www       651:     foreach my $role (sort keys %coursepersonnel) {
1.109     matthew   652:         foreach (split(/\,/,$coursepersonnel{$role})) {
                    653:             my ($puname,$pudom)=split(/\:/,$_);
                    654:             $r->print('<tr><td><label>'.
                    655:                       '<input type="checkbox" name="send_to_&&&&&&_'.
                    656:                       $puname.':'.$pudom.'" /> '.
                    657:                       &Apache::loncommon::plainname($puname,$pudom).
                    658:                       '</label></td>'.
                    659:                       '<td>('.$_.'),</td><td><i>'.$role.'</i></td></tr>');
                    660:         }
1.61      www       661:     }
1.110     matthew   662:     $r->print('</table><table>');
1.109     matthew   663:     while (my ($student,$info) = each(%$classlist)) {
                    664:         my ($sname,$sdom,$status,$fullname,$section) =
                    665:             (@{$info}[&Apache::loncoursedata::CL_SNAME(),
                    666:                       &Apache::loncoursedata::CL_SDOM(),
                    667:                       &Apache::loncoursedata::CL_STATUS(),
                    668:                       &Apache::loncoursedata::CL_FULLNAME(),
                    669:                       &Apache::loncoursedata::CL_SECTION()]);
1.110     matthew   670:         next if ($status ne 'Active');
1.109     matthew   671:         my $key = 'send_to_&&&'.$section.'&&&'.$student;
                    672:         if (! defined($fullname) || $fullname eq '') { $fullname = $sname; }
                    673:         $r->print('<tr><td><label>'.
                    674:                   qq{<input type="checkbox" name="$key">}.('&nbsp;'x2).
                    675:                   $fullname.'</td><td>'.$sname.'@'.$sdom.'</td><td>'.$section.
                    676:                   '</td></tr>');
1.28      harris41  677:     }
1.110     matthew   678:     $r->print('</table>');
1.17      www       679: }
                    680: 
1.13      www       681: # ==================================================== Display Critical Message
1.5       www       682: 
1.12      www       683: sub discrit {
                    684:     my $r=shift;
1.67      www       685:     my $header = '<h1><font color=red>'.&mt('Critical Messages').'</font></h1>'.
1.30      matthew   686:         '<form action=/adm/email method=post>'.
                    687:         '<input type=hidden name=confirm value=true>';
                    688:     my %what=&Apache::lonnet::dump('critical');
                    689:     my $result = '';
                    690:     foreach (sort keys %what) {
                    691:         my %content=&unpackagemsg($what{$_});
                    692:         next if ($content{'senderdomain'} eq '');
                    693:         $content{'message'}=~s/\n/\<br\>/g;
1.106     www       694:         $result.='<hr />'.&mt('From').': <b>'.
1.37      www       695: &Apache::loncommon::aboutmewrapper(
                    696:  &Apache::loncommon::plainname($content{'sendername'},$content{'senderdomain'}),$content{'sendername'},$content{'senderdomain'}).'</b> ('.
                    697: $content{'sendername'}.'@'.
                    698:             $content{'senderdomain'}.') '.$content{'time'}.
1.106     www       699:             '<br />'.&mt('Subject').': '.$content{'subject'}.
                    700:             '<br /><blockquote>'.
1.36      www       701:               &Apache::lontexconvert::msgtexconverted($content{'message'}).
1.84      www       702:             '</blockquote><small>'.
                    703: &mt('You have to confirm that you received this message. After confirmation, this message will be moved to your regular inbox').
                    704:             '</small><br />'.
1.67      www       705:             '<input type=submit name="rec_'.$_.'" value="'.&mt('Confirm Receipt').'">'.
1.30      matthew   706:             '<input type=submit name="reprec_'.$_.'" '.
1.67      www       707:                   'value="'.&mt('Confirm Receipt and Reply').'">';
1.30      matthew   708:     }
                    709:     # Check to see if there were any messages.
                    710:     if ($result eq '') {
1.67      www       711:         $result = "<h2>".&mt('You have no critical messages.')."</h2>".
1.106     www       712: 	    '<a href="/adm/roles">'.&mt('Select a course').'</a><br />'.
                    713:             '<a href="/adm/email">'.&mt('Communicate').'</a>';
1.30      matthew   714:     } else {
                    715:         $r->print($header);
                    716:     }
                    717:     $r->print($result);
1.108     www       718:     $r->print('<input type="hidden" name="displayedcrit" value="true" /></form>');
1.12      www       719: }
                    720: 
1.65      www       721: sub sortedmessages {
1.106     www       722:     my ($blocked,$startblock,$endblock,$numblocked,$folder) = @_;
                    723:     my $suffix=&foldersuffix($folder);
                    724:     my @messages = &Apache::lonnet::getkeys('nohist_email'.$suffix);
1.65      www       725:     #unpack the varibles and repack into temp for sorting
                    726:     my @temp;
                    727:     foreach (@messages) {
                    728: 	my $msgid=&Apache::lonnet::escape($_);
                    729: 	my ($sendtime,$shortsubj,$fromname,$fromdomain,$status)=
1.108     www       730: 	    &Apache::lonmsg::unpackmsgid($msgid,$folder);
1.65      www       731: 	my @temp1 = ($sendtime,$shortsubj,$fromname,$fromdomain,$status,
                    732: 		     $msgid);
1.101     raeburn   733:         # Check whether message was sent during blocking period.
                    734:         if ($sendtime >= $startblock && ($sendtime <= $endblock && $endblock > 0) ) {
                    735:             my $escid = &Apache::lonnet::unescape($msgid);
                    736:             $$blocked{$escid} = 'ON';
                    737:             $$numblocked ++;
                    738:         } else { 
                    739:             push @temp ,\@temp1;
                    740:         }
1.65      www       741:     }
                    742:     #default sort
                    743:     @temp = sort  {$a->[0] <=> $b->[0]} @temp;    
                    744:     if ($ENV{'form.sortedby'} eq "date"){
                    745:         @temp = sort  {$a->[0] <=> $b->[0]} @temp;    
                    746:     }
                    747:     if ($ENV{'form.sortedby'} eq "revdate"){
                    748:     	@temp = sort  {$b->[0] <=> $a->[0]} @temp; 
                    749:     }
                    750:     if ($ENV{'form.sortedby'} eq "user"){
                    751: 	@temp = sort  {lc($a->[2]) cmp lc($b->[2])} @temp;
                    752:     }
                    753:     if ($ENV{'form.sortedby'} eq "revuser"){
                    754: 	@temp = sort  {lc($b->[2]) cmp lc($a->[2])} @temp;
                    755:     }
                    756:     if ($ENV{'form.sortedby'} eq "domain"){
                    757:         @temp = sort  {$a->[3] cmp $b->[3]} @temp;
                    758:     }
                    759:     if ($ENV{'form.sortedby'} eq "revdomain"){
                    760:         @temp = sort  {$b->[3] cmp $a->[3]} @temp;
                    761:     }
                    762:     if ($ENV{'form.sortedby'} eq "subject"){
                    763:         @temp = sort  {lc($a->[1]) cmp lc($b->[1])} @temp;
                    764:     }
                    765:     if ($ENV{'form.sortedby'} eq "revsubject"){
                    766:         @temp = sort  {lc($b->[1]) cmp lc($a->[1])} @temp;
                    767:     }
                    768:     if ($ENV{'form.sortedby'} eq "status"){
                    769:         @temp = sort  {$a->[4] cmp $b->[4]} @temp;
                    770:     }
                    771:     if ($ENV{'form.sortedby'} eq "revstatus"){
                    772:         @temp = sort  {$b->[4] cmp $a->[4]} @temp;
                    773:     }
                    774:     return @temp;
                    775: }
                    776: 
1.112     www       777: # ======================================================== Display new messages
                    778: 
                    779: 
                    780: sub disnew {
                    781:     my $r=shift;
                    782:     my %lt=&Apache::lonlocal::texthash(
                    783: 				       'nm' => 'New Messages',
                    784: 				       'su' => 'Subject',
                    785: 				       'da' => 'Date',
                    786: 				       'us' => 'Username',
                    787: 				       'op' => 'Open',
                    788: 				       'do' => 'Domain'
                    789: 				       );
                    790:     my @msgids = sort split(/\&/,&Apache::lonnet::reply
                    791:                             ('keys:'.$ENV{'user.domain'}.':'.
                    792:                              $ENV{'user.name'}.':nohist_email',
                    793:                              $ENV{'user.home'}));
                    794:     my @newmsgs;
                    795:     my %setters = ();
                    796:     my $startblock = 0;
                    797:     my $endblock = 0;
                    798:     my %blocked = ();
                    799:     my $numblocked = 0;
                    800:     # Check for blocking of display because of scheduled online exams.
                    801:     &blockcheck(\%setters,\$startblock,\$endblock);
                    802:     foreach (@msgids) {
                    803:         my ($sendtime,$shortsubj,$fromname,$fromdom,$status)=
                    804: 	    &Apache::lonmsg::unpackmsgid($_);
                    805:         if (defined($sendtime) && $sendtime!~/error/) {
                    806:             my $numsendtime = $sendtime;
                    807:             $sendtime = &Apache::lonlocal::locallocaltime($sendtime);
                    808:             if ($status eq 'new') {
                    809:                 if ($numsendtime >= $startblock && ($numsendtime <= $endblock && $endblock > 0) ) {
                    810:                     $blocked{$_} = 'ON';
                    811:                     $numblocked ++;
                    812:                 } else {
                    813:                     push @newmsgs, { 
                    814:                         msgid    => $_,
                    815:                         sendtime => $sendtime,
                    816:                         shortsub => &Apache::lonnet::unescape($shortsubj),
                    817:                         from     => $fromname,
                    818:                         fromdom  => $fromdom 
                    819:                         }
                    820:                 }
                    821:             }
                    822:         }
                    823:     }
                    824:     if ($#newmsgs >= 0) {
                    825:         $r->print(<<TABLEHEAD);
                    826: <h2>$lt{'nm'}</h2>
                    827: <table border=2><tr><th>&nbsp</th>
                    828: <th>$lt{'da'}</th><th>$lt{'us'}</th><th>$lt{'do'}</th><th>$lt{'su'}</th></tr>
                    829: TABLEHEAD
                    830:         foreach my $msg (@newmsgs) {
                    831:             $r->print(<<"ENDLINK");
                    832: <tr bgcolor="#FFBB77">
                    833: <td><a href="/adm/email?display=$msg->{'msgid'}">$lt{'op'}</a></td>
                    834: ENDLINK
                    835:             foreach ('sendtime','from','fromdom','shortsub') {
                    836:                 $r->print("<td>$msg->{$_}</td>");
                    837:             }
                    838:             $r->print("</td></tr>");
                    839:         }
                    840:         $r->print('</table></body></html>');
                    841:     } elsif ($numblocked == 0) {
                    842:         $r->print("<h3>".&mt('You have no unread messages')."</h3>");
                    843:     }
                    844:     if ($numblocked > 0) {
                    845:         my $beginblock = &Apache::lonlocal::locallocaltime($startblock);
                    846:         my $finishblock = &Apache::lonlocal::locallocaltime($endblock);
                    847:         if ($numblocked == 1) {
                    848:             $r->print("<h3>".&mt('You have').' '.$numblocked.' '.&mt('blocked unread message').".</h3>");
                    849:             $r->print(&mt('This message is not viewable because').' ');
                    850:         } else {
                    851:             $r->print("<h3>".&mt('You have').' '.$numblocked.' '.&mt('blocked unread messages').".</h3>");
                    852:             $r->print(&mt('These').' '.$numblocked.' '.&mt('messages are not viewable because '));
                    853:         }
                    854:         $r->print(
                    855: &mt('display of LON-CAPA messages sent to you by other students between').' '.$beginblock.' '.&mt('and').' '.$finishblock.' '.&mt('is currently being blocked because of online exams').'.');
                    856:         &build_block_table($r,$startblock,$endblock,\%setters);
                    857:     }
                    858: }
                    859: 
                    860: 
1.15      www       861: # ======================================================== Display all messages
                    862: 
1.14      www       863: sub disall {
1.106     www       864:     my ($r,$folder)=@_;
1.113   ! www       865:     $r->print(&folderlist($folder));
        !           866:     unless ($folder) {
        !           867: 	&disnew();
        !           868: 	return;
        !           869:     }
        !           870:     if ($folder eq 'critical') {
        !           871: 	&discrit();
        !           872:     }
1.101     raeburn   873:     my %blocked = ();
                    874:     my %setters = ();
                    875:     my $startblock;
                    876:     my $endblock;
                    877:     my $numblocked = 0;
                    878:     &blockcheck(\%setters,\$startblock,\$endblock);
                    879:     $r->print(<<ENDDISHEADER);
1.29      www       880: <script>
                    881:     function checkall() {
                    882: 	for (i=0; i<document.forms.disall.elements.length; i++) {
                    883:             if 
                    884:           (document.forms.disall.elements[i].name.indexOf('delmark_')==0) {
                    885: 	      document.forms.disall.elements[i].checked=true;
                    886:             }
                    887:         }
                    888:     }
                    889: 
                    890:     function uncheckall() {
                    891: 	for (i=0; i<document.forms.disall.elements.length; i++) {
                    892:             if 
                    893:           (document.forms.disall.elements[i].name.indexOf('delmark_')==0) {
                    894: 	      document.forms.disall.elements[i].checked=false;
                    895:             }
                    896:         }
                    897:     }
                    898: </script>
                    899: ENDDISHEADER
1.108     www       900:     my $fsqs='&folder='.$folder;
                    901:     my @temp=sortedmessages(\%blocked,$startblock,$endblock,\$numblocked,$folder);
                    902:     my $totalnumber=$#temp+1;
                    903:     my $number=int($totalnumber/$interdis)+1;
                    904:     my $firstdis=$interdis*$startdis;
                    905:     if ($firstdis>$#temp) { $firstdis=$#temp-$interdis+1; }
                    906:     my $lastdis=$firstdis+$interdis-1;
                    907:     if ($lastdis>$#temp) { $lastdis=$#temp; }
1.113   ! www       908:     $r->print('<form method="post" name="disall" action="/adm/email">'.
1.106     www       909: 	      '<table border=2><tr><th colspan="3">&nbsp</th><th>');
1.62      www       910:     if ($ENV{'form.sortedby'} eq "revdate") {
1.108     www       911: 	$r->print('<a href = "?sortedby=date'.$fsqs.'">'.&mt('Date').'</a></th>');
1.62      www       912:     } else {
1.108     www       913: 	$r->print('<a href = "?sortedby=revdate'.$fsqs.'">'.&mt('Date').'</a></th>');
1.62      www       914:     }
                    915:     $r->print('<th>');
                    916:     if ($ENV{'form.sortedby'} eq "revuser") {
1.108     www       917: 	$r->print('<a href = "?sortedby=user'.$fsqs.'">'.&mt('Username').'</a>');
1.62      www       918:     } else {
1.108     www       919: 	$r->print('<a href = "?sortedby=revuser'.$fsqs.'">'.&mt('Username').'</a>');
1.62      www       920:     }
                    921:     $r->print('</th><th>');
                    922:     if ($ENV{'form.sortedby'} eq "revdomain") {
1.108     www       923: 	$r->print('<a href = "?sortedby=domain'.$fsqs.'">'.&mt('Domain').'</a>');
1.62      www       924:     } else {
1.108     www       925: 	$r->print('<a href = "?sortedby=revdomain'.$fsqs.'">'.&mt('Domain').'</a>');
1.62      www       926:     }
                    927:     $r->print('</th><th>');
                    928:     if ($ENV{'form.sortedby'} eq "revsubject") {
1.108     www       929: 	$r->print('<a href = "?sortedby=subject'.$fsqs.'">'.&mt('Subject').'</a>');
1.62      www       930:     } else {
1.108     www       931:     	$r->print('<a href = "?sortedby=revsubject'.$fsqs.'">'.&mt('Subject').'</a>');
1.62      www       932:     }
                    933:     $r->print('</th><th>');
                    934:     if ($ENV{'form.sortedby'} eq "revstatus") {
1.108     www       935: 	$r->print('<a href = "?sortedby=status'.$fsqs.'">'.&mt('Status').'</th>');
1.62      www       936:     } else {
1.108     www       937:      	$r->print('<a href = "?sortedby=revstatus'.$fsqs.'">'.&mt('Status').'</th>');
1.62      www       938:     }
                    939:     $r->print('</tr>');
1.108     www       940:     for (my $n=$firstdis;$n<=$lastdis;$n++) {
                    941: 	my ($sendtime,$shortsubj,$fromname,$fromdomain,$status,$origID)= @{$temp[$n]};
1.63      albertel  942: 	if (($status ne 'deleted') && defined($sendtime) && $sendtime!~/error/) {
1.39      albertel  943: 	    if ($status eq 'new') {
                    944: 		$r->print('<tr bgcolor="#FFBB77">');
                    945: 	    } elsif ($status eq 'read') {
                    946: 		$r->print('<tr bgcolor="#BBBB77">');
                    947: 	    } elsif ($status eq 'replied') {
1.62      www       948: 		$r->print('<tr bgcolor="#AAAA88">'); 
1.39      albertel  949: 	    } else {
                    950: 		$r->print('<tr bgcolor="#99BBBB">');
                    951: 	    }
1.106     www       952: 	    $r->print('<td></a><input type=checkbox name="delmark_'.$origID.'" /></td><td><a href="/adm/email?display='.$origID.$sqs. 
                    953: 		      '">'.&mt('Open').'</a></td><td>'.
                    954: 		      ($folder ne 'trash'?'<a href="/adm/email?markdel='.$origID.$sqs.
                    955: 		      '">'.&mt('Delete'):'&nbsp').'</td>'.
1.66      www       956: 		      '<td>'.&Apache::lonlocal::locallocaltime($sendtime).'</td><td>'.
1.39      albertel  957: 		      $fromname.'</td><td>'.$fromdomain.'</td><td>'.
1.14      www       958: 		      &Apache::lonnet::unescape($shortsubj).'</td><td>'.
                    959:                       $status.'</td></tr>');
1.106     www       960: 	} elsif ($status eq 'deleted') {
                    961: # purge
1.108     www       962: 	    &movemsg(&Apache::lonnet::unescape($origID),$folder,'trash');
1.63      albertel  963: 	}
                    964:     }   
                    965:     $r->print('</table><p>'.
1.106     www       966:   '<a href="javascript:checkall()">'.&mt('Check All').'</a>&nbsp;'.
                    967:   '<a href="javascript:uncheckall()">'.&mt('Uncheck All').'</a></p>'.
                    968:   '<input type="hidden" name="sortedby" value="'.$ENV{'form.sortedby'}.'" />');
                    969:     if ($folder ne 'trash') {
                    970: 	$r->print(
                    971: 	      '<p><input type="submit" name="markeddel" value="'.&mt('Delete Checked').'" /></p>');
                    972:     }
                    973: $r->print('<p><input type="submit" name="markedmove" value="'.&mt('Move Checked to Folder').'" />');
                    974:     my @allfolders=&Apache::lonnet::getkeys('email_folders');
                    975:     if ($allfolders[0]=~/^error:/) { @allfolders=(); }
                    976:     $r->print(
                    977: 	&Apache::loncommon::select_form('','movetofolder',
                    978: 			     ( map { $_ => $_ } @allfolders))
                    979: 	      );
                    980:     $r->print('<input type="hidden" name="folder" value="'.$folder.'" /></form>');
1.101     raeburn   981:     if ($numblocked > 0) {
                    982:         my $beginblock = &Apache::lonlocal::locallocaltime($startblock);
                    983:         my $finishblock = &Apache::lonlocal::locallocaltime($endblock);
                    984:         $r->print('<br /><br />'.
                    985:                   $numblocked.' '.&mt('message(s) is/are not viewable because display of LON-CAPA messages sent to you by other students between').' '.$beginblock.' '.&mt('and').' '.$finishblock.' '.&mt('is currently being blocked because of online exams.'));
                    986:         &build_block_table($r,$startblock,$endblock,\%setters);
                    987:     }
1.14      www       988: }
                    989: 
1.15      www       990: # ============================================================== Compose output
                    991: 
                    992: sub compout {
1.108     www       993:     my ($r,$forwarding,$replying,$broadcast,$replycrit,$folder)=@_;
1.92      www       994: 
                    995:     if ($broadcast eq 'individual') {
                    996: 	&printheader($r,'/adm/email?compose=individual',
                    997: 	     'Send a Message');
                    998:     } elsif ($broadcast) {
                    999: 	&printheader($r,'/adm/email?compose=group',
                   1000: 	     'Broadcast Message');
                   1001:     } elsif ($forwarding) {
                   1002: 	&Apache::lonhtmlcommon::add_breadcrumb
                   1003:         ({href=>"/adm/email?display=".&Apache::lonnet::escape($forwarding),
                   1004:           text=>"Display Message"});
                   1005: 	&printheader($r,'/adm/email?forward='.&Apache::lonnet::escape($forwarding),
                   1006: 	     'Forwarding a Message');
                   1007:     } elsif ($replying) {
                   1008: 	&Apache::lonhtmlcommon::add_breadcrumb
                   1009:         ({href=>"/adm/email?display=".&Apache::lonnet::escape($replying),
                   1010:           text=>"Display Message"});
                   1011: 	&printheader($r,'/adm/email?replyto='.&Apache::lonnet::escape($replying),
                   1012: 	     'Replying to a Message');
1.94      www      1013:     } elsif ($replycrit) {
                   1014: 	$r->print('<h3>'.&mt('Replying to a Critical Message').'</h3>');
                   1015: 	$replying=$replycrit;
1.92      www      1016:     } else {
                   1017: 	&printheader($r,'/adm/email?compose=upload',
                   1018: 	     'Distribute from Uploaded File');
                   1019:     }
                   1020: 
1.89      www      1021:     my $dispcrit='';
1.15      www      1022:     my $dissub='';
                   1023:     my $dismsg='';
1.67      www      1024:     my $func=&mt('Send New');
1.69      www      1025:     my %lt=&Apache::lonlocal::texthash('us' => 'Username',
                   1026: 				       'do' => 'Domain',
                   1027: 				       'ad' => 'Additional Recipients',
                   1028: 				       'sb' => 'Subject',
                   1029: 				       'ca' => 'Cancel',
                   1030: 				       'ma' => 'Mail');
                   1031: 
                   1032:     if (&Apache::lonnet::allowed('srm',$ENV{'request.course.id'})) {
1.35      bowersj2 1033: 	 my $crithelp = Apache::loncommon::help_open_topic("Course_Critical_Message");
1.15      www      1034:          $dispcrit=
1.92      www      1035:  '<input type="checkbox" name="critmsg" /> '.&mt('Send as critical message').' ' . $crithelp . 
1.35      bowersj2 1036:  '<br>'.
1.92      www      1037:  '<input type="checkbox" name="sendbck" /> '.&mt('Send as critical message').'  ' .
1.67      www      1038:  &mt('and return receipt') . $crithelp . '<p>';
1.92      www      1039:      }
                   1040:     my %message;
                   1041:     my %content;
                   1042:     my $defdom=$ENV{'user.domain'};
1.15      www      1043:     if ($forwarding) {
1.92      www      1044: 	%message=&Apache::lonnet::get('nohist_email',[$forwarding]);
1.108     www      1045: 	%content=&unpackagemsg($message{$forwarding},$folder);
1.92      www      1046: 	$dispcrit.='<input type="hidden" name="forwid" value="'.
                   1047: 	    $forwarding.'" />';
                   1048: 	$func=&mt('Forward');
                   1049: 	
                   1050: 	$dissub=&mt('Forwarding').': '.$content{'subject'};
                   1051: 	$dismsg=&mt('Forwarded message from').' '.
                   1052: 	    $content{'sendername'}.' '.&mt('at').' '.$content{'senderdomain'};
                   1053:     }
                   1054:     if ($replying) {
                   1055: 	%message=&Apache::lonnet::get('nohist_email',[$replying]);
1.108     www      1056: 	%content=&unpackagemsg($message{$replying},$folder);
1.105     albertel 1057: 	$dispcrit.='<input type="hidden" name="replyid" value="'.
                   1058: 	    $replying.'" />';
1.108     www      1059: 	$func=&mt('Send Reply to');
1.92      www      1060: 	
                   1061: 	$dissub=&mt('Reply').': '.$content{'subject'};       
                   1062: 	$dismsg='> '.$content{'message'};
                   1063: 	$dismsg=~s/\r/\n/g;
                   1064: 	$dismsg=~s/\f/\n/g;
                   1065: 	$dismsg=~s/\n+/\n\> /g;
1.15      www      1066:     }
1.111     www      1067:     my $citation=&displayresource(%content);
1.37      www      1068:     if ($ENV{'form.recdom'}) { $defdom=$ENV{'form.recdom'}; }
1.22      www      1069:       $r->print(
1.31      matthew  1070:                 '<form action="/adm/email"  name="compemail" method="post"'.
                   1071:                 ' enctype="multipart/form-data">'."\n".
1.92      www      1072:                 '<input type="hidden" name="sendmail" value="on" />'."\n".
1.31      matthew  1073:                 '<table>');
1.22      www      1074:     unless (($broadcast eq 'group') || ($broadcast eq 'upload')) {
1.92      www      1075: 	if ($replying) {
                   1076: 	    $r->print('<tr><td colspan="2">'.&mt('Replying to').' '.
                   1077: 		      &Apache::loncommon::aboutmewrapper(
                   1078: 							 &Apache::loncommon::plainname($content{'sendername'},$content{'senderdomain'}),$content{'sendername'},$content{'senderdomain'}).' ('.
                   1079: 		      $content{'sendername'}.'@'.
                   1080: 		      $content{'senderdomain'}.')'.
                   1081: 		      '<input type="hidden" name="recuname" value="'.$content{'sendername'}.'" />'.
                   1082: 		      '<input type="hidden" name="recdomain" value="'.$content{'senderdomain'}.'" />'.
                   1083: 		      '</td></tr>');
                   1084: 	} else {
                   1085: 	    my $domform = &Apache::loncommon::select_dom_form($defdom,'recdomain');
                   1086: 	    my $selectlink=&Apache::loncommon::selectstudent_link
1.46      www      1087: 	    ('compemail','recuname','recdomain');
1.92      www      1088: 	    $r->print(<<"ENDREC");
1.69      www      1089: <tr><td>$lt{'us'}:</td><td><input type="text" size="12" name="recuname" value="$ENV{'form.recname'}"></td><td rowspan="2">$selectlink</td></tr>
                   1090: <tr><td>$lt{'do'}:</td>
1.31      matthew  1091: <td>$domform</td></tr>
1.17      www      1092: ENDREC
1.92      www      1093:         }
1.17      www      1094:     }
1.55      bowersj2 1095:     my $latexHelp = Apache::loncommon::helpLatexCheatsheet();
1.31      matthew  1096:     if ($broadcast ne 'upload') {
1.22      www      1097:        $r->print(<<"ENDCOMP");
1.69      www      1098: <tr><td>$lt{'ad'}<br /><tt>username\@domain,username\@domain, ...
1.20      www      1099: </tt></td><td>
1.91      www      1100: <input type="text" size="50" name="additionalrec" /></td></tr>
                   1101: <tr><td>$lt{'sb'}:</td><td><input type="text" size="50" name="subject" value="$dissub" />
1.15      www      1102: </td></tr></table>
1.55      bowersj2 1103: $latexHelp
1.92      www      1104: <textarea name="message" cols="80" rows="15" wrap="hard">$dismsg
1.69      www      1105: </textarea></p><br />
1.15      www      1106: $dispcrit
1.69      www      1107: <input type="submit" name="send" value="$func $lt{'ma'}" />
1.111     www      1108: <input type="submit" name="cancel" value="$lt{'ca'}" /><hr />
                   1109: $citation
1.15      www      1110: ENDCOMP
1.31      matthew  1111:     } else { # $broadcast is 'upload'
1.22      www      1112: 	$r->print(<<ENDUPLOAD);
1.91      www      1113: <input type="hidden" name="sendmode" value="upload" />
1.86      www      1114: <input type="hidden" name="send" value="on" />
1.22      www      1115: <h3>Generate messages from a file</h3>
1.31      matthew  1116: <p>
1.91      www      1117: Subject: <input type="text" size="50" name="subject" />
1.31      matthew  1118: </p>
                   1119: <p>General message text<br />
1.91      www      1120: <textarea name="message" cols="60" rows="10" wrap="hard">$dismsg
1.31      matthew  1121: </textarea></p>
                   1122: <p>
                   1123: The file format for the uploaded portion of the message is:
1.22      www      1124: <pre>
                   1125: username1\@domain1: text
                   1126: username2\@domain2: text
1.31      matthew  1127: username3\@domain1: text
1.22      www      1128: </pre>
1.31      matthew  1129: </p>
                   1130: <p>
1.22      www      1131: The messages will be assembled from all lines with the respective 
1.31      matthew  1132: <tt>username\@domain</tt>, and appended to the general message text.</p>
                   1133: <p>
1.91      www      1134: <input type="file" name="upfile" size="40" /></p><p>
1.22      www      1135: $dispcrit
1.92      www      1136: <input type="submit" value="Upload and Send" /></p>
1.22      www      1137: ENDUPLOAD
                   1138:     }
1.17      www      1139:     if ($broadcast eq 'group') {
                   1140:        &discourse;
                   1141:     }
                   1142:     $r->print('</form>');
1.15      www      1143: }
                   1144: 
1.45      www      1145: # ---------------------------------------------------- Display all face to face
                   1146: 
1.104     matthew  1147: sub retrieve_instructor_comments {
                   1148:     my ($user,$domain)=@_;
                   1149:     my $target=$ENV{'form.grade_target'};
                   1150:     if (! $ENV{'request.course.id'}) { return; }
                   1151:     if (! &Apache::lonnet::allowed('srm',$ENV{'request.course.id'})) {
                   1152: 	return;
                   1153:     }
                   1154:     my %records=&Apache::lonnet::dump('nohist_email',
                   1155: 			 $ENV{'course.'.$ENV{'request.course.id'}.'.domain'},
                   1156: 			 $ENV{'course.'.$ENV{'request.course.id'}.'.num'},
                   1157:                          '%255b'.$user.'%253a'.$domain.'%255d');
                   1158:     my $result='';
                   1159:     foreach (sort(keys(%records))) {
                   1160:         my %content=&unpackagemsg($records{$_});
                   1161:         next if ($content{'senderdomain'} eq '');
                   1162:         next if ($content{'subject'} !~ /^Record/);
                   1163:         # $content{'message'}=~s/\n/\<br\>/g;
                   1164:         $result.='Recorded by '.
                   1165:             $content{'sendername'}.'@'.$content{'senderdomain'}."\n";
                   1166:         $result.=
                   1167:             &Apache::lontexconvert::msgtexconverted($content{'message'})."\n";
                   1168:      }
                   1169:     return $result;
                   1170: }
                   1171: 
1.45      www      1172: sub disfacetoface {
                   1173:     my ($r,$user,$domain)=@_;
1.98      sakharuk 1174:     my $target=$ENV{'form.grade_target'};
1.45      www      1175:     unless ($ENV{'request.course.id'}) { return; }
                   1176:     unless (&Apache::lonnet::allowed('srm',$ENV{'request.course.id'})) {
                   1177: 	return;
                   1178:     }
                   1179:     my %records=&Apache::lonnet::dump('nohist_email',
                   1180: 			 $ENV{'course.'.$ENV{'request.course.id'}.'.domain'},
                   1181: 			 $ENV{'course.'.$ENV{'request.course.id'}.'.num'},
                   1182:                          '%255b'.$user.'%253a'.$domain.'%255d');
                   1183:     my $result='';
                   1184:     foreach (sort keys %records) {
                   1185:         my %content=&unpackagemsg($records{$_});
                   1186:         next if ($content{'senderdomain'} eq '');
                   1187:         $content{'message'}=~s/\n/\<br\>/g;
                   1188:         if ($content{'subject'}=~/^Record/) {
1.69      www      1189: 	    $result.='<h3>'.&mt('Record').'</h3>';
1.102     raeburn  1190:         } elsif ($content{'subject'}=~/^Broadcast/) {
                   1191:             $result .='<h3>'.&mt('Broadcast Message').'</h3>';
1.45      www      1192:         } else {
1.102     raeburn  1193:             $result.='<h3>'.&mt('Critical Message').'</h3>';
1.45      www      1194:             %content=&unpackagemsg($content{'message'});
                   1195:             $content{'message'}=
1.92      www      1196:                 '<b>'.&mt('Subject').': '.$content{'subject'}.'</b><br />'.
1.45      www      1197: 		$content{'message'};
                   1198:         }
1.69      www      1199:         $result.=&mt('By').': <b>'.
1.45      www      1200: &Apache::loncommon::aboutmewrapper(
                   1201:  &Apache::loncommon::plainname($content{'sendername'},$content{'senderdomain'}),$content{'sendername'},$content{'senderdomain'}).'</b> ('.
                   1202: $content{'sendername'}.'@'.
                   1203:             $content{'senderdomain'}.') '.$content{'time'}.
1.92      www      1204:             '<br /><blockquote>'.
1.45      www      1205:               &Apache::lontexconvert::msgtexconverted($content{'message'}).
                   1206: 	      '</blockquote>';
                   1207:      }
                   1208:     # Check to see if there were any messages.
                   1209:     if ($result eq '') {
1.98      sakharuk 1210: 	if ($target ne 'tex') { 
1.102     raeburn  1211: 	    $r->print("<p><b>".&mt("No notes, face-to-face discussion records, critical messages, or broadcast messages in this course.")."</b></p>");
1.98      sakharuk 1212: 	} else {
1.102     raeburn  1213: 	    $r->print('\textbf{'.&mt("No notes, face-to-face discussion records, critical messages or broadcast messages in this course.").'}\\\\');
1.98      sakharuk 1214: 	}
1.45      www      1215:     } else {
                   1216:        $r->print($result);
                   1217:     }
                   1218: }
                   1219: 
1.44      www      1220: # ---------------------------------------------------------------- Face to face
                   1221: 
                   1222: sub facetoface {
                   1223:     my ($r,$stage)=@_;
                   1224:     unless (&Apache::lonnet::allowed('srm',$ENV{'request.course.id'})) {
                   1225: 	return;
                   1226:     }
1.89      www      1227:     &printheader($r,
                   1228: 		 '/adm/email?recordftf=query',
1.102     raeburn  1229: 		 "User Notes, Face-to-Face, Critical Messages, Broadcast Messages");
1.46      www      1230: # from query string
1.88      www      1231: 
1.46      www      1232:     if ($ENV{'form.recname'}) { $ENV{'form.recuname'}=$ENV{'form.recname'}; }
                   1233:     if ($ENV{'form.recdom'}) { $ENV{'form.recdomain'}=$ENV{'form.recdom'}; }
                   1234: 
1.44      www      1235:     my $defdom=$ENV{'user.domain'};
1.46      www      1236: # already filled in
1.44      www      1237:     if ($ENV{'form.recdomain'}) { $defdom=$ENV{'form.recdomain'}; }
1.46      www      1238: # generate output
1.44      www      1239:     my $domform = &Apache::loncommon::select_dom_form($defdom,'recdomain');
1.46      www      1240:     my $stdbrws = &Apache::loncommon::selectstudent_link
                   1241: 	('stdselect','recuname','recdomain');
1.88      www      1242:     my %lt=&Apache::lonlocal::texthash('user' => 'Username',
                   1243: 				       'dom' => 'Domain',
1.102     raeburn  1244: 				       'head' => 'User Notes, Records of Face-To-Face Discussions, Critical Messages, and Broadcast Messages in Course',
1.88      www      1245: 				       'subm' => 'Retrieve discussion and message records',
                   1246: 				       'newr' => 'New Record (record is visible to course faculty and staff)',
                   1247: 				       'post' => 'Post this Record');
1.44      www      1248:     $r->print(<<"ENDTREC");
1.88      www      1249: <h3>$lt{'head'}</h3>
1.46      www      1250: <form method="post" action="/adm/email" name="stdselect">
1.44      www      1251: <input type="hidden" name="recordftf" value="retrieve" />
                   1252: <table>
1.88      www      1253: <tr><td>$lt{'user'}:</td><td><input type="text" size="12" name="recuname" value="$ENV{'form.recuname'}" /></td>
1.44      www      1254: <td rowspan="2">
1.46      www      1255: $stdbrws
1.88      www      1256: <input type="submit" value="$lt{'subm'}" /></td>
1.44      www      1257: </tr>
1.88      www      1258: <tr><td>$lt{'dom'}:</td>
1.44      www      1259: <td>$domform</td></tr>
                   1260: </table>
                   1261: </form>
                   1262: ENDTREC
                   1263:     if (($stage ne 'query') &&
                   1264:         ($ENV{'form.recdomain'}) && ($ENV{'form.recuname'})) {
                   1265:         chomp($ENV{'form.newrecord'});
                   1266:         if ($ENV{'form.newrecord'}) {
1.45      www      1267:            &user_normal_msg_raw(
                   1268:             $ENV{'course.'.$ENV{'request.course.id'}.'.num'},
                   1269:             $ENV{'course.'.$ENV{'request.course.id'}.'.domain'},
1.88      www      1270:             &mt('Record').
                   1271: 	     ' ['.$ENV{'form.recuname'}.':'.$ENV{'form.recdomain'}.']',
1.45      www      1272: 	    $ENV{'form.newrecord'});
1.44      www      1273:         }
1.46      www      1274:         $r->print('<h3>'.&Apache::loncommon::plainname($ENV{'form.recuname'},
                   1275: 				     $ENV{'form.recdomain'}).'</h3>');
1.45      www      1276:         &disfacetoface($r,$ENV{'form.recuname'},$ENV{'form.recdomain'});
1.44      www      1277: 	$r->print(<<ENDRHEAD);
                   1278: <form method="post" action="/adm/email">
                   1279: <input name="recdomain" value="$ENV{'form.recdomain'}" type="hidden" />
                   1280: <input name="recuname" value="$ENV{'form.recuname'}" type="hidden" />
                   1281: ENDRHEAD
                   1282:         $r->print(<<ENDBFORM);
1.88      www      1283: <hr />$lt{'newr'}<br />
1.44      www      1284: <textarea name="newrecord" cols="80" rows="10" wrap="hard"></textarea>
1.45      www      1285: <br />
                   1286: <input type="hidden" name="recordftf" value="post" />
1.88      www      1287: <input type="submit" value="$lt{'post'}" />
1.44      www      1288: </form>
                   1289: ENDBFORM
                   1290:     }
                   1291: }
1.91      www      1292: 
1.101     raeburn  1293: # ----------------------------------------------------------- Blocking during exams
                   1294: 
                   1295: sub examblock {
                   1296:     my ($r,$action) = @_;
                   1297:     unless ($ENV{'request.course.id'}) { return;}
                   1298:     unless (&Apache::lonnet::allowed('srm',$ENV{'request.course.id'})) { $r->print('Not allowed'); }
                   1299:     my %lt=&Apache::lonlocal::texthash(
                   1300:             'comb' => 'Communication Blocking',
                   1301:             'cbds' => 'Communication blocking during scheduled exams',
                   1302:             'desc' => 'You can use communication blocking to prevent students enrolled in this course from displaying LON-CAPA messages sent by other students during an online exam. As blocking of communication could potentially interrupt legitimate communication between students who are also both enrolled in a different LON-CAPA course, please be careful that you select the correct start and end times for your scheduled exam when setting or modifying these parameters.',
                   1303:              'mecb' => 'Modify existing communication blocking periods',
                   1304:              'ncbc' => 'No communication blocks currently stored'
                   1305:     );
                   1306: 
                   1307:     my %ltext = &Apache::lonlocal::texthash(
                   1308:             'dura' => 'Duration',
                   1309:             'setb' => 'Set by',
                   1310:             'even' => 'Event',
                   1311:             'actn' => 'Action',
                   1312:             'star' => 'Start',
                   1313:             'endd' => 'End'
                   1314:     );
                   1315: 
                   1316:     &printheader($r,'/adm/email?block=display',$lt{'comb'});
                   1317:     $r->print('<h3>'.$lt{'cbds'}.'</h3>');
                   1318: 
                   1319:     if ($action eq 'store') {
                   1320:         &blockstore($r);
                   1321:     }
                   1322: 
                   1323:     $r->print($lt{'desc'}.'<br /><br />
                   1324:                <form name="blockform" method="post" action="/adm/email?block=store">
                   1325:              ');
                   1326: 
                   1327:     $r->print('<h4>'.$lt{'mecb'}.'</h4>');
                   1328:     my %records = ();
                   1329:     my $blockcount = 0;
                   1330:     my $parmcount = 0;
                   1331:     &get_blockdates(\%records,\$blockcount);
                   1332:     if ($blockcount > 0) {
                   1333:         $parmcount = &display_blocker_status($r,\%records,\%ltext);
                   1334:     } else {
                   1335:         $r->print($lt{'ncbc'}.'<br /><br />');
                   1336:     }
                   1337:     &display_addblocker_table($r,$parmcount,\%ltext);
                   1338:     $r->print(<<"END");
                   1339: <br />
                   1340: <input type="hidden" name="blocktotal" value="$blockcount" />
                   1341: <input type ="submit" value="Save Changes" />
                   1342: </form>
                   1343: </body>
                   1344: </html>
                   1345: END
                   1346:     return;
                   1347: }
                   1348: 
                   1349: sub blockstore {
                   1350:     my $r = shift;
                   1351:     my %lt=&Apache::lonlocal::texthash(
                   1352:             'tfcm' => 'The following changes were made',
                   1353:             'cbps' => 'communication blocking period(s)',
                   1354:             'werm' => 'was/were removed',
                   1355:             'wemo' => 'was/were modified',
                   1356:             'wead' => 'was/were added',
                   1357:             'ncwm' => 'No changes were made.' 
                   1358:     );
                   1359:     my %adds = ();
                   1360:     my %removals = ();
                   1361:     my %cancels = ();
                   1362:     my $modtotal = 0;
                   1363:     my $canceltotal = 0;
                   1364:     my $addtotal = 0;
                   1365:     my %blocking = ();
                   1366:     $r->print('<h3>'.$lt{'head'}.'</h3>');
                   1367:     foreach (keys %ENV) {
                   1368:         if ($_ =~ m/^form\.modify_(\w+)$/) {
                   1369:             $adds{$1} = $1;
                   1370:             $removals{$1} = $1;
                   1371:             $modtotal ++;
                   1372:         } elsif ($_ =~ m/^form\.cancel_(\d+)$/) {
                   1373:             $cancels{$1} = $1;
                   1374:             unless ( defined($removals{$1}) ) {
                   1375:                 $removals{$1} = $1;
                   1376:                 $canceltotal ++;
                   1377:             }
                   1378:         } elsif ($_ =~ m/^form\.add_(\d+)$/) {
                   1379:             $adds{$1} = $1;
                   1380:             $addtotal ++;
                   1381:         }
                   1382:     }
                   1383: 
                   1384:     foreach (keys %removals) {
                   1385:         my $hashkey = $ENV{'form.key_'.$_};
                   1386:         &Apache::lonnet::del('comm_block',["$hashkey"],
                   1387:                          $ENV{'course.'.$ENV{'request.course.id'}.'.domain'},
                   1388:                          $ENV{'course.'.$ENV{'request.course.id'}.'.num'}
                   1389:                          );
                   1390:     }
                   1391:     foreach (keys %adds) {
                   1392:         unless ( defined($cancels{$_}) ) {
                   1393:             my ($newstart,$newend) = &get_dates_from_form($_);
                   1394:             my $newkey = $newstart.'____'.$newend;
                   1395:             $blocking{$newkey} = $ENV{'user.name'}.'@'.$ENV{'user.domain'}.':'.$ENV{'form.title_'.$_};
                   1396:         }
                   1397:     }
                   1398:     if ($addtotal + $modtotal > 0) {
                   1399:         &Apache::lonnet::put('comm_block',\%blocking,
                   1400:                      $ENV{'course.'.$ENV{'request.course.id'}.'.domain'},
                   1401:                      $ENV{'course.'.$ENV{'request.course.id'}.'.num'}
                   1402:                      );
                   1403:     }
                   1404:     my $chgestotal = $canceltotal + $modtotal + $addtotal;
                   1405:     if ($chgestotal > 0) {
                   1406:         $r->print($lt{'tfcm'}.'<ul>');
                   1407:         if ($canceltotal > 0) {
                   1408:             $r->print('<li>'.$canceltotal.' '.$lt{'cbps'},' '.$lt{'werm'}.'</li>');
                   1409:         }
                   1410:         if ($modtotal > 0) {
                   1411:             $r->print('<li>'.$modtotal.' '.$lt{'cbps'},' '.$lt{'wemo'}.'</li>');
                   1412:         }
                   1413:         if ($addtotal > 0) {
                   1414:             $r->print('<li>'.$addtotal.' '.$lt{'cbps'},' '.$lt{'wead'}.'</li>');
                   1415:         }
                   1416:         $r->print('</ul>');
                   1417:     } else {
                   1418:         $r->print($lt{'ncwm'});
                   1419:     }
                   1420:     $r->print('<br />');
                   1421:     return;
                   1422: }
                   1423: 
                   1424: sub get_dates_from_form {
                   1425:     my $item = shift;
                   1426:     my $startdate = &Apache::lonhtmlcommon::get_date_from_form('startdate_'.$item);
                   1427:     my $enddate   = &Apache::lonhtmlcommon::get_date_from_form('enddate_'.$item);
                   1428:     return ($startdate,$enddate);
                   1429: }
                   1430: 
                   1431: sub get_blockdates {
                   1432:     my ($records,$blockcount) = @_;
                   1433:     $$blockcount = 0;
                   1434:     %{$records} = &Apache::lonnet::dump('comm_block',
                   1435:                          $ENV{'course.'.$ENV{'request.course.id'}.'.domain'},
                   1436:                          $ENV{'course.'.$ENV{'request.course.id'}.'.num'}
                   1437:                          );
                   1438:     $$blockcount = keys %{$records};
                   1439:                                                                                                              
                   1440:     foreach (keys %{$records}) {
                   1441:         if ($_ eq 'error: 2 tie(GDBM) Failed while attempting dump') {
                   1442:             $$blockcount = 0;
                   1443:             last;
                   1444:         }
                   1445:     }
                   1446: }
                   1447: 
                   1448: sub display_blocker_status {
                   1449:     my ($r,$records,$ltext) = @_;
                   1450:     my $parmcount = 0;
                   1451:     my @bgcols = ("#eeeeee","#dddddd");
                   1452:     my $function = &Apache::loncommon::get_users_function();
                   1453:     my $color = &Apache::loncommon::designparm($function.'.tabbg',
                   1454:                                                     $ENV{'user.domain'});
                   1455:     my %lt = &Apache::lonlocal::texthash(
                   1456:         'modi' => 'Modify',
                   1457:         'canc' => 'Cancel',
                   1458:     );
                   1459:     $r->print(<<"END");
                   1460: <table border="0" cellpadding="0" cellspacing="0">
                   1461:  <tr>
                   1462:   <td width="100%" bgcolor="#000000">
                   1463:    <table width="100%" border="0" cellpadding="1" cellspacing="0">
                   1464:     <tr>
                   1465:      <td width="100%" bgcolor="#000000">
                   1466:       <table border="0" cellpadding="3" cellspacing="3" bgcolor="#FFFFFF">
                   1467:        <tr bgcolor="$color">
                   1468:         <td><b>$$ltext{'dura'}</b></td>
                   1469:         <td><b>$$ltext{'setb'}</b></td>
                   1470:         <td><b>$$ltext{'even'}</b></td>
                   1471:         <td><b>$$ltext{'actn'}?</b></td>
                   1472:        </tr>
                   1473: END
                   1474:     foreach (sort keys %{$records}) {
                   1475:         my $iter = $parmcount%2;
                   1476:         my $onchange = 'onFocus="javascript:window.document.forms['.
                   1477:                        "'blockform'].elements['modify_".$parmcount."'].".
                   1478:                        'checked=true;"';
                   1479:         my ($start,$end) = split/____/,$_;
                   1480:         my $startform = &Apache::lonhtmlcommon::date_setter('blockform','startdate_'.$parmcount,$start,$onchange);
                   1481:         my $endform = &Apache::lonhtmlcommon::date_setter('blockform','enddate_'.$parmcount,$end,$onchange);
                   1482:         my ($setter,$title) = split/:/,$$records{$_};
                   1483:         my ($setuname,$setudom) = split/@/,$setter;
                   1484:         my $settername = &Apache::loncommon::plainname($setuname,$setudom);
                   1485:         $r->print(<<"END");
                   1486:        <tr bgcolor="$bgcols[$iter]">
                   1487:         <td>$$ltext{'star'}:&nbsp;$startform<br/>$$ltext{'endd'}:&nbsp;&nbsp;$endform</td>
                   1488:         <td>$settername</td>
                   1489:         <td><input type="text" name="title_$parmcount" size="15" value="$title"/><input type="hidden" name="key_$parmcount" value="$_"></td>
                   1490:         <td>$lt{'modi'}?&nbsp;<input type="checkbox" name="modify_$parmcount"/><br />$lt{'canc'}?&nbsp;&nbsp;<input type="checkbox" name="cancel_$parmcount"/>
                   1491:        </tr>
                   1492: END
                   1493:         $parmcount ++;
                   1494:     }
                   1495:     $r->print(<<"END");
                   1496:       </table>
                   1497:      </td>
                   1498:     </tr>
                   1499:    </table>
                   1500:   </td>
                   1501:  </tr>
                   1502: </table>
                   1503: <br />
                   1504: <br />
                   1505: END
                   1506:     return $parmcount;
                   1507: }
                   1508: 
                   1509: sub display_addblocker_table {
                   1510:     my ($r,$parmcount,$ltext) = @_;
                   1511:     my $start = time;
                   1512:     my $end = $start + (60 * 60 * 2); #Default is an exam of 2 hours duration.
                   1513:     my $onchange = 'onFocus="javascript:window.document.forms['.
                   1514:                    "'blockform'].elements['add_".$parmcount."'].".
                   1515:                    'checked=true;"';
                   1516:     my $startform = &Apache::lonhtmlcommon::date_setter('blockform','startdate_'.$parmcount,$start,$onchange);
                   1517:     my $endform = &Apache::lonhtmlcommon::date_setter('blockform','enddate_'.$parmcount,$end,$onchange);
                   1518:     my $function = &Apache::loncommon::get_users_function();
                   1519:     my $color = &Apache::loncommon::designparm($function.'.tabbg',
                   1520:                                                     $ENV{'user.domain'});
                   1521:     my %lt = &Apache::lonlocal::texthash(
                   1522:         'addb' => 'Add block',
                   1523:         'exam' => 'e.g., Exam 1',
                   1524:         'addn' => 'Add new communication blocking periods'
                   1525:     );
                   1526:     $r->print(<<"END");
                   1527: <h4>$lt{'addn'}</h4> 
                   1528: <table border="0" cellpadding="0" cellspacing="0">
                   1529:  <tr>
                   1530:   <td width="100%" bgcolor="#000000">
                   1531:    <table width="100%" border="0" cellpadding="1" cellspacing="0">
                   1532:     <tr>
                   1533:      <td width="100%" bgcolor="#000000">
                   1534:       <table border="0" cellpadding="3" cellspacing="3" bgcolor="#FFFFFF">
                   1535:        <tr bgcolor="#CCCCFF">
                   1536:         <td><b>$$ltext{'dura'}</b></td>
                   1537:         <td><b>$$ltext{'even'} $lt{'exam'}</b></td>
                   1538:         <td><b>$$ltext{'actn'}?</b></td>
                   1539:        </tr>
                   1540:        <tr bgcolor="#eeeeee">
                   1541:         <td>$$ltext{'star'}:&nbsp;$startform<br />$$ltext{'endd'}:&nbsp;&nbsp;$endform</td>
                   1542:         <td><input type="text" name="title_$parmcount" size="15" value=""/></td>
                   1543:         <td>$lt{'addb'}?&nbsp;<input type="checkbox" name="add_$parmcount" value="1"/></td>
                   1544:        </tr>
                   1545:       </table>
                   1546:      </td>
                   1547:     </tr>
                   1548:    </table>
                   1549:   </td>
                   1550:  </tr>
                   1551: </table>
                   1552: END
                   1553:     return;
                   1554: }
                   1555: 
                   1556: sub blockcheck {
                   1557:     my ($setters,$startblock,$endblock) = @_;
                   1558:     # Retrieve active student roles and active course coordinator/instructor roles
                   1559:     my @livecses = ();
                   1560:     my @staffcses = ();
                   1561:     $$startblock = 0;
                   1562:     $$endblock = 0;
                   1563:     foreach (keys %ENV) {
                   1564:         if ($_ =~ m-^user\.role\.(st|cc|in)\./(.+)$-) {
                   1565:             my $role = $1;
                   1566:             my $cse = $2;
                   1567:             $cse =~ s|/|_|;
                   1568:             if ($ENV{$_} =~ m/^(\d*)\.(\d*)$/) {
                   1569:                 unless (($2 > 0 && $2 < time) || ($1 > time)) {
                   1570:                     if ($role eq 'st') {
                   1571:                         push @livecses, $cse;
                   1572:                     } else {
                   1573:                         unless (grep/^$cse$/,@staffcses) {
                   1574:                             push @staffcses, $cse;
                   1575:                         }
                   1576:                     }
                   1577:                 }
                   1578:             }
                   1579:         } elsif ($_ =~ m-user\.role\.cr/(\w+)/(\w+)/([^/]+)\./(.+)$- ) { 
                   1580:             my $rolepriv = $ENV{'user.role..rolesdef_'.$3};
                   1581:         }
                   1582:     }
                   1583:     # Retrieve blocking times and identity of blocker for active courses for students.
                   1584:     if (@livecses > 0) {
                   1585:         foreach my $cse (@livecses) {
                   1586:             my ($cdom,$crs) = split/_/,$cse;
                   1587:             if ( (grep/^$cse$/,@staffcses) && ($ENV{'request.role'} !~ m-^st\./$cdom/$crs$-) ) {
                   1588:                 next;
                   1589:             } else {
                   1590:                 %{$$setters{$cse}} = ();
                   1591:                 @{$$setters{$cse}{'staff'}} = ();
                   1592:                 @{$$setters{$cse}{'times'}} = ();
                   1593:                 my %records = &Apache::lonnet::dump('comm_block',$cdom,$crs);
                   1594:                 foreach (keys %records) {
                   1595:                     if ($_ =~ m/^(\d+)____(\d+)$/) {
                   1596:                         if ($1 <= time && $2 >= time) {
                   1597:                             my ($staff,$title) = split/:/,$records{$_};
                   1598:                             push @{$$setters{$cse}{'staff'}}, $staff;
                   1599:                             push @{$$setters{$cse}{'times'}}, $_;
                   1600:                             if ( ($$startblock == 0) || ($$startblock > $1) ) {
                   1601:                                 $$startblock = $1;
                   1602:                             }
                   1603:                             if ( ($$endblock == 0) || ($$endblock < $2) ) {
                   1604:                                 $$endblock = $2;
                   1605:                             }
                   1606:                         }
                   1607:                     }
                   1608:                 }
                   1609:             }
                   1610:         }
                   1611:     }
                   1612: }
                   1613: 
                   1614: sub build_block_table {
                   1615:     my ($r,$startblock,$endblock,$setters) = @_;
                   1616:     my $function = &Apache::loncommon::get_users_function();
                   1617:     my $color = &Apache::loncommon::designparm($function.'.tabbg',
                   1618:                                                     $ENV{'user.domain'});
                   1619:     my %lt = &Apache::lonlocal::texthash(
                   1620:         'cacb' => 'Currently active communication blocks',
                   1621:         'cour' => 'Course',
                   1622:         'dura' => 'Duration',
                   1623:         'blse' => 'Block set by'
                   1624:     ); 
                   1625:     $r->print(<<"END");
                   1626: <br /<br />$lt{'cacb'}:<br /><br />
                   1627: <table border="0" cellpadding="0" cellspacing="0">
                   1628:  <tr>
                   1629:   <td width="100%" bgcolor="#000000">
                   1630:    <table width="100%" border="0" cellpadding="1" cellspacing="0">
                   1631:     <tr>
                   1632:      <td width="100%" bgcolor="#000000">
                   1633:       <table border="0" cellpadding="3" cellspacing="3" bgcolor="#FFFFFF">
                   1634:        <tr bgcolor="$color">
                   1635:         <td><b>$lt{'cour'}</b></td>
                   1636:         <td><b>$lt{'dura'}</b></td>
                   1637:         <td><b>$lt{'blse'}</b></td>
                   1638:        </tr>
                   1639: END
                   1640:     foreach (keys %{$setters}) {
                   1641:         my %courseinfo=&Apache::lonnet::coursedescription($_);
                   1642:         for (my $i=0; $i<@{$$setters{$_}{staff}}; $i++) {
                   1643:             my ($uname,$udom) = split/\@/,$$setters{$_}{staff}[$i];
                   1644:             my $fullname = &Apache::loncommon::plainname($uname,$udom);
                   1645:             my ($openblock,$closeblock) = split/____/,$$setters{$_}{times}[$i];
                   1646:             $openblock = &Apache::lonlocal::locallocaltime($openblock);
                   1647:             $closeblock= &Apache::lonlocal::locallocaltime($closeblock);
                   1648:             $r->print('<tr><td>'.$courseinfo{'description'}.'</td>'.
                   1649:                       '<td>'.$openblock.' to '.$closeblock.'</td>'.
                   1650:                       '<td>'.$fullname.' ('.$uname.'@'.$udom.
                   1651:                       ')</td></tr>');
                   1652:         }
                   1653:     }
                   1654:     $r->print('</table></td></tr></table></td></tr></table>');
                   1655: }
                   1656: 
1.90      www      1657: # ----------------------------------------------------------- Display a message
                   1658: 
                   1659: sub displaymessage {
1.106     www      1660:     my ($r,$msgid,$folder)=@_;
                   1661:     my $suffix=&foldersuffix($folder);
1.101     raeburn  1662:     my %blocked = ();
                   1663:     my %setters = ();
                   1664:     my $startblock = 0;
                   1665:     my $endblock = 0;
                   1666:     my $numblocked = 0;
                   1667: # info to generate "next" and "previous" buttons and check if message is blocked
                   1668:     &blockcheck(\%setters,\$startblock,\$endblock);
1.107     www      1669:     my @messages=&sortedmessages(\%blocked,$startblock,$endblock,\$numblocked,$folder);
1.101     raeburn  1670:     if ( $blocked{$msgid} eq 'ON' ) {
                   1671:         &printheader($r,'/adm/email',&mt('Display a Message'));
                   1672:         $r->print(&mt('You attempted to display a message that is currently blocked because you are enrolled in one or more courses for which there is an ongoing online exam.'));
                   1673:         &build_block_table($r,$startblock,$endblock,\%setters);
                   1674:         return;
                   1675:     }
1.107     www      1676:     &statuschange($msgid,'read',$folder);
1.106     www      1677:     my %message=&Apache::lonnet::get('nohist_email'.$suffix,[$msgid]);
1.90      www      1678:     my %content=&unpackagemsg($message{$msgid});
1.107     www      1679: 
1.90      www      1680:     my $counter=0;
                   1681:     $r->print('<pre>');
                   1682:     my $escmsgid=&Apache::lonnet::escape($msgid);
                   1683:     foreach (@messages) {
                   1684: 	if ($_->[5] eq $escmsgid){
                   1685: 	    last;
                   1686: 	}
                   1687: 	$counter++;
                   1688:     }
                   1689:     $r->print('</pre>');
                   1690:     my $number_of_messages = scalar(@messages); #subtract 1 for last index
                   1691: # start output
1.92      www      1692:     &printheader($r,'/adm/email?display='.&Apache::lonnet::escape($msgid),'Display a Message','',$content{'baseurl'});
1.90      www      1693:     my %courseinfo=&Apache::lonnet::coursedescription($content{'courseid'});
                   1694: # Functions
                   1695:     $r->print('<table border="2" width="100%"><tr bgcolor="#FFFFAA"><td>'.&mt('Functions').':</td>'.
                   1696: 	      '<td><a href="/adm/email?replyto='.&Apache::lonnet::escape($msgid).$sqs.
                   1697: 	      '"><b>'.&mt('Reply').'</b></a></td>'.
                   1698: 	      '<td><a href="/adm/email?forward='.&Apache::lonnet::escape($msgid).$sqs.
                   1699: 	      '"><b>'.&mt('Forward').'</b></a></td>'.
                   1700: 	      '<td><a href="/adm/email?markunread='.&Apache::lonnet::escape($msgid).$sqs.
                   1701: 	      '"><b>'.&mt('Mark Unread').'</b></a></td>'.
                   1702: 	      '<td><a href="/adm/email?markdel='.&Apache::lonnet::escape($msgid).$sqs.
                   1703: 	      '"><b>Delete</b></a></td>'.
                   1704: 	      '<td><a href="/adm/email?sortedby='.$ENV{'form.sortedby'}.
1.107     www      1705: 	      '&folder='.&Apache::lonnet::escape($folder).
1.90      www      1706: 	      '"><b>'.&mt('Display all Messages').'</b></a></td>');
                   1707:     if ($counter > 0){
                   1708: 	$r->print('<td><a href="/adm/email?display='.$messages[$counter-1]->[5].$sqs.
                   1709: 		  '"><b>'.&mt('Previous').'</b></a></td>');
                   1710:     }
                   1711:     if ($counter < $number_of_messages - 1){
                   1712: 	$r->print('<td><a href="/adm/email?display='.$messages[$counter+1]->[5].$sqs.
                   1713: 		  '"><b>'.&mt('Next').'</b></a></td>');
                   1714:     }
                   1715:     $r->print('</tr></table>');
                   1716:     $r->print('<br /><b>'.&mt('Subject').':</b> '.$content{'subject'}.
1.108     www      1717: 	      ($folder ne 'sent'?'<br /><b>'.&mt('From').':</b> '.
1.90      www      1718: 	      &Apache::loncommon::aboutmewrapper(
                   1719: 						 &Apache::loncommon::plainname($content{'sendername'},$content{'senderdomain'}),
                   1720: 						 $content{'sendername'},$content{'senderdomain'}).' ('.
                   1721: 	      $content{'sendername'}.' at '.
1.108     www      1722: 	      $content{'senderdomain'}.') ':'<br /><b>'.&mt('To').':</b> '.
                   1723: 	      &Apache::loncommon::aboutmewrapper(
                   1724: 						 &Apache::loncommon::plainname($content{'recuser'},$content{'recdomain'}),
                   1725: 						 $content{'recuser'},$content{'recdomain'}).' ('.
                   1726: 	      $content{'recuser'}.' at '.
                   1727: 	      $content{'recdomain'}.') ').
1.90      www      1728: 	      ($content{'courseid'}?'<br /><b>'.&mt('Course').':</b> '.$courseinfo{'description'}.
                   1729: 	       ($content{'coursesec'}?' ('.&mt('Group/Section').': '.$content{'coursesec'}.')':''):'').
                   1730: 	      '<br /><b>'.&mt('Time').':</b> '.$content{'time'}.
                   1731: 	      '<p><pre>'.
                   1732: 	      &Apache::lontexconvert::msgtexconverted($content{'message'},1).
1.111     www      1733: 	      '</pre><hr />'.&displayresource(%content).'</p>');
1.90      www      1734:     return;   
                   1735: }
1.44      www      1736: 
1.111     www      1737: # =========================================================== Show the citation
                   1738: 
                   1739: sub displayresource {
                   1740:     my %content=@_;
                   1741: #
                   1742: # If the recipient is in the same course that the message was sent from and
                   1743: # has sufficient privileges, show "all details," else show citation
                   1744: #
                   1745:     if (($ENV{'request.course.id'} eq $content{'courseid'})
                   1746:      && (&Apache::lonnet::allowed('vgr',$content{'courseid'}))) {
                   1747: 	my $symb=&Apache::lonnet::symbread($content{'baseurl'});
                   1748: # Could not get a symb, give up
                   1749: 	unless ($symb) { return $content{'citation'}; }
                   1750: # Have a symb, can render
                   1751: 	return '<h2>'.&mt('Current attempts of student (if applicable)').'</h2>'.
                   1752: 	    &Apache::loncommon::get_previous_attempt($symb,
                   1753: 						     $content{'sendername'},
                   1754: 						     $content{'senderdomain'},
                   1755: 						     $content{'courseid'}).
                   1756: 	    '<hr /><h2>'.&mt('Current screen output (if applicable)').'</h2>'.
                   1757: 	    &Apache::loncommon::get_student_view($symb,
                   1758: 						 $content{'sendername'},
                   1759: 						 $content{'senderdomain'},
                   1760: 						 $content{'courseid'}).
                   1761: 	    '<h2>'.&mt('Correct Answer(s) (if applicable)').'</h2>'.
                   1762: 	    &Apache::loncommon::get_student_answers($symb,
                   1763: 						    $content{'sendername'},
                   1764: 						    $content{'senderdomain'},
                   1765: 						    $content{'courseid'});
                   1766:     } else {
                   1767: 	return $content{'citation'};
                   1768:     }
                   1769: }
                   1770: 
1.88      www      1771: # ================================================================== The Header
                   1772: 
                   1773: sub header {
1.90      www      1774:     my ($r,$title,$baseurl)=@_;
1.88      www      1775:     $r->print('<html><head><title>Communication and Messages</title>');
                   1776:     if ($baseurl) {
                   1777: 	$r->print("<base href=\"http://$ENV{'SERVER_NAME'}/$baseurl\" />");
                   1778:     }
                   1779:     $r->print(&Apache::loncommon::studentbrowser_javascript().'</head>'.
                   1780: 	      &Apache::loncommon::bodytag('Communication and Messages'));
                   1781:         $r->print(&Apache::lonhtmlcommon::breadcrumbs
1.90      www      1782:                   (undef,($title?$title:'Communication and Messages')));
1.88      www      1783: 
                   1784: }
                   1785: 
1.90      www      1786: # ---------------------------------------------------------------- Print header
                   1787: 
                   1788: sub printheader {
                   1789:     my ($r,$url,$desc,$title,$baseurl)=@_;
                   1790:     &Apache::lonhtmlcommon::add_breadcrumb
                   1791: 	({href=>$url,
                   1792: 	  text=>$desc});
                   1793:     &header($r,$title,$baseurl);
                   1794: }
                   1795: 
                   1796: 
1.13      www      1797: # ===================================================================== Handler
                   1798: 
1.5       www      1799: sub handler {
                   1800:     my $r=shift;
                   1801: 
                   1802: # ----------------------------------------------------------- Set document type
1.87      www      1803:     
                   1804:     &Apache::loncommon::content_type($r,'text/html');
                   1805:     $r->send_http_header;
                   1806:     
                   1807:     return OK if $r->header_only;
                   1808:     
1.6       www      1809: # --------------------------- Get query string for limited number of parameters
1.32      matthew  1810:     &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'},
                   1811:         ['display','replyto','forward','markread','markdel','markunread',
1.44      www      1812:          'sendreply','compose','sendmail','critical','recname','recdom',
1.108     www      1813:          'recordftf','sortedby','block','folder','startdis','interdis']);
                   1814:     $sqs='&sortedby='.$ENV{'form.sortedby'}.
                   1815: 	 '&startdis='.$ENV{'form.startdis'}.
                   1816: 	 '&interdis='.$ENV{'form.interdis'};
                   1817: 
1.40      www      1818: # ------------------------------------------------------ They checked for email
1.101     raeburn  1819:     unless ($ENV{'form.block'}) {
                   1820:         &Apache::lonnet::put('email_status',{'recnewemail'=>0});
                   1821:     }
1.88      www      1822: 
                   1823: # ----------------------------------------------------------------- Breadcrumbs
                   1824: 
                   1825:     &Apache::lonhtmlcommon::clear_breadcrumbs();
                   1826:     &Apache::lonhtmlcommon::add_breadcrumb
                   1827:         ({href=>"/adm/communicate",
                   1828:           text=>"Communication/Messages",
                   1829:           faq=>12,bug=>'Communication Tools',});
                   1830: 
1.106     www      1831: # ------------------------------------------------------------------ Get Folder
                   1832: 
                   1833:     my $folder=$ENV{'form.folder'};
                   1834:     unless ($folder) { 
                   1835: 	$folder=''; 
                   1836:     } else {
                   1837: 	$sqs='&folder='.&Apache::lonnet::escape($folder);
                   1838:     }
                   1839: 
1.108     www      1840: # --------------------------------------------------------------------- Display
                   1841: 
                   1842:     $startdis=$ENV{'form.startdis'};
                   1843:     unless ($startdis) { $startdis=0; }
                   1844:     $interdis=$ENV{'form.interdis'};
                   1845:     unless ($interdis) { $interdis=20; }
                   1846: 
1.5       www      1847: # --------------------------------------------------------------- Render Output
1.88      www      1848: 
1.87      www      1849:     if ($ENV{'form.display'}) {
1.106     www      1850: 	&displaymessage($r,$ENV{'form.display'},$folder);
1.87      www      1851:     } elsif ($ENV{'form.replyto'}) {
1.108     www      1852: 	&compout($r,'',$ENV{'form.replyto'},undef,undef,$folder);
1.87      www      1853:     } elsif ($ENV{'form.confirm'}) {
1.92      www      1854: 	&printheader($r,'','Confirmed Receipt');
1.87      www      1855: 	foreach (keys %ENV) {
                   1856: 	    if ($_=~/^form\.rec\_(.*)$/) {
1.92      www      1857: 		$r->print('<b>'.&mt('Confirming Receipt').':</b> '.
1.87      www      1858: 			  &user_crit_received($1).'<br>');
                   1859: 	    }
                   1860: 	    if ($_=~/^form\.reprec\_(.*)$/) {
                   1861: 		my $msgid=$1;
1.92      www      1862: 		$r->print('<b>'.&mt('Confirming Receipt').':</b> '.
1.87      www      1863: 			  &user_crit_received($msgid).'<br>');
1.94      www      1864: 		&compout($r,'','','',$msgid);
1.87      www      1865: 	    }
                   1866: 	}
                   1867: 	&discrit($r);
                   1868:     } elsif ($ENV{'form.critical'}) {
1.92      www      1869: 	&printheader($r,'','Displaying Critical Messages');
1.87      www      1870: 	&discrit($r);
                   1871:     } elsif ($ENV{'form.forward'}) {
                   1872: 	&compout($r,$ENV{'form.forward'});
                   1873:     } elsif ($ENV{'form.markdel'}) {
1.92      www      1874: 	&printheader($r,'','Deleted Message');
1.106     www      1875: 	&statuschange($ENV{'form.markdel'},'deleted',$folder);
                   1876: 	&disall($r,$folder);
                   1877:     } elsif ($ENV{'form.markedmove'}) {
                   1878: 	my $total=0;
                   1879: 	foreach (keys %ENV) {
                   1880: 	    if ($_=~/^form\.delmark_(.*)$/) {
                   1881: 		&movemsg(&Apache::lonnet::unescape($1),$folder,
                   1882: 			 $ENV{'form.movetofolder'});
                   1883: 		$total++;
                   1884: 	    }
                   1885: 	}
                   1886: 	&printheader($r,'','Moved Messages');
                   1887: 	$r->print('Moved '.$total.' message(s)<p>');
                   1888: 	&disall($r,$folder);
1.87      www      1889:     } elsif ($ENV{'form.markeddel'}) {
                   1890: 	my $total=0;
                   1891: 	foreach (keys %ENV) {
                   1892: 	    if ($_=~/^form\.delmark_(.*)$/) {
1.108     www      1893: 		&statuschange(&Apache::lonnet::unescape($1),'deleted',$folder);
1.87      www      1894: 		$total++;
                   1895: 	    }
                   1896: 	}
1.92      www      1897: 	&printheader($r,'','Deleted Messages');
1.87      www      1898: 	$r->print('Deleted '.$total.' message(s)<p>');
1.106     www      1899: 	&disall($r,$folder);
1.87      www      1900:     } elsif ($ENV{'form.markunread'}) {
1.92      www      1901: 	&printheader($r,'','Marked Message as Unread');
1.87      www      1902: 	&statuschange($ENV{'form.markunread'},'new');
1.106     www      1903: 	&disall($r,$folder);
1.87      www      1904:     } elsif ($ENV{'form.compose'}) {
1.92      www      1905: 	&compout($r,'','',$ENV{'form.compose'});
1.87      www      1906:     } elsif ($ENV{'form.recordftf'}) {
                   1907: 	&facetoface($r,$ENV{'form.recordftf'});
1.101     raeburn  1908:     } elsif ($ENV{'form.block'}) {
                   1909:         &examblock($r,$ENV{'form.block'});
1.87      www      1910:     } elsif ($ENV{'form.sendmail'}) {
                   1911: 	my $sendstatus='';
                   1912: 	if ($ENV{'form.send'}) {
1.92      www      1913: 	    &printheader($r,'','Messages being sent.');
                   1914: 	    $r->rflush();
1.87      www      1915: 	    my %content=();
                   1916: 	    undef %content;
                   1917: 	    if ($ENV{'form.forwid'}) {
                   1918: 		my $msgid=$ENV{'form.forwid'};
                   1919: 		my %message=&Apache::lonnet::get('nohist_email',[$msgid]);
                   1920: 		%content=&unpackagemsg($message{$msgid},1);
                   1921: 		&statuschange($msgid,'forwarded');
                   1922: 		$ENV{'form.message'}.="\n\n-- Forwarded message --\n\n".
                   1923: 		    $content{'message'};
                   1924: 	    }
1.105     albertel 1925: 	    if ($ENV{'form.replyid'}) {
                   1926: 		my $msgid=$ENV{'form.replyid'};
                   1927: 		my %message=&Apache::lonnet::get('nohist_email',[$msgid]);
                   1928: 		%content=&unpackagemsg($message{$msgid},1);
                   1929: 		&statuschange($msgid,'replied');
                   1930: 	    }
1.87      www      1931: 	    my %toaddr=();
                   1932: 	    undef %toaddr;
                   1933: 	    if ($ENV{'form.sendmode'} eq 'group') {
                   1934: 		foreach (keys %ENV) {
                   1935: 		    if ($_=~/^form\.send\_to\_\&\&\&[^\&]*\&\&\&\_(.+)$/) {
                   1936: 			$toaddr{$1}='';
                   1937: 		    }
                   1938: 		}
                   1939: 	    } elsif ($ENV{'form.sendmode'} eq 'upload') {
                   1940: 		foreach (split(/[\n\r\f]+/,$ENV{'form.upfile'})) {
                   1941: 		    my ($rec,$txt)=split(/\s*\:\s*/,$_);
                   1942: 		    if ($txt) {
                   1943: 			$rec=~s/\@/\:/;
                   1944: 			$toaddr{$rec}.=$txt."\n";
                   1945: 		    }
                   1946: 		}
                   1947: 	    } else {
                   1948: 		$toaddr{$ENV{'form.recuname'}.':'.$ENV{'form.recdomain'}}='';
                   1949: 	    }
                   1950: 	    if ($ENV{'form.additionalrec'}) {
                   1951: 		foreach (split(/\,/,$ENV{'form.additionalrec'})) {
                   1952: 		    my ($auname,$audom)=split(/\@/,$_);
                   1953: 		    $toaddr{$auname.':'.$audom}='';
                   1954: 		}
                   1955: 	    }
1.92      www      1956: 
1.87      www      1957: 	    foreach (keys %toaddr) {
                   1958: 		my ($recuname,$recdomain)=split(/\:/,$_);
                   1959: 		my $msgtxt=&Apache::lonfeedback::clear_out_html($ENV{'form.message'});
1.92      www      1960: 		if ($toaddr{$_}) { $msgtxt.='<hr />'.$toaddr{$_}; }
                   1961: 		my $thismsg;    
1.87      www      1962: 		if ((($ENV{'form.critmsg'}) || ($ENV{'form.sendbck'})) && 
                   1963: 		    (&Apache::lonnet::allowed('srm',$ENV{'request.course.id'}))) {
1.92      www      1964: 		    $r->print(&mt('Sending critical message').' '.$recuname.'@'.$recdomain.': ');
                   1965: 		    $thismsg=&user_crit_msg($recuname,$recdomain,
1.87      www      1966: 						    &Apache::lonfeedback::clear_out_html($ENV{'form.subject'}),
                   1967: 						    $msgtxt,
                   1968: 						    $ENV{'form.sendbck'});
                   1969: 		} else {
1.92      www      1970: 		    $r->print(&mt('Sending').' '.$recuname.'@'.$recdomain.': ');
                   1971: 		    $thismsg=&user_normal_msg($recuname,$recdomain,
1.87      www      1972: 						      &Apache::lonfeedback::clear_out_html($ENV{'form.subject'}),
                   1973: 						      $msgtxt,
                   1974: 						      $content{'citation'});
1.102     raeburn  1975:                     if (($ENV{'request.course.id'}) && ($ENV{'form.sendmode'} eq 'group')) {
                   1976:                         &user_normal_msg_raw(
                   1977:                         $ENV{'course.'.$ENV{'request.course.id'}.'.num'},
                   1978:                         $ENV{'course.'.$ENV{'request.course.id'}.'.domain'},
                   1979:                         'Broadcast ['.$recuname.':'.$recdomain.']',
                   1980:                         $msgtxt);
                   1981:                     }
1.87      www      1982: 		}
1.92      www      1983: 		$r->print($thismsg.'<br />');
                   1984: 		$sendstatus.=' '.$thismsg;
1.87      www      1985: 	    }
1.95      www      1986: 	} else {
                   1987: 	    &printheader($r,'','No messages sent.'); 
1.87      www      1988: 	}
                   1989: 	if ($sendstatus=~/^(\s*(?:ok|con_delayed)\s*)*$/) {
                   1990: 	    $r->print('<br /><font color="green">'.&mt('Completed.').'</font>');
                   1991: 	    if ($ENV{'form.displayedcrit'}) {
                   1992: 		&discrit($r);
                   1993: 	    } else {
1.95      www      1994: 		&Apache::loncommunicate::menu($r);
1.87      www      1995: 	    }
                   1996: 	} else {
                   1997: 	    $r->print(
                   1998: 		      '<h2><font color="red">'.&mt('Could not deliver message').'</font></h2>'.
                   1999: 		      &mt('Please use the browser "Back" button and correct the recipient addresses')
                   2000: 		      );
                   2001: 	}
1.106     www      2002:     } elsif ($ENV{'form.newfolder'}) {
                   2003: 	&printheader($r,'','New Folder');
                   2004: 	&makefolder($ENV{'form.newfolder'});
                   2005: 	&disall($r,$ENV{'form.newfolder'});
1.87      www      2006:     } else {
1.92      www      2007: 	&printheader($r,'','Display All Messages');
1.106     www      2008: 	&disall($r,$folder);
1.87      www      2009:     }
                   2010:     $r->print('</body></html>');
                   2011:     return OK;
1.5       www      2012: }
1.2       www      2013: # ================================================= Main program, reset counter
                   2014: 
1.27      www      2015: BEGIN {
1.2       www      2016:     $msgcount=0;
1.1       www      2017: }
1.58      bowersj2 2018: 
                   2019: =pod
                   2020: 
                   2021: =back
                   2022: 
1.59      bowersj2 2023: =cut
                   2024: 
                   2025: 1; 
1.1       www      2026: 
                   2027: __END__
                   2028: 
                   2029: 
                   2030: 
                   2031: 
                   2032: 
                   2033: 
                   2034: 

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