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

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

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