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

1.1       www         1: # The LearningOnline Network with CAPA
1.26      albertel    2: # Routines for messaging
                      3: #
1.173   ! albertel    4: # $Id: lonmsg.pm,v 1.172 2006/01/10 15:29:13 albertel 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
1.132     www        93: are much more useful than traditional email can be made to be, even
1.58      bowersj2   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: 
1.162     raeburn   100: The routines used to package messages and unpackage messages are not
                    101: only used by lonmsg when creating/extracting messages for LON-CAPA's
                    102: internal messaging system, but also by lonnotify.pm which is available
                    103: for use by Domain Coordinators to broadcast standard e-mail to specified
                    104: users in their domain.  The XML packaging used in the two cases is very
                    105: similar.  The differences are the use of <recuser>$uname</recuser> and 
                    106: <recdomain>$udom</recdomain> in stored internal messages, compared 
                    107: with <recipient username="$uname:$udom">$email</recipient> in stored
1.163     albertel  108: Domain Coordinator e-mail for the storage of information about 
1.162     raeburn   109: recipients of the message/e-mail.
                    110: 
1.58      bowersj2  111: =head1 FUNCTIONS
                    112: 
                    113: =over 4
                    114: 
                    115: =cut
                    116: 
1.1       www       117: use strict;
1.140     albertel  118: use Apache::lonnet;
1.2       www       119: use vars qw($msgcount);
1.47      albertel  120: use HTML::TokeParser();
1.5       www       121: use Apache::Constants qw(:common);
1.47      albertel  122: use Apache::loncommon();
                    123: use Apache::lontexconvert();
                    124: use HTML::Entities();
1.53      www       125: use Mail::Send;
1.67      www       126: use Apache::lonlocal;
1.95      www       127: use Apache::loncommunicate;
1.153     www       128: use Apache::lonfeedback;
1.154     www       129: use Apache::lonrss();
1.1       www       130: 
1.65      www       131: # Querystring component with sorting type
                    132: my $sqs;
1.108     www       133: my $startdis;
                    134: my $interdis;
1.65      www       135: 
1.1       www       136: # ===================================================================== Package
                    137: 
1.3       www       138: sub packagemsg {
1.108     www       139:     my ($subject,$message,$citation,$baseurl,$attachmenturl,
1.162     raeburn   140: 	$recuser,$recdomain,$msgid,$type)=@_;
1.96      albertel  141:     $message =&HTML::Entities::encode($message,'<>&"');
                    142:     $citation=&HTML::Entities::encode($citation,'<>&"');
                    143:     $subject =&HTML::Entities::encode($subject,'<>&"');
1.49      albertel  144:     #remove machine specification
                    145:     $baseurl =~ s|^http://[^/]+/|/|;
1.96      albertel  146:     $baseurl =&HTML::Entities::encode($baseurl,'<>&"');
1.51      www       147:     #remove machine specification
                    148:     $attachmenturl =~ s|^http://[^/]+/|/|;
1.96      albertel  149:     $attachmenturl =&HTML::Entities::encode($attachmenturl,'<>&"');
1.158     raeburn   150:     my $course_context;
                    151:     if (defined($env{'form.replyid'})) {
                    152:         my ($sendtime,$shortsubj,$fromname,$fromdomain,$count,$origcid)=
                    153:                    split(/\:/,&Apache::lonnet::unescape($env{'form.replyid'}));
                    154:         $course_context = $origcid;
                    155:     }
1.160     raeburn   156:     foreach my $key (keys(%env)) {
                    157:         if ($key=~/^form\.(rep)?rec\_(.*)$/) {
                    158:             my ($sendtime,$shortsubj,$fromname,$fromdomain,$count,$origcid) =
                    159:                                     split(/\:/,&Apache::lonnet::unescape($2));
                    160:             $course_context = $origcid;
                    161:             last;
                    162:         }
                    163:     }
1.158     raeburn   164:     unless(defined($course_context)) {
                    165:         $course_context = $env{'request.course.id'};
                    166:     }
1.2       www       167:     my $now=time;
                    168:     $msgcount++;
1.156     raeburn   169:     unless(defined($msgid)) {
1.159     raeburn   170:         $msgid = &buildmsgid($now,$subject,$env{'user.name'},$env{'user.domain'},
                    171:                             $msgcount,$course_context,$$);
1.156     raeburn   172:     }
1.140     albertel  173:     my $result='<sendername>'.$env{'user.name'}.'</sendername>'.
                    174:            '<senderdomain>'.$env{'user.domain'}.'</senderdomain>'.
1.1       www       175:            '<subject>'.$subject.'</subject>'.
1.67      www       176: 	   '<time>'.&Apache::lonlocal::locallocaltime($now).'</time>'.
1.1       www       177: 	   '<servername>'.$ENV{'SERVER_NAME'}.'</servername>'.
                    178:            '<host>'.$ENV{'HTTP_HOST'}.'</host>'.
                    179: 	   '<client>'.$ENV{'REMOTE_ADDR'}.'</client>'.
1.140     albertel  180: 	   '<browsertype>'.$env{'browser.type'}.'</browsertype>'.
                    181: 	   '<browseros>'.$env{'browser.os'}.'</browseros>'.
                    182: 	   '<browserversion>'.$env{'browser.version'}.'</browserversion>'.
                    183:            '<browsermathml>'.$env{'browser.mathml'}.'</browsermathml>'.
1.1       www       184: 	   '<browserraw>'.$ENV{'HTTP_USER_AGENT'}.'</browserraw>'.
1.158     raeburn   185: 	   '<courseid>'.$course_context.'</courseid>'.
1.140     albertel  186: 	   '<coursesec>'.$env{'request.course.sec'}.'</coursesec>'.
                    187: 	   '<role>'.$env{'request.role'}.'</role>'.
                    188: 	   '<resource>'.$env{'request.filename'}.'</resource>'.
1.156     raeburn   189:            '<msgid>'.$msgid.'</msgid>';
                    190:     if (ref($recuser) eq 'ARRAY') {
                    191:         for (my $i=0; $i<@{$recuser}; $i++) {
1.162     raeburn   192:             if ($type eq 'dcmail') {
                    193:                 my ($username,$email) = split(/:/,$$recuser[$i]);
                    194:                 $username = &Apache::lonnet::unescape($username);
                    195:                 $email = &Apache::lonnet::unescape($email);
                    196:                 $username = &HTML::Entities::encode($username,'<>&"');
                    197:                 $email = &HTML::Entities::encode($email,'<>&"');
                    198:                 $result .= '<recipient username="'.$username.'">'.
                    199:                                             $email.'</recipient>';
                    200:             } else {
                    201:                 $result .= '<recuser>'.$$recuser[$i].'</recuser>'.
                    202:                            '<recdomain>'.$$recdomain[$i].'</recdomain>';
                    203:             }
1.156     raeburn   204:         }
                    205:     } else {
                    206:         $result .= '<recuser>'.$recuser.'</recuser>'.
                    207:                    '<recdomain>'.$recdomain.'</recdomain>';
                    208:     }
                    209:     $result .= '<message>'.$message.'</message>';
1.49      albertel  210:     if (defined($citation)) {
                    211: 	$result.='<citation>'.$citation.'</citation>';
                    212:     }
                    213:     if (defined($baseurl)) {
                    214: 	$result.= '<baseurl>'.$baseurl.'</baseurl>';
                    215:     }
1.51      www       216:     if (defined($attachmenturl)) {
1.52      www       217: 	$result.= '<attachmenturl>'.$attachmenturl.'</attachmenturl>';
1.51      www       218:     }
1.49      albertel  219:     return $msgid,$result;
1.1       www       220: }
                    221: 
1.2       www       222: # ================================================== Unpack message into a hash
                    223: 
1.3       www       224: sub unpackagemsg {
1.52      www       225:     my ($message,$notoken)=@_;
1.2       www       226:     my %content=();
                    227:     my $parser=HTML::TokeParser->new(\$message);
                    228:     my $token;
                    229:     while ($token=$parser->get_token) {
                    230:        if ($token->[0] eq 'S') {
                    231: 	   my $entry=$token->[1];
                    232:            my $value=$parser->get_text('/'.$entry);
1.156     raeburn   233:            if (($entry eq 'recuser') || ($entry eq 'recdomain')) {
                    234:                push(@{$content{$entry}},$value);
1.162     raeburn   235:            } elsif ($entry eq 'recipient') {
                    236:                my $username = $token->[2]{'username'};
                    237:                $username = &HTML::Entities::decode($username,'<>&"');
                    238:                $content{$entry}{$username} = $value;
1.156     raeburn   239:            } else {
                    240:                $content{$entry}=$value;
                    241:            }
1.2       www       242:        }
                    243:     }
1.168     albertel  244:     if (!exists($content{'recuser'})) { $content{'recuser'} = []; }
1.52      www       245:     if ($content{'attachmenturl'}) {
1.100     albertel  246:        my ($fname)=($content{'attachmenturl'}=~m|/([^/]+)$|);
1.52      www       247:        if ($notoken) {
1.100     albertel  248: 	   $content{'message'}.='<p>'.&mt('Attachment').': <tt>'.$fname.'</tt>';
1.52      www       249:        } else {
1.99      albertel  250: 	   &Apache::lonnet::allowuploaded('/adm/msg',
                    251: 					  $content{'attachmenturl'});
                    252: 	   $content{'message'}.='<p>'.&mt('Attachment').
                    253: 	       ': <a href="'.$content{'attachmenturl'}.'"><tt>'.
1.100     albertel  254: 	       $fname.'</tt></a>';
1.52      www       255:        }
                    256:     }
1.2       www       257:     return %content;
                    258: }
                    259: 
1.6       www       260: # ======================================================= Get info out of msgid
                    261: 
1.159     raeburn   262: sub buildmsgid {
                    263:     my ($now,$subject,$uname,$udom,$msgcount,$course_context,$pid) = @_;
                    264:     $subject=&Apache::lonnet::escape($subject);
                    265:     return(&Apache::lonnet::escape($now.':'.$subject.':'.$uname.':'.
                    266:            $udom.':'.$msgcount.':'.$course_context.':'.$pid));
                    267: }
                    268: 
1.6       www       269: sub unpackmsgid {
1.169     albertel  270:     my ($msgid,$folder,$skipstatus,$status_cache)=@_;
1.106     www       271:     $msgid=&Apache::lonnet::unescape($msgid);
1.167     raeburn   272:     my ($sendtime,$shortsubj,$fromname,$fromdomain,$count,$fromcid,
                    273:                      $processid)=split(/\:/,&Apache::lonnet::unescape($msgid));
                    274:     if (!defined($processid)) { $fromcid = ''; }
1.164     raeburn   275:     my %status=();
                    276:     unless ($skipstatus) {
1.169     albertel  277: 	if (ref($status_cache)) {
                    278: 	    $status{$msgid} = $status_cache->{$msgid};
                    279: 	} else {
                    280: 	    my $suffix=&foldersuffix($folder);
                    281: 	    %status=&Apache::lonnet::get('email_status'.$suffix,[$msgid]);
                    282: 	}
                    283: 	if ($status{$msgid}=~/^error\:/) { $status{$msgid}=''; }
1.164     raeburn   284:         unless ($status{$msgid}) { $status{$msgid}='new'; }
                    285:     }
1.141     raeburn   286:     return ($sendtime,$shortsubj,$fromname,$fromdomain,$status{$msgid},$fromcid);
                    287: }
