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

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

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