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

1.1       www         1: # The LearningOnline Network with CAPA
1.26      albertel    2: # Routines for messaging
                      3: #
1.98    ! sakharuk    4: # $Id: lonmsg.pm,v 1.97 2004/04/06 15:56:37 matthew 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)=@_;
1.98    ! sakharuk  914:     my $target=$ENV{'form.grade_target'};
1.45      www       915:     unless ($ENV{'request.course.id'}) { return; }
                    916:     unless (&Apache::lonnet::allowed('srm',$ENV{'request.course.id'})) {
                    917: 	return;
                    918:     }
                    919:     my %records=&Apache::lonnet::dump('nohist_email',
                    920: 			 $ENV{'course.'.$ENV{'request.course.id'}.'.domain'},
                    921: 			 $ENV{'course.'.$ENV{'request.course.id'}.'.num'},
                    922:                          '%255b'.$user.'%253a'.$domain.'%255d');
                    923:     my $result='';
                    924:     foreach (sort keys %records) {
                    925:         my %content=&unpackagemsg($records{$_});
                    926:         next if ($content{'senderdomain'} eq '');
                    927:         $content{'message'}=~s/\n/\<br\>/g;
                    928:         if ($content{'subject'}=~/^Record/) {
1.69      www       929: 	    $result.='<h3>'.&mt('Record').'</h3>';
1.45      www       930:         } else {
1.69      www       931:             $result.='<h3>'.&mt('Sent Message').'</h3>';
1.45      www       932:             %content=&unpackagemsg($content{'message'});
                    933:             $content{'message'}=
1.92      www       934:                 '<b>'.&mt('Subject').': '.$content{'subject'}.'</b><br />'.
1.45      www       935: 		$content{'message'};
                    936:         }
1.69      www       937:         $result.=&mt('By').': <b>'.
1.45      www       938: &Apache::loncommon::aboutmewrapper(
                    939:  &Apache::loncommon::plainname($content{'sendername'},$content{'senderdomain'}),$content{'sendername'},$content{'senderdomain'}).'</b> ('.
                    940: $content{'sendername'}.'@'.
                    941:             $content{'senderdomain'}.') '.$content{'time'}.
1.92      www       942:             '<br /><blockquote>'.
1.45      www       943:               &Apache::lontexconvert::msgtexconverted($content{'message'}).
                    944: 	      '</blockquote>';
                    945:      }
                    946:     # Check to see if there were any messages.
                    947:     if ($result eq '') {
1.98    ! sakharuk  948: 	if ($target ne 'tex') { 
        !           949: 	    $r->print("<p><b>".&mt("No notes, face-to-face discussion records, or critical messages in this course.")."</b></p>");
        !           950: 	} else {
        !           951: 	    $r->print('\textbf{'.&mt("No notes, face-to-face discussion records, or critical messages in this course.").'}\\\\');
        !           952: 	}
1.45      www       953:     } else {
                    954:        $r->print($result);
                    955:     }
                    956: }
                    957: 
