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

1.1       www         1: # The LearningOnline Network with CAPA
1.26      albertel    2: # Routines for messaging
                      3: #
1.91    ! www         4: # $Id: lonmsg.pm,v 1.90 2004/03/01 18:30:50 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>'.
1.85      www       142: 	   '<coursesec>'.$ENV{'request.course.sec'}.'</coursesec>'.
1.1       www       143: 	   '<role>'.$ENV{'request.role'}.'</role>'.
                    144: 	   '<resource>'.$ENV{'request.filename'}.'</resource>'.
1.2       www       145:            '<msgid>'.$msgid.'</msgid>'.
1.49      albertel  146: 	   '<message>'.$message.'</message>';
                    147:     if (defined($citation)) {
                    148: 	$result.='<citation>'.$citation.'</citation>';
                    149:     }
                    150:     if (defined($baseurl)) {
                    151: 	$result.= '<baseurl>'.$baseurl.'</baseurl>';
                    152:     }
1.51      www       153:     if (defined($attachmenturl)) {
1.52      www       154: 	$result.= '<attachmenturl>'.$attachmenturl.'</attachmenturl>';
1.51      www       155:     }
1.49      albertel  156:     return $msgid,$result;
1.1       www       157: }
                    158: 
1.2       www       159: # ================================================== Unpack message into a hash
                    160: 
1.3       www       161: sub unpackagemsg {
1.52      www       162:     my ($message,$notoken)=@_;
1.2       www       163:     my %content=();
                    164:     my $parser=HTML::TokeParser->new(\$message);
                    165:     my $token;
                    166:     while ($token=$parser->get_token) {
                    167:        if ($token->[0] eq 'S') {
                    168: 	   my $entry=$token->[1];
                    169:            my $value=$parser->get_text('/'.$entry);
                    170:            $content{$entry}=$value;
                    171:        }
                    172:     }
1.52      www       173:     if ($content{'attachmenturl'}) {
                    174:        my ($fname,$ft)=($content{'attachmenturl'}=~/\/(\w+)\.(\w+)$/);
                    175:        if ($notoken) {
1.67      www       176: 	   $content{'message'}.='<p>'.&mt('Attachment').': <tt>'.$fname.'.'.$ft.'</tt>';
1.52      www       177:        } else {
1.67      www       178: 	   $content{'message'}.='<p>'.&mt('Attachment').': <a href="'.
1.52      www       179: 	       &Apache::lonnet::tokenwrapper($content{'attachmenturl'}).
                    180: 	       '"><tt>'.$fname.'.'.$ft.'</tt></a>';
                    181:        }
                    182:     }
1.2       www       183:     return %content;
                    184: }
                    185: 
1.6       www       186: # ======================================================= Get info out of msgid
                    187: 
                    188: sub unpackmsgid {
1.7       www       189:     my $msgid=&Apache::lonnet::unescape(shift);
1.6       www       190:     my ($sendtime,$shortsubj,$fromname,$fromdomain)=split(/\:/,
1.7       www       191:                           &Apache::lonnet::unescape($msgid));
1.8       albertel  192:     my %status=&Apache::lonnet::get('email_status',[$msgid]);
1.6       www       193:     if ($status{$msgid}=~/^error\:/) { $status{$msgid}=''; }
                    194:     unless ($status{$msgid}) { $status{$msgid}='new'; }
                    195:     return ($sendtime,$shortsubj,$fromname,$fromdomain,$status{$msgid});
                    196: } 
                    197: 
1.53      www       198: 
                    199: sub sendemail {
                    200:     my ($to,$subject,$body)=@_;
                    201:     $body=
1.67      www       202:     "*** ".&mt('This is an automatic message generated by the LON-CAPA system.')."\n".
                    203:     "*** ".&mt('Please do not reply to this address.')."\n\n".$body;
1.53      www       204:     my $msg = new Mail::Send;
                    205:     $msg->to($to);
                    206:     $msg->subject('[LON-CAPA] '.$subject);
1.68      www       207:     if (my $fh = $msg->open('smtp',Server => 'localhost')) {
                    208: 	print $fh $body;
                    209: 	$fh->close;
                    210:     }
1.53      www       211: }
                    212: 
                    213: # ==================================================== Send notification emails
                    214: 
                    215: sub sendnotification {
                    216:     my ($to,$touname,$toudom,$subj,$crit)=@_;
                    217:     my $sender=$ENV{'environment.firstname'}.' '.$ENV{'environment.lastname'};
                    218:     my $critical=($crit?' critical':'');
                    219:     my $url='http://'.
                    220:       $Apache::lonnet::hostname{&Apache::lonnet::homeserver($touname,$toudom)}.
1.54      www       221:       '/adm/email?username='.$touname.'&domain='.$toudom;
1.53      www       222:     my $body=(<<ENDMSG);
                    223: You received a$critical message from $sender in LON-CAPA. The subject is
                    224: 
                    225:  $subj
                    226: 
                    227: Use
                    228: 
                    229:  $url
                    230: 
                    231: to access this message.
                    232: ENDMSG
                    233:     &sendemail($to,'New'.$critical.' message from '.$sender,$body);
                    234: }
1.40      www       235: # ============================================================= Check for email
                    236: 
                    237: sub newmail {
                    238:     if ((time-$ENV{'user.mailcheck.time'})>300) {
                    239:         my %what=&Apache::lonnet::get('email_status',['recnewemail']);
                    240:         &Apache::lonnet::appenv('user.mailcheck.time'=>time);
                    241:         if ($what{'recnewemail'}>0) { return 1; }
                    242:     }
                    243:     return 0;
                    244: }
                    245: 
1.1       www       246: # =============================== Automated message to the author of a resource
                    247: 
1.58      bowersj2  248: =pod
                    249: 
                    250: =item * B<author_res_msg($filename, $message)>: Sends message $message to the owner
                    251:     of the resource with the URI $filename.
                    252: 
                    253: =cut
                    254: 
