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

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

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