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

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

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