1.44      www       958: # ---------------------------------------------------------------- Face to face
                    959: 
                    960: sub facetoface {
                    961:     my ($r,$stage)=@_;
                    962:     unless (&Apache::lonnet::allowed('srm',$ENV{'request.course.id'})) {
                    963: 	return;
                    964:     }
1.89      www       965:     &printheader($r,
                    966: 		 '/adm/email?recordftf=query',
                    967: 		 "User Notes, Face-to-Face, Critical Messages");
1.46      www       968: # from query string
1.88      www       969: 
1.46      www       970:     if ($ENV{'form.recname'}) { $ENV{'form.recuname'}=$ENV{'form.recname'}; }
                    971:     if ($ENV{'form.recdom'}) { $ENV{'form.recdomain'}=$ENV{'form.recdom'}; }
                    972: 
1.44      www       973:     my $defdom=$ENV{'user.domain'};
1.46      www       974: # already filled in
1.44      www       975:     if ($ENV{'form.recdomain'}) { $defdom=$ENV{'form.recdomain'}; }
1.46      www       976: # generate output
1.44      www       977:     my $domform = &Apache::loncommon::select_dom_form($defdom,'recdomain');
1.46      www       978:     my $stdbrws = &Apache::loncommon::selectstudent_link
                    979: 	('stdselect','recuname','recdomain');
1.88      www       980:     my %lt=&Apache::lonlocal::texthash('user' => 'Username',
                    981: 				       'dom' => 'Domain',
                    982: 				       'head' => 'User Notes, Records of Face-To-Face Discussions, and Critical Messages in Course',
                    983: 				       'subm' => 'Retrieve discussion and message records',
                    984: 				       'newr' => 'New Record (record is visible to course faculty and staff)',
                    985: 				       'post' => 'Post this Record');
1.44      www       986:     $r->print(<<"ENDTREC");
1.88      www       987: <h3>$lt{'head'}</h3>
1.46      www       988: <form method="post" action="/adm/email" name="stdselect">
1.44      www       989: <input type="hidden" name="recordftf" value="retrieve" />
                    990: <table>
1.88      www       991: <tr><td>$lt{'user'}:</td><td><input type="text" size="12" name="recuname" value="$ENV{'form.recuname'}" /></td>
1.44      www       992: <td rowspan="2">
1.46      www       993: $stdbrws
1.88      www       994: <input type="submit" value="$lt{'subm'}" /></td>
1.44      www       995: </tr>
1.88      www       996: <tr><td>$lt{'dom'}:</td>
1.44      www       997: <td>$domform</td></tr>
                    998: </table>
                    999: </form>
                   1000: ENDTREC
                   1001:     if (($stage ne 'query') &&
                   1002:         ($ENV{'form.recdomain'}) && ($ENV{'form.recuname'})) {
                   1003:         chomp($ENV{'form.newrecord'});
                   1004:         if ($ENV{'form.newrecord'}) {
1.45      www      1005:            &user_normal_msg_raw(
                   1006:             $ENV{'course.'.$ENV{'request.course.id'}.'.num'},
                   1007:             $ENV{'course.'.$ENV{'request.course.id'}.'.domain'},
1.88      www      1008:             &mt('Record').
                   1009: 	     ' ['.$ENV{'form.recuname'}.':'.$ENV{'form.recdomain'}.']',
1.45      www      1010: 	    $ENV{'form.newrecord'});
1.44      www      1011:         }
1.46      www      1012:         $r->print('<h3>'.&Apache::loncommon::plainname($ENV{'form.recuname'},
                   1013: 				     $ENV{'form.recdomain'}).'</h3>');
1.45      www      1014:         &disfacetoface($r,$ENV{'form.recuname'},$ENV{'form.recdomain'});
1.44      www      1015: 	$r->print(<<ENDRHEAD);
                   1016: <form method="post" action="/adm/email">
                   1017: <input name="recdomain" value="$ENV{'form.recdomain'}" type="hidden" />
                   1018: <input name="recuname" value="$ENV{'form.recuname'}" type="hidden" />
                   1019: ENDRHEAD
                   1020:         $r->print(<<ENDBFORM);
1.88      www      1021: <hr />$lt{'newr'}<br />
1.44      www      1022: <textarea name="newrecord" cols="80" rows="10" wrap="hard"></textarea>
1.45      www      1023: <br />
                   1024: <input type="hidden" name="recordftf" value="post" />
1.88      www      1025: <input type="submit" value="$lt{'post'}" />
1.44      www      1026: </form>
                   1027: ENDBFORM
                   1028:     }
                   1029: }