1.6       www       288: 
1.53      www       289: 
                    290: sub sendemail {
                    291:     my ($to,$subject,$body)=@_;
                    292:     $body=
1.67      www       293:     "*** ".&mt('This is an automatic message generated by the LON-CAPA system.')."\n".
                    294:     "*** ".&mt('Please do not reply to this address.')."\n\n".$body;
1.53      www       295:     my $msg = new Mail::Send;
                    296:     $msg->to($to);
                    297:     $msg->subject('[LON-CAPA] '.$subject);
1.97      matthew   298:     if (my $fh = $msg->open()) {
1.172     albertel  299: 	print $fh $body;
                    300: 	$fh->close;
1.68      www       301:     }
1.53      www       302: }
                    303: 
                    304: # ==================================================== Send notification emails
                    305: 
                    306: sub sendnotification {
1.131     www       307:     my ($to,$touname,$toudom,$subj,$crit,$text)=@_;
1.140     albertel  308:     my $sender=$env{'environment.firstname'}.' '.$env{'environment.lastname'};
1.131     www       309:     unless ($sender=~/\w/) { 
1.172     albertel  310: 	$sender=$env{'user.name'}.'@'.$env{'user.domain'};
1.131     www       311:     }
1.53      www       312:     my $critical=($crit?' critical':'');
1.131     www       313:     $text=~s/\&lt\;/\</gs;
                    314:     $text=~s/\&gt\;/\>/gs;
                    315:     $text=~s/\<\/*[^\>]+\>//gs;
1.53      www       316:     my $url='http://'.
                    317:       $Apache::lonnet::hostname{&Apache::lonnet::homeserver($touname,$toudom)}.
1.54      www       318:       '/adm/email?username='.$touname.'&domain='.$toudom;
1.53      www       319:     my $body=(<<ENDMSG);
                    320: You received a$critical message from $sender in LON-CAPA. The subject is
                    321: 
                    322:  $subj
                    323: 
1.131     www       324: === Excerpt ============================================================
                    325: $text
                    326: ========================================================================
                    327: 
1.53      www       328: Use
                    329: 
                    330:  $url
                    331: 
1.131     www       332: to access the full message.
1.53      www       333: ENDMSG
                    334:     &sendemail($to,'New'.$critical.' message from '.$sender,$body);
                    335: }
1.40      www       336: # ============================================================= Check for email
                    337: 
                    338: sub newmail {
1.140     albertel  339:     if ((time-$env{'user.mailcheck.time'})>300) {
1.40      www       340:         my %what=&Apache::lonnet::get('email_status',['recnewemail']);
                    341:         &Apache::lonnet::appenv('user.mailcheck.time'=>time);
                    342:         if ($what{'recnewemail'}>0) { return 1; }
                    343:     }
                    344:     return 0;
                    345: }
                    346: 
1.1       www       347: # =============================== Automated message to the author of a resource
                    348: 
1.58      bowersj2  349: =pod
                    350: 
                    351: =item * B<author_res_msg($filename, $message)>: Sends message $message to the owner
                    352:     of the resource with the URI $filename.
                    353: 
                    354: =cut
                    355: 
1.1       www       356: sub author_res_msg {
                    357:     my ($filename,$message)=@_;
1.2       www       358:     unless ($message) { return 'empty'; }
1.1       www       359:     $filename=&Apache::lonnet::declutter($filename);
1.72      www       360:     my ($domain,$author,@dummy)=split(/\//,$filename);
1.1       www       361:     my $homeserver=&Apache::lonnet::homeserver($author,$domain);
                    362:     if ($homeserver ne 'no_host') {
                    363:        my $id=unpack("%32C*",$message);
1.2       www       364:        my $msgid;
1.72      www       365:        ($msgid,$message)=&packagemsg($filename,$message);
1.3       www       366:        return &Apache::lonnet::reply('put:'.$domain.':'.$author.
1.72      www       367:          ':nohist_res_msgs:'.
                    368:           &Apache::lonnet::escape($filename.'_'.$id).'='.
                    369:           &Apache::lonnet::escape($message),$homeserver);
1.1       www       370:     }
1.2       www       371:     return 'no_host';
1.73      www       372: }
                    373: 
                    374: # =========================================== Retrieve author resource messages
                    375: 
                    376: sub retrieve_author_res_msg {
1.75      www       377:     my $url=shift;
1.73      www       378:     $url=&Apache::lonnet::declutter($url);
1.80      www       379:     my ($domain,$author)=($url=~/^(\w+)\/(\w+)\//);
1.76      www       380:     my %errormsgs=&Apache::lonnet::dump('nohist_res_msgs',$domain,$author);
1.73      www       381:     my $msgs='';
                    382:     foreach (keys %errormsgs) {
1.80      www       383: 	if ($_=~/^\Q$url\E\_\d+$/) {
1.73      www       384: 	    my %content=&unpackagemsg($errormsgs{$_});
1.74      www       385: 	    $msgs.='<p><img src="/adm/lonMisc/bomb.gif" /><b>'.
                    386: 		$content{'time'}.'</b>: '.$content{'message'}.
                    387: 		'<br /></p>';
1.73      www       388: 	}
                    389:     } 
                    390:     return $msgs;     
                    391: }
                    392: 
                    393: 
                    394: # =============================== Delete all author messages related to one URL
                    395: 
                    396: sub del_url_author_res_msg {
1.75      www       397:     my $url=shift;
1.73      www       398:     $url=&Apache::lonnet::declutter($url);
1.77      www       399:     my ($domain,$author)=($url=~/^(\w+)\/(\w+)\//);
                    400:     my @delmsgs=();
                    401:     foreach (&Apache::lonnet::getkeys('nohist_res_msgs',$domain,$author)) {
                    402: 	if ($_=~/^\Q$url\E\_\d+$/) {
                    403: 	    push (@delmsgs,$_);
                    404: 	}
                    405:     }
                    406:     return &Apache::lonnet::del('nohist_res_msgs',\@delmsgs,$domain,$author);
1.73      www       407: }
1.152     www       408: # =================================== Clear out all author messages in URL path
1.73      www       409: 
1.152     www       410: sub clear_author_res_msg {
                    411:     my $url=shift;
                    412:     $url=&Apache::lonnet::declutter($url);
                    413:     my ($domain,$author)=($url=~/^(\w+)\/(\w+)\//);
                    414:     my @delmsgs=();
                    415:     foreach (&Apache::lonnet::getkeys('nohist_res_msgs',$domain,$author)) {
                    416: 	if ($_=~/^\Q$url\E/) {
                    417: 	    push (@delmsgs,$_);
                    418: 	}
                    419:     }
                    420:     return &Apache::lonnet::del('nohist_res_msgs',\@delmsgs,$domain,$author);
                    421: }
1.73      www       422: # ================= Return hash with URLs for which there is a resource message
                    423: 
                    424: sub all_url_author_res_msg {
                    425:     my ($author,$domain)=@_;
1.75      www       426:     my %returnhash=();
1.76      www       427:     foreach (&Apache::lonnet::getkeys('nohist_res_msgs',$domain,$author)) {
1.75      www       428: 	$_=~/^(.+)\_\d+/;
                    429: 	$returnhash{$1}=1;
                    430:     }
                    431:     return %returnhash;
1.1       www       432: }
                    433: 
                    434: # ================================================== Critical message to a user
                    435: 
1.38      www       436: sub user_crit_msg_raw {
1.159     raeburn   437:     my ($user,$domain,$subject,$message,$sendback,$toperm,$sentmessage)=@_;
1.2       www       438: # Check if allowed missing
                    439:     my $status='';
                    440:     my $msgid='undefined';
                    441:     unless (($message)&&($user)&&($domain)) { $status='empty'; };
1.131     www       442:     my $text=$message;
1.2       www       443:     my $homeserver=&Apache::lonnet::homeserver($user,$domain);
                    444:     if ($homeserver ne 'no_host') {
1.3       www       445:        ($msgid,$message)=&packagemsg($subject,$message);
1.24      www       446:        if ($sendback) { $message.='<sendback>true</sendback>'; }
1.4       www       447:        $status=&Apache::lonnet::critical(
                    448:            'put:'.$domain.':'.$user.':critical:'.
                    449:            &Apache::lonnet::escape($msgid).'='.
                    450:            &Apache::lonnet::escape($message),$homeserver);
1.159     raeburn   451:         if (defined($sentmessage)) {
                    452:             $$sentmessage = $message;
                    453:         }
1.2       www       454:     } else {
                    455:        $status='no_host';
                    456:     }
1.53      www       457: # Notifications
1.132     www       458:     my %userenv = &Apache::lonnet::get('environment',['critnotification',
                    459:                                                       'permanentemail'],
1.53      www       460:                                        $domain,$user);
                    461:     if ($userenv{'critnotification'}) {
1.131     www       462:       &sendnotification($userenv{'critnotification'},$user,$domain,$subject,1,
                    463: 			$text);
1.53      www       464:     }
1.132     www       465:     if ($toperm && $userenv{'permanentemail'}) {
                    466:       &sendnotification($userenv{'permanentemail'},$user,$domain,$subject,1,
                    467: 			$text);
                    468:     }
1.53      www       469: # Log this
1.2       www       470:     &Apache::lonnet::logthis(
1.4       www       471:       'Sending critical email '.$msgid.
1.2       www       472:       ', log status: '.
1.140     albertel  473:       &Apache::lonnet::log($env{'user.domain'},$env{'user.name'},
                    474:                          $env{'user.home'},
1.2       www       475:       'Sending critical '.$msgid.' to '.$user.' at '.$domain.' with status: '
1.4       www       476:       .$status));
1.2       www       477:     return $status;
                    478: }
                    479: 
1.38      www       480: # New routine that respects "forward" and calls old routine
                    481: 
1.58      bowersj2  482: =pod
                    483: 
                    484: =item * B<user_crit_msg($user, $domain, $subject, $message, $sendback)>: Sends
                    485:     a critical message $message to the $user at $domain. If $sendback is true,
                    486:     a reciept will be sent to the current user when $user recieves the message.
                    487: 
                    488: =cut
                    489: 
1.38      www       490: sub user_crit_msg {
1.159     raeburn   491:     my ($user,$domain,$subject,$message,$sendback,$toperm,$sentmessage)=@_;
1.38      www       492:     my $status='';
                    493:     my %userenv = &Apache::lonnet::get('environment',['msgforward'],
                    494:                                        $domain,$user);
                    495:     my $msgforward=$userenv{'msgforward'};
                    496:     if ($msgforward) {
                    497:        foreach (split(/\,/,$msgforward)) {
                    498: 	 my ($forwuser,$forwdomain)=split(/\:/,$_);
                    499:          $status.=
                    500: 	   &user_crit_msg_raw($forwuser,$forwdomain,$subject,$message,
1.159     raeburn   501:                 $sendback,$toperm,$sentmessage).' ';
1.38      www       502:        }
                    503:     } else { 
1.159     raeburn   504: 	$status=&user_crit_msg_raw($user,$domain,$subject,$message,$sendback,$toperm,$sentmessage);
1.38      www       505:     }
                    506:     return $status;
                    507: }
                    508: 
1.2       www       509: # =================================================== Critical message received
                    510: 
                    511: sub user_crit_received {
1.12      www       512:     my $msgid=shift;
                    513:     my %message=&Apache::lonnet::get('critical',[$msgid]);
1.52      www       514:     my %contents=&unpackagemsg($message{$msgid},1);
1.24      www       515:     my $status='rec: '.($contents{'sendback'}?
1.5       www       516:      &user_normal_msg($contents{'sendername'},$contents{'senderdomain'},
1.140     albertel  517:                      &mt('Receipt').': '.$env{'user.name'}.' '.&mt('at').' '.$env{'user.domain'}.', '.$contents{'subject'},
                    518:                      &mt('User').' '.$env{'user.name'}.' '.&mt('at').' '.$env{'user.domain'}.
1.42      www       519:                      ' acknowledged receipt of message'."\n".'   "'.
1.67      www       520:                      $contents{'subject'}.'"'."\n".&mt('dated').' '.
1.42      www       521:                      $contents{'time'}.".\n"
                    522:                      ):'no msg req');
1.5       www       523:     $status.=' trans: '.
1.12      www       524:      &Apache::lonnet::put(
                    525:      'nohist_email',{$contents{'msgid'} => $message{$msgid}});
1.5       www       526:     $status.=' del: '.
1.9       albertel  527:      &Apache::lonnet::del('critical',[$contents{'msgid'}]);
1.140     albertel  528:     &Apache::lonnet::log($env{'user.domain'},$env{'user.name'},
                    529:                          $env{'user.home'},'Received critical message '.
1.5       www       530:                          $contents{'msgid'}.
                    531:                          ', '.$status);
1.12      www       532:     return $status;
1.2       www       533: }
                    534: 
                    535: # ======================================================== Normal communication
                    536: 
1.38      www       537: sub user_normal_msg_raw {
1.132     www       538:     my ($user,$domain,$subject,$message,$citation,$baseurl,$attachmenturl,
1.159     raeburn   539: 	$toperm,$currid,$newid,$sentmessage)=@_;
1.2       www       540: # Check if allowed missing
1.173   ! albertel  541:     my ($status,$packed_message);
1.2       www       542:     my $msgid='undefined';
1.131     www       543:     my $text=$message;
1.2       www       544:     unless (($message)&&($user)&&($domain)) { $status='empty'; };
                    545:     my $homeserver=&Apache::lonnet::homeserver($user,$domain);
                    546:     if ($homeserver ne 'no_host') {
1.173   ! albertel  547:        ($msgid,$packed_message)=
        !           548: 	                 &packagemsg($subject,$message,$citation,$baseurl,
        !           549:                                      $attachmenturl,$user,$domain,$currid);
        !           550:        (undef, my $packed_message_no_citation)=
        !           551:                          &packagemsg($subject,$message,undef     ,$baseurl,
1.159     raeburn   552:                                      $attachmenturl,$user,$domain,$currid);
1.108     www       553: # Store in user folder
1.4       www       554:        $status=&Apache::lonnet::critical(
                    555:            'put:'.$domain.':'.$user.':nohist_email:'.
                    556:            &Apache::lonnet::escape($msgid).'='.
1.173   ! albertel  557:            &Apache::lonnet::escape($packed_message),$homeserver);
1.108     www       558: # Save new message received time
1.40      www       559:        &Apache::lonnet::put
                    560:                          ('email_status',{'recnewemail'=>time},$domain,$user);
1.159     raeburn   561: # Into sent-mail folder unless a broadcast message or critical message
                    562:        unless (($env{'request.course.id'}) && 
                    563:                (($env{'form.sendmode'} eq 'group')  || 
                    564:                (($env{'form.critmsg'}) || ($env{'form.sendbck'})) &&
1.170     albertel  565:                (&Apache::lonnet::allowed('srm',$env{'request.course.id'})
                    566: 		|| &Apache::lonnet::allowed('srm',$env{'request.course.id'}.
                    567: 					    '/'.$env{'request.course.sec'})))) {
1.173   ! albertel  568:            $status .= &store_sent_mail($msgid,$packed_message_no_citation);
1.156     raeburn   569:        }
1.2       www       570:     } else {
                    571:        $status='no_host';
1.53      www       572:     }
1.156     raeburn   573:     if (defined($newid)) {
                    574:         $$newid = $msgid;
                    575:     }
1.159     raeburn   576:     if (defined($sentmessage)) {
1.173   ! albertel  577:         $$sentmessage = $packed_message;
1.159     raeburn   578:     }
                    579: 
1.53      www       580: # Notifications
1.132     www       581:     my %userenv = &Apache::lonnet::get('environment',['notification',
                    582:                                                       'permanentemail'],
1.53      www       583:                                        $domain,$user);
                    584:     if ($userenv{'notification'}) {
1.131     www       585: 	&sendnotification($userenv{'notification'},$user,$domain,$subject,0,
                    586: 			  $text);
1.2       www       587:     }
1.132     www       588:     if ($toperm && $userenv{'permanentemail'}) {
                    589:       &sendnotification($userenv{'permanentemail'},$user,$domain,$subject,0,
                    590: 			$text);
                    591:     }
1.140     albertel  592:     &Apache::lonnet::log($env{'user.domain'},$env{'user.name'},
                    593:                          $env{'user.home'},
1.2       www       594:       'Sending '.$msgid.' to '.$user.' at '.$domain.' with status: '.$status);
                    595:     return $status;
                    596: }
1.38      www       597: 
                    598: # New routine that respects "forward" and calls old routine
                    599: 
1.58      bowersj2  600: =pod
                    601: 
                    602: =item * B<user_normal_msg($user, $domain, $subject, $message,
                    603:     $citation, $baseurl, $attachmenturl)>: Sends a message to the
                    604:     $user at $domain, with subject $subject and message $message.
                    605: 
                    606: =cut
                    607: 
1.38      www       608: sub user_normal_msg {
1.132     www       609:     my ($user,$domain,$subject,$message,$citation,$baseurl,$attachmenturl,
1.159     raeburn   610: 	$toperm,$sentmessage)=@_;
1.38      www       611:     my $status='';
                    612:     my %userenv = &Apache::lonnet::get('environment',['msgforward'],
                    613:                                        $domain,$user);
                    614:     my $msgforward=$userenv{'msgforward'};
                    615:     if ($msgforward) {
1.171     banghart  616:         foreach (split(/\,/,$msgforward)) {
1.172     albertel  617: 	    my ($forwuser,$forwdomain)=split(/\:/,$_);
                    618: 	    $status.=
1.171     banghart  619: 	        &user_normal_msg_raw($forwuser,$forwdomain,$subject,$message,
1.172     albertel  620: 				     $citation,$baseurl,$attachmenturl,$toperm,
                    621: 				     undef,undef,$sentmessage).' ';
1.171     banghart  622:         }
1.38      www       623:     } else { 
1.172     albertel  624: 	$status=&user_normal_msg_raw($user,$domain,$subject,$message,
                    625: 				     $citation,$baseurl,$attachmenturl,$toperm,
                    626: 				     undef,undef,$sentmessage);
1.38      www       627:     }
                    628:     return $status;
                    629: }
                    630: 
1.156     raeburn   631: sub store_sent_mail {
                    632:     my ($msgid,$message) = @_;
1.171     banghart  633:     my $status =' '.&Apache::lonnet::critical(
                    634:                'put:'.$env{'user.domain'}.':'.$env{'user.name'}.
                    635:                                           ':nohist_email_sent:'.
                    636:                &Apache::lonnet::escape($msgid).'='.
                    637:                &Apache::lonnet::escape($message),$env{'user.home'});
1.156     raeburn   638:     return $status;
                    639: }
1.2       www       640: 
1.106     www       641: # ============================================================ List all folders
                    642: 
                    643: sub folderlist {
                    644:     my $folder=shift;
                    645:     my @allfolders=&Apache::lonnet::getkeys('email_folders');
                    646:     if ($allfolders[0]=~/^error:/) { @allfolders=(); }
                    647:     return '<form method="post" action="/adm/email">'.
1.108     www       648: 	&mt('Folder').': '.
1.106     www       649: 	&Apache::loncommon::select_form($folder,'folder',
                    650: 			     ('' => &mt('INBOX'),'trash' => &mt('TRASH'),
1.114     www       651: 			      'new' => &mt('New Messages Only'),
1.113     www       652:                               'critical' => &mt('Critical'),
1.106     www       653: 			      'sent' => &mt('Sent Messages'),
                    654: 			      map { $_ => $_ } @allfolders)).
1.125     www       655: 			      ' '.&mt('Show').
                    656: 			      '<select name="interdis">'.
                    657: 			      join("\n",map { '<option value="'.$_.'"'.
                    658: 	 ($_==$interdis?' selected="selected"':'').'>'.$_.'</option>' }
                    659: 				   (10,20,50,100,200)).'</select>'.	
1.108     www       660:    '<input type="submit" value="'.&mt('View Folder').'" /><br />'.
1.140     albertel  661:     '<input type="hidden" name="sortedby" value="'.$env{'form.sortedby'}.'" />'.
1.118     www       662: 			      ($folder=~/^(new|critical)/?'</form>':'');
                    663: }
                    664: 
                    665: sub scrollbuttons {
                    666:     my ($start,$maxdis,$first,$finish,$total)=@_;
1.124     www       667:     unless ($total>0) { return ''; }
1.118     www       668:     $start++; $maxdis++;$first++;$finish++;
1.152     www       669:     return
                    670:    &mt('Page').': '. 
1.108     www       671:    '<input type="submit" name="firstview" value="'.&mt('First').'" />'.
                    672:    '<input type="submit" name="prevview" value="'.&mt('Previous').'" />'.
1.118     www       673:    '<input type="text" size="5" name="startdis" value="'.$start.'" onChange="this.form.submit()" /> of '.$maxdis.
1.108     www       674:    '<input type="submit" name="nextview" value="'.&mt('Next').'" />'.
1.118     www       675:    '<input type="submit" name="lastview" value="'.&mt('Last').'" /><br />'.
1.152     www       676:    &mt('Showing messages [_1] through [_2] of [_3]',$first,$finish,$total).'</form>';
1.106     www       677: }
1.108     www       678: 
1.106     www       679: # =============================================================== Folder suffix
                    680: 
                    681: sub foldersuffix {
                    682:     my $folder=shift;
                    683:     unless ($folder) { return ''; }
                    684:     return '_'.&Apache::lonnet::escape($folder);
                    685: }
                    686: 
1.7       www       687: # =============================================================== Status Change
                    688: 
                    689: sub statuschange {
1.106     www       690:     my ($msgid,$newstatus,$folder)=@_;
                    691:     my $suffix=&foldersuffix($folder);
                    692:     my %status=&Apache::lonnet::get('email_status'.$suffix,[$msgid]);
1.7       www       693:     if ($status{$msgid}=~/^error\:/) { $status{$msgid}=''; }
                    694:     unless ($status{$msgid}) { $status{$msgid}='new'; }
                    695:     unless (($status{$msgid} eq 'replied') || 
                    696:             ($status{$msgid} eq 'forwarded')) {
1.106     www       697: 	&Apache::lonnet::put('email_status'.$suffix,{$msgid => $newstatus});
1.7       www       698:     }
1.14      www       699:     if (($newstatus eq 'deleted') || ($newstatus eq 'new')) {
1.106     www       700: 	&Apache::lonnet::put('email_status'.$suffix,{$msgid => $newstatus});
1.14      www       701:     }
1.148     www       702:     if ($newstatus eq 'deleted') {
                    703:        &movemsg(&Apache::lonnet::unescape($msgid),$folder,'trash');
                    704:    }
1.7       www       705: }
1.14      www       706: 
1.106     www       707: # ============================================================= Make new folder
                    708: 
                    709: sub makefolder {
                    710:     my ($newfolder)=@_;
1.113     www       711:     if (($newfolder eq 'sent')
                    712:      || ($newfolder eq 'critical')
1.114     www       713:      || ($newfolder eq 'trash')
                    714:      || ($newfolder eq 'new')) { return; }
1.106     www       715:     &Apache::lonnet::put('email_folders',{$newfolder => time});
                    716: }
                    717: 
                    718: # ======================================================== Move between folders
                    719: 
                    720: sub movemsg {
                    721:     my ($msgid,$srcfolder,$trgfolder)=@_;
1.142     www       722:     if ($srcfolder eq 'new') { $srcfolder=''; }
1.106     www       723:     my $srcsuffix=&foldersuffix($srcfolder);
                    724:     my $trgsuffix=&foldersuffix($trgfolder);
1.107     www       725: 
                    726: # Copy message
                    727:     my %message=&Apache::lonnet::get('nohist_email'.$srcsuffix,[$msgid]);
                    728:     &Apache::lonnet::put('nohist_email'.$trgsuffix,{$msgid => $message{$msgid}});
                    729: 
                    730: # Copy status
1.128     www       731:     unless ($trgfolder eq 'trash') {
                    732: 	my %status=&Apache::lonnet::get('email_status'.$srcsuffix,[$msgid]);
                    733: 	&Apache::lonnet::put('email_status'.$trgsuffix,{$msgid => $status{$msgid}});
1.107     www       734:     }
                    735: # Delete orginals
1.106     www       736:     &Apache::lonnet::del('nohist_email'.$srcsuffix,[$msgid]);
1.127     www       737:     &Apache::lonnet::del('email_status'.$srcsuffix,[$msgid]);
1.106     www       738: }
                    739: 
1.17      www       740: # ======================================================= Display a course list
                    741: 
                    742: sub discourse {
                    743:     my $r=shift;
1.109     matthew   744:     my $classlist = &Apache::loncoursedata::get_classlist();
1.17      www       745:     my $now=time;
1.138     albertel  746:     my %lt=&Apache::lonlocal::texthash('cfa' => 'Check All',
                    747:             'cfs' => 'Check Section/Group',
                    748:             'cfn' => 'Uncheck All');
1.17      www       749:     $r->print(<<ENDDISHEADER);
1.92      www       750: <input type="hidden" name="sendmode" value="group" />
1.17      www       751: <script>
                    752:     function checkall() {
                    753: 	for (i=0; i<document.forms.compemail.elements.length; i++) {
                    754:             if 
                    755:           (document.forms.compemail.elements[i].name.indexOf('send_to_')==0) {
                    756: 	      document.forms.compemail.elements[i].checked=true;
                    757:             }
                    758:         }
                    759:     }
                    760: 
1.19      www       761:     function checksec() {
                    762: 	for (i=0; i<document.forms.compemail.elements.length; i++) {
                    763:             if 
                    764:           (document.forms.compemail.elements[i].name.indexOf
                    765:            ('send_to_&&&'+document.forms.compemail.chksec.value)==0) {
                    766: 	      document.forms.compemail.elements[i].checked=true;
                    767:             }
                    768:         }
                    769:     }
                    770: 
1.17      www       771:     function uncheckall() {
                    772: 	for (i=0; i<document.forms.compemail.elements.length; i++) {
                    773:             if 
                    774:           (document.forms.compemail.elements[i].name.indexOf('send_to_')==0) {
                    775: 	      document.forms.compemail.elements[i].checked=false;
                    776:             }
                    777:         }
                    778:     }
                    779: </script>
1.92      www       780: <input type="button" onClick="checkall()" value="$lt{'cfa'}" />&nbsp;
                    781: <input type="button" onClick="checksec()" value="$lt{'cfs'}" />
1.136     albertel  782: <input type="text" size="5" name="chksec" />&nbsp;
1.92      www       783: <input type="button" onClick="uncheckall()" value="$lt{'cfn'}" />
1.17      www       784: <p>
                    785: ENDDISHEADER
1.109     matthew   786:     my %coursepersonnel=&Apache::lonnet::get_course_adv_roles();
                    787:     $r->print('<table>');
1.61      www       788:     foreach my $role (sort keys %coursepersonnel) {
1.109     matthew   789:         foreach (split(/\,/,$coursepersonnel{$role})) {
                    790:             my ($puname,$pudom)=split(/\:/,$_);
                    791:             $r->print('<tr><td><label>'.
                    792:                       '<input type="checkbox" name="send_to_&&&&&&_'.
                    793:                       $puname.':'.$pudom.'" /> '.
                    794:                       &Apache::loncommon::plainname($puname,$pudom).
                    795:                       '</label></td>'.
                    796:                       '<td>('.$_.'),</td><td><i>'.$role.'</i></td></tr>');
                    797:         }
1.61      www       798:     }
1.110     matthew   799:     $r->print('</table><table>');
1.134     albertel  800:     my $sort = sub {
                    801: 	my $aname=lc($classlist->{$a}[&Apache::loncoursedata::CL_FULLNAME()]);
                    802: 	if (!$aname) { $aname=$a; }
                    803: 	my $bname=lc($classlist->{$b}[&Apache::loncoursedata::CL_FULLNAME()]);
                    804: 	if (!$bname) { $bname=$b; }
                    805: 	return $aname cmp $bname;
                    806:     };
                    807:     foreach my $student (sort $sort (keys(%{$classlist}))) {
                    808: 	my $info=$classlist->{$student};
1.109     matthew   809:         my ($sname,$sdom,$status,$fullname,$section) =
                    810:             (@{$info}[&Apache::loncoursedata::CL_SNAME(),
                    811:                       &Apache::loncoursedata::CL_SDOM(),
                    812:                       &Apache::loncoursedata::CL_STATUS(),
                    813:                       &Apache::loncoursedata::CL_FULLNAME(),
                    814:                       &Apache::loncoursedata::CL_SECTION()]);
1.110     matthew   815:         next if ($status ne 'Active');
1.143     albertel  816: 	next if ($env{'request.course.sec'} &&
                    817: 		 $section ne $env{'request.course.sec'});
1.129     matthew   818:         my $key = 'send_to_&&&'.$section.'&&&_'.$student;
1.109     matthew   819:         if (! defined($fullname) || $fullname eq '') { $fullname = $sname; }
                    820:         $r->print('<tr><td><label>'.
1.136     albertel  821:                   qq{<input type="checkbox" name="$key" />}.('&nbsp;'x2).
                    822:                   $fullname.'</label></td><td>'.$sname.'@'.$sdom.'</td><td>'.$section.
1.109     matthew   823:                   '</td></tr>');
1.28      harris41  824:     }
1.110     matthew   825:     $r->print('</table>');
1.17      www       826: }
                    827: 
1.13      www       828: # ==================================================== Display Critical Message
1.5       www       829: 
1.12      www       830: sub discrit {
                    831:     my $r=shift;
1.67      www       832:     my $header = '<h1><font color=red>'.&mt('Critical Messages').'</font></h1>'.
1.136     albertel  833:         '<form action="/adm/email" method="POST">'.
                    834:         '<input type="hidden" name="confirm" value="true" />';
1.30      matthew   835:     my %what=&Apache::lonnet::dump('critical');
                    836:     my $result = '';
                    837:     foreach (sort keys %what) {
                    838:         my %content=&unpackagemsg($what{$_});
                    839:         next if ($content{'senderdomain'} eq '');
1.106     www       840:         $result.='<hr />'.&mt('From').': <b>'.
1.37      www       841: &Apache::loncommon::aboutmewrapper(
                    842:  &Apache::loncommon::plainname($content{'sendername'},$content{'senderdomain'}),$content{'sendername'},$content{'senderdomain'}).'</b> ('.
                    843: $content{'sendername'}.'@'.
                    844:             $content{'senderdomain'}.') '.$content{'time'}.
1.106     www       845:             '<br />'.&mt('Subject').': '.$content{'subject'}.
1.130     albertel  846:             '<br /><pre>'.
1.36      www       847:               &Apache::lontexconvert::msgtexconverted($content{'message'}).
1.130     albertel  848:             '</pre><small>'.
1.84      www       849: &mt('You have to confirm that you received this message. After confirmation, this message will be moved to your regular inbox').
                    850:             '</small><br />'.
1.136     albertel  851:             '<input type="submit" name="rec_'.$_.'" value="'.&mt('Confirm Receipt').'" />'.
                    852:             '<input type="submit" name="reprec_'.$_.'" '.
                    853:                   'value="'.&mt('Confirm Receipt and Reply').'" />';
1.30      matthew   854:     }
                    855:     # Check to see if there were any messages.
                    856:     if ($result eq '') {
1.67      www       857:         $result = "<h2>".&mt('You have no critical messages.')."</h2>".
1.106     www       858: 	    '<a href="/adm/roles">'.&mt('Select a course').'</a><br />'.
                    859:             '<a href="/adm/email">'.&mt('Communicate').'</a>';
1.30      matthew   860:     } else {
                    861:         $r->print($header);
                    862:     }
                    863:     $r->print($result);
1.108     www       864:     $r->print('<input type="hidden" name="displayedcrit" value="true" /></form>');
1.12      www       865: }
                    866: 
1.65      www       867: sub sortedmessages {
1.106     www       868:     my ($blocked,$startblock,$endblock,$numblocked,$folder) = @_;
                    869:     my $suffix=&foldersuffix($folder);
                    870:     my @messages = &Apache::lonnet::getkeys('nohist_email'.$suffix);
1.65      www       871:     #unpack the varibles and repack into temp for sorting
                    872:     my @temp;
1.167     raeburn   873:     my %descriptions;
1.169     albertel  874:     my %status_cache = 
                    875: 	&Apache::lonnet::get('email_status'.&foldersuffix($folder),\@messages);
1.65      www       876:     foreach (@messages) {
                    877: 	my $msgid=&Apache::lonnet::escape($_);
1.141     raeburn   878: 	my ($sendtime,$shortsubj,$fromname,$fromdomain,$status,$fromcid)=
1.169     albertel  879: 	    &Apache::lonmsg::unpackmsgid($msgid,$folder,undef,
                    880: 					 \%status_cache);
1.167     raeburn   881:         my $description = &get_course_desc($fromcid,\%descriptions);
1.65      www       882: 	my @temp1 = ($sendtime,$shortsubj,$fromname,$fromdomain,$status,
1.157     raeburn   883: 		     $msgid,$description);
1.101     raeburn   884:         # Check whether message was sent during blocking period.
                    885:         if ($sendtime >= $startblock && ($sendtime <= $endblock && $endblock > 0) ) {
                    886:             my $escid = &Apache::lonnet::unescape($msgid);
                    887:             $$blocked{$escid} = 'ON';
                    888:             $$numblocked ++;
                    889:         } else { 
                    890:             push @temp ,\@temp1;
                    891:         }
1.65      www       892:     }
                    893:     #default sort
                    894:     @temp = sort  {$a->[0] <=> $b->[0]} @temp;    
1.140     albertel  895:     if ($env{'form.sortedby'} eq "date"){
1.65      www       896:         @temp = sort  {$a->[0] <=> $b->[0]} @temp;    
                    897:     }
1.140     albertel  898:     if ($env{'form.sortedby'} eq "revdate"){
1.65      www       899:     	@temp = sort  {$b->[0] <=> $a->[0]} @temp; 
                    900:     }
1.140     albertel  901:     if ($env{'form.sortedby'} eq "user"){
1.65      www       902: 	@temp = sort  {lc($a->[2]) cmp lc($b->[2])} @temp;
                    903:     }
1.140     albertel  904:     if ($env{'form.sortedby'} eq "revuser"){
1.65      www       905: 	@temp = sort  {lc($b->[2]) cmp lc($a->[2])} @temp;
                    906:     }
1.140     albertel  907:     if ($env{'form.sortedby'} eq "domain"){
1.65      www       908:         @temp = sort  {$a->[3] cmp $b->[3]} @temp;
                    909:     }
1.140     albertel  910:     if ($env{'form.sortedby'} eq "revdomain"){
1.65      www       911:         @temp = sort  {$b->[3] cmp $a->[3]} @temp;
                    912:     }
1.140     albertel  913:     if ($env{'form.sortedby'} eq "subject"){
1.65      www       914:         @temp = sort  {lc($a->[1]) cmp lc($b->[1])} @temp;
                    915:     }
1.140     albertel  916:     if ($env{'form.sortedby'} eq "revsubject"){
1.65      www       917:         @temp = sort  {lc($b->[1]) cmp lc($a->[1])} @temp;
                    918:     }
1.157     raeburn   919:     if ($env{'form.sortedby'} eq "course"){
                    920:         @temp = sort  {lc($a->[6]) cmp lc($b->[6])} @temp;
                    921:     }
                    922:     if ($env{'form.sortedby'} eq "revcourse"){
                    923:         @temp = sort  {lc($b->[6]) cmp lc($a->[6])} @temp;
                    924:     }
1.140     albertel  925:     if ($env{'form.sortedby'} eq "status"){
1.65      www       926:         @temp = sort  {$a->[4] cmp $b->[4]} @temp;
                    927:     }
1.140     albertel  928:     if ($env{'form.sortedby'} eq "revstatus"){
1.65      www       929:         @temp = sort  {$b->[4] cmp $a->[4]} @temp;
                    930:     }
                    931:     return @temp;
                    932: }
                    933: 
1.157     raeburn   934: sub get_course_desc {
1.167     raeburn   935:     my ($fromcid,$descriptions) = @_;
1.166     raeburn   936:     my $description;
1.167     raeburn   937:     if (!$fromcid) {
                    938:         return $description;
1.157     raeburn   939:     } else {
1.167     raeburn   940:         if (defined($$descriptions{$fromcid})) {
                    941:             $description = $$descriptions{$fromcid};
                    942:         } else {
                    943:             if (defined($env{'course.'.$fromcid.'.description'})) {
                    944:                 $description = $env{'course.'.$fromcid.'.description'};
                    945:             } else {
                    946:                 my %courseinfo=&Apache::lonnet::coursedescription($fromcid);                $description = $courseinfo{'description'};
                    947:                 $description = $courseinfo{'description'};
                    948:             }
                    949:             $$descriptions{$fromcid} = $description;
                    950:         }
                    951:         return $description;
1.157     raeburn   952:     }
                    953: }
                    954: 
1.112     www       955: # ======================================================== Display new messages
                    956: 
                    957: 
                    958: sub disnew {
                    959:     my $r=shift;
                    960:     my %lt=&Apache::lonlocal::texthash(
                    961: 				       'nm' => 'New Messages',
                    962: 				       'su' => 'Subject',
1.157     raeburn   963:                                        'co' => 'Course',
1.112     www       964: 				       'da' => 'Date',
                    965: 				       'us' => 'Username',
                    966: 				       'op' => 'Open',
                    967: 				       'do' => 'Domain'
                    968: 				       );
1.169     albertel  969:     my @msgids = sort(&Apache::lonnet::getkeys('nohist_email'));
1.112     www       970:     my @newmsgs;
                    971:     my %setters = ();
                    972:     my $startblock = 0;
                    973:     my $endblock = 0;
                    974:     my %blocked = ();
                    975:     my $numblocked = 0;
                    976:     # Check for blocking of display because of scheduled online exams.
                    977:     &blockcheck(\%setters,\$startblock,\$endblock);
1.169     albertel  978:     my %status_cache = 
                    979: 	&Apache::lonnet::get('email_status',\@msgids);
1.167     raeburn   980:     my %descriptions;
1.112     www       981:     foreach (@msgids) {
1.169     albertel  982: 	my $msgid=&Apache::lonnet::escape($_);
1.141     raeburn   983:         my ($sendtime,$shortsubj,$fromname,$fromdom,$status,$fromcid)=
1.169     albertel  984: 	    &Apache::lonmsg::unpackmsgid($msgid,undef,undef,\%status_cache);
1.112     www       985:         if (defined($sendtime) && $sendtime!~/error/) {
1.167     raeburn   986:             my $description = &get_course_desc($fromcid,\%descriptions);
1.112     www       987:             my $numsendtime = $sendtime;
                    988:             $sendtime = &Apache::lonlocal::locallocaltime($sendtime);
                    989:             if ($status eq 'new') {
                    990:                 if ($numsendtime >= $startblock && ($numsendtime <= $endblock && $endblock > 0) ) {
                    991:                     $blocked{$_} = 'ON';
                    992:                     $numblocked ++;
                    993:                 } else {
                    994:                     push @newmsgs, { 
1.169     albertel  995:                         msgid    => $msgid,
1.112     www       996:                         sendtime => $sendtime,
                    997:                         shortsub => &Apache::lonnet::unescape($shortsubj),
                    998:                         from     => $fromname,
1.157     raeburn   999:                         fromdom  => $fromdom,
                   1000:                         course   => $description 
1.112     www      1001:                         }
                   1002:                 }
                   1003:             }
                   1004:         }
                   1005:     }
                   1006:     if ($#newmsgs >= 0) {
                   1007:         $r->print(<<TABLEHEAD);
                   1008: <h2>$lt{'nm'}</h2>
                   1009: <table border=2><tr><th>&nbsp</th>
1.157     raeburn  1010: <th>$lt{'da'}</th><th>$lt{'us'}</th><th>$lt{'do'}</th><th>$lt{'su'}</th><th>$lt{'co'}</th></tr>
1.112     www      1011: TABLEHEAD
                   1012:         foreach my $msg (@newmsgs) {
                   1013:             $r->print(<<"ENDLINK");
1.152     www      1014: <tr class="new" bgcolor="#FFBB77" onMouseOver="javascript:style.backgroundColor='#DD9955'" 
                   1015: onMouseOut="javascript:style.backgroundColor='#FFBB77'">
1.131     www      1016: <td><a href="/adm/email?dismode=new&display=$msg->{'msgid'}">$lt{'op'}</a></td>
1.112     www      1017: ENDLINK
1.157     raeburn  1018:             foreach ('sendtime','from','fromdom','shortsub','course') {
1.112     www      1019:                 $r->print("<td>$msg->{$_}</td>");
                   1020:             }
                   1021:             $r->print("</td></tr>");
                   1022:         }
1.139     albertel 1023:         $r->print('</table>'.&Apache::loncommon::endbodytag().'</html>');
1.112     www      1024:     } elsif ($numblocked == 0) {
                   1025:         $r->print("<h3>".&mt('You have no unread messages')."</h3>");
                   1026:     }
                   1027:     if ($numblocked > 0) {
                   1028:         my $beginblock = &Apache::lonlocal::locallocaltime($startblock);
                   1029:         my $finishblock = &Apache::lonlocal::locallocaltime($endblock);
                   1030:         if ($numblocked == 1) {
                   1031:             $r->print("<h3>".&mt('You have').' '.$numblocked.' '.&mt('blocked unread message').".</h3>");
                   1032:             $r->print(&mt('This message is not viewable because').' ');
                   1033:         } else {
                   1034:             $r->print("<h3>".&mt('You have').' '.$numblocked.' '.&mt('blocked unread messages').".</h3>");
                   1035:             $r->print(&mt('These').' '.$numblocked.' '.&mt('messages are not viewable because '));
                   1036:         }
                   1037:         $r->print(
                   1038: &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').'.');
                   1039:         &build_block_table($r,$startblock,$endblock,\%setters);
                   1040:     }
                   1041: }
                   1042: 
                   1043: 
1.15      www      1044: # ======================================================== Display all messages
                   1045: 
1.14      www      1046: sub disall {
1.106     www      1047:     my ($r,$folder)=@_;
1.113     www      1048:     $r->print(&folderlist($folder));
1.114     www      1049:     if ($folder eq 'new') {
                   1050: 	&disnew($r);
                   1051:     } elsif ($folder eq 'critical') {
                   1052: 	&discrit($r);
                   1053:     } else {
                   1054: 	&disfolder($r,$folder);
1.113     www      1055:     }
1.114     www      1056: }
                   1057: 
                   1058: # ============================================================ Display a folder
                   1059: 
                   1060: sub disfolder {
                   1061:     my ($r,$folder)=@_;
1.101     raeburn  1062:     my %blocked = ();
                   1063:     my %setters = ();
                   1064:     my $startblock;
                   1065:     my $endblock;
                   1066:     my $numblocked = 0;
                   1067:     &blockcheck(\%setters,\$startblock,\$endblock);
                   1068:     $r->print(<<ENDDISHEADER);
1.29      www      1069: <script>
                   1070:     function checkall() {
                   1071: 	for (i=0; i<document.forms.disall.elements.length; i++) {
                   1072:             if 
                   1073:           (document.forms.disall.elements[i].name.indexOf('delmark_')==0) {
                   1074: 	      document.forms.disall.elements[i].checked=true;
                   1075:             }
                   1076:         }
                   1077:     }
                   1078: 
                   1079:     function uncheckall() {
                   1080: 	for (i=0; i<document.forms.disall.elements.length; i++) {
                   1081:             if 
                   1082:           (document.forms.disall.elements[i].name.indexOf('delmark_')==0) {
                   1083: 	      document.forms.disall.elements[i].checked=false;
                   1084:             }
                   1085:         }
                   1086:     }
                   1087: </script>
                   1088: ENDDISHEADER
1.108     www      1089:     my $fsqs='&folder='.$folder;
                   1090:     my @temp=sortedmessages(\%blocked,$startblock,$endblock,\$numblocked,$folder);
                   1091:     my $totalnumber=$#temp+1;
1.124     www      1092:     unless ($totalnumber>0) {
                   1093: 	$r->print('<h2>'.&mt('Empty Folder').'</h2>');
                   1094: 	return;
                   1095:     }
1.125     www      1096:     unless ($interdis) {
                   1097: 	$interdis=20;
                   1098:     }
1.118     www      1099:     my $number=int($totalnumber/$interdis);
                   1100:     if (($startdis<0) || ($startdis>$number)) { $startdis=$number; }
1.108     www      1101:     my $firstdis=$interdis*$startdis;
                   1102:     if ($firstdis>$#temp) { $firstdis=$#temp-$interdis+1; }
                   1103:     my $lastdis=$firstdis+$interdis-1;
                   1104:     if ($lastdis>$#temp) { $lastdis=$#temp; }
1.118     www      1105:     $r->print(&scrollbuttons($startdis,$number,$firstdis,$lastdis,$totalnumber));
1.113     www      1106:     $r->print('<form method="post" name="disall" action="/adm/email">'.
1.106     www      1107: 	      '<table border=2><tr><th colspan="3">&nbsp</th><th>');
1.140     albertel 1108:     if ($env{'form.sortedby'} eq "revdate") {
1.108     www      1109: 	$r->print('<a href = "?sortedby=date'.$fsqs.'">'.&mt('Date').'</a></th>');
1.62      www      1110:     } else {
1.108     www      1111: 	$r->print('<a href = "?sortedby=revdate'.$fsqs.'">'.&mt('Date').'</a></th>');
1.62      www      1112:     }
                   1113:     $r->print('<th>');
1.140     albertel 1114:     if ($env{'form.sortedby'} eq "revuser") {
1.108     www      1115: 	$r->print('<a href = "?sortedby=user'.$fsqs.'">'.&mt('Username').'</a>');
1.62      www      1116:     } else {
1.108     www      1117: 	$r->print('<a href = "?sortedby=revuser'.$fsqs.'">'.&mt('Username').'</a>');
1.62      www      1118:     }
                   1119:     $r->print('</th><th>');
1.140     albertel 1120:     if ($env{'form.sortedby'} eq "revdomain") {
1.108     www      1121: 	$r->print('<a href = "?sortedby=domain'.$fsqs.'">'.&mt('Domain').'</a>');
1.62      www      1122:     } else {
1.108     www      1123: 	$r->print('<a href = "?sortedby=revdomain'.$fsqs.'">'.&mt('Domain').'</a>');
1.62      www      1124:     }
                   1125:     $r->print('</th><th>');
1.140     albertel 1126:     if ($env{'form.sortedby'} eq "revsubject") {
1.108     www      1127: 	$r->print('<a href = "?sortedby=subject'.$fsqs.'">'.&mt('Subject').'</a>');
1.62      www      1128:     } else {
1.108     www      1129:     	$r->print('<a href = "?sortedby=revsubject'.$fsqs.'">'.&mt('Subject').'</a>');
1.62      www      1130:     }
                   1131:     $r->print('</th><th>');
1.157     raeburn  1132:     if ($env{'form.sortedby'} eq "revcourse") {
                   1133:         $r->print('<a href = "?sortedby=course'.$fsqs.'">'.&mt('Course').'</a>');
                   1134:     } else {
                   1135:         $r->print('<a href = "?sortedby=revcourse'.$fsqs.'">'.&mt('Course').'</a>');
                   1136:     }
                   1137:     $r->print('</th><th>');
1.140     albertel 1138:     if ($env{'form.sortedby'} eq "revstatus") {
1.135     albertel 1139: 	$r->print('<a href = "?sortedby=status'.$fsqs.'">'.&mt('Status').'</a></th>');
1.62      www      1140:     } else {
1.135     albertel 1141:      	$r->print('<a href = "?sortedby=revstatus'.$fsqs.'">'.&mt('Status').'</a></th>');
1.62      www      1142:     }
1.126     www      1143:     $r->print("</tr>\n");
1.108     www      1144:     for (my $n=$firstdis;$n<=$lastdis;$n++) {
1.157     raeburn  1145: 	my ($sendtime,$shortsubj,$fromname,$fromdomain,$status,$origID,$description)= @{$temp[$n]};
1.63      albertel 1146: 	if (($status ne 'deleted') && defined($sendtime) && $sendtime!~/error/) {
1.39      albertel 1147: 	    if ($status eq 'new') {
1.152     www      1148: 		$r->print('<tr bgcolor="#FFBB77" onMouseOver="javascript:style.backgroundColor=\'#DD9955\'"  onMouseOut="javascript:style.backgroundColor=\'#FFBB77\'">');
1.39      albertel 1149: 	    } elsif ($status eq 'read') {
1.152     www      1150: 		$r->print('<tr bgcolor="#BBBB77" onMouseOver="javascript:style.backgroundColor=\'#999944\'"  onMouseOut="javascript:style.backgroundColor=\'#BBBB77\'">');
1.39      albertel 1151: 	    } elsif ($status eq 'replied') {
1.152     www      1152: 		$r->print('<tr bgcolor="#AAAA88" onMouseOver="javascript:style.backgroundColor=\'#888855\'"  onMouseOut="javascript:style.backgroundColor=\'#AAAA88\'">'); 
1.39      albertel 1153: 	    } else {
1.152     www      1154: 		$r->print('<tr bgcolor="#99BBBB" onMouseOver="javascript:style.backgroundColor=\'#669999\'"  onMouseOut="javascript:style.backgroundColor=\'#99BBBB\'">');
1.39      albertel 1155: 	    }
1.136     albertel 1156: 	    $r->print('<td><input type="checkbox" name="delmark_'.$origID.'" /></td><td><a href="/adm/email?display='.$origID.$sqs. 
1.106     www      1157: 		      '">'.&mt('Open').'</a></td><td>'.
                   1158: 		      ($folder ne 'trash'?'<a href="/adm/email?markdel='.$origID.$sqs.
1.135     albertel 1159: 		      '">'.&mt('Delete'):'&nbsp').'</a></td>'.
1.66      www      1160: 		      '<td>'.&Apache::lonlocal::locallocaltime($sendtime).'</td><td>'.
1.39      albertel 1161: 		      $fromname.'</td><td>'.$fromdomain.'</td><td>'.
1.14      www      1162: 		      &Apache::lonnet::unescape($shortsubj).'</td><td>'.
1.157     raeburn  1163:                       $description.'</td><td>'.$status.'</td></tr>'."\n");
1.106     www      1164: 	} elsif ($status eq 'deleted') {
                   1165: # purge
1.108     www      1166: 	    &movemsg(&Apache::lonnet::unescape($origID),$folder,'trash');
1.63      albertel 1167: 	}
                   1168:     }   
1.126     www      1169:     $r->print("</table>\n<p>".
1.106     www      1170:   '<a href="javascript:checkall()">'.&mt('Check All').'</a>&nbsp;'.
                   1171:   '<a href="javascript:uncheckall()">'.&mt('Uncheck All').'</a></p>'.
1.140     albertel 1172:   '<input type="hidden" name="sortedby" value="'.$env{'form.sortedby'}.'" />');
1.106     www      1173:     if ($folder ne 'trash') {
                   1174: 	$r->print(
                   1175: 	      '<p><input type="submit" name="markeddel" value="'.&mt('Delete Checked').'" /></p>');
                   1176:     }
1.118     www      1177:     $r->print('<p><input type="submit" name="markedmove" value="'.&mt('Move Checked to Folder').'" />');
1.106     www      1178:     my @allfolders=&Apache::lonnet::getkeys('email_folders');
                   1179:     if ($allfolders[0]=~/^error:/) { @allfolders=(); }
                   1180:     $r->print(
                   1181: 	&Apache::loncommon::select_form('','movetofolder',
                   1182: 			     ( map { $_ => $_ } @allfolders))
                   1183: 	      );
1.126     www      1184:     my $postedstartdis=$startdis+1;
1.140     albertel 1185:     $r->print('<input type="hidden" name="folder" value="'.$folder.'" /><input type="hidden" name="startdis" value="'.$postedstartdis.'" /><input type="hidden" name="interdis" value="'.$env{'form.interdis'}.'" /></form>');
1.101     raeburn  1186:     if ($numblocked > 0) {
                   1187:         my $beginblock = &Apache::lonlocal::locallocaltime($startblock);
                   1188:         my $finishblock = &Apache::lonlocal::locallocaltime($endblock);
                   1189:         $r->print('<br /><br />'.
                   1190:                   $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.'));
                   1191:         &build_block_table($r,$startblock,$endblock,\%setters);
                   1192:     }
1.14      www      1193: }
                   1194: 
1.15      www      1195: # ============================================================== Compose output
                   1196: 
                   1197: sub compout {
1.142     www      1198:     my ($r,$forwarding,$replying,$broadcast,$replycrit,$folder,$dismode)=@_;
1.121     www      1199:     my $suffix=&foldersuffix($folder);
1.92      www      1200: 
                   1201:     if ($broadcast eq 'individual') {
                   1202: 	&printheader($r,'/adm/email?compose=individual',
                   1203: 	     'Send a Message');
                   1204:     } elsif ($broadcast) {
                   1205: 	&printheader($r,'/adm/email?compose=group',
                   1206: 	     'Broadcast Message');
                   1207:     } elsif ($forwarding) {
                   1208: 	&Apache::lonhtmlcommon::add_breadcrumb
                   1209:         ({href=>"/adm/email?display=".&Apache::lonnet::escape($forwarding),
                   1210:           text=>"Display Message"});
                   1211: 	&printheader($r,'/adm/email?forward='.&Apache::lonnet::escape($forwarding),
                   1212: 	     'Forwarding a Message');
                   1213:     } elsif ($replying) {
                   1214: 	&Apache::lonhtmlcommon::add_breadcrumb
                   1215:         ({href=>"/adm/email?display=".&Apache::lonnet::escape($replying),
                   1216:           text=>"Display Message"});
                   1217: 	&printheader($r,'/adm/email?replyto='.&Apache::lonnet::escape($replying),
                   1218: 	     'Replying to a Message');
1.94      www      1219:     } elsif ($replycrit) {
                   1220: 	$r->print('<h3>'.&mt('Replying to a Critical Message').'</h3>');
                   1221: 	$replying=$replycrit;
1.92      www      1222:     } else {
                   1223: 	&printheader($r,'/adm/email?compose=upload',
                   1224: 	     'Distribute from Uploaded File');
                   1225:     }
                   1226: 
1.89      www      1227:     my $dispcrit='';
1.15      www      1228:     my $dissub='';
                   1229:     my $dismsg='';
1.115     www      1230:     my $disbase='';
1.67      www      1231:     my $func=&mt('Send New');
1.69      www      1232:     my %lt=&Apache::lonlocal::texthash('us' => 'Username',
                   1233: 				       'do' => 'Domain',
                   1234: 				       'ad' => 'Additional Recipients',
                   1235: 				       'sb' => 'Subject',
                   1236: 				       'ca' => 'Cancel',
                   1237: 				       'ma' => 'Mail');
                   1238: 
1.170     albertel 1239:     if (&Apache::lonnet::allowed('srm',$env{'request.course.id'})
                   1240: 	|| &Apache::lonnet::allowed('srm',$env{'request.course.id'}.
                   1241: 				    '/'.$env{'request.course.sec'})) {
1.35      bowersj2 1242: 	 my $crithelp = Apache::loncommon::help_open_topic("Course_Critical_Message");
1.15      www      1243:          $dispcrit=
1.136     albertel 1244:  '<p><label><input type="checkbox" name="critmsg" /> '.&mt('Send as critical message').'</label> ' . $crithelp . 
                   1245:  '</p><p>'.
                   1246:  '<label><input type="checkbox" name="sendbck" /> '.&mt('Send as critical message').'  ' .
                   1247:  &mt('and return receipt') . '</label>' . $crithelp . 
                   1248:  '</p><p><label><input type="checkbox" name="permanent" /> '.
1.154     www      1249: &mt('Send copy to permanent email address (if known)').'</label></p>'.
1.161     albertel 1250: '<!-- <p><label><input type="checkbox" name="rsspost" /> '.
                   1251: 		  &mt('Include in course RSS newsfeed').'</label></p>-->';      }
1.92      www      1252:     my %message;
                   1253:     my %content;
1.140     albertel 1254:     my $defdom=$env{'user.domain'};
1.15      www      1255:     if ($forwarding) {
1.121     www      1256: 	%message=&Apache::lonnet::get('nohist_email'.$suffix,[$forwarding]);
1.108     www      1257: 	%content=&unpackagemsg($message{$forwarding},$folder);
1.92      www      1258: 	$dispcrit.='<input type="hidden" name="forwid" value="'.
                   1259: 	    $forwarding.'" />';
                   1260: 	$func=&mt('Forward');
                   1261: 	
                   1262: 	$dissub=&mt('Forwarding').': '.$content{'subject'};
                   1263: 	$dismsg=&mt('Forwarded message from').' '.
                   1264: 	    $content{'sendername'}.' '.&mt('at').' '.$content{'senderdomain'};
1.115     www      1265: 	if ($content{'baseurl'}) {
                   1266: 	    $disbase='<input type="hidden" name="baseurl" value="'.&Apache::lonnet::escape($content{'baseurl'}).'" />';
                   1267: 	}
1.92      www      1268:     }
                   1269:     if ($replying) {
1.121     www      1270: 	%message=&Apache::lonnet::get('nohist_email'.$suffix,[$replying]);
1.108     www      1271: 	%content=&unpackagemsg($message{$replying},$folder);
1.105     albertel 1272: 	$dispcrit.='<input type="hidden" name="replyid" value="'.
                   1273: 	    $replying.'" />';
1.108     www      1274: 	$func=&mt('Send Reply to');
1.92      www      1275: 	
                   1276: 	$dissub=&mt('Reply').': '.$content{'subject'};       
                   1277: 	$dismsg='> '.$content{'message'};
                   1278: 	$dismsg=~s/\r/\n/g;
                   1279: 	$dismsg=~s/\f/\n/g;
                   1280: 	$dismsg=~s/\n+/\n\> /g;
1.115     www      1281: 	if ($content{'baseurl'}) {
                   1282: 	    $disbase='<input type="hidden" name="baseurl" value="'.&Apache::lonnet::escape($content{'baseurl'}).'" />';
1.140     albertel 1283: 	    if ($env{'user.adv'}) {
1.136     albertel 1284: 		$disbase.='<label><input type="checkbox" name="storebasecomment" />'.&mt('Store message for re-use').
                   1285: 		    '</label> <a href="/adm/email?showcommentbaseurl='.
1.120     www      1286: 		    &Apache::lonnet::escape($content{'baseurl'}).'" target="comments">'.
                   1287: 		    &mt('Show re-usable messages').'</a><br />';
1.115     www      1288: 	    }
                   1289: 	}
1.15      www      1290:     }
1.111     www      1291:     my $citation=&displayresource(%content);
1.140     albertel 1292:     if ($env{'form.recdom'}) { $defdom=$env{'form.recdom'}; }
1.22      www      1293:       $r->print(
1.31      matthew  1294:                 '<form action="/adm/email"  name="compemail" method="post"'.
                   1295:                 ' enctype="multipart/form-data">'."\n".
1.92      www      1296:                 '<input type="hidden" name="sendmail" value="on" />'."\n".
1.31      matthew  1297:                 '<table>');
1.22      www      1298:     unless (($broadcast eq 'group') || ($broadcast eq 'upload')) {
1.92      www      1299: 	if ($replying) {
                   1300: 	    $r->print('<tr><td colspan="2">'.&mt('Replying to').' '.
                   1301: 		      &Apache::loncommon::aboutmewrapper(
                   1302: 							 &Apache::loncommon::plainname($content{'sendername'},$content{'senderdomain'}),$content{'sendername'},$content{'senderdomain'}).' ('.
                   1303: 		      $content{'sendername'}.'@'.
                   1304: 		      $content{'senderdomain'}.')'.
                   1305: 		      '<input type="hidden" name="recuname" value="'.$content{'sendername'}.'" />'.
                   1306: 		      '<input type="hidden" name="recdomain" value="'.$content{'senderdomain'}.'" />'.
                   1307: 		      '</td></tr>');
                   1308: 	} else {
                   1309: 	    my $domform = &Apache::loncommon::select_dom_form($defdom,'recdomain');
                   1310: 	    my $selectlink=&Apache::loncommon::selectstudent_link
1.46      www      1311: 	    ('compemail','recuname','recdomain');
1.92      www      1312: 	    $r->print(<<"ENDREC");
1.140     albertel 1313: <tr><td>$lt{'us'}:</td><td><input type="text" size="12" name="recuname" value="$env{'form.recname'}" /></td><td rowspan="2">$selectlink</td></tr>
1.69      www      1314: <tr><td>$lt{'do'}:</td>
1.31      matthew  1315: <td>$domform</td></tr>
1.17      www      1316: ENDREC
1.92      www      1317:         }
1.17      www      1318:     }
1.55      bowersj2 1319:     my $latexHelp = Apache::loncommon::helpLatexCheatsheet();
1.31      matthew  1320:     if ($broadcast ne 'upload') {
1.22      www      1321:        $r->print(<<"ENDCOMP");
1.69      www      1322: <tr><td>$lt{'ad'}<br /><tt>username\@domain,username\@domain, ...
1.20      www      1323: </tt></td><td>
1.91      www      1324: <input type="text" size="50" name="additionalrec" /></td></tr>
                   1325: <tr><td>$lt{'sb'}:</td><td><input type="text" size="50" name="subject" value="$dissub" />
1.15      www      1326: </td></tr></table>
1.55      bowersj2 1327: $latexHelp
1.144     albertel 1328: <textarea name="message" id="message" cols="80" rows="15" wrap="hard">$dismsg
1.69      www      1329: </textarea></p><br />
1.15      www      1330: $dispcrit
1.115     www      1331: $disbase
1.142     www      1332: <input type="hidden" name="folder" value="$folder" />
                   1333: <input type="hidden" name="dismode" value="$dismode" />
1.69      www      1334: <input type="submit" name="send" value="$func $lt{'ma'}" />
1.111     www      1335: <input type="submit" name="cancel" value="$lt{'ca'}" /><hr />
                   1336: $citation
1.15      www      1337: ENDCOMP
1.31      matthew  1338:     } else { # $broadcast is 'upload'
1.22      www      1339: 	$r->print(<<ENDUPLOAD);
1.91      www      1340: <input type="hidden" name="sendmode" value="upload" />
1.86      www      1341: <input type="hidden" name="send" value="on" />
1.22      www      1342: <h3>Generate messages from a file</h3>
1.31      matthew  1343: <p>
1.91      www      1344: Subject: <input type="text" size="50" name="subject" />
1.31      matthew  1345: </p>
                   1346: <p>General message text<br />
1.144     albertel 1347: <textarea name="message" id="message" cols="60" rows="10" wrap="hard">$dismsg
1.31      matthew  1348: </textarea></p>
                   1349: <p>
                   1350: The file format for the uploaded portion of the message is:
1.22      www      1351: <pre>
                   1352: username1\@domain1: text
                   1353: username2\@domain2: text
1.31      matthew  1354: username3\@domain1: text
1.22      www      1355: </pre>
1.31      matthew  1356: </p>
                   1357: <p>
1.22      www      1358: The messages will be assembled from all lines with the respective 
1.31      matthew  1359: <tt>username\@domain</tt>, and appended to the general message text.</p>
                   1360: <p>
1.91      www      1361: <input type="file" name="upfile" size="40" /></p><p>
1.22      www      1362: $dispcrit
1.92      www      1363: <input type="submit" value="Upload and Send" /></p>
1.22      www      1364: ENDUPLOAD
                   1365:     }
1.17      www      1366:     if ($broadcast eq 'group') {
1.170     albertel 1367:        &discourse($r);
1.17      www      1368:     }
1.144     albertel 1369:     $r->print('</form>'.
1.153     www      1370: 	      &Apache::lonfeedback::generate_preview_button('compemail','message').
1.144     albertel 1371: 	      &Apache::lonhtmlcommon::htmlareaselectactive('message'));
1.15      www      1372: }
                   1373: 
1.45      www      1374: # ---------------------------------------------------- Display all face to face
                   1375: 
1.104     matthew  1376: sub retrieve_instructor_comments {
                   1377:     my ($user,$domain)=@_;
1.140     albertel 1378:     my $target=$env{'form.grade_target'};
                   1379:     if (! $env{'request.course.id'}) { return; }
1.170     albertel 1380:     if (! &Apache::lonnet::allowed('srm',$env{'request.course.id'})
                   1381: 	&& ! &Apache::lonnet::allowed('srm',$env{'request.course.id'}.
                   1382: 				      '/'.$env{'request.course.sec'})) {
1.104     matthew  1383: 	return;
                   1384:     }
                   1385:     my %records=&Apache::lonnet::dump('nohist_email',
1.140     albertel 1386: 			 $env{'course.'.$env{'request.course.id'}.'.domain'},
                   1387: 			 $env{'course.'.$env{'request.course.id'}.'.num'},
1.104     matthew  1388:                          '%255b'.$user.'%253a'.$domain.'%255d');
                   1389:     my $result='';
                   1390:     foreach (sort(keys(%records))) {
                   1391:         my %content=&unpackagemsg($records{$_});
                   1392:         next if ($content{'senderdomain'} eq '');
                   1393:         next if ($content{'subject'} !~ /^Record/);
1.145     albertel 1394: 	# &Apache::lonfeedback::newline_to_br(\$content{'message'});
                   1395: 	$result.='Recorded by '.
1.104     matthew  1396:             $content{'sendername'}.'@'.$content{'senderdomain'}."\n";
                   1397:         $result.=
                   1398:             &Apache::lontexconvert::msgtexconverted($content{'message'})."\n";
                   1399:      }
                   1400:     return $result;
                   1401: }
                   1402: 
1.45      www      1403: sub disfacetoface {
                   1404:     my ($r,$user,$domain)=@_;
1.140     albertel 1405:     my $target=$env{'form.grade_target'};
                   1406:     unless ($env{'request.course.id'}) { return; }
1.170     albertel 1407:     if  (!&Apache::lonnet::allowed('srm',$env{'request.course.id'})
                   1408: 	 && ! &Apache::lonnet::allowed('srm',$env{'request.course.id'}.
                   1409: 				       '/'.$env{'request.course.sec'})) {
                   1410: 	$r->print('Not allowed');
1.45      www      1411: 	return;
                   1412:     }
                   1413:     my %records=&Apache::lonnet::dump('nohist_email',
1.140     albertel 1414: 			 $env{'course.'.$env{'request.course.id'}.'.domain'},
                   1415: 			 $env{'course.'.$env{'request.course.id'}.'.num'},
1.45      www      1416:                          '%255b'.$user.'%253a'.$domain.'%255d');
                   1417:     my $result='';
                   1418:     foreach (sort keys %records) {
                   1419:         my %content=&unpackagemsg($records{$_});
                   1420:         next if ($content{'senderdomain'} eq '');
1.145     albertel 1421: 	&Apache::lonfeedback::newline_to_br(\$content{'message'});
1.45      www      1422:         if ($content{'subject'}=~/^Record/) {
1.69      www      1423: 	    $result.='<h3>'.&mt('Record').'</h3>';
1.102     raeburn  1424:         } elsif ($content{'subject'}=~/^Broadcast/) {
                   1425:             $result .='<h3>'.&mt('Broadcast Message').'</h3>';
1.159     raeburn  1426:             if ($content{'subject'}=~/^Broadcast\./) {
                   1427:                 %content=&unpackagemsg($content{'message'});
                   1428:                 $content{'message'}=
                   1429:                     '<b>'.&mt('Subject').': '.$content{'subject'}.'</b><br />'.
                   1430:                     $content{'message'};
                   1431:             }    
1.45      www      1432:         } else {
1.102     raeburn  1433:             $result.='<h3>'.&mt('Critical Message').'</h3>';
1.45      www      1434:             %content=&unpackagemsg($content{'message'});
                   1435:             $content{'message'}=
1.92      www      1436:                 '<b>'.&mt('Subject').': '.$content{'subject'}.'</b><br />'.
1.45      www      1437: 		$content{'message'};
                   1438:         }
1.69      www      1439:         $result.=&mt('By').': <b>'.
1.45      www      1440: &Apache::loncommon::aboutmewrapper(
                   1441:  &Apache::loncommon::plainname($content{'sendername'},$content{'senderdomain'}),$content{'sendername'},$content{'senderdomain'}).'</b> ('.
                   1442: $content{'sendername'}.'@'.
                   1443:             $content{'senderdomain'}.') '.$content{'time'}.
1.130     albertel 1444:             '<br /><pre>'.
1.45      www      1445:               &Apache::lontexconvert::msgtexconverted($content{'message'}).
1.130     albertel 1446: 	      '</pre>';
1.45      www      1447:      }
                   1448:     # Check to see if there were any messages.
                   1449:     if ($result eq '') {
1.98      sakharuk 1450: 	if ($target ne 'tex') { 
1.102     raeburn  1451: 	    $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 1452: 	} else {
1.102     raeburn  1453: 	    $r->print('\textbf{'.&mt("No notes, face-to-face discussion records, critical messages or broadcast messages in this course.").'}\\\\');
1.98      sakharuk 1454: 	}
1.45      www      1455:     } else {
                   1456:        $r->print($result);
                   1457:     }
                   1458: }
                   1459: 
1.44      www      1460: # ---------------------------------------------------------------- Face to face
                   1461: 
                   1462: sub facetoface {
                   1463:     my ($r,$stage)=@_;
1.170     albertel 1464:     if (!&Apache::lonnet::allowed('srm',$env{'request.course.id'})
                   1465: 	&& ! &Apache::lonnet::allowed('srm',$env{'request.course.id'}.
                   1466: 				      '/'.$env{'request.course.sec'})) {
                   1467: 	$r->print('Not allowed');
1.44      www      1468: 	return;
                   1469:     }
1.89      www      1470:     &printheader($r,
                   1471: 		 '/adm/email?recordftf=query',
1.102     raeburn  1472: 		 "User Notes, Face-to-Face, Critical Messages, Broadcast Messages");
1.46      www      1473: # from query string
1.88      www      1474: 
1.140     albertel 1475:     if ($env{'form.recname'}) { $env{'form.recuname'}=$env{'form.recname'}; }
                   1476:     if ($env{'form.recdom'}) { $env{'form.recdomain'}=$env{'form.recdom'}; }
1.46      www      1477: 
1.140     albertel 1478:     my $defdom=$env{'user.domain'};
1.46      www      1479: # already filled in
1.140     albertel 1480:     if ($env{'form.recdomain'}) { $defdom=$env{'form.recdomain'}; }
1.46      www      1481: # generate output
1.44      www      1482:     my $domform = &Apache::loncommon::select_dom_form($defdom,'recdomain');
1.46      www      1483:     my $stdbrws = &Apache::loncommon::selectstudent_link
                   1484: 	('stdselect','recuname','recdomain');
1.88      www      1485:     my %lt=&Apache::lonlocal::texthash('user' => 'Username',
                   1486: 				       'dom' => 'Domain',
1.102     raeburn  1487: 				       'head' => 'User Notes, Records of Face-To-Face Discussions, Critical Messages, and Broadcast Messages in Course',
1.88      www      1488: 				       'subm' => 'Retrieve discussion and message records',
                   1489: 				       'newr' => 'New Record (record is visible to course faculty and staff)',
                   1490: 				       'post' => 'Post this Record');
1.44      www      1491:     $r->print(<<"ENDTREC");
1.88      www      1492: <h3>$lt{'head'}</h3>
1.46      www      1493: <form method="post" action="/adm/email" name="stdselect">
1.44      www      1494: <input type="hidden" name="recordftf" value="retrieve" />
                   1495: <table>
1.140     albertel 1496: <tr><td>$lt{'user'}:</td><td><input type="text" size="12" name="recuname" value="$env{'form.recuname'}" /></td>
1.44      www      1497: <td rowspan="2">
1.46      www      1498: $stdbrws
1.88      www      1499: <input type="submit" value="$lt{'subm'}" /></td>
1.44      www      1500: </tr>
1.88      www      1501: <tr><td>$lt{'dom'}:</td>
1.44      www      1502: <td>$domform</td></tr>
                   1503: </table>
                   1504: </form>
                   1505: ENDTREC
                   1506:     if (($stage ne 'query') &&
1.140     albertel 1507:         ($env{'form.recdomain'}) && ($env{'form.recuname'})) {
                   1508:         chomp($env{'form.newrecord'});
                   1509:         if ($env{'form.newrecord'}) {
1.159     raeburn  1510:            my $recordtxt = $env{'form.newrecord'};
1.45      www      1511:            &user_normal_msg_raw(
1.140     albertel 1512:             $env{'course.'.$env{'request.course.id'}.'.num'},
                   1513:             $env{'course.'.$env{'request.course.id'}.'.domain'},
1.88      www      1514:             &mt('Record').
1.140     albertel 1515: 	     ' ['.$env{'form.recuname'}.':'.$env{'form.recdomain'}.']',
1.159     raeburn  1516: 	    $recordtxt);
1.44      www      1517:         }
1.140     albertel 1518:         $r->print('<h3>'.&Apache::loncommon::plainname($env{'form.recuname'},
                   1519: 				     $env{'form.recdomain'}).'</h3>');
                   1520:         &disfacetoface($r,$env{'form.recuname'},$env{'form.recdomain'});
1.44      www      1521: 	$r->print(<<ENDRHEAD);
                   1522: <form method="post" action="/adm/email">
1.140     albertel 1523: <input name="recdomain" value="$env{'form.recdomain'}" type="hidden" />
                   1524: <input name="recuname" value="$env{'form.recuname'}" type="hidden" />
1.44      www      1525: ENDRHEAD
                   1526:         $r->print(<<ENDBFORM);
1.88      www      1527: <hr />$lt{'newr'}<br />
1.44      www      1528: <textarea name="newrecord" cols="80" rows="10" wrap="hard"></textarea>
1.45      www      1529: <br />
                   1530: <input type="hidden" name="recordftf" value="post" />
1.88      www      1531: <input type="submit" value="$lt{'post'}" />
1.44      www      1532: </form>
                   1533: ENDBFORM
                   1534:     }
                   1535: }
1.91      www      1536: 
1.101     raeburn  1537: # ----------------------------------------------------------- Blocking during exams
                   1538: 
                   1539: sub examblock {
                   1540:     my ($r,$action) = @_;
1.140     albertel 1541:     unless ($env{'request.course.id'}) { return;}
1.170     albertel 1542:     if (!&Apache::lonnet::allowed('srm',$env{'request.course.id'})
                   1543: 	&& ! &Apache::lonnet::allowed('srm',$env{'request.course.id'}.
                   1544: 				      '/'.$env{'request.course.sec'})) {
                   1545: 	$r->print('Not allowed');
                   1546: 	return;
                   1547:     }
1.101     raeburn  1548:     my %lt=&Apache::lonlocal::texthash(
                   1549:             'comb' => 'Communication Blocking',
                   1550:             'cbds' => 'Communication blocking during scheduled exams',
                   1551:             '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.',
                   1552:              'mecb' => 'Modify existing communication blocking periods',
                   1553:              'ncbc' => 'No communication blocks currently stored'
                   1554:     );
                   1555: 
                   1556:     my %ltext = &Apache::lonlocal::texthash(
                   1557:             'dura' => 'Duration',
                   1558:             'setb' => 'Set by',
                   1559:             'even' => 'Event',
                   1560:             'actn' => 'Action',
                   1561:             'star' => 'Start',
                   1562:             'endd' => 'End'
                   1563:     );
                   1564: 
                   1565:     &printheader($r,'/adm/email?block=display',$lt{'comb'});
                   1566:     $r->print('<h3>'.$lt{'cbds'}.'</h3>');
                   1567: 
                   1568:     if ($action eq 'store') {
                   1569:         &blockstore($r);
                   1570:     }
                   1571: 
                   1572:     $r->print($lt{'desc'}.'<br /><br />
                   1573:                <form name="blockform" method="post" action="/adm/email?block=store">
                   1574:              ');
                   1575: 
                   1576:     $r->print('<h4>'.$lt{'mecb'}.'</h4>');
                   1577:     my %records = ();
                   1578:     my $blockcount = 0;
                   1579:     my $parmcount = 0;
                   1580:     &get_blockdates(\%records,\$blockcount);
                   1581:     if ($blockcount > 0) {
                   1582:         $parmcount = &display_blocker_status($r,\%records,\%ltext);
                   1583:     } else {
                   1584:         $r->print($lt{'ncbc'}.'<br /><br />');
                   1585:     }
                   1586:     &display_addblocker_table($r,$parmcount,\%ltext);
1.139     albertel 1587:     my $endbody=&Apache::loncommon::endbodytag();
1.101     raeburn  1588:     $r->print(<<"END");
                   1589: <br />
                   1590: <input type="hidden" name="blocktotal" value="$blockcount" />
                   1591: <input type ="submit" value="Save Changes" />
                   1592: </form>
1.139     albertel 1593: $endbody
1.101     raeburn  1594: </html>
                   1595: END
                   1596:     return;
                   1597: }
                   1598: 
                   1599: sub blockstore {
                   1600:     my $r = shift;
                   1601:     my %lt=&Apache::lonlocal::texthash(
                   1602:             'tfcm' => 'The following changes were made',
                   1603:             'cbps' => 'communication blocking period(s)',
                   1604:             'werm' => 'was/were removed',
                   1605:             'wemo' => 'was/were modified',
                   1606:             'wead' => 'was/were added',
                   1607:             'ncwm' => 'No changes were made.' 
                   1608:     );
                   1609:     my %adds = ();
                   1610:     my %removals = ();
                   1611:     my %cancels = ();
                   1612:     my $modtotal = 0;
                   1613:     my $canceltotal = 0;
                   1614:     my $addtotal = 0;
                   1615:     my %blocking = ();
                   1616:     $r->print('<h3>'.$lt{'head'}.'</h3>');
1.140     albertel 1617:     foreach (keys %env) {
1.101     raeburn  1618:         if ($_ =~ m/^form\.modify_(\w+)$/) {
                   1619:             $adds{$1} = $1;
                   1620:             $removals{$1} = $1;
                   1621:             $modtotal ++;
                   1622:         } elsif ($_ =~ m/^form\.cancel_(\d+)$/) {
                   1623:             $cancels{$1} = $1;
                   1624:             unless ( defined($removals{$1}) ) {
                   1625:                 $removals{$1} = $1;
                   1626:                 $canceltotal ++;
                   1627:             }
                   1628:         } elsif ($_ =~ m/^form\.add_(\d+)$/) {
                   1629:             $adds{$1} = $1;
                   1630:             $addtotal ++;
                   1631:         }
                   1632:     }
                   1633: 
                   1634:     foreach (keys %removals) {
1.140     albertel 1635:         my $hashkey = $env{'form.key_'.$_};
1.101     raeburn  1636:         &Apache::lonnet::del('comm_block',["$hashkey"],
1.140     albertel 1637:                          $env{'course.'.$env{'request.course.id'}.'.domain'},
                   1638:                          $env{'course.'.$env{'request.course.id'}.'.num'}
1.101     raeburn  1639:                          );
                   1640:     }
                   1641:     foreach (keys %adds) {
                   1642:         unless ( defined($cancels{$_}) ) {
                   1643:             my ($newstart,$newend) = &get_dates_from_form($_);
                   1644:             my $newkey = $newstart.'____'.$newend;
1.140     albertel 1645:             $blocking{$newkey} = $env{'user.name'}.'@'.$env{'user.domain'}.':'.$env{'form.title_'.$_};
1.101     raeburn  1646:         }
                   1647:     }
                   1648:     if ($addtotal + $modtotal > 0) {
                   1649:         &Apache::lonnet::put('comm_block',\%blocking,
1.140     albertel 1650:                      $env{'course.'.$env{'request.course.id'}.'.domain'},
                   1651:                      $env{'course.'.$env{'request.course.id'}.'.num'}
1.101     raeburn  1652:                      );
                   1653:     }
                   1654:     my $chgestotal = $canceltotal + $modtotal + $addtotal;
                   1655:     if ($chgestotal > 0) {
                   1656:         $r->print($lt{'tfcm'}.'<ul>');
                   1657:         if ($canceltotal > 0) {
                   1658:             $r->print('<li>'.$canceltotal.' '.$lt{'cbps'},' '.$lt{'werm'}.'</li>');
                   1659:         }
                   1660:         if ($modtotal > 0) {
                   1661:             $r->print('<li>'.$modtotal.' '.$lt{'cbps'},' '.$lt{'wemo'}.'</li>');
                   1662:         }
                   1663:         if ($addtotal > 0) {
                   1664:             $r->print('<li>'.$addtotal.' '.$lt{'cbps'},' '.$lt{'wead'}.'</li>');
                   1665:         }
                   1666:         $r->print('</ul>');
                   1667:     } else {
                   1668:         $r->print($lt{'ncwm'});
                   1669:     }
                   1670:     $r->print('<br />');
                   1671:     return;
                   1672: }
                   1673: 
                   1674: sub get_dates_from_form {
                   1675:     my $item = shift;
                   1676:     my $startdate = &Apache::lonhtmlcommon::get_date_from_form('startdate_'.$item);
                   1677:     my $enddate   = &Apache::lonhtmlcommon::get_date_from_form('enddate_'.$item);
                   1678:     return ($startdate,$enddate);
                   1679: }
                   1680: 
                   1681: sub get_blockdates {
                   1682:     my ($records,$blockcount) = @_;
                   1683:     $$blockcount = 0;
                   1684:     %{$records} = &Apache::lonnet::dump('comm_block',
1.140     albertel 1685:                          $env{'course.'.$env{'request.course.id'}.'.domain'},
                   1686:                          $env{'course.'.$env{'request.course.id'}.'.num'}
1.101     raeburn  1687:                          );
                   1688:     $$blockcount = keys %{$records};
                   1689:                                                                                                              
                   1690:     foreach (keys %{$records}) {
                   1691:         if ($_ eq 'error: 2 tie(GDBM) Failed while attempting dump') {
                   1692:             $$blockcount = 0;
                   1693:             last;
                   1694:         }
                   1695:     }
                   1696: }
                   1697: 
                   1698: sub display_blocker_status {
                   1699:     my ($r,$records,$ltext) = @_;
                   1700:     my $parmcount = 0;
                   1701:     my @bgcols = ("#eeeeee","#dddddd");
                   1702:     my $function = &Apache::loncommon::get_users_function();
                   1703:     my $color = &Apache::loncommon::designparm($function.'.tabbg',
1.140     albertel 1704:                                                     $env{'user.domain'});
1.101     raeburn  1705:     my %lt = &Apache::lonlocal::texthash(
                   1706:         'modi' => 'Modify',
                   1707:         'canc' => 'Cancel',
                   1708:     );
                   1709:     $r->print(<<"END");
                   1710: <table border="0" cellpadding="0" cellspacing="0">
                   1711:  <tr>
                   1712:   <td width="100%" bgcolor="#000000">
                   1713:    <table width="100%" border="0" cellpadding="1" cellspacing="0">
                   1714:     <tr>
                   1715:      <td width="100%" bgcolor="#000000">
                   1716:       <table border="0" cellpadding="3" cellspacing="3" bgcolor="#FFFFFF">
                   1717:        <tr bgcolor="$color">
                   1718:         <td><b>$$ltext{'dura'}</b></td>
                   1719:         <td><b>$$ltext{'setb'}</b></td>
                   1720:         <td><b>$$ltext{'even'}</b></td>
                   1721:         <td><b>$$ltext{'actn'}?</b></td>
                   1722:        </tr>
                   1723: END
                   1724:     foreach (sort keys %{$records}) {
                   1725:         my $iter = $parmcount%2;
                   1726:         my $onchange = 'onFocus="javascript:window.document.forms['.
                   1727:                        "'blockform'].elements['modify_".$parmcount."'].".
                   1728:                        'checked=true;"';
                   1729:         my ($start,$end) = split/____/,$_;
                   1730:         my $startform = &Apache::lonhtmlcommon::date_setter('blockform','startdate_'.$parmcount,$start,$onchange);
                   1731:         my $endform = &Apache::lonhtmlcommon::date_setter('blockform','enddate_'.$parmcount,$end,$onchange);
                   1732:         my ($setter,$title) = split/:/,$$records{$_};
                   1733:         my ($setuname,$setudom) = split/@/,$setter;
                   1734:         my $settername = &Apache::loncommon::plainname($setuname,$setudom);
                   1735:         $r->print(<<"END");
                   1736:        <tr bgcolor="$bgcols[$iter]">
                   1737:         <td>$$ltext{'star'}:&nbsp;$startform<br/>$$ltext{'endd'}:&nbsp;&nbsp;$endform</td>
                   1738:         <td>$settername</td>
1.136     albertel 1739:         <td><input type="text" name="title_$parmcount" size="15" value="$title" /><input type="hidden" name="key_$parmcount" value="$_" /></td>
                   1740:         <td><label>$lt{'modi'}?&nbsp;<input type="checkbox" name="modify_$parmcount" /></label><br /><label>$lt{'canc'}?&nbsp;&nbsp;<input type="checkbox" name="cancel_$parmcount" /></label>
1.101     raeburn  1741:        </tr>
                   1742: END
                   1743:         $parmcount ++;
                   1744:     }
                   1745:     $r->print(<<"END");
                   1746:       </table>
                   1747:      </td>
                   1748:     </tr>
                   1749:    </table>
                   1750:   </td>
                   1751:  </tr>
                   1752: </table>
                   1753: <br />
                   1754: <br />
                   1755: END
                   1756:     return $parmcount;
                   1757: }
                   1758: 
                   1759: sub display_addblocker_table {
                   1760:     my ($r,$parmcount,$ltext) = @_;
                   1761:     my $start = time;
                   1762:     my $end = $start + (60 * 60 * 2); #Default is an exam of 2 hours duration.
                   1763:     my $onchange = 'onFocus="javascript:window.document.forms['.
                   1764:                    "'blockform'].elements['add_".$parmcount."'].".
                   1765:                    'checked=true;"';
                   1766:     my $startform = &Apache::lonhtmlcommon::date_setter('blockform','startdate_'.$parmcount,$start,$onchange);
                   1767:     my $endform = &Apache::lonhtmlcommon::date_setter('blockform','enddate_'.$parmcount,$end,$onchange);
                   1768:     my $function = &Apache::loncommon::get_users_function();
                   1769:     my $color = &Apache::loncommon::designparm($function.'.tabbg',
1.140     albertel 1770:                                                     $env{'user.domain'});
1.101     raeburn  1771:     my %lt = &Apache::lonlocal::texthash(
                   1772:         'addb' => 'Add block',
                   1773:         'exam' => 'e.g., Exam 1',
                   1774:         'addn' => 'Add new communication blocking periods'
                   1775:     );
                   1776:     $r->print(<<"END");
                   1777: <h4>$lt{'addn'}</h4> 
                   1778: <table border="0" cellpadding="0" cellspacing="0">
                   1779:  <tr>
                   1780:   <td width="100%" bgcolor="#000000">
                   1781:    <table width="100%" border="0" cellpadding="1" cellspacing="0">
                   1782:     <tr>
                   1783:      <td width="100%" bgcolor="#000000">
                   1784:       <table border="0" cellpadding="3" cellspacing="3" bgcolor="#FFFFFF">
                   1785:        <tr bgcolor="#CCCCFF">
                   1786:         <td><b>$$ltext{'dura'}</b></td>
                   1787:         <td><b>$$ltext{'even'} $lt{'exam'}</b></td>
                   1788:         <td><b>$$ltext{'actn'}?</b></td>
                   1789:        </tr>
                   1790:        <tr bgcolor="#eeeeee">
                   1791:         <td>$$ltext{'star'}:&nbsp;$startform<br />$$ltext{'endd'}:&nbsp;&nbsp;$endform</td>
1.136     albertel 1792:         <td><input type="text" name="title_$parmcount" size="15" value="" /></td>
                   1793:         <td><label>$lt{'addb'}?&nbsp;<input type="checkbox" name="add_$parmcount" value="1" /></label></td>
1.101     raeburn  1794:        </tr>
                   1795:       </table>
                   1796:      </td>
                   1797:     </tr>
                   1798:    </table>
                   1799:   </td>
                   1800:  </tr>
                   1801: </table>
                   1802: END
                   1803:     return;
                   1804: }
                   1805: 
                   1806: sub blockcheck {
                   1807:     my ($setters,$startblock,$endblock) = @_;
                   1808:     # Retrieve active student roles and active course coordinator/instructor roles
                   1809:     my @livecses = ();
                   1810:     my @staffcses = ();
                   1811:     $$startblock = 0;
                   1812:     $$endblock = 0;
1.140     albertel 1813:     foreach (keys %env) {
1.101     raeburn  1814:         if ($_ =~ m-^user\.role\.(st|cc|in)\./(.+)$-) {
                   1815:             my $role = $1;
                   1816:             my $cse = $2;
                   1817:             $cse =~ s|/|_|;
1.140     albertel 1818:             if ($env{$_} =~ m/^(\d*)\.(\d*)$/) {
1.101     raeburn  1819:                 unless (($2 > 0 && $2 < time) || ($1 > time)) {
                   1820:                     if ($role eq 'st') {
                   1821:                         push @livecses, $cse;
                   1822:                     } else {
                   1823:                         unless (grep/^$cse$/,@staffcses) {
                   1824:                             push @staffcses, $cse;
                   1825:                         }
                   1826:                     }
                   1827:                 }
                   1828:             }
                   1829:         } elsif ($_ =~ m-user\.role\.cr/(\w+)/(\w+)/([^/]+)\./(.+)$- ) { 
1.140     albertel 1830:             my $rolepriv = $env{'user.role..rolesdef_'.$3};
1.101     raeburn  1831:         }
                   1832:     }
                   1833:     # Retrieve blocking times and identity of blocker for active courses for students.
                   1834:     if (@livecses > 0) {
                   1835:         foreach my $cse (@livecses) {
                   1836:             my ($cdom,$crs) = split/_/,$cse;
1.140     albertel 1837:             if ( (grep/^$cse$/,@staffcses) && ($env{'request.role'} !~ m-^st\./$cdom/$crs$-) ) {
1.101     raeburn  1838:                 next;
                   1839:             } else {
                   1840:                 %{$$setters{$cse}} = ();
                   1841:                 @{$$setters{$cse}{'staff'}} = ();
                   1842:                 @{$$setters{$cse}{'times'}} = ();
                   1843:                 my %records = &Apache::lonnet::dump('comm_block',$cdom,$crs);
                   1844:                 foreach (keys %records) {
                   1845:                     if ($_ =~ m/^(\d+)____(\d+)$/) {
                   1846:                         if ($1 <= time && $2 >= time) {
                   1847:                             my ($staff,$title) = split/:/,$records{$_};
                   1848:                             push @{$$setters{$cse}{'staff'}}, $staff;
                   1849:                             push @{$$setters{$cse}{'times'}}, $_;
                   1850:                             if ( ($$startblock == 0) || ($$startblock > $1) ) {
                   1851:                                 $$startblock = $1;
                   1852:                             }
                   1853:                             if ( ($$endblock == 0) || ($$endblock < $2) ) {
                   1854:                                 $$endblock = $2;
                   1855:                             }
                   1856:                         }
                   1857:                     }
                   1858:                 }
                   1859:             }
                   1860:         }
                   1861:     }
                   1862: }
                   1863: 
                   1864: sub build_block_table {
                   1865:     my ($r,$startblock,$endblock,$setters) = @_;
                   1866:     my $function = &Apache::loncommon::get_users_function();
                   1867:     my $color = &Apache::loncommon::designparm($function.'.tabbg',
1.140     albertel 1868:                                                     $env{'user.domain'});
1.101     raeburn  1869:     my %lt = &Apache::lonlocal::texthash(
                   1870:         'cacb' => 'Currently active communication blocks',
                   1871:         'cour' => 'Course',
                   1872:         'dura' => 'Duration',
                   1873:         'blse' => 'Block set by'
                   1874:     ); 
                   1875:     $r->print(<<"END");
                   1876: <br /<br />$lt{'cacb'}:<br /><br />
                   1877: <table border="0" cellpadding="0" cellspacing="0">
                   1878:  <tr>
                   1879:   <td width="100%" bgcolor="#000000">
                   1880:    <table width="100%" border="0" cellpadding="1" cellspacing="0">
                   1881:     <tr>
                   1882:      <td width="100%" bgcolor="#000000">
                   1883:       <table border="0" cellpadding="3" cellspacing="3" bgcolor="#FFFFFF">
                   1884:        <tr bgcolor="$color">
                   1885:         <td><b>$lt{'cour'}</b></td>
                   1886:         <td><b>$lt{'dura'}</b></td>
                   1887:         <td><b>$lt{'blse'}</b></td>
                   1888:        </tr>
                   1889: END
                   1890:     foreach (keys %{$setters}) {
                   1891:         my %courseinfo=&Apache::lonnet::coursedescription($_);
                   1892:         for (my $i=0; $i<@{$$setters{$_}{staff}}; $i++) {
                   1893:             my ($uname,$udom) = split/\@/,$$setters{$_}{staff}[$i];
                   1894:             my $fullname = &Apache::loncommon::plainname($uname,$udom);
                   1895:             my ($openblock,$closeblock) = split/____/,$$setters{$_}{times}[$i];
                   1896:             $openblock = &Apache::lonlocal::locallocaltime($openblock);
                   1897:             $closeblock= &Apache::lonlocal::locallocaltime($closeblock);
                   1898:             $r->print('<tr><td>'.$courseinfo{'description'}.'</td>'.
                   1899:                       '<td>'.$openblock.' to '.$closeblock.'</td>'.
                   1900:                       '<td>'.$fullname.' ('.$uname.'@'.$udom.
                   1901:                       ')</td></tr>');
                   1902:         }
                   1903:     }
                   1904:     $r->print('</table></td></tr></table></td></tr></table>');
                   1905: }
                   1906: 
1.90      www      1907: # ----------------------------------------------------------- Display a message
                   1908: 
                   1909: sub displaymessage {
1.106     www      1910:     my ($r,$msgid,$folder)=@_;
                   1911:     my $suffix=&foldersuffix($folder);
1.101     raeburn  1912:     my %blocked = ();
                   1913:     my %setters = ();
                   1914:     my $startblock = 0;
                   1915:     my $endblock = 0;
                   1916:     my $numblocked = 0;
                   1917: # info to generate "next" and "previous" buttons and check if message is blocked
                   1918:     &blockcheck(\%setters,\$startblock,\$endblock);
1.107     www      1919:     my @messages=&sortedmessages(\%blocked,$startblock,$endblock,\$numblocked,$folder);
1.101     raeburn  1920:     if ( $blocked{$msgid} eq 'ON' ) {
                   1921:         &printheader($r,'/adm/email',&mt('Display a Message'));
                   1922:         $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.'));
                   1923:         &build_block_table($r,$startblock,$endblock,\%setters);
                   1924:         return;
                   1925:     }
1.107     www      1926:     &statuschange($msgid,'read',$folder);
1.106     www      1927:     my %message=&Apache::lonnet::get('nohist_email'.$suffix,[$msgid]);
1.90      www      1928:     my %content=&unpackagemsg($message{$msgid});
1.107     www      1929: 
1.90      www      1930:     my $counter=0;
                   1931:     $r->print('<pre>');
                   1932:     my $escmsgid=&Apache::lonnet::escape($msgid);
                   1933:     foreach (@messages) {
                   1934: 	if ($_->[5] eq $escmsgid){
                   1935: 	    last;
                   1936: 	}
                   1937: 	$counter++;
                   1938:     }
                   1939:     $r->print('</pre>');
                   1940:     my $number_of_messages = scalar(@messages); #subtract 1 for last index
                   1941: # start output
1.92      www      1942:     &printheader($r,'/adm/email?display='.&Apache::lonnet::escape($msgid),'Display a Message','',$content{'baseurl'});
1.90      www      1943:     my %courseinfo=&Apache::lonnet::coursedescription($content{'courseid'});
                   1944: # Functions
                   1945:     $r->print('<table border="2" width="100%"><tr bgcolor="#FFFFAA"><td>'.&mt('Functions').':</td>'.
                   1946: 	      '<td><a href="/adm/email?replyto='.&Apache::lonnet::escape($msgid).$sqs.
                   1947: 	      '"><b>'.&mt('Reply').'</b></a></td>'.
                   1948: 	      '<td><a href="/adm/email?forward='.&Apache::lonnet::escape($msgid).$sqs.
                   1949: 	      '"><b>'.&mt('Forward').'</b></a></td>'.
                   1950: 	      '<td><a href="/adm/email?markunread='.&Apache::lonnet::escape($msgid).$sqs.
                   1951: 	      '"><b>'.&mt('Mark Unread').'</b></a></td>'.
                   1952: 	      '<td><a href="/adm/email?markdel='.&Apache::lonnet::escape($msgid).$sqs.
1.148     www      1953: 	      '"><b>'.&mt('Delete').'</b></a></td>'.
1.125     www      1954: 	      '<td><a href="/adm/email?'.$sqs.
1.140     albertel 1955: 	      ($env{'form.dismode'} eq 'new'?'&folder=new':'').
1.125     www      1956: 	      '"><b>'.&mt('Back to Folder Display').'</b></a></td>');
1.90      www      1957:     if ($counter > 0){
                   1958: 	$r->print('<td><a href="/adm/email?display='.$messages[$counter-1]->[5].$sqs.
                   1959: 		  '"><b>'.&mt('Previous').'</b></a></td>');
                   1960:     }
                   1961:     if ($counter < $number_of_messages - 1){
                   1962: 	$r->print('<td><a href="/adm/email?display='.$messages[$counter+1]->[5].$sqs.
                   1963: 		  '"><b>'.&mt('Next').'</b></a></td>');
                   1964:     }
                   1965:     $r->print('</tr></table>');
1.146     www      1966:     if ($env{'user.adv'}) {
                   1967: 	$r->print('<table border="2" width="100%"><tr bgcolor="#FFAAAA"><td>'.&mt('Currently available actions (will open extra window)').':</td>');
1.151     www      1968: 	my $symb=&Apache::lonnet::symbread($content{'baseurl'});      
1.146     www      1969: 	if (&Apache::lonnet::allowed('vgr',$env{'request.course.id'})) {
                   1970: 		$r->print('<td><b>'.&Apache::loncommon::track_student_link(&mt('View recent activity'),$content{'sendername'},$content{'senderdomain'},'check').'</b></td>');
                   1971: 	    }
1.151     www      1972: 	if (&Apache::lonnet::allowed('opa',$env{'request.course.id'}) && $symb) {
1.147     www      1973: 	    $r->print('<td><b>'.&Apache::loncommon::pprmlink(&mt('Set/Change parameters'),$content{'sendername'},$content{'senderdomain'},$symb,'check').'</b></td>');
1.146     www      1974: 	}
1.151     www      1975: 	if (&Apache::lonnet::allowed('mgr',$env{'request.course.id'}) && $symb) {
1.147     www      1976: 	    $r->print('<td><b>'.&Apache::loncommon::pgrdlink(&mt('Set/Change grades'),$content{'sendername'},$content{'senderdomain'},$symb,'check').'</b></td>');
1.146     www      1977: 	}
                   1978: 	$r->print('</tr></table>');
                   1979:     }
1.156     raeburn  1980:     my $tolist;
                   1981:     my @recipients = ();
                   1982:     for (my $i=0; $i<@{$content{'recuser'}}; $i++) {
                   1983:         $recipients[$i] =  &Apache::loncommon::aboutmewrapper(
                   1984:            &Apache::loncommon::plainname($content{'recuser'}[$i],
                   1985:                                       $content{'recdomain'}[$i]),
                   1986:               $content{'recuser'}[$i],$content{'recdomain'}[$i]).
                   1987:        ' ('.$content{'recuser'}[$i].' at '.$content{'recdomain'}[$i].') ';
                   1988:     }
                   1989:     $tolist = join(', ',@recipients);
1.90      www      1990:     $r->print('<br /><b>'.&mt('Subject').':</b> '.$content{'subject'}.
1.108     www      1991: 	      ($folder ne 'sent'?'<br /><b>'.&mt('From').':</b> '.
1.90      www      1992: 	      &Apache::loncommon::aboutmewrapper(
                   1993: 						 &Apache::loncommon::plainname($content{'sendername'},$content{'senderdomain'}),
                   1994: 						 $content{'sendername'},$content{'senderdomain'}).' ('.
                   1995: 	      $content{'sendername'}.' at '.
1.108     www      1996: 	      $content{'senderdomain'}.') ':'<br /><b>'.&mt('To').':</b> '.
1.156     raeburn  1997:               $tolist).
1.90      www      1998: 	      ($content{'courseid'}?'<br /><b>'.&mt('Course').':</b> '.$courseinfo{'description'}.
                   1999: 	       ($content{'coursesec'}?' ('.&mt('Group/Section').': '.$content{'coursesec'}.')':''):'').
                   2000: 	      '<br /><b>'.&mt('Time').':</b> '.$content{'time'}.
1.115     www      2001: 	      ($content{'baseurl'}?'<br /><b>'.&mt('Refers to').':</b> <a href="'.$content{'baseurl'}.'">'.
                   2002: 	       $content{'baseurl'}.' ('.&Apache::lonnet::gettitle($content{'baseurl'}).')</a>':'').
1.90      www      2003: 	      '<p><pre>'.
                   2004: 	      &Apache::lontexconvert::msgtexconverted($content{'message'},1).
1.111     www      2005: 	      '</pre><hr />'.&displayresource(%content).'</p>');
1.90      www      2006:     return;   
                   2007: }
1.44      www      2008: 
1.111     www      2009: # =========================================================== Show the citation
                   2010: 
                   2011: sub displayresource {
                   2012:     my %content=@_;
                   2013: #
                   2014: # If the recipient is in the same course that the message was sent from and
                   2015: # has sufficient privileges, show "all details," else show citation
                   2016: #
1.140     albertel 2017:     if (($env{'request.course.id'} eq $content{'courseid'})
1.111     www      2018:      && (&Apache::lonnet::allowed('vgr',$content{'courseid'}))) {
                   2019: 	my $symb=&Apache::lonnet::symbread($content{'baseurl'});
                   2020: # Could not get a symb, give up
                   2021: 	unless ($symb) { return $content{'citation'}; }
                   2022: # Have a symb, can render
                   2023: 	return '<h2>'.&mt('Current attempts of student (if applicable)').'</h2>'.
                   2024: 	    &Apache::loncommon::get_previous_attempt($symb,
                   2025: 						     $content{'sendername'},
                   2026: 						     $content{'senderdomain'},
                   2027: 						     $content{'courseid'}).
                   2028: 	    '<hr /><h2>'.&mt('Current screen output (if applicable)').'</h2>'.
                   2029: 	    &Apache::loncommon::get_student_view($symb,
                   2030: 						 $content{'sendername'},
                   2031: 						 $content{'senderdomain'},
                   2032: 						 $content{'courseid'}).
                   2033: 	    '<h2>'.&mt('Correct Answer(s) (if applicable)').'</h2>'.
                   2034: 	    &Apache::loncommon::get_student_answers($symb,
                   2035: 						    $content{'sendername'},
                   2036: 						    $content{'senderdomain'},
                   2037: 						    $content{'courseid'});
1.173   ! albertel 2038:     } elsif ($env{'user.adv'}) {
1.111     www      2039: 	return $content{'citation'};
                   2040:     }
1.173   ! albertel 2041:     return '';
1.111     www      2042: }
                   2043: 
1.88      www      2044: # ================================================================== The Header
                   2045: 
                   2046: sub header {
1.90      www      2047:     my ($r,$title,$baseurl)=@_;
1.137     albertel 2048:     $r->print(&Apache::lonxml::xmlbegin().
                   2049: 	      '<head>'.&Apache::lonxml::fontsettings().
1.144     albertel 2050: 	      '<title>Communication and Messages</title>'.
                   2051: 	      &Apache::lonhtmlcommon::htmlareaheaders());
1.88      www      2052:     if ($baseurl) {
                   2053: 	$r->print("<base href=\"http://$ENV{'SERVER_NAME'}/$baseurl\" />");
                   2054:     }
                   2055:     $r->print(&Apache::loncommon::studentbrowser_javascript().'</head>'.
                   2056: 	      &Apache::loncommon::bodytag('Communication and Messages'));
                   2057:         $r->print(&Apache::lonhtmlcommon::breadcrumbs
1.90      www      2058:                   (undef,($title?$title:'Communication and Messages')));
1.88      www      2059: 
                   2060: }
                   2061: 
1.90      www      2062: # ---------------------------------------------------------------- Print header
                   2063: 
                   2064: sub printheader {
                   2065:     my ($r,$url,$desc,$title,$baseurl)=@_;
                   2066:     &Apache::lonhtmlcommon::add_breadcrumb
                   2067: 	({href=>$url,
                   2068: 	  text=>$desc});
                   2069:     &header($r,$title,$baseurl);
                   2070: }
                   2071: 
1.120     www      2072: # ------------------------------------------------------------ Store the comment
                   2073: 
                   2074: sub storecomment {
                   2075:     my ($r)=@_;
1.140     albertel 2076:     my $msgtxt=&Apache::lonfeedback::clear_out_html($env{'form.message'});
1.120     www      2077:     my $cleanmsgtxt='';
                   2078:     foreach (split(/[\n\r]/,$msgtxt)) {
                   2079: 	unless ($_=~/^\s*(\>|\&gt\;)/) {
                   2080: 	    $cleanmsgtxt.=$_."\n";
                   2081: 	}
                   2082:     }
1.140     albertel 2083:     my $key=&Apache::lonnet::escape($env{'form.baseurl'}).'___'.time;
1.120     www      2084:     &Apache::lonnet::put('nohist_stored_comments',{ $key => $cleanmsgtxt });
                   2085: }
                   2086: 
                   2087: sub storedcommentlisting {
                   2088:     my ($r)=@_;
                   2089:     my %msgs=&Apache::lonnet::dump('nohist_stored_comments',undef,undef,
1.140     albertel 2090:        '^'.&Apache::lonnet::escape(&Apache::lonnet::escape($env{'form.showcommentbaseurl'})));
1.137     albertel 2091:     $r->print(&Apache::lonxml::xmlbegin().'<head>'.
                   2092: 	      &Apache::lonxml::fontsettings().'</head><body>');
1.120     www      2093:     if ((keys %msgs)[0]=~/^error\:/) {
                   2094: 	$r->print(&mt('No stored comments yet.'));
                   2095:     } else {
                   2096: 	my $found=0;
                   2097: 	foreach (sort keys %msgs) {
                   2098: 	    $r->print("\n".$msgs{$_}."<hr />");
                   2099: 	    $found=1;
                   2100: 	}
                   2101: 	unless ($found) {
                   2102: 	    $r->print(&mt('No stored comments yet for this resource.'));
                   2103: 	}
                   2104:     }
                   2105: }
                   2106: 
1.115     www      2107: # ---------------------------------------------------------------- Send an email
                   2108: 
                   2109: sub sendoffmail {
1.120     www      2110:     my ($r,$folder)=@_;
                   2111:     my $suffix=&foldersuffix($folder);
1.115     www      2112:     my $sendstatus='';
1.159     raeburn  2113:     my %specialmsg_status;
                   2114:     my $numspecial = 0;
1.140     albertel 2115:     if ($env{'form.send'}) {
1.115     www      2116: 	&printheader($r,'','Messages being sent.');
                   2117: 	$r->rflush();
                   2118: 	my %content=();
                   2119: 	undef %content;
1.140     albertel 2120: 	if ($env{'form.forwid'}) {
                   2121: 	    my $msgid=$env{'form.forwid'};
1.120     www      2122: 	    my %message=&Apache::lonnet::get('nohist_email'.$suffix,[$msgid]);
1.115     www      2123: 	    %content=&unpackagemsg($message{$msgid},1);
1.120     www      2124: 	    &statuschange($msgid,'forwarded',$folder);
1.140     albertel 2125: 	    $env{'form.message'}.="\n\n-- Forwarded message --\n\n".
1.115     www      2126: 		$content{'message'};
                   2127: 	}
1.140     albertel 2128: 	if ($env{'form.replyid'}) {
                   2129: 	    my $msgid=$env{'form.replyid'};
1.120     www      2130: 	    my %message=&Apache::lonnet::get('nohist_email'.$suffix,[$msgid]);
1.115     www      2131: 	    %content=&unpackagemsg($message{$msgid},1);
1.120     www      2132: 	    &statuschange($msgid,'replied',$folder);
1.115     www      2133: 	}
                   2134: 	my %toaddr=();
                   2135: 	undef %toaddr;
1.140     albertel 2136: 	if ($env{'form.sendmode'} eq 'group') {
                   2137: 	    foreach (keys %env) {
1.115     www      2138: 		if ($_=~/^form\.send\_to\_\&\&\&[^\&]*\&\&\&\_(.+)$/) {
                   2139: 		    $toaddr{$1}='';
                   2140: 		}
                   2141: 	    }
1.140     albertel 2142: 	} elsif ($env{'form.sendmode'} eq 'upload') {
                   2143: 	    foreach (split(/[\n\r\f]+/,$env{'form.upfile'})) {
1.115     www      2144: 		my ($rec,$txt)=split(/\s*\:\s*/,$_);
                   2145: 		if ($txt) {
                   2146: 		    $rec=~s/\@/\:/;
                   2147: 		    $toaddr{$rec}.=$txt."\n";
                   2148: 		}
                   2149: 	    }
                   2150: 	} else {
1.140     albertel 2151: 	    $toaddr{$env{'form.recuname'}.':'.$env{'form.recdomain'}}='';
1.115     www      2152: 	}
1.140     albertel 2153: 	if ($env{'form.additionalrec'}) {
                   2154: 	    foreach (split(/\,/,$env{'form.additionalrec'})) {
1.115     www      2155: 		my ($auname,$audom)=split(/\@/,$_);
                   2156: 		$toaddr{$auname.':'.$audom}='';
                   2157: 	    }
                   2158: 	}
