File:  [LON-CAPA] / loncom / interface / lonmsg.pm
Revision 1.73: download - view: text, annotated - select for diffs
Tue Dec 30 14:57:49 2003 UTC (20 years, 5 months ago) by www
Branches: MAIN
CVS tags: HEAD
Bug #2444: saving my work, does not work yet.

    1: # The LearningOnline Network with CAPA
    2: # Routines for messaging
    3: #
    4: # $Id: lonmsg.pm,v 1.73 2003/12/30 14:57:49 www Exp $
    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/
   27: #
   28: #
   29: # (Routines to control the menu
   30: #
   31: # (TeX Conversion Module
   32: #
   33: # 05/29/00,05/30 Gerd Kortemeyer)
   34: #
   35: # 10/05 Gerd Kortemeyer)
   36: #
   37: # 10/19,10/20,10/30,
   38: # 02/06/01 Gerd Kortemeyer
   39: # 07/27 Guy Albertelli
   40: # 07/27,07/28,07/30,08/03,08/06,08/08,08/09,08/10,8/13,8/15,
   41: # 10/1,11/5 Gerd Kortemeyer
   42: # YEAR=2002
   43: # 1/1,3/18 Gerd Kortemeyer
   44: #
   45: package Apache::lonmsg;
   46: 
   47: =pod
   48: 
   49: =head1 NAME
   50: 
   51: Apache::lonmsg: supports internal messaging
   52: 
   53: =head1 SYNOPSIS
   54: 
   55: lonmsg provides routines for sending messages, receiving messages, and
   56: a handler to allow users to read, send, and delete messages.
   57: 
   58: =head1 OVERVIEW
   59: 
   60: =head2 Messaging Overview
   61: 
   62: X<messages>LON-CAPA provides an internal messaging system similar to
   63: email, but customized for LON-CAPA's usage. LON-CAPA implements its
   64: own messaging system, rather then building on top of email, because of
   65: the features LON-CAPA messages can offer that conventional e-mail can
   66: not:
   67: 
   68: =over 4
   69: 
   70: =item * B<Critical messages>: A message the recipient B<must>
   71: acknowlegde receipt of before they are allowed to continue using the
   72: system, preventing a user from claiming they never got a message
   73: 
   74: =item * B<Receipts>: LON-CAPA can reliably send reciepts informing the
   75: sender that it has been read; again, useful for preventing students
   76: from claiming they did not see a message. (While conventional e-mail
   77: has some reciept support, it's sporadic, e-mail client-specific, and
   78: generally the receiver can opt to not send one, making it useless in
   79: this case.)
   80: 
   81: =item * B<Context>: LON-CAPA knows about the sender, such as where
   82: they are in a course. When a student mails an instructor asking for
   83: help on the problem, the instructor receives not just the student's
   84: question, but all submissions the student has made up to that point,
   85: the user's rendering of the problem, and the complete view the student
   86: saw of the resource, including discussion up to that point. Finally,
   87: the instructor is reading all of this inside of LON-CAPA, not their
   88: email program, so they have full access to LON-CAPA's grading
   89: interface, or other features they may wish to use in response to the
   90: student's query.
   91: 
   92: =back
   93: 
   94: Users can ask LON-CAPA to forward messages to conventional e-mail
   95: addresses on their B<PREF> screen, but generally, LON-CAPA messages
   96: are much more useful then traditional email can be made to be, even
   97: with HTML support.
   98: 
   99: Right now, this document will cover just how to send a message, since
  100: it is likely you will not need to programmatically read messages,
  101: since lonmsg already implements that functionality.
  102: 
  103: =head1 FUNCTIONS
  104: 
  105: =over 4
  106: 
  107: =cut
  108: 
  109: use strict;
  110: use Apache::lonnet();
  111: use vars qw($msgcount);
  112: use HTML::TokeParser();
  113: use Apache::Constants qw(:common);
  114: use Apache::loncommon();
  115: use Apache::lontexconvert();
  116: use HTML::Entities();
  117: use Mail::Send;
  118: use Apache::lonlocal;
  119: 
  120: # Querystring component with sorting type
  121: my $sqs;
  122: 
  123: # ===================================================================== Package
  124: 
  125: sub packagemsg {
  126:     my ($subject,$message,$citation,$baseurl,$attachmenturl)=@_;
  127:     $message =&HTML::Entities::encode($message);
  128:     $citation=&HTML::Entities::encode($citation);
  129:     $subject =&HTML::Entities::encode($subject);
  130:     #remove machine specification
  131:     $baseurl =~ s|^http://[^/]+/|/|;
  132:     $baseurl =&HTML::Entities::encode($baseurl);
  133:     #remove machine specification
  134:     $attachmenturl =~ s|^http://[^/]+/|/|;
  135:     $attachmenturl =&HTML::Entities::encode($attachmenturl);
  136: 
  137:     my $now=time;
  138:     $msgcount++;
  139:     my $partsubj=$subject;
  140:     $partsubj=&Apache::lonnet::escape($partsubj);
  141:     my $msgid=&Apache::lonnet::escape(
  142:            $now.':'.$partsubj.':'.$ENV{'user.name'}.':'.
  143:            $ENV{'user.domain'}.':'.$msgcount.':'.$$);
  144:     my $result='<sendername>'.$ENV{'user.name'}.'</sendername>'.
  145:            '<senderdomain>'.$ENV{'user.domain'}.'</senderdomain>'.
  146:            '<subject>'.$subject.'</subject>'.
  147: 	   '<time>'.&Apache::lonlocal::locallocaltime($now).'</time>'.
  148: 	   '<servername>'.$ENV{'SERVER_NAME'}.'</servername>'.
  149:            '<host>'.$ENV{'HTTP_HOST'}.'</host>'.
  150: 	   '<client>'.$ENV{'REMOTE_ADDR'}.'</client>'.
  151: 	   '<browsertype>'.$ENV{'browser.type'}.'</browsertype>'.
  152: 	   '<browseros>'.$ENV{'browser.os'}.'</browseros>'.
  153: 	   '<browserversion>'.$ENV{'browser.version'}.'</browserversion>'.
  154:            '<browsermathml>'.$ENV{'browser.mathml'}.'</browsermathml>'.
  155: 	   '<browserraw>'.$ENV{'HTTP_USER_AGENT'}.'</browserraw>'.
  156: 	   '<courseid>'.$ENV{'request.course.id'}.'</courseid>'.
  157: 	   '<role>'.$ENV{'request.role'}.'</role>'.
  158: 	   '<resource>'.$ENV{'request.filename'}.'</resource>'.
  159:            '<msgid>'.$msgid.'</msgid>'.
  160: 	   '<message>'.$message.'</message>';
  161:     if (defined($citation)) {
  162: 	$result.='<citation>'.$citation.'</citation>';
  163:     }
  164:     if (defined($baseurl)) {
  165: 	$result.= '<baseurl>'.$baseurl.'</baseurl>';
  166:     }
  167:     if (defined($attachmenturl)) {
  168: 	$result.= '<attachmenturl>'.$attachmenturl.'</attachmenturl>';
  169:     }
  170:     return $msgid,$result;
  171: }
  172: 
  173: # ================================================== Unpack message into a hash
  174: 
  175: sub unpackagemsg {
  176:     my ($message,$notoken)=@_;
  177:     my %content=();
  178:     my $parser=HTML::TokeParser->new(\$message);
  179:     my $token;
  180:     while ($token=$parser->get_token) {
  181:        if ($token->[0] eq 'S') {
  182: 	   my $entry=$token->[1];
  183:            my $value=$parser->get_text('/'.$entry);
  184:            $content{$entry}=$value;
  185:        }
  186:     }
  187:     if ($content{'attachmenturl'}) {
  188:        my ($fname,$ft)=($content{'attachmenturl'}=~/\/(\w+)\.(\w+)$/);
  189:        if ($notoken) {
  190: 	   $content{'message'}.='<p>'.&mt('Attachment').': <tt>'.$fname.'.'.$ft.'</tt>';
  191:        } else {
  192: 	   $content{'message'}.='<p>'.&mt('Attachment').': <a href="'.
  193: 	       &Apache::lonnet::tokenwrapper($content{'attachmenturl'}).
  194: 	       '"><tt>'.$fname.'.'.$ft.'</tt></a>';
  195:        }
  196:     }
  197:     return %content;
  198: }
  199: 
  200: # ======================================================= Get info out of msgid
  201: 
  202: sub unpackmsgid {
  203:     my $msgid=&Apache::lonnet::unescape(shift);
  204:     my ($sendtime,$shortsubj,$fromname,$fromdomain)=split(/\:/,
  205:                           &Apache::lonnet::unescape($msgid));
  206:     my %status=&Apache::lonnet::get('email_status',[$msgid]);
  207:     if ($status{$msgid}=~/^error\:/) { $status{$msgid}=''; }
  208:     unless ($status{$msgid}) { $status{$msgid}='new'; }
  209:     return ($sendtime,$shortsubj,$fromname,$fromdomain,$status{$msgid});
  210: } 
  211: 
  212: 
  213: sub sendemail {
  214:     my ($to,$subject,$body)=@_;
  215:     $body=
  216:     "*** ".&mt('This is an automatic message generated by the LON-CAPA system.')."\n".
  217:     "*** ".&mt('Please do not reply to this address.')."\n\n".$body;
  218:     my $msg = new Mail::Send;
  219:     $msg->to($to);
  220:     $msg->subject('[LON-CAPA] '.$subject);
  221:     if (my $fh = $msg->open('smtp',Server => 'localhost')) {
  222: 	print $fh $body;
  223: 	$fh->close;
  224:     }
  225: }
  226: 
  227: # ==================================================== Send notification emails
  228: 
  229: sub sendnotification {
  230:     my ($to,$touname,$toudom,$subj,$crit)=@_;
  231:     my $sender=$ENV{'environment.firstname'}.' '.$ENV{'environment.lastname'};
  232:     my $critical=($crit?' critical':'');
  233:     my $url='http://'.
  234:       $Apache::lonnet::hostname{&Apache::lonnet::homeserver($touname,$toudom)}.
  235:       '/adm/email?username='.$touname.'&domain='.$toudom;
  236:     my $body=(<<ENDMSG);
  237: You received a$critical message from $sender in LON-CAPA. The subject is
  238: 
  239:  $subj
  240: 
  241: Use
  242: 
  243:  $url
  244: 
  245: to access this message.
  246: ENDMSG
  247:     &sendemail($to,'New'.$critical.' message from '.$sender,$body);
  248: }
  249: # ============================================================= Check for email
  250: 
  251: sub newmail {
  252:     if ((time-$ENV{'user.mailcheck.time'})>300) {
  253:         my %what=&Apache::lonnet::get('email_status',['recnewemail']);
  254:         &Apache::lonnet::appenv('user.mailcheck.time'=>time);
  255:         if ($what{'recnewemail'}>0) { return 1; }
  256:     }
  257:     return 0;
  258: }
  259: 
  260: # =============================== Automated message to the author of a resource
  261: 
  262: =pod
  263: 
  264: =item * B<author_res_msg($filename, $message)>: Sends message $message to the owner
  265:     of the resource with the URI $filename.
  266: 
  267: =cut
  268: 
  269: sub author_res_msg {
  270:     my ($filename,$message)=@_;
  271:     unless ($message) { return 'empty'; }
  272:     $filename=&Apache::lonnet::declutter($filename);
  273:     my ($domain,$author,@dummy)=split(/\//,$filename);
  274:     my $homeserver=&Apache::lonnet::homeserver($author,$domain);
  275:     if ($homeserver ne 'no_host') {
  276:        my $id=unpack("%32C*",$message);
  277:        my $msgid;
  278:        ($msgid,$message)=&packagemsg($filename,$message);
  279:        return &Apache::lonnet::reply('put:'.$domain.':'.$author.
  280:          ':nohist_res_msgs:'.
  281:           &Apache::lonnet::escape($filename.'_'.$id).'='.
  282:           &Apache::lonnet::escape($message),$homeserver);
  283:     }
  284:     return 'no_host';
  285: }
  286: 
  287: # =========================================== Retrieve author resource messages
  288: 
  289: sub retrieve_author_res_msg {
  290:     my ($author,$domain,$url)=@_;
  291:     $url=&Apache::lonnet::declutter($url);
  292:     my %errormsgs=&Apache::lonnet::dump('nohist_res_msgs',$1,$2);
  293:     my $msgs='';
  294:     foreach (keys %errormsgs) {
  295: 	if ($_=~/^\Q$url\E\_\d+$/) {
  296: 	    my %content=&unpackagemsg($errormsgs{$_});
  297: 	    $msgs.='<b>'.$content{'time'}.'</b>: '.$content{'message'}.
  298: 		      '<br />';
  299: 	}
  300:     } 
  301:     return $msgs;     
  302: }
  303: 
  304: 
  305: # =============================== Delete all author messages related to one URL
  306: 
  307: sub del_url_author_res_msg {
  308:     my ($author,$domain,$url)=@_;
  309:     $url=&Apache::lonnet::declutter($url);
  310: }
  311: 
  312: # ================= Return hash with URLs for which there is a resource message
  313: 
  314: sub all_url_author_res_msg {
  315:     my ($author,$domain)=@_;
  316: }
  317: 
  318: # ================================================== Critical message to a user
  319: 
  320: sub user_crit_msg_raw {
  321:     my ($user,$domain,$subject,$message,$sendback)=@_;
  322: # Check if allowed missing
  323:     my $status='';
  324:     my $msgid='undefined';
  325:     unless (($message)&&($user)&&($domain)) { $status='empty'; };
  326:     my $homeserver=&Apache::lonnet::homeserver($user,$domain);
  327:     if ($homeserver ne 'no_host') {
  328:        ($msgid,$message)=&packagemsg($subject,$message);
  329:        if ($sendback) { $message.='<sendback>true</sendback>'; }
  330:        $status=&Apache::lonnet::critical(
  331:            'put:'.$domain.':'.$user.':critical:'.
  332:            &Apache::lonnet::escape($msgid).'='.
  333:            &Apache::lonnet::escape($message),$homeserver);
  334:        if ($ENV{'request.course.id'}) {
  335:           &user_normal_msg_raw(
  336:             $ENV{'course.'.$ENV{'request.course.id'}.'.num'},
  337:             $ENV{'course.'.$ENV{'request.course.id'}.'.domain'},
  338:             'Critical ['.$user.':'.$domain.']',
  339: 	    $message);
  340:        }
  341:     } else {
  342:        $status='no_host';
  343:     }
  344: # Notifications
  345:     my %userenv = &Apache::lonnet::get('environment',['critnotification'],
  346:                                        $domain,$user);
  347:     if ($userenv{'critnotification'}) {
  348:       &sendnotification($userenv{'critnotification'},$user,$domain,$subject,1);
  349:     }
  350: # Log this
  351:     &Apache::lonnet::logthis(
  352:       'Sending critical email '.$msgid.
  353:       ', log status: '.
  354:       &Apache::lonnet::log($ENV{'user.domain'},$ENV{'user.name'},
  355:                          $ENV{'user.home'},
  356:       'Sending critical '.$msgid.' to '.$user.' at '.$domain.' with status: '
  357:       .$status));
  358:     return $status;
  359: }
  360: 
  361: # New routine that respects "forward" and calls old routine
  362: 
  363: =pod
  364: 
  365: =item * B<user_crit_msg($user, $domain, $subject, $message, $sendback)>: Sends
  366:     a critical message $message to the $user at $domain. If $sendback is true,
  367:     a reciept will be sent to the current user when $user recieves the message.
  368: 
  369: =cut
  370: 
  371: sub user_crit_msg {
  372:     my ($user,$domain,$subject,$message,$sendback)=@_;
  373:     my $status='';
  374:     my %userenv = &Apache::lonnet::get('environment',['msgforward'],
  375:                                        $domain,$user);
  376:     my $msgforward=$userenv{'msgforward'};
  377:     if ($msgforward) {
  378:        foreach (split(/\,/,$msgforward)) {
  379: 	 my ($forwuser,$forwdomain)=split(/\:/,$_);
  380:          $status.=
  381: 	   &user_crit_msg_raw($forwuser,$forwdomain,$subject,$message,
  382:                 $sendback).' ';
  383:        }
  384:     } else { 
  385: 	$status=&user_crit_msg_raw($user,$domain,$subject,$message,$sendback);
  386:     }
  387:     return $status;
  388: }
  389: 
  390: # =================================================== Critical message received
  391: 
  392: sub user_crit_received {
  393:     my $msgid=shift;
  394:     my %message=&Apache::lonnet::get('critical',[$msgid]);
  395:     my %contents=&unpackagemsg($message{$msgid},1);
  396:     my $status='rec: '.($contents{'sendback'}?
  397:      &user_normal_msg($contents{'sendername'},$contents{'senderdomain'},
  398:                      &mt('Receipt').': '.$ENV{'user.name'}.' at '.$ENV{'user.domain'},
  399:                      &mt('User').' '.$ENV{'user.name'}.' '.&mt('at').' '.$ENV{'user.domain'}.
  400:                      ' acknowledged receipt of message'."\n".'   "'.
  401:                      $contents{'subject'}.'"'."\n".&mt('dated').' '.
  402:                      $contents{'time'}.".\n"
  403:                      ):'no msg req');
  404:     $status.=' trans: '.
  405:      &Apache::lonnet::put(
  406:      'nohist_email',{$contents{'msgid'} => $message{$msgid}});
  407:     $status.=' del: '.
  408:      &Apache::lonnet::del('critical',[$contents{'msgid'}]);
  409:     &Apache::lonnet::log($ENV{'user.domain'},$ENV{'user.name'},
  410:                          $ENV{'user.home'},'Received critical message '.
  411:                          $contents{'msgid'}.
  412:                          ', '.$status);
  413:     return $status;
  414: }
  415: 
  416: # ======================================================== Normal communication
  417: 
  418: sub user_normal_msg_raw {
  419:     my ($user,$domain,$subject,$message,$citation,$baseurl,$attachmenturl)=@_;
  420: # Check if allowed missing
  421:     my $status='';
  422:     my $msgid='undefined';
  423:     unless (($message)&&($user)&&($domain)) { $status='empty'; };
  424:     my $homeserver=&Apache::lonnet::homeserver($user,$domain);
  425:     if ($homeserver ne 'no_host') {
  426:        ($msgid,$message)=&packagemsg($subject,$message,$citation,$baseurl,
  427:                                      $attachmenturl);
  428:        $status=&Apache::lonnet::critical(
  429:            'put:'.$domain.':'.$user.':nohist_email:'.
  430:            &Apache::lonnet::escape($msgid).'='.
  431:            &Apache::lonnet::escape($message),$homeserver);
  432:        &Apache::lonnet::put
  433:                          ('email_status',{'recnewemail'=>time},$domain,$user);
  434:     } else {
  435:        $status='no_host';
  436:     }
  437: # Notifications
  438:     my %userenv = &Apache::lonnet::get('environment',['notification'],
  439:                                        $domain,$user);
  440:     if ($userenv{'notification'}) {
  441: 	&sendnotification($userenv{'notification'},$user,$domain,$subject,0);
  442:     }
  443:     &Apache::lonnet::log($ENV{'user.domain'},$ENV{'user.name'},
  444:                          $ENV{'user.home'},
  445:       'Sending '.$msgid.' to '.$user.' at '.$domain.' with status: '.$status);
  446:     return $status;
  447: }
  448: 
  449: # New routine that respects "forward" and calls old routine
  450: 
  451: =pod
  452: 
  453: =item * B<user_normal_msg($user, $domain, $subject, $message,
  454:     $citation, $baseurl, $attachmenturl)>: Sends a message to the
  455:     $user at $domain, with subject $subject and message $message.
  456: 
  457: =cut
  458: 
  459: sub user_normal_msg {
  460:     my ($user,$domain,$subject,$message,$citation,$baseurl,$attachmenturl)=@_;
  461:     my $status='';
  462:     my %userenv = &Apache::lonnet::get('environment',['msgforward'],
  463:                                        $domain,$user);
  464:     my $msgforward=$userenv{'msgforward'};
  465:     if ($msgforward) {
  466:        foreach (split(/\,/,$msgforward)) {
  467: 	 my ($forwuser,$forwdomain)=split(/\:/,$_);
  468:          $status.=
  469: 	  &user_normal_msg_raw($forwuser,$forwdomain,$subject,$message,
  470: 			       $citation,$baseurl,$attachmenturl).' ';
  471:        }
  472:     } else { 
  473: 	$status=&user_normal_msg_raw($user,$domain,$subject,$message,
  474: 				     $citation,$baseurl,$attachmenturl);
  475:     }
  476:     return $status;
  477: }
  478: 
  479: 
  480: # =============================================================== Status Change
  481: 
  482: sub statuschange {
  483:     my ($msgid,$newstatus)=@_;
  484:     my %status=&Apache::lonnet::get('email_status',[$msgid]);
  485:     if ($status{$msgid}=~/^error\:/) { $status{$msgid}=''; }
  486:     unless ($status{$msgid}) { $status{$msgid}='new'; }
  487:     unless (($status{$msgid} eq 'replied') || 
  488:             ($status{$msgid} eq 'forwarded')) {
  489: 	&Apache::lonnet::put('email_status',{$msgid => $newstatus});
  490:     }
  491:     if (($newstatus eq 'deleted') || ($newstatus eq 'new')) {
  492: 	&Apache::lonnet::put('email_status',{$msgid => $newstatus});
  493:     }
  494: }
  495: 
  496: # ======================================================= Display a course list
  497: 
  498: sub discourse {
  499:     my $r=shift;
  500:     my %courselist=&Apache::lonnet::dump(
  501:                    'classlist',
  502: 		   $ENV{'course.'.$ENV{'request.course.id'}.'.domain'},
  503: 		   $ENV{'course.'.$ENV{'request.course.id'}.'.num'});
  504:     my $now=time;
  505:     my %lt=&Apache::lonlocal::texthash('cfa' => 'Check for All',
  506:             'cfs' => 'Check for Section/Group',
  507:             'cfn' => 'Check for None');
  508:     $r->print(<<ENDDISHEADER);
  509: <input type=hidden name=sendmode value=group>
  510: <script>
  511:     function checkall() {
  512: 	for (i=0; i<document.forms.compemail.elements.length; i++) {
  513:             if 
  514:           (document.forms.compemail.elements[i].name.indexOf('send_to_')==0) {
  515: 	      document.forms.compemail.elements[i].checked=true;
  516:             }
  517:         }
  518:     }
  519: 
  520:     function checksec() {
  521: 	for (i=0; i<document.forms.compemail.elements.length; i++) {
  522:             if 
  523:           (document.forms.compemail.elements[i].name.indexOf
  524:            ('send_to_&&&'+document.forms.compemail.chksec.value)==0) {
  525: 	      document.forms.compemail.elements[i].checked=true;
  526:             }
  527:         }
  528:     }
  529: 
  530:     function uncheckall() {
  531: 	for (i=0; i<document.forms.compemail.elements.length; i++) {
  532:             if 
  533:           (document.forms.compemail.elements[i].name.indexOf('send_to_')==0) {
  534: 	      document.forms.compemail.elements[i].checked=false;
  535:             }
  536:         }
  537:     }
  538: </script>
  539: <input type=button onClick="checkall()" value="$lt{'cfa'}">&nbsp;
  540: <input type=button onClick="checksec()" value="$lt{'cfs'}">
  541: <input type=text size=5 name=chksec>&nbsp;
  542: <input type=button onClick="uncheckall()" value="$lt{'cfn'}">
  543: <p>
  544: ENDDISHEADER
  545:     my %coursepersonnel=
  546:        &Apache::lonnet::get_course_adv_roles();
  547:     foreach my $role (sort keys %coursepersonnel) {
  548:        foreach (split(/\,/,$coursepersonnel{$role})) {
  549: 	   my ($puname,$pudom)=split(/\:/,$_);
  550: 	   $r->print(
  551:              '<br /><input type="checkbox" name="send_to_&&&&&&_'.
  552:              $puname.':'.$pudom.'" /> '.
  553: 		     &Apache::loncommon::plainname($puname,
  554:                           $pudom).' ('.$_.'), <i>'.$role.'</i>');
  555: 	}
  556:     }
  557: 
  558:     foreach (sort keys %courselist) {
  559:         my ($end,$start)=split(/\:/,$courselist{$_});
  560:         my $active=1;
  561:         if (($end) && ($now>$end)) { $active=0; }
  562:         if ($active) {
  563:            my ($sname,$sdom)=split(/\:/,$_);
  564:            my %reply=&Apache::lonnet::get('environment',
  565:               ['firstname','middlename','lastname','generation'],
  566:               $sdom,$sname);
  567:            my $section=&Apache::lonnet::usection
  568: 	       ($sdom,$sname,$ENV{'request.course.id'});
  569:            $r->print(
  570:         '<br><input type=checkbox name="send_to_&&&'.$section.'&&&_'.$_.'"> '.
  571: 		      $reply{'firstname'}.' '. 
  572:                       $reply{'middlename'}.' '.
  573:                       $reply{'lastname'}.' '.
  574:                       $reply{'generation'}.
  575:                       ' ('.$_.') '.$section);
  576:         } 
  577:     }
  578: }
  579: 
  580: # ==================================================== Display Critical Message
  581: 
  582: sub discrit {
  583:     my $r=shift;
  584:     my $header = '<h1><font color=red>'.&mt('Critical Messages').'</font></h1>'.
  585:         '<form action=/adm/email method=post>'.
  586:         '<input type=hidden name=confirm value=true>';
  587:     my %what=&Apache::lonnet::dump('critical');
  588:     my $result = '';
  589:     foreach (sort keys %what) {
  590:         my %content=&unpackagemsg($what{$_});
  591:         next if ($content{'senderdomain'} eq '');
  592:         $content{'message'}=~s/\n/\<br\>/g;
  593:         $result.='<hr>'.&mt('From').': <b>'.
  594: &Apache::loncommon::aboutmewrapper(
  595:  &Apache::loncommon::plainname($content{'sendername'},$content{'senderdomain'}),$content{'sendername'},$content{'senderdomain'}).'</b> ('.
  596: $content{'sendername'}.'@'.
  597:             $content{'senderdomain'}.') '.$content{'time'}.
  598:             '<br>'.&mt('Subject').': '.$content{'subject'}.
  599:             '<br><blockquote>'.
  600:               &Apache::lontexconvert::msgtexconverted($content{'message'}).
  601:             '</blockquote>'.
  602:             '<input type=submit name="rec_'.$_.'" value="'.&mt('Confirm Receipt').'">'.
  603:             '<input type=submit name="reprec_'.$_.'" '.
  604:                   'value="'.&mt('Confirm Receipt and Reply').'">';
  605:     }
  606:     # Check to see if there were any messages.
  607:     if ($result eq '') {
  608:         $result = "<h2>".&mt('You have no critical messages.')."</h2>".
  609: 	    '<a href="/adm/roles">'.&mt('Select a course').'</a>';
  610:     } else {
  611:         $r->print($header);
  612:     }
  613:     $r->print($result);
  614:     $r->print('<input type=hidden name="displayedcrit" value="true"></form>');
  615: }
  616: 
  617: # =============================================================== Compose reply
  618: 
  619: sub comprep {
  620:     my ($r,$msgid)=@_;
  621:       my %message=&Apache::lonnet::get('nohist_email',[$msgid]);
  622:       my %content=&unpackagemsg($message{$msgid},1);
  623:       my $quotemsg='> '.$content{'message'};
  624:       $quotemsg=~s/\r/\n/g;
  625:       $quotemsg=~s/\f/\n/g;
  626:       $quotemsg=~s/\n+/\n\> /g;
  627:       my $torepl=&Apache::loncommon::aboutmewrapper(
  628:  &Apache::loncommon::plainname($content{'sendername'},$content{'senderdomain'}),$content{'sendername'},$content{'senderdomain'}).' ('.
  629: $content{'sendername'}.'@'.
  630:             $content{'senderdomain'}.')';
  631:       my $subject=&mt('Re').': '.$content{'subject'};
  632:       my $dispcrit='';
  633:       if (&Apache::lonnet::allowed('srm',$ENV{'request.course.id'})) {
  634: 	 my $crithelp = Apache::loncommon::help_open_topic("Course_Critical_Message");
  635:          $dispcrit=
  636:  '<input type=checkbox name=critmsg> '.&mt('Send as critical message').' ' . $crithelp . 
  637:  '<br>'.
  638:  '<input type=checkbox name=sendbck> '.&mt('Send as critical message').' ' .
  639:  &mt('and return receipt') . $crithelp . '<p>';
  640:       }
  641:     my %lt=&Apache::lonlocal::texthash(
  642: 				   'to' => 'To',
  643: 				   'sb' => 'Subject',
  644: 				   'sr' => 'Send Reply',
  645: 				   'ca' => 'Cancel'
  646: 				   );
  647:       $r->print(<<"ENDREPLY");
  648: <form action="/adm/email" method="post">
  649: <input type="hidden" name="sendreply" value="$msgid">
  650: $lt{'to'}: $torepl<br />
  651: $lt{'sb'}: <input type="text" size=50 name="subject" value="$subject"><p>
  652: <textarea name="message" cols="84" rows="10" wrap="hard">
  653: $quotemsg
  654: </textarea></p><br />
  655: $dispcrit
  656: <input type="submit" name="send" value="$lt{'sr'}" />
  657: <input type="submit" name="cancel" value="$lt{'ca'}"/ >
  658: </form>
  659: ENDREPLY
  660: }
  661: 
  662: sub sortedmessages {
  663:     my @messages = &Apache::lonnet::getkeys('nohist_email');
  664:     #unpack the varibles and repack into temp for sorting
  665:     my @temp;
  666:     foreach (@messages) {
  667: 	my $msgid=&Apache::lonnet::escape($_);
  668: 	my ($sendtime,$shortsubj,$fromname,$fromdomain,$status)=
  669: 	    &Apache::lonmsg::unpackmsgid($msgid);
  670: 	my @temp1 = ($sendtime,$shortsubj,$fromname,$fromdomain,$status,
  671: 		     $msgid);
  672: 	push @temp ,\@temp1;
  673:     }
  674:     #default sort
  675:     @temp = sort  {$a->[0] <=> $b->[0]} @temp;    
  676:     if ($ENV{'form.sortedby'} eq "date"){
  677:         @temp = sort  {$a->[0] <=> $b->[0]} @temp;    
  678:     }
  679:     if ($ENV{'form.sortedby'} eq "revdate"){
  680:     	@temp = sort  {$b->[0] <=> $a->[0]} @temp; 
  681:     }
  682:     if ($ENV{'form.sortedby'} eq "user"){
  683: 	@temp = sort  {lc($a->[2]) cmp lc($b->[2])} @temp;
  684:     }
  685:     if ($ENV{'form.sortedby'} eq "revuser"){
  686: 	@temp = sort  {lc($b->[2]) cmp lc($a->[2])} @temp;
  687:     }
  688:     if ($ENV{'form.sortedby'} eq "domain"){
  689:         @temp = sort  {$a->[3] cmp $b->[3]} @temp;
  690:     }
  691:     if ($ENV{'form.sortedby'} eq "revdomain"){
  692:         @temp = sort  {$b->[3] cmp $a->[3]} @temp;
  693:     }
  694:     if ($ENV{'form.sortedby'} eq "subject"){
  695:         @temp = sort  {lc($a->[1]) cmp lc($b->[1])} @temp;
  696:     }
  697:     if ($ENV{'form.sortedby'} eq "revsubject"){
  698:         @temp = sort  {lc($b->[1]) cmp lc($a->[1])} @temp;
  699:     }
  700:     if ($ENV{'form.sortedby'} eq "status"){
  701:         @temp = sort  {$a->[4] cmp $b->[4]} @temp;
  702:     }
  703:     if ($ENV{'form.sortedby'} eq "revstatus"){
  704:         @temp = sort  {$b->[4] cmp $a->[4]} @temp;
  705:     }
  706:     return @temp;
  707: }
  708: 
  709: # ======================================================== Display all messages
  710: 
  711: sub disall {
  712:     my $r=shift;
  713:      $r->print(<<ENDDISHEADER);
  714: <script>
  715:     function checkall() {
  716: 	for (i=0; i<document.forms.disall.elements.length; i++) {
  717:             if 
  718:           (document.forms.disall.elements[i].name.indexOf('delmark_')==0) {
  719: 	      document.forms.disall.elements[i].checked=true;
  720:             }
  721:         }
  722:     }
  723: 
  724:     function uncheckall() {
  725: 	for (i=0; i<document.forms.disall.elements.length; i++) {
  726:             if 
  727:           (document.forms.disall.elements[i].name.indexOf('delmark_')==0) {
  728: 	      document.forms.disall.elements[i].checked=false;
  729:             }
  730:         }
  731:     }
  732: </script>
  733: ENDDISHEADER
  734:     $r->print('<h1>'.&mt('Display All Messages').'</h1><form method=post name=disall '.
  735: 	      'action="/adm/email">'.
  736: 	      '<table border=2><tr><th colspan=2>&nbsp</th><th>');
  737:     if ($ENV{'form.sortedby'} eq "revdate") {
  738: 	$r->print('<a href = "?sortedby=date">'.&mt('Date').'</a></th>');
  739:     } else {
  740: 	$r->print('<a href = "?sortedby=revdate">'.&mt('Date').'</a></th>');
  741:     }
  742:     $r->print('<th>');
  743:     if ($ENV{'form.sortedby'} eq "revuser") {
  744: 	$r->print('<a href = "?sortedby=user">'.&mt('Username').'</a>');
  745:     } else {
  746: 	$r->print('<a href = "?sortedby=revuser">'.&mt('Username').'</a>');
  747:     }
  748:     $r->print('</th><th>');
  749:     if ($ENV{'form.sortedby'} eq "revdomain") {
  750: 	$r->print('<a href = "?sortedby=domain">'.&mt('Domain').'</a>');
  751:     } else {
  752: 	$r->print('<a href = "?sortedby=revdomain">'.&mt('Domain').'</a>');
  753:     }
  754:     $r->print('</th><th>');
  755:     if ($ENV{'form.sortedby'} eq "revsubject") {
  756: 	$r->print('<a href = "?sortedby=subject">'.&mt('Subject').'</a>');
  757:     } else {
  758:     	$r->print('<a href = "?sortedby=revsubject">'.&mt('Subject').'</a>');
  759:     }
  760:     $r->print('</th><th>');
  761:     if ($ENV{'form.sortedby'} eq "revstatus") {
  762: 	$r->print('<a href = "?sortedby=status">'.&mt('Status').'</th>');
  763:     } else {
  764:      	$r->print('<a href = "?sortedby=revstatus">'.&mt('Status').'</th>');
  765:     }
  766:     $r->print('</tr>');
  767:     my @temp=sortedmessages();
  768:     foreach (@temp){
  769: 	my ($sendtime,$shortsubj,$fromname,$fromdomain,$status,$origID)= @$_;
  770: 	if (($status ne 'deleted') && defined($sendtime) && $sendtime!~/error/) {
  771: 	    if ($status eq 'new') {
  772: 		$r->print('<tr bgcolor="#FFBB77">');
  773: 	    } elsif ($status eq 'read') {
  774: 		$r->print('<tr bgcolor="#BBBB77">');
  775: 	    } elsif ($status eq 'replied') {
  776: 		$r->print('<tr bgcolor="#AAAA88">'); 
  777: 	    } else {
  778: 		$r->print('<tr bgcolor="#99BBBB">');
  779: 	    }
  780: 	    $r->print('<td><a href="/adm/email?display='.$origID.$sqs. 
  781: 		      '">'.&mt('Open').'</a></td><td><a href="/adm/email?markdel='.$origID.$sqs.
  782: 		      '">'.&mt('Delete').'</a><input type=checkbox name="delmark_'.$origID.'"></td>'.
  783: 		      '<td>'.&Apache::lonlocal::locallocaltime($sendtime).'</td><td>'.
  784: 		      $fromname.'</td><td>'.$fromdomain.'</td><td>'.
  785: 		      &Apache::lonnet::unescape($shortsubj).'</td><td>'.
  786:                       $status.'</td></tr>');
  787: 	}
  788:     }   
  789:     $r->print('</table><p>'.
  790:               '<a href="javascript:checkall()">'.&mt('Check All').'</a>&nbsp;'.
  791:               '<a href="javascript:uncheckall()">'.&mt('Uncheck All').'</a><p>'.
  792: 	      '<input type="hidden" name="sortedby" value="'.$ENV{'form.sortedby'}.'" />'.
  793:               '<input type=submit name="markeddel" value="'.&mt('Delete Checked').'">'.
  794:               '</form></body></html>');
  795: }
  796: 
  797: # ============================================================== Compose output
  798: 
  799: sub compout {
  800:     my ($r,$forwarding,$broadcast)=@_;
  801:       my $dispcrit='';
  802:     my $dissub='';
  803:     my $dismsg='';
  804:     my $func=&mt('Send New');
  805:     my %lt=&Apache::lonlocal::texthash('us' => 'Username',
  806: 				       'do' => 'Domain',
  807: 				       'ad' => 'Additional Recipients',
  808: 				       'sb' => 'Subject',
  809: 				       'ca' => 'Cancel',
  810: 				       'ma' => 'Mail');
  811: 
  812:     if (&Apache::lonnet::allowed('srm',$ENV{'request.course.id'})) {
  813: 	 my $crithelp = Apache::loncommon::help_open_topic("Course_Critical_Message");
  814:          $dispcrit=
  815:  '<input type="checkbox" name="critmsg"> '.&mt('Send as critical message').' ' . $crithelp . 
  816:  '<br>'.
  817:  '<input type="checkbox" name="sendbck"> '.&mt('Send as critical message').'  ' .
  818:  &mt('and return receipt') . $crithelp . '<p>';
  819:       }
  820:     if ($forwarding) {
  821:        $dispcrit.='<input type="hidden" name="forwid" value="'.
  822: 	   $forwarding.'">';
  823:        $func=&mt('Forward');
  824:       my %message=&Apache::lonnet::get('nohist_email',[$forwarding]);
  825:       my %content=&unpackagemsg($message{$forwarding});
  826: 
  827:        $dissub=&mt('Forwarding').': '.$content{'subject'};
  828:        $dismsg=&mt('Forwarded message from').' '.
  829: 	   $content{'sendername'}.' '.&mt('at').' '.$content{'senderdomain'};
  830:     }
  831:     my $defdom=$ENV{'user.domain'};
  832:     if ($ENV{'form.recdom'}) { $defdom=$ENV{'form.recdom'}; }
  833:       $r->print(
  834:                 '<form action="/adm/email"  name="compemail" method="post"'.
  835:                 ' enctype="multipart/form-data">'."\n".
  836:                 '<input type="hidden" name="sendmail" value="on">'."\n".
  837:                 '<table>');
  838:     unless (($broadcast eq 'group') || ($broadcast eq 'upload')) {
  839:         my $domform = &Apache::loncommon::select_dom_form($defdom,'recdomain');
  840:         my $selectlink=&Apache::loncommon::selectstudent_link
  841: 	    ('compemail','recuname','recdomain');
  842:        $r->print(<<"ENDREC");
  843: <table>
  844: <tr><td>$lt{'us'}:</td><td><input type="text" size="12" name="recuname" value="$ENV{'form.recname'}"></td><td rowspan="2">$selectlink</td></tr>
  845: <tr><td>$lt{'do'}:</td>
  846: <td>$domform</td></tr>
  847: ENDREC
  848:     }
  849:     my $latexHelp = Apache::loncommon::helpLatexCheatsheet();
  850:     if ($broadcast ne 'upload') {
  851:        $r->print(<<"ENDCOMP");
  852: <tr><td>$lt{'ad'}<br /><tt>username\@domain,username\@domain, ...
  853: </tt></td><td>
  854: <input type="text" size="50" name="additionalrec"></td></tr>
  855: <tr><td>$lt{'sb'}:</td><td><input type="text" size="50" name="subject" value="$dissub">
  856: </td></tr></table>
  857: $latexHelp
  858: <textarea name="message" cols="80" rows="10" wrap="hard">$dismsg
  859: </textarea></p><br />
  860: $dispcrit
  861: <input type="submit" name="send" value="$func $lt{'ma'}" />
  862: <input type="submit" name="cancel" value="$lt{'ca'}" />
  863: ENDCOMP
  864:     } else { # $broadcast is 'upload'
  865: 	$r->print(<<ENDUPLOAD);
  866: <input type=hidden name=sendmode value=upload>
  867: <h3>Generate messages from a file</h3>
  868: <p>
  869: Subject: <input type=text size=50 name=subject>
  870: </p>
  871: <p>General message text<br />
  872: <textarea name=message cols=60 rows=10 wrap=hard>$dismsg
  873: </textarea></p>
  874: <p>
  875: The file format for the uploaded portion of the message is:
  876: <pre>
  877: username1\@domain1: text
  878: username2\@domain2: text
  879: username3\@domain1: text
  880: </pre>
  881: </p>
  882: <p>
  883: The messages will be assembled from all lines with the respective 
  884: <tt>username\@domain</tt>, and appended to the general message text.</p>
  885: <p>
  886: <input type=file name=upfile size=20><p>
  887: $dispcrit
  888: <input type=submit value="Upload and send">
  889: ENDUPLOAD
  890:     }
  891:     if ($broadcast eq 'group') {
  892:        &discourse;
  893:     }
  894:     $r->print('</form>');
  895: }
  896: 
  897: # ---------------------------------------------------- Display all face to face
  898: 
  899: sub disfacetoface {
  900:     my ($r,$user,$domain)=@_;
  901:     unless ($ENV{'request.course.id'}) { return; }
  902:     unless (&Apache::lonnet::allowed('srm',$ENV{'request.course.id'})) {
  903: 	return;
  904:     }
  905:     my %records=&Apache::lonnet::dump('nohist_email',
  906: 			 $ENV{'course.'.$ENV{'request.course.id'}.'.domain'},
  907: 			 $ENV{'course.'.$ENV{'request.course.id'}.'.num'},
  908:                          '%255b'.$user.'%253a'.$domain.'%255d');
  909:     my $result='';
  910:     foreach (sort keys %records) {
  911:         my %content=&unpackagemsg($records{$_});
  912:         next if ($content{'senderdomain'} eq '');
  913:         $content{'message'}=~s/\n/\<br\>/g;
  914:         if ($content{'subject'}=~/^Record/) {
  915: 	    $result.='<h3>'.&mt('Record').'</h3>';
  916:         } else {
  917:             $result.='<h3>'.&mt('Sent Message').'</h3>';
  918:             %content=&unpackagemsg($content{'message'});
  919:             $content{'message'}=
  920:                 '<b>Subject: '.$content{'subject'}.'</b><br />'.
  921: 		$content{'message'};
  922:         }
  923:         $result.=&mt('By').': <b>'.
  924: &Apache::loncommon::aboutmewrapper(
  925:  &Apache::loncommon::plainname($content{'sendername'},$content{'senderdomain'}),$content{'sendername'},$content{'senderdomain'}).'</b> ('.
  926: $content{'sendername'}.'@'.
  927:             $content{'senderdomain'}.') '.$content{'time'}.
  928:             '<br><blockquote>'.
  929:               &Apache::lontexconvert::msgtexconverted($content{'message'}).
  930: 	      '</blockquote>';
  931:      }
  932:     # Check to see if there were any messages.
  933:     if ($result eq '') {
  934:         $r->print("<p><b>No notes, face-to-face discussion records, or critical messages in this course.</b></p>");
  935:     } else {
  936:        $r->print($result);
  937:     }
  938: }
  939: 
  940: # ---------------------------------------------------------------- Face to face
  941: 
  942: sub facetoface {
  943:     my ($r,$stage)=@_;
  944:     unless (&Apache::lonnet::allowed('srm',$ENV{'request.course.id'})) {
  945: 	return;
  946:     }
  947: # from query string
  948:     if ($ENV{'form.recname'}) { $ENV{'form.recuname'}=$ENV{'form.recname'}; }
  949:     if ($ENV{'form.recdom'}) { $ENV{'form.recdomain'}=$ENV{'form.recdom'}; }
  950: 
  951:     my $defdom=$ENV{'user.domain'};
  952: # already filled in
  953:     if ($ENV{'form.recdomain'}) { $defdom=$ENV{'form.recdomain'}; }
  954: # generate output
  955:     my $domform = &Apache::loncommon::select_dom_form($defdom,'recdomain');
  956:     my $stdbrws = &Apache::loncommon::selectstudent_link
  957: 	('stdselect','recuname','recdomain');
  958:     $r->print(<<"ENDTREC");
  959: <h3>User Notes, Records of Face-To-Face Discussions, and Critical Messages in Course</h3>
  960: <form method="post" action="/adm/email" name="stdselect">
  961: <input type="hidden" name="recordftf" value="retrieve" />
  962: <table>
  963: <tr><td>Username:</td><td><input type=text size=12 name=recuname value="$ENV{'form.recuname'}"></td>
  964: <td rowspan="2">
  965: $stdbrws
  966: <input type="submit" value="Retrieve discussion and message records"></td>
  967: </tr>
  968: <tr><td>Domain:</td>
  969: <td>$domform</td></tr>
  970: </table>
  971: </form>
  972: ENDTREC
  973:     if (($stage ne 'query') &&
  974:         ($ENV{'form.recdomain'}) && ($ENV{'form.recuname'})) {
  975:         chomp($ENV{'form.newrecord'});
  976:         if ($ENV{'form.newrecord'}) {
  977:            &user_normal_msg_raw(
  978:             $ENV{'course.'.$ENV{'request.course.id'}.'.num'},
  979:             $ENV{'course.'.$ENV{'request.course.id'}.'.domain'},
  980:             'Record ['.$ENV{'form.recuname'}.':'.$ENV{'form.recdomain'}.']',
  981: 	    $ENV{'form.newrecord'});
  982:         }
  983:         $r->print('<h3>'.&Apache::loncommon::plainname($ENV{'form.recuname'},
  984: 				     $ENV{'form.recdomain'}).'</h3>');
  985:         &disfacetoface($r,$ENV{'form.recuname'},$ENV{'form.recdomain'});
  986: 	$r->print(<<ENDRHEAD);
  987: <form method="post" action="/adm/email">
  988: <input name="recdomain" value="$ENV{'form.recdomain'}" type="hidden" />
  989: <input name="recuname" value="$ENV{'form.recuname'}" type="hidden" />
  990: ENDRHEAD
  991:         $r->print(<<ENDBFORM);
  992: <hr />New Record (record is visible to course faculty and staff)<br />
  993: <textarea name="newrecord" cols="80" rows="10" wrap="hard"></textarea>
  994: <br />
  995: <input type="hidden" name="recordftf" value="post" />
  996: <input type="submit" value="Post this record" />
  997: </form>
  998: ENDBFORM
  999:     }
 1000: }
 1001: 
 1002: # ===================================================================== Handler
 1003: 
 1004: sub handler {
 1005:     my $r=shift;
 1006: 
 1007: # ----------------------------------------------------------- Set document type
 1008: 
 1009:   &Apache::loncommon::content_type($r,'text/html');
 1010:   $r->send_http_header;
 1011: 
 1012:   return OK if $r->header_only;
 1013: 
 1014: # --------------------------- Get query string for limited number of parameters
 1015:     &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'},
 1016:         ['display','replyto','forward','markread','markdel','markunread',
 1017:          'sendreply','compose','sendmail','critical','recname','recdom',
 1018:          'recordftf','sortedby']);
 1019:     $sqs='&sortedby='.$ENV{'form.sortedby'};
 1020: # ------------------------------------------------------ They checked for email
 1021:   &Apache::lonnet::put('email_status',{'recnewemail'=>0});
 1022: # --------------------------------------------------------------- Render Output
 1023:   if (!$ENV{'form.display'}) {
 1024:       $r->print('<html><head><title>EMail and Messaging</title>'.
 1025: 		&Apache::loncommon::studentbrowser_javascript().'</head>'.
 1026: 		&Apache::loncommon::bodytag('EMail and Messages'));
 1027:   }
 1028:   if ($ENV{'form.display'}) {
 1029:       my $msgid=$ENV{'form.display'};
 1030:       &statuschange($msgid,'read');
 1031:       my %message=&Apache::lonnet::get('nohist_email',[$msgid]);
 1032:       my %content=&unpackagemsg($message{$msgid});
 1033: # info to generate "next" and "previous" buttons
 1034:       my @messages=&sortedmessages();
 1035:       my $counter=0;
 1036:       $r->print('<pre>');
 1037:       my $escmsgid=&Apache::lonnet::escape($msgid);
 1038:       foreach (@messages) {
 1039:  	  if ($_->[5] eq $escmsgid){
 1040:  	      last;
 1041:  	  }
 1042:  	  $counter++;
 1043:       }
 1044:       $r->print('</pre>');
 1045:       my $number_of_messages = scalar(@messages); #subtract 1 for last index
 1046: # start output
 1047:       $r->print('<html><head><title>EMail and Messaging</title>');
 1048:       if (defined($content{'baseurl'})) {
 1049: 	  $r->print("<base href=\"http://$ENV{'SERVER_NAME'}/$content{'baseurl'}\" />");
 1050:       }
 1051:       $r->print(&Apache::loncommon::studentbrowser_javascript().
 1052: 		'</head>'.
 1053: 		&Apache::loncommon::bodytag('EMail and Messages'));
 1054:       $r->print('<b>'.&mt('Subject').':</b> '.$content{'subject'}.
 1055:              '<br><b>'.&mt('From').':</b> '.
 1056: &Apache::loncommon::aboutmewrapper(
 1057: &Apache::loncommon::plainname($content{'sendername'},$content{'senderdomain'}),
 1058: $content{'sendername'},$content{'senderdomain'}).' ('.
 1059:                                  $content{'sendername'}.' at '.
 1060:                                  $content{'senderdomain'}.') '.
 1061:              '<br><b>'.&mt('Time').':</b> '.$content{'time'}.'<p>'.
 1062:              '<table border=2><tr bgcolor="#FFFFAA"><td>'.&mt('Functions').':</td>'.
 1063:            '<td><a href="/adm/email?replyto='.&Apache::lonnet::escape($msgid).$sqs.
 1064:              '"><b>'.&mt('Reply').'</b></a></td>'.
 1065:            '<td><a href="/adm/email?forward='.&Apache::lonnet::escape($msgid).$sqs.
 1066:              '"><b>'.&mt('Forward').'</b></a></td>'.
 1067:         '<td><a href="/adm/email?markunread='.&Apache::lonnet::escape($msgid).$sqs.
 1068:              '"><b>'.&mt('Mark Unread').'</b></a></td>'.
 1069:         '<td><a href="/adm/email?markdel='.&Apache::lonnet::escape($msgid).$sqs.
 1070:              '"><b>Delete</b></a></td>'.
 1071: 		'<td><a href="/adm/email?sortedby='.$ENV{'form.sortedby'}.
 1072: 		'"><b>'.&mt('Display all Messages').'</b></a></td>');
 1073:       if ($counter > 0){
 1074:  	  $r->print('<td><a href="/adm/email?display='.$messages[$counter-1]->[5].$sqs.
 1075:            '"><b>'.&mt('Previous').'</b></a></td>');
 1076:        }
 1077:        if ($counter < $number_of_messages - 1){
 1078:  	  $r->print('<td><a href="/adm/email?display='.$messages[$counter+1]->[5].$sqs.
 1079:            '"><b>'.&mt('Next').'</b></a></td>');
 1080:        }
 1081:        $r->print('</tr></table><p><pre>'.
 1082:              &Apache::lontexconvert::msgtexconverted($content{'message'}).
 1083:              '</pre><hr>'.$content{'citation'});
 1084:   } elsif ($ENV{'form.replyto'}) {
 1085:       &comprep($r,$ENV{'form.replyto'});
 1086:   } elsif ($ENV{'form.sendreply'}) {
 1087:       if ($ENV{'form.send'}) {
 1088: 	  my $msgid=$ENV{'form.sendreply'};
 1089: 	  my %message=&Apache::lonnet::get('nohist_email',[$msgid]);
 1090: 	  my %content=&unpackagemsg($message{$msgid},1);
 1091: 	  &statuschange($msgid,'replied');
 1092: 	  if ((($ENV{'form.critmsg'}) || ($ENV{'form.sendbck'})) && 
 1093: 	      (&Apache::lonnet::allowed('srm',$ENV{'request.course.id'}))) {
 1094: 	      $r->print(&mt('Sending critical message').': '.
 1095: 			&user_crit_msg($content{'sendername'},
 1096: 				       $content{'senderdomain'},
 1097: 				       &Apache::lonfeedback::clear_out_html($ENV{'form.subject'}),
 1098: 				       &Apache::lonfeedback::clear_out_html($ENV{'form.message'}),
 1099: 				       $ENV{'form.sendbck'}));
 1100: 	  } else {
 1101: 	      $r->print(&mt('Sending').': '.&user_normal_msg($content{'sendername'},
 1102: 							     $content{'senderdomain'},
 1103: 							     &Apache::lonfeedback::clear_out_html($ENV{'form.subject'}),
 1104: 							     &Apache::lonfeedback::clear_out_html($ENV{'form.message'})));
 1105: 	  }
 1106:       }
 1107:       if ($ENV{'form.displayedcrit'}) {
 1108:           &discrit($r);
 1109:       } else {
 1110: 	  &disall($r);
 1111:       }
 1112:   } elsif ($ENV{'form.confirm'}) {
 1113:       foreach (keys %ENV) {
 1114:           if ($_=~/^form\.rec\_(.*)$/) {
 1115: 	      $r->print('<b>Confirming Receipt:</b> '.
 1116:                         &user_crit_received($1).'<br>');
 1117:           }
 1118:           if ($_=~/^form\.reprec\_(.*)$/) {
 1119:               my $msgid=$1;
 1120: 	      $r->print('<b>Confirming Receipt:</b> '.
 1121:                         &user_crit_received($msgid).'<br>');
 1122:               &comprep($r,$msgid);
 1123:           }
 1124:       }
 1125:       &discrit($r);
 1126:   } elsif ($ENV{'form.critical'}) {
 1127:       &discrit($r);
 1128:   } elsif ($ENV{'form.forward'}) {
 1129:       &compout($r,$ENV{'form.forward'});
 1130:   } elsif ($ENV{'form.markread'}) {
 1131:   } elsif ($ENV{'form.markdel'}) {
 1132:       &statuschange($ENV{'form.markdel'},'deleted');
 1133:       &disall($r);
 1134:   } elsif ($ENV{'form.markeddel'}) {
 1135:       my $total=0;
 1136:       foreach (keys %ENV) {
 1137:           if ($_=~/^form\.delmark_(.*)$/) {
 1138: 	      &statuschange(&Apache::lonnet::unescape($1),'deleted');
 1139:               $total++;
 1140:           }
 1141:       }
 1142:       $r->print('Deleted '.$total.' message(s)<p>');
 1143:       &disall($r);
 1144:   } elsif ($ENV{'form.markunread'}) {
 1145:       &statuschange($ENV{'form.markunread'},'new');
 1146:       &disall($r);
 1147:   } elsif ($ENV{'form.compose'}) {
 1148:       &compout($r,'',$ENV{'form.compose'});
 1149:   } elsif ($ENV{'form.recordftf'}) {
 1150:       &facetoface($r,$ENV{'form.recordftf'});
 1151:   } elsif ($ENV{'form.sendmail'}) {
 1152:       my $sendstatus='';
 1153:       if ($ENV{'form.send'}) {
 1154: 	  my %content=();
 1155: 	  undef %content;
 1156: 	  if ($ENV{'form.forwid'}) {
 1157: 	      my $msgid=$ENV{'form.forwid'};
 1158: 	      my %message=&Apache::lonnet::get('nohist_email',[$msgid]);
 1159: 	      %content=&unpackagemsg($message{$msgid},1);
 1160: 	      &statuschange($msgid,'forwarded');
 1161: 	      $ENV{'form.message'}.="\n\n-- Forwarded message --\n\n".
 1162: 		  $content{'message'};
 1163: 	  }
 1164: 	  my %toaddr=();
 1165: 	  undef %toaddr;
 1166: 	  if ($ENV{'form.sendmode'} eq 'group') {
 1167: 	      foreach (keys %ENV) {
 1168: 		  if ($_=~/^form\.send\_to\_\&\&\&[^\&]*\&\&\&\_(.+)$/) {
 1169: 		      $toaddr{$1}='';
 1170: 		  }
 1171: 	      }
 1172: 	  } elsif ($ENV{'form.sendmode'} eq 'upload') {
 1173: 	      foreach (split(/[\n\r\f]+/,$ENV{'form.upfile'})) {
 1174: 		  my ($rec,$txt)=split(/\s*\:\s*/,$_);
 1175: 		  if ($txt) {
 1176: 		      $rec=~s/\@/\:/;
 1177: 		      $toaddr{$rec}.=$txt."\n";
 1178: 		  }
 1179: 	      }
 1180: 	  } else {
 1181: 	      $toaddr{$ENV{'form.recuname'}.':'.$ENV{'form.recdomain'}}='';
 1182: 	  }
 1183: 	  if ($ENV{'form.additionalrec'}) {
 1184: 	      foreach (split(/\,/,$ENV{'form.additionalrec'})) {
 1185: 		  my ($auname,$audom)=split(/\@/,$_);
 1186: 		  $toaddr{$auname.':'.$audom}='';
 1187: 	      }
 1188: 	  }
 1189: 	  foreach (keys %toaddr) {
 1190: 	      my ($recuname,$recdomain)=split(/\:/,$_);
 1191: 	      my $msgtxt=&Apache::lonfeedback::clear_out_html($ENV{'form.message'});
 1192: 	      if ($toaddr{$_}) { $msgtxt.='<hr>'.$toaddr{$_}; }    
 1193: 	      if ((($ENV{'form.critmsg'}) || ($ENV{'form.sendbck'})) && 
 1194: 		  (&Apache::lonnet::allowed('srm',$ENV{'request.course.id'}))) {
 1195: 		  $r->print(&mt('Sending critical message').' ...');
 1196:                   $sendstatus.=' '.&user_crit_msg($recuname,$recdomain,
 1197: 					   &Apache::lonfeedback::clear_out_html($ENV{'form.subject'}),
 1198: 					   $msgtxt,
 1199: 					   $ENV{'form.sendbck'});
 1200: 	      } else {
 1201: 		  $r->print(&mt('Sending').' ...');
 1202:                   $sendstatus.=' '.&user_normal_msg($recuname,$recdomain,
 1203: 				                         &Apache::lonfeedback::clear_out_html($ENV{'form.subject'}),
 1204: 							 $msgtxt,
 1205: 							 $content{'citation'});
 1206: 	      }
 1207: 	      $r->print('<br />');
 1208: 	  }
 1209:       }
 1210:       if ($sendstatus=~/^(\s*(?:ok|con_delayed)\s*)*$/) {
 1211: 	  if ($ENV{'form.displayedcrit'}) {
 1212: 	      &discrit($r);
 1213: 	  } else {
 1214: 	      &disall($r);
 1215: 	  }
 1216:       } else {
 1217: 	  $r->print(
 1218:   '<h2><font color="red">'.&mt('Could not deliver message').'</font></h2>'.
 1219:   &mt('Please use the browser "Back" button and correct the recipient addresses')
 1220: 		    );
 1221:       }
 1222:   } else {
 1223:       &disall($r);
 1224:   }
 1225:   $r->print('</body></html>');
 1226:   return OK;
 1227: 
 1228: }
 1229: # ================================================= Main program, reset counter
 1230: 
 1231: BEGIN {
 1232:     $msgcount=0;
 1233: }
 1234: 
 1235: =pod
 1236: 
 1237: =back
 1238: 
 1239: =cut
 1240: 
 1241: 1; 
 1242: 
 1243: __END__
 1244: 
 1245: 
 1246: 
 1247: 
 1248: 
 1249: 
 1250: 

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