1.1       www       255: sub author_res_msg {
                    256:     my ($filename,$message)=@_;
1.2       www       257:     unless ($message) { return 'empty'; }
1.1       www       258:     $filename=&Apache::lonnet::declutter($filename);
1.72      www       259:     my ($domain,$author,@dummy)=split(/\//,$filename);
1.1       www       260:     my $homeserver=&Apache::lonnet::homeserver($author,$domain);
                    261:     if ($homeserver ne 'no_host') {
                    262:        my $id=unpack("%32C*",$message);
1.2       www       263:        my $msgid;
1.72      www       264:        ($msgid,$message)=&packagemsg($filename,$message);
1.3       www       265:        return &Apache::lonnet::reply('put:'.$domain.':'.$author.
1.72      www       266:          ':nohist_res_msgs:'.
                    267:           &Apache::lonnet::escape($filename.'_'.$id).'='.
                    268:           &Apache::lonnet::escape($message),$homeserver);
1.1       www       269:     }
1.2       www       270:     return 'no_host';
1.73      www       271: }
                    272: 
                    273: # =========================================== Retrieve author resource messages
                    274: 
                    275: sub retrieve_author_res_msg {
1.75      www       276:     my $url=shift;
1.73      www       277:     $url=&Apache::lonnet::declutter($url);
1.80      www       278:     my ($domain,$author)=($url=~/^(\w+)\/(\w+)\//);
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.80      www       282: 	if ($_=~/^\Q$url\E\_\d+$/) {
1.73      www       283: 	    my %content=&unpackagemsg($errormsgs{$_});
1.74      www       284: 	    $msgs.='<p><img src="/adm/lonMisc/bomb.gif" /><b>'.
                    285: 		$content{'time'}.'</b>: '.$content{'message'}.
                    286: 		'<br /></p>';
1.73      www       287: 	}
                    288:     } 
                    289:     return $msgs;     
                    290: }
                    291: 
                    292: 
                    293: # =============================== Delete all author messages related to one URL
                    294: 
                    295: sub del_url_author_res_msg {
1.75      www       296:     my $url=shift;
1.73      www       297:     $url=&Apache::lonnet::declutter($url);
1.77      www       298:     my ($domain,$author)=($url=~/^(\w+)\/(\w+)\//);
                    299:     my @delmsgs=();
                    300:     foreach (&Apache::lonnet::getkeys('nohist_res_msgs',$domain,$author)) {
                    301: 	if ($_=~/^\Q$url\E\_\d+$/) {
                    302: 	    push (@delmsgs,$_);
                    303: 	}
                    304:     }
                    305:     return &Apache::lonnet::del('nohist_res_msgs',\@delmsgs,$domain,$author);
1.73      www       306: }
                    307: 
                    308: # ================= Return hash with URLs for which there is a resource message
                    309: 
                    310: sub all_url_author_res_msg {
                    311:     my ($author,$domain)=@_;
1.75      www       312:     my %returnhash=();
1.76      www       313:     foreach (&Apache::lonnet::getkeys('nohist_res_msgs',$domain,$author)) {
1.75      www       314: 	$_=~/^(.+)\_\d+/;
                    315: 	$returnhash{$1}=1;
                    316:     }
                    317:     return %returnhash;
1.1       www       318: }
                    319: 
                    320: # ================================================== Critical message to a user
                    321: 
1.38      www       322: sub user_crit_msg_raw {
1.24      www       323:     my ($user,$domain,$subject,$message,$sendback)=@_;
1.2       www       324: # Check if allowed missing
                    325:     my $status='';
                    326:     my $msgid='undefined';
                    327:     unless (($message)&&($user)&&($domain)) { $status='empty'; };
                    328:     my $homeserver=&Apache::lonnet::homeserver($user,$domain);
                    329:     if ($homeserver ne 'no_host') {
1.3       www       330:        ($msgid,$message)=&packagemsg($subject,$message);
1.24      www       331:        if ($sendback) { $message.='<sendback>true</sendback>'; }
1.4       www       332:        $status=&Apache::lonnet::critical(
                    333:            'put:'.$domain.':'.$user.':critical:'.
                    334:            &Apache::lonnet::escape($msgid).'='.
                    335:            &Apache::lonnet::escape($message),$homeserver);
1.45      www       336:        if ($ENV{'request.course.id'}) {
                    337:           &user_normal_msg_raw(
                    338:             $ENV{'course.'.$ENV{'request.course.id'}.'.num'},
                    339:             $ENV{'course.'.$ENV{'request.course.id'}.'.domain'},
                    340:             'Critical ['.$user.':'.$domain.']',
                    341: 	    $message);
                    342:        }
1.2       www       343:     } else {
                    344:        $status='no_host';
                    345:     }
1.53      www       346: # Notifications
                    347:     my %userenv = &Apache::lonnet::get('environment',['critnotification'],
                    348:                                        $domain,$user);
                    349:     if ($userenv{'critnotification'}) {
                    350:       &sendnotification($userenv{'critnotification'},$user,$domain,$subject,1);
                    351:     }
                    352: # Log this
1.2       www       353:     &Apache::lonnet::logthis(
1.4       www       354:       'Sending critical email '.$msgid.
1.2       www       355:       ', log status: '.
                    356:       &Apache::lonnet::log($ENV{'user.domain'},$ENV{'user.name'},
                    357:                          $ENV{'user.home'},
                    358:       'Sending critical '.$msgid.' to '.$user.' at '.$domain.' with status: '
1.4       www       359:       .$status));
1.2       www       360:     return $status;
                    361: }
                    362: 
1.38      www       363: # New routine that respects "forward" and calls old routine
                    364: 
1.58      bowersj2  365: =pod
                    366: 
                    367: =item * B<user_crit_msg($user, $domain, $subject, $message, $sendback)>: Sends
                    368:     a critical message $message to the $user at $domain. If $sendback is true,
                    369:     a reciept will be sent to the current user when $user recieves the message.
                    370: 
                    371: =cut
                    372: 
1.38      www       373: sub user_crit_msg {
                    374:     my ($user,$domain,$subject,$message,$sendback)=@_;
                    375:     my $status='';
                    376:     my %userenv = &Apache::lonnet::get('environment',['msgforward'],
                    377:                                        $domain,$user);
                    378:     my $msgforward=$userenv{'msgforward'};
                    379:     if ($msgforward) {
                    380:        foreach (split(/\,/,$msgforward)) {
                    381: 	 my ($forwuser,$forwdomain)=split(/\:/,$_);
                    382:          $status.=
                    383: 	   &user_crit_msg_raw($forwuser,$forwdomain,$subject,$message,
                    384:                 $sendback).' ';
                    385:        }
                    386:     } else { 
                    387: 	$status=&user_crit_msg_raw($user,$domain,$subject,$message,$sendback);
                    388:     }
                    389:     return $status;
                    390: }
                    391: 
1.2       www       392: # =================================================== Critical message received
                    393: 
                    394: sub user_crit_received {
1.12      www       395:     my $msgid=shift;
                    396:     my %message=&Apache::lonnet::get('critical',[$msgid]);
1.52      www       397:     my %contents=&unpackagemsg($message{$msgid},1);
1.24      www       398:     my $status='rec: '.($contents{'sendback'}?
1.5       www       399:      &user_normal_msg($contents{'sendername'},$contents{'senderdomain'},
1.82      www       400:                      &mt('Receipt').': '.$ENV{'user.name'}.' '.&mt('at').' '.$ENV{'user.domain'}.', '.$contents{'subject'},
1.67      www       401:                      &mt('User').' '.$ENV{'user.name'}.' '.&mt('at').' '.$ENV{'user.domain'}.
1.42      www       402:                      ' acknowledged receipt of message'."\n".'   "'.
1.67      www       403:                      $contents{'subject'}.'"'."\n".&mt('dated').' '.
1.42      www       404:                      $contents{'time'}.".\n"
                    405:                      ):'no msg req');
1.5       www       406:     $status.=' trans: '.
1.12      www       407:      &Apache::lonnet::put(
                    408:      'nohist_email',{$contents{'msgid'} => $message{$msgid}});
1.5       www       409:     $status.=' del: '.
1.9       albertel  410:      &Apache::lonnet::del('critical',[$contents{'msgid'}]);
1.5       www       411:     &Apache::lonnet::log($ENV{'user.domain'},$ENV{'user.name'},
                    412:                          $ENV{'user.home'},'Received critical message '.
                    413:                          $contents{'msgid'}.
                    414:                          ', '.$status);
1.12      www       415:     return $status;
1.2       www       416: }
                    417: 
                    418: # ======================================================== Normal communication
                    419: 
1.38      www       420: sub user_normal_msg_raw {
1.51      www       421:     my ($user,$domain,$subject,$message,$citation,$baseurl,$attachmenturl)=@_;
1.2       www       422: # Check if allowed missing
                    423:     my $status='';
                    424:     my $msgid='undefined';
                    425:     unless (($message)&&($user)&&($domain)) { $status='empty'; };
                    426:     my $homeserver=&Apache::lonnet::homeserver($user,$domain);
                    427:     if ($homeserver ne 'no_host') {
1.51      www       428:        ($msgid,$message)=&packagemsg($subject,$message,$citation,$baseurl,
                    429:                                      $attachmenturl);
1.4       www       430:        $status=&Apache::lonnet::critical(
                    431:            'put:'.$domain.':'.$user.':nohist_email:'.
                    432:            &Apache::lonnet::escape($msgid).'='.
                    433:            &Apache::lonnet::escape($message),$homeserver);
1.40      www       434:        &Apache::lonnet::put
                    435:                          ('email_status',{'recnewemail'=>time},$domain,$user);
1.2       www       436:     } else {
                    437:        $status='no_host';
1.53      www       438:     }
                    439: # Notifications
                    440:     my %userenv = &Apache::lonnet::get('environment',['notification'],
                    441:                                        $domain,$user);
                    442:     if ($userenv{'notification'}) {
                    443: 	&sendnotification($userenv{'notification'},$user,$domain,$subject,0);
1.2       www       444:     }
                    445:     &Apache::lonnet::log($ENV{'user.domain'},$ENV{'user.name'},
                    446:                          $ENV{'user.home'},
                    447:       'Sending '.$msgid.' to '.$user.' at '.$domain.' with status: '.$status);
                    448:     return $status;
                    449: }
1.38      www       450: 
                    451: # New routine that respects "forward" and calls old routine
                    452: 
1.58      bowersj2  453: =pod
                    454: 
                    455: =item * B<user_normal_msg($user, $domain, $subject, $message,
                    456:     $citation, $baseurl, $attachmenturl)>: Sends a message to the
                    457:     $user at $domain, with subject $subject and message $message.
                    458: 
                    459: =cut
                    460: 
1.38      www       461: sub user_normal_msg {
1.52      www       462:     my ($user,$domain,$subject,$message,$citation,$baseurl,$attachmenturl)=@_;
1.38      www       463:     my $status='';
                    464:     my %userenv = &Apache::lonnet::get('environment',['msgforward'],
                    465:                                        $domain,$user);
                    466:     my $msgforward=$userenv{'msgforward'};
                    467:     if ($msgforward) {
                    468:        foreach (split(/\,/,$msgforward)) {
                    469: 	 my ($forwuser,$forwdomain)=split(/\:/,$_);
                    470:          $status.=
                    471: 	  &user_normal_msg_raw($forwuser,$forwdomain,$subject,$message,
1.52      www       472: 			       $citation,$baseurl,$attachmenturl).' ';
1.38      www       473:        }
                    474:     } else { 
1.49      albertel  475: 	$status=&user_normal_msg_raw($user,$domain,$subject,$message,
1.52      www       476: 				     $citation,$baseurl,$attachmenturl);
1.38      www       477:     }
                    478:     return $status;
                    479: }
                    480: 
1.2       www       481: 
1.7       www       482: # =============================================================== Status Change
                    483: 
                    484: sub statuschange {
                    485:     my ($msgid,$newstatus)=@_;
1.8       albertel  486:     my %status=&Apache::lonnet::get('email_status',[$msgid]);
1.7       www       487:     if ($status{$msgid}=~/^error\:/) { $status{$msgid}=''; }
                    488:     unless ($status{$msgid}) { $status{$msgid}='new'; }
                    489:     unless (($status{$msgid} eq 'replied') || 
                    490:             ($status{$msgid} eq 'forwarded')) {
1.10      albertel  491: 	&Apache::lonnet::put('email_status',{$msgid => $newstatus});
1.7       www       492:     }
1.14      www       493:     if (($newstatus eq 'deleted') || ($newstatus eq 'new')) {
                    494: 	&Apache::lonnet::put('email_status',{$msgid => $newstatus});
                    495:     }
1.7       www       496: }
1.14      www       497: 
1.17      www       498: # ======================================================= Display a course list
                    499: 
                    500: sub discourse {
                    501:     my $r=shift;
                    502:     my %courselist=&Apache::lonnet::dump(
                    503:                    'classlist',
                    504: 		   $ENV{'course.'.$ENV{'request.course.id'}.'.domain'},
                    505: 		   $ENV{'course.'.$ENV{'request.course.id'}.'.num'});
                    506:     my $now=time;
1.67      www       507:     my %lt=&Apache::lonlocal::texthash('cfa' => 'Check for All',
                    508:             'cfs' => 'Check for Section/Group',
                    509:             'cfn' => 'Check for None');
1.17      www       510:     $r->print(<<ENDDISHEADER);
                    511: <input type=hidden name=sendmode value=group>
                    512: <script>
                    513:     function checkall() {
                    514: 	for (i=0; i<document.forms.compemail.elements.length; i++) {
                    515:             if 
                    516:           (document.forms.compemail.elements[i].name.indexOf('send_to_')==0) {
                    517: 	      document.forms.compemail.elements[i].checked=true;
                    518:             }
                    519:         }
                    520:     }
                    521: 
1.19      www       522:     function checksec() {
                    523: 	for (i=0; i<document.forms.compemail.elements.length; i++) {
                    524:             if 
                    525:           (document.forms.compemail.elements[i].name.indexOf
                    526:            ('send_to_&&&'+document.forms.compemail.chksec.value)==0) {
                    527: 	      document.forms.compemail.elements[i].checked=true;
                    528:             }
                    529:         }
                    530:     }
                    531: 
1.17      www       532:     function uncheckall() {
                    533: 	for (i=0; i<document.forms.compemail.elements.length; i++) {
                    534:             if 
                    535:           (document.forms.compemail.elements[i].name.indexOf('send_to_')==0) {
                    536: 	      document.forms.compemail.elements[i].checked=false;
                    537:             }
                    538:         }
                    539:     }
                    540: </script>
1.67      www       541: <input type=button onClick="checkall()" value="$lt{'cfa'}">&nbsp;
                    542: <input type=button onClick="checksec()" value="$lt{'cfs'}">
1.19      www       543: <input type=text size=5 name=chksec>&nbsp;
1.67      www       544: <input type=button onClick="uncheckall()" value="$lt{'cfn'}">
1.17      www       545: <p>
                    546: ENDDISHEADER
1.61      www       547:     my %coursepersonnel=
                    548:        &Apache::lonnet::get_course_adv_roles();
                    549:     foreach my $role (sort keys %coursepersonnel) {
                    550:        foreach (split(/\,/,$coursepersonnel{$role})) {
                    551: 	   my ($puname,$pudom)=split(/\:/,$_);
                    552: 	   $r->print(
                    553:              '<br /><input type="checkbox" name="send_to_&&&&&&_'.
                    554:              $puname.':'.$pudom.'" /> '.
                    555: 		     &Apache::loncommon::plainname($puname,
                    556:                           $pudom).' ('.$_.'), <i>'.$role.'</i>');
                    557: 	}
                    558:     }
                    559: 
1.28      harris41  560:     foreach (sort keys %courselist) {
1.17      www       561:         my ($end,$start)=split(/\:/,$courselist{$_});
                    562:         my $active=1;
                    563:         if (($end) && ($now>$end)) { $active=0; }
                    564:         if ($active) {
                    565:            my ($sname,$sdom)=split(/\:/,$_);
                    566:            my %reply=&Apache::lonnet::get('environment',
                    567:               ['firstname','middlename','lastname','generation'],
                    568:               $sdom,$sname);
1.19      www       569:            my $section=&Apache::lonnet::usection
                    570: 	       ($sdom,$sname,$ENV{'request.course.id'});
                    571:            $r->print(
                    572:         '<br><input type=checkbox name="send_to_&&&'.$section.'&&&_'.$_.'"> '.
1.17      www       573: 		      $reply{'firstname'}.' '. 
                    574:                       $reply{'middlename'}.' '.
                    575:                       $reply{'lastname'}.' '.
                    576:                       $reply{'generation'}.
1.19      www       577:                       ' ('.$_.') '.$section);
1.17      www       578:         } 
1.28      harris41  579:     }
1.17      www       580: }
                    581: 
1.13      www       582: # ==================================================== Display Critical Message
1.5       www       583: 
1.12      www       584: sub discrit {
                    585:     my $r=shift;
1.67      www       586:     my $header = '<h1><font color=red>'.&mt('Critical Messages').'</font></h1>'.
1.30      matthew   587:         '<form action=/adm/email method=post>'.
                    588:         '<input type=hidden name=confirm value=true>';
                    589:     my %what=&Apache::lonnet::dump('critical');
                    590:     my $result = '';
                    591:     foreach (sort keys %what) {
                    592:         my %content=&unpackagemsg($what{$_});
                    593:         next if ($content{'senderdomain'} eq '');
                    594:         $content{'message'}=~s/\n/\<br\>/g;
1.67      www       595:         $result.='<hr>'.&mt('From').': <b>'.
1.37      www       596: &Apache::loncommon::aboutmewrapper(
                    597:  &Apache::loncommon::plainname($content{'sendername'},$content{'senderdomain'}),$content{'sendername'},$content{'senderdomain'}).'</b> ('.
                    598: $content{'sendername'}.'@'.
                    599:             $content{'senderdomain'}.') '.$content{'time'}.
1.67      www       600:             '<br>'.&mt('Subject').': '.$content{'subject'}.
1.36      www       601:             '<br><blockquote>'.
                    602:               &Apache::lontexconvert::msgtexconverted($content{'message'}).
1.84      www       603:             '</blockquote><small>'.
                    604: &mt('You have to confirm that you received this message. After confirmation, this message will be moved to your regular inbox').
                    605:             '</small><br />'.
1.67      www       606:             '<input type=submit name="rec_'.$_.'" value="'.&mt('Confirm Receipt').'">'.
1.30      matthew   607:             '<input type=submit name="reprec_'.$_.'" '.
1.67      www       608:                   'value="'.&mt('Confirm Receipt and Reply').'">';
1.30      matthew   609:     }
                    610:     # Check to see if there were any messages.
                    611:     if ($result eq '') {
1.67      www       612:         $result = "<h2>".&mt('You have no critical messages.')."</h2>".
                    613: 	    '<a href="/adm/roles">'.&mt('Select a course').'</a>';
1.30      matthew   614:     } else {
                    615:         $r->print($header);
                    616:     }
                    617:     $r->print($result);
                    618:     $r->print('<input type=hidden name="displayedcrit" value="true"></form>');
1.12      www       619: }
                    620: 
1.13      www       621: # =============================================================== Compose reply
                    622: 
                    623: sub comprep {
                    624:     my ($r,$msgid)=@_;
                    625:       my %message=&Apache::lonnet::get('nohist_email',[$msgid]);
1.52      www       626:       my %content=&unpackagemsg($message{$msgid},1);
1.13      www       627:       my $quotemsg='> '.$content{'message'};
                    628:       $quotemsg=~s/\r/\n/g;
                    629:       $quotemsg=~s/\f/\n/g;
                    630:       $quotemsg=~s/\n+/\n\> /g;
1.57      www       631:       my $torepl=&Apache::loncommon::aboutmewrapper(
                    632:  &Apache::loncommon::plainname($content{'sendername'},$content{'senderdomain'}),$content{'sendername'},$content{'senderdomain'}).' ('.
                    633: $content{'sendername'}.'@'.
                    634:             $content{'senderdomain'}.')';
1.67      www       635:       my $subject=&mt('Re').': '.$content{'subject'};
1.13      www       636:       my $dispcrit='';
                    637:       if (&Apache::lonnet::allowed('srm',$ENV{'request.course.id'})) {
1.35      bowersj2  638: 	 my $crithelp = Apache::loncommon::help_open_topic("Course_Critical_Message");
1.13      www       639:          $dispcrit=
1.67      www       640:  '<input type=checkbox name=critmsg> '.&mt('Send as critical message').' ' . $crithelp . 
1.35      bowersj2  641:  '<br>'.
1.67      www       642:  '<input type=checkbox name=sendbck> '.&mt('Send as critical message').' ' .
                    643:  &mt('and return receipt') . $crithelp . '<p>';
1.13      www       644:       }
1.69      www       645:     my %lt=&Apache::lonlocal::texthash(
                    646: 				   'to' => 'To',
                    647: 				   'sb' => 'Subject',
                    648: 				   'sr' => 'Send Reply',
                    649: 				   'ca' => 'Cancel'
                    650: 				   );
1.13      www       651:       $r->print(<<"ENDREPLY");
1.69      www       652: <form action="/adm/email" method="post">
                    653: <input type="hidden" name="sendreply" value="$msgid">
                    654: $lt{'to'}: $torepl<br />
                    655: $lt{'sb'}: <input type="text" size=50 name="subject" value="$subject"><p>
                    656: <textarea name="message" cols="84" rows="10" wrap="hard">
1.13      www       657: $quotemsg
1.69      www       658: </textarea></p><br />
1.13      www       659: $dispcrit
1.69      www       660: <input type="submit" name="send" value="$lt{'sr'}" />
                    661: <input type="submit" name="cancel" value="$lt{'ca'}"/ >
1.13      www       662: </form>
                    663: ENDREPLY
                    664: }
                    665: 
1.65      www       666: sub sortedmessages {
                    667:     my @messages = &Apache::lonnet::getkeys('nohist_email');
                    668:     #unpack the varibles and repack into temp for sorting
                    669:     my @temp;
                    670:     foreach (@messages) {
                    671: 	my $msgid=&Apache::lonnet::escape($_);
                    672: 	my ($sendtime,$shortsubj,$fromname,$fromdomain,$status)=
                    673: 	    &Apache::lonmsg::unpackmsgid($msgid);
                    674: 	my @temp1 = ($sendtime,$shortsubj,$fromname,$fromdomain,$status,
                    675: 		     $msgid);
                    676: 	push @temp ,\@temp1;
                    677:     }
                    678:     #default sort
                    679:     @temp = sort  {$a->[0] <=> $b->[0]} @temp;    
                    680:     if ($ENV{'form.sortedby'} eq "date"){
                    681:         @temp = sort  {$a->[0] <=> $b->[0]} @temp;    
                    682:     }
                    683:     if ($ENV{'form.sortedby'} eq "revdate"){
                    684:     	@temp = sort  {$b->[0] <=> $a->[0]} @temp; 
                    685:     }
                    686:     if ($ENV{'form.sortedby'} eq "user"){
                    687: 	@temp = sort  {lc($a->[2]) cmp lc($b->[2])} @temp;
                    688:     }
                    689:     if ($ENV{'form.sortedby'} eq "revuser"){
                    690: 	@temp = sort  {lc($b->[2]) cmp lc($a->[2])} @temp;
                    691:     }
                    692:     if ($ENV{'form.sortedby'} eq "domain"){
                    693:         @temp = sort  {$a->[3] cmp $b->[3]} @temp;
                    694:     }
                    695:     if ($ENV{'form.sortedby'} eq "revdomain"){
                    696:         @temp = sort  {$b->[3] cmp $a->[3]} @temp;
                    697:     }
                    698:     if ($ENV{'form.sortedby'} eq "subject"){
                    699:         @temp = sort  {lc($a->[1]) cmp lc($b->[1])} @temp;
                    700:     }
                    701:     if ($ENV{'form.sortedby'} eq "revsubject"){
                    702:         @temp = sort  {lc($b->[1]) cmp lc($a->[1])} @temp;
                    703:     }
                    704:     if ($ENV{'form.sortedby'} eq "status"){
                    705:         @temp = sort  {$a->[4] cmp $b->[4]} @temp;
                    706:     }
                    707:     if ($ENV{'form.sortedby'} eq "revstatus"){
                    708:         @temp = sort  {$b->[4] cmp $a->[4]} @temp;
                    709:     }
                    710:     return @temp;
                    711: }
                    712: 
1.15      www       713: # ======================================================== Display all messages
                    714: 
1.14      www       715: sub disall {
                    716:     my $r=shift;
1.29      www       717:      $r->print(<<ENDDISHEADER);
                    718: <script>
                    719:     function checkall() {
                    720: 	for (i=0; i<document.forms.disall.elements.length; i++) {
                    721:             if 
                    722:           (document.forms.disall.elements[i].name.indexOf('delmark_')==0) {
                    723: 	      document.forms.disall.elements[i].checked=true;
                    724:             }
                    725:         }
                    726:     }
                    727: 
                    728:     function uncheckall() {
                    729: 	for (i=0; i<document.forms.disall.elements.length; i++) {
                    730:             if 
                    731:           (document.forms.disall.elements[i].name.indexOf('delmark_')==0) {
                    732: 	      document.forms.disall.elements[i].checked=false;
                    733:             }
                    734:         }
                    735:     }
                    736: </script>
                    737: ENDDISHEADER
1.67      www       738:     $r->print('<h1>'.&mt('Display All Messages').'</h1><form method=post name=disall '.
1.63      albertel  739: 	      'action="/adm/email">'.
                    740: 	      '<table border=2><tr><th colspan=2>&nbsp</th><th>');
1.62      www       741:     if ($ENV{'form.sortedby'} eq "revdate") {
1.67      www       742: 	$r->print('<a href = "?sortedby=date">'.&mt('Date').'</a></th>');
1.62      www       743:     } else {
1.67      www       744: 	$r->print('<a href = "?sortedby=revdate">'.&mt('Date').'</a></th>');
1.62      www       745:     }
                    746:     $r->print('<th>');
                    747:     if ($ENV{'form.sortedby'} eq "revuser") {
1.67      www       748: 	$r->print('<a href = "?sortedby=user">'.&mt('Username').'</a>');
1.62      www       749:     } else {
1.67      www       750: 	$r->print('<a href = "?sortedby=revuser">'.&mt('Username').'</a>');
1.62      www       751:     }
                    752:     $r->print('</th><th>');
                    753:     if ($ENV{'form.sortedby'} eq "revdomain") {
1.67      www       754: 	$r->print('<a href = "?sortedby=domain">'.&mt('Domain').'</a>');
1.62      www       755:     } else {
1.67      www       756: 	$r->print('<a href = "?sortedby=revdomain">'.&mt('Domain').'</a>');
1.62      www       757:     }
                    758:     $r->print('</th><th>');
                    759:     if ($ENV{'form.sortedby'} eq "revsubject") {
1.67      www       760: 	$r->print('<a href = "?sortedby=subject">'.&mt('Subject').'</a>');
1.62      www       761:     } else {
1.67      www       762:     	$r->print('<a href = "?sortedby=revsubject">'.&mt('Subject').'</a>');
1.62      www       763:     }
                    764:     $r->print('</th><th>');
                    765:     if ($ENV{'form.sortedby'} eq "revstatus") {
1.67      www       766: 	$r->print('<a href = "?sortedby=status">'.&mt('Status').'</th>');
1.62      www       767:     } else {
1.67      www       768:      	$r->print('<a href = "?sortedby=revstatus">'.&mt('Status').'</th>');
1.62      www       769:     }
                    770:     $r->print('</tr>');
1.65      www       771:     my @temp=sortedmessages();
1.63      albertel  772:     foreach (@temp){
1.64      www       773: 	my ($sendtime,$shortsubj,$fromname,$fromdomain,$status,$origID)= @$_;
1.63      albertel  774: 	if (($status ne 'deleted') && defined($sendtime) && $sendtime!~/error/) {
1.39      albertel  775: 	    if ($status eq 'new') {
                    776: 		$r->print('<tr bgcolor="#FFBB77">');
                    777: 	    } elsif ($status eq 'read') {
                    778: 		$r->print('<tr bgcolor="#BBBB77">');
                    779: 	    } elsif ($status eq 'replied') {
1.62      www       780: 		$r->print('<tr bgcolor="#AAAA88">'); 
1.39      albertel  781: 	    } else {
                    782: 		$r->print('<tr bgcolor="#99BBBB">');
                    783: 	    }
1.65      www       784: 	    $r->print('<td><a href="/adm/email?display='.$origID.$sqs. 
1.67      www       785: 		      '">'.&mt('Open').'</a></td><td><a href="/adm/email?markdel='.$origID.$sqs.
                    786: 		      '">'.&mt('Delete').'</a><input type=checkbox name="delmark_'.$origID.'"></td>'.
1.66      www       787: 		      '<td>'.&Apache::lonlocal::locallocaltime($sendtime).'</td><td>'.
1.39      albertel  788: 		      $fromname.'</td><td>'.$fromdomain.'</td><td>'.
1.14      www       789: 		      &Apache::lonnet::unescape($shortsubj).'</td><td>'.
                    790:                       $status.'</td></tr>');
1.63      albertel  791: 	}
                    792:     }   
                    793:     $r->print('</table><p>'.
1.67      www       794:               '<a href="javascript:checkall()">'.&mt('Check All').'</a>&nbsp;'.
                    795:               '<a href="javascript:uncheckall()">'.&mt('Uncheck All').'</a><p>'.
1.65      www       796: 	      '<input type="hidden" name="sortedby" value="'.$ENV{'form.sortedby'}.'" />'.
1.67      www       797:               '<input type=submit name="markeddel" value="'.&mt('Delete Checked').'">'.
1.25      www       798:               '</form></body></html>');
1.14      www       799: }
                    800: 
1.15      www       801: # ============================================================== Compose output
                    802: 
                    803: sub compout {
1.17      www       804:     my ($r,$forwarding,$broadcast)=@_;
1.89      www       805:     &printheader($r,'/adm/email?compose=upload',
                    806: 	     'Distribute from uploaded file');
                    807:     my $dispcrit='';
1.15      www       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.91    ! 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);
1.91    ! www       872: <input type="hidden" name="sendmode" value="upload" />
1.86      www       873: <input type="hidden" name="send" value="on" />
1.22      www       874: <h3>Generate messages from a file</h3>
1.31      matthew   875: <p>
1.91    ! www       876: Subject: <input type="text" size="50" name="subject" />
1.31      matthew   877: </p>
                    878: <p>General message text<br />
1.91    ! www       879: <textarea name="message" cols="60" rows="10" wrap="hard">$dismsg
1.31      matthew   880: </textarea></p>
                    881: <p>
                    882: The file format for the uploaded portion of the message is:
1.22      www       883: <pre>
                    884: username1\@domain1: text
                    885: username2\@domain2: text
1.31      matthew   886: username3\@domain1: text
1.22      www       887: </pre>
1.31      matthew   888: </p>
                    889: <p>
1.22      www       890: The messages will be assembled from all lines with the respective 
1.31      matthew   891: <tt>username\@domain</tt>, and appended to the general message text.</p>
                    892: <p>
1.91    ! www       893: <input type="file" name="upfile" size="40" /></p><p>
1.22      www       894: $dispcrit
1.91    ! www       895: <input type="submit" value="Upload and send" /></p>
1.22      www       896: ENDUPLOAD
                    897:     }
1.17      www       898:     if ($broadcast eq 'group') {
                    899:        &discourse;
                    900:     }
                    901:     $r->print('</form>');
1.15      www       902: }
                    903: 