1.91      www      1030: 
1.90      www      1031: # ----------------------------------------------------------- Display a message
                   1032: 
                   1033: sub displaymessage {
                   1034:     my ($r,$msgid)=@_;
                   1035:     &statuschange($msgid,'read');
                   1036:     my %message=&Apache::lonnet::get('nohist_email',[$msgid]);
                   1037:     my %content=&unpackagemsg($message{$msgid});
                   1038: # info to generate "next" and "previous" buttons
                   1039:     my @messages=&sortedmessages();
                   1040:     my $counter=0;
                   1041:     $r->print('<pre>');
                   1042:     my $escmsgid=&Apache::lonnet::escape($msgid);
                   1043:     foreach (@messages) {
                   1044: 	if ($_->[5] eq $escmsgid){
                   1045: 	    last;
                   1046: 	}
                   1047: 	$counter++;
                   1048:     }
                   1049:     $r->print('</pre>');
                   1050:     my $number_of_messages = scalar(@messages); #subtract 1 for last index
                   1051: # start output
1.92      www      1052:     &printheader($r,'/adm/email?display='.&Apache::lonnet::escape($msgid),'Display a Message','',$content{'baseurl'});
1.90      www      1053:     my %courseinfo=&Apache::lonnet::coursedescription($content{'courseid'});
                   1054: # Functions
                   1055:     $r->print('<table border="2" width="100%"><tr bgcolor="#FFFFAA"><td>'.&mt('Functions').':</td>'.
                   1056: 	      '<td><a href="/adm/email?replyto='.&Apache::lonnet::escape($msgid).$sqs.
                   1057: 	      '"><b>'.&mt('Reply').'</b></a></td>'.
                   1058: 	      '<td><a href="/adm/email?forward='.&Apache::lonnet::escape($msgid).$sqs.
                   1059: 	      '"><b>'.&mt('Forward').'</b></a></td>'.
                   1060: 	      '<td><a href="/adm/email?markunread='.&Apache::lonnet::escape($msgid).$sqs.
                   1061: 	      '"><b>'.&mt('Mark Unread').'</b></a></td>'.
                   1062: 	      '<td><a href="/adm/email?markdel='.&Apache::lonnet::escape($msgid).$sqs.
                   1063: 	      '"><b>Delete</b></a></td>'.
                   1064: 	      '<td><a href="/adm/email?sortedby='.$ENV{'form.sortedby'}.
                   1065: 	      '"><b>'.&mt('Display all Messages').'</b></a></td>');
                   1066:     if ($counter > 0){
                   1067: 	$r->print('<td><a href="/adm/email?display='.$messages[$counter-1]->[5].$sqs.
                   1068: 		  '"><b>'.&mt('Previous').'</b></a></td>');
                   1069:     }
                   1070:     if ($counter < $number_of_messages - 1){
                   1071: 	$r->print('<td><a href="/adm/email?display='.$messages[$counter+1]->[5].$sqs.
                   1072: 		  '"><b>'.&mt('Next').'</b></a></td>');
                   1073:     }
                   1074:     $r->print('</tr></table>');
                   1075:     $r->print('<br /><b>'.&mt('Subject').':</b> '.$content{'subject'}.
                   1076: 	      '<br /><b>'.&mt('From').':</b> '.
                   1077: 	      &Apache::loncommon::aboutmewrapper(
                   1078: 						 &Apache::loncommon::plainname($content{'sendername'},$content{'senderdomain'}),
                   1079: 						 $content{'sendername'},$content{'senderdomain'}).' ('.
                   1080: 	      $content{'sendername'}.' at '.
                   1081: 	      $content{'senderdomain'}.') '.
                   1082: 	      ($content{'courseid'}?'<br /><b>'.&mt('Course').':</b> '.$courseinfo{'description'}.
                   1083: 	       ($content{'coursesec'}?' ('.&mt('Group/Section').': '.$content{'coursesec'}.')':''):'').
                   1084: 	      '<br /><b>'.&mt('Time').':</b> '.$content{'time'}.
                   1085: 	      '<p><pre>'.
                   1086: 	      &Apache::lontexconvert::msgtexconverted($content{'message'},1).
                   1087: 	      '</pre><hr />'.$content{'citation'}.'</p>');
                   1088:     return;   
                   1089: }
