File:  [LON-CAPA] / loncom / interface / lonmsg.pm
Revision 1.65: download - view: text, annotated - select for diffs
Fri Aug 29 20:38:12 2003 UTC (20 years, 9 months ago) by www
Branches: MAIN
CVS tags: HEAD
Bug #2031: Next and Previous buttons when viewing messages
Jon Kitzman
combined with functionality bug #788 (Kitzman) to keep sorting order
when doing "Next" and "Previous"
We never had a routine to get the keys of a namespace in lonnet.pm

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

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