1.156     raeburn  2159: 
1.159     raeburn  2160:         my $savemsg;
1.156     raeburn  2161:         my $msgtype;
1.159     raeburn  2162:         my %sentmessage;
1.156     raeburn  2163:         if ((($env{'form.critmsg'}) || ($env{'form.sendbck'})) &&
1.170     albertel 2164:             (&Apache::lonnet::allowed('srm',$env{'request.course.id'})
                   2165: 	     || &Apache::lonnet::allowed('srm',$env{'request.course.id'}.
                   2166: 					 '/'.$env{'request.course.sec'})
                   2167: 	     )) {
1.159     raeburn  2168:             $savemsg=&Apache::lonfeedback::clear_out_html($env{'form.message'},1);
                   2169:             $msgtype = 'critical';
1.156     raeburn  2170:         } else {
1.159     raeburn  2171:             $savemsg=&Apache::lonfeedback::clear_out_html($env{'form.message'});
1.156     raeburn  2172:         }
1.115     www      2173: 	
                   2174: 	foreach (keys %toaddr) {
                   2175: 	    my ($recuname,$recdomain)=split(/\:/,$_);
1.159     raeburn  2176:             my $msgtxt = $savemsg;
1.115     www      2177: 	    if ($toaddr{$_}) { $msgtxt.='<hr />'.$toaddr{$_}; }
1.156     raeburn  2178: 	    my $thismsg;
1.140     albertel 2179: 	    if ((($env{'form.critmsg'}) || ($env{'form.sendbck'})) && 
1.170     albertel 2180: 		(&Apache::lonnet::allowed('srm',$env{'request.course.id'})
                   2181: 		 || &Apache::lonnet::allowed('srm',$env{'request.course.id'}.
                   2182: 					     '/'.$env{'request.course.sec'}))) {
1.115     www      2183: 		$r->print(&mt('Sending critical message').' '.$recuname.'@'.$recdomain.': ');
                   2184: 		$thismsg=&user_crit_msg($recuname,$recdomain,
1.140     albertel 2185: 					&Apache::lonfeedback::clear_out_html($env{'form.subject'}),
1.115     www      2186: 					$msgtxt,
1.159     raeburn  2187: 					$env{'form.sendbck'},$env{'form.permanent'},\$sentmessage{$_});
1.115     www      2188: 	    } else {
                   2189: 		$r->print(&mt('Sending').' '.$recuname.'@'.$recdomain.': ');
                   2190: 		$thismsg=&user_normal_msg($recuname,$recdomain,
1.140     albertel 2191: 					  &Apache::lonfeedback::clear_out_html($env{'form.subject'}),
1.115     www      2192: 					  $msgtxt,
1.159     raeburn  2193: 					  $content{'citation'},undef,undef,$env{'form.permanent'},\$sentmessage{$_});
1.156     raeburn  2194:             }
1.159     raeburn  2195: 	    if (($env{'request.course.id'}) && (($msgtype eq 'critical') || 
                   2196:                                          ($env{'form.sendmode'} eq 'group'))) {
                   2197: 	        $specialmsg_status{$recuname.':'.$recdomain}  = $thismsg;
1.156     raeburn  2198:                 if ($thismsg eq 'ok') {
1.159     raeburn  2199:                     $numspecial ++;
1.156     raeburn  2200:                 }
1.115     www      2201: 	    }
                   2202: 	    $r->print($thismsg.'<br />');
                   2203: 	    $sendstatus.=' '.$thismsg;
                   2204: 	}
