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

1.1       www         1: # The LearningOnline Network with CAPA
1.26      albertel    2: # Routines for messaging
                      3: #
1.126   ! www         4: # $Id: lonmsg.pm,v 1.125 2004/12/15 02:35:17 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: #
1.75      www        28: 
                     29: 
1.1       www        30: package Apache::lonmsg;
                     31: 
1.58      bowersj2   32: =pod
                     33: 
                     34: =head1 NAME
                     35: 
                     36: Apache::lonmsg: supports internal messaging
                     37: 
                     38: =head1 SYNOPSIS
                     39: 
                     40: lonmsg provides routines for sending messages, receiving messages, and
                     41: a handler to allow users to read, send, and delete messages.
                     42: 
                     43: =head1 OVERVIEW
                     44: 
                     45: =head2 Messaging Overview
                     46: 
                     47: X<messages>LON-CAPA provides an internal messaging system similar to
                     48: email, but customized for LON-CAPA's usage. LON-CAPA implements its
                     49: own messaging system, rather then building on top of email, because of
                     50: the features LON-CAPA messages can offer that conventional e-mail can
                     51: not:
                     52: 
                     53: =over 4
                     54: 
                     55: =item * B<Critical messages>: A message the recipient B<must>
                     56: acknowlegde receipt of before they are allowed to continue using the
                     57: system, preventing a user from claiming they never got a message
                     58: 
                     59: =item * B<Receipts>: LON-CAPA can reliably send reciepts informing the
                     60: sender that it has been read; again, useful for preventing students
                     61: from claiming they did not see a message. (While conventional e-mail
                     62: has some reciept support, it's sporadic, e-mail client-specific, and
                     63: generally the receiver can opt to not send one, making it useless in
                     64: this case.)
                     65: 
                     66: =item * B<Context>: LON-CAPA knows about the sender, such as where
                     67: they are in a course. When a student mails an instructor asking for
                     68: help on the problem, the instructor receives not just the student's
                     69: question, but all submissions the student has made up to that point,
                     70: the user's rendering of the problem, and the complete view the student
                     71: saw of the resource, including discussion up to that point. Finally,
                     72: the instructor is reading all of this inside of LON-CAPA, not their
                     73: email program, so they have full access to LON-CAPA's grading
                     74: interface, or other features they may wish to use in response to the
                     75: student's query.
                     76: 
1.101     raeburn    77: =item * B<Blocking>: LON-CAPA can block display of e-mails that are 
                     78: sent to a student during an online exam. A course coordinator or
                     79: instructor can set an open and close date/time for scheduled online
                     80: exams in a course. If a user uses the LON-CAPA internal messaging 
                     81: system to display e-mails during the scheduled blocking event,  
                     82: display of all e-mail sent during the blocking period will be 
                     83: suppressed, and a message of explanation, including details of the 
                     84: currently active blocking periods will be displayed instead. A user 
                     85: who has a course coordinator or instructor role in a course will be
                     86: unaffected by any blocking periods for the course, unless the user
                     87: also has a student role in the course, AND has selected the student role.
                     88: 
1.58      bowersj2   89: =back
                     90: 
                     91: Users can ask LON-CAPA to forward messages to conventional e-mail
                     92: addresses on their B<PREF> screen, but generally, LON-CAPA messages
                     93: are much more useful then traditional email can be made to be, even
                     94: with HTML support.
                     95: 
                     96: Right now, this document will cover just how to send a message, since
                     97: it is likely you will not need to programmatically read messages,
                     98: since lonmsg already implements that functionality.
                     99: 
                    100: =head1 FUNCTIONS
                    101: 
                    102: =over 4
                    103: 
                    104: =cut
                    105: 
1.1       www       106: use strict;
                    107: use Apache::lonnet();
1.2       www       108: use vars qw($msgcount);
1.47      albertel  109: use HTML::TokeParser();
1.5       www       110: use Apache::Constants qw(:common);
1.47      albertel  111: use Apache::loncommon();
                    112: use Apache::lontexconvert();
                    113: use HTML::Entities();
1.53      www       114: use Mail::Send;
1.67      www       115: use Apache::lonlocal;
1.95      www       116: use Apache::loncommunicate;
1.1       www       117: 
1.65      www       118: # Querystring component with sorting type
                    119: my $sqs;
1.108     www       120: my $startdis;
                    121: my $interdis;
1.65      www       122: 
1.1       www       123: # ===================================================================== Package
                    124: 
1.3       www       125: sub packagemsg {
1.108     www       126:     my ($subject,$message,$citation,$baseurl,$attachmenturl,
                    127: 	$recuser,$recdomain)=@_;
1.96      albertel  128:     $message =&HTML::Entities::encode($message,'<>&"');
                    129:     $citation=&HTML::Entities::encode($citation,'<>&"');
                    130:     $subject =&HTML::Entities::encode($subject,'<>&"');
1.49      albertel  131:     #remove machine specification
                    132:     $baseurl =~ s|^http://[^/]+/|/|;
1.96      albertel  133:     $baseurl =&HTML::Entities::encode($baseurl,'<>&"');
1.51      www       134:     #remove machine specification
                    135:     $attachmenturl =~ s|^http://[^/]+/|/|;
1.96      albertel  136:     $attachmenturl =&HTML::Entities::encode($attachmenturl,'<>&"');
1.51      www       137: 
1.2       www       138:     my $now=time;
                    139:     $msgcount++;
1.6       www       140:     my $partsubj=$subject;
                    141:     $partsubj=&Apache::lonnet::escape($partsubj);
                    142:     my $msgid=&Apache::lonnet::escape(
                    143:            $now.':'.$partsubj.':'.$ENV{'user.name'}.':'.
                    144:            $ENV{'user.domain'}.':'.$msgcount.':'.$$);
1.49      albertel  145:     my $result='<sendername>'.$ENV{'user.name'}.'</sendername>'.
1.1       www       146:            '<senderdomain>'.$ENV{'user.domain'}.'</senderdomain>'.
                    147:            '<subject>'.$subject.'</subject>'.
1.67      www       148: 	   '<time>'.&Apache::lonlocal::locallocaltime($now).'</time>'.
1.1       www       149: 	   '<servername>'.$ENV{'SERVER_NAME'}.'</servername>'.
                    150:            '<host>'.$ENV{'HTTP_HOST'}.'</host>'.
                    151: 	   '<client>'.$ENV{'REMOTE_ADDR'}.'</client>'.
                    152: 	   '<browsertype>'.$ENV{'browser.type'}.'</browsertype>'.
                    153: 	   '<browseros>'.$ENV{'browser.os'}.'</browseros>'.
                    154: 	   '<browserversion>'.$ENV{'browser.version'}.'</browserversion>'.
                    155:            '<browsermathml>'.$ENV{'browser.mathml'}.'</browsermathml>'.
                    156: 	   '<browserraw>'.$ENV{'HTTP_USER_AGENT'}.'</browserraw>'.
                    157: 	   '<courseid>'.$ENV{'request.course.id'}.'</courseid>'.
1.85      www       158: 	   '<coursesec>'.$ENV{'request.course.sec'}.'</coursesec>'.
1.1       www       159: 	   '<role>'.$ENV{'request.role'}.'</role>'.
                    160: 	   '<resource>'.$ENV{'request.filename'}.'</resource>'.
1.2       www       161:            '<msgid>'.$msgid.'</msgid>'.
1.108     www       162: 	   '<recuser>'.$recuser.'</recuser>'.
                    163: 	   '<recdomain>'.$recdomain.'</recdomain>'.
1.49      albertel  164: 	   '<message>'.$message.'</message>';
                    165:     if (defined($citation)) {
                    166: 	$result.='<citation>'.$citation.'</citation>';
                    167:     }
                    168:     if (defined($baseurl)) {
                    169: 	$result.= '<baseurl>'.$baseurl.'</baseurl>';
                    170:     }
1.51      www       171:     if (defined($attachmenturl)) {
1.52      www       172: 	$result.= '<attachmenturl>'.$attachmenturl.'</attachmenturl>';
1.51      www       173:     }
1.49      albertel  174:     return $msgid,$result;
1.1       www       175: }
                    176: 
1.2       www       177: # ================================================== Unpack message into a hash
                    178: 
1.3       www       179: sub unpackagemsg {
1.52      www       180:     my ($message,$notoken)=@_;
1.2       www       181:     my %content=();
                    182:     my $parser=HTML::TokeParser->new(\$message);
                    183:     my $token;
                    184:     while ($token=$parser->get_token) {
                    185:        if ($token->[0] eq 'S') {
                    186: 	   my $entry=$token->[1];
                    187:            my $value=$parser->get_text('/'.$entry);
                    188:            $content{$entry}=$value;
                    189:        }
                    190:     }
1.52      www       191:     if ($content{'attachmenturl'}) {
1.100     albertel  192:        my ($fname)=($content{'attachmenturl'}=~m|/([^/]+)$|);
1.52      www       193:        if ($notoken) {
1.100     albertel  194: 	   $content{'message'}.='<p>'.&mt('Attachment').': <tt>'.$fname.'</tt>';
1.52      www       195:        } else {
1.99      albertel  196: 	   &Apache::lonnet::allowuploaded('/adm/msg',
                    197: 					  $content{'attachmenturl'});
                    198: 	   $content{'message'}.='<p>'.&mt('Attachment').
                    199: 	       ': <a href="'.$content{'attachmenturl'}.'"><tt>'.
1.100     albertel  200: 	       $fname.'</tt></a>';
1.52      www       201:        }
                    202:     }
1.2       www       203:     return %content;
                    204: }
                    205: 
1.6       www       206: # ======================================================= Get info out of msgid
                    207: 
                    208: sub unpackmsgid {
1.106     www       209:     my ($msgid,$folder)=@_;
                    210:     $msgid=&Apache::lonnet::unescape($msgid);
                    211:     my $suffix=&foldersuffix($folder);
1.6       www       212:     my ($sendtime,$shortsubj,$fromname,$fromdomain)=split(/\:/,
1.7       www       213:                           &Apache::lonnet::unescape($msgid));
1.106     www       214:     my %status=&Apache::lonnet::get('email_status'.$suffix,[$msgid]);
1.6       www       215:     if ($status{$msgid}=~/^error\:/) { $status{$msgid}=''; }
                    216:     unless ($status{$msgid}) { $status{$msgid}='new'; }
                    217:     return ($sendtime,$shortsubj,$fromname,$fromdomain,$status{$msgid});
                    218: } 
                    219: 
1.53      www       220: 
                    221: sub sendemail {
                    222:     my ($to,$subject,$body)=@_;
                    223:     $body=
1.67      www       224:     "*** ".&mt('This is an automatic message generated by the LON-CAPA system.')."\n".
                    225:     "*** ".&mt('Please do not reply to this address.')."\n\n".$body;
1.53      www       226:     my $msg = new Mail::Send;
                    227:     $msg->to($to);
                    228:     $msg->subject('[LON-CAPA] '.$subject);
1.103     albertel  229:     my %oldENV=%ENV;
                    230:     undef(%ENV);
1.97      matthew   231:     if (my $fh = $msg->open()) {
1.68      www       232: 	print $fh $body;
                    233: 	$fh->close;
                    234:     }
1.103     albertel  235:     %ENV=%oldENV;
                    236:     undef(%oldENV);
1.53      www       237: }
                    238: 
                    239: # ==================================================== Send notification emails
                    240: 
                    241: sub sendnotification {
                    242:     my ($to,$touname,$toudom,$subj,$crit)=@_;
                    243:     my $sender=$ENV{'environment.firstname'}.' '.$ENV{'environment.lastname'};
                    244:     my $critical=($crit?' critical':'');
                    245:     my $url='http://'.
                    246:       $Apache::lonnet::hostname{&Apache::lonnet::homeserver($touname,$toudom)}.
1.54      www       247:       '/adm/email?username='.$touname.'&domain='.$toudom;
1.53      www       248:     my $body=(<<ENDMSG);
                    249: You received a$critical message from $sender in LON-CAPA. The subject is
                    250: 
                    251:  $subj
                    252: 
                    253: Use
                    254: 
                    255:  $url
                    256: 
                    257: to access this message.
                    258: ENDMSG
                    259:     &sendemail($to,'New'.$critical.' message from '.$sender,$body);
                    260: }
1.40      www       261: # ============================================================= Check for email
                    262: 
                    263: sub newmail {
                    264:     if ((time-$ENV{'user.mailcheck.time'})>300) {
                    265:         my %what=&Apache::lonnet::get('email_status',['recnewemail']);
                    266:         &Apache::lonnet::appenv('user.mailcheck.time'=>time);
                    267:         if ($what{'recnewemail'}>0) { return 1; }
                    268:     }
                    269:     return 0;
                    270: }
                    271: 
1.1       www       272: # =============================== Automated message to the author of a resource
                    273: 
1.58      bowersj2  274: =pod
                    275: 
                    276: =item * B<author_res_msg($filename, $message)>: Sends message $message to the owner
                    277:     of the resource with the URI $filename.
                    278: 
                    279: =cut
                    280: 