1.45      www       904: # ---------------------------------------------------- Display all face to face
                    905: 
                    906: sub disfacetoface {
                    907:     my ($r,$user,$domain)=@_;
                    908:     unless ($ENV{'request.course.id'}) { return; }
                    909:     unless (&Apache::lonnet::allowed('srm',$ENV{'request.course.id'})) {
                    910: 	return;
                    911:     }
                    912:     my %records=&Apache::lonnet::dump('nohist_email',
                    913: 			 $ENV{'course.'.$ENV{'request.course.id'}.'.domain'},
                    914: 			 $ENV{'course.'.$ENV{'request.course.id'}.'.num'},
                    915:                          '%255b'.$user.'%253a'.$domain.'%255d');
                    916:     my $result='';
                    917:     foreach (sort keys %records) {
                    918:         my %content=&unpackagemsg($records{$_});
                    919:         next if ($content{'senderdomain'} eq '');
                    920:         $content{'message'}=~s/\n/\<br\>/g;
                    921:         if ($content{'subject'}=~/^Record/) {
1.69      www       922: 	    $result.='<h3>'.&mt('Record').'</h3>';
1.45      www       923:         } else {
1.69      www       924:             $result.='<h3>'.&mt('Sent Message').'</h3>';
1.45      www       925:             %content=&unpackagemsg($content{'message'});
                    926:             $content{'message'}=
                    927:                 '<b>Subject: '.$content{'subject'}.'</b><br />'.
                    928: 		$content{'message'};
                    929:         }
1.69      www       930:         $result.=&mt('By').': <b>'.
1.45      www       931: &Apache::loncommon::aboutmewrapper(
                    932:  &Apache::loncommon::plainname($content{'sendername'},$content{'senderdomain'}),$content{'sendername'},$content{'senderdomain'}).'</b> ('.
                    933: $content{'sendername'}.'@'.
                    934:             $content{'senderdomain'}.') '.$content{'time'}.
                    935:             '<br><blockquote>'.
                    936:               &Apache::lontexconvert::msgtexconverted($content{'message'}).
                    937: 	      '</blockquote>';
                    938:      }
                    939:     # Check to see if there were any messages.
                    940:     if ($result eq '') {
1.46      www       941:         $r->print("<p><b>No notes, face-to-face discussion records, or critical messages in this course.</b></p>");
1.45      www       942:     } else {
                    943:        $r->print($result);
                    944:     }
                    945: }
                    946: 