1.159     raeburn  2205:         if (($env{'request.course.id'}) && (($env{'form.sendmode'} eq 'group')
                   2206:                                               || ($msgtype eq 'critical'))) {
1.156     raeburn  2207:             my $subj_prefix;
                   2208:             if ($msgtype eq 'critical') {
1.159     raeburn  2209:                 $subj_prefix = 'Critical.';
1.156     raeburn  2210:             } else {
1.159     raeburn  2211:                 $subj_prefix = 'Broadcast.';
1.156     raeburn  2212:             }
1.159     raeburn  2213:             my ($specialmsgid,$specialresult);
                   2214:             my $cnum = $env{'course.'.$env{'request.course.id'}.'.num'};
                   2215:             my $cdom = $env{'course.'.$env{'request.course.id'}.'.domain'};
                   2216:             my $course_str = &Apache::lonnet::escape('['.$cnum.':'.$cdom.']');
                   2217: 
                   2218:             if ($numspecial) {
                   2219:                 $specialresult = &user_normal_msg_raw($cnum,$cdom,$subj_prefix.
                   2220:                     ' '.$course_str,$savemsg,undef,undef,undef,
                   2221:                     undef,undef,\$specialmsgid);
                   2222:                 $specialmsgid = &Apache::lonnet::unescape($specialmsgid);
1.156     raeburn  2223:             }
1.159     raeburn  2224:             if ($specialresult eq 'ok') {
1.156     raeburn  2225:                 my $record_sent;
                   2226:                 my @recusers = ();
                   2227:                 my @recudoms = ();
1.159     raeburn  2228:                 my ($stamp,$msgsubj,$msgname,$msgdom,$msgcount,$context,$pid) = 
                   2229:                             split(/\:/,&Apache::lonnet::unescape($specialmsgid));
1.156     raeburn  2230:                 foreach my $recipient (sort(keys(%toaddr))) {
1.159     raeburn  2231:                     if ($specialmsg_status{$recipient} eq 'ok') {
                   2232:                         my $usersubj = $subj_prefix.'['.$recipient.']';
                   2233:                         my $usermsgid = &buildmsgid($stamp,$usersubj,$msgname,
                   2234:                                               $msgdom,$msgcount,$context,$pid);
                   2235:                         &user_normal_msg_raw($cnum,$cdom,$subj_prefix.
                   2236:                         ' ['.$recipient.']',$sentmessage{$recipient},
                   2237:                         undef,undef,undef,undef,$usermsgid);
1.156     raeburn  2238:                         my ($uname,$udom) = split/:/,$recipient;
                   2239:                         push(@recusers,$uname);
                   2240:                         push(@recudoms,$udom);
                   2241:                     }
                   2242:                 }
                   2243:                 if (@recusers) {
1.159     raeburn  2244:                     my $specialmessage;
                   2245:                     my $sentsubj = $subj_prefix.' ('.$numspecial.' sent) '.
                   2246:                     &Apache::lonfeedback::clear_out_html($env{'form.subject'});
                   2247:                     $sentsubj = &HTML::Entities::encode($sentsubj,'<>&"');
                   2248:                     my $sentmsgid = &buildmsgid($stamp,$sentsubj,$msgname,
                   2249:                                               $msgdom,$msgcount,$context,$pid);
                   2250:                     ($specialmsgid,$specialmessage) =
                   2251:                          &packagemsg(&Apache::lonfeedback::clear_out_html(
                   2252:                              $env{'form.subject'}),$savemsg,undef,undef,undef,
                   2253:                                             \@recusers,\@recudoms,$sentmsgid);
                   2254:                     $record_sent = &store_sent_mail($specialmsgid,$specialmessage);
1.156     raeburn  2255:                 }
                   2256:             } else {
1.159     raeburn  2257:                 &Apache::lonnet::logthis('Failed to create record of critical message or broadcast in '.$env{'course.'.$env{'request.course.id'}.'.num'}.' at '.$env{'course.'.$env{'request.course.id'}.'.domain'}.' - no msgid generated');
1.156     raeburn  2258:             }
                   2259:         }
