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

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

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