1.44      www       947: # ---------------------------------------------------------------- Face to face
                    948: 
                    949: sub facetoface {
                    950:     my ($r,$stage)=@_;
                    951:     unless (&Apache::lonnet::allowed('srm',$ENV{'request.course.id'})) {
                    952: 	return;
                    953:     }
1.89      www       954:     &printheader($r,
                    955: 		 '/adm/email?recordftf=query',
                    956: 		 "User Notes, Face-to-Face, Critical Messages");
1.46      www       957: # from query string
1.88      www       958: 
1.46      www       959:     if ($ENV{'form.recname'}) { $ENV{'form.recuname'}=$ENV{'form.recname'}; }
                    960:     if ($ENV{'form.recdom'}) { $ENV{'form.recdomain'}=$ENV{'form.recdom'}; }
                    961: 
1.44      www       962:     my $defdom=$ENV{'user.domain'};
1.46      www       963: # already filled in
1.44      www       964:     if ($ENV{'form.recdomain'}) { $defdom=$ENV{'form.recdomain'}; }
1.46      www       965: # generate output
1.44      www       966:     my $domform = &Apache::loncommon::select_dom_form($defdom,'recdomain');
1.46      www       967:     my $stdbrws = &Apache::loncommon::selectstudent_link
                    968: 	('stdselect','recuname','recdomain');
1.88      www       969:     my %lt=&Apache::lonlocal::texthash('user' => 'Username',
                    970: 				       'dom' => 'Domain',
                    971: 				       'head' => 'User Notes, Records of Face-To-Face Discussions, and Critical Messages in Course',
                    972: 				       'subm' => 'Retrieve discussion and message records',
                    973: 				       'newr' => 'New Record (record is visible to course faculty and staff)',
                    974: 				       'post' => 'Post this Record');
1.44      www       975:     $r->print(<<"ENDTREC");
1.88      www       976: <h3>$lt{'head'}</h3>
1.46      www       977: <form method="post" action="/adm/email" name="stdselect">
1.44      www       978: <input type="hidden" name="recordftf" value="retrieve" />
                    979: <table>
1.88      www       980: <tr><td>$lt{'user'}:</td><td><input type="text" size="12" name="recuname" value="$ENV{'form.recuname'}" /></td>
1.44      www       981: <td rowspan="2">
1.46      www       982: $stdbrws
1.88      www       983: <input type="submit" value="$lt{'subm'}" /></td>
1.44      www       984: </tr>
1.88      www       985: <tr><td>$lt{'dom'}:</td>
1.44      www       986: <td>$domform</td></tr>
                    987: </table>
                    988: </form>
                    989: ENDTREC
                    990:     if (($stage ne 'query') &&
                    991:         ($ENV{'form.recdomain'}) && ($ENV{'form.recuname'})) {
                    992:         chomp($ENV{'form.newrecord'});
                    993:         if ($ENV{'form.newrecord'}) {
1.45      www       994:            &user_normal_msg_raw(
                    995:             $ENV{'course.'.$ENV{'request.course.id'}.'.num'},
                    996:             $ENV{'course.'.$ENV{'request.course.id'}.'.domain'},
1.88      www       997:             &mt('Record').
                    998: 	     ' ['.$ENV{'form.recuname'}.':'.$ENV{'form.recdomain'}.']',
1.45      www       999: 	    $ENV{'form.newrecord'});
1.44      www      1000:         }
1.46      www      1001:         $r->print('<h3>'.&Apache::loncommon::plainname($ENV{'form.recuname'},
                   1002: 				     $ENV{'form.recdomain'}).'</h3>');
1.45      www      1003:         &disfacetoface($r,$ENV{'form.recuname'},$ENV{'form.recdomain'});
1.44      www      1004: 	$r->print(<<ENDRHEAD);
                   1005: <form method="post" action="/adm/email">
                   1006: <input name="recdomain" value="$ENV{'form.recdomain'}" type="hidden" />
                   1007: <input name="recuname" value="$ENV{'form.recuname'}" type="hidden" />
                   1008: ENDRHEAD
                   1009:         $r->print(<<ENDBFORM);
1.88      www      1010: <hr />$lt{'newr'}<br />
1.44      www      1011: <textarea name="newrecord" cols="80" rows="10" wrap="hard"></textarea>
1.45      www      1012: <br />
                   1013: <input type="hidden" name="recordftf" value="post" />
1.88      www      1014: <input type="submit" value="$lt{'post'}" />
1.44      www      1015: </form>
                   1016: ENDBFORM
                   1017:     }
                   1018: }