1.115     www      2260:     } else {
                   2261: 	&printheader($r,'','No messages sent.'); 
                   2262:     }
                   2263:     if ($sendstatus=~/^(\s*(?:ok|con_delayed)\s*)*$/) {
                   2264: 	$r->print('<br /><font color="green">'.&mt('Completed.').'</font>');
1.140     albertel 2265: 	if ($env{'form.displayedcrit'}) {
1.115     www      2266: 	    &discrit($r);
                   2267: 	} else {
                   2268: 	    &Apache::loncommunicate::menu($r);
                   2269: 	}
                   2270:     } else {
                   2271: 	$r->print(
                   2272: 		  '<h2><font color="red">'.&mt('Could not deliver message').'</font></h2>'.
                   2273: 		  &mt('Please use the browser "Back" button and correct the recipient addresses')
                   2274: 		  );
                   2275:     }
                   2276: }
1.90      www      2277: 
1.13      www      2278: # ===================================================================== Handler
                   2279: 
1.5       www      2280: sub handler {
                   2281:     my $r=shift;
                   2282: 
                   2283: # ----------------------------------------------------------- Set document type
1.87      www      2284:     
                   2285:     &Apache::loncommon::content_type($r,'text/html');
                   2286:     $r->send_http_header;
                   2287:     
                   2288:     return OK if $r->header_only;
                   2289:     
1.6       www      2290: # --------------------------- Get query string for limited number of parameters
1.32      matthew  2291:     &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'},
                   2292:         ['display','replyto','forward','markread','markdel','markunread',
1.44      www      2293:          'sendreply','compose','sendmail','critical','recname','recdom',
1.120     www      2294:          'recordftf','sortedby','block','folder','startdis','interdis',
1.131     www      2295: 	 'showcommentbaseurl','dismode']);
1.140     albertel 2296:     $sqs='&sortedby='.$env{'form.sortedby'};
1.108     www      2297: 
1.40      www      2298: # ------------------------------------------------------ They checked for email
1.140     albertel 2299:     unless ($env{'form.block'}) {
1.101     raeburn  2300:         &Apache::lonnet::put('email_status',{'recnewemail'=>0});
                   2301:     }