1.44      www      1090: 
1.88      www      1091: # ================================================================== The Header
                   1092: 
                   1093: sub header {
1.90      www      1094:     my ($r,$title,$baseurl)=@_;
1.88      www      1095:     $r->print('<html><head><title>Communication and Messages</title>');
                   1096:     if ($baseurl) {
                   1097: 	$r->print("<base href=\"http://$ENV{'SERVER_NAME'}/$baseurl\" />");
                   1098:     }
                   1099:     $r->print(&Apache::loncommon::studentbrowser_javascript().'</head>'.
                   1100: 	      &Apache::loncommon::bodytag('Communication and Messages'));
                   1101:         $r->print(&Apache::lonhtmlcommon::breadcrumbs
1.90      www      1102:                   (undef,($title?$title:'Communication and Messages')));
1.88      www      1103: 
                   1104: }
                   1105: 
1.90      www      1106: # ---------------------------------------------------------------- Print header
                   1107: 
                   1108: sub printheader {
                   1109:     my ($r,$url,$desc,$title,$baseurl)=@_;
                   1110:     &Apache::lonhtmlcommon::add_breadcrumb
                   1111: 	({href=>$url,
                   1112: 	  text=>$desc});
                   1113:     &header($r,$title,$baseurl);
                   1114: }
                   1115: 
                   1116: 
1.13      www      1117: # ===================================================================== Handler
                   1118: 
1.5       www      1119: sub handler {
                   1120:     my $r=shift;
                   1121: 
                   1122: # ----------------------------------------------------------- Set document type
1.87      www      1123:     
                   1124:     &Apache::loncommon::content_type($r,'text/html');
                   1125:     $r->send_http_header;
                   1126:     
                   1127:     return OK if $r->header_only;
                   1128:     
1.6       www      1129: # --------------------------- Get query string for limited number of parameters
1.32      matthew  1130:     &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'},
                   1131:         ['display','replyto','forward','markread','markdel','markunread',
1.44      www      1132:          'sendreply','compose','sendmail','critical','recname','recdom',
1.62      www      1133:          'recordftf','sortedby']);
1.65      www      1134:     $sqs='&sortedby='.$ENV{'form.sortedby'};
1.40      www      1135: # ------------------------------------------------------ They checked for email
1.87      www      1136:     &Apache::lonnet::put('email_status',{'recnewemail'=>0});
1.88      www      1137: 
                   1138: # ----------------------------------------------------------------- Breadcrumbs
                   1139: 
                   1140:     &Apache::lonhtmlcommon::clear_breadcrumbs();
                   1141:     &Apache::lonhtmlcommon::add_breadcrumb
                   1142:         ({href=>"/adm/communicate",
                   1143:           text=>"Communication/Messages",
                   1144:           faq=>12,bug=>'Communication Tools',});
                   1145: 