1.91    ! www      1019: 
        !          1020: # ----------------------------------------------- Generate the text input field
        !          1021: 
        !          1022: sub textinputarea {
        !          1023:     my ($r,$name,$quote)=@_;
        !          1024:     $r->print('<textarea name="'.$name.
        !          1025: 	      '" cols="80" rows="12" wrap="hard">'.$quote.'</textarea>');
        !          1026: }
        !          1027: 
1.90      www      1028: # ----------------------------------------------------------- Display a message
                   1029: 
                   1030: sub displaymessage {
                   1031:     my ($r,$msgid)=@_;
                   1032:     &statuschange($msgid,'read');
                   1033:     my %message=&Apache::lonnet::get('nohist_email',[$msgid]);
                   1034:     my %content=&unpackagemsg($message{$msgid});
                   1035: # info to generate "next" and "previous" buttons
                   1036:     my @messages=&sortedmessages();
                   1037:     my $counter=0;
                   1038:     $r->print('<pre>');
                   1039:     my $escmsgid=&Apache::lonnet::escape($msgid);
                   1040:     foreach (@messages) {
                   1041: 	if ($_->[5] eq $escmsgid){
                   1042: 	    last;
                   1043: 	}
                   1044: 	$counter++;
                   1045:     }
                   1046:     $r->print('</pre>');
                   1047:     my $number_of_messages = scalar(@messages); #subtract 1 for last index
                   1048: # start output
                   1049:     &printheader($r,'/adm/email?display='.$msgid,'Display a Message','',$content{'baseurl'});
                   1050:     my %courseinfo=&Apache::lonnet::coursedescription($content{'courseid'});
                   1051: # Functions
                   1052:     $r->print('<table border="2" width="100%"><tr bgcolor="#FFFFAA"><td>'.&mt('Functions').':</td>'.
                   1053: 	      '<td><a href="/adm/email?replyto='.&Apache::lonnet::escape($msgid).$sqs.
                   1054: 	      '"><b>'.&mt('Reply').'</b></a></td>'.
                   1055: 	      '<td><a href="/adm/email?forward='.&Apache::lonnet::escape($msgid).$sqs.
                   1056: 	      '"><b>'.&mt('Forward').'</b></a></td>'.
                   1057: 	      '<td><a href="/adm/email?markunread='.&Apache::lonnet::escape($msgid).$sqs.
                   1058: 	      '"><b>'.&mt('Mark Unread').'</b></a></td>'.
                   1059: 	      '<td><a href="/adm/email?markdel='.&Apache::lonnet::escape($msgid).$sqs.
                   1060: 	      '"><b>Delete</b></a></td>'.
                   1061: 	      '<td><a href="/adm/email?sortedby='.$ENV{'form.sortedby'}.
                   1062: 	      '"><b>'.&mt('Display all Messages').'</b></a></td>');
                   1063:     if ($counter > 0){
                   1064: 	$r->print('<td><a href="/adm/email?display='.$messages[$counter-1]->[5].$sqs.
                   1065: 		  '"><b>'.&mt('Previous').'</b></a></td>');
                   1066:     }
                   1067:     if ($counter < $number_of_messages - 1){
                   1068: 	$r->print('<td><a href="/adm/email?display='.$messages[$counter+1]->[5].$sqs.
                   1069: 		  '"><b>'.&mt('Next').'</b></a></td>');
                   1070:     }
                   1071:     $r->print('</tr></table>');
                   1072:     $r->print('<br /><b>'.&mt('Subject').':</b> '.$content{'subject'}.
                   1073: 	      '<br /><b>'.&mt('From').':</b> '.
                   1074: 	      &Apache::loncommon::aboutmewrapper(
                   1075: 						 &Apache::loncommon::plainname($content{'sendername'},$content{'senderdomain'}),
                   1076: 						 $content{'sendername'},$content{'senderdomain'}).' ('.
                   1077: 	      $content{'sendername'}.' at '.
                   1078: 	      $content{'senderdomain'}.') '.
                   1079: 	      ($content{'courseid'}?'<br /><b>'.&mt('Course').':</b> '.$courseinfo{'description'}.
                   1080: 	       ($content{'coursesec'}?' ('.&mt('Group/Section').': '.$content{'coursesec'}.')':''):'').
                   1081: 	      '<br /><b>'.&mt('Time').':</b> '.$content{'time'}.
                   1082: 	      '<p><pre>'.
                   1083: 	      &Apache::lontexconvert::msgtexconverted($content{'message'},1).
                   1084: 	      '</pre><hr />'.$content{'citation'}.'</p>');
                   1085:     return;   
                   1086: }