1.88      www      2302: 
                   2303: # ----------------------------------------------------------------- Breadcrumbs
                   2304: 
                   2305:     &Apache::lonhtmlcommon::clear_breadcrumbs();
                   2306:     &Apache::lonhtmlcommon::add_breadcrumb
                   2307:         ({href=>"/adm/communicate",
                   2308:           text=>"Communication/Messages",
                   2309:           faq=>12,bug=>'Communication Tools',});
                   2310: 
1.106     www      2311: # ------------------------------------------------------------------ Get Folder
                   2312: 
1.140     albertel 2313:     my $folder=$env{'form.folder'};
1.106     www      2314:     unless ($folder) { 
                   2315: 	$folder=''; 
                   2316:     } else {
1.125     www      2317: 	$sqs.='&folder='.&Apache::lonnet::escape($folder);
1.106     www      2318:     }
1.142     www      2319: # ------------------------------------------------------------ Get Display Mode
                   2320: 
                   2321:     my $dismode=$env{'form.dismode'};
                   2322:     unless ($dismode) { 
                   2323: 	$dismode=''; 
                   2324:     } else {
                   2325: 	$sqs.='&dismode='.&Apache::lonnet::escape($dismode);
                   2326:     }
1.106     www      2327: 
1.108     www      2328: # --------------------------------------------------------------------- Display
                   2329: 
