Annotation of loncom/interface/lonmsgdisplay.pm, revision 1.34

1.1       albertel    1: # The LearningOnline Network with CAPA
                      2: # Routines for messaging display
                      3: #
1.34    ! albertel    4: # $Id: lonmsgdisplay.pm,v 1.33 2006/06/27 18:34:43 albertel Exp $
1.1       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/
                     27: #
                     28: 
                     29: 
                     30: package Apache::lonmsgdisplay;
                     31: 
                     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: 
                     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: 
                     89: =back
                     90: 
                     91: Users can ask LON-CAPA to forward messages to conventional e-mail
                     92: addresses on their B<PREF> screen, but generally, LON-CAPA messages
                     93: are much more useful than traditional email can be made to be, even
                     94: with HTML support.
                     95: 
                     96: Right now, this document will cover just how to send a message, since
                     97: it is likely you will not need to programmatically read messages,
                     98: since lonmsg already implements that functionality.
                     99: 
                    100: 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
                    108: Domain Coordinator e-mail for the storage of information about 
                    109: recipients of the message/e-mail.
                    110: 
                    111: =head1 FUNCTIONS
                    112: 
                    113: =over 4
                    114: 
                    115: =cut
                    116: 
                    117: use strict;
                    118: use Apache::lonnet;
                    119: use HTML::TokeParser();
                    120: use Apache::Constants qw(:common);
                    121: use Apache::loncommon();
                    122: use Apache::lontexconvert();
                    123: use HTML::Entities();
                    124: use Apache::lonlocal;
                    125: use Apache::loncommunicate;
                    126: use Apache::lonfeedback;
                    127: use Apache::lonrss();
1.27      albertel  128: use Apache::lonselstudent();
1.29      www       129: use lib '/home/httpd/lib/perl/';
                    130: use LONCAPA;
1.1       albertel  131: 
                    132: # Querystring component with sorting type
                    133: my $sqs;
                    134: my $startdis;
                    135: my $interdis;
                    136: 
                    137: # ============================================================ List all folders
                    138: 
                    139: sub folderlist {
                    140:     my $folder=shift;
                    141:     my @allfolders=&Apache::lonnet::getkeys('email_folders');
                    142:     if ($allfolders[0]=~/^error:/) { @allfolders=(); }
                    143:     return '<form method="post" action="/adm/email">'.
                    144: 	&mt('Folder').': '.
                    145: 	&Apache::loncommon::select_form($folder,'folder',
                    146: 			     ('' => &mt('INBOX'),'trash' => &mt('TRASH'),
                    147: 			      'new' => &mt('New Messages Only'),
                    148:                               'critical' => &mt('Critical'),
                    149: 			      'sent' => &mt('Sent Messages'),
                    150: 			      map { $_ => $_ } @allfolders)).
                    151: 			      ' '.&mt('Show').
                    152: 			      '<select name="interdis">'.
                    153: 			      join("\n",map { '<option value="'.$_.'"'.
                    154: 	 ($_==$interdis?' selected="selected"':'').'>'.$_.'</option>' }
                    155: 				   (10,20,50,100,200)).'</select>'.	
                    156:    '<input type="submit" value="'.&mt('View Folder').'" /><br />'.
                    157:     '<input type="hidden" name="sortedby" value="'.$env{'form.sortedby'}.'" />'.
                    158: 			      ($folder=~/^(new|critical)/?'</form>':'');
                    159: }
                    160: 
                    161: sub scrollbuttons {
                    162:     my ($start,$maxdis,$first,$finish,$total)=@_;
                    163:     unless ($total>0) { return ''; }
                    164:     $start++; $maxdis++;$first++;$finish++;
                    165:     return
                    166:    &mt('Page').': '. 
                    167:    '<input type="submit" name="firstview" value="'.&mt('First').'" />'.
                    168:    '<input type="submit" name="prevview" value="'.&mt('Previous').'" />'.
                    169:    '<input type="text" size="5" name="startdis" value="'.$start.'" onChange="this.form.submit()" /> of '.$maxdis.
                    170:    '<input type="submit" name="nextview" value="'.&mt('Next').'" />'.
                    171:    '<input type="submit" name="lastview" value="'.&mt('Last').'" /><br />'.
                    172:    &mt('Showing messages [_1] through [_2] of [_3]',$first,$finish,$total).'</form>';
                    173: }
                    174: # =============================================================== Status Change
                    175: 
                    176: sub statuschange {
                    177:     my ($msgid,$newstatus,$folder)=@_;
1.3       albertel  178:     my $suffix=&Apache::lonmsg::foldersuffix($folder);
1.1       albertel  179:     my %status=&Apache::lonnet::get('email_status'.$suffix,[$msgid]);
                    180:     if ($status{$msgid}=~/^error\:/) { $status{$msgid}=''; }
                    181:     unless ($status{$msgid}) { $status{$msgid}='new'; }
                    182:     unless (($status{$msgid} eq 'replied') || 
                    183:             ($status{$msgid} eq 'forwarded')) {
                    184: 	&Apache::lonnet::put('email_status'.$suffix,{$msgid => $newstatus});
                    185:     }
                    186:     if (($newstatus eq 'deleted') || ($newstatus eq 'new')) {
                    187: 	&Apache::lonnet::put('email_status'.$suffix,{$msgid => $newstatus});
                    188:     }
                    189:     if ($newstatus eq 'deleted') {
1.9       albertel  190: 	return &movemsg($msgid,$folder,'trash');
                    191:     }
                    192:     return ;
1.1       albertel  193: }
                    194: 
                    195: # ============================================================= Make new folder
                    196: 
                    197: sub makefolder {
                    198:     my ($newfolder)=@_;
                    199:     if (($newfolder eq 'sent')
                    200:      || ($newfolder eq 'critical')
                    201:      || ($newfolder eq 'trash')
                    202:      || ($newfolder eq 'new')) { return; }
                    203:     &Apache::lonnet::put('email_folders',{$newfolder => time});
                    204: }
                    205: 
                    206: # ======================================================== Move between folders
                    207: 
                    208: sub movemsg {
                    209:     my ($msgid,$srcfolder,$trgfolder)=@_;
                    210:     if ($srcfolder eq 'new') { $srcfolder=''; }
1.3       albertel  211:     my $srcsuffix=&Apache::lonmsg::foldersuffix($srcfolder);
                    212:     my $trgsuffix=&Apache::lonmsg::foldersuffix($trgfolder);
1.9       albertel  213:     if ($srcsuffix eq $trgsuffix) {
                    214: 	return (0,&mt('Message not moved, Attempted to move message to the same folder as it already is in.'));
                    215:     }
1.1       albertel  216: 
                    217: # Copy message
                    218:     my %message=&Apache::lonnet::get('nohist_email'.$srcsuffix,[$msgid]);
1.9       albertel  219:     if (!exists($message{$msgid}) || $message{$msgid} eq '') {
1.32      albertel  220: 	if (&Apache::lonnet::error(%message)) {
1.9       albertel  221: 	    return (0,&mt('Message not moved, A network error occurred.'));
                    222: 	} else {
                    223: 	    return (0,&mt('Message not moved as the message is no longer in the source folder.'));
                    224: 	}
                    225:     }
                    226: 
                    227:     my $result =&Apache::lonnet::put('nohist_email'.$trgsuffix,
                    228: 				     {$msgid => $message{$msgid}});
1.32      albertel  229:     if (&Apache::lonnet::error($result)) {
1.9       albertel  230: 	return (0,&mt('Message not moved, A network error occurred.'));
                    231:     }
1.1       albertel  232: 
                    233: # Copy status
                    234:     unless ($trgfolder eq 'trash') {
1.9       albertel  235:        	my %status=&Apache::lonnet::get('email_status'.$srcsuffix,[$msgid]);
                    236: 	# a non-existant status is the mark of an unread msg
1.32      albertel  237: 	if (&Apache::lonnet::error(%status)) {
1.9       albertel  238: 	    return (0,&mt('Message copied to new folder but status was not, A network error occurred.'));
                    239: 	}
                    240: 	my $result=&Apache::lonnet::put('email_status'.$trgsuffix,
                    241: 					{$msgid => $status{$msgid}});
1.32      albertel  242: 	if (&Apache::lonnet::error($result)) {
1.9       albertel  243: 	    return (0,&mt('Message copied to new folder but status was not, A network error occurred.'));
                    244: 	}
1.1       albertel  245:     }
1.9       albertel  246: 
1.1       albertel  247: # Delete orginals
1.9       albertel  248:     my $result_del_msg = 
                    249: 	&Apache::lonnet::del('nohist_email'.$srcsuffix,[$msgid]);
                    250:     my $result_del_stat =
                    251: 	&Apache::lonnet::del('email_status'.$srcsuffix,[$msgid]);
1.32      albertel  252:     if (&Apache::lonnet::error($result_del_msg)) {
1.9       albertel  253: 	return (0,&mt('Message copied, but unable to delete the original from the source folder.'));
                    254:     }
1.32      albertel  255:     if (&Apache::lonnet::error($result_del_stat)) {
1.9       albertel  256: 	return (0,&mt('Message copied, but unable to delete the original status from the source folder.'));
                    257:     }
                    258: 
                    259:     return (1);
1.1       albertel  260: }
                    261: 
                    262: # ======================================================= Display a course list
                    263: 
                    264: sub discourse {
1.25      foxr      265:     my $result;
                    266:     my ($course_personnel,
                    267: 	$current_members,
                    268: 	$expired_members,
1.28      foxr      269: 	$future_members) = 
                    270: 	    &Apache::lonselstudent::get_people_in_class($env{'request.course.sec'});
1.25      foxr      271:     unshift @$current_members, (@$course_personnel);
                    272:     my %defaultUsers;
                    273: 
                    274:     $result = &Apache::lonselstudent::render_student_list($current_members,
                    275: 							  "compemail",
                    276: 							  "current",
                    277: 							  \%defaultUsers,
                    278: 							  1,"selectedusers",1);
                    279: 
1.28      foxr      280:     $result .= &Apache::lonselstudent::render_student_list($expired_members,
                    281: 							   "compemail",
                    282: 							   "expired",
                    283: 							   \%defaultUsers,
                    284: 							   1, "selectedusers",0);
                    285:     $result .= &Apache::lonselstudent::render_student_list($future_members,
                    286: 							   "compemail",
                    287: 							   "future",
                    288: 							   \%defaultUsers,
                    289: 							   1, "selectedusers", 0);
1.25      foxr      290:     return $result;
                    291: }
                    292: 
