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

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

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