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

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

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