1.1       www       281: sub author_res_msg {
                    282:     my ($filename,$message)=@_;
1.2       www       283:     unless ($message) { return 'empty'; }
1.1       www       284:     $filename=&Apache::lonnet::declutter($filename);
1.72      www       285:     my ($domain,$author,@dummy)=split(/\//,$filename);
1.1       www       286:     my $homeserver=&Apache::lonnet::homeserver($author,$domain);
                    287:     if ($homeserver ne 'no_host') {
                    288:        my $id=unpack("%32C*",$message);
1.2       www       289:        my $msgid;
1.72      www       290:        ($msgid,$message)=&packagemsg($filename,$message);
1.3       www       291:        return &Apache::lonnet::reply('put:'.$domain.':'.$author.
1.72      www       292:          ':nohist_res_msgs:'.
                    293:           &Apache::lonnet::escape($filename.'_'.$id).'='.
                    294:           &Apache::lonnet::escape($message),$homeserver);
1.1       www       295:     }
1.2       www       296:     return 'no_host';
1.73      www       297: }
                    298: 
                    299: # =========================================== Retrieve author resource messages
                    300: 
                    301: sub retrieve_author_res_msg {
1.75      www       302:     my $url=shift;
1.73      www       303:     $url=&Apache::lonnet::declutter($url);
1.80      www       304:     my ($domain,$author)=($url=~/^(\w+)\/(\w+)\//);
1.76      www       305:     my %errormsgs=&Apache::lonnet::dump('nohist_res_msgs',$domain,$author);
1.73      www       306:     my $msgs='';
                    307:     foreach (keys %errormsgs) {
1.80      www       308: 	if ($_=~/^\Q$url\E\_\d+$/) {
1.73      www       309: 	    my %content=&unpackagemsg($errormsgs{$_});
1.74      www       310: 	    $msgs.='<p><img src="/adm/lonMisc/bomb.gif" /><b>'.
                    311: 		$content{'time'}.'</b>: '.$content{'message'}.
                    312: 		'<br /></p>';
1.73      www       313: 	}
                    314:     } 
                    315:     return $msgs;     
                    316: }
                    317: 
                    318: 
                    319: # =============================== Delete all author messages related to one URL
                    320: 
                    321: sub del_url_author_res_msg {
1.75      www       322:     my $url=shift;
1.73      www       323:     $url=&Apache::lonnet::declutter($url);
1.77      www       324:     my ($domain,$author)=($url=~/^(\w+)\/(\w+)\//);
                    325:     my @delmsgs=();
                    326:     foreach (&Apache::lonnet::getkeys('nohist_res_msgs',$domain,$author)) {
                    327: 	if ($_=~/^\Q$url\E\_\d+$/) {
                    328: 	    push (@delmsgs,$_);
                    329: 	}
                    330:     }
                    331:     return &Apache::lonnet::del('nohist_res_msgs',\@delmsgs,$domain,$author);
1.73      www       332: }
                    333: 
                    334: # ================= Return hash with URLs for which there is a resource message
                    335: 
                    336: sub all_url_author_res_msg {
                    337:     my ($author,$domain)=@_;
1.75      www       338:     my %returnhash=();
1.76      www       339:     foreach (&Apache::lonnet::getkeys('nohist_res_msgs',$domain,$author)) {
1.75      www       340: 	$_=~/^(.+)\_\d+/;
                    341: 	$returnhash{$1}=1;
                    342:     }
                    343:     return %returnhash;
1.1       www       344: }
                    345: 
                    346: # ================================================== Critical message to a user
                    347: 
1.38      www       348: sub user_crit_msg_raw {
1.24      www       349:     my ($user,$domain,$subject,$message,$sendback)=@_;
1.2       www       350: # Check if allowed missing
                    351:     my $status='';
                    352:     my $msgid='undefined';
                    353:     unless (($message)&&($user)&&($domain)) { $status='empty'; };
                    354:     my $homeserver=&Apache::lonnet::homeserver($user,$domain);
                    355:     if ($homeserver ne 'no_host') {
1.3       www       356:        ($msgid,$message)=&packagemsg($subject,$message);
1.24      www       357:        if ($sendback) { $message.='<sendback>true</sendback>'; }
1.4       www       358:        $status=&Apache::lonnet::critical(
                    359:            'put:'.$domain.':'.$user.':critical:'.
                    360:            &Apache::lonnet::escape($msgid).'='.
                    361:            &Apache::lonnet::escape($message),$homeserver);
1.45      www       362:        if ($ENV{'request.course.id'}) {
                    363:           &user_normal_msg_raw(
                    364:             $ENV{'course.'.$ENV{'request.course.id'}.'.num'},
                    365:             $ENV{'course.'.$ENV{'request.course.id'}.'.domain'},
                    366:             'Critical ['.$user.':'.$domain.']',
                    367: 	    $message);
                    368:        }
1.2       www       369:     } else {
                    370:        $status='no_host';
                    371:     }
1.53      www       372: # Notifications
                    373:     my %userenv = &Apache::lonnet::get('environment',['critnotification'],
                    374:                                        $domain,$user);
                    375:     if ($userenv{'critnotification'}) {
                    376:       &sendnotification($userenv{'critnotification'},$user,$domain,$subject,1);
                    377:     }
                    378: # Log this
1.2       www       379:     &Apache::lonnet::logthis(
1.4       www       380:       'Sending critical email '.$msgid.
1.2       www       381:       ', log status: '.
                    382:       &Apache::lonnet::log($ENV{'user.domain'},$ENV{'user.name'},
                    383:                          $ENV{'user.home'},
                    384:       'Sending critical '.$msgid.' to '.$user.' at '.$domain.' with status: '
1.4       www       385:       .$status));
1.2       www       386:     return $status;
                    387: }
                    388: 
1.38      www       389: # New routine that respects "forward" and calls old routine
                    390: 
1.58      bowersj2  391: =pod
                    392: 
                    393: =item * B<user_crit_msg($user, $domain, $subject, $message, $sendback)>: Sends
                    394:     a critical message $message to the $user at $domain. If $sendback is true,
                    395:     a reciept will be sent to the current user when $user recieves the message.
                    396: 
                    397: =cut
                    398: 
1.38      www       399: sub user_crit_msg {
                    400:     my ($user,$domain,$subject,$message,$sendback)=@_;
                    401:     my $status='';
                    402:     my %userenv = &Apache::lonnet::get('environment',['msgforward'],
                    403:                                        $domain,$user);
                    404:     my $msgforward=$userenv{'msgforward'};
                    405:     if ($msgforward) {
                    406:        foreach (split(/\,/,$msgforward)) {
                    407: 	 my ($forwuser,$forwdomain)=split(/\:/,$_);
                    408:          $status.=
                    409: 	   &user_crit_msg_raw($forwuser,$forwdomain,$subject,$message,
                    410:                 $sendback).' ';
                    411:        }
                    412:     } else { 
                    413: 	$status=&user_crit_msg_raw($user,$domain,$subject,$message,$sendback);
                    414:     }
                    415:     return $status;
                    416: }
                    417: 
1.2       www       418: # =================================================== Critical message received
                    419: 
                    420: sub user_crit_received {
1.12      www       421:     my $msgid=shift;
                    422:     my %message=&Apache::lonnet::get('critical',[$msgid]);
1.52      www       423:     my %contents=&unpackagemsg($message{$msgid},1);
1.24      www       424:     my $status='rec: '.($contents{'sendback'}?
1.5       www       425:      &user_normal_msg($contents{'sendername'},$contents{'senderdomain'},
1.82      www       426:                      &mt('Receipt').': '.$ENV{'user.name'}.' '.&mt('at').' '.$ENV{'user.domain'}.', '.$contents{'subject'},
1.67      www       427:                      &mt('User').' '.$ENV{'user.name'}.' '.&mt('at').' '.$ENV{'user.domain'}.
1.42      www       428:                      ' acknowledged receipt of message'."\n".'   "'.
1.67      www       429:                      $contents{'subject'}.'"'."\n".&mt('dated').' '.
1.42      www       430:                      $contents{'time'}.".\n"
                    431:                      ):'no msg req');
1.5       www       432:     $status.=' trans: '.
1.12      www       433:      &Apache::lonnet::put(
                    434:      'nohist_email',{$contents{'msgid'} => $message{$msgid}});
1.5       www       435:     $status.=' del: '.
1.9       albertel  436:      &Apache::lonnet::del('critical',[$contents{'msgid'}]);
1.5       www       437:     &Apache::lonnet::log($ENV{'user.domain'},$ENV{'user.name'},
                    438:                          $ENV{'user.home'},'Received critical message '.
                    439:                          $contents{'msgid'}.
                    440:                          ', '.$status);
1.12      www       441:     return $status;
1.2       www       442: }
                    443: 
                    444: # ======================================================== Normal communication
                    445: 
1.38      www       446: sub user_normal_msg_raw {
1.51      www       447:     my ($user,$domain,$subject,$message,$citation,$baseurl,$attachmenturl)=@_;
1.2       www       448: # Check if allowed missing
                    449:     my $status='';
                    450:     my $msgid='undefined';
                    451:     unless (($message)&&($user)&&($domain)) { $status='empty'; };
                    452:     my $homeserver=&Apache::lonnet::homeserver($user,$domain);
                    453:     if ($homeserver ne 'no_host') {
1.51      www       454:        ($msgid,$message)=&packagemsg($subject,$message,$citation,$baseurl,
1.108     www       455:                                      $attachmenturl,$user,$domain);
                    456: # Store in user folder
1.4       www       457:        $status=&Apache::lonnet::critical(
                    458:            'put:'.$domain.':'.$user.':nohist_email:'.
                    459:            &Apache::lonnet::escape($msgid).'='.
                    460:            &Apache::lonnet::escape($message),$homeserver);
1.108     www       461: # Save new message received time
1.40      www       462:        &Apache::lonnet::put
                    463:                          ('email_status',{'recnewemail'=>time},$domain,$user);
1.108     www       464: # Into sent-mail folder
                    465:        $status.=' '.&Apache::lonnet::critical(
                    466:            'put:'.$ENV{'user.domain'}.':'.$ENV{'user.name'}.
                    467: 					      ':nohist_email_sent:'.
                    468:            &Apache::lonnet::escape($msgid).'='.
                    469:            &Apache::lonnet::escape($message),$ENV{'user.home'});
1.2       www       470:     } else {
                    471:        $status='no_host';
1.53      www       472:     }
                    473: # Notifications
                    474:     my %userenv = &Apache::lonnet::get('environment',['notification'],
                    475:                                        $domain,$user);
                    476:     if ($userenv{'notification'}) {
                    477: 	&sendnotification($userenv{'notification'},$user,$domain,$subject,0);
1.2       www       478:     }
                    479:     &Apache::lonnet::log($ENV{'user.domain'},$ENV{'user.name'},
                    480:                          $ENV{'user.home'},
                    481:       'Sending '.$msgid.' to '.$user.' at '.$domain.' with status: '.$status);
                    482:     return $status;
                    483: }
1.38      www       484: 
                    485: # New routine that respects "forward" and calls old routine
                    486: 
1.58      bowersj2  487: =pod
                    488: 
                    489: =item * B<user_normal_msg($user, $domain, $subject, $message,
                    490:     $citation, $baseurl, $attachmenturl)>: Sends a message to the
                    491:     $user at $domain, with subject $subject and message $message.
                    492: 
                    493: =cut
                    494: 
1.38      www       495: sub user_normal_msg {
1.52      www       496:     my ($user,$domain,$subject,$message,$citation,$baseurl,$attachmenturl)=@_;
1.38      www       497:     my $status='';
                    498:     my %userenv = &Apache::lonnet::get('environment',['msgforward'],
                    499:                                        $domain,$user);
                    500:     my $msgforward=$userenv{'msgforward'};
                    501:     if ($msgforward) {
                    502:        foreach (split(/\,/,$msgforward)) {
                    503: 	 my ($forwuser,$forwdomain)=split(/\:/,$_);
                    504:          $status.=
                    505: 	  &user_normal_msg_raw($forwuser,$forwdomain,$subject,$message,
1.52      www       506: 			       $citation,$baseurl,$attachmenturl).' ';
1.38      www       507:        }
                    508:     } else { 
1.49      albertel  509: 	$status=&user_normal_msg_raw($user,$domain,$subject,$message,
1.52      www       510: 				     $citation,$baseurl,$attachmenturl);
1.38      www       511:     }
                    512:     return $status;
                    513: }
                    514: 
1.2       www       515: 
1.106     www       516: # ============================================================ List all folders
                    517: 
                    518: sub folderlist {
                    519:     my $folder=shift;
                    520:     my @allfolders=&Apache::lonnet::getkeys('email_folders');
                    521:     if ($allfolders[0]=~/^error:/) { @allfolders=(); }
                    522:     return '<form method="post" action="/adm/email">'.
1.108     www       523: 	&mt('Folder').': '.
1.106     www       524: 	&Apache::loncommon::select_form($folder,'folder',
                    525: 			     ('' => &mt('INBOX'),'trash' => &mt('TRASH'),
1.114     www       526: 			      'new' => &mt('New Messages Only'),
1.113     www       527:                               'critical' => &mt('Critical'),
1.106     www       528: 			      'sent' => &mt('Sent Messages'),
                    529: 			      map { $_ => $_ } @allfolders)).
1.125     www       530: 			      ' '.&mt('Show').
                    531: 			      '<select name="interdis">'.
                    532: 			      join("\n",map { '<option value="'.$_.'"'.
                    533: 	 ($_==$interdis?' selected="selected"':'').'>'.$_.'</option>' }
                    534: 				   (10,20,50,100,200)).'</select>'.	
1.108     www       535:    '<input type="submit" value="'.&mt('View Folder').'" /><br />'.
1.119     www       536:     '<input type="hidden" name="sortedby" value="'.$ENV{'form.sortedby'}.'" />'.
1.118     www       537: 			      ($folder=~/^(new|critical)/?'</form>':'');
                    538: }
                    539: 
                    540: sub scrollbuttons {
                    541:     my ($start,$maxdis,$first,$finish,$total)=@_;
1.124     www       542:     unless ($total>0) { return ''; }
1.118     www       543:     $start++; $maxdis++;$first++;$finish++;
1.124     www       544:     return 
1.108     www       545:    '<input type="submit" name="firstview" value="'.&mt('First').'" />'.
                    546:    '<input type="submit" name="prevview" value="'.&mt('Previous').'" />'.
1.118     www       547:    '<input type="text" size="5" name="startdis" value="'.$start.'" onChange="this.form.submit()" /> of '.$maxdis.
1.108     www       548:    '<input type="submit" name="nextview" value="'.&mt('Next').'" />'.
1.118     www       549:    '<input type="submit" name="lastview" value="'.&mt('Last').'" /><br />'.
                    550:    &mt('Messages [_1] through [_2] of [_3]',$first,$finish,$total).'</form>';
1.106     www       551: }
1.108     www       552: 
1.106     www       553: # =============================================================== Folder suffix
                    554: 
                    555: sub foldersuffix {
                    556:     my $folder=shift;
                    557:     unless ($folder) { return ''; }
                    558:     return '_'.&Apache::lonnet::escape($folder);
                    559: }
                    560: 
1.7       www       561: # =============================================================== Status Change
                    562: 
                    563: sub statuschange {
1.106     www       564:     my ($msgid,$newstatus,$folder)=@_;
                    565:     my $suffix=&foldersuffix($folder);
                    566:     my %status=&Apache::lonnet::get('email_status'.$suffix,[$msgid]);
1.7       www       567:     if ($status{$msgid}=~/^error\:/) { $status{$msgid}=''; }
                    568:     unless ($status{$msgid}) { $status{$msgid}='new'; }
                    569:     unless (($status{$msgid} eq 'replied') || 
                    570:             ($status{$msgid} eq 'forwarded')) {
1.106     www       571: 	&Apache::lonnet::put('email_status'.$suffix,{$msgid => $newstatus});
1.7       www       572:     }
1.14      www       573:     if (($newstatus eq 'deleted') || ($newstatus eq 'new')) {
1.106     www       574: 	&Apache::lonnet::put('email_status'.$suffix,{$msgid => $newstatus});
1.14      www       575:     }
1.7       www       576: }
1.14      www       577: 
1.106     www       578: # ============================================================= Make new folder
                    579: 
                    580: sub makefolder {
                    581:     my ($newfolder)=@_;
1.113     www       582:     if (($newfolder eq 'sent')
                    583:      || ($newfolder eq 'critical')
1.114     www       584:      || ($newfolder eq 'trash')
                    585:      || ($newfolder eq 'new')) { return; }
1.106     www       586:     &Apache::lonnet::put('email_folders',{$newfolder => time});
                    587: }
                    588: 
                    589: # ======================================================== Move between folders
                    590: 
                    591: sub movemsg {
                    592:     my ($msgid,$srcfolder,$trgfolder)=@_;
1.107     www       593:     my $unmsgid=&Apache::lonnet::unescape($msgid);
1.106     www       594:     my $srcsuffix=&foldersuffix($srcfolder);
                    595:     my $trgsuffix=&foldersuffix($trgfolder);
1.107     www       596: 
                    597: # Copy message
                    598:     my %message=&Apache::lonnet::get('nohist_email'.$srcsuffix,[$msgid]);
                    599:     &Apache::lonnet::put('nohist_email'.$trgsuffix,{$msgid => $message{$msgid}});
                    600: 
                    601: # Copy status
                    602:     my %status=&Apache::lonnet::get('email_status'.$srcsuffix,[$unmsgid]);
                    603:     &Apache::lonnet::put('email_status'.$trgsuffix,{$unmsgid => $status{$unmsgid}});
                    604: # See if was deleted -> becomes "read" in trash
1.108     www       605:     my $currentstatus=(&unpackmsgid($status{$unmsgid}),$srcfolder);
1.107     www       606:     if ($currentstatus eq 'deleted') {
                    607: 	&statuschange($msgid,'read',$trgfolder);
                    608:     }
                    609: # Delete orginals
1.106     www       610:     &Apache::lonnet::del('nohist_email'.$srcsuffix,[$msgid]);
1.107     www       611:     &Apache::lonnet::del('email_status'.$srcsuffix,[$unmsgid]);
1.106     www       612: }
                    613: 
1.17      www       614: # ======================================================= Display a course list
                    615: 
                    616: sub discourse {
                    617:     my $r=shift;
1.109     matthew   618:     my $classlist = &Apache::loncoursedata::get_classlist();
1.17      www       619:     my $now=time;
1.67      www       620:     my %lt=&Apache::lonlocal::texthash('cfa' => 'Check for All',
                    621:             'cfs' => 'Check for Section/Group',
                    622:             'cfn' => 'Check for None');
1.17      www       623:     $r->print(<<ENDDISHEADER);
1.92      www       624: <input type="hidden" name="sendmode" value="group" />
1.17      www       625: <script>
                    626:     function checkall() {
                    627: 	for (i=0; i<document.forms.compemail.elements.length; i++) {
                    628:             if 
                    629:           (document.forms.compemail.elements[i].name.indexOf('send_to_')==0) {
                    630: 	      document.forms.compemail.elements[i].checked=true;
                    631:             }
                    632:         }
                    633:     }
                    634: 
1.19      www       635:     function checksec() {
                    636: 	for (i=0; i<document.forms.compemail.elements.length; i++) {
                    637:             if 
                    638:           (document.forms.compemail.elements[i].name.indexOf
                    639:            ('send_to_&&&'+document.forms.compemail.chksec.value)==0) {
                    640: 	      document.forms.compemail.elements[i].checked=true;
                    641:             }
                    642:         }
                    643:     }
                    644: 
1.17      www       645:     function uncheckall() {
                    646: 	for (i=0; i<document.forms.compemail.elements.length; i++) {
                    647:             if 
                    648:           (document.forms.compemail.elements[i].name.indexOf('send_to_')==0) {
                    649: 	      document.forms.compemail.elements[i].checked=false;
                    650:             }
                    651:         }
                    652:     }
                    653: </script>
1.92      www       654: <input type="button" onClick="checkall()" value="$lt{'cfa'}" />&nbsp;
                    655: <input type="button" onClick="checksec()" value="$lt{'cfs'}" />
                    656: <input type="text" size="5" name=chksec />&nbsp;
                    657: <input type="button" onClick="uncheckall()" value="$lt{'cfn'}" />
1.17      www       658: <p>
                    659: ENDDISHEADER
1.109     matthew   660:     my %coursepersonnel=&Apache::lonnet::get_course_adv_roles();
                    661:     $r->print('<table>');
1.61      www       662:     foreach my $role (sort keys %coursepersonnel) {
1.109     matthew   663:         foreach (split(/\,/,$coursepersonnel{$role})) {
                    664:             my ($puname,$pudom)=split(/\:/,$_);
                    665:             $r->print('<tr><td><label>'.
                    666:                       '<input type="checkbox" name="send_to_&&&&&&_'.
                    667:                       $puname.':'.$pudom.'" /> '.
                    668:                       &Apache::loncommon::plainname($puname,$pudom).
                    669:                       '</label></td>'.
                    670:                       '<td>('.$_.'),</td><td><i>'.$role.'</i></td></tr>');
                    671:         }
1.61      www       672:     }
1.110     matthew   673:     $r->print('</table><table>');
1.109     matthew   674:     while (my ($student,$info) = each(%$classlist)) {
                    675:         my ($sname,$sdom,$status,$fullname,$section) =
                    676:             (@{$info}[&Apache::loncoursedata::CL_SNAME(),
                    677:                       &Apache::loncoursedata::CL_SDOM(),
                    678:                       &Apache::loncoursedata::CL_STATUS(),
                    679:                       &Apache::loncoursedata::CL_FULLNAME(),
                    680:                       &Apache::loncoursedata::CL_SECTION()]);
1.110     matthew   681:         next if ($status ne 'Active');
1.109     matthew   682:         my $key = 'send_to_&&&'.$section.'&&&'.$student;
                    683:         if (! defined($fullname) || $fullname eq '') { $fullname = $sname; }
                    684:         $r->print('<tr><td><label>'.
                    685:                   qq{<input type="checkbox" name="$key">}.('&nbsp;'x2).
                    686:                   $fullname.'</td><td>'.$sname.'@'.$sdom.'</td><td>'.$section.
                    687:                   '</td></tr>');
1.28      harris41  688:     }
1.110     matthew   689:     $r->print('</table>');
1.17      www       690: }
                    691: 
1.13      www       692: # ==================================================== Display Critical Message
1.5       www       693: 
1.12      www       694: sub discrit {
                    695:     my $r=shift;
1.67      www       696:     my $header = '<h1><font color=red>'.&mt('Critical Messages').'</font></h1>'.
1.30      matthew   697:         '<form action=/adm/email method=post>'.
                    698:         '<input type=hidden name=confirm value=true>';
                    699:     my %what=&Apache::lonnet::dump('critical');
                    700:     my $result = '';
                    701:     foreach (sort keys %what) {
                    702:         my %content=&unpackagemsg($what{$_});
                    703:         next if ($content{'senderdomain'} eq '');
1.106     www       704:         $result.='<hr />'.&mt('From').': <b>'.
1.37      www       705: &Apache::loncommon::aboutmewrapper(
                    706:  &Apache::loncommon::plainname($content{'sendername'},$content{'senderdomain'}),$content{'sendername'},$content{'senderdomain'}).'</b> ('.
                    707: $content{'sendername'}.'@'.
                    708:             $content{'senderdomain'}.') '.$content{'time'}.
1.106     www       709:             '<br />'.&mt('Subject').': '.$content{'subject'}.
                    710:             '<br /><blockquote>'.
1.36      www       711:               &Apache::lontexconvert::msgtexconverted($content{'message'}).
1.84      www       712:             '</blockquote><small>'.
                    713: &mt('You have to confirm that you received this message. After confirmation, this message will be moved to your regular inbox').
                    714:             '</small><br />'.
1.67      www       715:             '<input type=submit name="rec_'.$_.'" value="'.&mt('Confirm Receipt').'">'.
1.30      matthew   716:             '<input type=submit name="reprec_'.$_.'" '.
1.67      www       717:                   'value="'.&mt('Confirm Receipt and Reply').'">';
1.30      matthew   718:     }
                    719:     # Check to see if there were any messages.
                    720:     if ($result eq '') {
1.67      www       721:         $result = "<h2>".&mt('You have no critical messages.')."</h2>".
1.106     www       722: 	    '<a href="/adm/roles">'.&mt('Select a course').'</a><br />'.
                    723:             '<a href="/adm/email">'.&mt('Communicate').'</a>';
1.30      matthew   724:     } else {
                    725:         $r->print($header);
                    726:     }
                    727:     $r->print($result);
1.108     www       728:     $r->print('<input type="hidden" name="displayedcrit" value="true" /></form>');
1.12      www       729: }
                    730: 
1.65      www       731: sub sortedmessages {
1.106     www       732:     my ($blocked,$startblock,$endblock,$numblocked,$folder) = @_;
                    733:     my $suffix=&foldersuffix($folder);
                    734:     my @messages = &Apache::lonnet::getkeys('nohist_email'.$suffix);
1.65      www       735:     #unpack the varibles and repack into temp for sorting
                    736:     my @temp;
                    737:     foreach (@messages) {
                    738: 	my $msgid=&Apache::lonnet::escape($_);
                    739: 	my ($sendtime,$shortsubj,$fromname,$fromdomain,$status)=
1.108     www       740: 	    &Apache::lonmsg::unpackmsgid($msgid,$folder);
1.65      www       741: 	my @temp1 = ($sendtime,$shortsubj,$fromname,$fromdomain,$status,
                    742: 		     $msgid);
1.101     raeburn   743:         # Check whether message was sent during blocking period.
                    744:         if ($sendtime >= $startblock && ($sendtime <= $endblock && $endblock > 0) ) {
                    745:             my $escid = &Apache::lonnet::unescape($msgid);
                    746:             $$blocked{$escid} = 'ON';
                    747:             $$numblocked ++;
                    748:         } else { 
                    749:             push @temp ,\@temp1;
                    750:         }
1.65      www       751:     }
                    752:     #default sort
                    753:     @temp = sort  {$a->[0] <=> $b->[0]} @temp;    
                    754:     if ($ENV{'form.sortedby'} eq "date"){
                    755:         @temp = sort  {$a->[0] <=> $b->[0]} @temp;    
                    756:     }
                    757:     if ($ENV{'form.sortedby'} eq "revdate"){
                    758:     	@temp = sort  {$b->[0] <=> $a->[0]} @temp; 
                    759:     }
                    760:     if ($ENV{'form.sortedby'} eq "user"){
                    761: 	@temp = sort  {lc($a->[2]) cmp lc($b->[2])} @temp;
                    762:     }
                    763:     if ($ENV{'form.sortedby'} eq "revuser"){
                    764: 	@temp = sort  {lc($b->[2]) cmp lc($a->[2])} @temp;
                    765:     }
                    766:     if ($ENV{'form.sortedby'} eq "domain"){
                    767:         @temp = sort  {$a->[3] cmp $b->[3]} @temp;
                    768:     }
                    769:     if ($ENV{'form.sortedby'} eq "revdomain"){
                    770:         @temp = sort  {$b->[3] cmp $a->[3]} @temp;
                    771:     }
                    772:     if ($ENV{'form.sortedby'} eq "subject"){
                    773:         @temp = sort  {lc($a->[1]) cmp lc($b->[1])} @temp;
                    774:     }
                    775:     if ($ENV{'form.sortedby'} eq "revsubject"){
                    776:         @temp = sort  {lc($b->[1]) cmp lc($a->[1])} @temp;
                    777:     }
                    778:     if ($ENV{'form.sortedby'} eq "status"){
                    779:         @temp = sort  {$a->[4] cmp $b->[4]} @temp;
                    780:     }
                    781:     if ($ENV{'form.sortedby'} eq "revstatus"){
                    782:         @temp = sort  {$b->[4] cmp $a->[4]} @temp;
                    783:     }
                    784:     return @temp;
                    785: }
                    786: 
1.112     www       787: # ======================================================== Display new messages
                    788: 
                    789: 
                    790: sub disnew {
                    791:     my $r=shift;
                    792:     my %lt=&Apache::lonlocal::texthash(
                    793: 				       'nm' => 'New Messages',
                    794: 				       'su' => 'Subject',
                    795: 				       'da' => 'Date',
                    796: 				       'us' => 'Username',
                    797: 				       'op' => 'Open',
                    798: 				       'do' => 'Domain'
                    799: 				       );
                    800:     my @msgids = sort split(/\&/,&Apache::lonnet::reply
                    801:                             ('keys:'.$ENV{'user.domain'}.':'.
                    802:                              $ENV{'user.name'}.':nohist_email',
                    803:                              $ENV{'user.home'}));
                    804:     my @newmsgs;
                    805:     my %setters = ();
                    806:     my $startblock = 0;
                    807:     my $endblock = 0;
                    808:     my %blocked = ();
                    809:     my $numblocked = 0;
                    810:     # Check for blocking of display because of scheduled online exams.
                    811:     &blockcheck(\%setters,\$startblock,\$endblock);
                    812:     foreach (@msgids) {
                    813:         my ($sendtime,$shortsubj,$fromname,$fromdom,$status)=
                    814: 	    &Apache::lonmsg::unpackmsgid($_);
                    815:         if (defined($sendtime) && $sendtime!~/error/) {
                    816:             my $numsendtime = $sendtime;
                    817:             $sendtime = &Apache::lonlocal::locallocaltime($sendtime);
                    818:             if ($status eq 'new') {
                    819:                 if ($numsendtime >= $startblock && ($numsendtime <= $endblock && $endblock > 0) ) {
                    820:                     $blocked{$_} = 'ON';
                    821:                     $numblocked ++;
                    822:                 } else {
                    823:                     push @newmsgs, { 
                    824:                         msgid    => $_,
                    825:                         sendtime => $sendtime,
                    826:                         shortsub => &Apache::lonnet::unescape($shortsubj),
                    827:                         from     => $fromname,
                    828:                         fromdom  => $fromdom 
                    829:                         }
                    830:                 }
                    831:             }
                    832:         }
                    833:     }
                    834:     if ($#newmsgs >= 0) {
                    835:         $r->print(<<TABLEHEAD);
                    836: <h2>$lt{'nm'}</h2>
                    837: <table border=2><tr><th>&nbsp</th>
                    838: <th>$lt{'da'}</th><th>$lt{'us'}</th><th>$lt{'do'}</th><th>$lt{'su'}</th></tr>
                    839: TABLEHEAD
                    840:         foreach my $msg (@newmsgs) {
                    841:             $r->print(<<"ENDLINK");
                    842: <tr bgcolor="#FFBB77">
                    843: <td><a href="/adm/email?display=$msg->{'msgid'}">$lt{'op'}</a></td>
                    844: ENDLINK
                    845:             foreach ('sendtime','from','fromdom','shortsub') {
                    846:                 $r->print("<td>$msg->{$_}</td>");
                    847:             }
                    848:             $r->print("</td></tr>");
                    849:         }
                    850:         $r->print('</table></body></html>');
                    851:     } elsif ($numblocked == 0) {
                    852:         $r->print("<h3>".&mt('You have no unread messages')."</h3>");
                    853:     }
                    854:     if ($numblocked > 0) {
                    855:         my $beginblock = &Apache::lonlocal::locallocaltime($startblock);
                    856:         my $finishblock = &Apache::lonlocal::locallocaltime($endblock);
                    857:         if ($numblocked == 1) {
                    858:             $r->print("<h3>".&mt('You have').' '.$numblocked.' '.&mt('blocked unread message').".</h3>");
                    859:             $r->print(&mt('This message is not viewable because').' ');
                    860:         } else {
                    861:             $r->print("<h3>".&mt('You have').' '.$numblocked.' '.&mt('blocked unread messages').".</h3>");
                    862:             $r->print(&mt('These').' '.$numblocked.' '.&mt('messages are not viewable because '));
                    863:         }
                    864:         $r->print(
                    865: &mt('display of LON-CAPA messages sent to you by other students between').' '.$beginblock.' '.&mt('and').' '.$finishblock.' '.&mt('is currently being blocked because of online exams').'.');
                    866:         &build_block_table($r,$startblock,$endblock,\%setters);
                    867:     }
                    868: }
                    869: 
                    870: 
1.15      www       871: # ======================================================== Display all messages
                    872: 
1.14      www       873: sub disall {
1.106     www       874:     my ($r,$folder)=@_;
1.113     www       875:     $r->print(&folderlist($folder));
1.114     www       876:     if ($folder eq 'new') {
                    877: 	&disnew($r);
                    878:     } elsif ($folder eq 'critical') {
                    879: 	&discrit($r);
                    880:     } else {
                    881: 	&disfolder($r,$folder);
1.113     www       882:     }
1.114     www       883: }
                    884: 
                    885: # ============================================================ Display a folder
                    886: 
                    887: sub disfolder {
                    888:     my ($r,$folder)=@_;
1.101     raeburn   889:     my %blocked = ();
                    890:     my %setters = ();
                    891:     my $startblock;
                    892:     my $endblock;
                    893:     my $numblocked = 0;
                    894:     &blockcheck(\%setters,\$startblock,\$endblock);
                    895:     $r->print(<<ENDDISHEADER);
1.29      www       896: <script>
                    897:     function checkall() {
                    898: 	for (i=0; i<document.forms.disall.elements.length; i++) {
                    899:             if 
                    900:           (document.forms.disall.elements[i].name.indexOf('delmark_')==0) {
                    901: 	      document.forms.disall.elements[i].checked=true;
                    902:             }
                    903:         }
                    904:     }
                    905: 
                    906:     function uncheckall() {
                    907: 	for (i=0; i<document.forms.disall.elements.length; i++) {
                    908:             if 
                    909:           (document.forms.disall.elements[i].name.indexOf('delmark_')==0) {
                    910: 	      document.forms.disall.elements[i].checked=false;
                    911:             }
                    912:         }
                    913:     }
                    914: </script>
                    915: ENDDISHEADER
1.108     www       916:     my $fsqs='&folder='.$folder;
                    917:     my @temp=sortedmessages(\%blocked,$startblock,$endblock,\$numblocked,$folder);
                    918:     my $totalnumber=$#temp+1;
1.124     www       919:     unless ($totalnumber>0) {
                    920: 	$r->print('<h2>'.&mt('Empty Folder').'</h2>');
                    921: 	return;
                    922:     }
1.125     www       923:     unless ($interdis) {
                    924: 	$interdis=20;
                    925:     }
1.118     www       926:     my $number=int($totalnumber/$interdis);
                    927:     if (($startdis<0) || ($startdis>$number)) { $startdis=$number; }
1.108     www       928:     my $firstdis=$interdis*$startdis;
                    929:     if ($firstdis>$#temp) { $firstdis=$#temp-$interdis+1; }
                    930:     my $lastdis=$firstdis+$interdis-1;
                    931:     if ($lastdis>$#temp) { $lastdis=$#temp; }
1.118     www       932:     $r->print(&scrollbuttons($startdis,$number,$firstdis,$lastdis,$totalnumber));
1.113     www       933:     $r->print('<form method="post" name="disall" action="/adm/email">'.
1.106     www       934: 	      '<table border=2><tr><th colspan="3">&nbsp</th><th>');
1.62      www       935:     if ($ENV{'form.sortedby'} eq "revdate") {
1.108     www       936: 	$r->print('<a href = "?sortedby=date'.$fsqs.'">'.&mt('Date').'</a></th>');
1.62      www       937:     } else {
1.108     www       938: 	$r->print('<a href = "?sortedby=revdate'.$fsqs.'">'.&mt('Date').'</a></th>');
1.62      www       939:     }
                    940:     $r->print('<th>');
                    941:     if ($ENV{'form.sortedby'} eq "revuser") {
1.108     www       942: 	$r->print('<a href = "?sortedby=user'.$fsqs.'">'.&mt('Username').'</a>');
1.62      www       943:     } else {
1.108     www       944: 	$r->print('<a href = "?sortedby=revuser'.$fsqs.'">'.&mt('Username').'</a>');
1.62      www       945:     }
                    946:     $r->print('</th><th>');
                    947:     if ($ENV{'form.sortedby'} eq "revdomain") {
1.108     www       948: 	$r->print('<a href = "?sortedby=domain'.$fsqs.'">'.&mt('Domain').'</a>');
1.62      www       949:     } else {
1.108     www       950: 	$r->print('<a href = "?sortedby=revdomain'.$fsqs.'">'.&mt('Domain').'</a>');
1.62      www       951:     }
                    952:     $r->print('</th><th>');
                    953:     if ($ENV{'form.sortedby'} eq "revsubject") {
1.108     www       954: 	$r->print('<a href = "?sortedby=subject'.$fsqs.'">'.&mt('Subject').'</a>');
1.62      www       955:     } else {
1.108     www       956:     	$r->print('<a href = "?sortedby=revsubject'.$fsqs.'">'.&mt('Subject').'</a>');
1.62      www       957:     }
                    958:     $r->print('</th><th>');
                    959:     if ($ENV{'form.sortedby'} eq "revstatus") {
1.108     www       960: 	$r->print('<a href = "?sortedby=status'.$fsqs.'">'.&mt('Status').'</th>');
1.62      www       961:     } else {
1.108     www       962:      	$r->print('<a href = "?sortedby=revstatus'.$fsqs.'">'.&mt('Status').'</th>');
1.62      www       963:     }
1.126   ! www       964:     $r->print("</tr>\n");
1.108     www       965:     for (my $n=$firstdis;$n<=$lastdis;$n++) {
                    966: 	my ($sendtime,$shortsubj,$fromname,$fromdomain,$status,$origID)= @{$temp[$n]};
1.63      albertel  967: 	if (($status ne 'deleted') && defined($sendtime) && $sendtime!~/error/) {
1.39      albertel  968: 	    if ($status eq 'new') {
                    969: 		$r->print('<tr bgcolor="#FFBB77">');
                    970: 	    } elsif ($status eq 'read') {
                    971: 		$r->print('<tr bgcolor="#BBBB77">');
                    972: 	    } elsif ($status eq 'replied') {
1.62      www       973: 		$r->print('<tr bgcolor="#AAAA88">'); 
1.39      albertel  974: 	    } else {
                    975: 		$r->print('<tr bgcolor="#99BBBB">');
                    976: 	    }
1.106     www       977: 	    $r->print('<td></a><input type=checkbox name="delmark_'.$origID.'" /></td><td><a href="/adm/email?display='.$origID.$sqs. 
                    978: 		      '">'.&mt('Open').'</a></td><td>'.
                    979: 		      ($folder ne 'trash'?'<a href="/adm/email?markdel='.$origID.$sqs.
                    980: 		      '">'.&mt('Delete'):'&nbsp').'</td>'.
1.66      www       981: 		      '<td>'.&Apache::lonlocal::locallocaltime($sendtime).'</td><td>'.
1.39      albertel  982: 		      $fromname.'</td><td>'.$fromdomain.'</td><td>'.
1.14      www       983: 		      &Apache::lonnet::unescape($shortsubj).'</td><td>'.
1.126   ! www       984:                       $status."</td></tr>\n");
1.106     www       985: 	} elsif ($status eq 'deleted') {
                    986: # purge
1.108     www       987: 	    &movemsg(&Apache::lonnet::unescape($origID),$folder,'trash');
1.63      albertel  988: 	}
                    989:     }   
1.126   ! www       990:     $r->print("</table>\n<p>".
1.106     www       991:   '<a href="javascript:checkall()">'.&mt('Check All').'</a>&nbsp;'.
                    992:   '<a href="javascript:uncheckall()">'.&mt('Uncheck All').'</a></p>'.
                    993:   '<input type="hidden" name="sortedby" value="'.$ENV{'form.sortedby'}.'" />');
                    994:     if ($folder ne 'trash') {
                    995: 	$r->print(
                    996: 	      '<p><input type="submit" name="markeddel" value="'.&mt('Delete Checked').'" /></p>');
                    997:     }
1.118     www       998:     $r->print('<p><input type="submit" name="markedmove" value="'.&mt('Move Checked to Folder').'" />');
1.106     www       999:     my @allfolders=&Apache::lonnet::getkeys('email_folders');
                   1000:     if ($allfolders[0]=~/^error:/) { @allfolders=(); }
                   1001:     $r->print(
                   1002: 	&Apache::loncommon::select_form('','movetofolder',
                   1003: 			     ( map { $_ => $_ } @allfolders))
                   1004: 	      );
1.126   ! www      1005:     my $postedstartdis=$startdis+1;
        !          1006:     $r->print('<input type="hidden" name="folder" value="'.$folder.'" /><input type="hidden" name="startdis" value="'.$postedstartdis.'" /><input type="hidden" name="interdis" value="'.$ENV{'form.interdis'}.'" /></form>');
1.101     raeburn  1007:     if ($numblocked > 0) {
                   1008:         my $beginblock = &Apache::lonlocal::locallocaltime($startblock);
                   1009:         my $finishblock = &Apache::lonlocal::locallocaltime($endblock);
                   1010:         $r->print('<br /><br />'.
                   1011:                   $numblocked.' '.&mt('message(s) is/are not viewable because display of LON-CAPA messages sent to you by other students between').' '.$beginblock.' '.&mt('and').' '.$finishblock.' '.&mt('is currently being blocked because of online exams.'));
                   1012:         &build_block_table($r,$startblock,$endblock,\%setters);
                   1013:     }
1.14      www      1014: }
                   1015: 
1.15      www      1016: # ============================================================== Compose output
                   1017: 
                   1018: sub compout {
1.108     www      1019:     my ($r,$forwarding,$replying,$broadcast,$replycrit,$folder)=@_;
1.121     www      1020:     my $suffix=&foldersuffix($folder);
1.92      www      1021: 
                   1022:     if ($broadcast eq 'individual') {
                   1023: 	&printheader($r,'/adm/email?compose=individual',
                   1024: 	     'Send a Message');
                   1025:     } elsif ($broadcast) {
                   1026: 	&printheader($r,'/adm/email?compose=group',
                   1027: 	     'Broadcast Message');
                   1028:     } elsif ($forwarding) {
                   1029: 	&Apache::lonhtmlcommon::add_breadcrumb
                   1030:         ({href=>"/adm/email?display=".&Apache::lonnet::escape($forwarding),
                   1031:           text=>"Display Message"});
                   1032: 	&printheader($r,'/adm/email?forward='.&Apache::lonnet::escape($forwarding),
                   1033: 	     'Forwarding a Message');
                   1034:     } elsif ($replying) {
                   1035: 	&Apache::lonhtmlcommon::add_breadcrumb
                   1036:         ({href=>"/adm/email?display=".&Apache::lonnet::escape($replying),
                   1037:           text=>"Display Message"});
                   1038: 	&printheader($r,'/adm/email?replyto='.&Apache::lonnet::escape($replying),
                   1039: 	     'Replying to a Message');
1.94      www      1040:     } elsif ($replycrit) {
                   1041: 	$r->print('<h3>'.&mt('Replying to a Critical Message').'</h3>');
                   1042: 	$replying=$replycrit;
1.92      www      1043:     } else {
                   1044: 	&printheader($r,'/adm/email?compose=upload',
                   1045: 	     'Distribute from Uploaded File');
                   1046:     }
                   1047: 
1.89      www      1048:     my $dispcrit='';
1.15      www      1049:     my $dissub='';
                   1050:     my $dismsg='';
1.115     www      1051:     my $disbase='';
1.67      www      1052:     my $func=&mt('Send New');
1.69      www      1053:     my %lt=&Apache::lonlocal::texthash('us' => 'Username',
                   1054: 				       'do' => 'Domain',
                   1055: 				       'ad' => 'Additional Recipients',
                   1056: 				       'sb' => 'Subject',
                   1057: 				       'ca' => 'Cancel',
                   1058: 				       'ma' => 'Mail');
                   1059: 
                   1060:     if (&Apache::lonnet::allowed('srm',$ENV{'request.course.id'})) {
1.35      bowersj2 1061: 	 my $crithelp = Apache::loncommon::help_open_topic("Course_Critical_Message");
1.15      www      1062:          $dispcrit=
1.92      www      1063:  '<input type="checkbox" name="critmsg" /> '.&mt('Send as critical message').' ' . $crithelp . 
1.35      bowersj2 1064:  '<br>'.
1.92      www      1065:  '<input type="checkbox" name="sendbck" /> '.&mt('Send as critical message').'  ' .
1.67      www      1066:  &mt('and return receipt') . $crithelp . '<p>';
1.92      www      1067:      }
                   1068:     my %message;
                   1069:     my %content;
                   1070:     my $defdom=$ENV{'user.domain'};
1.15      www      1071:     if ($forwarding) {
1.121     www      1072: 	%message=&Apache::lonnet::get('nohist_email'.$suffix,[$forwarding]);
1.108     www      1073: 	%content=&unpackagemsg($message{$forwarding},$folder);
1.92      www      1074: 	$dispcrit.='<input type="hidden" name="forwid" value="'.
                   1075: 	    $forwarding.'" />';
                   1076: 	$func=&mt('Forward');
                   1077: 	
                   1078: 	$dissub=&mt('Forwarding').': '.$content{'subject'};
                   1079: 	$dismsg=&mt('Forwarded message from').' '.
                   1080: 	    $content{'sendername'}.' '.&mt('at').' '.$content{'senderdomain'};
1.115     www      1081: 	if ($content{'baseurl'}) {
                   1082: 	    $disbase='<input type="hidden" name="baseurl" value="'.&Apache::lonnet::escape($content{'baseurl'}).'" />';
                   1083: 	}
1.92      www      1084:     }
                   1085:     if ($replying) {
1.121     www      1086: 	%message=&Apache::lonnet::get('nohist_email'.$suffix,[$replying]);
1.108     www      1087: 	%content=&unpackagemsg($message{$replying},$folder);
1.105     albertel 1088: 	$dispcrit.='<input type="hidden" name="replyid" value="'.
                   1089: 	    $replying.'" />';
1.108     www      1090: 	$func=&mt('Send Reply to');
1.92      www      1091: 	
                   1092: 	$dissub=&mt('Reply').': '.$content{'subject'};       
                   1093: 	$dismsg='> '.$content{'message'};
                   1094: 	$dismsg=~s/\r/\n/g;
                   1095: 	$dismsg=~s/\f/\n/g;
                   1096: 	$dismsg=~s/\n+/\n\> /g;
1.115     www      1097: 	if ($content{'baseurl'}) {
                   1098: 	    $disbase='<input type="hidden" name="baseurl" value="'.&Apache::lonnet::escape($content{'baseurl'}).'" />';
                   1099: 	    if ($ENV{'user.adv'}) {
1.120     www      1100: 		$disbase.='<input type="checkbox" name="storebasecomment" />'.&mt('Store message for re-use').
                   1101: 		    ' <a href="/adm/email?showcommentbaseurl='.
                   1102: 		    &Apache::lonnet::escape($content{'baseurl'}).'" target="comments">'.
                   1103: 		    &mt('Show re-usable messages').'</a><br />';
1.115     www      1104: 	    }
                   1105: 	}
1.15      www      1106:     }
1.111     www      1107:     my $citation=&displayresource(%content);
1.37      www      1108:     if ($ENV{'form.recdom'}) { $defdom=$ENV{'form.recdom'}; }
1.22      www      1109:       $r->print(
1.31      matthew  1110:                 '<form action="/adm/email"  name="compemail" method="post"'.
                   1111:                 ' enctype="multipart/form-data">'."\n".
1.92      www      1112:                 '<input type="hidden" name="sendmail" value="on" />'."\n".
1.31      matthew  1113:                 '<table>');
1.22      www      1114:     unless (($broadcast eq 'group') || ($broadcast eq 'upload')) {
1.92      www      1115: 	if ($replying) {
                   1116: 	    $r->print('<tr><td colspan="2">'.&mt('Replying to').' '.
                   1117: 		      &Apache::loncommon::aboutmewrapper(
                   1118: 							 &Apache::loncommon::plainname($content{'sendername'},$content{'senderdomain'}),$content{'sendername'},$content{'senderdomain'}).' ('.
                   1119: 		      $content{'sendername'}.'@'.
                   1120: 		      $content{'senderdomain'}.')'.
                   1121: 		      '<input type="hidden" name="recuname" value="'.$content{'sendername'}.'" />'.
                   1122: 		      '<input type="hidden" name="recdomain" value="'.$content{'senderdomain'}.'" />'.
                   1123: 		      '</td></tr>');
                   1124: 	} else {
                   1125: 	    my $domform = &Apache::loncommon::select_dom_form($defdom,'recdomain');
                   1126: 	    my $selectlink=&Apache::loncommon::selectstudent_link
1.46      www      1127: 	    ('compemail','recuname','recdomain');
1.92      www      1128: 	    $r->print(<<"ENDREC");
1.69      www      1129: <tr><td>$lt{'us'}:</td><td><input type="text" size="12" name="recuname" value="$ENV{'form.recname'}"></td><td rowspan="2">$selectlink</td></tr>
                   1130: <tr><td>$lt{'do'}:</td>
1.31      matthew  1131: <td>$domform</td></tr>
1.17      www      1132: ENDREC
1.92      www      1133:         }
1.17      www      1134:     }
1.55      bowersj2 1135:     my $latexHelp = Apache::loncommon::helpLatexCheatsheet();
1.31      matthew  1136:     if ($broadcast ne 'upload') {
1.22      www      1137:        $r->print(<<"ENDCOMP");
1.69      www      1138: <tr><td>$lt{'ad'}<br /><tt>username\@domain,username\@domain, ...
1.20      www      1139: </tt></td><td>
1.91      www      1140: <input type="text" size="50" name="additionalrec" /></td></tr>
                   1141: <tr><td>$lt{'sb'}:</td><td><input type="text" size="50" name="subject" value="$dissub" />
1.15      www      1142: </td></tr></table>
1.55      bowersj2 1143: $latexHelp
1.92      www      1144: <textarea name="message" cols="80" rows="15" wrap="hard">$dismsg
1.69      www      1145: </textarea></p><br />
1.15      www      1146: $dispcrit
1.115     www      1147: $disbase
1.69      www      1148: <input type="submit" name="send" value="$func $lt{'ma'}" />
1.111     www      1149: <input type="submit" name="cancel" value="$lt{'ca'}" /><hr />
                   1150: $citation
1.15      www      1151: ENDCOMP
1.31      matthew  1152:     } else { # $broadcast is 'upload'
1.22      www      1153: 	$r->print(<<ENDUPLOAD);
1.91      www      1154: <input type="hidden" name="sendmode" value="upload" />
1.86      www      1155: <input type="hidden" name="send" value="on" />
1.22      www      1156: <h3>Generate messages from a file</h3>
1.31      matthew  1157: <p>
1.91      www      1158: Subject: <input type="text" size="50" name="subject" />
1.31      matthew  1159: </p>
                   1160: <p>General message text<br />
1.91      www      1161: <textarea name="message" cols="60" rows="10" wrap="hard">$dismsg
1.31      matthew  1162: </textarea></p>
                   1163: <p>
                   1164: The file format for the uploaded portion of the message is:
1.22      www      1165: <pre>
                   1166: username1\@domain1: text
                   1167: username2\@domain2: text
1.31      matthew  1168: username3\@domain1: text
1.22      www      1169: </pre>
1.31      matthew  1170: </p>
                   1171: <p>
1.22      www      1172: The messages will be assembled from all lines with the respective 
1.31      matthew  1173: <tt>username\@domain</tt>, and appended to the general message text.</p>
                   1174: <p>
1.91      www      1175: <input type="file" name="upfile" size="40" /></p><p>
1.22      www      1176: $dispcrit
1.92      www      1177: <input type="submit" value="Upload and Send" /></p>
1.22      www      1178: ENDUPLOAD
                   1179:     }
1.17      www      1180:     if ($broadcast eq 'group') {
                   1181:        &discourse;
                   1182:     }
                   1183:     $r->print('</form>');
1.15      www      1184: }
                   1185: 
1.45      www      1186: # ---------------------------------------------------- Display all face to face
                   1187: 
1.104     matthew  1188: sub retrieve_instructor_comments {
                   1189:     my ($user,$domain)=@_;
                   1190:     my $target=$ENV{'form.grade_target'};
                   1191:     if (! $ENV{'request.course.id'}) { return; }
                   1192:     if (! &Apache::lonnet::allowed('srm',$ENV{'request.course.id'})) {
                   1193: 	return;
                   1194:     }
                   1195:     my %records=&Apache::lonnet::dump('nohist_email',
                   1196: 			 $ENV{'course.'.$ENV{'request.course.id'}.'.domain'},
                   1197: 			 $ENV{'course.'.$ENV{'request.course.id'}.'.num'},
                   1198:                          '%255b'.$user.'%253a'.$domain.'%255d');
                   1199:     my $result='';
                   1200:     foreach (sort(keys(%records))) {
                   1201:         my %content=&unpackagemsg($records{$_});
                   1202:         next if ($content{'senderdomain'} eq '');
                   1203:         next if ($content{'subject'} !~ /^Record/);
                   1204:         # $content{'message'}=~s/\n/\<br\>/g;
                   1205:         $result.='Recorded by '.
                   1206:             $content{'sendername'}.'@'.$content{'senderdomain'}."\n";
                   1207:         $result.=
                   1208:             &Apache::lontexconvert::msgtexconverted($content{'message'})."\n";
                   1209:      }
                   1210:     return $result;
                   1211: }
                   1212: 
1.45      www      1213: sub disfacetoface {
                   1214:     my ($r,$user,$domain)=@_;
1.98      sakharuk 1215:     my $target=$ENV{'form.grade_target'};
1.45      www      1216:     unless ($ENV{'request.course.id'}) { return; }
                   1217:     unless (&Apache::lonnet::allowed('srm',$ENV{'request.course.id'})) {
                   1218: 	return;
                   1219:     }
                   1220:     my %records=&Apache::lonnet::dump('nohist_email',
                   1221: 			 $ENV{'course.'.$ENV{'request.course.id'}.'.domain'},
                   1222: 			 $ENV{'course.'.$ENV{'request.course.id'}.'.num'},
                   1223:                          '%255b'.$user.'%253a'.$domain.'%255d');
                   1224:     my $result='';
                   1225:     foreach (sort keys %records) {
                   1226:         my %content=&unpackagemsg($records{$_});
                   1227:         next if ($content{'senderdomain'} eq '');
                   1228:         $content{'message'}=~s/\n/\<br\>/g;
                   1229:         if ($content{'subject'}=~/^Record/) {
1.69      www      1230: 	    $result.='<h3>'.&mt('Record').'</h3>';
1.102     raeburn  1231:         } elsif ($content{'subject'}=~/^Broadcast/) {
                   1232:             $result .='<h3>'.&mt('Broadcast Message').'</h3>';
1.45      www      1233:         } else {
1.102     raeburn  1234:             $result.='<h3>'.&mt('Critical Message').'</h3>';
1.45      www      1235:             %content=&unpackagemsg($content{'message'});
                   1236:             $content{'message'}=
1.92      www      1237:                 '<b>'.&mt('Subject').': '.$content{'subject'}.'</b><br />'.
1.45      www      1238: 		$content{'message'};
                   1239:         }
1.69      www      1240:         $result.=&mt('By').': <b>'.
1.45      www      1241: &Apache::loncommon::aboutmewrapper(
                   1242:  &Apache::loncommon::plainname($content{'sendername'},$content{'senderdomain'}),$content{'sendername'},$content{'senderdomain'}).'</b> ('.
                   1243: $content{'sendername'}.'@'.
                   1244:             $content{'senderdomain'}.') '.$content{'time'}.
1.92      www      1245:             '<br /><blockquote>'.
1.45      www      1246:               &Apache::lontexconvert::msgtexconverted($content{'message'}).
                   1247: 	      '</blockquote>';
                   1248:      }
                   1249:     # Check to see if there were any messages.
                   1250:     if ($result eq '') {
1.98      sakharuk 1251: 	if ($target ne 'tex') { 
1.102     raeburn  1252: 	    $r->print("<p><b>".&mt("No notes, face-to-face discussion records, critical messages, or broadcast messages in this course.")."</b></p>");
1.98      sakharuk 1253: 	} else {
1.102     raeburn  1254: 	    $r->print('\textbf{'.&mt("No notes, face-to-face discussion records, critical messages or broadcast messages in this course.").'}\\\\');
1.98      sakharuk 1255: 	}
1.45      www      1256:     } else {
                   1257:        $r->print($result);
                   1258:     }
                   1259: }
                   1260: 
1.44      www      1261: # ---------------------------------------------------------------- Face to face
                   1262: 
                   1263: sub facetoface {
                   1264:     my ($r,$stage)=@_;
                   1265:     unless (&Apache::lonnet::allowed('srm',$ENV{'request.course.id'})) {
                   1266: 	return;
                   1267:     }
1.89      www      1268:     &printheader($r,
                   1269: 		 '/adm/email?recordftf=query',
1.102     raeburn  1270: 		 "User Notes, Face-to-Face, Critical Messages, Broadcast Messages");
1.46      www      1271: # from query string
1.88      www      1272: 
1.46      www      1273:     if ($ENV{'form.recname'}) { $ENV{'form.recuname'}=$ENV{'form.recname'}; }
                   1274:     if ($ENV{'form.recdom'}) { $ENV{'form.recdomain'}=$ENV{'form.recdom'}; }
                   1275: 
1.44      www      1276:     my $defdom=$ENV{'user.domain'};
1.46      www      1277: # already filled in
1.44      www      1278:     if ($ENV{'form.recdomain'}) { $defdom=$ENV{'form.recdomain'}; }
1.46      www      1279: # generate output
1.44      www      1280:     my $domform = &Apache::loncommon::select_dom_form($defdom,'recdomain');
1.46      www      1281:     my $stdbrws = &Apache::loncommon::selectstudent_link
                   1282: 	('stdselect','recuname','recdomain');
1.88      www      1283:     my %lt=&Apache::lonlocal::texthash('user' => 'Username',
                   1284: 				       'dom' => 'Domain',
1.102     raeburn  1285: 				       'head' => 'User Notes, Records of Face-To-Face Discussions, Critical Messages, and Broadcast Messages in Course',
1.88      www      1286: 				       'subm' => 'Retrieve discussion and message records',
                   1287: 				       'newr' => 'New Record (record is visible to course faculty and staff)',
                   1288: 				       'post' => 'Post this Record');
1.44      www      1289:     $r->print(<<"ENDTREC");
1.88      www      1290: <h3>$lt{'head'}</h3>
1.46      www      1291: <form method="post" action="/adm/email" name="stdselect">
1.44      www      1292: <input type="hidden" name="recordftf" value="retrieve" />
                   1293: <table>
1.88      www      1294: <tr><td>$lt{'user'}:</td><td><input type="text" size="12" name="recuname" value="$ENV{'form.recuname'}" /></td>
1.44      www      1295: <td rowspan="2">
1.46      www      1296: $stdbrws
1.88      www      1297: <input type="submit" value="$lt{'subm'}" /></td>
1.44      www      1298: </tr>
1.88      www      1299: <tr><td>$lt{'dom'}:</td>
1.44      www      1300: <td>$domform</td></tr>
                   1301: </table>
                   1302: </form>
                   1303: ENDTREC
                   1304:     if (($stage ne 'query') &&
                   1305:         ($ENV{'form.recdomain'}) && ($ENV{'form.recuname'})) {
                   1306:         chomp($ENV{'form.newrecord'});
                   1307:         if ($ENV{'form.newrecord'}) {
1.45      www      1308:            &user_normal_msg_raw(
                   1309:             $ENV{'course.'.$ENV{'request.course.id'}.'.num'},
                   1310:             $ENV{'course.'.$ENV{'request.course.id'}.'.domain'},
1.88      www      1311:             &mt('Record').
                   1312: 	     ' ['.$ENV{'form.recuname'}.':'.$ENV{'form.recdomain'}.']',
1.45      www      1313: 	    $ENV{'form.newrecord'});
1.44      www      1314:         }
1.46      www      1315:         $r->print('<h3>'.&Apache::loncommon::plainname($ENV{'form.recuname'},
                   1316: 				     $ENV{'form.recdomain'}).'</h3>');
1.45      www      1317:         &disfacetoface($r,$ENV{'form.recuname'},$ENV{'form.recdomain'});
1.44      www      1318: 	$r->print(<<ENDRHEAD);
                   1319: <form method="post" action="/adm/email">
                   1320: <input name="recdomain" value="$ENV{'form.recdomain'}" type="hidden" />
                   1321: <input name="recuname" value="$ENV{'form.recuname'}" type="hidden" />
                   1322: ENDRHEAD
                   1323:         $r->print(<<ENDBFORM);
1.88      www      1324: <hr />$lt{'newr'}<br />
1.44      www      1325: <textarea name="newrecord" cols="80" rows="10" wrap="hard"></textarea>
1.45      www      1326: <br />
                   1327: <input type="hidden" name="recordftf" value="post" />
1.88      www      1328: <input type="submit" value="$lt{'post'}" />
1.44      www      1329: </form>
                   1330: ENDBFORM
                   1331:     }
                   1332: }
1.91      www      1333: 
1.101     raeburn  1334: # ----------------------------------------------------------- Blocking during exams
                   1335: 
                   1336: sub examblock {
                   1337:     my ($r,$action) = @_;
                   1338:     unless ($ENV{'request.course.id'}) { return;}
                   1339:     unless (&Apache::lonnet::allowed('srm',$ENV{'request.course.id'})) { $r->print('Not allowed'); }
                   1340:     my %lt=&Apache::lonlocal::texthash(
                   1341:             'comb' => 'Communication Blocking',
                   1342:             'cbds' => 'Communication blocking during scheduled exams',
                   1343:             'desc' => 'You can use communication blocking to prevent students enrolled in this course from displaying LON-CAPA messages sent by other students during an online exam. As blocking of communication could potentially interrupt legitimate communication between students who are also both enrolled in a different LON-CAPA course, please be careful that you select the correct start and end times for your scheduled exam when setting or modifying these parameters.',
                   1344:              'mecb' => 'Modify existing communication blocking periods',
                   1345:              'ncbc' => 'No communication blocks currently stored'
                   1346:     );
                   1347: 
                   1348:     my %ltext = &Apache::lonlocal::texthash(
                   1349:             'dura' => 'Duration',
                   1350:             'setb' => 'Set by',
                   1351:             'even' => 'Event',
                   1352:             'actn' => 'Action',
                   1353:             'star' => 'Start',
                   1354:             'endd' => 'End'
                   1355:     );
                   1356: 
                   1357:     &printheader($r,'/adm/email?block=display',$lt{'comb'});
                   1358:     $r->print('<h3>'.$lt{'cbds'}.'</h3>');
                   1359: 
                   1360:     if ($action eq 'store') {
                   1361:         &blockstore($r);
                   1362:     }
                   1363: 
                   1364:     $r->print($lt{'desc'}.'<br /><br />
                   1365:                <form name="blockform" method="post" action="/adm/email?block=store">
                   1366:              ');
                   1367: 
                   1368:     $r->print('<h4>'.$lt{'mecb'}.'</h4>');
                   1369:     my %records = ();
                   1370:     my $blockcount = 0;
                   1371:     my $parmcount = 0;
                   1372:     &get_blockdates(\%records,\$blockcount);
                   1373:     if ($blockcount > 0) {
                   1374:         $parmcount = &display_blocker_status($r,\%records,\%ltext);
                   1375:     } else {
                   1376:         $r->print($lt{'ncbc'}.'<br /><br />');
                   1377:     }
                   1378:     &display_addblocker_table($r,$parmcount,\%ltext);
                   1379:     $r->print(<<"END");
                   1380: <br />
                   1381: <input type="hidden" name="blocktotal" value="$blockcount" />
                   1382: <input type ="submit" value="Save Changes" />
                   1383: </form>
                   1384: </body>
                   1385: </html>
                   1386: END
                   1387:     return;
                   1388: }
                   1389: 
                   1390: sub blockstore {
                   1391:     my $r = shift;
                   1392:     my %lt=&Apache::lonlocal::texthash(
                   1393:             'tfcm' => 'The following changes were made',
                   1394:             'cbps' => 'communication blocking period(s)',
                   1395:             'werm' => 'was/were removed',
                   1396:             'wemo' => 'was/were modified',
                   1397:             'wead' => 'was/were added',
                   1398:             'ncwm' => 'No changes were made.' 
                   1399:     );
                   1400:     my %adds = ();
                   1401:     my %removals = ();
                   1402:     my %cancels = ();
                   1403:     my $modtotal = 0;
                   1404:     my $canceltotal = 0;
                   1405:     my $addtotal = 0;
                   1406:     my %blocking = ();
                   1407:     $r->print('<h3>'.$lt{'head'}.'</h3>');
                   1408:     foreach (keys %ENV) {
                   1409:         if ($_ =~ m/^form\.modify_(\w+)$/) {
                   1410:             $adds{$1} = $1;
                   1411:             $removals{$1} = $1;
                   1412:             $modtotal ++;
                   1413:         } elsif ($_ =~ m/^form\.cancel_(\d+)$/) {
                   1414:             $cancels{$1} = $1;
                   1415:             unless ( defined($removals{$1}) ) {
                   1416:                 $removals{$1} = $1;
                   1417:                 $canceltotal ++;
                   1418:             }
                   1419:         } elsif ($_ =~ m/^form\.add_(\d+)$/) {
                   1420:             $adds{$1} = $1;
                   1421:             $addtotal ++;
                   1422:         }
                   1423:     }
                   1424: 
                   1425:     foreach (keys %removals) {
                   1426:         my $hashkey = $ENV{'form.key_'.$_};
                   1427:         &Apache::lonnet::del('comm_block',["$hashkey"],
                   1428:                          $ENV{'course.'.$ENV{'request.course.id'}.'.domain'},
                   1429:                          $ENV{'course.'.$ENV{'request.course.id'}.'.num'}
                   1430:                          );
                   1431:     }
                   1432:     foreach (keys %adds) {
                   1433:         unless ( defined($cancels{$_}) ) {
                   1434:             my ($newstart,$newend) = &get_dates_from_form($_);
                   1435:             my $newkey = $newstart.'____'.$newend;
                   1436:             $blocking{$newkey} = $ENV{'user.name'}.'@'.$ENV{'user.domain'}.':'.$ENV{'form.title_'.$_};
                   1437:         }
                   1438:     }
                   1439:     if ($addtotal + $modtotal > 0) {
                   1440:         &Apache::lonnet::put('comm_block',\%blocking,
                   1441:                      $ENV{'course.'.$ENV{'request.course.id'}.'.domain'},
                   1442:                      $ENV{'course.'.$ENV{'request.course.id'}.'.num'}
                   1443:                      );
                   1444:     }
                   1445:     my $chgestotal = $canceltotal + $modtotal + $addtotal;
                   1446:     if ($chgestotal > 0) {
                   1447:         $r->print($lt{'tfcm'}.'<ul>');
                   1448:         if ($canceltotal > 0) {
                   1449:             $r->print('<li>'.$canceltotal.' '.$lt{'cbps'},' '.$lt{'werm'}.'</li>');
                   1450:         }
                   1451:         if ($modtotal > 0) {
                   1452:             $r->print('<li>'.$modtotal.' '.$lt{'cbps'},' '.$lt{'wemo'}.'</li>');
                   1453:         }
                   1454:         if ($addtotal > 0) {
                   1455:             $r->print('<li>'.$addtotal.' '.$lt{'cbps'},' '.$lt{'wead'}.'</li>');
                   1456:         }
                   1457:         $r->print('</ul>');
                   1458:     } else {
                   1459:         $r->print($lt{'ncwm'});
                   1460:     }
                   1461:     $r->print('<br />');
                   1462:     return;
                   1463: }
                   1464: 
                   1465: sub get_dates_from_form {
                   1466:     my $item = shift;
                   1467:     my $startdate = &Apache::lonhtmlcommon::get_date_from_form('startdate_'.$item);
                   1468:     my $enddate   = &Apache::lonhtmlcommon::get_date_from_form('enddate_'.$item);
                   1469:     return ($startdate,$enddate);
                   1470: }
                   1471: 
                   1472: sub get_blockdates {
                   1473:     my ($records,$blockcount) = @_;
                   1474:     $$blockcount = 0;
                   1475:     %{$records} = &Apache::lonnet::dump('comm_block',
                   1476:                          $ENV{'course.'.$ENV{'request.course.id'}.'.domain'},
                   1477:                          $ENV{'course.'.$ENV{'request.course.id'}.'.num'}
                   1478:                          );
                   1479:     $$blockcount = keys %{$records};
                   1480:                                                                                                              
                   1481:     foreach (keys %{$records}) {
                   1482:         if ($_ eq 'error: 2 tie(GDBM) Failed while attempting dump') {
                   1483:             $$blockcount = 0;
                   1484:             last;
                   1485:         }
                   1486:     }
                   1487: }
                   1488: 
                   1489: sub display_blocker_status {
                   1490:     my ($r,$records,$ltext) = @_;
                   1491:     my $parmcount = 0;
                   1492:     my @bgcols = ("#eeeeee","#dddddd");
                   1493:     my $function = &Apache::loncommon::get_users_function();
                   1494:     my $color = &Apache::loncommon::designparm($function.'.tabbg',
                   1495:                                                     $ENV{'user.domain'});
                   1496:     my %lt = &Apache::lonlocal::texthash(
                   1497:         'modi' => 'Modify',
                   1498:         'canc' => 'Cancel',
                   1499:     );
                   1500:     $r->print(<<"END");
                   1501: <table border="0" cellpadding="0" cellspacing="0">
                   1502:  <tr>
                   1503:   <td width="100%" bgcolor="#000000">
                   1504:    <table width="100%" border="0" cellpadding="1" cellspacing="0">
                   1505:     <tr>
                   1506:      <td width="100%" bgcolor="#000000">
                   1507:       <table border="0" cellpadding="3" cellspacing="3" bgcolor="#FFFFFF">
                   1508:        <tr bgcolor="$color">
                   1509:         <td><b>$$ltext{'dura'}</b></td>
                   1510:         <td><b>$$ltext{'setb'}</b></td>
                   1511:         <td><b>$$ltext{'even'}</b></td>
                   1512:         <td><b>$$ltext{'actn'}?</b></td>
                   1513:        </tr>
                   1514: END
                   1515:     foreach (sort keys %{$records}) {
                   1516:         my $iter = $parmcount%2;
                   1517:         my $onchange = 'onFocus="javascript:window.document.forms['.
                   1518:                        "'blockform'].elements['modify_".$parmcount."'].".
                   1519:                        'checked=true;"';
                   1520:         my ($start,$end) = split/____/,$_;
                   1521:         my $startform = &Apache::lonhtmlcommon::date_setter('blockform','startdate_'.$parmcount,$start,$onchange);
                   1522:         my $endform = &Apache::lonhtmlcommon::date_setter('blockform','enddate_'.$parmcount,$end,$onchange);
                   1523:         my ($setter,$title) = split/:/,$$records{$_};
                   1524:         my ($setuname,$setudom) = split/@/,$setter;
                   1525:         my $settername = &Apache::loncommon::plainname($setuname,$setudom);
                   1526:         $r->print(<<"END");
                   1527:        <tr bgcolor="$bgcols[$iter]">
                   1528:         <td>$$ltext{'star'}:&nbsp;$startform<br/>$$ltext{'endd'}:&nbsp;&nbsp;$endform</td>
                   1529:         <td>$settername</td>
                   1530:         <td><input type="text" name="title_$parmcount" size="15" value="$title"/><input type="hidden" name="key_$parmcount" value="$_"></td>
                   1531:         <td>$lt{'modi'}?&nbsp;<input type="checkbox" name="modify_$parmcount"/><br />$lt{'canc'}?&nbsp;&nbsp;<input type="checkbox" name="cancel_$parmcount"/>
                   1532:        </tr>
                   1533: END
                   1534:         $parmcount ++;
                   1535:     }
                   1536:     $r->print(<<"END");
                   1537:       </table>
                   1538:      </td>
                   1539:     </tr>
                   1540:    </table>
                   1541:   </td>
                   1542:  </tr>
                   1543: </table>
                   1544: <br />
                   1545: <br />
                   1546: END
                   1547:     return $parmcount;
                   1548: }
                   1549: 
                   1550: sub display_addblocker_table {
                   1551:     my ($r,$parmcount,$ltext) = @_;
                   1552:     my $start = time;
                   1553:     my $end = $start + (60 * 60 * 2); #Default is an exam of 2 hours duration.
                   1554:     my $onchange = 'onFocus="javascript:window.document.forms['.
                   1555:                    "'blockform'].elements['add_".$parmcount."'].".
                   1556:                    'checked=true;"';
                   1557:     my $startform = &Apache::lonhtmlcommon::date_setter('blockform','startdate_'.$parmcount,$start,$onchange);
                   1558:     my $endform = &Apache::lonhtmlcommon::date_setter('blockform','enddate_'.$parmcount,$end,$onchange);
                   1559:     my $function = &Apache::loncommon::get_users_function();
                   1560:     my $color = &Apache::loncommon::designparm($function.'.tabbg',
                   1561:                                                     $ENV{'user.domain'});
                   1562:     my %lt = &Apache::lonlocal::texthash(
                   1563:         'addb' => 'Add block',
                   1564:         'exam' => 'e.g., Exam 1',
                   1565:         'addn' => 'Add new communication blocking periods'
                   1566:     );
                   1567:     $r->print(<<"END");
                   1568: <h4>$lt{'addn'}</h4> 
                   1569: <table border="0" cellpadding="0" cellspacing="0">
                   1570:  <tr>
                   1571:   <td width="100%" bgcolor="#000000">
                   1572:    <table width="100%" border="0" cellpadding="1" cellspacing="0">
                   1573:     <tr>
                   1574:      <td width="100%" bgcolor="#000000">
                   1575:       <table border="0" cellpadding="3" cellspacing="3" bgcolor="#FFFFFF">
                   1576:        <tr bgcolor="#CCCCFF">
                   1577:         <td><b>$$ltext{'dura'}</b></td>
                   1578:         <td><b>$$ltext{'even'} $lt{'exam'}</b></td>
                   1579:         <td><b>$$ltext{'actn'}?</b></td>
                   1580:        </tr>
                   1581:        <tr bgcolor="#eeeeee">
                   1582:         <td>$$ltext{'star'}:&nbsp;$startform<br />$$ltext{'endd'}:&nbsp;&nbsp;$endform</td>
                   1583:         <td><input type="text" name="title_$parmcount" size="15" value=""/></td>
                   1584:         <td>$lt{'addb'}?&nbsp;<input type="checkbox" name="add_$parmcount" value="1"/></td>
                   1585:        </tr>
                   1586:       </table>
                   1587:      </td>
                   1588:     </tr>
                   1589:    </table>
                   1590:   </td>
                   1591:  </tr>
                   1592: </table>
                   1593: END
                   1594:     return;
                   1595: }
                   1596: 
                   1597: sub blockcheck {
                   1598:     my ($setters,$startblock,$endblock) = @_;
                   1599:     # Retrieve active student roles and active course coordinator/instructor roles
                   1600:     my @livecses = ();
                   1601:     my @staffcses = ();
                   1602:     $$startblock = 0;
                   1603:     $$endblock = 0;
                   1604:     foreach (keys %ENV) {
                   1605:         if ($_ =~ m-^user\.role\.(st|cc|in)\./(.+)$-) {
                   1606:             my $role = $1;
                   1607:             my $cse = $2;
                   1608:             $cse =~ s|/|_|;
                   1609:             if ($ENV{$_} =~ m/^(\d*)\.(\d*)$/) {
                   1610:                 unless (($2 > 0 && $2 < time) || ($1 > time)) {
                   1611:                     if ($role eq 'st') {
                   1612:                         push @livecses, $cse;
                   1613:                     } else {
                   1614:                         unless (grep/^$cse$/,@staffcses) {
                   1615:                             push @staffcses, $cse;
                   1616:                         }
                   1617:                     }
                   1618:                 }
                   1619:             }
                   1620:         } elsif ($_ =~ m-user\.role\.cr/(\w+)/(\w+)/([^/]+)\./(.+)$- ) { 
                   1621:             my $rolepriv = $ENV{'user.role..rolesdef_'.$3};
                   1622:         }
                   1623:     }
                   1624:     # Retrieve blocking times and identity of blocker for active courses for students.
                   1625:     if (@livecses > 0) {
                   1626:         foreach my $cse (@livecses) {
                   1627:             my ($cdom,$crs) = split/_/,$cse;
                   1628:             if ( (grep/^$cse$/,@staffcses) && ($ENV{'request.role'} !~ m-^st\./$cdom/$crs$-) ) {
                   1629:                 next;
                   1630:             } else {
                   1631:                 %{$$setters{$cse}} = ();
                   1632:                 @{$$setters{$cse}{'staff'}} = ();
                   1633:                 @{$$setters{$cse}{'times'}} = ();
                   1634:                 my %records = &Apache::lonnet::dump('comm_block',$cdom,$crs);
                   1635:                 foreach (keys %records) {
                   1636:                     if ($_ =~ m/^(\d+)____(\d+)$/) {
                   1637:                         if ($1 <= time && $2 >= time) {
                   1638:                             my ($staff,$title) = split/:/,$records{$_};
                   1639:                             push @{$$setters{$cse}{'staff'}}, $staff;
                   1640:                             push @{$$setters{$cse}{'times'}}, $_;
                   1641:                             if ( ($$startblock == 0) || ($$startblock > $1) ) {
                   1642:                                 $$startblock = $1;
                   1643:                             }
                   1644:                             if ( ($$endblock == 0) || ($$endblock < $2) ) {
                   1645:                                 $$endblock = $2;
                   1646:                             }
                   1647:                         }
                   1648:                     }
                   1649:                 }
                   1650:             }
                   1651:         }
                   1652:     }
                   1653: }
                   1654: 
                   1655: sub build_block_table {
                   1656:     my ($r,$startblock,$endblock,$setters) = @_;
                   1657:     my $function = &Apache::loncommon::get_users_function();
                   1658:     my $color = &Apache::loncommon::designparm($function.'.tabbg',
                   1659:                                                     $ENV{'user.domain'});
                   1660:     my %lt = &Apache::lonlocal::texthash(
                   1661:         'cacb' => 'Currently active communication blocks',
                   1662:         'cour' => 'Course',
                   1663:         'dura' => 'Duration',
                   1664:         'blse' => 'Block set by'
                   1665:     ); 
                   1666:     $r->print(<<"END");
                   1667: <br /<br />$lt{'cacb'}:<br /><br />
                   1668: <table border="0" cellpadding="0" cellspacing="0">
                   1669:  <tr>
                   1670:   <td width="100%" bgcolor="#000000">
                   1671:    <table width="100%" border="0" cellpadding="1" cellspacing="0">
                   1672:     <tr>
                   1673:      <td width="100%" bgcolor="#000000">
                   1674:       <table border="0" cellpadding="3" cellspacing="3" bgcolor="#FFFFFF">
                   1675:        <tr bgcolor="$color">
                   1676:         <td><b>$lt{'cour'}</b></td>
                   1677:         <td><b>$lt{'dura'}</b></td>
                   1678:         <td><b>$lt{'blse'}</b></td>
                   1679:        </tr>
                   1680: END
                   1681:     foreach (keys %{$setters}) {
                   1682:         my %courseinfo=&Apache::lonnet::coursedescription($_);
                   1683:         for (my $i=0; $i<@{$$setters{$_}{staff}}; $i++) {
                   1684:             my ($uname,$udom) = split/\@/,$$setters{$_}{staff}[$i];
                   1685:             my $fullname = &Apache::loncommon::plainname($uname,$udom);
                   1686:             my ($openblock,$closeblock) = split/____/,$$setters{$_}{times}[$i];
                   1687:             $openblock = &Apache::lonlocal::locallocaltime($openblock);
                   1688:             $closeblock= &Apache::lonlocal::locallocaltime($closeblock);
                   1689:             $r->print('<tr><td>'.$courseinfo{'description'}.'</td>'.
                   1690:                       '<td>'.$openblock.' to '.$closeblock.'</td>'.
                   1691:                       '<td>'.$fullname.' ('.$uname.'@'.$udom.
                   1692:                       ')</td></tr>');
                   1693:         }
                   1694:     }
                   1695:     $r->print('</table></td></tr></table></td></tr></table>');
                   1696: }
                   1697: 
1.90      www      1698: # ----------------------------------------------------------- Display a message
                   1699: 
                   1700: sub displaymessage {
1.106     www      1701:     my ($r,$msgid,$folder)=@_;
                   1702:     my $suffix=&foldersuffix($folder);
1.101     raeburn  1703:     my %blocked = ();
                   1704:     my %setters = ();
                   1705:     my $startblock = 0;
                   1706:     my $endblock = 0;
                   1707:     my $numblocked = 0;
                   1708: # info to generate "next" and "previous" buttons and check if message is blocked
                   1709:     &blockcheck(\%setters,\$startblock,\$endblock);
1.107     www      1710:     my @messages=&sortedmessages(\%blocked,$startblock,$endblock,\$numblocked,$folder);
1.101     raeburn  1711:     if ( $blocked{$msgid} eq 'ON' ) {
                   1712:         &printheader($r,'/adm/email',&mt('Display a Message'));
                   1713:         $r->print(&mt('You attempted to display a message that is currently blocked because you are enrolled in one or more courses for which there is an ongoing online exam.'));
                   1714:         &build_block_table($r,$startblock,$endblock,\%setters);
                   1715:         return;
                   1716:     }
1.107     www      1717:     &statuschange($msgid,'read',$folder);
1.106     www      1718:     my %message=&Apache::lonnet::get('nohist_email'.$suffix,[$msgid]);
1.90      www      1719:     my %content=&unpackagemsg($message{$msgid});
1.107     www      1720: 
1.90      www      1721:     my $counter=0;
                   1722:     $r->print('<pre>');
                   1723:     my $escmsgid=&Apache::lonnet::escape($msgid);
                   1724:     foreach (@messages) {
                   1725: 	if ($_->[5] eq $escmsgid){
                   1726: 	    last;
                   1727: 	}
                   1728: 	$counter++;
                   1729:     }
                   1730:     $r->print('</pre>');
                   1731:     my $number_of_messages = scalar(@messages); #subtract 1 for last index
                   1732: # start output
1.92      www      1733:     &printheader($r,'/adm/email?display='.&Apache::lonnet::escape($msgid),'Display a Message','',$content{'baseurl'});
1.90      www      1734:     my %courseinfo=&Apache::lonnet::coursedescription($content{'courseid'});
                   1735: # Functions
                   1736:     $r->print('<table border="2" width="100%"><tr bgcolor="#FFFFAA"><td>'.&mt('Functions').':</td>'.
                   1737: 	      '<td><a href="/adm/email?replyto='.&Apache::lonnet::escape($msgid).$sqs.
                   1738: 	      '"><b>'.&mt('Reply').'</b></a></td>'.
                   1739: 	      '<td><a href="/adm/email?forward='.&Apache::lonnet::escape($msgid).$sqs.
                   1740: 	      '"><b>'.&mt('Forward').'</b></a></td>'.
                   1741: 	      '<td><a href="/adm/email?markunread='.&Apache::lonnet::escape($msgid).$sqs.
                   1742: 	      '"><b>'.&mt('Mark Unread').'</b></a></td>'.
                   1743: 	      '<td><a href="/adm/email?markdel='.&Apache::lonnet::escape($msgid).$sqs.
                   1744: 	      '"><b>Delete</b></a></td>'.
1.125     www      1745: 	      '<td><a href="/adm/email?'.$sqs.
                   1746: 	      '"><b>'.&mt('Back to Folder Display').'</b></a></td>');
1.90      www      1747:     if ($counter > 0){
                   1748: 	$r->print('<td><a href="/adm/email?display='.$messages[$counter-1]->[5].$sqs.
                   1749: 		  '"><b>'.&mt('Previous').'</b></a></td>');
                   1750:     }
                   1751:     if ($counter < $number_of_messages - 1){
                   1752: 	$r->print('<td><a href="/adm/email?display='.$messages[$counter+1]->[5].$sqs.
                   1753: 		  '"><b>'.&mt('Next').'</b></a></td>');
                   1754:     }
                   1755:     $r->print('</tr></table>');
                   1756:     $r->print('<br /><b>'.&mt('Subject').':</b> '.$content{'subject'}.
1.108     www      1757: 	      ($folder ne 'sent'?'<br /><b>'.&mt('From').':</b> '.
1.90      www      1758: 	      &Apache::loncommon::aboutmewrapper(
                   1759: 						 &Apache::loncommon::plainname($content{'sendername'},$content{'senderdomain'}),
                   1760: 						 $content{'sendername'},$content{'senderdomain'}).' ('.
                   1761: 	      $content{'sendername'}.' at '.
1.108     www      1762: 	      $content{'senderdomain'}.') ':'<br /><b>'.&mt('To').':</b> '.
                   1763: 	      &Apache::loncommon::aboutmewrapper(
                   1764: 						 &Apache::loncommon::plainname($content{'recuser'},$content{'recdomain'}),
                   1765: 						 $content{'recuser'},$content{'recdomain'}).' ('.
                   1766: 	      $content{'recuser'}.' at '.
                   1767: 	      $content{'recdomain'}.') ').
1.90      www      1768: 	      ($content{'courseid'}?'<br /><b>'.&mt('Course').':</b> '.$courseinfo{'description'}.
                   1769: 	       ($content{'coursesec'}?' ('.&mt('Group/Section').': '.$content{'coursesec'}.')':''):'').
                   1770: 	      '<br /><b>'.&mt('Time').':</b> '.$content{'time'}.
1.115     www      1771: 	      ($content{'baseurl'}?'<br /><b>'.&mt('Refers to').':</b> <a href="'.$content{'baseurl'}.'">'.
                   1772: 	       $content{'baseurl'}.' ('.&Apache::lonnet::gettitle($content{'baseurl'}).')</a>':'').
1.90      www      1773: 	      '<p><pre>'.
                   1774: 	      &Apache::lontexconvert::msgtexconverted($content{'message'},1).
1.111     www      1775: 	      '</pre><hr />'.&displayresource(%content).'</p>');
1.90      www      1776:     return;   
                   1777: }
1.44      www      1778: 
1.111     www      1779: # =========================================================== Show the citation
                   1780: 
                   1781: sub displayresource {
                   1782:     my %content=@_;
                   1783: #
                   1784: # If the recipient is in the same course that the message was sent from and
                   1785: # has sufficient privileges, show "all details," else show citation
                   1786: #
                   1787:     if (($ENV{'request.course.id'} eq $content{'courseid'})
                   1788:      && (&Apache::lonnet::allowed('vgr',$content{'courseid'}))) {
                   1789: 	my $symb=&Apache::lonnet::symbread($content{'baseurl'});
                   1790: # Could not get a symb, give up
                   1791: 	unless ($symb) { return $content{'citation'}; }
                   1792: # Have a symb, can render
                   1793: 	return '<h2>'.&mt('Current attempts of student (if applicable)').'</h2>'.
                   1794: 	    &Apache::loncommon::get_previous_attempt($symb,
                   1795: 						     $content{'sendername'},
                   1796: 						     $content{'senderdomain'},
                   1797: 						     $content{'courseid'}).
                   1798: 	    '<hr /><h2>'.&mt('Current screen output (if applicable)').'</h2>'.
                   1799: 	    &Apache::loncommon::get_student_view($symb,
                   1800: 						 $content{'sendername'},
                   1801: 						 $content{'senderdomain'},
                   1802: 						 $content{'courseid'}).
                   1803: 	    '<h2>'.&mt('Correct Answer(s) (if applicable)').'</h2>'.
                   1804: 	    &Apache::loncommon::get_student_answers($symb,
                   1805: 						    $content{'sendername'},
                   1806: 						    $content{'senderdomain'},
                   1807: 						    $content{'courseid'});
                   1808:     } else {
                   1809: 	return $content{'citation'};
                   1810:     }
                   1811: }
                   1812: 
1.88      www      1813: # ================================================================== The Header
                   1814: 
                   1815: sub header {
1.90      www      1816:     my ($r,$title,$baseurl)=@_;
1.88      www      1817:     $r->print('<html><head><title>Communication and Messages</title>');
                   1818:     if ($baseurl) {
                   1819: 	$r->print("<base href=\"http://$ENV{'SERVER_NAME'}/$baseurl\" />");
                   1820:     }
                   1821:     $r->print(&Apache::loncommon::studentbrowser_javascript().'</head>'.
                   1822: 	      &Apache::loncommon::bodytag('Communication and Messages'));
                   1823:         $r->print(&Apache::lonhtmlcommon::breadcrumbs
1.90      www      1824:                   (undef,($title?$title:'Communication and Messages')));
1.88      www      1825: 
                   1826: }
                   1827: 
1.90      www      1828: # ---------------------------------------------------------------- Print header
                   1829: 
                   1830: sub printheader {
                   1831:     my ($r,$url,$desc,$title,$baseurl)=@_;
                   1832:     &Apache::lonhtmlcommon::add_breadcrumb
                   1833: 	({href=>$url,
                   1834: 	  text=>$desc});
                   1835:     &header($r,$title,$baseurl);
                   1836: }
                   1837: 
1.120     www      1838: # ------------------------------------------------------------ Store the comment
                   1839: 
                   1840: sub storecomment {
                   1841:     my ($r)=@_;
                   1842:     my $msgtxt=&Apache::lonfeedback::clear_out_html($ENV{'form.message'});
                   1843:     my $cleanmsgtxt='';
                   1844:     foreach (split(/[\n\r]/,$msgtxt)) {
                   1845: 	unless ($_=~/^\s*(\>|\&gt\;)/) {
                   1846: 	    $cleanmsgtxt.=$_."\n";
                   1847: 	}
                   1848:     }
                   1849:     my $key=&Apache::lonnet::escape($ENV{'form.baseurl'}).'___'.time;
                   1850:     &Apache::lonnet::put('nohist_stored_comments',{ $key => $cleanmsgtxt });
                   1851: }
                   1852: 
                   1853: sub storedcommentlisting {
                   1854:     my ($r)=@_;
                   1855:     my %msgs=&Apache::lonnet::dump('nohist_stored_comments',undef,undef,
                   1856:        '^'.&Apache::lonnet::escape(&Apache::lonnet::escape($ENV{'form.showcommentbaseurl'})));
                   1857:     $r->print('<html><body>');
                   1858:     if ((keys %msgs)[0]=~/^error\:/) {
                   1859: 	$r->print(&mt('No stored comments yet.'));
                   1860:     } else {
                   1861: 	my $found=0;
                   1862: 	foreach (sort keys %msgs) {
                   1863: 	    $r->print("\n".$msgs{$_}."<hr />");
                   1864: 	    $found=1;
                   1865: 	}
                   1866: 	unless ($found) {
                   1867: 	    $r->print(&mt('No stored comments yet for this resource.'));
                   1868: 	}
                   1869:     }
                   1870: }
                   1871: 
1.115     www      1872: # ---------------------------------------------------------------- Send an email
                   1873: 
                   1874: sub sendoffmail {
1.120     www      1875:     my ($r,$folder)=@_;
                   1876:     my $suffix=&foldersuffix($folder);
1.115     www      1877:     my $sendstatus='';
                   1878:     if ($ENV{'form.send'}) {
                   1879: 	&printheader($r,'','Messages being sent.');
                   1880: 	$r->rflush();
                   1881: 	my %content=();
                   1882: 	undef %content;
                   1883: 	if ($ENV{'form.forwid'}) {
                   1884: 	    my $msgid=$ENV{'form.forwid'};
1.120     www      1885: 	    my %message=&Apache::lonnet::get('nohist_email'.$suffix,[$msgid]);
1.115     www      1886: 	    %content=&unpackagemsg($message{$msgid},1);
1.120     www      1887: 	    &statuschange($msgid,'forwarded',$folder);
1.115     www      1888: 	    $ENV{'form.message'}.="\n\n-- Forwarded message --\n\n".
                   1889: 		$content{'message'};
                   1890: 	}
                   1891: 	if ($ENV{'form.replyid'}) {
                   1892: 	    my $msgid=$ENV{'form.replyid'};
1.120     www      1893: 	    my %message=&Apache::lonnet::get('nohist_email'.$suffix,[$msgid]);
1.115     www      1894: 	    %content=&unpackagemsg($message{$msgid},1);
1.120     www      1895: 	    &statuschange($msgid,'replied',$folder);
1.115     www      1896: 	}
                   1897: 	my %toaddr=();
                   1898: 	undef %toaddr;
                   1899: 	if ($ENV{'form.sendmode'} eq 'group') {
                   1900: 	    foreach (keys %ENV) {
                   1901: 		if ($_=~/^form\.send\_to\_\&\&\&[^\&]*\&\&\&\_(.+)$/) {
                   1902: 		    $toaddr{$1}='';
                   1903: 		}
                   1904: 	    }
                   1905: 	} elsif ($ENV{'form.sendmode'} eq 'upload') {
                   1906: 	    foreach (split(/[\n\r\f]+/,$ENV{'form.upfile'})) {
                   1907: 		my ($rec,$txt)=split(/\s*\:\s*/,$_);
                   1908: 		if ($txt) {
                   1909: 		    $rec=~s/\@/\:/;
                   1910: 		    $toaddr{$rec}.=$txt."\n";
                   1911: 		}
                   1912: 	    }
                   1913: 	} else {
                   1914: 	    $toaddr{$ENV{'form.recuname'}.':'.$ENV{'form.recdomain'}}='';
                   1915: 	}
                   1916: 	if ($ENV{'form.additionalrec'}) {
                   1917: 	    foreach (split(/\,/,$ENV{'form.additionalrec'})) {
                   1918: 		my ($auname,$audom)=split(/\@/,$_);
                   1919: 		$toaddr{$auname.':'.$audom}='';
                   1920: 	    }
                   1921: 	}
                   1922: 	
                   1923: 	foreach (keys %toaddr) {
                   1924: 	    my ($recuname,$recdomain)=split(/\:/,$_);
1.122     raeburn  1925:             my $msgtxt;
                   1926:             if ((($ENV{'form.critmsg'}) || ($ENV{'form.sendbck'})) &&
                   1927:                 (&Apache::lonnet::allowed('srm',$ENV{'request.course.id'}))) {
                   1928:                 $msgtxt=&Apache::lonfeedback::clear_out_html($ENV{'form.message'},1);
                   1929:             } else {  
                   1930: 	        $msgtxt=&Apache::lonfeedback::clear_out_html($ENV{'form.message'});
                   1931:             }
1.115     www      1932: 	    if ($toaddr{$_}) { $msgtxt.='<hr />'.$toaddr{$_}; }
                   1933: 	    my $thismsg;    
                   1934: 	    if ((($ENV{'form.critmsg'}) || ($ENV{'form.sendbck'})) && 
                   1935: 		(&Apache::lonnet::allowed('srm',$ENV{'request.course.id'}))) {
                   1936: 		$r->print(&mt('Sending critical message').' '.$recuname.'@'.$recdomain.': ');
                   1937: 		$thismsg=&user_crit_msg($recuname,$recdomain,
                   1938: 					&Apache::lonfeedback::clear_out_html($ENV{'form.subject'}),
                   1939: 					$msgtxt,
                   1940: 					$ENV{'form.sendbck'});
                   1941: 	    } else {
                   1942: 		$r->print(&mt('Sending').' '.$recuname.'@'.$recdomain.': ');
                   1943: 		$thismsg=&user_normal_msg($recuname,$recdomain,
                   1944: 					  &Apache::lonfeedback::clear_out_html($ENV{'form.subject'}),
                   1945: 					  $msgtxt,
                   1946: 					  $content{'citation'});
                   1947: 		if (($ENV{'request.course.id'}) && ($ENV{'form.sendmode'} eq 'group')) {
                   1948: 		    &user_normal_msg_raw(
                   1949: 					 $ENV{'course.'.$ENV{'request.course.id'}.'.num'},
                   1950: 					 $ENV{'course.'.$ENV{'request.course.id'}.'.domain'},
                   1951: 					 'Broadcast ['.$recuname.':'.$recdomain.']',
                   1952: 					 $msgtxt);
                   1953: 		}
                   1954: 	    }
                   1955: 	    $r->print($thismsg.'<br />');
                   1956: 	    $sendstatus.=' '.$thismsg;
                   1957: 	}
                   1958:     } else {
                   1959: 	&printheader($r,'','No messages sent.'); 
                   1960:     }
                   1961:     if ($sendstatus=~/^(\s*(?:ok|con_delayed)\s*)*$/) {
                   1962: 	$r->print('<br /><font color="green">'.&mt('Completed.').'</font>');
                   1963: 	if ($ENV{'form.displayedcrit'}) {
                   1964: 	    &discrit($r);
                   1965: 	} else {
                   1966: 	    &Apache::loncommunicate::menu($r);
                   1967: 	}
                   1968:     } else {
                   1969: 	$r->print(
                   1970: 		  '<h2><font color="red">'.&mt('Could not deliver message').'</font></h2>'.
                   1971: 		  &mt('Please use the browser "Back" button and correct the recipient addresses')
                   1972: 		  );
                   1973:     }
                   1974: }
1.90      www      1975: 
1.13      www      1976: # ===================================================================== Handler
                   1977: 
1.5       www      1978: sub handler {
                   1979:     my $r=shift;
                   1980: 
                   1981: # ----------------------------------------------------------- Set document type
1.87      www      1982:     
                   1983:     &Apache::loncommon::content_type($r,'text/html');
                   1984:     $r->send_http_header;
                   1985:     
                   1986:     return OK if $r->header_only;
                   1987:     
1.6       www      1988: # --------------------------- Get query string for limited number of parameters
1.32      matthew  1989:     &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'},
                   1990:         ['display','replyto','forward','markread','markdel','markunread',
1.44      www      1991:          'sendreply','compose','sendmail','critical','recname','recdom',
1.120     www      1992:          'recordftf','sortedby','block','folder','startdis','interdis',
                   1993: 	 'showcommentbaseurl']);
1.125     www      1994:     $sqs='&sortedby='.$ENV{'form.sortedby'};
1.108     www      1995: 
1.40      www      1996: # ------------------------------------------------------ They checked for email
1.101     raeburn  1997:     unless ($ENV{'form.block'}) {
                   1998:         &Apache::lonnet::put('email_status',{'recnewemail'=>0});
                   1999:     }
1.88      www      2000: 
                   2001: # ----------------------------------------------------------------- Breadcrumbs
                   2002: 
                   2003:     &Apache::lonhtmlcommon::clear_breadcrumbs();
                   2004:     &Apache::lonhtmlcommon::add_breadcrumb
                   2005:         ({href=>"/adm/communicate",
                   2006:           text=>"Communication/Messages",
                   2007:           faq=>12,bug=>'Communication Tools',});
                   2008: 
1.106     www      2009: # ------------------------------------------------------------------ Get Folder
                   2010: 
                   2011:     my $folder=$ENV{'form.folder'};
                   2012:     unless ($folder) { 
                   2013: 	$folder=''; 
                   2014:     } else {
1.125     www      2015: 	$sqs.='&folder='.&Apache::lonnet::escape($folder);
1.106     www      2016:     }
                   2017: 
1.108     www      2018: # --------------------------------------------------------------------- Display
                   2019: 
                   2020:     $startdis=$ENV{'form.startdis'};
1.118     www      2021:     $startdis--;
1.108     www      2022:     unless ($startdis) { $startdis=0; }
1.125     www      2023: 
1.108     www      2024:     $interdis=$ENV{'form.interdis'};
                   2025:     unless ($interdis) { $interdis=20; }
1.125     www      2026:     $sqs.='&interdis='.$interdis;
                   2027: 
1.117     www      2028:     if ($ENV{'form.firstview'}) {
                   2029: 	$startdis=0;
                   2030:     }
                   2031:     if ($ENV{'form.lastview'}) {
                   2032: 	$startdis=-1;
                   2033:     }
                   2034:     if ($ENV{'form.prevview'}) {
                   2035: 	$startdis--;
                   2036:     }
                   2037:     if ($ENV{'form.nextview'}) {
                   2038: 	$startdis++;
                   2039:     }
1.125     www      2040:     my $postedstartdis=$startdis+1;
                   2041:     $sqs.='&startdis='.$postedstartdis;
1.108     www      2042: 
1.5       www      2043: # --------------------------------------------------------------- Render Output
1.88      www      2044: 
1.87      www      2045:     if ($ENV{'form.display'}) {
1.106     www      2046: 	&displaymessage($r,$ENV{'form.display'},$folder);
1.87      www      2047:     } elsif ($ENV{'form.replyto'}) {
1.108     www      2048: 	&compout($r,'',$ENV{'form.replyto'},undef,undef,$folder);
1.87      www      2049:     } elsif ($ENV{'form.confirm'}) {
1.92      www      2050: 	&printheader($r,'','Confirmed Receipt');
1.87      www      2051: 	foreach (keys %ENV) {
                   2052: 	    if ($_=~/^form\.rec\_(.*)$/) {
1.92      www      2053: 		$r->print('<b>'.&mt('Confirming Receipt').':</b> '.
1.87      www      2054: 			  &user_crit_received($1).'<br>');
                   2055: 	    }
                   2056: 	    if ($_=~/^form\.reprec\_(.*)$/) {
                   2057: 		my $msgid=$1;
1.92      www      2058: 		$r->print('<b>'.&mt('Confirming Receipt').':</b> '.
1.87      www      2059: 			  &user_crit_received($msgid).'<br>');
1.94      www      2060: 		&compout($r,'','','',$msgid);
1.87      www      2061: 	    }
                   2062: 	}
                   2063: 	&discrit($r);
                   2064:     } elsif ($ENV{'form.critical'}) {
1.92      www      2065: 	&printheader($r,'','Displaying Critical Messages');
1.87      www      2066: 	&discrit($r);
                   2067:     } elsif ($ENV{'form.forward'}) {
1.121     www      2068: 	&compout($r,$ENV{'form.forward'},undef,undef,undef,$folder);
1.87      www      2069:     } elsif ($ENV{'form.markdel'}) {
1.92      www      2070: 	&printheader($r,'','Deleted Message');
1.106     www      2071: 	&statuschange($ENV{'form.markdel'},'deleted',$folder);
1.120     www      2072: 	&Apache::loncommunicate::menu($r);
1.106     www      2073: 	&disall($r,$folder);
                   2074:     } elsif ($ENV{'form.markedmove'}) {
                   2075: 	my $total=0;
                   2076: 	foreach (keys %ENV) {
                   2077: 	    if ($_=~/^form\.delmark_(.*)$/) {
                   2078: 		&movemsg(&Apache::lonnet::unescape($1),$folder,
                   2079: 			 $ENV{'form.movetofolder'});
                   2080: 		$total++;
                   2081: 	    }
                   2082: 	}
                   2083: 	&printheader($r,'','Moved Messages');
                   2084: 	$r->print('Moved '.$total.' message(s)<p>');
1.120     www      2085: 	&Apache::loncommunicate::menu($r);
1.106     www      2086: 	&disall($r,$folder);
1.87      www      2087:     } elsif ($ENV{'form.markeddel'}) {
                   2088: 	my $total=0;
                   2089: 	foreach (keys %ENV) {
                   2090: 	    if ($_=~/^form\.delmark_(.*)$/) {
1.108     www      2091: 		&statuschange(&Apache::lonnet::unescape($1),'deleted',$folder);
1.87      www      2092: 		$total++;
                   2093: 	    }
                   2094: 	}
1.92      www      2095: 	&printheader($r,'','Deleted Messages');
1.87      www      2096: 	$r->print('Deleted '.$total.' message(s)<p>');
1.120     www      2097: 	&Apache::loncommunicate::menu($r);
1.106     www      2098: 	&disall($r,$folder);
1.87      www      2099:     } elsif ($ENV{'form.markunread'}) {
1.92      www      2100: 	&printheader($r,'','Marked Message as Unread');
1.87      www      2101: 	&statuschange($ENV{'form.markunread'},'new');
1.120     www      2102: 	&Apache::loncommunicate::menu($r);
1.106     www      2103: 	&disall($r,$folder);
1.87      www      2104:     } elsif ($ENV{'form.compose'}) {
1.92      www      2105: 	&compout($r,'','',$ENV{'form.compose'});
1.87      www      2106:     } elsif ($ENV{'form.recordftf'}) {
                   2107: 	&facetoface($r,$ENV{'form.recordftf'});
1.101     raeburn  2108:     } elsif ($ENV{'form.block'}) {
                   2109:         &examblock($r,$ENV{'form.block'});
1.87      www      2110:     } elsif ($ENV{'form.sendmail'}) {
1.120     www      2111: 	&sendoffmail($r,$folder);
                   2112: 	if ($ENV{'form.storebasecomment'}) {
                   2113: 	    &storecomment($r);
                   2114: 	}
                   2115: 	&disall($r,$folder);
1.106     www      2116:     } elsif ($ENV{'form.newfolder'}) {
                   2117: 	&printheader($r,'','New Folder');
                   2118: 	&makefolder($ENV{'form.newfolder'});
1.120     www      2119: 	&Apache::loncommunicate::menu($r);
1.106     www      2120: 	&disall($r,$ENV{'form.newfolder'});
1.120     www      2121:     } elsif ($ENV{'form.showcommentbaseurl'}) {
                   2122: 	&storedcommentlisting($r);
1.87      www      2123:     } else {
1.92      www      2124: 	&printheader($r,'','Display All Messages');
1.120     www      2125: 	&Apache::loncommunicate::menu($r);
1.106     www      2126: 	&disall($r,$folder);
1.87      www      2127:     }
                   2128:     $r->print('</body></html>');
                   2129:     return OK;
1.5       www      2130: }
1.2       www      2131: # ================================================= Main program, reset counter
                   2132: 
1.27      www      2133: BEGIN {
1.2       www      2134:     $msgcount=0;
1.1       www      2135: }
1.58      bowersj2 2136: 
                   2137: =pod
                   2138: 
                   2139: =back
                   2140: 
1.59      bowersj2 2141: =cut
                   2142: 
                   2143: 1; 
1.1       www      2144: 
                   2145: __END__
                   2146: 
                   2147: 
                   2148: 
                   2149: 
                   2150: 
                   2151: 
                   2152: 

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