1.44      www      1087: 
1.88      www      1088: # ================================================================== The Header
                   1089: 
                   1090: sub header {
1.90      www      1091:     my ($r,$title,$baseurl)=@_;
1.88      www      1092:     $r->print('<html><head><title>Communication and Messages</title>');
                   1093:     if ($baseurl) {
                   1094: 	$r->print("<base href=\"http://$ENV{'SERVER_NAME'}/$baseurl\" />");
                   1095:     }
                   1096:     $r->print(&Apache::loncommon::studentbrowser_javascript().'</head>'.
                   1097: 	      &Apache::loncommon::bodytag('Communication and Messages'));
                   1098:         $r->print(&Apache::lonhtmlcommon::breadcrumbs
1.90      www      1099:                   (undef,($title?$title:'Communication and Messages')));
1.88      www      1100: 
                   1101: }
                   1102: 
1.90      www      1103: # ---------------------------------------------------------------- Print header
                   1104: 
                   1105: sub printheader {
                   1106:     my ($r,$url,$desc,$title,$baseurl)=@_;
                   1107:     &Apache::lonhtmlcommon::add_breadcrumb
                   1108: 	({href=>$url,
                   1109: 	  text=>$desc});
                   1110:     &header($r,$title,$baseurl);
                   1111: }
                   1112: 
                   1113: 
