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

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

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