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

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

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