1.13      www      1114: # ===================================================================== Handler
                   1115: 
1.5       www      1116: sub handler {
                   1117:     my $r=shift;
                   1118: 
                   1119: # ----------------------------------------------------------- Set document type
1.87      www      1120:     
                   1121:     &Apache::loncommon::content_type($r,'text/html');
                   1122:     $r->send_http_header;
                   1123:     
                   1124:     return OK if $r->header_only;
                   1125:     
1.6       www      1126: # --------------------------- Get query string for limited number of parameters
1.32      matthew  1127:     &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'},
                   1128:         ['display','replyto','forward','markread','markdel','markunread',
1.44      www      1129:          'sendreply','compose','sendmail','critical','recname','recdom',
1.62      www      1130:          'recordftf','sortedby']);
1.65      www      1131:     $sqs='&sortedby='.$ENV{'form.sortedby'};
1.40      www      1132: # ------------------------------------------------------ They checked for email
1.87      www      1133:     &Apache::lonnet::put('email_status',{'recnewemail'=>0});
1.88      www      1134: 
                   1135: # ----------------------------------------------------------------- Breadcrumbs
                   1136: 
                   1137:     &Apache::lonhtmlcommon::clear_breadcrumbs();
                   1138:     &Apache::lonhtmlcommon::add_breadcrumb
                   1139:         ({href=>"/adm/communicate",
                   1140:           text=>"Communication/Messages",
                   1141:           faq=>12,bug=>'Communication Tools',});
                   1142: 
