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

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

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