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

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

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