File:  [LON-CAPA] / loncom / interface / lonmsg.pm
Revision 1.98: download - view: text, annotated - select for diffs
Mon May 3 19:10:39 2004 UTC (20 years, 1 month ago) by sakharuk
Branches: MAIN
CVS tags: HEAD
Added 'tex' target neccessary to work with lonaboutme.pm.

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

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