1.5       www      1146: # --------------------------------------------------------------- Render Output
1.88      www      1147: 
1.87      www      1148:     if ($ENV{'form.display'}) {
1.90      www      1149: 	&displaymessage($r,$ENV{'form.display'});
1.87      www      1150:     } elsif ($ENV{'form.replyto'}) {
1.92      www      1151: 	&compout($r,'',$ENV{'form.replyto'});
1.87      www      1152:     } elsif ($ENV{'form.confirm'}) {
1.92      www      1153: 	&printheader($r,'','Confirmed Receipt');
1.87      www      1154: 	foreach (keys %ENV) {
                   1155: 	    if ($_=~/^form\.rec\_(.*)$/) {
1.92      www      1156: 		$r->print('<b>'.&mt('Confirming Receipt').':</b> '.
1.87      www      1157: 			  &user_crit_received($1).'<br>');
                   1158: 	    }
                   1159: 	    if ($_=~/^form\.reprec\_(.*)$/) {
                   1160: 		my $msgid=$1;
1.92      www      1161: 		$r->print('<b>'.&mt('Confirming Receipt').':</b> '.
1.87      www      1162: 			  &user_crit_received($msgid).'<br>');
1.94      www      1163: 		&compout($r,'','','',$msgid);
1.87      www      1164: 	    }
                   1165: 	}
                   1166: 	&discrit($r);
                   1167:     } elsif ($ENV{'form.critical'}) {
1.92      www      1168: 	&printheader($r,'','Displaying Critical Messages');
1.87      www      1169: 	&discrit($r);
                   1170:     } elsif ($ENV{'form.forward'}) {
                   1171: 	&compout($r,$ENV{'form.forward'});
                   1172:     } elsif ($ENV{'form.markdel'}) {
1.92      www      1173: 	&printheader($r,'','Deleted Message');
1.87      www      1174: 	&statuschange($ENV{'form.markdel'},'deleted');
                   1175: 	&disall($r);
                   1176:     } elsif ($ENV{'form.markeddel'}) {
                   1177: 	my $total=0;
                   1178: 	foreach (keys %ENV) {
                   1179: 	    if ($_=~/^form\.delmark_(.*)$/) {
                   1180: 		&statuschange(&Apache::lonnet::unescape($1),'deleted');
                   1181: 		$total++;
                   1182: 	    }
                   1183: 	}
1.92      www      1184: 	&printheader($r,'','Deleted Messages');
1.87      www      1185: 	$r->print('Deleted '.$total.' message(s)<p>');
                   1186: 	&disall($r);
                   1187:     } elsif ($ENV{'form.markunread'}) {
1.92      www      1188: 	&printheader($r,'','Marked Message as Unread');
1.87      www      1189: 	&statuschange($ENV{'form.markunread'},'new');
                   1190: 	&disall($r);
                   1191:     } elsif ($ENV{'form.compose'}) {
1.92      www      1192: 	&compout($r,'','',$ENV{'form.compose'});
1.87      www      1193:     } elsif ($ENV{'form.recordftf'}) {
                   1194: 	&facetoface($r,$ENV{'form.recordftf'});
                   1195:     } elsif ($ENV{'form.sendmail'}) {
                   1196: 	my $sendstatus='';
                   1197: 	if ($ENV{'form.send'}) {
1.92      www      1198: 	    &printheader($r,'','Messages being sent.');
                   1199: 	    $r->rflush();
1.87      www      1200: 	    my %content=();
                   1201: 	    undef %content;
                   1202: 	    if ($ENV{'form.forwid'}) {
                   1203: 		my $msgid=$ENV{'form.forwid'};
                   1204: 		my %message=&Apache::lonnet::get('nohist_email',[$msgid]);
                   1205: 		%content=&unpackagemsg($message{$msgid},1);
                   1206: 		&statuschange($msgid,'forwarded');
                   1207: 		$ENV{'form.message'}.="\n\n-- Forwarded message --\n\n".
                   1208: 		    $content{'message'};
                   1209: 	    }
                   1210: 	    my %toaddr=();
                   1211: 	    undef %toaddr;
                   1212: 	    if ($ENV{'form.sendmode'} eq 'group') {
                   1213: 		foreach (keys %ENV) {
                   1214: 		    if ($_=~/^form\.send\_to\_\&\&\&[^\&]*\&\&\&\_(.+)$/) {
                   1215: 			$toaddr{$1}='';
                   1216: 		    }
                   1217: 		}
                   1218: 	    } elsif ($ENV{'form.sendmode'} eq 'upload') {
                   1219: 		foreach (split(/[\n\r\f]+/,$ENV{'form.upfile'})) {
                   1220: 		    my ($rec,$txt)=split(/\s*\:\s*/,$_);
                   1221: 		    if ($txt) {
                   1222: 			$rec=~s/\@/\:/;
                   1223: 			$toaddr{$rec}.=$txt."\n";
                   1224: 		    }
                   1225: 		}
                   1226: 	    } else {
                   1227: 		$toaddr{$ENV{'form.recuname'}.':'.$ENV{'form.recdomain'}}='';
                   1228: 	    }
                   1229: 	    if ($ENV{'form.additionalrec'}) {
                   1230: 		foreach (split(/\,/,$ENV{'form.additionalrec'})) {
                   1231: 		    my ($auname,$audom)=split(/\@/,$_);
                   1232: 		    $toaddr{$auname.':'.$audom}='';
                   1233: 		}
                   1234: 	    }
1.92      www      1235: 
1.87      www      1236: 	    foreach (keys %toaddr) {
                   1237: 		my ($recuname,$recdomain)=split(/\:/,$_);
                   1238: 		my $msgtxt=&Apache::lonfeedback::clear_out_html($ENV{'form.message'});
1.92      www      1239: 		if ($toaddr{$_}) { $msgtxt.='<hr />'.$toaddr{$_}; }
                   1240: 		my $thismsg;    
1.87      www      1241: 		if ((($ENV{'form.critmsg'}) || ($ENV{'form.sendbck'})) && 
                   1242: 		    (&Apache::lonnet::allowed('srm',$ENV{'request.course.id'}))) {
1.92      www      1243: 		    $r->print(&mt('Sending critical message').' '.$recuname.'@'.$recdomain.': ');
                   1244: 		    $thismsg=&user_crit_msg($recuname,$recdomain,
1.87      www      1245: 						    &Apache::lonfeedback::clear_out_html($ENV{'form.subject'}),
                   1246: 						    $msgtxt,
                   1247: 						    $ENV{'form.sendbck'});
                   1248: 		} else {
1.92      www      1249: 		    $r->print(&mt('Sending').' '.$recuname.'@'.$recdomain.': ');
                   1250: 		    $thismsg=&user_normal_msg($recuname,$recdomain,
1.87      www      1251: 						      &Apache::lonfeedback::clear_out_html($ENV{'form.subject'}),
                   1252: 						      $msgtxt,
                   1253: 						      $content{'citation'});
                   1254: 		}
1.92      www      1255: 		$r->print($thismsg.'<br />');
                   1256: 		$sendstatus.=' '.$thismsg;
1.87      www      1257: 	    }
1.95      www      1258: 	} else {
                   1259: 	    &printheader($r,'','No messages sent.'); 
1.87      www      1260: 	}
                   1261: 	if ($sendstatus=~/^(\s*(?:ok|con_delayed)\s*)*$/) {
                   1262: 	    $r->print('<br /><font color="green">'.&mt('Completed.').'</font>');
                   1263: 	    if ($ENV{'form.displayedcrit'}) {
                   1264: 		&discrit($r);
                   1265: 	    } else {
1.95      www      1266: 		&Apache::loncommunicate::menu($r);
1.87      www      1267: 	    }
                   1268: 	} else {
                   1269: 	    $r->print(
                   1270: 		      '<h2><font color="red">'.&mt('Could not deliver message').'</font></h2>'.
                   1271: 		      &mt('Please use the browser "Back" button and correct the recipient addresses')
                   1272: 		      );
                   1273: 	}
                   1274:     } else {
1.92      www      1275: 	&printheader($r,'','Display All Messages');
1.87      www      1276: 	&disall($r);
                   1277:     }
                   1278:     $r->print('</body></html>');
                   1279:     return OK;
1.5       www      1280: }
1.2       www      1281: # ================================================= Main program, reset counter
                   1282: 
1.27      www      1283: BEGIN {
1.2       www      1284:     $msgcount=0;
1.1       www      1285: }
1.58      bowersj2 1286: 
                   1287: =pod
                   1288: 
                   1289: =back
                   1290: 
1.59      bowersj2 1291: =cut
                   1292: 
                   1293: 1; 
1.1       www      1294: 
                   1295: __END__
                   1296: 
                   1297: 
                   1298: 
                   1299: 
                   1300: 
                   1301: 
                   1302: 

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