1.5       www      1143: # --------------------------------------------------------------- Render Output
1.88      www      1144: 
1.87      www      1145:     if ($ENV{'form.display'}) {
1.90      www      1146: 	&displaymessage($r,$ENV{'form.display'});
1.87      www      1147:     } elsif ($ENV{'form.replyto'}) {
                   1148: 	&comprep($r,$ENV{'form.replyto'});
                   1149:     } elsif ($ENV{'form.sendreply'}) {
                   1150: 	if ($ENV{'form.send'}) {
                   1151: 	    my $msgid=$ENV{'form.sendreply'};
                   1152: 	    my %message=&Apache::lonnet::get('nohist_email',[$msgid]);
                   1153: 	    my %content=&unpackagemsg($message{$msgid},1);
                   1154: 	    &statuschange($msgid,'replied');
                   1155: 	    if ((($ENV{'form.critmsg'}) || ($ENV{'form.sendbck'})) && 
                   1156: 		(&Apache::lonnet::allowed('srm',$ENV{'request.course.id'}))) {
                   1157: 		$r->print(&mt('Sending critical message').': '.
                   1158: 			  &user_crit_msg($content{'sendername'},
                   1159: 					 $content{'senderdomain'},
                   1160: 					 &Apache::lonfeedback::clear_out_html($ENV{'form.subject'}),
                   1161: 					 &Apache::lonfeedback::clear_out_html($ENV{'form.message'}),
                   1162: 					 $ENV{'form.sendbck'}));
                   1163: 	    } else {
                   1164: 		$r->print(&mt('Sending').': '.&user_normal_msg($content{'sendername'},
                   1165: 							       $content{'senderdomain'},
                   1166: 							       &Apache::lonfeedback::clear_out_html($ENV{'form.subject'}),
                   1167: 							       &Apache::lonfeedback::clear_out_html($ENV{'form.message'})));
                   1168: 	    }
                   1169: 	}
                   1170: 	if ($ENV{'form.displayedcrit'}) {
                   1171: 	    &discrit($r);
                   1172: 	} else {
                   1173: 	    &disall($r);
                   1174: 	}
                   1175:     } elsif ($ENV{'form.confirm'}) {
                   1176: 	foreach (keys %ENV) {
                   1177: 	    if ($_=~/^form\.rec\_(.*)$/) {
                   1178: 		$r->print('<b>Confirming Receipt:</b> '.
                   1179: 			  &user_crit_received($1).'<br>');
                   1180: 	    }
                   1181: 	    if ($_=~/^form\.reprec\_(.*)$/) {
                   1182: 		my $msgid=$1;
                   1183: 		$r->print('<b>Confirming Receipt:</b> '.
                   1184: 			  &user_crit_received($msgid).'<br>');
                   1185: 		&comprep($r,$msgid);
                   1186: 	    }
                   1187: 	}
                   1188: 	&discrit($r);
                   1189:     } elsif ($ENV{'form.critical'}) {
                   1190: 	&discrit($r);
                   1191:     } elsif ($ENV{'form.forward'}) {
                   1192: 	&compout($r,$ENV{'form.forward'});
                   1193:     } elsif ($ENV{'form.markread'}) {
                   1194:     } elsif ($ENV{'form.markdel'}) {
                   1195: 	&statuschange($ENV{'form.markdel'},'deleted');
                   1196: 	&disall($r);
                   1197:     } elsif ($ENV{'form.markeddel'}) {
                   1198: 	my $total=0;
                   1199: 	foreach (keys %ENV) {
                   1200: 	    if ($_=~/^form\.delmark_(.*)$/) {
                   1201: 		&statuschange(&Apache::lonnet::unescape($1),'deleted');
                   1202: 		$total++;
                   1203: 	    }
                   1204: 	}
                   1205: 	$r->print('Deleted '.$total.' message(s)<p>');
                   1206: 	&disall($r);
                   1207:     } elsif ($ENV{'form.markunread'}) {
                   1208: 	&statuschange($ENV{'form.markunread'},'new');
                   1209: 	&disall($r);
                   1210:     } elsif ($ENV{'form.compose'}) {
                   1211: 	&compout($r,'',$ENV{'form.compose'});
                   1212:     } elsif ($ENV{'form.recordftf'}) {
                   1213: 	&facetoface($r,$ENV{'form.recordftf'});
                   1214:     } elsif ($ENV{'form.sendmail'}) {
                   1215: 	my $sendstatus='';
                   1216: 	if ($ENV{'form.send'}) {
                   1217: 	    my %content=();
                   1218: 	    undef %content;
                   1219: 	    if ($ENV{'form.forwid'}) {
                   1220: 		my $msgid=$ENV{'form.forwid'};
                   1221: 		my %message=&Apache::lonnet::get('nohist_email',[$msgid]);
                   1222: 		%content=&unpackagemsg($message{$msgid},1);
                   1223: 		&statuschange($msgid,'forwarded');
                   1224: 		$ENV{'form.message'}.="\n\n-- Forwarded message --\n\n".
                   1225: 		    $content{'message'};
                   1226: 	    }
                   1227: 	    my %toaddr=();
                   1228: 	    undef %toaddr;
                   1229: 	    if ($ENV{'form.sendmode'} eq 'group') {
                   1230: 		foreach (keys %ENV) {
                   1231: 		    if ($_=~/^form\.send\_to\_\&\&\&[^\&]*\&\&\&\_(.+)$/) {
                   1232: 			$toaddr{$1}='';
                   1233: 		    }
                   1234: 		}
                   1235: 	    } elsif ($ENV{'form.sendmode'} eq 'upload') {
                   1236: 		foreach (split(/[\n\r\f]+/,$ENV{'form.upfile'})) {
                   1237: 		    my ($rec,$txt)=split(/\s*\:\s*/,$_);
                   1238: 		    if ($txt) {
                   1239: 			$rec=~s/\@/\:/;
                   1240: 			$toaddr{$rec}.=$txt."\n";
                   1241: 		    }
                   1242: 		}
                   1243: 	    } else {
                   1244: 		$toaddr{$ENV{'form.recuname'}.':'.$ENV{'form.recdomain'}}='';
                   1245: 	    }
                   1246: 	    if ($ENV{'form.additionalrec'}) {
                   1247: 		foreach (split(/\,/,$ENV{'form.additionalrec'})) {
                   1248: 		    my ($auname,$audom)=split(/\@/,$_);
                   1249: 		    $toaddr{$auname.':'.$audom}='';
                   1250: 		}
                   1251: 	    }
                   1252: 	    foreach (keys %toaddr) {
                   1253: 		my ($recuname,$recdomain)=split(/\:/,$_);
                   1254: 		my $msgtxt=&Apache::lonfeedback::clear_out_html($ENV{'form.message'});
                   1255: 		if ($toaddr{$_}) { $msgtxt.='<hr>'.$toaddr{$_}; }    
                   1256: 		if ((($ENV{'form.critmsg'}) || ($ENV{'form.sendbck'})) && 
                   1257: 		    (&Apache::lonnet::allowed('srm',$ENV{'request.course.id'}))) {
                   1258: 		    $r->print(&mt('Sending critical message').' ...');
                   1259: 		    $sendstatus.=' '.&user_crit_msg($recuname,$recdomain,
                   1260: 						    &Apache::lonfeedback::clear_out_html($ENV{'form.subject'}),
                   1261: 						    $msgtxt,
                   1262: 						    $ENV{'form.sendbck'});
                   1263: 		} else {
                   1264: 		    $r->print(&mt('Sending').' ...');
                   1265: 		    $sendstatus.=' '.&user_normal_msg($recuname,$recdomain,
                   1266: 						      &Apache::lonfeedback::clear_out_html($ENV{'form.subject'}),
                   1267: 						      $msgtxt,
                   1268: 						      $content{'citation'});
                   1269: 		}
                   1270: 		$r->print('<br />');
                   1271: 	    }
                   1272: 	}
                   1273: 	if ($sendstatus=~/^(\s*(?:ok|con_delayed)\s*)*$/) {
                   1274: 	    $r->print('<br /><font color="green">'.&mt('Completed.').'</font>');
                   1275: 	    if ($ENV{'form.displayedcrit'}) {
                   1276: 		&discrit($r);
                   1277: 	    } else {
                   1278: 		&disall($r);
                   1279: 	    }
                   1280: 	} else {
                   1281: 	    $r->print(
                   1282: 		      '<h2><font color="red">'.&mt('Could not deliver message').'</font></h2>'.
                   1283: 		      &mt('Please use the browser "Back" button and correct the recipient addresses')
                   1284: 		      );
                   1285: 	}
                   1286:     } else {
                   1287: 	&disall($r);
                   1288:     }
                   1289:     $r->print('</body></html>');
                   1290:     return OK;
1.5       www      1291: }
1.2       www      1292: # ================================================= Main program, reset counter
                   1293: 
1.27      www      1294: BEGIN {
1.2       www      1295:     $msgcount=0;
1.1       www      1296: }
1.58      bowersj2 1297: 
                   1298: =pod
                   1299: 
                   1300: =back
                   1301: 
1.59      bowersj2 1302: =cut
                   1303: 
                   1304: 1; 
1.1       www      1305: 
                   1306: __END__
                   1307: 
                   1308: 
                   1309: 
                   1310: 
                   1311: 
                   1312: 
                   1313: 

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