1.1       albertel  293: # ==================================================== Display Critical Message
                    294: 
                    295: sub discrit {
                    296:     my $r=shift;
1.5       albertel  297:     my $header = '<h1><font color="red">'.&mt('Critical Messages').'</font></h1>'.
1.1       albertel  298:         '<form action="/adm/email" method="POST">'.
                    299:         '<input type="hidden" name="confirm" value="true" />';
                    300:     my %what=&Apache::lonnet::dump('critical');
                    301:     my $result = '';
                    302:     foreach (sort keys %what) {
                    303:         my %content=&Apache::lonmsg::unpackagemsg($what{$_});
                    304:         next if ($content{'senderdomain'} eq '');
                    305:         $result.='<hr />'.&mt('From').': <b>'.
                    306: &Apache::loncommon::aboutmewrapper(
                    307:  &Apache::loncommon::plainname($content{'sendername'},$content{'senderdomain'}),$content{'sendername'},$content{'senderdomain'}).'</b> ('.
1.14      albertel  308: $content{'sendername'}.':'.
1.1       albertel  309:             $content{'senderdomain'}.') '.$content{'time'}.
                    310:             '<br />'.&mt('Subject').': '.$content{'subject'}.
                    311:             '<br /><pre>'.
                    312:               &Apache::lontexconvert::msgtexconverted($content{'message'}).
                    313:             '</pre><small>'.
                    314: &mt('You have to confirm that you received this message. After confirmation, this message will be moved to your regular inbox').
                    315:             '</small><br />'.
                    316:             '<input type="submit" name="rec_'.$_.'" value="'.&mt('Confirm Receipt').'" />'.
                    317:             '<input type="submit" name="reprec_'.$_.'" '.
                    318:                   'value="'.&mt('Confirm Receipt and Reply').'" />';
                    319:     }
                    320:     # Check to see if there were any messages.
                    321:     if ($result eq '') {
                    322:         $result = "<h2>".&mt('You have no critical messages.')."</h2>".
1.30      raeburn   323: 	    '<a href="/adm/roles">'.&mt('Select a course or group').'</a><br />'.
1.1       albertel  324:             '<a href="/adm/email">'.&mt('Communicate').'</a>';
                    325:     } else {
                    326:         $r->print($header);
                    327:     }
                    328:     $r->print($result);
                    329:     $r->print('<input type="hidden" name="displayedcrit" value="true" /></form>');
                    330: }
                    331: 
                    332: sub sortedmessages {
                    333:     my ($blocked,$startblock,$endblock,$numblocked,$folder) = @_;
                    334:     my $suffix=&Apache::lonmsg::foldersuffix($folder);
                    335:     my @messages = &Apache::lonnet::getkeys('nohist_email'.$suffix);
                    336:     #unpack the varibles and repack into temp for sorting
                    337:     my @temp;
                    338:     my %descriptions;
                    339:     my %status_cache = 
                    340: 	&Apache::lonnet::get('email_status'.&Apache::lonmsg::foldersuffix($folder),\@messages);
1.11      albertel  341: 
                    342:     my $get_received;
                    343:     if ($folder eq 'sent' 
                    344: 	&& ($env{'form.sortedby'} =~ m/^(rev)?(user|domain)$/)) {
                    345: 	$get_received = 1;
                    346:     }
                    347: 
                    348:     foreach my $msgid (@messages) {
1.29      www       349: 	my $esc_msgid=&escape($msgid);
1.1       albertel  350: 	my ($sendtime,$shortsubj,$fromname,$fromdomain,$status,$fromcid)=
1.11      albertel  351: 	    &Apache::lonmsg::unpackmsgid($esc_msgid,$folder,undef,
1.1       albertel  352: 					 \%status_cache);
                    353:         my $description = &get_course_desc($fromcid,\%descriptions);
                    354: 	my @temp1 = ($sendtime,$shortsubj,$fromname,$fromdomain,$status,
1.11      albertel  355: 		     $esc_msgid,$description);
                    356: 	if ($get_received) {
                    357: 	    my %message = &Apache::lonnet::get('nohist_email'.$suffix,
                    358: 					       [$msgid]);
                    359: 	    my %content = &Apache::lonmsg::unpackagemsg($message{$msgid});
                    360: 	    push(@temp1,$content{'recuser'},$content{'recdomain'});
                    361: 	}
1.1       albertel  362:         # Check whether message was sent during blocking period.
                    363:         if ($sendtime >= $startblock && ($sendtime <= $endblock && $endblock > 0) ) {
1.11      albertel  364:             $$blocked{$msgid} = 'ON';
1.1       albertel  365:             $$numblocked ++;
                    366:         } else { 
                    367:             push @temp ,\@temp1;
                    368:         }
                    369:     }
                    370:     #default sort
                    371:     @temp = sort  {$a->[0] <=> $b->[0]} @temp;    
                    372:     if ($env{'form.sortedby'} eq "date"){
                    373:         @temp = sort  {$a->[0] <=> $b->[0]} @temp;    
                    374:     }
                    375:     if ($env{'form.sortedby'} eq "revdate"){
                    376:     	@temp = sort  {$b->[0] <=> $a->[0]} @temp; 
                    377:     }
                    378:     if ($env{'form.sortedby'} eq "user"){
1.11      albertel  379: 	if ($get_received) {
                    380: 	    @temp = sort  {lc($a->[7][0]) cmp lc($b->[7][0])} @temp;
                    381: 	} else {
                    382: 	    @temp = sort  {lc($a->[2])    cmp lc($b->[2])}    @temp;
                    383: 	}
1.1       albertel  384:     }
                    385:     if ($env{'form.sortedby'} eq "revuser"){
1.11      albertel  386: 	if ($get_received) {
                    387: 	    @temp = sort  {lc($b->[7][0]) cmp lc($a->[7][0])} @temp;
                    388: 	} else {
                    389: 	    @temp = sort  {lc($b->[2])    cmp lc($a->[2])}    @temp;
                    390: 	}
1.1       albertel  391:     }
                    392:     if ($env{'form.sortedby'} eq "domain"){
1.11      albertel  393: 	if ($get_received) {
                    394: 	    @temp = sort  {$a->[8][0] cmp $b->[8][0]} @temp;
                    395: 	} else {
                    396: 	    @temp = sort  {$a->[3]    cmp $b->[3]}    @temp;
                    397: 	}
1.1       albertel  398:     }
                    399:     if ($env{'form.sortedby'} eq "revdomain"){
1.11      albertel  400: 	if ($get_received) {
                    401: 	    @temp = sort  {$b->[8][0] cmp $a->[8][0]} @temp;
                    402: 	} else {
                    403: 	    @temp = sort  {$b->[3]    cmp $a->[3]}    @temp;
                    404: 	}
1.1       albertel  405:     }
                    406:     if ($env{'form.sortedby'} eq "subject"){
                    407:         @temp = sort  {lc($a->[1]) cmp lc($b->[1])} @temp;
                    408:     }
                    409:     if ($env{'form.sortedby'} eq "revsubject"){
                    410:         @temp = sort  {lc($b->[1]) cmp lc($a->[1])} @temp;
                    411:     }
                    412:     if ($env{'form.sortedby'} eq "course"){
                    413:         @temp = sort  {lc($a->[6]) cmp lc($b->[6])} @temp;
                    414:     }
                    415:     if ($env{'form.sortedby'} eq "revcourse"){
                    416:         @temp = sort  {lc($b->[6]) cmp lc($a->[6])} @temp;
                    417:     }
                    418:     if ($env{'form.sortedby'} eq "status"){
                    419:         @temp = sort  {$a->[4] cmp $b->[4]} @temp;
                    420:     }
                    421:     if ($env{'form.sortedby'} eq "revstatus"){
                    422:         @temp = sort  {$b->[4] cmp $a->[4]} @temp;
                    423:     }
                    424:     return @temp;
                    425: }
                    426: 
                    427: sub get_course_desc {
                    428:     my ($fromcid,$descriptions) = @_;
                    429:     my $description;
                    430:     if (!$fromcid) {
                    431:         return $description;
                    432:     } else {
                    433:         if (defined($$descriptions{$fromcid})) {
                    434:             $description = $$descriptions{$fromcid};
                    435:         } else {
                    436:             if (defined($env{'course.'.$fromcid.'.description'})) {
                    437:                 $description = $env{'course.'.$fromcid.'.description'};
                    438:             } else {
                    439:                 my %courseinfo=&Apache::lonnet::coursedescription($fromcid);                $description = $courseinfo{'description'};
                    440:                 $description = $courseinfo{'description'};
                    441:             }
                    442:             $$descriptions{$fromcid} = $description;
                    443:         }
                    444:         return $description;
                    445:     }
                    446: }
                    447: 
                    448: # ======================================================== Display new messages
                    449: 
                    450: 
                    451: sub disnew {
                    452:     my $r=shift;
                    453:     my %lt=&Apache::lonlocal::texthash(
                    454: 				       'nm' => 'New Messages',
                    455: 				       'su' => 'Subject',
1.30      raeburn   456:                                        'co' => 'Course/Group',
1.1       albertel  457: 				       'da' => 'Date',
                    458: 				       'us' => 'Username',
                    459: 				       'op' => 'Open',
                    460: 				       'do' => 'Domain'
                    461: 				       );
                    462:     my @msgids = sort(&Apache::lonnet::getkeys('nohist_email'));
                    463:     my @newmsgs;
                    464:     my %setters = ();
                    465:     my $startblock = 0;
                    466:     my $endblock = 0;
                    467:     my %blocked = ();
                    468:     my $numblocked = 0;
                    469:     # Check for blocking of display because of scheduled online exams.
                    470:     &blockcheck(\%setters,\$startblock,\$endblock);
                    471:     my %status_cache = 
                    472: 	&Apache::lonnet::get('email_status',\@msgids);
                    473:     my %descriptions;
                    474:     foreach (@msgids) {
1.29      www       475: 	my $msgid=&escape($_);
1.1       albertel  476:         my ($sendtime,$shortsubj,$fromname,$fromdom,$status,$fromcid)=
                    477: 	    &Apache::lonmsg::unpackmsgid($msgid,undef,undef,\%status_cache);
                    478:         if (defined($sendtime) && $sendtime!~/error/) {
                    479:             my $description = &get_course_desc($fromcid,\%descriptions);
                    480:             my $numsendtime = $sendtime;
                    481:             $sendtime = &Apache::lonlocal::locallocaltime($sendtime);
                    482:             if ($status eq 'new') {
                    483:                 if ($numsendtime >= $startblock && ($numsendtime <= $endblock && $endblock > 0) ) {
                    484:                     $blocked{$_} = 'ON';
                    485:                     $numblocked ++;
                    486:                 } else {
                    487:                     push @newmsgs, { 
                    488:                         msgid    => $msgid,
                    489:                         sendtime => $sendtime,
1.8       albertel  490:                         shortsub => $shortsubj,
1.1       albertel  491:                         from     => $fromname,
                    492:                         fromdom  => $fromdom,
                    493:                         course   => $description 
                    494:                         }
                    495:                 }
                    496:             }
                    497:         }
                    498:     }
                    499:     if ($#newmsgs >= 0) {
                    500:         $r->print(<<TABLEHEAD);
                    501: <h2>$lt{'nm'}</h2>
1.4       albertel  502: <table class="LC_mail_list"><tr><th>&nbsp</th>
1.1       albertel  503: <th>$lt{'da'}</th><th>$lt{'us'}</th><th>$lt{'do'}</th><th>$lt{'su'}</th><th>$lt{'co'}</th></tr>
                    504: TABLEHEAD
                    505:         foreach my $msg (@newmsgs) {
                    506:             $r->print(<<"ENDLINK");
1.4       albertel  507: <tr class="LC_mail_new">
1.1       albertel  508: <td><a href="/adm/email?dismode=new&display=$msg->{'msgid'}">$lt{'op'}</a></td>
                    509: ENDLINK
                    510:             foreach ('sendtime','from','fromdom','shortsub','course') {
                    511:                 $r->print("<td>$msg->{$_}</td>");
                    512:             }
                    513:             $r->print("</td></tr>");
                    514:         }
                    515:         $r->print('</table>');
                    516:     } elsif ($numblocked == 0) {
                    517:         $r->print("<h3>".&mt('You have no unread messages')."</h3>");
                    518:     }
                    519:     if ($numblocked > 0) {
                    520:         my $beginblock = &Apache::lonlocal::locallocaltime($startblock);
                    521:         my $finishblock = &Apache::lonlocal::locallocaltime($endblock);
                    522:         if ($numblocked == 1) {
                    523:             $r->print("<h3>".&mt('You have').' '.$numblocked.' '.&mt('blocked unread message').".</h3>");
                    524:             $r->print(&mt('This message is not viewable because').' ');
                    525:         } else {
                    526:             $r->print("<h3>".&mt('You have').' '.$numblocked.' '.&mt('blocked unread messages').".</h3>");
                    527:             $r->print(&mt('These').' '.$numblocked.' '.&mt('messages are not viewable because '));
                    528:         }
                    529:         $r->print(
                    530: &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').'.');
                    531:         &build_block_table($r,$startblock,$endblock,\%setters);
                    532:     }
                    533: }
                    534: 
                    535: 
                    536: # ======================================================== Display all messages
                    537: 
                    538: sub disall {
                    539:     my ($r,$folder)=@_;
                    540:     $r->print(&folderlist($folder));
                    541:     if ($folder eq 'new') {
                    542: 	&disnew($r);
                    543:     } elsif ($folder eq 'critical') {
                    544: 	&discrit($r);
                    545:     } else {
                    546: 	&disfolder($r,$folder);
                    547:     }
                    548: }
                    549: 
                    550: # ============================================================ Display a folder
                    551: 
                    552: sub disfolder {
                    553:     my ($r,$folder)=@_;
                    554:     my %blocked = ();
                    555:     my %setters = ();
                    556:     my $startblock;
                    557:     my $endblock;
                    558:     my $numblocked = 0;
                    559:     &blockcheck(\%setters,\$startblock,\$endblock);
                    560:     $r->print(<<ENDDISHEADER);
1.16      albertel  561: <script type="text/javascript">
1.1       albertel  562:     function checkall() {
                    563: 	for (i=0; i<document.forms.disall.elements.length; i++) {
                    564:             if 
                    565:           (document.forms.disall.elements[i].name.indexOf('delmark_')==0) {
                    566: 	      document.forms.disall.elements[i].checked=true;
                    567:             }
                    568:         }
                    569:     }
                    570: 
                    571:     function uncheckall() {
                    572: 	for (i=0; i<document.forms.disall.elements.length; i++) {
                    573:             if 
1.30      raeburn   574:           (document.forms.disall.elements[i].name.indexof('delmark_')==0) {
1.1       albertel  575: 	      document.forms.disall.elements[i].checked=false;
                    576:             }
                    577:         }
                    578:     }
                    579: </script>
                    580: ENDDISHEADER
                    581:     my $fsqs='&folder='.$folder;
                    582:     my @temp=sortedmessages(\%blocked,$startblock,$endblock,\$numblocked,$folder);
                    583:     my $totalnumber=$#temp+1;
                    584:     unless ($totalnumber>0) {
                    585: 	$r->print('<h2>'.&mt('Empty Folder').'</h2>');
                    586: 	return;
                    587:     }
                    588:     unless ($interdis) {
                    589: 	$interdis=20;
                    590:     }
                    591:     my $number=int($totalnumber/$interdis);
                    592:     if (($startdis<0) || ($startdis>$number)) { $startdis=$number; }
                    593:     my $firstdis=$interdis*$startdis;
                    594:     if ($firstdis>$#temp) { $firstdis=$#temp-$interdis+1; }
                    595:     my $lastdis=$firstdis+$interdis-1;
                    596:     if ($lastdis>$#temp) { $lastdis=$#temp; }
                    597:     $r->print(&scrollbuttons($startdis,$number,$firstdis,$lastdis,$totalnumber));
                    598:     $r->print('<form method="post" name="disall" action="/adm/email">'.
1.4       albertel  599: 	      '<table class="LC_mail_list"><tr><th colspan="3">&nbsp</th><th>');
1.1       albertel  600:     if ($env{'form.sortedby'} eq "revdate") {
                    601: 	$r->print('<a href = "?sortedby=date'.$fsqs.'">'.&mt('Date').'</a></th>');
                    602:     } else {
                    603: 	$r->print('<a href = "?sortedby=revdate'.$fsqs.'">'.&mt('Date').'</a></th>');
                    604:     }
                    605:     $r->print('<th>');
                    606:     if ($env{'form.sortedby'} eq "revuser") {
                    607: 	$r->print('<a href = "?sortedby=user'.$fsqs.'">'.&mt('Username').'</a>');
                    608:     } else {
                    609: 	$r->print('<a href = "?sortedby=revuser'.$fsqs.'">'.&mt('Username').'</a>');
                    610:     }
                    611:     $r->print('</th><th>');
                    612:     if ($env{'form.sortedby'} eq "revdomain") {
                    613: 	$r->print('<a href = "?sortedby=domain'.$fsqs.'">'.&mt('Domain').'</a>');
                    614:     } else {
                    615: 	$r->print('<a href = "?sortedby=revdomain'.$fsqs.'">'.&mt('Domain').'</a>');
                    616:     }
                    617:     $r->print('</th><th>');
                    618:     if ($env{'form.sortedby'} eq "revsubject") {
                    619: 	$r->print('<a href = "?sortedby=subject'.$fsqs.'">'.&mt('Subject').'</a>');
                    620:     } else {
                    621:     	$r->print('<a href = "?sortedby=revsubject'.$fsqs.'">'.&mt('Subject').'</a>');
                    622:     }
                    623:     $r->print('</th><th>');
                    624:     if ($env{'form.sortedby'} eq "revcourse") {
1.30      raeburn   625:         $r->print('<a href = "?sortedby=course'.$fsqs.'">'.&mt('Course/Group').'</a>');
1.1       albertel  626:     } else {
1.30      raeburn   627:         $r->print('<a href = "?sortedby=revcourse'.$fsqs.'">'.&mt('Course/Group').'</a>');
1.1       albertel  628:     }
                    629:     $r->print('</th><th>');
                    630:     if ($env{'form.sortedby'} eq "revstatus") {
                    631: 	$r->print('<a href = "?sortedby=status'.$fsqs.'">'.&mt('Status').'</a></th>');
                    632:     } else {
                    633:      	$r->print('<a href = "?sortedby=revstatus'.$fsqs.'">'.&mt('Status').'</a></th>');
                    634:     }
                    635:     $r->print("</tr>\n");
1.6       albertel  636: 
                    637:     my $suffix = &Apache::lonmsg::foldersuffix($folder);
1.1       albertel  638:     for (my $n=$firstdis;$n<=$lastdis;$n++) {
1.11      albertel  639: 	my ($sendtime,$shortsubj,$fromname,$fromdomain,$status,$origID,
                    640: 	    $description,$recv_name,$recv_domain)= 
                    641: 		@{$temp[$n]};
1.1       albertel  642: 	if (($status ne 'deleted') && defined($sendtime) && $sendtime!~/error/) {
                    643: 	    if ($status eq 'new') {
1.4       albertel  644: 		$r->print('<tr class="LC_mail_new">');
1.1       albertel  645: 	    } elsif ($status eq 'read') {
1.4       albertel  646: 		$r->print('<tr class="LC_mail_read">');
1.1       albertel  647: 	    } elsif ($status eq 'replied') {
1.4       albertel  648: 		$r->print('<tr class="LC_mail_replied">'); 
1.1       albertel  649: 	    } else {
1.4       albertel  650: 		$r->print('<tr class="LC_mail_other">');
1.1       albertel  651: 	    }
1.6       albertel  652: 	    my ($dis_name,$dis_domain) = ($fromname,$fromdomain);
                    653: 	    if ($folder eq 'sent') {
1.11      albertel  654: 		if (defined($recv_name) && !defined($recv_domain)) {
                    655: 		    $dis_name   = join('<br />',@{$recv_name});
                    656: 		    $dis_domain = join('<br />',@{$recv_domain});
                    657: 		} else {
1.29      www       658: 		    my $msg_id  = &unescape($origID);
1.11      albertel  659: 		    my %message = &Apache::lonnet::get('nohist_email'.$suffix,
                    660: 						       [$msg_id]);
                    661: 		    my %content = &Apache::lonmsg::unpackagemsg($message{$msg_id});
                    662: 		    $dis_name   = join('<br />',@{$content{'recuser'}});
                    663: 		    $dis_domain = join('<br />',@{$content{'recdomain'}});
                    664: 		}
1.6       albertel  665: 	    }
1.1       albertel  666: 	    $r->print('<td><input type="checkbox" name="delmark_'.$origID.'" /></td><td><a href="/adm/email?display='.$origID.$sqs. 
                    667: 		      '">'.&mt('Open').'</a></td><td>'.
                    668: 		      ($folder ne 'trash'?'<a href="/adm/email?markdel='.$origID.$sqs.
                    669: 		      '">'.&mt('Delete'):'&nbsp').'</a></td>'.
                    670: 		      '<td>'.&Apache::lonlocal::locallocaltime($sendtime).'</td><td>'.
1.6       albertel  671: 		      $dis_name.'</td><td>'.$dis_domain.'</td><td>'.
1.8       albertel  672: 		      $shortsubj.'</td><td>'.
1.1       albertel  673:                       $description.'</td><td>'.$status.'</td></tr>'."\n");
                    674: 	} elsif ($status eq 'deleted') {
                    675: # purge
1.9       albertel  676: 	    my ($result,$msg) = 
1.29      www       677: 		&movemsg(&unescape($origID),$folder,'trash');
1.9       albertel  678: 	    
1.1       albertel  679: 	}
                    680:     }   
                    681:     $r->print("</table>\n<p>".
                    682:   '<a href="javascript:checkall()">'.&mt('Check All').'</a>&nbsp;'.
                    683:   '<a href="javascript:uncheckall()">'.&mt('Uncheck All').'</a></p>'.
                    684:   '<input type="hidden" name="sortedby" value="'.$env{'form.sortedby'}.'" />');
                    685:     if ($folder ne 'trash') {
                    686: 	$r->print(
                    687: 	      '<p><input type="submit" name="markeddel" value="'.&mt('Delete Checked').'" /></p>');
                    688:     }
                    689:     $r->print('<p><input type="submit" name="markedmove" value="'.&mt('Move Checked to Folder').'" />');
                    690:     my @allfolders=&Apache::lonnet::getkeys('email_folders');
                    691:     if ($allfolders[0]=~/^error:/) { @allfolders=(); }
                    692:     $r->print(
                    693: 	&Apache::loncommon::select_form('','movetofolder',
                    694: 			     ( map { $_ => $_ } @allfolders))
                    695: 	      );
                    696:     my $postedstartdis=$startdis+1;
                    697:     $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>');
                    698:     if ($numblocked > 0) {
                    699:         my $beginblock = &Apache::lonlocal::locallocaltime($startblock);
                    700:         my $finishblock = &Apache::lonlocal::locallocaltime($endblock);
                    701:         $r->print('<br /><br />'.
                    702:                   $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.'));
                    703:         &build_block_table($r,$startblock,$endblock,\%setters);
                    704:     }
                    705: }
                    706: 
                    707: # ============================================================== Compose output
                    708: 
                    709: sub compout {
                    710:     my ($r,$forwarding,$replying,$broadcast,$replycrit,$folder,$dismode)=@_;
                    711:     my $suffix=&Apache::lonmsg::foldersuffix($folder);
                    712: 
                    713:     if ($broadcast eq 'individual') {
                    714: 	&printheader($r,'/adm/email?compose=individual',
                    715: 	     'Send a Message');
                    716:     } elsif ($broadcast) {
                    717: 	&printheader($r,'/adm/email?compose=group',
                    718: 	     'Broadcast Message');
                    719:     } elsif ($forwarding) {
                    720: 	&Apache::lonhtmlcommon::add_breadcrumb
1.29      www       721:         ({href=>"/adm/email?display=".&escape($forwarding),
1.1       albertel  722:           text=>"Display Message"});
1.29      www       723: 	&printheader($r,'/adm/email?forward='.&escape($forwarding),
1.1       albertel  724: 	     'Forwarding a Message');
                    725:     } elsif ($replying) {
                    726: 	&Apache::lonhtmlcommon::add_breadcrumb
1.29      www       727:         ({href=>"/adm/email?display=".&escape($replying),
1.1       albertel  728:           text=>"Display Message"});
1.29      www       729: 	&printheader($r,'/adm/email?replyto='.&escape($replying),
1.1       albertel  730: 	     'Replying to a Message');
                    731:     } elsif ($replycrit) {
                    732: 	$r->print('<h3>'.&mt('Replying to a Critical Message').'</h3>');
                    733: 	$replying=$replycrit;
                    734:     } else {
                    735: 	&printheader($r,'/adm/email?compose=upload',
                    736: 	     'Distribute from Uploaded File');
                    737:     }
                    738: 
                    739:     my $dispcrit='';
                    740:     my $dissub='';
                    741:     my $dismsg='';
                    742:     my $disbase='';
                    743:     my $func=&mt('Send New');
                    744:     my %lt=&Apache::lonlocal::texthash('us' => 'Username',
                    745: 				       'do' => 'Domain',
                    746: 				       'ad' => 'Additional Recipients',
                    747: 				       'sb' => 'Subject',
                    748: 				       'ca' => 'Cancel',
                    749: 				       'ma' => 'Mail');
                    750: 
                    751:     if (&Apache::lonnet::allowed('srm',$env{'request.course.id'})
                    752: 	|| &Apache::lonnet::allowed('srm',$env{'request.course.id'}.
                    753: 				    '/'.$env{'request.course.sec'})) {
                    754: 	 my $crithelp = Apache::loncommon::help_open_topic("Course_Critical_Message");
                    755:          $dispcrit=
                    756:  '<p><label><input type="checkbox" name="critmsg" /> '.&mt('Send as critical message').'</label> ' . $crithelp . 
                    757:  '</p><p>'.
                    758:  '<label><input type="checkbox" name="sendbck" /> '.&mt('Send as critical message').'  ' .
                    759:  &mt('and return receipt') . '</label>' . $crithelp . 
                    760:  '</p><p><label><input type="checkbox" name="permanent" /> '.
                    761: &mt('Send copy to permanent email address (if known)').'</label></p>'.
                    762: '<p><label><input type="checkbox" name="rsspost" /> '.
                    763: 		  &mt('Include in course RSS newsfeed').'</label></p>';
                    764:      }
                    765:     my %message;
                    766:     my %content;
                    767:     my $defdom=$env{'user.domain'};
                    768:     if ($forwarding) {
                    769: 	%message=&Apache::lonnet::get('nohist_email'.$suffix,[$forwarding]);
                    770: 	%content=&Apache::lonmsg::unpackagemsg($message{$forwarding},$folder);
                    771: 	$dispcrit.='<input type="hidden" name="forwid" value="'.
                    772: 	    $forwarding.'" />';
                    773: 	$func=&mt('Forward');
                    774: 	
                    775: 	$dissub=&mt('Forwarding').': '.$content{'subject'};
                    776: 	$dismsg=&mt('Forwarded message from').' '.
                    777: 	    $content{'sendername'}.' '.&mt('at').' '.$content{'senderdomain'};
                    778: 	if ($content{'baseurl'}) {
1.29      www       779: 	    $disbase='<input type="hidden" name="baseurl" value="'.&escape($content{'baseurl'}).'" />';
1.1       albertel  780: 	}
                    781:     }
                    782:     if ($replying) {
                    783: 	%message=&Apache::lonnet::get('nohist_email'.$suffix,[$replying]);
                    784: 	%content=&Apache::lonmsg::unpackagemsg($message{$replying},$folder);
                    785: 	$dispcrit.='<input type="hidden" name="replyid" value="'.
                    786: 	    $replying.'" />';
                    787: 	$func=&mt('Send Reply to');
                    788: 	
                    789: 	$dissub=&mt('Reply').': '.$content{'subject'};       
                    790: 	$dismsg='> '.$content{'message'};
                    791: 	$dismsg=~s/\r/\n/g;
                    792: 	$dismsg=~s/\f/\n/g;
                    793: 	$dismsg=~s/\n+/\n\> /g;
                    794: 	if ($content{'baseurl'}) {
1.29      www       795: 	    $disbase='<input type="hidden" name="baseurl" value="'.&escape($content{'baseurl'}).'" />';
1.1       albertel  796: 	    if ($env{'user.adv'}) {
                    797: 		$disbase.='<label><input type="checkbox" name="storebasecomment" />'.&mt('Store message for re-use').
                    798: 		    '</label> <a href="/adm/email?showcommentbaseurl='.
1.29      www       799: 		    &escape($content{'baseurl'}).'" target="comments">'.
1.1       albertel  800: 		    &mt('Show re-usable messages').'</a><br />';
                    801: 	    }
                    802: 	}
                    803:     }
                    804:     my $citation=&displayresource(%content);
                    805:     if ($env{'form.recdom'}) { $defdom=$env{'form.recdom'}; }
1.23      www       806:     if ($env{'form.text'}) { $dismsg=$env{'form.text'}; }
                    807:     if ($env{'form.subject'}) { $dissub=$env{'form.subject'}; }
                    808:     $r->print(
1.1       albertel  809:                 '<form action="/adm/email"  name="compemail" method="post"'.
                    810:                 ' enctype="multipart/form-data">'."\n".
                    811:                 '<input type="hidden" name="sendmail" value="on" />'."\n".
                    812:                 '<table>');
                    813:     unless (($broadcast eq 'group') || ($broadcast eq 'upload')) {
                    814: 	if ($replying) {
                    815: 	    $r->print('<tr><td colspan="2">'.&mt('Replying to').' '.
                    816: 		      &Apache::loncommon::aboutmewrapper(
                    817: 							 &Apache::loncommon::plainname($content{'sendername'},$content{'senderdomain'}),$content{'sendername'},$content{'senderdomain'}).' ('.
1.14      albertel  818: 		      $content{'sendername'}.':'.
1.1       albertel  819: 		      $content{'senderdomain'}.')'.
                    820: 		      '<input type="hidden" name="recuname" value="'.$content{'sendername'}.'" />'.
                    821: 		      '<input type="hidden" name="recdomain" value="'.$content{'senderdomain'}.'" />'.
                    822: 		      '</td></tr>');
                    823: 	} else {
                    824: 	    my $domform = &Apache::loncommon::select_dom_form($defdom,'recdomain');
                    825: 	    my $selectlink=&Apache::loncommon::selectstudent_link
                    826: 	    ('compemail','recuname','recdomain');
                    827: 	    $r->print(<<"ENDREC");
                    828: <tr><td>$lt{'us'}:</td><td><input type="text" size="12" name="recuname" value="$env{'form.recname'}" /></td><td rowspan="2">$selectlink</td></tr>
                    829: <tr><td>$lt{'do'}:</td>
                    830: <td>$domform</td></tr>
                    831: ENDREC
                    832:         }
                    833:     }
                    834:     my $latexHelp = Apache::loncommon::helpLatexCheatsheet();
                    835:     if ($broadcast ne 'upload') {
                    836:        $r->print(<<"ENDCOMP");
                    837: <tr><td>$lt{'ad'}<br /><tt>username\@domain,username\@domain, ...
                    838: </tt></td><td>
                    839: <input type="text" size="50" name="additionalrec" /></td></tr>
                    840: <tr><td>$lt{'sb'}:</td><td><input type="text" size="50" name="subject" value="$dissub" />
                    841: </td></tr></table>
                    842: $latexHelp
                    843: <textarea name="message" id="message" cols="80" rows="15" wrap="hard">$dismsg
                    844: </textarea></p><br />
                    845: $dispcrit
                    846: $disbase
                    847: <input type="hidden" name="folder" value="$folder" />
                    848: <input type="hidden" name="dismode" value="$dismode" />
                    849: <input type="submit" name="send" value="$func $lt{'ma'}" />
                    850: <input type="submit" name="cancel" value="$lt{'ca'}" /><hr />
                    851: $citation
                    852: ENDCOMP
                    853:     } else { # $broadcast is 'upload'
                    854: 	$r->print(<<ENDUPLOAD);
                    855: <input type="hidden" name="sendmode" value="upload" />
                    856: <input type="hidden" name="send" value="on" />
                    857: <h3>Generate messages from a file</h3>
                    858: <p>
                    859: Subject: <input type="text" size="50" name="subject" />
                    860: </p>
                    861: <p>General message text<br />
                    862: <textarea name="message" id="message" cols="60" rows="10" wrap="hard">$dismsg
                    863: </textarea></p>
                    864: <p>
                    865: The file format for the uploaded portion of the message is:
                    866: <pre>
                    867: username1\@domain1: text
                    868: username2\@domain2: text
                    869: username3\@domain1: text
                    870: </pre>
                    871: </p>
                    872: <p>
                    873: The messages will be assembled from all lines with the respective 
                    874: <tt>username\@domain</tt>, and appended to the general message text.</p>
                    875: <p>
                    876: <input type="file" name="upfile" size="40" /></p><p>
                    877: $dispcrit
                    878: <input type="submit" value="Upload and Send" /></p>
                    879: ENDUPLOAD
                    880:     }
                    881:     if ($broadcast eq 'group') {
                    882:        &discourse($r);
1.25      foxr      883:        my $studentsel = &discourse();
                    884:        $r->print($studentsel);
1.1       albertel  885:     }
                    886:     $r->print('</form>'.
                    887: 	      &Apache::lonfeedback::generate_preview_button('compemail','message').
                    888: 	      &Apache::lonhtmlcommon::htmlareaselectactive('message'));
                    889: }
                    890: 
                    891: # ---------------------------------------------------- Display all face to face
                    892: 
                    893: sub retrieve_instructor_comments {
                    894:     my ($user,$domain)=@_;
                    895:     my $target=$env{'form.grade_target'};
                    896:     if (! $env{'request.course.id'}) { return; }
                    897:     if (! &Apache::lonnet::allowed('srm',$env{'request.course.id'})
                    898: 	&& ! &Apache::lonnet::allowed('srm',$env{'request.course.id'}.
                    899: 				      '/'.$env{'request.course.sec'})) {
                    900: 	return;
                    901:     }
                    902:     my %records=&Apache::lonnet::dump('nohist_email',
                    903: 			 $env{'course.'.$env{'request.course.id'}.'.domain'},
                    904: 			 $env{'course.'.$env{'request.course.id'}.'.num'},
                    905:                          '%255b'.$user.'%253a'.$domain.'%255d');
                    906:     my $result='';
                    907:     foreach (sort(keys(%records))) {
                    908:         my %content=&Apache::lonmsg::unpackagemsg($records{$_});
                    909:         next if ($content{'senderdomain'} eq '');
                    910:         next if ($content{'subject'} !~ /^Record/);
                    911: 	# &Apache::lonfeedback::newline_to_br(\$content{'message'});
                    912: 	$result.='Recorded by '.
1.14      albertel  913:             $content{'sendername'}.':'.$content{'senderdomain'}."\n";
1.1       albertel  914:         $result.=
                    915:             &Apache::lontexconvert::msgtexconverted($content{'message'})."\n";
                    916:      }
                    917:     return $result;
                    918: }
                    919: 
                    920: sub disfacetoface {
                    921:     my ($r,$user,$domain)=@_;
                    922:     my $target=$env{'form.grade_target'};
                    923:     unless ($env{'request.course.id'}) { return; }
                    924:     if  (!&Apache::lonnet::allowed('srm',$env{'request.course.id'})
                    925: 	 && ! &Apache::lonnet::allowed('srm',$env{'request.course.id'}.
                    926: 				       '/'.$env{'request.course.sec'})) {
                    927: 	$r->print('Not allowed');
                    928: 	return;
                    929:     }
                    930:     my %records=&Apache::lonnet::dump('nohist_email',
                    931: 			 $env{'course.'.$env{'request.course.id'}.'.domain'},
                    932: 			 $env{'course.'.$env{'request.course.id'}.'.num'},
                    933:                          '%255b'.$user.'%253a'.$domain.'%255d');
                    934:     my $result='';
                    935:     foreach (sort keys %records) {
                    936:         my %content=&Apache::lonmsg::unpackagemsg($records{$_});
                    937:         next if ($content{'senderdomain'} eq '');
                    938: 	&Apache::lonfeedback::newline_to_br(\$content{'message'});
                    939:         if ($content{'subject'}=~/^Record/) {
                    940: 	    $result.='<h3>'.&mt('Record').'</h3>';
                    941:         } elsif ($content{'subject'}=~/^Broadcast/) {
                    942:             $result .='<h3>'.&mt('Broadcast Message').'</h3>';
                    943:             if ($content{'subject'}=~/^Broadcast\./) {
                    944:                 if (defined($content{'coursemsgid'})) {
1.29      www       945:                     my $crsmsgid = &escape($content{'coursemsgid'});
1.1       albertel  946:                     my $broadcast_message = &general_message($crsmsgid);
                    947:                     $content{'message'} = '<b>'.&mt('Subject').': '.$content{'message'}.'</b><br />'.$broadcast_message;
                    948:                 } else {
                    949:                     %content=&Apache::lonmsg::unpackagemsg($content{'message'});
                    950:                     $content{'message'} =
                    951:                     '<b>'.&mt('Subject').': '.$content{'subject'}.'</b><br />'.
                    952:                     $content{'message'};
                    953:                 }
                    954:             }    
                    955:         } else {
                    956:             $result.='<h3>'.&mt('Critical Message').'</h3>';
                    957:             if (defined($content{'coursemsgid'})) {
1.29      www       958:                 my $crsmsgid=&escape($content{'coursemsgid'});
1.1       albertel  959:                 my $critical_message = &general_message($crsmsgid);
                    960:                 $content{'message'} = '<b>'.&mt('Subject').': '.$content{'message'}.'</b><br />'.$critical_message;
                    961:             } else {
                    962:                 %content=&Apache::lonmsg::unpackagemsg($content{'message'});
                    963:                 $content{'message'}=
                    964:                 '<b>'.&mt('Subject').': '.$content{'subject'}.'</b><br />'.
                    965: 		$content{'message'};
                    966:             }
                    967:         }
                    968:         $result.=&mt('By').': <b>'.
                    969: &Apache::loncommon::aboutmewrapper(
                    970:  &Apache::loncommon::plainname($content{'sendername'},$content{'senderdomain'}),$content{'sendername'},$content{'senderdomain'}).'</b> ('.
1.14      albertel  971: $content{'sendername'}.':'.
1.1       albertel  972:             $content{'senderdomain'}.') '.$content{'time'}.
                    973:             '<br /><pre>'.
                    974:               &Apache::lontexconvert::msgtexconverted($content{'message'}).
                    975: 	      '</pre>';
                    976:      }
                    977:     # Check to see if there were any messages.
                    978:     if ($result eq '') {
1.33      albertel  979:         my $lctype = lc(&Apache::loncommon::course_type());
1.1       albertel  980: 	if ($target ne 'tex') { 
1.30      raeburn   981: 	    $r->print("<p><b>".&mt('No notes, face-to-face discussion records, critical messages, or broadcast messages in this [_1].',$lctype)."</b></p>");
1.1       albertel  982: 	} else {
1.30      raeburn   983: 	    $r->print('\textbf{'.&mt('No notes, face-to-face discussion records, critical messages or broadcast messages in this [_1].',$lctype).'}\\\\');
1.1       albertel  984: 	}
                    985:     } else {
                    986:        $r->print($result);
                    987:     }
                    988: }
                    989: 
                    990: sub general_message {
                    991:     my ($crsmsgid) = @_;
                    992:     my %general_content;
                    993:     if ($crsmsgid) { 
                    994:         my %course_content = &Apache::lonnet::get('nohist_email',[$crsmsgid],
                    995:                            $env{'course.'.$env{'request.course.id'}.'.domain'},
                    996:                            $env{'course.'.$env{'request.course.id'}.'.num'});
                    997:         %general_content = &Apache::lonmsg::unpackagemsg($course_content{$crsmsgid});
                    998:     }
                    999:     return $general_content{'message'};
                   1000: }
                   1001: 
                   1002: # ---------------------------------------------------------------- Face to face
                   1003: 
                   1004: sub facetoface {
                   1005:     my ($r,$stage)=@_;
                   1006:     if (!&Apache::lonnet::allowed('srm',$env{'request.course.id'})
                   1007: 	&& ! &Apache::lonnet::allowed('srm',$env{'request.course.id'}.
                   1008: 				      '/'.$env{'request.course.sec'})) {
                   1009: 	$r->print('Not allowed');
                   1010: 	return;
                   1011:     }
1.33      albertel 1012:     my $crstype = &Apache::loncommon::course_type();
                   1013:     my $leaders = ($crstype eq 'Group') ? 'coordinators and leaders'
                   1014:                                         : 'faculty and staff';
1.1       albertel 1015:     &printheader($r,
                   1016: 		 '/adm/email?recordftf=query',
                   1017: 		 "User Notes, Face-to-Face, Critical Messages, Broadcast Messages");
                   1018: # from query string
                   1019: 
                   1020:     if ($env{'form.recname'}) { $env{'form.recuname'}=$env{'form.recname'}; }
                   1021:     if ($env{'form.recdom'}) { $env{'form.recdomain'}=$env{'form.recdom'}; }
                   1022: 
                   1023:     my $defdom=$env{'user.domain'};
                   1024: # already filled in
                   1025:     if ($env{'form.recdomain'}) { $defdom=$env{'form.recdomain'}; }
                   1026: # generate output
                   1027:     my $domform = &Apache::loncommon::select_dom_form($defdom,'recdomain');
                   1028:     my $stdbrws = &Apache::loncommon::selectstudent_link
                   1029: 	('stdselect','recuname','recdomain');
                   1030:     my %lt=&Apache::lonlocal::texthash('user' => 'Username',
                   1031: 				       'dom' => 'Domain',
1.30      raeburn  1032: 				       'head' => "User Notes, Records of Face-To-Face Discussions, Critical Messages, and Broadcast Messages in $crstype",
1.1       albertel 1033: 				       'subm' => 'Retrieve discussion and message records',
1.30      raeburn  1034: 				       'newr' => 'New Record (record is visible to '.lc($crstype).' '.$leaders.')',
1.1       albertel 1035: 				       'post' => 'Post this Record');
                   1036:     $r->print(<<"ENDTREC");
                   1037: <h3>$lt{'head'}</h3>
                   1038: <form method="post" action="/adm/email" name="stdselect">
                   1039: <input type="hidden" name="recordftf" value="retrieve" />
                   1040: <table>
                   1041: <tr><td>$lt{'user'}:</td><td><input type="text" size="12" name="recuname" value="$env{'form.recuname'}" /></td>
                   1042: <td rowspan="2">
                   1043: $stdbrws
                   1044: <input type="submit" value="$lt{'subm'}" /></td>
                   1045: </tr>
                   1046: <tr><td>$lt{'dom'}:</td>
                   1047: <td>$domform</td></tr>
                   1048: </table>
                   1049: </form>
                   1050: ENDTREC
                   1051:     if (($stage ne 'query') &&
                   1052:         ($env{'form.recdomain'}) && ($env{'form.recuname'})) {
                   1053:         chomp($env{'form.newrecord'});
                   1054:         if ($env{'form.newrecord'}) {
1.31      albertel 1055: 	    &Apache::lonmsg::store_instructor_comment($env{'form.newrecord'},
                   1056: 						      $env{'form.recuname'},
                   1057: 						      $env{'form.recdomain'});
1.1       albertel 1058:         }
                   1059:         $r->print('<h3>'.&Apache::loncommon::plainname($env{'form.recuname'},
                   1060: 				     $env{'form.recdomain'}).'</h3>');
                   1061:         &disfacetoface($r,$env{'form.recuname'},$env{'form.recdomain'});
                   1062: 	$r->print(<<ENDRHEAD);
                   1063: <form method="post" action="/adm/email">
                   1064: <input name="recdomain" value="$env{'form.recdomain'}" type="hidden" />
                   1065: <input name="recuname" value="$env{'form.recuname'}" type="hidden" />
                   1066: ENDRHEAD
                   1067:         $r->print(<<ENDBFORM);
                   1068: <hr />$lt{'newr'}<br />
                   1069: <textarea name="newrecord" cols="80" rows="10" wrap="hard"></textarea>
                   1070: <br />
                   1071: <input type="hidden" name="recordftf" value="post" />
                   1072: <input type="submit" value="$lt{'post'}" />
                   1073: </form>
                   1074: ENDBFORM
                   1075:     }
                   1076: }
                   1077: 
                   1078: # ----------------------------------------------------------- Blocking during exams
                   1079: 
                   1080: sub examblock {
                   1081:     my ($r,$action) = @_;
                   1082:     unless ($env{'request.course.id'}) { return;}
                   1083:     if (!&Apache::lonnet::allowed('srm',$env{'request.course.id'})
                   1084: 	&& ! &Apache::lonnet::allowed('srm',$env{'request.course.id'}.
                   1085: 				      '/'.$env{'request.course.sec'})) {
                   1086: 	$r->print('Not allowed');
                   1087: 	return;
                   1088:     }
1.34    ! albertel 1089:     my $usertype = (&Apache::loncommon::course_type() eq 'Group') ? 'members'
1.33      albertel 1090: 	                                                          : 'students';
1.1       albertel 1091:     my %lt=&Apache::lonlocal::texthash(
                   1092:             'comb' => 'Communication Blocking',
                   1093:             'cbds' => 'Communication blocking during scheduled exams',
1.30      raeburn  1094:             'desc' => "You can use communication blocking to prevent $usertype enrolled in this course from displaying LON-CAPA messages sent by other $usertype during an online exam. As blocking of communication could potentially interrupt legitimate communication between $usertype 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.",
1.1       albertel 1095:              'mecb' => 'Modify existing communication blocking periods',
                   1096:              'ncbc' => 'No communication blocks currently stored'
                   1097:     );
                   1098: 
                   1099:     my %ltext = &Apache::lonlocal::texthash(
                   1100:             'dura' => 'Duration',
                   1101:             'setb' => 'Set by',
                   1102:             'even' => 'Event',
                   1103:             'actn' => 'Action',
                   1104:             'star' => 'Start',
                   1105:             'endd' => 'End'
                   1106:     );
                   1107: 
                   1108:     &printheader($r,'/adm/email?block=display',$lt{'comb'});
                   1109:     $r->print('<h3>'.$lt{'cbds'}.'</h3>');
                   1110: 
                   1111:     if ($action eq 'store') {
                   1112:         &blockstore($r);
                   1113:     }
                   1114: 
                   1115:     $r->print($lt{'desc'}.'<br /><br />
                   1116:                <form name="blockform" method="post" action="/adm/email?block=store">
                   1117:              ');
                   1118: 
                   1119:     $r->print('<h4>'.$lt{'mecb'}.'</h4>');
                   1120:     my %records = ();
                   1121:     my $blockcount = 0;
                   1122:     my $parmcount = 0;
                   1123:     &get_blockdates(\%records,\$blockcount);
                   1124:     if ($blockcount > 0) {
                   1125:         $parmcount = &display_blocker_status($r,\%records,\%ltext);
                   1126:     } else {
                   1127:         $r->print($lt{'ncbc'}.'<br /><br />');
                   1128:     }
                   1129:     &display_addblocker_table($r,$parmcount,\%ltext);
                   1130:     my $end_page=&Apache::loncommon::end_page();
                   1131:     $r->print(<<"END");
                   1132: <br />
                   1133: <input type="hidden" name="blocktotal" value="$blockcount" />
                   1134: <input type ="submit" value="Save Changes" />
                   1135: </form>
                   1136: $end_page
                   1137: END
                   1138:     return;
                   1139: }
                   1140: 
                   1141: sub blockstore {
                   1142:     my $r = shift;
                   1143:     my %lt=&Apache::lonlocal::texthash(
                   1144:             'tfcm' => 'The following changes were made',
                   1145:             'cbps' => 'communication blocking period(s)',
                   1146:             'werm' => 'was/were removed',
                   1147:             'wemo' => 'was/were modified',
                   1148:             'wead' => 'was/were added',
                   1149:             'ncwm' => 'No changes were made.' 
                   1150:     );
                   1151:     my %adds = ();
                   1152:     my %removals = ();
                   1153:     my %cancels = ();
                   1154:     my $modtotal = 0;
                   1155:     my $canceltotal = 0;
                   1156:     my $addtotal = 0;
                   1157:     my %blocking = ();
                   1158:     $r->print('<h3>'.$lt{'head'}.'</h3>');
                   1159:     foreach (keys %env) {
                   1160:         if ($_ =~ m/^form\.modify_(\w+)$/) {
                   1161:             $adds{$1} = $1;
                   1162:             $removals{$1} = $1;
                   1163:             $modtotal ++;
                   1164:         } elsif ($_ =~ m/^form\.cancel_(\d+)$/) {
                   1165:             $cancels{$1} = $1;
                   1166:             unless ( defined($removals{$1}) ) {
                   1167:                 $removals{$1} = $1;
                   1168:                 $canceltotal ++;
                   1169:             }
                   1170:         } elsif ($_ =~ m/^form\.add_(\d+)$/) {
                   1171:             $adds{$1} = $1;
                   1172:             $addtotal ++;
                   1173:         }
                   1174:     }
                   1175: 
                   1176:     foreach (keys %removals) {
                   1177:         my $hashkey = $env{'form.key_'.$_};
                   1178:         &Apache::lonnet::del('comm_block',["$hashkey"],
                   1179:                          $env{'course.'.$env{'request.course.id'}.'.domain'},
                   1180:                          $env{'course.'.$env{'request.course.id'}.'.num'}
                   1181:                          );
                   1182:     }
                   1183:     foreach (keys %adds) {
                   1184:         unless ( defined($cancels{$_}) ) {
                   1185:             my ($newstart,$newend) = &get_dates_from_form($_);
                   1186:             my $newkey = $newstart.'____'.$newend;
1.14      albertel 1187:             $blocking{$newkey} = $env{'user.name'}.':'.$env{'user.domain'}.':'.$env{'form.title_'.$_};
1.1       albertel 1188:         }
                   1189:     }
                   1190:     if ($addtotal + $modtotal > 0) {
                   1191:         &Apache::lonnet::put('comm_block',\%blocking,
                   1192:                      $env{'course.'.$env{'request.course.id'}.'.domain'},
                   1193:                      $env{'course.'.$env{'request.course.id'}.'.num'}
                   1194:                      );
                   1195:     }
                   1196:     my $chgestotal = $canceltotal + $modtotal + $addtotal;
                   1197:     if ($chgestotal > 0) {
                   1198:         $r->print($lt{'tfcm'}.'<ul>');
                   1199:         if ($canceltotal > 0) {
                   1200:             $r->print('<li>'.$canceltotal.' '.$lt{'cbps'},' '.$lt{'werm'}.'</li>');
                   1201:         }
                   1202:         if ($modtotal > 0) {
                   1203:             $r->print('<li>'.$modtotal.' '.$lt{'cbps'},' '.$lt{'wemo'}.'</li>');
                   1204:         }
                   1205:         if ($addtotal > 0) {
                   1206:             $r->print('<li>'.$addtotal.' '.$lt{'cbps'},' '.$lt{'wead'}.'</li>');
                   1207:         }
                   1208:         $r->print('</ul>');
                   1209:     } else {
                   1210:         $r->print($lt{'ncwm'});
                   1211:     }
                   1212:     $r->print('<br />');
                   1213:     return;
                   1214: }
                   1215: 
                   1216: sub get_dates_from_form {
                   1217:     my $item = shift;
                   1218:     my $startdate = &Apache::lonhtmlcommon::get_date_from_form('startdate_'.$item);
                   1219:     my $enddate   = &Apache::lonhtmlcommon::get_date_from_form('enddate_'.$item);
                   1220:     return ($startdate,$enddate);
                   1221: }
                   1222: 
                   1223: sub get_blockdates {
                   1224:     my ($records,$blockcount) = @_;
                   1225:     $$blockcount = 0;
                   1226:     %{$records} = &Apache::lonnet::dump('comm_block',
                   1227:                          $env{'course.'.$env{'request.course.id'}.'.domain'},
                   1228:                          $env{'course.'.$env{'request.course.id'}.'.num'}
                   1229:                          );
1.15      albertel 1230:     $$blockcount = keys(%{$records});
                   1231: 
                   1232:     if ((keys(%{$records}))[0] =~ /^error: 2 /) {
                   1233: 	$records = {};
                   1234: 	$$blockcount = 0;
1.1       albertel 1235:     }
                   1236: }
                   1237: 
                   1238: sub display_blocker_status {
                   1239:     my ($r,$records,$ltext) = @_;
                   1240:     my $parmcount = 0;
1.16      albertel 1241:   
1.1       albertel 1242:     my %lt = &Apache::lonlocal::texthash(
                   1243:         'modi' => 'Modify',
                   1244:         'canc' => 'Cancel',
                   1245:     );
1.18      albertel 1246:     $r->print(&Apache::loncommon::start_data_table());
1.1       albertel 1247:     $r->print(<<"END");
1.16      albertel 1248:   <tr>
                   1249:     <th>$$ltext{'dura'}</th>
                   1250:     <th>$$ltext{'setb'}</th>
                   1251:     <th>$$ltext{'even'}</th>
                   1252:     <th>$$ltext{'actn'}?</th>
                   1253:   </tr>
1.1       albertel 1254: END
1.18      albertel 1255:     foreach my $record (sort(keys(%{$records}))) {
1.1       albertel 1256:         my $onchange = 'onFocus="javascript:window.document.forms['.
                   1257:                        "'blockform'].elements['modify_".$parmcount."'].".
                   1258:                        'checked=true;"';
1.18      albertel 1259:         my ($start,$end) = split(/____/,$record);
1.1       albertel 1260:         my $startform = &Apache::lonhtmlcommon::date_setter('blockform','startdate_'.$parmcount,$start,$onchange);
                   1261:         my $endform = &Apache::lonhtmlcommon::date_setter('blockform','enddate_'.$parmcount,$end,$onchange);
1.15      albertel 1262: 	
1.18      albertel 1263: 	my ($setuname,$setudom,$title) = 
                   1264: 	    &parse_block_record($$records{$record});
1.21      albertel 1265: 	$title = &HTML::Entities::encode($title,'"<>&');
1.1       albertel 1266:         my $settername = &Apache::loncommon::plainname($setuname,$setudom);
1.18      albertel 1267:         $r->print(&Apache::loncommon::start_data_table_row());
1.1       albertel 1268:         $r->print(<<"END");
                   1269:         <td>$$ltext{'star'}:&nbsp;$startform<br/>$$ltext{'endd'}:&nbsp;&nbsp;$endform</td>
                   1270:         <td>$settername</td>
1.18      albertel 1271:         <td><input type="text" name="title_$parmcount" size="15" value="$title" /><input type="hidden" name="key_$parmcount" value="$record" /></td>
1.1       albertel 1272:         <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>
                   1273: END
1.18      albertel 1274:         $r->print(&Apache::loncommon::end_data_table_row());
                   1275:         $parmcount++;
1.1       albertel 1276:     }
                   1277:     $r->print(<<"END");
                   1278: </table>
                   1279: <br />
                   1280: <br />
                   1281: END
                   1282:     return $parmcount;
                   1283: }
                   1284: 
1.15      albertel 1285: sub parse_block_record {
                   1286:     my ($record) = @_;
                   1287:     my ($setuname,$setudom,$title);
                   1288:     my @data = split(/:/,$record,3);
                   1289:     if (scalar(@data) eq 2) {
                   1290: 	$title = $data[1];
                   1291: 	($setuname,$setudom) = split(/@/,$data[0]);
                   1292:     } else {
                   1293: 	($setuname,$setudom,$title) = @data;
                   1294:     }
                   1295:     return ($setuname,$setudom,$title);
                   1296: }
                   1297: 
1.1       albertel 1298: sub display_addblocker_table {
                   1299:     my ($r,$parmcount,$ltext) = @_;
                   1300:     my $start = time;
                   1301:     my $end = $start + (60 * 60 * 2); #Default is an exam of 2 hours duration.
                   1302:     my $onchange = 'onFocus="javascript:window.document.forms['.
                   1303:                    "'blockform'].elements['add_".$parmcount."'].".
                   1304:                    'checked=true;"';
                   1305:     my $startform = &Apache::lonhtmlcommon::date_setter('blockform','startdate_'.$parmcount,$start,$onchange);
                   1306:     my $endform = &Apache::lonhtmlcommon::date_setter('blockform','enddate_'.$parmcount,$end,$onchange);
                   1307:     my %lt = &Apache::lonlocal::texthash(
                   1308:         'addb' => 'Add block',
                   1309:         'exam' => 'e.g., Exam 1',
                   1310:         'addn' => 'Add new communication blocking periods'
                   1311:     );
                   1312:     $r->print(<<"END");
                   1313: <h4>$lt{'addn'}</h4> 
1.18      albertel 1314: END
                   1315:     $r->print(&Apache::loncommon::start_data_table());
                   1316:     $r->print(<<"END");
1.16      albertel 1317:    <tr>
                   1318:      <th>$$ltext{'dura'}</th>
                   1319:      <th>$$ltext{'even'} $lt{'exam'}</th>
                   1320:      <th>$$ltext{'actn'}?</th>
                   1321:    </tr>
1.18      albertel 1322: END
                   1323:    $r->print(&Apache::loncommon::start_data_table_row());
                   1324:     $r->print(<<"END");
1.16      albertel 1325:      <td>$$ltext{'star'}:&nbsp;$startform<br />$$ltext{'endd'}:&nbsp;&nbsp;$endform</td>
                   1326:      <td><input type="text" name="title_$parmcount" size="15" value="" /></td>
                   1327:      <td><label>$lt{'addb'}?&nbsp;<input type="checkbox" name="add_$parmcount" value="1" /></label></td>
1.1       albertel 1328: END
1.18      albertel 1329:     $r->print(&Apache::loncommon::end_data_table_row());
                   1330:     $r->print(&Apache::loncommon::end_data_table());
1.1       albertel 1331:     return;
                   1332: }
                   1333: 
                   1334: sub blockcheck {
                   1335:     my ($setters,$startblock,$endblock) = @_;
                   1336:     # Retrieve active student roles and active course coordinator/instructor roles
1.15      albertel 1337:     my %live_courses =
                   1338: 	map { $_ => 1} &Apache::loncommon::findallcourses();
                   1339:     # FIXME should really probe for apriv, but ::allowed can only probe the 
                   1340:     #       currently active role
                   1341:     my %staff_of =
                   1342: 	map { $_ => 1} &Apache::loncommon::findallcourses(['cc','in']);
                   1343: 
                   1344:     # Retrieve blocking times and identity of blocker for active courses
                   1345:     # for students.
                   1346:     return if (!%live_courses);
                   1347: 
                   1348:     foreach my $course (keys(%live_courses)) {
1.19      albertel 1349: 	my $cdom = $env{'course.'.$course.'.domain'};
                   1350: 	my $cnum = $env{'course.'.$course.'.num'};
1.15      albertel 1351: 
                   1352: 	# if they are a staff member and are currently not playing student
                   1353: 	next if ( $staff_of{$course} 
                   1354: 		  && ($env{'request.role'} !~ m{^st\./$cdom/$cnum}));
                   1355: 
                   1356: 	$setters->{$course} = {};
                   1357: 	$setters->{$course}{'staff'} = [];
                   1358: 	$setters->{$course}{'times'} = [];
                   1359: 	my %records = &Apache::lonnet::dump('comm_block',$cdom,$cnum);
                   1360: 	foreach my $record (keys %records) {
                   1361: 	    my ($start,$end) = ($record =~ m/^(\d+)____(\d+)$/);
                   1362: 	    if ($start <= time && $end >= time) {
                   1363: 		my ($staff_name,$staff_dom,$title) = 
                   1364: 		    &parse_block_record($records{$record});
                   1365: 		push(@{$$setters{$course}{'staff'}}, [$staff_name,$staff_dom]);
                   1366: 		push(@{$$setters{$course}{'times'}}, [$start,$end]);
                   1367: 		if ( ($$startblock == 0) || ($$startblock > $1) ) {
                   1368: 		    $$startblock = $1;
                   1369: 		}
                   1370: 		if ( ($$endblock == 0) || ($$endblock < $2) ) {
                   1371: 		    $$endblock = $2;
                   1372: 		}
                   1373: 	    }
                   1374: 	}
1.1       albertel 1375:     }
                   1376: }
                   1377: 
                   1378: sub build_block_table {
                   1379:     my ($r,$startblock,$endblock,$setters) = @_;
                   1380:     my %lt = &Apache::lonlocal::texthash(
                   1381:         'cacb' => 'Currently active communication blocks',
1.30      raeburn  1382:         'cour' => 'Course/Group',
1.1       albertel 1383:         'dura' => 'Duration',
                   1384:         'blse' => 'Block set by'
1.18      albertel 1385:     );
                   1386:     $r->print(<<"END");
                   1387: <br /><br />$lt{'cacb'}:<br /><br />
                   1388: END
                   1389:     $r->print(&Apache::loncommon::start_data_table());
1.1       albertel 1390:     $r->print(<<"END");
1.16      albertel 1391: <tr>
                   1392:  <th>$lt{'cour'}</th>
                   1393:  <th>$lt{'dura'}</th>
                   1394:  <th>$lt{'blse'}</th>
                   1395: </tr>
1.1       albertel 1396: END
1.18      albertel 1397:     foreach my $course (keys(%{$setters})) {
                   1398:         my %courseinfo=&Apache::lonnet::coursedescription($course);
                   1399:         for (my $i=0; $i<@{$$setters{$course}{staff}}; $i++) {
                   1400:             my ($uname,$udom) = @{$$setters{$course}{staff}[$i]};
1.1       albertel 1401:             my $fullname = &Apache::loncommon::plainname($uname,$udom);
1.18      albertel 1402:             my ($openblock,$closeblock) = @{$$setters{$course}{times}[$i]};
1.1       albertel 1403:             $openblock = &Apache::lonlocal::locallocaltime($openblock);
                   1404:             $closeblock= &Apache::lonlocal::locallocaltime($closeblock);
1.18      albertel 1405:             $r->print(&Apache::loncommon::start_data_table_row().
                   1406: 		      '<td>'.$courseinfo{'description'}.'</td>'.
1.1       albertel 1407:                       '<td>'.$openblock.' to '.$closeblock.'</td>'.
1.14      albertel 1408:                       '<td>'.$fullname.' ('.$uname.':'.$udom.
1.18      albertel 1409:                       ')</td>'.
                   1410: 		       &Apache::loncommon::end_data_table_row());
1.1       albertel 1411:         }
                   1412:     }
1.18      albertel 1413:     $r->print(&Apache::loncommon::end_data_table());
1.1       albertel 1414: }
                   1415: 
                   1416: # ----------------------------------------------------------- Display a message
                   1417: 
                   1418: sub displaymessage {
                   1419:     my ($r,$msgid,$folder)=@_;
                   1420:     my $suffix=&Apache::lonmsg::foldersuffix($folder);
                   1421:     my %blocked = ();
                   1422:     my %setters = ();
                   1423:     my $startblock = 0;
                   1424:     my $endblock = 0;
                   1425:     my $numblocked = 0;
1.33      albertel 1426:     my $crstype = &Apache::loncommon::course_type();
1.30      raeburn  1427: 
1.1       albertel 1428: # info to generate "next" and "previous" buttons and check if message is blocked
                   1429:     &blockcheck(\%setters,\$startblock,\$endblock);
                   1430:     my @messages=&sortedmessages(\%blocked,$startblock,$endblock,\$numblocked,$folder);
                   1431:     if ( $blocked{$msgid} eq 'ON' ) {
                   1432:         &printheader($r,'/adm/email',&mt('Display a Message'));
                   1433:         $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.'));
                   1434:         &build_block_table($r,$startblock,$endblock,\%setters);
                   1435:         return;
                   1436:     }
                   1437:     &statuschange($msgid,'read',$folder);
                   1438:     my %message=&Apache::lonnet::get('nohist_email'.$suffix,[$msgid]);
                   1439:     my %content=&Apache::lonmsg::unpackagemsg($message{$msgid});
                   1440: 
                   1441:     my $counter=0;
                   1442:     $r->print('<pre>');
1.29      www      1443:     my $escmsgid=&escape($msgid);
1.1       albertel 1444:     foreach (@messages) {
                   1445: 	if ($_->[5] eq $escmsgid){
                   1446: 	    last;
                   1447: 	}
                   1448: 	$counter++;
                   1449:     }
                   1450:     $r->print('</pre>');
                   1451:     my $number_of_messages = scalar(@messages); #subtract 1 for last index
                   1452: # start output
1.29      www      1453:     &printheader($r,'/adm/email?display='.&escape($msgid),'Display a Message','',$content{'baseurl'});
1.1       albertel 1454:     my %courseinfo=&Apache::lonnet::coursedescription($content{'courseid'});
                   1455: # Functions
                   1456:     $r->print('<table border="2" width="100%"><tr bgcolor="#FFFFAA"><td>'.&mt('Functions').':</td>'.
1.29      www      1457: 	      '<td><a href="/adm/email?replyto='.&escape($msgid).$sqs.
1.1       albertel 1458: 	      '"><b>'.&mt('Reply').'</b></a></td>'.
1.29      www      1459: 	      '<td><a href="/adm/email?forward='.&escape($msgid).$sqs.
1.1       albertel 1460: 	      '"><b>'.&mt('Forward').'</b></a></td>'.
1.29      www      1461: 	      '<td><a href="/adm/email?markunread='.&escape($msgid).$sqs.
1.1       albertel 1462: 	      '"><b>'.&mt('Mark Unread').'</b></a></td>'.
1.29      www      1463: 	      '<td><a href="/adm/email?markdel='.&escape($msgid).$sqs.
1.1       albertel 1464: 	      '"><b>'.&mt('Delete').'</b></a></td>'.
                   1465: 	      '<td><a href="/adm/email?'.$sqs.
                   1466: 	      ($env{'form.dismode'} eq 'new'?'&folder=new':'').
                   1467: 	      '"><b>'.&mt('Back to Folder Display').'</b></a></td>');
                   1468:     if ($counter > 0){
                   1469: 	$r->print('<td><a href="/adm/email?display='.$messages[$counter-1]->[5].$sqs.
                   1470: 		  '"><b>'.&mt('Previous').'</b></a></td>');
                   1471:     }
                   1472:     if ($counter < $number_of_messages - 1){
                   1473: 	$r->print('<td><a href="/adm/email?display='.$messages[$counter+1]->[5].$sqs.
                   1474: 		  '"><b>'.&mt('Next').'</b></a></td>');
                   1475:     }
                   1476:     $r->print('</tr></table>');
                   1477:     if ($env{'user.adv'}) {
                   1478: 	$r->print('<table border="2" width="100%"><tr bgcolor="#FFAAAA"><td>'.&mt('Currently available actions (will open extra window)').':</td>');
                   1479: 	my $symb=&Apache::lonnet::symbread($content{'baseurl'});      
                   1480: 	if (&Apache::lonnet::allowed('vgr',$env{'request.course.id'})) {
                   1481: 		$r->print('<td><b>'.&Apache::loncommon::track_student_link(&mt('View recent activity'),$content{'sendername'},$content{'senderdomain'},'check').'</b></td>');
                   1482: 	    }
                   1483: 	if (&Apache::lonnet::allowed('opa',$env{'request.course.id'}) && $symb) {
                   1484: 	    $r->print('<td><b>'.&Apache::loncommon::pprmlink(&mt('Set/Change parameters'),$content{'sendername'},$content{'senderdomain'},$symb,'check').'</b></td>');
                   1485: 	}
                   1486: 	if (&Apache::lonnet::allowed('mgr',$env{'request.course.id'}) && $symb) {
                   1487: 	    $r->print('<td><b>'.&Apache::loncommon::pgrdlink(&mt('Set/Change grades'),$content{'sendername'},$content{'senderdomain'},$symb,'check').'</b></td>');
                   1488: 	}
                   1489: 	$r->print('</tr></table>');
                   1490:     }
                   1491:     my $tolist;
                   1492:     my @recipients = ();
                   1493:     for (my $i=0; $i<@{$content{'recuser'}}; $i++) {
                   1494:         $recipients[$i] =  &Apache::loncommon::aboutmewrapper(
                   1495:            &Apache::loncommon::plainname($content{'recuser'}[$i],
                   1496:                                       $content{'recdomain'}[$i]),
                   1497:               $content{'recuser'}[$i],$content{'recdomain'}[$i]).
                   1498:        ' ('.$content{'recuser'}[$i].' at '.$content{'recdomain'}[$i].') ';
                   1499:     }
                   1500:     $tolist = join(', ',@recipients);
                   1501:     $r->print('<br /><b>'.&mt('Subject').':</b> '.$content{'subject'}.
                   1502: 	      ($folder ne 'sent'?'<br /><b>'.&mt('From').':</b> '.
                   1503: 	      &Apache::loncommon::aboutmewrapper(
                   1504: 						 &Apache::loncommon::plainname($content{'sendername'},$content{'senderdomain'}),
                   1505: 						 $content{'sendername'},$content{'senderdomain'}).' ('.
                   1506: 	      $content{'sendername'}.' at '.
                   1507: 	      $content{'senderdomain'}.') ':'<br /><b>'.&mt('To').':</b> '.
                   1508:               $tolist).
1.30      raeburn  1509: 	      ($content{'courseid'}?'<br /><b>'.&mt($crstype).':</b> '.$courseinfo{'description'}.
                   1510: 	       ($content{'coursesec'}?' ('.&mt('Section').': '.$content{'coursesec'}.')':''):'').
1.1       albertel 1511: 	      '<br /><b>'.&mt('Time').':</b> '.$content{'time'}.
                   1512: 	      ($content{'baseurl'}?'<br /><b>'.&mt('Refers to').':</b> <a href="'.$content{'baseurl'}.'">'.
                   1513: 	       $content{'baseurl'}.' ('.&Apache::lonnet::gettitle($content{'baseurl'}).')</a>':'').
                   1514: 	      '<p><pre>'.
                   1515: 	      &Apache::lontexconvert::msgtexconverted($content{'message'},1).
                   1516: 	      '</pre><hr />'.&displayresource(%content).'</p>');
                   1517:     return;   
                   1518: }
                   1519: 
                   1520: # =========================================================== Show the citation
                   1521: 
                   1522: sub displayresource {
                   1523:     my %content=@_;
                   1524: #
                   1525: # If the recipient is in the same course that the message was sent from and
                   1526: # has sufficient privileges, show "all details," else show citation
                   1527: #
                   1528:     if (($env{'request.course.id'} eq $content{'courseid'})
                   1529:      && (&Apache::lonnet::allowed('vgr',$content{'courseid'}))) {
                   1530: 	my $symb=&Apache::lonnet::symbread($content{'baseurl'});
                   1531: # Could not get a symb, give up
                   1532: 	unless ($symb) { return $content{'citation'}; }
                   1533: # Have a symb, can render
                   1534: 	return '<h2>'.&mt('Current attempts of student (if applicable)').'</h2>'.
                   1535: 	    &Apache::loncommon::get_previous_attempt($symb,
                   1536: 						     $content{'sendername'},
                   1537: 						     $content{'senderdomain'},
                   1538: 						     $content{'courseid'}).
                   1539: 	    '<hr /><h2>'.&mt('Current screen output (if applicable)').'</h2>'.
                   1540: 	    &Apache::loncommon::get_student_view($symb,
                   1541: 						 $content{'sendername'},
                   1542: 						 $content{'senderdomain'},
                   1543: 						 $content{'courseid'}).
                   1544: 	    '<h2>'.&mt('Correct Answer(s) (if applicable)').'</h2>'.
                   1545: 	    &Apache::loncommon::get_student_answers($symb,
                   1546: 						    $content{'sendername'},
                   1547: 						    $content{'senderdomain'},
                   1548: 						    $content{'courseid'});
                   1549:     } elsif ($env{'user.adv'}) {
                   1550: 	return $content{'citation'};
                   1551:     }
                   1552:     return '';
                   1553: }
                   1554: 
                   1555: # ================================================================== The Header
                   1556: 
                   1557: sub header {
                   1558:     my ($r,$title,$baseurl)=@_;
                   1559:     
                   1560:     my $extra = &Apache::loncommon::studentbrowser_javascript();
                   1561:     if ($baseurl) {
                   1562: 	$extra .= "<base href=\"http://$ENV{'SERVER_NAME'}/$baseurl\" />";
                   1563:     }
                   1564:     $r->print(&Apache::loncommon::start_page('Communication and Messages',
                   1565: 					     $extra));
                   1566:     $r->print(&Apache::lonhtmlcommon::breadcrumbs
1.22      albertel 1567: 	      (($title?$title:'Communication and Messages')));
1.1       albertel 1568: 
                   1569: }
                   1570: 
                   1571: # ---------------------------------------------------------------- Print header
                   1572: 
                   1573: sub printheader {
                   1574:     my ($r,$url,$desc,$title,$baseurl)=@_;
                   1575:     &Apache::lonhtmlcommon::add_breadcrumb
                   1576: 	({href=>$url,
                   1577: 	  text=>$desc});
                   1578:     &header($r,$title,$baseurl);
                   1579: }
                   1580: 
                   1581: # ------------------------------------------------------------ Store the comment
                   1582: 
                   1583: sub storecomment {
                   1584:     my ($r)=@_;
                   1585:     my $msgtxt=&Apache::lonfeedback::clear_out_html($env{'form.message'});
                   1586:     my $cleanmsgtxt='';
                   1587:     foreach (split(/[\n\r]/,$msgtxt)) {
                   1588: 	unless ($_=~/^\s*(\>|\&gt\;)/) {
                   1589: 	    $cleanmsgtxt.=$_."\n";
                   1590: 	}
                   1591:     }
1.29      www      1592:     my $key=&escape($env{'form.baseurl'}).'___'.time;
1.1       albertel 1593:     &Apache::lonnet::put('nohist_stored_comments',{ $key => $cleanmsgtxt });
                   1594: }
                   1595: 
                   1596: sub storedcommentlisting {
                   1597:     my ($r)=@_;
                   1598:     my %msgs=&Apache::lonnet::dump('nohist_stored_comments',undef,undef,
1.29      www      1599:        '^'.&escape(&escape($env{'form.showcommentbaseurl'})));
1.1       albertel 1600:     $r->print(&Apache::loncommon::start_page('Stored Comment Listing',undef,
                   1601: 					     {'onlybody' => 1}));
                   1602:     if ((keys %msgs)[0]=~/^error\:/) {
                   1603: 	$r->print(&mt('No stored comments yet.'));
                   1604:     } else {
                   1605: 	my $found=0;
                   1606: 	foreach (sort keys %msgs) {
                   1607: 	    $r->print("\n".$msgs{$_}."<hr />");
                   1608: 	    $found=1;
                   1609: 	}
                   1610: 	unless ($found) {
                   1611: 	    $r->print(&mt('No stored comments yet for this resource.'));
                   1612: 	}
                   1613:     }
                   1614: }
                   1615: 
                   1616: # ---------------------------------------------------------------- Send an email
                   1617: 
                   1618: sub sendoffmail {
                   1619:     my ($r,$folder)=@_;
                   1620:     my $suffix=&Apache::lonmsg::foldersuffix($folder);
                   1621:     my $sendstatus='';
                   1622:     my %specialmsg_status;
                   1623:     my $numspecial = 0;
                   1624:     if ($env{'form.send'}) {
                   1625: 	&printheader($r,'','Messages being sent.');
                   1626: 	$r->rflush();
                   1627: 	my %content=();
                   1628: 	undef %content;
                   1629: 	if ($env{'form.forwid'}) {
                   1630: 	    my $msgid=$env{'form.forwid'};
                   1631: 	    my %message=&Apache::lonnet::get('nohist_email'.$suffix,[$msgid]);
                   1632: 	    %content=&Apache::lonmsg::unpackagemsg($message{$msgid},1);
                   1633: 	    &statuschange($msgid,'forwarded',$folder);
                   1634: 	    $env{'form.message'}.="\n\n-- Forwarded message --\n\n".
                   1635: 		$content{'message'};
                   1636: 	}
                   1637: 	if ($env{'form.replyid'}) {
                   1638: 	    my $msgid=$env{'form.replyid'};
                   1639: 	    my %message=&Apache::lonnet::get('nohist_email'.$suffix,[$msgid]);
                   1640: 	    %content=&Apache::lonmsg::unpackagemsg($message{$msgid},1);
                   1641: 	    &statuschange($msgid,'replied',$folder);
                   1642: 	}
1.13      albertel 1643: 
1.25      foxr     1644: 	my $to = $env{'form.selectedusers.forminput'};
                   1645: 	my $mode = $env{'form.sendmode'};
                   1646: 
1.13      albertel 1647: 	my %toaddr;
1.25      foxr     1648: 	if ($to) {
                   1649: 	    foreach my $dest (@$to) {
1.27      albertel 1650: 		my ($user,$domain) = split(/:/, $dest);
1.25      foxr     1651: 		if (($user ne '') && ($domain ne '')) {
                   1652: 		    my $address = $user.":".$domain; # How the code below expects it.
                   1653: 		    $toaddr{$address} = '';
                   1654: 		}
                   1655: 	    }
                   1656: 	}
                   1657: 
1.1       albertel 1658: 	if ($env{'form.sendmode'} eq 'group') {
1.25      foxr     1659: 	     foreach my $address (keys(%env)) {
                   1660: 	 	if ($address=~/^form\.send\_to\_\&\&\&[^\&]*\&\&\&\_(.+)$/) {
                   1661: 	 	    $toaddr{$1}='';
                   1662: 	 	}
1.1       albertel 1663: 	    }
                   1664: 	} elsif ($env{'form.sendmode'} eq 'upload') {
1.13      albertel 1665: 	    foreach my $line (split(/[\n\r\f]+/,$env{'form.upfile'})) {
                   1666: 		my ($rec,$txt)=split(/\s*\:\s*/,$line);
1.1       albertel 1667: 		if ($txt) {
                   1668: 		    $rec=~s/\@/\:/;
                   1669: 		    $toaddr{$rec}.=$txt."\n";
                   1670: 		}
                   1671: 	    }
                   1672: 	} else {
1.25      foxr     1673: 	    if (($env{'form.recuname'} ne '') && ($env{'form.recdomain'} ne '')) {
                   1674: 		$toaddr{$env{'form.recuname'}.':'.$env{'form.recdomain'}}='';
                   1675: 	    }
1.1       albertel 1676: 	}
                   1677: 	if ($env{'form.additionalrec'}) {
                   1678: 	    foreach (split(/\,/,$env{'form.additionalrec'})) {
                   1679: 		my ($auname,$audom)=split(/\@/,$_);
1.25      foxr     1680: 		if (($auname ne "") && ($audom ne "")) {
                   1681: 		    $toaddr{$auname.':'.$audom}='';
                   1682: 		}
1.1       albertel 1683: 	    }
                   1684: 	}
                   1685: 
                   1686:         my $savemsg;
                   1687:         my $msgtype;
                   1688:         my %sentmessage;
1.7       albertel 1689:         my $msgsubj=&Apache::lonfeedback::clear_out_html($env{'form.subject'},
                   1690: 							 undef,1);
1.1       albertel 1691:         if ((($env{'form.critmsg'}) || ($env{'form.sendbck'})) &&
                   1692:             (&Apache::lonnet::allowed('srm',$env{'request.course.id'})
                   1693: 	     || &Apache::lonnet::allowed('srm',$env{'request.course.id'}.
                   1694: 					 '/'.$env{'request.course.sec'})
                   1695: 	     )) {
                   1696:             $savemsg=&Apache::lonfeedback::clear_out_html($env{'form.message'},1);
                   1697:             $msgtype = 'critical';
                   1698:         } else {
                   1699:             $savemsg=&Apache::lonfeedback::clear_out_html($env{'form.message'});
                   1700:         }
                   1701: 	
1.13      albertel 1702: 	foreach my $address (sort(keys(%toaddr))) {
                   1703: 	    my ($recuname,$recdomain)=split(/\:/,$address);
1.1       albertel 1704:             my $msgtxt = $savemsg;
1.13      albertel 1705: 	    if ($toaddr{$address}) { $msgtxt.='<hr />'.$toaddr{$address}; }
1.12      albertel 1706: 	    my @thismsg;
1.1       albertel 1707: 	    if ((($env{'form.critmsg'}) || ($env{'form.sendbck'})) && 
                   1708: 		(&Apache::lonnet::allowed('srm',$env{'request.course.id'})
                   1709: 		 || &Apache::lonnet::allowed('srm',$env{'request.course.id'}.
                   1710: 					     '/'.$env{'request.course.sec'}))) {
1.13      albertel 1711: 		$r->print(&mt('Sending critical message').' '.$recuname.':'.$recdomain.': ');
                   1712: 		@thismsg=
                   1713: 		    &Apache::lonmsg::user_crit_msg($recuname,$recdomain,
                   1714: 						   $msgsubj,$msgtxt,
                   1715: 						   $env{'form.sendbck'},
                   1716: 						   $env{'form.permanent'},
                   1717: 						   \$sentmessage{$address});
1.1       albertel 1718: 	    } else {
1.14      albertel 1719: 		$r->print(&mt('Sending').' '.$recuname.':'.$recdomain.': ');
1.13      albertel 1720: 		@thismsg=
                   1721: 		    &Apache::lonmsg::user_normal_msg($recuname,$recdomain,
                   1722: 						     $msgsubj,$msgtxt,
                   1723: 						     $content{'citation'},
                   1724: 						     undef,undef,
                   1725: 						     $env{'form.permanent'},
                   1726: 						     \$sentmessage{$address});
1.1       albertel 1727:             }
                   1728: 	    if (($env{'request.course.id'}) && (($msgtype eq 'critical') || 
                   1729:                                          ($env{'form.sendmode'} eq 'group'))) {
1.12      albertel 1730: 	        $specialmsg_status{$recuname.':'.$recdomain} =
                   1731: 		    join(' ',@thismsg);
                   1732: 		foreach my $result (@thismsg) {
                   1733: 		    if ($result eq 'ok') {
                   1734: 			$numspecial++;
                   1735: 		    }
                   1736: 		}
1.1       albertel 1737: 	    }
1.12      albertel 1738: 	    $sendstatus.=' '.join(' ',@thismsg);
1.1       albertel 1739: 	}
                   1740:         if (($env{'request.course.id'}) && (($env{'form.sendmode'} eq 'group')
                   1741:                                               || ($msgtype eq 'critical'))) {
                   1742:             my $subj_prefix;
                   1743:             if ($msgtype eq 'critical') {
                   1744:                 $subj_prefix = 'Critical.';
                   1745:             } else {
                   1746:                 $subj_prefix = 'Broadcast.';
                   1747:             }
                   1748:             my ($specialmsgid,$specialresult);
                   1749:             my $cnum = $env{'course.'.$env{'request.course.id'}.'.num'};
                   1750:             my $cdom = $env{'course.'.$env{'request.course.id'}.'.domain'};
1.29      www      1751:             my $course_str = &escape('['.$cnum.':'.$cdom.']');
1.1       albertel 1752: 
                   1753:             if ($numspecial) {
                   1754:                 $specialresult = &Apache::lonmsg::user_normal_msg_raw($cnum,$cdom,$subj_prefix.
                   1755:                     ' '.$course_str,$savemsg,undef,undef,undef,
                   1756:                     undef,undef,\$specialmsgid);
1.29      www      1757:                 $specialmsgid = &unescape($specialmsgid);
1.1       albertel 1758:             }
                   1759:             if ($specialresult eq 'ok') {
                   1760:                 my $record_sent;
1.13      albertel 1761:                 my @recusers;
                   1762:                 my @recudoms;
                   1763:                 my ($stamp,$crssubj,$msgname,$msgdom,$msgcount,$context,$pid) =
1.29      www      1764: 		    split(/\:/,&unescape($specialmsgid));
1.13      albertel 1765: 
1.1       albertel 1766:                 foreach my $recipient (sort(keys(%toaddr))) {
                   1767:                     if ($specialmsg_status{$recipient} eq 'ok') {
                   1768:                         my $usersubj = $subj_prefix.'['.$recipient.']';
                   1769:                         my $usermsgid = 
                   1770: 			    &Apache::lonmsg::buildmsgid($stamp,$usersubj,
                   1771: 							$msgname,$msgdom,
                   1772: 							$msgcount,$context,
                   1773: 							$pid);
                   1774:                         &Apache::lonmsg::user_normal_msg_raw($cnum,$cdom,$subj_prefix.
                   1775:                                              ' ['.$recipient.']',$msgsubj,undef,
                   1776:                         undef,undef,undef,$usermsgid,undef,undef,$specialmsgid);
1.13      albertel 1777:                         my ($uname,$udom) = split(/:/,$recipient);
1.1       albertel 1778:                         push(@recusers,$uname);
                   1779:                         push(@recudoms,$udom);
                   1780:                     }
                   1781:                 }
                   1782:                 if (@recusers) {
                   1783:                     my $specialmessage;
1.13      albertel 1784:                     my $sentsubj = 
                   1785: 			$subj_prefix.' ('.$numspecial.' sent) '.$msgsubj;
1.1       albertel 1786:                     $sentsubj = &HTML::Entities::encode($sentsubj,'<>&"');
                   1787:                     my $sentmsgid = 
                   1788: 			&Apache::lonmsg::buildmsgid($stamp,$sentsubj,$msgname,
                   1789: 						    $msgdom,$msgcount,$context,
                   1790: 						    $pid);
                   1791:                     ($specialmsgid,$specialmessage) = &Apache::lonmsg::packagemsg($msgsubj,$savemsg,
                   1792:                             undef,undef,undef,\@recusers,\@recudoms,$sentmsgid);
                   1793:                     $record_sent = &Apache::lonmsg::store_sent_mail($specialmsgid,$specialmessage);
                   1794:                 }
                   1795:             } else {
                   1796:                 &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');
                   1797:             }
                   1798:         }
                   1799:     } else {
                   1800: 	&printheader($r,'','No messages sent.'); 
                   1801:     }
                   1802:     if ($sendstatus=~/^(\s*(?:ok|con_delayed)\s*)*$/) {
1.5       albertel 1803: 	$r->print('<br /><span class="LC_success">'.&mt('Completed.').'</span>');
1.1       albertel 1804: 	if ($env{'form.displayedcrit'}) {
                   1805: 	    &discrit($r);
                   1806: 	} else {
                   1807: 	    &Apache::loncommunicate::menu($r);
                   1808: 	}
                   1809:     } else {
1.5       albertel 1810: 	$r->print('<p><span class="LC_error">'.&mt('Could not deliver message').'</span> '.
1.25      foxr     1811: 		  &mt('Please use the browser "Back" button and correct the recipient addresses '."($sendstatus)").'</p>');
1.1       albertel 1812:     }
                   1813: }
                   1814: 
                   1815: # ===================================================================== Handler
                   1816: 
                   1817: sub handler {
                   1818:     my $r=shift;
                   1819: 
                   1820: # ----------------------------------------------------------- Set document type
                   1821:     
                   1822:     &Apache::loncommon::content_type($r,'text/html');
                   1823:     $r->send_http_header;
                   1824:     
                   1825:     return OK if $r->header_only;
                   1826:     
                   1827: # --------------------------- Get query string for limited number of parameters
                   1828:     &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'},
                   1829:         ['display','replyto','forward','markread','markdel','markunread',
                   1830:          'sendreply','compose','sendmail','critical','recname','recdom',
                   1831:          'recordftf','sortedby','block','folder','startdis','interdis',
1.23      www      1832: 	 'showcommentbaseurl','dismode','group','subject','text']);
1.1       albertel 1833:     $sqs='&sortedby='.$env{'form.sortedby'};
                   1834: 
                   1835: # ------------------------------------------------------ They checked for email
                   1836:     unless ($env{'form.block'}) {
                   1837:         &Apache::lonnet::put('email_status',{'recnewemail'=>0});
                   1838:     }
                   1839: 
                   1840: # ----------------------------------------------------------------- Breadcrumbs
                   1841: 
                   1842:     &Apache::lonhtmlcommon::clear_breadcrumbs();
                   1843:     &Apache::lonhtmlcommon::add_breadcrumb
                   1844:         ({href=>"/adm/communicate",
                   1845:           text=>"Communication/Messages",
                   1846:           faq=>12,bug=>'Communication Tools',});
                   1847: 
                   1848: # ------------------------------------------------------------------ Get Folder
                   1849: 
                   1850:     my $folder=$env{'form.folder'};
                   1851:     unless ($folder) { 
                   1852: 	$folder=''; 
                   1853:     } else {
1.29      www      1854: 	$sqs.='&folder='.&escape($folder);
1.1       albertel 1855:     }
                   1856: # ------------------------------------------------------------ Get Display Mode
                   1857: 
                   1858:     my $dismode=$env{'form.dismode'};
                   1859:     unless ($dismode) { 
                   1860: 	$dismode=''; 
                   1861:     } else {
1.29      www      1862: 	$sqs.='&dismode='.&escape($dismode);
1.1       albertel 1863:     }
                   1864: 
                   1865: # --------------------------------------------------------------------- Display
                   1866: 
                   1867:     $startdis=$env{'form.startdis'};
                   1868:     $startdis--;
                   1869:     unless ($startdis) { $startdis=0; }
                   1870: 
                   1871:     $interdis=$env{'form.interdis'};
                   1872:     unless ($interdis) { $interdis=20; }
                   1873:     $sqs.='&interdis='.$interdis;
                   1874: 
                   1875:     if ($env{'form.firstview'}) {
                   1876: 	$startdis=0;
                   1877:     }
                   1878:     if ($env{'form.lastview'}) {
                   1879: 	$startdis=-1;
                   1880:     }
                   1881:     if ($env{'form.prevview'}) {
                   1882: 	$startdis--;
                   1883:     }
                   1884:     if ($env{'form.nextview'}) {
                   1885: 	$startdis++;
                   1886:     }
                   1887:     my $postedstartdis=$startdis+1;
                   1888:     $sqs.='&startdis='.$postedstartdis;
                   1889: 
                   1890: # --------------------------------------------------------------- Render Output
                   1891: 
                   1892:     if ($env{'form.display'}) {
                   1893: 	&displaymessage($r,$env{'form.display'},$folder);
                   1894:     } elsif ($env{'form.replyto'}) {
                   1895: 	&compout($r,'',$env{'form.replyto'},undef,undef,$folder,$dismode);
                   1896:     } elsif ($env{'form.confirm'}) {
                   1897: 	&printheader($r,'','Confirmed Receipt');
                   1898: 	foreach (keys %env) {
                   1899: 	    if ($_=~/^form\.rec\_(.*)$/) {
                   1900: 		$r->print('<b>'.&mt('Confirming Receipt').':</b> '.
                   1901: 			  &Apache::lonmsg::user_crit_received($1).'<br>');
                   1902: 	    }
                   1903: 	    if ($_=~/^form\.reprec\_(.*)$/) {
                   1904: 		my $msgid=$1;
                   1905: 		$r->print('<b>'.&mt('Confirming Receipt').':</b> '.
                   1906: 			  &Apache::lonmsg::user_crit_received($msgid).'<br>');
                   1907: 		&compout($r,'','','',$msgid);
                   1908: 	    }
                   1909: 	}
                   1910: 	&discrit($r);
                   1911:     } elsif ($env{'form.critical'}) {
                   1912: 	&printheader($r,'','Displaying Critical Messages');
                   1913: 	&discrit($r);
                   1914:     } elsif ($env{'form.forward'}) {
                   1915: 	&compout($r,$env{'form.forward'},undef,undef,undef,$folder);
                   1916:     } elsif ($env{'form.markdel'}) {
                   1917: 	&printheader($r,'','Deleted Message');
1.9       albertel 1918: 	my ($result,$msg) = 
                   1919: 	    &statuschange($env{'form.markdel'},'deleted',$folder);
                   1920: 	if (!$result) {
1.10      albertel 1921: 	    $r->print('<p class="LC_error">'.
                   1922: 		      &mt('Failed to delete the message.').'</p>'.
1.9       albertel 1923: 		      '<p class="LC_error">'.$msg."</p>\n");
                   1924: 	}
1.1       albertel 1925: 	&Apache::loncommunicate::menu($r);
                   1926: 	&disall($r,($folder?$folder:$dismode));
                   1927:     } elsif ($env{'form.markedmove'}) {
1.9       albertel 1928: 	my ($total,$failed,@failed_msg)=(0,0);
                   1929: 	foreach my $key (keys(%env)) {
                   1930: 	    if ($key=~/^form\.delmark_(.*)$/) {
                   1931: 		my ($result,$msg) =
1.29      www      1932: 		    &movemsg(&unescape($1),$folder,
1.9       albertel 1933: 			     $env{'form.movetofolder'});
                   1934: 		if ($result) {
                   1935: 		    $total++;
                   1936: 		} else {
                   1937: 		    $failed++;
                   1938: 		    push(@failed_msg,$msg);
                   1939: 		}
1.1       albertel 1940: 	    }
                   1941: 	}
                   1942: 	&printheader($r,'','Moved Messages');
1.9       albertel 1943: 	if ($failed) {
                   1944: 	    $r->print('<p class="LC_error">
1.10      albertel 1945:                           '.&mt('Failed to move [_1] message(s)',$failed).
                   1946: 		      '</p>');
1.9       albertel 1947: 	    $r->print('<p class="LC_error">'.
                   1948: 		      join("</p>\n<p class=\"LC_error\">",@failed_msg).
                   1949: 		      "</p>\n");
                   1950: 	}
1.10      albertel 1951: 	$r->print(&mt('Moved [_1] message(s)',$total).'<p>');
1.1       albertel 1952: 	&Apache::loncommunicate::menu($r);
                   1953: 	&disall($r,($folder?$folder:$dismode));
                   1954:     } elsif ($env{'form.markeddel'}) {
1.9       albertel 1955: 	my ($total,$failed,@failed_msg)=(0,0);
                   1956: 	foreach my $key (keys(%env)) {
                   1957: 	    if ($key=~/^form\.delmark_(.*)$/) {
                   1958: 		my ($result,$msg) = 
1.29      www      1959: 		    &statuschange(&unescape($1),'deleted',
1.9       albertel 1960: 				  $folder);
                   1961: 		if ($result) {
                   1962: 		    $total++;
                   1963: 		} else {
                   1964: 		    $failed++;
                   1965: 		    push(@failed_msg,$msg);
                   1966: 		}
1.1       albertel 1967: 	    }
                   1968: 	}
                   1969: 	&printheader($r,'','Deleted Messages');
1.9       albertel 1970: 	if ($failed) {
                   1971: 	    $r->print('<p class="LC_error">
1.10      albertel 1972:                           '.&mt('Failed to delete [_1] message(s)',$failed).
                   1973: 		      '</p>');
1.9       albertel 1974: 	    $r->print('<p class="LC_error">'.
                   1975: 		      join("</p>\n<p class=\"LC_error\">",@failed_msg).
                   1976: 		      "</p>\n");
                   1977: 	}
1.10      albertel 1978: 	$r->print(&mt('Deleted [_1] message(s)',$total).'<p>');
1.1       albertel 1979: 	&Apache::loncommunicate::menu($r);
                   1980: 	&disall($r,($folder?$folder:$dismode));
                   1981:     } elsif ($env{'form.markunread'}) {
                   1982: 	&printheader($r,'','Marked Message as Unread');
                   1983: 	&statuschange($env{'form.markunread'},'new');
                   1984: 	&Apache::loncommunicate::menu($r);
                   1985: 	&disall($r,($folder?$folder:$dismode));
                   1986:     } elsif ($env{'form.compose'}) {
                   1987: 	&compout($r,'','',$env{'form.compose'});
                   1988:     } elsif ($env{'form.recordftf'}) {
                   1989: 	&facetoface($r,$env{'form.recordftf'});
                   1990:     } elsif ($env{'form.block'}) {
                   1991:         &examblock($r,$env{'form.block'});
                   1992:     } elsif ($env{'form.sendmail'}) {
                   1993: 	&sendoffmail($r,$folder);
                   1994: 	if ($env{'form.storebasecomment'}) {
                   1995: 	    &storecomment($r);
                   1996: 	}
                   1997: 	if (($env{'form.rsspost'}) && ($env{'request.course.id'})) {
                   1998: 	    &Apache::lonrss::addentry($env{'course.'.$env{'request.course.id'}.'.num'},
                   1999: 				      $env{'course.'.$env{'request.course.id'}.'.domain'},
                   2000: 				      'Course_Announcements',
                   2001: 				      $env{'form.subject'},
                   2002: 				      $env{'form.message'},'/adm/communicate','public');
                   2003: 	}
                   2004: 	&disall($r,($folder?$folder:$dismode));
                   2005:     } elsif ($env{'form.newfolder'}) {
                   2006: 	&printheader($r,'','New Folder');
                   2007: 	&makefolder($env{'form.newfolder'});
                   2008: 	&Apache::loncommunicate::menu($r);
                   2009: 	&disall($r,$env{'form.newfolder'});
                   2010:     } elsif ($env{'form.showcommentbaseurl'}) {
                   2011: 	&storedcommentlisting($r);
                   2012:     } else {
                   2013: 	&printheader($r,'','Display All Messages');
                   2014: 	&Apache::loncommunicate::menu($r); 
                   2015: 	&disall($r,($folder?$folder:$dismode));
                   2016:     }
                   2017:     $r->print(&Apache::loncommon::end_page());
                   2018:     return OK;
                   2019: }
                   2020: # ================================================= Main program, reset counter
                   2021: 
                   2022: =pod
                   2023: 
                   2024: =back
                   2025: 
                   2026: =cut
                   2027: 
                   2028: 1; 
                   2029: 
                   2030: __END__
                   2031: 
                   2032: 
                   2033: 
                   2034: 
                   2035: 
                   2036: 
                   2037: 

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