1.140     albertel 2330:     $startdis=$env{'form.startdis'};
1.118     www      2331:     $startdis--;
1.108     www      2332:     unless ($startdis) { $startdis=0; }
1.125     www      2333: 
1.140     albertel 2334:     $interdis=$env{'form.interdis'};
1.108     www      2335:     unless ($interdis) { $interdis=20; }
1.125     www      2336:     $sqs.='&interdis='.$interdis;
                   2337: 
1.140     albertel 2338:     if ($env{'form.firstview'}) {
1.117     www      2339: 	$startdis=0;
                   2340:     }
1.140     albertel 2341:     if ($env{'form.lastview'}) {
1.117     www      2342: 	$startdis=-1;
                   2343:     }
1.140     albertel 2344:     if ($env{'form.prevview'}) {
1.117     www      2345: 	$startdis--;
                   2346:     }
1.140     albertel 2347:     if ($env{'form.nextview'}) {
1.117     www      2348: 	$startdis++;
                   2349:     }
1.125     www      2350:     my $postedstartdis=$startdis+1;
                   2351:     $sqs.='&startdis='.$postedstartdis;
1.108     www      2352: 
1.5       www      2353: # --------------------------------------------------------------- Render Output
1.88      www      2354: 
1.140     albertel 2355:     if ($env{'form.display'}) {
                   2356: 	&displaymessage($r,$env{'form.display'},$folder);
                   2357:     } elsif ($env{'form.replyto'}) {
1.142     www      2358: 	&compout($r,'',$env{'form.replyto'},undef,undef,$folder,$dismode);
1.140     albertel 2359:     } elsif ($env{'form.confirm'}) {
1.92      www      2360: 	&printheader($r,'','Confirmed Receipt');
1.140     albertel 2361: 	foreach (keys %env) {
1.87      www      2362: 	    if ($_=~/^form\.rec\_(.*)$/) {
1.92      www      2363: 		$r->print('<b>'.&mt('Confirming Receipt').':</b> '.
1.87      www      2364: 			  &user_crit_received($1).'<br>');
                   2365: 	    }
                   2366: 	    if ($_=~/^form\.reprec\_(.*)$/) {
                   2367: 		my $msgid=$1;
1.92      www      2368: 		$r->print('<b>'.&mt('Confirming Receipt').':</b> '.
1.87      www      2369: 			  &user_crit_received($msgid).'<br>');
1.94      www      2370: 		&compout($r,'','','',$msgid);
1.87      www      2371: 	    }
                   2372: 	}
                   2373: 	&discrit($r);
1.140     albertel 2374:     } elsif ($env{'form.critical'}) {
1.92      www      2375: 	&printheader($r,'','Displaying Critical Messages');
1.87      www      2376: 	&discrit($r);
1.140     albertel 2377:     } elsif ($env{'form.forward'}) {
                   2378: 	&compout($r,$env{'form.forward'},undef,undef,undef,$folder);
                   2379:     } elsif ($env{'form.markdel'}) {
1.92      www      2380: 	&printheader($r,'','Deleted Message');
1.140     albertel 2381: 	&statuschange($env{'form.markdel'},'deleted',$folder);
1.120     www      2382: 	&Apache::loncommunicate::menu($r);
1.142     www      2383: 	&disall($r,($folder?$folder:$dismode));
1.140     albertel 2384:     } elsif ($env{'form.markedmove'}) {
1.106     www      2385: 	my $total=0;
1.140     albertel 2386: 	foreach (keys %env) {
1.106     www      2387: 	    if ($_=~/^form\.delmark_(.*)$/) {
                   2388: 		&movemsg(&Apache::lonnet::unescape($1),$folder,
1.140     albertel 2389: 			 $env{'form.movetofolder'});
1.106     www      2390: 		$total++;
                   2391: 	    }
                   2392: 	}
                   2393: 	&printheader($r,'','Moved Messages');
                   2394: 	$r->print('Moved '.$total.' message(s)<p>');
1.120     www      2395: 	&Apache::loncommunicate::menu($r);
1.142     www      2396: 	&disall($r,($folder?$folder:$dismode));
1.140     albertel 2397:     } elsif ($env{'form.markeddel'}) {
1.87      www      2398: 	my $total=0;
1.140     albertel 2399: 	foreach (keys %env) {
1.87      www      2400: 	    if ($_=~/^form\.delmark_(.*)$/) {
1.108     www      2401: 		&statuschange(&Apache::lonnet::unescape($1),'deleted',$folder);
1.87      www      2402: 		$total++;
                   2403: 	    }
                   2404: 	}
1.92      www      2405: 	&printheader($r,'','Deleted Messages');
1.87      www      2406: 	$r->print('Deleted '.$total.' message(s)<p>');
1.120     www      2407: 	&Apache::loncommunicate::menu($r);
1.142     www      2408: 	&disall($r,($folder?$folder:$dismode));
1.140     albertel 2409:     } elsif ($env{'form.markunread'}) {
1.92      www      2410: 	&printheader($r,'','Marked Message as Unread');
1.140     albertel 2411: 	&statuschange($env{'form.markunread'},'new');
1.120     www      2412: 	&Apache::loncommunicate::menu($r);
1.142     www      2413: 	&disall($r,($folder?$folder:$dismode));
1.140     albertel 2414:     } elsif ($env{'form.compose'}) {
                   2415: 	&compout($r,'','',$env{'form.compose'});
                   2416:     } elsif ($env{'form.recordftf'}) {
                   2417: 	&facetoface($r,$env{'form.recordftf'});
                   2418:     } elsif ($env{'form.block'}) {
                   2419:         &examblock($r,$env{'form.block'});
                   2420:     } elsif ($env{'form.sendmail'}) {
1.120     www      2421: 	&sendoffmail($r,$folder);
1.140     albertel 2422: 	if ($env{'form.storebasecomment'}) {
1.120     www      2423: 	    &storecomment($r);
                   2424: 	}
1.154     www      2425: 	if (($env{'form.rsspost'}) && ($env{'request.course.id'})) {
                   2426: 	    &Apache::lonrss::addentry($env{'course.'.$env{'request.course.id'}.'.num'},
                   2427: 				      $env{'course.'.$env{'request.course.id'}.'.domain'},
                   2428: 				      'Course_Announcements',
                   2429: 				      $env{'form.subject'},
                   2430: 				      $env{'form.message'},'/adm/communicate','public');
                   2431: 	}
1.142     www      2432: 	&disall($r,($folder?$folder:$dismode));
1.140     albertel 2433:     } elsif ($env{'form.newfolder'}) {
1.106     www      2434: 	&printheader($r,'','New Folder');
1.140     albertel 2435: 	&makefolder($env{'form.newfolder'});
1.120     www      2436: 	&Apache::loncommunicate::menu($r);
1.140     albertel 2437: 	&disall($r,$env{'form.newfolder'});
                   2438:     } elsif ($env{'form.showcommentbaseurl'}) {
1.120     www      2439: 	&storedcommentlisting($r);
1.87      www      2440:     } else {
1.92      www      2441: 	&printheader($r,'','Display All Messages');
1.142     www      2442: 	&Apache::loncommunicate::menu($r); 
                   2443: 	&disall($r,($folder?$folder:$dismode));
1.87      www      2444:     }
1.139     albertel 2445:     $r->print(&Apache::loncommon::endbodytag().'</html>');
1.87      www      2446:     return OK;
1.5       www      2447: }
1.2       www      2448: # ================================================= Main program, reset counter
                   2449: 
1.27      www      2450: BEGIN {
1.2       www      2451:     $msgcount=0;
1.1       www      2452: }
1.58      bowersj2 2453: 
                   2454: =pod
                   2455: 
                   2456: =back
                   2457: 
1.59      bowersj2 2458: =cut
                   2459: 
                   2460: 1; 
1.1       www      2461: 
                   2462: __END__
                   2463: 
                   2464: 
                   2465: 
                   2466: 
                   2467: 
                   2468: 
                   2469: 

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