File:  [LON-CAPA] / loncom / interface / lonmsgdisplay.pm
Revision 1.126: download - view: text, annotated - select for diffs
Thu Jun 11 16:48:33 2009 UTC (14 years, 11 months ago) by bisitz
Branches: MAIN
CVS tags: HEAD
Functionslist:
- Moved sub routines from lontemplate.pm to lonhtmlcommon.pm to allow wider access
- Renamed sub routine names:
    - start_functionslist -> start_funclist
    - item_functionslist -> add_item_funclist
    - end_functionslist -> end_funclist
- Added line breaks for HTML source code
- Added pod documentation to sub routines
- Adjusted sub routine calls

Changes related to modification of LON-CAPA screen header:
- Moved functionslists into new standard screen header "head_subbox"

Other changes:
- Got rid of special style for fieldset legend; adjusted texts
  (lonmsgdisplay.pm, loncommon.pm, de.pm, zh.pm)
- Added warning/info style to warning/info message for weboutput
  and corrected tex output for "No page information provided" message
  (lonsimplepage.pm)
- Optimized floating for functions/actions
  (lonmsgdisplay.pm)

    1: # The LearningOnline Network with CAPA
    2: # Routines for messaging display
    3: #
    4: # $Id: lonmsgdisplay.pm,v 1.126 2009/06/11 16:48:33 bisitz Exp $
    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::lonmsgdisplay: supports internal messaging
   37: 
   38: =head1 SYNOPSIS
   39: 
   40: lonmsgdisplay provides a handler to allow users to read, send, 
   41: and delete messages, and to create and delete message folders,
   42: and to move messages between folders.
   43: 
   44: =head1 OVERVIEW
   45: 
   46: =head2 Messaging Overview
   47: 
   48: X<messages>LON-CAPA provides an internal messaging system similar to
   49: email, but customized for LON-CAPA's usage. LON-CAPA implements its
   50: own messaging system, rather then building on top of email, because of
   51: the features LON-CAPA messages can offer that conventional e-mail can
   52: not:
   53: 
   54: =over 4
   55: 
   56: =item * B<Critical messages>: A message the recipient B<must>
   57: acknowlegde receipt of before they are allowed to continue using the
   58: system, preventing a user from claiming they never got a message
   59: 
   60: =item * B<Receipts>: LON-CAPA can reliably send reciepts informing the
   61: sender that it has been read; again, useful for preventing students
   62: from claiming they did not see a message. (While conventional e-mail
   63: has some reciept support, it's sporadic, e-mail client-specific, and
   64: generally the receiver can opt to not send one, making it useless in
   65: this case.)
   66: 
   67: =item * B<Context>: LON-CAPA knows about the sender, such as where
   68: they are in a course. When a student mails an instructor asking for
   69: help on the problem, the instructor receives not just the student's
   70: question, but all submissions the student has made up to that point,
   71: the user's rendering of the problem, and the complete view the student
   72: saw of the resource, including discussion up to that point. Finally,
   73: the instructor is reading all of this inside of LON-CAPA, not their
   74: email program, so they have full access to LON-CAPA's grading
   75: interface, or other features they may wish to use in response to the
   76: student's query.
   77: 
   78: =item * B<Blocking>: LON-CAPA can block selected communication 
   79: features for students during an online exam. A course coordinator or
   80: instructor can set an open and close date/time for scheduled online
   81: exams in a course. If a user uses the LON-CAPA internal messaging 
   82: system to display e-mails during the scheduled blocking event,  
   83: display of all e-mail sent during the blocking period will be 
   84: suppressed, and a message of explanation, including details of the 
   85: currently active blocking periods will be displayed instead. A user 
   86: who has a course coordinator or instructor role in a course will be
   87: unaffected by any blocking periods for the course, unless the user
   88: also has a student role in the course, AND has selected the student role.
   89: 
   90: =back
   91: 
   92: Users can ask LON-CAPA to forward messages to conventional e-mail
   93: addresses on their B<PREF> screen, but generally, LON-CAPA messages
   94: are much more useful than traditional email can be made to be, even
   95: with HTML support.
   96: 
   97: =cut
   98: 
   99: use strict;
  100: use Apache::lonnet;
  101: use HTML::TokeParser();
  102: use Apache::Constants qw(:common);
  103: use Apache::loncommon();
  104: use Apache::lonhtmlcommon();
  105: use Apache::lontexconvert();
  106: use HTML::Entities();
  107: use Apache::lonlocal;
  108: use Apache::loncommunicate;
  109: use Apache::lonfeedback;
  110: use Apache::lonrss();
  111: use Apache::lonselstudent();
  112: use lib '/home/httpd/lib/perl/';
  113: use LONCAPA;
  114: 
  115: # Querystring component with sorting type
  116: my $sqs;
  117: my $startdis;
  118: 
  119: # ============================================================ List all folders
  120: 
  121: sub folderlist {
  122:     my ($folder,$msgstatus) = @_;
  123:     my %lt = &Apache::lonlocal::texthash(
  124:                 actn => 'Action',
  125:                 fold => 'Folder',
  126:                 show => 'Show',
  127:                 status => 'Message Status',
  128:                 go   => 'Go',
  129:                 nnff => 'New Name for Folder',
  130:                 newn => 'New Name',
  131:                 thfm => 'The folder may not be renamed',
  132:                 fmnb => 'folder may not be renamed as it is a folder provided by the system.',
  133:                 asth => 'as this name is already in use for a system-provided or user-defined folder.',
  134:                 the => 'The',
  135:                 tnfm => 'The new folder may not be named',
  136: 
  137:     );
  138: 
  139: 	# set se lastvisit for the new mail check in the toplevel menu
  140: 	&Apache::lonnet::appenv({'user.mailcheck.lastvisit'=>time});
  141: 
  142:     my %actions = &Apache::lonlocal::texthash(
  143:                                 view => 'View Folder',
  144:                                 rename => 'Rename Folder',
  145:                                 delete => 'Delete Folder',
  146:     );
  147:     $actions{'select_form_order'} = ['view','rename','delete'];
  148: 
  149:     my %statushash = &get_msgstatus_types();
  150: 
  151:     $statushash{'select_form_order'} = ['','new','read','replied','forwarded'];
  152: 
  153:     my %permfolders = &get_permanent_folders();
  154:     my $permlist = join("','",sort(keys(%permfolders)));
  155:     my ($permlistkeys,$permlistvals);
  156:     foreach my $key (sort(keys(%permfolders))) {
  157:         $permlistvals .= $permfolders{$key}."','";
  158:         $permlistkeys .= $key."','";
  159:     }
  160:     $permlistvals =~ s/','$//;
  161:     $permlistkeys =~ s/','$//;
  162:     my %gotfolders = &Apache::lonmsg::get_user_folders();
  163:     my %userfolders;
  164: 
  165:     foreach my $key (keys(%gotfolders)) {
  166:         $key =~ s/(['"])/\$1/g; #' stupid emacs
  167:         $userfolders{$key} = $key;
  168:     }
  169:     my @userorder = sort(keys(%userfolders));
  170:     my %formhash = (%permfolders,%userfolders);
  171:     my $folderlist = join("','",@userorder);
  172:     $folderlist .= "','".$permlistvals;
  173: 
  174:     $formhash{'select_form_order'} = ['','critical',@userorder,'sent','trash'];
  175:     my $output = qq|<script type="text/javascript">
  176: function folder_choice(targetform,caller) {
  177:     var permfolders_keys = new Array('$permlistkeys');
  178:     var permfolders_vals = new Array('$permlistvals');
  179:     var allfolders = new Array('$folderlist');
  180:     if (caller == 'change') {
  181:         if (targetform.folderaction.options[targetform.folderaction.selectedIndex].value == 'rename') {
  182:             for (var i=0; i<permfolders_keys.length; i++) {
  183:                 if (permfolders_keys[i] == targetform.folder.value) {
  184:                     alert("$lt{'the'} '"+permfolders_vals[i]+"' $lt{'fmnb'}");
  185:                     return;
  186:                 }
  187:             }
  188:             var foldername=prompt('$lt{'nnff'}','$lt{'newn'}');
  189:             if (foldername) {
  190:                 targetform.renamed.value=foldername;
  191:                 for (var i=0; i<allfolders.length; i++) {
  192:                     if (allfolders[i] == foldername) {
  193:                         alert("$lt{'thfm'} '"+foldername+"' $lt{'asth'}");
  194:                         return;
  195:                     }
  196:                 }
  197:                 targetform.submit();
  198:             }
  199:         }
  200:         else {
  201:             targetform.submit();
  202:         }
  203:     }
  204:     if (caller == 'new') {
  205:         var newname=targetform.newfolder.value;
  206:         if (newname) {
  207:             for (var i=0; i<allfolders.length; i++) {
  208:                 if (allfolders[i] == newname) {
  209:                     alert("$lt{'tnfm'} '"+newname+"' $lt{'asth'}");
  210:                     return;
  211:                 }
  212:             }
  213:             targetform.submit();
  214:         }
  215:     }
  216: }
  217: </script>|;
  218:     my %show = ('select_form_order' => [10,20,50,100,200],
  219: 		map {$_=>$_} (10,20,50,100,200));
  220: 		
  221: 		   
  222:     $output .= '
  223: <form method="post" action="/adm/email" name="folderlist">
  224: <table border="0" cellspacing="2" cellpadding="2">
  225:  <tr>
  226:   <td align="left">
  227:    <table border="0" cellspacing="2" cellpadding="2">
  228:     <tr>
  229:      <td align="center"><b>'.$lt{'fold'}.'</b><br />'."\n".
  230:          &Apache::loncommon::select_form($folder,'folder',%formhash).'
  231:      </td>
  232:      <td align="center"><b>'.$lt{'show'}.'</b><br />'."\n".
  233:          &Apache::loncommon::select_form($env{'form.interdis'},'interdis',
  234: 					 %show).'
  235:      </td>
  236:      <td align="center"><b>'.$lt{'status'}.'</b><br />'."\n".
  237:        &Apache::loncommon::select_form($msgstatus,'msgstatus',%statushash).'
  238:      </td>
  239:      <td align="center"><b>'.$lt{'actn'}.'</b><br />'.
  240:          &Apache::loncommon::select_form('view','folderaction',%actions).'
  241:      </td><td><br />'.
  242:     '<input type="button" value="'.$lt{'go'}.'" onClick="javascript:folder_choice(this.form,'."'change'".');" />
  243:      </td>
  244:     </tr>
  245:    </table>
  246:   </td><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td>
  247:   <td align="right">
  248:    <table><tr><td><br />
  249:     <input type="button" value="'.&mt('New Folder').
  250:     '" onClick="javascript:folder_choice(this.form,'."'new'".');" /></td>'.
  251:     '<td align="center"><b>'.&mt('Name').'</b><br />'.
  252:     '<input type="text" size="15" name="newfolder" value="" />
  253:     </td></tr></table>
  254:   </td>
  255:  </tr>
  256: </table>'."\n".
  257:     '<input type="hidden" name="sortedby" value="'.$env{'form.sortedby'}.'" />'.
  258:     '<input type="hidden" name="renamed" value="" />'.
  259:         ($folder=~/^critical/?'</form>':'');
  260:     return $output;
  261: }
  262: 
  263: sub get_permanent_folders {
  264:     my %permfolders = 
  265: 	&Apache::lonlocal::texthash(''         => 'INBOX',
  266: 				    'trash'    => 'TRASH',
  267: 				    'critical' => 'Critical',
  268: 				    'sent'     => 'Sent Messages',
  269: 				    );
  270:     return %permfolders;
  271: }
  272: 
  273: sub get_msgstatus_types {
  274:     my %statushash = &Apache::lonlocal::texthash(
  275:                                 '' => 'Any',
  276:                                 new => 'Unread',
  277:                                 read => 'Read',
  278:                                 replied => 'Replied to',
  279:                                 forwarded => 'Forwarded',
  280:     );
  281:     return %statushash;
  282: }
  283: 
  284: sub scrollbuttons {
  285:     my ($start,$maxdis,$first,$finish,$total,$msgstatus)=@_;
  286:     unless ($total>0) { return ''; }
  287:     $start++; $maxdis++;$first++;$finish++;
  288: 
  289:     my %statushash = &get_msgstatus_types();
  290:     my $status;
  291:     if ($msgstatus eq '') {
  292:         $status = &mt('All');
  293:     } else {
  294:         $status = $statushash{$msgstatus};
  295:     }
  296:     return
  297:    '<b>'.&mt('Page').'</b>: '. 
  298:    '<input type="submit" name="firstview" value="'.&mt('First').'" />'.
  299:    '<input type="submit" name="prevview" value="'.&mt('Previous').'" />'.
  300:    '<input type="text" size="5" name="startdis" value="'.$start.'" onChange="this.form.submit()" /> of '.$maxdis.
  301:    '<input type="submit" name="nextview" value="'.&mt('Next').'" />'.
  302:    '<input type="submit" name="lastview" value="'.&mt('Last').'" /><br />'.
  303:    &mt('<b>[_1] messages</b>: showing messages [_2] through [_3] of [_4].',$status,$first,$finish,$total).'</form>';
  304: }
  305: # =============================================================== Status Change
  306: 
  307: sub statuschange {
  308:     my ($msgid,$newstatus,$folder)=@_;
  309:     my $suffix=&Apache::lonmsg::foldersuffix($folder);
  310:     my %status=&Apache::lonnet::get('email_status'.$suffix,[$msgid]);
  311:     if ($status{$msgid}=~/^error\:/) { $status{$msgid}=''; }
  312:     unless ($status{$msgid}) { $status{$msgid}='new'; }
  313:     unless (($status{$msgid} eq 'replied') || 
  314:             ($status{$msgid} eq 'forwarded')) {
  315: 	&Apache::lonnet::put('email_status'.$suffix,{$msgid => $newstatus});
  316:     }
  317:     if (($newstatus eq 'deleted') || ($newstatus eq 'new')) {
  318: 	&Apache::lonnet::put('email_status'.$suffix,{$msgid => $newstatus});
  319:     }
  320:     if ($newstatus eq 'deleted') {
  321: 	return &movemsg($msgid,$folder,'trash');
  322:     }
  323:     return ;
  324: }
  325: 
  326: # ============================================================= Make new folder
  327: 
  328: sub makefolder {
  329:     my ($newfolder) = @_;
  330:     my %permfolders = &get_permanent_folders();
  331:     my %userfolders = &Apache::lonmsg::get_user_folders();
  332:     my ($outcome,$warning);
  333:     if (defined($userfolders{$newfolder})) {
  334:         return &mt('The folder name: "[_1]" is already in use for an existing folder.',$newfolder);
  335:     }
  336:     foreach my $perm (keys(%permfolders)) {
  337:         if ($permfolders{$perm} eq $newfolder) {
  338:             return &mt('The folder name: "[_1]" is already used for one of the folders automatically generated by the system.',$newfolder);
  339:         }
  340:     } 
  341:     if (&get_msgfolder_lock() eq 'ok') {
  342:         my %counter_hash = &Apache::lonnet::get('email_folders',["\0".'idcount']);
  343:         my $lastcount = $counter_hash{"\0".'idcount'};
  344:         my $folder_id = $lastcount + 1;
  345:         while (defined($userfolders{$folder_id})) {
  346:             $folder_id ++;
  347:         }
  348:         my %folderinfo = ( id      => $folder_id,
  349:                            created => time, );
  350:         $outcome =  
  351: 	    &Apache::lonnet::put('email_folders',{$newfolder => \%folderinfo,
  352: 						  "\0".'idcount' => $folder_id});
  353:         my $releaseresult = &release_msgfolder_lock();
  354:         if ($releaseresult ne 'ok') {
  355:             $warning = $releaseresult;
  356:         }
  357:     } else {
  358:         $outcome = 
  359: 	    &mt('Error - could not obtain lock on message folders record.');
  360:     }
  361:     return ($outcome,$warning);
  362: }
  363: 
  364: # ============================================================= Delete folder
  365: 
  366: sub deletefolder {
  367:     my ($folder)=@_;
  368:     my %permfolders = &get_permanent_folders();
  369:     if (defined($permfolders{$folder})) {
  370:         return &mt('The folder "[_1]" may not be deleted',$folder); 
  371:     }
  372:     my %userfolders = &Apache::lonmsg::get_user_folders();
  373:     if (!defined($userfolders{$folder})) {
  374:         return &mt('The folder "[_1]" does not exist so deletion is not required.',
  375:                    $folder);
  376:     }
  377:     # check folder is empty;
  378:     my $suffix=&Apache::lonmsg::foldersuffix($folder);
  379:     my @messages = &Apache::lonnet::getkeys('nohist_email'.$suffix);
  380:     if (@messages > 0) {
  381:         return &mt('The folder "[_1]" contains messages so it may not be deleted.',$folder).
  382:                '<br />'.
  383:                &mt('Delete or move the messages to a different folder first.');
  384:     }
  385:     my $delresult = &Apache::lonnet::del('email_folders',[$folder]);
  386:     return $delresult;
  387: }
  388: 
  389: sub renamefolder {
  390:     my ($folder) = @_;
  391:     my $newname = $env{'form.renamed'};
  392:     my %permfolders = &get_permanent_folders();
  393:     if ($env{'form.renamed'} eq '') {
  394:         return &mt('The folder "[_1]" may not be renamed to "[_2]" as the new name you requested is an invalid name.',$folder,$newname);
  395:     }
  396:     if (defined($permfolders{$folder})) {
  397:         return &mt('The folder "[_1]" may not be renamed as it is a folder provided by the system.',$folder);
  398:     }
  399:     if (defined($permfolders{$newname})) {
  400:         return &mt('The folder "[_1]" may not be renamed to "[_2]" as the new name you requested is reserved for folders provided automatically by the system.',$folder,$newname);
  401:     }
  402:     my %userfolders = &Apache::lonmsg::get_user_folders();
  403:     if (defined($userfolders{$newname})) {
  404:         return &mt('The folder "[_1]" may not be renamed to "[_2]" because the new name you requested is already being used for an existing folder.',$folder,$newname);
  405:     }
  406:     if (!defined($userfolders{$folder})) {
  407:         return &mt('The folder "[_1]" could not be renamed to "[_2]" because the folder does not exist.',$folder,$newname);
  408:     }
  409:     my %folderinfo;
  410:     if (ref($userfolders{$folder}) eq 'HASH') {
  411:         %folderinfo = %{$userfolders{$folder}};
  412:     } else {
  413:         %folderinfo = ( id      => $folder,
  414:                         created => $userfolders{$folder},);
  415:     }
  416:     my $outcome =
  417:      &Apache::lonnet::put('email_folders',{$newname => \%folderinfo});
  418:     if ($outcome eq 'ok') {
  419:         $outcome = &Apache::lonnet::del('email_folders',[$folder]);
  420:     }
  421:     return $outcome;
  422: }
  423: 
  424: sub get_msgfolder_lock {
  425:     # get lock for mail folder counter.
  426:     my $lockhash = { "\0".'lock_counter' => time, };
  427:     my $tries = 0;
  428:     my $gotlock = &Apache::lonnet::newput('email_folders',$lockhash);
  429:     while (($gotlock ne 'ok') && $tries <3) {
  430:         $tries ++;
  431:         sleep(1);
  432:         $gotlock = &Apache::lonnet::newput('email_folders',$lockhash);
  433:     }
  434:     return $gotlock;
  435: }
  436: 
  437: sub release_msgfolder_lock {
  438:     #  remove lock
  439:     my @del_lock = ("\0".'lock_counter');
  440:     my $dellockoutcome=&Apache::lonnet::del('email_folders',\@del_lock);
  441:     if ($dellockoutcome ne 'ok') {
  442:         return ('<br />'.&mt('Warning: failed to release lock for counter').'<br />');
  443:     } else {
  444:         return 'ok';
  445:     }
  446: }
  447: 
  448: 
  449: # ======================================================== Move between folders
  450: 
  451: sub movemsg {
  452:     my ($msgid,$srcfolder,$trgfolder)=@_;
  453:     if ($srcfolder eq 'new') { $srcfolder=''; }
  454:     my $srcsuffix=&Apache::lonmsg::foldersuffix($srcfolder);
  455:     my $trgsuffix=&Apache::lonmsg::foldersuffix($trgfolder);
  456:     if ($srcsuffix eq $trgsuffix) {
  457: 	return (0,&mt('Message not moved, Attempted to move message to the same folder as it already is in.'));
  458:     }
  459: 
  460: # Copy message
  461:     my %message=&Apache::lonnet::get('nohist_email'.$srcsuffix,[$msgid]);
  462:     if (!exists($message{$msgid}) || $message{$msgid} eq '') {
  463: 	if (&Apache::lonnet::error(%message)) {
  464: 	    return (0,&mt('Message not moved, A network error occurred.'));
  465: 	} else {
  466: 	    return (0,&mt('Message not moved as the message is no longer in the source folder.'));
  467: 	}
  468:     }
  469: 
  470:     my $result =&Apache::lonnet::put('nohist_email'.$trgsuffix,
  471: 				     {$msgid => $message{$msgid}});
  472:     if (&Apache::lonnet::error($result)) {
  473: 	return (0,&mt('Message not moved, A network error occurred.'));
  474:     }
  475: 
  476: # Copy status
  477:     unless ($trgfolder eq 'trash') {
  478:        	my %status=&Apache::lonnet::get('email_status'.$srcsuffix,[$msgid]);
  479: 	# a non-existant status is the mark of an unread msg
  480: 	if (&Apache::lonnet::error(%status)) {
  481: 	    return (0,&mt('Message copied to new folder but status was not, A network error occurred.'));
  482: 	}
  483: 	my $result=&Apache::lonnet::put('email_status'.$trgsuffix,
  484: 					{$msgid => $status{$msgid}});
  485: 	if (&Apache::lonnet::error($result)) {
  486: 	    return (0,&mt('Message copied to new folder but status was not, A network error occurred.'));
  487: 	}
  488:     }
  489: 
  490: # Delete orginals
  491:     my $result_del_msg = 
  492: 	&Apache::lonnet::del('nohist_email'.$srcsuffix,[$msgid]);
  493:     my $result_del_stat =
  494: 	&Apache::lonnet::del('email_status'.$srcsuffix,[$msgid]);
  495:     if (&Apache::lonnet::error($result_del_msg)) {
  496: 	return (0,&mt('Message copied, but unable to delete the original from the source folder.'));
  497:     }
  498:     if (&Apache::lonnet::error($result_del_stat)) {
  499: 	return (0,&mt('Message copied, but unable to delete the original status from the source folder.'));
  500:     }
  501: 
  502:     return (1);
  503: }
  504: 
  505: # ======================================================= Display a course list
  506: 
  507: sub discourse {
  508:     my ($statushash) = @_;
  509:     my ($result,$active,$previous,$future);
  510:     my ($course_personnel,
  511: 	$current_members,
  512: 	$expired_members,
  513: 	$future_members) = 
  514: 	    &Apache::lonselstudent::get_people_in_class($env{'request.course.sec'});
  515:     unshift @$current_members, (@$course_personnel);
  516:     my %defaultUsers;
  517:     
  518:     my $tmptext;
  519:     if ($tmptext = &Apache::lonselstudent::render_student_list($current_members,
  520:                                                                "activeusers",
  521:                                                                "current",
  522:                                                                \%defaultUsers,
  523:                                                                1,"selectedusers",1,'email')
  524:        ) {
  525:        $result .= '<fieldset id="LC_activeusers"><legend>'
  526:                  .&mt('Bcc: course members with current access')
  527:                  .'</legend>'
  528:                  .'<form name="activeusers">';
  529:        $result .= $tmptext.'</form></fieldset><br />';
  530:        if (ref($statushash) eq 'HASH') {
  531:            $statushash->{'active'} = 1;
  532:        }
  533:     }
  534:     if ($tmptext = &Apache::lonselstudent::render_student_list($expired_members,
  535:                                                                "previoususers",
  536:                                                                "expired",
  537:                                                                \%defaultUsers,
  538:                                                                1, "selectedusers",0,'email')
  539:        ) {
  540:        $result .= '<fieldset id="LC_previoususers"><legend>'
  541:                  .&mt('Bcc: course members with expired access')
  542:                  .'</legend>'
  543:                  .'<form name="previoususers">';
  544:        $result .= $tmptext.'</form></fieldset><br />';
  545:        if (ref($statushash) eq 'HASH') {
  546:            $statushash->{'previous'} = 1;
  547:        }
  548: 
  549:     }
  550:     if ($tmptext = &Apache::lonselstudent::render_student_list($future_members,
  551:                                                                "futureusers",
  552:                                                                "future",
  553:                                                                \%defaultUsers,
  554:                                                                1, "selectedusers",0,'email')
  555:        ) {
  556:        $result .= '<fieldset id="LC_futureusers"><legend>'
  557:                  .&mt('Bcc: course members with future access')
  558:                  .'</legend>'
  559:                  .'<form name="previoususers">';
  560:        $result .= $tmptext.'</form></fieldset>';
  561:        if (ref($statushash) eq 'HASH') {
  562:            $statushash->{'future'} = 1;
  563:        }
  564: 
  565:     }
  566:     return $result;
  567: }
  568: 
  569: sub disgroup {
  570:     my ($r,$cdom,$cnum,$group,$access_status) = @_;
  571:     my $hasfloat;
  572:     #  Needs to be in a course
  573:     if (!($env{'request.course.fn'})) {
  574:         $r->print('<span class="LC_error">'.&mt('Error: you must have a course role selected to be able to send a broadcast message to a group in the course.').'</span>');
  575:         return;
  576:     }
  577:     if ($cdom eq '' || $cnum eq '') {
  578:         $r->print('<span class="LC_error">'.&mt('Error: could not determine domain or number of course').'</span>');
  579:         return;
  580:     }
  581:     my ($memberinfo,$numitems) =
  582:                  &Apache::longroup::group_memberlist($cdom,$cnum,$group,{},[]);
  583:     my @statustypes = ('active');
  584:     my $viewgrps = &Apache::lonnet::allowed('vcg',$env{'request.course.id'}.
  585:                    ($env{'request.course.sec'}?'/'.$env{'request.course.sec'}:''));
  586:     my $editgrps = &Apache::lonnet::allowed('mdg',$env{'request.course.id'}.
  587:                    ($env{'request.course.sec'}?'/'.$env{'request.course.sec'}:''));
  588:     if ($viewgrps || $editgrps) {
  589:         push(@statustypes,('future','previous'));
  590:     }
  591:     if (keys(%{$memberinfo}) == 0) {
  592:         $r->print('<span class="LC_warning">'.
  593:                   &mt('As this group has no members, there are no recipients to select').
  594:                   '</span>');
  595:         return;
  596:     } else {
  597:         $hasfloat = 1;
  598:         unless($env{'environment.wysiwygeditor'} eq 'on') {
  599:             $r->print('<div class="LC_left_float">');
  600:         }
  601:         my %Sortby = (
  602:                          active   => {},
  603:                          previous => {},
  604:                          future   => {},
  605:                      );
  606:         my %lt = &Apache::lonlocal::texthash(
  607:                                      'name'     => 'Name',
  608:                                      'usnm'     => 'Username',
  609:                                      'doma'     => 'Domain',
  610:                                      'active'   => 'Broadcast to Active Members',
  611:                                      'previous' => 'Broadcast (Bcc) to Former Members',
  612:                                      'future'   => 'Broadcast (Bcc) to Future Members',
  613:                                     );
  614:         foreach my $user (sort(keys(%{$memberinfo}))) {
  615:             my $status = $$memberinfo{$user}{status};
  616:             if ($env{'form.'.$status.'.sortby'} eq 'fullname') {
  617:                 push(@{$Sortby{$status}{$$memberinfo{$user}{fullname}}},$user);
  618:             } elsif ($env{'form.'.$status.'.sortby'} eq 'username') {
  619:                 push(@{$Sortby{$status}{$$memberinfo{$user}{uname}}},$user);
  620:             } elsif ($env{'form.'.$status.'.sortby'} eq 'domain') {
  621:                 push(@{$Sortby{$status}{$$memberinfo{$user}{udom}}},$user);
  622:             } else {
  623:                 push(@{$Sortby{$status}{$$memberinfo{$user}{fullname}}},$user);
  624:             }
  625:         }
  626:         $r->print(&group_check_uncheck());
  627:         foreach my $status (@statustypes)  {
  628:             if (ref($numitems) eq 'HASH') {
  629:                 if ((defined($$numitems{$status})) && ($$numitems{$status})) {
  630:                     my $formname = $status.'users';
  631:                     if (ref($access_status) eq 'HASH') {
  632:                         $access_status->{$status} = $$numitems{$status};
  633:                     }
  634:                     $r->print('<fieldset>'.
  635:                               '<legend>'.$lt{$status}.'</legend>'.
  636:                               '<form name="'.$formname.'">'.
  637:                               '<span class="LC_nobreak">'.
  638:                               '<input type="button" value="'.&mt('Check All').'" '.
  639:                               'onclick="javascript:toggleAll('."this.form,'check'".')" />'.
  640:                               '&nbsp;&nbsp;'.
  641:                               '<input type="button" value="'.&mt('Uncheck All').'" '.
  642:                               'onclick="javascript:toggleAll('."this.form,'uncheck'".')" />'.
  643:                               '</span>');
  644:                     if ($status eq 'active') {
  645:                         $r->print(('&nbsp;'x3).'<select name="groupmail">'.
  646:                                  '<option value="bcc" selected="selected">'.&mt('Bcc').'</option>'.
  647:                                  '<option value="cc">'.&mt('Cc').'</option>'.
  648:                                '</select>');
  649:                     }
  650:                     $r->print('<br />'.&Apache::loncommon::start_data_table().
  651:                                &Apache::loncommon::start_data_table_header_row().
  652:                                "<th>$lt{'name'}</th>".
  653:                                "<th>$lt{'usnm'}</th>".
  654:                                "<th>$lt{'doma'}</th>".
  655:                                &Apache::loncommon::end_data_table_header_row());
  656:                     foreach my $key (sort(keys(%{$Sortby{$status}}))) {
  657:                         foreach my $user (@{$Sortby{$status}{$key}}) {
  658:                             $r->print(&Apache::loncommon::start_data_table_row().
  659:                                 '<td><span class="LC_nobreak"><input type="checkbox" '.
  660:                                 'name="selectedusers_forminput" value="'.
  661:                                 $user.':'.$status.'" />'.
  662:                                 $$memberinfo{$user}{'fullname'}.'</span></td>'.
  663:                                 '<td>'.$$memberinfo{$user}{'uname'}.'</td>'.
  664:                                 '<td>'.$$memberinfo{$user}{'udom'}.'</td>'.
  665:                                 &Apache::loncommon::end_data_table_row());
  666:                         }
  667:                     }
  668:                     $r->print(&Apache::loncommon::end_data_table().'</form>'.
  669:                               '</fieldset><br />');
  670:                 }
  671:             }
  672:         }
  673:         unless($env{'environment.wysiwygeditor'} eq 'on') {
  674:             $r->print('</div>');
  675:         }
  676:     }
  677:     return $hasfloat;
  678: }
  679: 
  680: sub group_check_uncheck {
  681:     my $output = qq|
  682: <script type="text/javascript">
  683: function toggleAll(form,action) {
  684:     if (typeof(form.selectedusers_forminput.length)=="undefined") {
  685:          if (action == 'check') {
  686:             form.selectedusers_forminput.checked = true;
  687:          } else {
  688:             form.selectedusers_forminput.checked = false;
  689:         }
  690:     } else {
  691:         for (var i=0; i<form.selectedusers_forminput.length; i++) {
  692:             if (action == 'check') {
  693:                 form.selectedusers_forminput[i].checked = true;
  694:             } else {
  695:                 form.selectedusers_forminput[i].checked = false;
  696:             }
  697:         }
  698:     }
  699: }
  700: </script>
  701:     |;
  702: }
  703: 
  704: sub groupmail_header {
  705:     my ($action,$group,$cdom,$cnum) = @_;
  706:     my ($description,$refarg);
  707:     if (!$cdom || !$cnum) {
  708:         $cdom = $env{'course.'.$env{'request.course.id'}.'.domain'};
  709:         $cnum = $env{'course.'.$env{'request.course.id'}.'.num'};
  710:     }
  711:     if (exists($env{'form.ref'})) {
  712:         $refarg = 'ref='.$env{'form.ref'};
  713:     }
  714:     if (!$group) {
  715:         $group = $env{'form.group'};
  716:     }
  717:     if ($group eq '') {
  718:         return  '';
  719:     } else {
  720:         my %curr_groups = &Apache::longroup::coursegroups($cdom,$cnum,$group);
  721:         if (defined($curr_groups{$group})) {
  722:             my %groupinfo =
  723:                     &Apache::longroup::get_group_settings($curr_groups{$group});
  724:             $description = &unescape($groupinfo{'description'});
  725:         }
  726:     }
  727:     &Apache::lonhtmlcommon::clear_breadcrumbs();
  728:     if ($refarg) {
  729:         &Apache::lonhtmlcommon::add_breadcrumb
  730:             ({href=>"/adm/coursegroups",
  731:               text=>"Groups",
  732:               title=>"View course groups"});
  733:     }
  734:     &Apache::lonhtmlcommon::add_breadcrumb
  735:         ({href=>"/adm/$cdom/$cnum/$group/smppg?$refarg",
  736:           text=>"Group: $description",
  737:           title=>"Go to group's home page"},
  738:          {href=>"/adm/email?compose=group&amp;group=".
  739:                 "$env{'form.group'}&amp;$refarg",
  740:           text=>"Send a Message in a Group",
  741:           title=>"Compose Group Message"},);
  742:     if ($action eq 'sending') {
  743:             &Apache::lonhtmlcommon::add_breadcrumb
  744:                          ({text=>"Messages being sent.",
  745:                            title=>"E-mails sent"},);
  746:     }
  747:     my $groupheader = &Apache::loncommon::start_page('Group Message');
  748:     $groupheader .= &Apache::lonhtmlcommon::breadcrumbs
  749:                 ('Group - '.$env{'form.group'}.' Email');
  750:     return $groupheader;
  751: }
  752: 
  753: sub groupmail_sent {
  754:     my ($group,$cdom,$cnum) = @_;
  755:     my $refarg;
  756:     if (exists($env{'form.ref'})) {
  757:         $refarg = 'ref='.$env{'form.ref'};
  758:     }
  759:     my $output .= '<br /><br /><a href="/adm/email?compose=group&amp;group='.
  760:                   $group.'&amp;'.$refarg.'">'.
  761:                   &mt('Send another group message').'</a>'.'&nbsp;&nbsp;&nbsp;'.
  762:                   '<a href="/adm/'.$cdom.'/'.$cnum.'/'.$group.'/smppg?'.
  763:                   $refarg.'">'. &mt('Return to group page').'</a>';
  764:     return $output;
  765: }
  766: 
  767: # ==================================================== Display Critical Message
  768: 
  769: sub discrit {
  770:     my $r=shift;
  771:     my $header = '<h1>'.&mt('Critical Messages').'</h1>'
  772:                 .'<div class="LC_warning">'
  773:                 .&mt('Access to other pages will be prevented until you have moved all critical messages to your inbox.')
  774:                 .'</div><br />'
  775:                 .'<form action="/adm/email" method="post">'
  776:                 .'<input type="hidden" name="confirm" value="true" />';
  777:     my %what=&Apache::lonnet::dump('critical');
  778:     my $result = '';
  779:     foreach my $key (sort(keys(%what))) {
  780:         my %content=&Apache::lonmsg::unpackagemsg($what{$key});
  781:         next if ($content{'senderdomain'} eq '');
  782:         $result .= &Apache::lonhtmlcommon::start_pick_box()
  783:                   .&Apache::lonhtmlcommon::row_title(&mt('From'),undef,'LC_oddrow_value')
  784:                   .'<b>'.&Apache::loncommon::aboutmewrapper(
  785:                    &Apache::loncommon::plainname($content{'sendername'},$content{'senderdomain'}),$content{'sendername'},$content{'senderdomain'}).'</b>'
  786:                   .' ('.$content{'sendername'}.':'.$content{'senderdomain'}.')'
  787:                   .&Apache::lonhtmlcommon::row_closure(1)
  788:                   .&Apache::lonhtmlcommon::row_title(&mt('Date'),undef,'LC_evenrow_value')
  789:                   .$content{'time'}
  790:                   .&Apache::lonhtmlcommon::row_closure(1)
  791:                   .&Apache::lonhtmlcommon::row_title(&mt('Subject'),undef,'LC_oddrow_value')
  792:                   .$content{'subject'}
  793:                   .&Apache::lonhtmlcommon::row_closure(1)
  794:                   .&Apache::lonhtmlcommon::row_title(&mt('Message'),undef,'LC_evenrow_value')
  795:                   .'<pre>'.&Apache::lontexconvert::msgtexconverted($content{'message'}).'</pre>'
  796:                   .&Apache::lonhtmlcommon::row_closure()
  797:                   .&Apache::lonhtmlcommon::row_title('',undef,'LC_oddrow_value')
  798:                   .'<div class="LC_warning">';
  799:         my ($rec_button,$reprec_button);
  800:         $rec_button = &mt('Move to Inbox');
  801:         if (!$content{'noreplies'}) {
  802:             $reprec_button = &mt('Move to Inbox/Compose reply');
  803:         }
  804:         if ($content{'sendback'}) {
  805:             $rec_button = &mt('Confirm Receipt');
  806:             if (!$content{'noreplies'}) {
  807:                 $reprec_button = &mt('Confirm Receipt and Reply');
  808:             }
  809:             $result .= &mt('You have to confirm that you have received this message before you can view other pages. After confirmation, this message will be moved to your regular inbox');
  810:         } else {
  811:             $result .= &mt('Access to other pages will be prevented until you have moved the message to your inbox.'); 
  812:         }
  813:         $result .= '</div>'
  814:                   .&Apache::lonhtmlcommon::row_closure(1)
  815:                   .&Apache::lonhtmlcommon::row_title('',undef,'LC_evenrow_value')
  816:                   .'<input type="submit" name="rec_'.$key.'" value="'.$rec_button.'" />';
  817:         if (!$content{'noreplies'}) {
  818:             $result .= '<input type="submit" name="reprec_'.$key.'" '
  819:                       .'value="'.$reprec_button.'" />'
  820:         }
  821:         $result .= &Apache::lonhtmlcommon::row_closure(1)
  822:                   .&Apache::lonhtmlcommon::end_pick_box()
  823:                   .'<br />';
  824:     }
  825:     # Check to see if there were any messages.
  826:     if ($result eq '') {
  827:         $result = "<h2>".&mt('You have no critical messages.')."</h2>".
  828: 	    '<a href="/adm/roles">'.&mt('Select a course').'</a><br />'.
  829:             '<a href="/adm/email">'.&mt('Communicate').'</a>';
  830:     } else {
  831:         $r->print($header);
  832:     }
  833:     $r->print($result);
  834:     $r->print('<input type="hidden" name="displayedcrit" value="true" /></form>');
  835: }
  836: 
  837: sub sortedmessages {
  838:     my ($blocked,$startblock,$endblock,$numblocked,$folder,$msgstatus) = @_;
  839:     my $suffix=&Apache::lonmsg::foldersuffix($folder);
  840:     my @messages = &Apache::lonnet::getkeys('nohist_email'.$suffix);
  841:     #unpack the varibles and repack into temp for sorting
  842:     my @temp;
  843:     my %descriptions;
  844:     my %status_cache = 
  845: 	&Apache::lonnet::get('email_status'.&Apache::lonmsg::foldersuffix($folder),\@messages);
  846: 
  847:     my $get_received;
  848:     if ($folder eq 'sent' 
  849: 	&& ($env{'form.sortedby'} =~ m/^(rev)?(user|domain)$/)) {
  850: 	$get_received = 1;
  851:     }
  852: 
  853:     foreach my $msgid (@messages) {
  854:         next if ($msgid eq '');
  855: 	my $esc_msgid=&escape($msgid);
  856: 	my ($sendtime,$shortsubj,$fromname,$fromdomain,$status,$fromcid,$processid,$symb,$error) =
  857: 	    &Apache::lonmsg::unpackmsgid($esc_msgid,$folder,undef,
  858: 					 \%status_cache);
  859:         next if ($msgstatus ne '' && $msgstatus ne $status);
  860:         my $description = &get_course_desc($fromcid,\%descriptions);
  861: 	my @temp1 = ($sendtime,$shortsubj,$fromname,$fromdomain,$status,
  862: 		     $esc_msgid,$description);
  863: 	if ($get_received) {
  864: 	    my %message = &Apache::lonnet::get('nohist_email'.$suffix,
  865: 					       [$msgid]);
  866: 	    my %content = &Apache::lonmsg::unpackagemsg($message{$msgid});
  867: 	    push(@temp1,$content{'recuser'},$content{'recdomain'});
  868: 	}
  869:         # Check whether message was sent during blocking period.
  870:         if ($sendtime >= $startblock && ($sendtime <= $endblock && $endblock > 0) ) {
  871:             $$blocked{$msgid} = 'ON';
  872:             $$numblocked ++;
  873:         } else { 
  874:             push @temp ,\@temp1;
  875:         }
  876:     }
  877:     #default sort
  878:     @temp = sort  {$a->[0] <=> $b->[0]} @temp;    
  879:     if ($env{'form.sortedby'} eq "date"){
  880:         @temp = sort  {$a->[0] <=> $b->[0]} @temp;    
  881:     }
  882:     if ($env{'form.sortedby'} eq "revdate"){
  883:     	@temp = sort  {$b->[0] <=> $a->[0]} @temp; 
  884:     }
  885:     if ($env{'form.sortedby'} eq "user"){
  886: 	if ($get_received) {
  887: 	    @temp = sort  {lc($a->[7][0]) cmp lc($b->[7][0])} @temp;
  888: 	} else {
  889: 	    @temp = sort  {lc($a->[2])    cmp lc($b->[2])}    @temp;
  890: 	}
  891:     }
  892:     if ($env{'form.sortedby'} eq "revuser"){
  893: 	if ($get_received) {
  894: 	    @temp = sort  {lc($b->[7][0]) cmp lc($a->[7][0])} @temp;
  895: 	} else {
  896: 	    @temp = sort  {lc($b->[2])    cmp lc($a->[2])}    @temp;
  897: 	}
  898:     }
  899:     if ($env{'form.sortedby'} eq "domain"){
  900: 	if ($get_received) {
  901: 	    @temp = sort  {$a->[8][0] cmp $b->[8][0]} @temp;
  902: 	} else {
  903: 	    @temp = sort  {$a->[3]    cmp $b->[3]}    @temp;
  904: 	}
  905:     }
  906:     if ($env{'form.sortedby'} eq "revdomain"){
  907: 	if ($get_received) {
  908: 	    @temp = sort  {$b->[8][0] cmp $a->[8][0]} @temp;
  909: 	} else {
  910: 	    @temp = sort  {$b->[3]    cmp $a->[3]}    @temp;
  911: 	}
  912:     }
  913:     if ($env{'form.sortedby'} eq "subject"){
  914:         @temp = sort  {lc($a->[1]) cmp lc($b->[1])} @temp;
  915:     }
  916:     if ($env{'form.sortedby'} eq "revsubject"){
  917:         @temp = sort  {lc($b->[1]) cmp lc($a->[1])} @temp;
  918:     }
  919:     if ($env{'form.sortedby'} eq "course"){
  920:         @temp = sort  {lc($a->[6]) cmp lc($b->[6])} @temp;
  921:     }
  922:     if ($env{'form.sortedby'} eq "revcourse"){
  923:         @temp = sort  {lc($b->[6]) cmp lc($a->[6])} @temp;
  924:     }
  925:     if ($env{'form.sortedby'} eq "status"){
  926:         @temp = sort  {$a->[4] cmp $b->[4]} @temp;
  927:     }
  928:     if ($env{'form.sortedby'} eq "revstatus"){
  929:         @temp = sort  {$b->[4] cmp $a->[4]} @temp;
  930:     }
  931:     return @temp;
  932: }
  933: 
  934: sub get_course_desc {
  935:     my ($fromcid,$descriptions) = @_;
  936:     my $description;
  937:     if (!$fromcid) {
  938:         return $description;
  939:     } else {
  940:         if (defined($$descriptions{$fromcid})) {
  941:             $description = $$descriptions{$fromcid};
  942:         } else {
  943:             if (defined($env{'course.'.$fromcid.'.description'})) {
  944:                 $description = $env{'course.'.$fromcid.'.description'};
  945:             } else {
  946:                 my %courseinfo=&Apache::lonnet::coursedescription($fromcid);
  947:                 $description = $courseinfo{'description'};
  948:             }
  949:             $$descriptions{$fromcid} = $description;
  950:         }
  951:         return $description;
  952:     }
  953: }
  954: 
  955: # ======================================================== Display all messages
  956: 
  957: sub disall {
  958:     my ($r,$folder,$msgstatus)=@_;
  959:     my %saveable = ('msgstatus' => 'scalar',
  960: 		    'sortedby'  => 'scalar',
  961: 		    'interdis'  => 'scalar',
  962: 		    );
  963:     &Apache::loncommon::store_settings('user','mail',\%saveable);
  964:     &Apache::loncommon::restore_settings('user','mail',\%saveable);
  965:     $folder    ||= $env{'form.folder'};
  966:     $msgstatus ||= $env{'form.msgstatus'};
  967:     $env{'form.interdis'} ||= 20;
  968: 
  969:     $r->print(&folderlist($folder,$msgstatus));
  970:     if ($folder eq 'critical') {
  971: 	&discrit($r);
  972:     } else {
  973: 	&disfolder($r,$folder,$msgstatus);
  974:     }
  975: }
  976: 
  977: # ============================================================ Display a folder
  978: 
  979: sub disfolder {
  980:     my ($r,$folder,$msgstatus)=@_;
  981:     my %statushash = &get_msgstatus_types();
  982:     my %blocked = ();
  983:     my %setters = ();
  984:     my $numblocked = 0;
  985:     my ($startblock,$endblock) = &Apache::loncommon::blockcheck(\%setters,'com');
  986:     my %lt = &Apache::lonlocal::texthash(
  987:                       sede => 'Select a destination folder to which the messages will be moved.',
  988:                       nome => 'No messages have been selected to apply ths action to.',
  989:                       chec => 'Check the checkbox for at least one message.',  
  990:     );
  991:     my $jscript = &Apache::loncommon::check_uncheck_jscript();
  992:     $r->print(<<ENDDISHEADER);
  993: <script type="text/javascript">
  994:     $jscript
  995: 
  996:     function validate_checkedaction() {
  997:         document.disall.markedaction.value = document.disall.checkedaction.options[document.disall.checkedaction.selectedIndex].value;
  998:         if (document.disall.checkedaction.options[document.disall.checkedaction.selectedIndex].value == 'markedmove') {
  999:             if (document.disall.movetofolder.options[document.disall.movetofolder.selectedIndex].value == "") {
 1000:                 alert("$lt{'sede'}");
 1001:                 return;
 1002:             } 
 1003:         }
 1004:         var checktotal = 0;
 1005:         if (document.forms.disall.delmark.length > 0) {
 1006:             for (var i=0; i<document.forms.disall.delmark.length; i++) {
 1007:                 if (document.forms.disall.delmark[i].checked) {
 1008:                     checktotal ++;
 1009:                 }
 1010:             }
 1011:         } else {
 1012:             if (document.forms.disall.delmark.checked) {
 1013:                 checktotal ++;
 1014:             }
 1015:         }   
 1016:         if (checktotal == 0) {
 1017:             alert("$lt{'nome'}\\n$lt{'chec'}");
 1018:             return;
 1019:         }
 1020:         document.disall.submit();
 1021:     }
 1022: 
 1023: </script>
 1024: ENDDISHEADER
 1025: 
 1026:     my $fsqs='&folder='.$folder;
 1027:     my @temp=&sortedmessages(\%blocked,$startblock,$endblock,\$numblocked,$folder,$msgstatus);
 1028:     my $totalnumber=$#temp+1;
 1029:     if ($totalnumber < 1) {
 1030:         if ($msgstatus eq '') {
 1031: 	    $r->print('<h2>'.&mt('Empty Folder').'</h2>');
 1032:         } elsif ($msgstatus eq 'replied') {
 1033:             $r->print('<h2>'.&mt('You have not replied to any messages in this folder.').'</h2>');
 1034:         } else { 
 1035:             $r->print('<h2>'.&mt('There are no '.lc($statushash{$msgstatus}).' messages in this folder.').'</h2>');
 1036:         }
 1037:         if ($numblocked > 0) {
 1038:             $r->print(&blocked_in_folder($numblocked,$startblock,$endblock,
 1039:                                          \%setters));
 1040:         }
 1041: 	return;
 1042:     }
 1043:     my $interdis = $env{'form.interdis'};
 1044:     my $number=int($totalnumber/$interdis);
 1045:     if ($totalnumber%$interdis == 0) {
 1046: 	$number--; 
 1047:     }
 1048: 
 1049:     if (($startdis<0) || ($startdis>$number)) { $startdis=$number; }
 1050:     my $firstdis=$interdis*$startdis;
 1051:     if ($firstdis>$#temp) { $firstdis=$#temp-$interdis+1; }
 1052:     my $lastdis=$firstdis+$interdis-1;
 1053:     if ($lastdis>$#temp) { $lastdis=$#temp; }
 1054:     $r->print(&scrollbuttons($startdis,$number,$firstdis,$lastdis,$totalnumber,$msgstatus));
 1055:     $r->print('<form method="post" name="disall" action="/adm/email">'.
 1056: 	      '<table class="LC_mail_list"><tr><th colspan="1">&nbsp;</th><th>');
 1057:     if ($env{'form.sortedby'} eq "revdate") {
 1058: 	$r->print('<a href = "?sortedby=date'.$fsqs.'">'.&mt('Date').'</a></th>');
 1059:     } else {
 1060: 	$r->print('<a href = "?sortedby=revdate'.$fsqs.'">'.&mt('Date').'</a></th>');
 1061:     }
 1062:     $r->print('<th>');
 1063:     if ($env{'form.sortedby'} eq "revuser") {
 1064: 	$r->print('<a href = "?sortedby=user'.$fsqs.'">'.&mt('Username').'</a>');
 1065:     } else {
 1066: 	$r->print('<a href = "?sortedby=revuser'.$fsqs.'">'.&mt('Username').'</a>');
 1067:     }
 1068:     $r->print('</th><th>');
 1069:     if ($env{'form.sortedby'} eq "revdomain") {
 1070: 	$r->print('<a href = "?sortedby=domain'.$fsqs.'">'.&mt('Domain').'</a>');
 1071:     } else {
 1072: 	$r->print('<a href = "?sortedby=revdomain'.$fsqs.'">'.&mt('Domain').'</a>');
 1073:     }
 1074:     $r->print('</th><th>');
 1075:     if ($env{'form.sortedby'} eq "revsubject") {
 1076: 	$r->print('<a href = "?sortedby=subject'.$fsqs.'">'.&mt('Subject').'</a>');
 1077:     } else {
 1078:     	$r->print('<a href = "?sortedby=revsubject'.$fsqs.'">'.&mt('Subject').'</a>');
 1079:     }
 1080:     $r->print('</th><th>');
 1081:     if ($env{'form.sortedby'} eq "revcourse") {
 1082:         $r->print('<a href = "?sortedby=course'.$fsqs.'">'.&mt('Course').'</a>');
 1083:     } else {
 1084:         $r->print('<a href = "?sortedby=revcourse'.$fsqs.'">'.&mt('Course').'</a>');
 1085:     }
 1086:     $r->print('</th><th>');
 1087:     if ($env{'form.sortedby'} eq "revstatus") {
 1088: 	$r->print('<a href = "?sortedby=status'.$fsqs.'">'.&mt('Status').'</a></th>');
 1089:     } else {
 1090:      	$r->print('<a href = "?sortedby=revstatus'.$fsqs.'">'.&mt('Status').'</a></th>');
 1091:     }
 1092:     $r->print("</tr>\n");
 1093: 
 1094:     my $suffix = &Apache::lonmsg::foldersuffix($folder);
 1095:     my $count = 0;
 1096:     for (my $n=$firstdis;$n<=$lastdis;$n++) {
 1097: 	my ($sendtime,$shortsubj,$fromname,$fromdomain,$status,$origID,
 1098: 	    $description,$recv_name,$recv_domain)= 
 1099: 		@{$temp[$n]};
 1100: 	if (($status ne 'deleted') && defined($sendtime) && $sendtime!~/error/) {
 1101:             $count ++;
 1102: 	    if ($status eq 'new') {
 1103: 		$r->print('<tr class="LC_mail_new">');
 1104: 	    } elsif ($status eq 'read') {
 1105: 		$r->print('<tr class="LC_mail_read">');
 1106: 	    } elsif ($status eq 'replied') {
 1107: 		$r->print('<tr class="LC_mail_replied">'); 
 1108: 	    } else {
 1109: 		$r->print('<tr class="LC_mail_other">');
 1110: 	    }
 1111: 	    my ($dis_name,$dis_domain) = ($fromname,$fromdomain);
 1112: 	    if ($folder eq 'sent') {
 1113:                 if (defined($recv_name) && defined($recv_domain)) {
 1114: 		    if (ref($recv_name) eq 'ARRAY' && 
 1115:                         ref($recv_domain) eq 'ARRAY') {
 1116: 		        $dis_name   = join('<br />',@{$recv_name});
 1117: 		        $dis_domain = join('<br />',@{$recv_domain});
 1118:                     }
 1119: 		} else {
 1120: 		    my $msg_id  = &unescape($origID);
 1121: 		    my %message = &Apache::lonnet::get('nohist_email'.$suffix,
 1122: 						       [$msg_id]);
 1123: 		    my %content = &Apache::lonmsg::unpackagemsg($message{$msg_id});
 1124:                     if (ref($content{'recuser'}) eq 'ARRAY') {
 1125: 		        $dis_name   = join('<br />',@{$content{'recuser'}});
 1126:                     }
 1127:                     if (ref($content{'recdomain'}) eq 'ARRAY') {
 1128: 		        $dis_domain = join('<br />',@{$content{'recdomain'}});
 1129:                     }
 1130: 		}
 1131: 	    }
 1132:             my $localsenttime = &Apache::lonlocal::locallocaltime($sendtime);
 1133: 	    $r->print('<td align="right"><span class="LC_nobreak">'.(($status eq 'new')?'<b>':'').
 1134:                       $count.'.'.(($status eq 'new')?'</b>':'').'&nbsp;'.
 1135:                       '<input type="checkbox" name="delmark"'. 
 1136:                       ' value="'.$origID.'" /></span></td>');
 1137:             foreach my $item ($localsenttime,$dis_name,$dis_domain,$shortsubj) {
 1138:                 $r->print('<td>'.(($status eq 'new')?'<b>':'').
 1139:                           '<a href="/adm/email?display='.$origID.$sqs.'">'.
 1140:                           $item.(($status eq 'new')?'</b>':'').'</td>');
 1141:             }
 1142:             my $showstatus;
 1143:             my %statushash = &get_msgstatus_types();
 1144:             if ($status eq '') {
 1145:                 $showstatus = '';
 1146:             } else {
 1147:                 $showstatus = $statushash{$status};
 1148:             }
 1149: 	    $r->print('<td>'.(($status eq 'new')?'<b>':'').$description.
 1150:                       (($status eq 'new')?'</b>':'').'</td><td>'.
 1151:                       (($status eq 'new')?'<b>':'').$showstatus.
 1152:                       (($status eq 'new')?'</b>':'').'</td></tr>'."\n");
 1153: 	} elsif ($status eq 'deleted') {
 1154: # purge
 1155: 	    my ($result,$msg) = 
 1156: 		&movemsg(&unescape($origID),$folder,'trash');
 1157: 	    
 1158: 	}
 1159:     }   
 1160:     $r->print("</table>\n");
 1161:     $r->print('<table border="0" cellspacing="2" cellpadding="2">
 1162:  <tr>
 1163:   <td>'.
 1164:   '<input type="button" onclick="javascript:checkAll(document.disall.delmark)" value="'.&mt('Check All').'" /><br />'."\n".
 1165:   '<input type="button" onclick="javascript:uncheckAll(document.disall.delmark)" value="'.&mt('Uncheck All').'" />'."\n".
 1166:   '<input type="hidden" name="sortedby" value="'.$env{'form.sortedby'}.'" /></td><td>&nbsp;</td>'."\n".
 1167:   '<td align="center"><b>'.&mt('Action').'</b><br />'."\n".
 1168:   '  <select name="checkedaction">'."\n");
 1169: 
 1170:     if ($folder ne 'trash') {
 1171:         $r->print('    <option value="markeddel">'.&mt('Delete').'</option>'."\n");
 1172:     }
 1173:     if ($msgstatus ne 'read') {
 1174:         $r->print('    <option value="markedread">'.&mt('Mark Read').'</option>."\n"');
 1175:     }
 1176:     if ($msgstatus ne 'unread') {
 1177:         $r->print('    <option value="markedunread">'.&mt('Mark Unread').'</option>'."\n");
 1178:     }
 1179:     $r->print('   <option value="markedforward">'.&mt('Forward').'</option>'."\n");
 1180: 
 1181:     my %gotfolders = &Apache::lonmsg::get_user_folders();
 1182:     if (keys(%gotfolders) > 0) {
 1183:         $r->print('   <option value="markedmove">'.&mt('Move to Folder ->').
 1184:                   '</option>');
 1185:     }
 1186:     $r->print("\n".'</select></td>'."\n");
 1187: 
 1188:     if (keys(%gotfolders) > 0) {
 1189:         $r->print('<td align="center"><b>'.&mt('Destination folder').'<b><br />');
 1190: 	my %userfolders;
 1191:         foreach my $key (keys(%gotfolders)) {
 1192:             $userfolders{$key} = $key;
 1193:         }
 1194:         $userfolders{''} = "";
 1195:         $r->print(&Apache::loncommon::select_form('','movetofolder',%userfolders).
 1196:                   '</td>');
 1197:     }
 1198:     $r->print('<td>&nbsp;</td><td>&nbsp;&nbsp;'.
 1199:               '<input type="button" name="go" value="'.&mt('Go').
 1200:               '" onclick="javascript:validate_checkedaction()"/></td>'."\n".
 1201:               '</tr></table>');
 1202:     my $postedstartdis=$startdis+1;
 1203:     $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'}.'" /><input type="hidden" name="msgstatus" value="'.$msgstatus.'" /><input type="hidden" name="markedaction" value="" /></form>');
 1204:     if ($numblocked > 0) {
 1205:         $r->print(&blocked_in_folder($numblocked,$startblock,$endblock,
 1206:                                      \%setters));
 1207:     }
 1208: }
 1209: 
 1210: sub blocked_in_folder {
 1211:     my ($numblocked,$startblock,$endblock,$setters) = @_;
 1212:     my $beginblock = &Apache::lonlocal::locallocaltime($startblock);
 1213:     my $finishblock = &Apache::lonlocal::locallocaltime($endblock);
 1214:     my $output = '<br /><br />'.
 1215:                   &mt('[quant,_1,message is, messages are] not viewable because display of LON-CAPA messages sent to you by other students between [_2] and [_3] is currently being blocked because of online exams.',$numblocked,$beginblock,$finishblock);
 1216:     $output .= &Apache::loncommon::build_block_table($startblock,$endblock,
 1217:                                                      $setters);
 1218:     return $output;
 1219: }
 1220: 
 1221: # ============================================================== Compose output
 1222: 
 1223: sub compout {
 1224:     my ($r,$forwarding,$replying,$broadcast,$replycrit,$folder,$dismode,
 1225:         $multiforward)=@_;
 1226:     my $suffix=&Apache::lonmsg::foldersuffix($folder);
 1227:     my ($cdom,$cnum,$group,$refarg);
 1228:     if (exists($env{'form.group'})) {
 1229:         $cdom = $env{'course.'.$env{'request.course.id'}.'.domain'};
 1230:         $cnum = $env{'course.'.$env{'request.course.id'}.'.num'};
 1231:         $group = $env{'form.group'};
 1232:         my $action = 'composing';
 1233:         $r->print(&groupmail_header($action,$group,$cdom,$cnum));
 1234:     } elsif ($broadcast eq 'individual') {
 1235: 	&printheader($r,'/adm/email?compose=individual',
 1236: 	     'Send a Message');
 1237:     } elsif ($broadcast) {
 1238: 	&printheader($r,'/adm/email?compose=group',
 1239: 	     'Broadcast Message');
 1240:     } elsif ($forwarding) {
 1241: 	&Apache::lonhtmlcommon::add_breadcrumb
 1242:         ({href=>"/adm/email?display=".&escape($forwarding),
 1243:           text=>"Display Message"});
 1244: 	&printheader($r,'/adm/email?forward='.&escape($forwarding),
 1245: 	     'Forwarding a Message');
 1246:     } elsif ($replying) {
 1247: 	&Apache::lonhtmlcommon::add_breadcrumb
 1248:         ({href=>"/adm/email?display=".&escape($replying),
 1249:           text=>"Display Message"});
 1250: 	&printheader($r,'/adm/email?replyto='.&escape($replying),
 1251: 	     'Replying to a Message');
 1252:     } elsif ($replycrit) {
 1253: 	$r->print('<h3>'.&mt('Replying to a Critical Message').'</h3>');
 1254: 	$replying=$replycrit;
 1255:     } elsif ($multiforward) {
 1256:         &Apache::lonhtmlcommon::add_breadcrumb
 1257:         ({href=>"/adm/email?folder=".&escape($folder),
 1258:           text=>"Display All Messages"});
 1259:         &printheader($r,'/adm/email?compose=multiforward',
 1260:              'Forwarding Multiple Messages');
 1261:         if ($multiforward > 1) {
 1262:             $r->print(&mt('Each of the <b>[quant,_1,message]</b> you checked'
 1263:                          .' will be forwarded to the recipient(s) you select below.',$multiforward)
 1264:                     .'<br />');
 1265:         } else {
 1266:             $r->print(&mt('The message you checked will be forwarded to the recipient(s) you select below.').'<br />');
 1267:         }
 1268: 
 1269:     } else {
 1270: 	&printheader($r,'/adm/email?compose=upload',
 1271: 	     'Distribute from Uploaded File');
 1272:     }
 1273: 
 1274:     my $dispcrit='';
 1275:     my $dissub='';
 1276:     my $dismsg='';
 1277:     my $disbase='';
 1278:     my $attachrow;
 1279:     my $func1='Send'; # do not translate here!
 1280:     my %func2=( # do not translate here!
 1281:                'ma'  => 'Message',
 1282:                'msg' => 'Messages',
 1283:               );
 1284:     my %lt=&Apache::lonlocal::texthash('us'  => 'Username',
 1285: 				       'do'  => 'Domain',
 1286: 				       'ad'  => 'Additional Recipients',
 1287:                                        'rt'  => 'Reply to',
 1288:                                        'ar'  => 'Allow replies',
 1289: 				       'sb'  => 'Subject',
 1290: 				       'ca'  => 'Cancel',
 1291:                                        'gen' => 'Generate messages from a file',
 1292:                                        'gmt' => 'General message text',
 1293:                                        'tff' => 'The file format for the uploaded portion of the message is',
 1294:                                        'uas' => 'Upload and Send',
 1295:                                        'atta' => 'Attachment',
 1296:                                        'to'   => 'To:',
 1297:                                       );
 1298:     my %attachmax = (
 1299:                      text => &mt('(128 KB max size)'),
 1300:                      num  => 131072,
 1301:                     );
 1302:     if (!$forwarding && !$multiforward) {
 1303:         $attachrow = '<tr><td colspan="3"><b>'.$lt{'atta'}.'</b> '.$attachmax{'text'}.': <input type="file" name="attachment" /></td></tr>';
 1304:     }
 1305:     if (&Apache::lonnet::allowed('srm',$env{'request.course.id'})
 1306: 	|| &Apache::lonnet::allowed('srm',$env{'request.course.id'}.
 1307: 				    '/'.$env{'request.course.sec'})) {
 1308: 	 my $crithelp = Apache::loncommon::help_open_topic("Course_Critical_Message");
 1309:          $dispcrit=
 1310:  '<span class="LC_nobreak"><label><input type="checkbox" name="critmsg" /> '.&mt('Send as critical message').'.</label>'.$crithelp.'&nbsp;&nbsp;'.&mt('Require return receipt?').'<label><input type="radio" name="sendbck" value="1" />'.&mt('Yes').'</label>&nbsp;&nbsp;<label><input type="radio" name="sendbck" value="" checked="checked" />'.&mt('No').'</label></span><br />'.
 1311:  '<label><input type="checkbox" name="permanent" /> '.
 1312: &mt('Send copy to permanent e-mail address (if known)').'</label><br />'.
 1313: '<label><input type="checkbox" name="rsspost" /> '.
 1314: 		  &mt('Include in course RSS newsfeed').'</label><br />';
 1315:     }
 1316:     if ($broadcast ne 'group') {
 1317:         if (&Apache::lonnet::allowed('dff',$env{'request.course.id'}) ||
 1318:             &Apache::lonnet::allowed('dff',$env{'request.course.id'}.
 1319:                                      '/'.$env{'request.course.sec'})) {
 1320: 
 1321:             $dispcrit.='<label>'.
 1322:                        '<input type="checkbox" name="courserecord" value="1" /> '.
 1323:                        &mt("Include in course's 'User records' for recipient(s)").
 1324:                        '</label><br />';
 1325:         }
 1326:     }
 1327: 
 1328:     my %message;
 1329:     my %content;
 1330:     my ($hasfloat,$broadcast_js,$sendmode,$can_grp_broadcast);
 1331:     my $defdom=$env{'user.domain'};
 1332:     if ($broadcast eq 'group') {
 1333:         my %access_status = (
 1334:                            active => 0,
 1335:                            previous => 0,
 1336:                            future => 0,
 1337:                       );
 1338:  
 1339:         if ($group eq '') {
 1340:             my $studentsel = &discourse(\%access_status);
 1341:             if ($studentsel) {
 1342:                 if ($env{'environment.wysiwygeditor'} eq 'on') {
 1343:                     $r->print($studentsel);
 1344:                 } else {
 1345:                     $r->print('<div class="LC_left_float">'.$studentsel.'</div>');
 1346:                 }
 1347:                 $hasfloat = 1;
 1348:             }
 1349:         } else {
 1350:             $can_grp_broadcast = &check_group_priv($group);
 1351:             if ($can_grp_broadcast) {
 1352:                 $hasfloat = &disgroup($r,$cdom,$cnum,$group,\%access_status);
 1353:             }
 1354:         }
 1355:         if ($hasfloat) {
 1356:             $sendmode = '<input type="hidden" name="sendmode" value="group" />'."\n";
 1357:             $broadcast_js = qq|
 1358: <script type="text/javascript">
 1359: function courseRecipients() {
 1360: |;
 1361:         foreach my $type (keys(%access_status)) {
 1362:             if ($access_status{$type}) {
 1363:                 my $formname = $type.'users';
 1364:                 if ($type eq 'active' && $group ne '') {
 1365:                     $broadcast_js .= qq|
 1366:                     document.compemail.groupmail.value = document.$formname.groupmail[document.$formname.groupmail.selectedIndex].value;
 1367: |;
 1368:                 }
 1369:                 $broadcast_js .= qq|
 1370:     if (typeof(document.$formname.selectedusers_forminput.length)=="undefined") {
 1371:         document.compemail.courserecips.value += '_&&&_'+document.$formname.selectedusers_forminput.value;
 1372:     } else {
 1373:         for (var i=0; i<document.$formname.selectedusers_forminput.length; i++) {
 1374:             if (document.$formname.selectedusers_forminput[i].checked) {
 1375:                 document.compemail.courserecips.value += '_&&&_'+document.$formname.selectedusers_forminput[i].value;  
 1376:             }
 1377:         }
 1378:     }
 1379:                 |;
 1380:             }
 1381:         }
 1382:         $broadcast_js .= qq|
 1383:     return;
 1384: }
 1385: </script>
 1386: 
 1387: |;
 1388:         }
 1389:     }
 1390:     if ($forwarding) {
 1391: 	%message=&Apache::lonnet::get('nohist_email'.$suffix,[$forwarding]);
 1392: 	%content=&Apache::lonmsg::unpackagemsg($message{$forwarding},$folder);
 1393: 	$dispcrit.='<input type="hidden" name="forwid" value="'.
 1394: 	    $forwarding.'" />';
 1395: 	$func1='Forward'; # do not translate here!
 1396: 	
 1397: 	$dissub=&mt('Forwarding').': '.$content{'subject'};
 1398: 	$dismsg=&mt('Forwarded message from').' '.
 1399: 	    $content{'sendername'}.' '.&mt('at').' '.$content{'senderdomain'};
 1400: 	if ($content{'baseurl'}) {
 1401: 	    $disbase='<input type="hidden" name="baseurl" value="'.&escape($content{'baseurl'}).'" />';
 1402: 	}
 1403:     }
 1404:     if ($replying) {
 1405: 	%message=&Apache::lonnet::get('nohist_email'.$suffix,[$replying]);
 1406: 	%content=&Apache::lonmsg::unpackagemsg($message{$replying},$folder);
 1407: 	$dispcrit.='<input type="hidden" name="replyid" value="'.
 1408: 	    $replying.'" />';
 1409: 	$func1='Send Reply to'; # do not translate here!
 1410: 	
 1411: 	$dissub=&mt('Reply').': '.$content{'subject'};       
 1412: 	$dismsg='> '.$content{'message'};
 1413: 	$dismsg=~s/\r/\n/g;
 1414: 	$dismsg=~s/\f/\n/g;
 1415: 	$dismsg=~s/\n+/\n\> /g;
 1416: 	if ($content{'baseurl'}) {
 1417: 	    $disbase='<input type="hidden" name="baseurl" value="'.&escape($content{'baseurl'}).'" />';
 1418: 	    if ($env{'user.adv'}) {
 1419: 		$disbase.='<label><input type="checkbox" name="storebasecomment" />'.&mt('Save message for re-use').
 1420: 		    '</label> <a href="/adm/email?showcommentbaseurl='.
 1421: 		    &escape($content{'baseurl'}).'" target="comments">'.
 1422: 		    &mt('Show re-usable messages').'</a><br />';
 1423: 	    }
 1424: 	}
 1425:         my $jscript = &Apache::loncommon::check_uncheck_jscript();
 1426:         $r->print(<<"ENDREPSCRIPT");
 1427: <script type="text/javascript">
 1428: $jscript
 1429: </script>
 1430: ENDREPSCRIPT
 1431:     }
 1432:     my $citation=&displayresource(%content);
 1433:     my $onsubmit;
 1434:     if ($env{'form.recdom'}) { $defdom=$env{'form.recdom'}; }
 1435:     if ($env{'form.text'}) { $dismsg=$env{'form.text'}; }
 1436:     if ($env{'form.subject'}) { $dissub=$env{'form.subject'}; }
 1437:     if ($hasfloat) {
 1438:         if ($env{'environment.wysiwygeditor'} eq 'on') {
 1439:             $r->print($broadcast_js);
 1440:         } else {
 1441:             $r->print($broadcast_js.'<div class="LC_left_float">');
 1442:         }
 1443:         $onsubmit = ' onsubmit="javascript:courseRecipients();" ';
 1444:     }
 1445:     $r->print(
 1446:                 '<form action="/adm/email"  name="compemail" method="post"'.
 1447:                 ' enctype="multipart/form-data"'.$onsubmit.'>'."\n".
 1448:                 '<input type="hidden" name="sendmail" value="on" />'."\n".
 1449:                 '<table>');
 1450:     if (($broadcast eq 'group') && ($group ne '') && (!$can_grp_broadcast)) {
 1451:         $r->print(&recipient_input_row($cdom,%lt));
 1452:     }
 1453:     if (($broadcast ne 'group') && ($broadcast ne 'upload')) {
 1454: 	if ($replying) {
 1455:             if ($content{'noreplies'}) {
 1456:                 $r->print('<tr><td>'.&mt('This message was designated by the sender not to allow replies.').'</td></tr></table></form>');
 1457:                 return;
 1458:             }
 1459:             $r->print('<tr><td colspan="3"><b>'.&mt('Replying to').'</b> ');
 1460:             if ($content{'replytoaddr'}) {
 1461:                 my ($replytoname,$replytodom) = split(/:/,$content{'replytoaddr'});
 1462:                 if ($replytoname ne '' && $replytodom ne '') {
 1463:                     $r->print(&Apache::loncommon::plainname($replytoname,
 1464:                                  $replytodom).' ('.$replytoname.':'.
 1465:                                  $replytodom.')');
 1466:                     $r->print('<input type="hidden" name="recuname" value="'.
 1467:                           $replytoname.'" />'.
 1468:                           '<input type="hidden" name="recdomain" value="'.
 1469:                           $replytodom.'" /></td></tr>');
 1470: 
 1471:                 } else {
 1472:                     $r->print(&mt('The sender did not designate a reply to address for this message.').'</td></tr></table>');
 1473:                     return;
 1474:                 }
 1475:             } else {
 1476: 	        $r->print(&Apache::loncommon::aboutmewrapper(
 1477: 							 &Apache::loncommon::plainname($content{'sendername'},$content{'senderdomain'}),$content{'sendername'},$content{'senderdomain'}).' ('.
 1478: 		      $content{'sendername'}.':'.
 1479: 		      $content{'senderdomain'}.')');
 1480:                 $r->print('<input type="hidden" name="recuname" value="'.
 1481: 		          $content{'sendername'}.'" />'.
 1482: 		          '<input type="hidden" name="recdomain" value="'.
 1483:                           $content{'senderdomain'}.'" /></td></tr>');
 1484:             }
 1485:             if ($content{'recipid'}) {
 1486:                 my %recips;
 1487:                 &retrieve_recips('replying',\%content,\%recips);
 1488:                 if (ref($recips{'to'}) eq 'ARRAY') {
 1489:                     if (@{$recips{'to'}} > 0) {
 1490:                         my $replyall;
 1491:                         if (@{$recips{'to'}} > 1) {
 1492:                             $replyall = qq|
 1493:  <span class="LC_nobreak">
 1494:       <input type="button" value="check all"
 1495:         onclick="javascript:checkAll(document.compemail.replying_to)" />
 1496:         &nbsp;&nbsp;
 1497:       <input type="button" value="uncheck all"
 1498:         onclick="javascript:uncheckAll(document.compemail.replying_to)" />
 1499:  </span>
 1500: |;
 1501:                         }
 1502:                         my $tolist = join(' ',@{$recips{'to'}});
 1503:                         $r->print('<tr><td colspan="3"><table><tr><td>'.&mt('[_1]Send reply[_2] to other recipients','<b>','</b>').':<br />'.$replyall.'</td><td>'.$tolist.'</td></tr></table></td></tr>');
 1504:                     }
 1505:                 }
 1506:                 if (ref($recips{'cc'}) eq 'ARRAY') {
 1507:                     if (@{$recips{'cc'}} > 0) {
 1508:                         my $replyall;
 1509:                         if (@{$recips{'cc'}} > 1) {
 1510:                             $replyall = qq|
 1511:  <span class="LC_nobreak">
 1512:       <input type="button" value="check all"
 1513:         onclick="javascript:checkAll(document.compemail.replying_cc)" />
 1514:         &nbsp;&nbsp;
 1515:       <input type="button" value="uncheck all"
 1516:         onclick="javascript:uncheckAll(document.compemail.replying_cc)" />
 1517:  </span>
 1518: |;
 1519:                         }
 1520:                         my $cclist = join(' ',@{$recips{'cc'}});
 1521:                         $r->print('<tr><td colspan="3"><table><tr><td>'.&mt('[_1]Cc[_2] to other copied recipients','<b>','</b>').':<br />'.$replyall.'</td><td>'.$cclist.'</td></tr></table></td></tr>');
 1522:                     }
 1523:                 }
 1524:                 if ($content{'group'} ne '') {
 1525:                     if (&check_group_priv($content{'group'})) {
 1526:                         if (ref($recips{'group_cc_broadcast'}) eq 'ARRAY') {
 1527:                             if (@{$recips{'group_cc_broadcast'}} > 0) {
 1528:                                 my $replyall;
 1529:                                 if (@{$recips{'group_cc_broadcast'}} > 1) {
 1530:                                     $replyall = qq|
 1531:  <span class="LC_nobreak">
 1532:       <input type="button" value="check all"
 1533:         onclick="javascript:checkAll(document.compemail.replying_groupcc)" />
 1534:         &nbsp;&nbsp;
 1535:       <input type="button" value="uncheck all"
 1536:         onclick="javascript:uncheckAll(document.compemail.replying_groupcc)" />
 1537:  </span>
 1538: |;
 1539:                                 }
 1540:                                 my $groupcclist = join(' ',@{$recips{'group_cc_broadcast'}});
 1541:                                 $r->print('<tr><td colspan="3"><table><tr><td>'.&mt('[_1]Cc[_2] to other copied group members','<b>','</b>').':<br />'.$replyall.'</td><td>'.$groupcclist.'<input type="hidden" name="group" value="'.$content{'group'}.'" /><input type="hidden" name="sendmode" value="group" /><input type="hidden" name="groupmail" value="cc" /></td></tr></table></td></tr>');
 1542:                             }
 1543:                         }
 1544:                     }
 1545:                 }
 1546:             }
 1547: 	} else {
 1548:             $r->print(&recipient_input_row($defdom,%lt));
 1549:         }
 1550:     }
 1551:     my $latexHelp = &Apache::loncommon::helpLatexCheatsheet(undef,undef,1);
 1552:     my $wysiwyglink=&Apache::lonhtmlcommon::htmlareaselectactive('message').'<br />';
 1553:     my $subj_size;
 1554:     if ($multiforward) {
 1555:         $r->print(&additional_rec_row(\%lt));
 1556:         $r->print('<tr><td colspan="2">'.
 1557:                   &mt('Unless you choose otherwise:').'<ul><li>'.
 1558:         &mt("The subject in each forwarded message will be <i>'Forwarding:'</i> followed by the original subject.").'</li><li>'.
 1559:         &mt("The message itself will begin with a first line: <i>'Forwarded message from'</i> followed by the original sender's name.").'</li></ul></td></tr>');
 1560:         $func1='Forward'; # do not translate here!
 1561:         $dissub = &mt('Forwarding').': ';
 1562:         $subj_size = '10';
 1563:         my $extra = '&lt;'.&mt('original subject').'&gt;&nbsp;&nbsp;&nbsp;'.
 1564:                     '<input type="radio" name="showorigsubj" value="1" checked="checked" />'.&mt('Yes').'&nbsp;<input type="radio" name="showorigsubj" value="0" />'.&mt('No');
 1565:         $dismsg = &mt('Forwarded message from ').' ';
 1566:         my $sender = &mt("sender's name");
 1567:         $r->print(&msg_subject_row($dissub,\%lt,$subj_size,$extra));
 1568:         $r->print('<tr><td>'.&mt('Message begins with:').'</td><td><input type="text" name="msgheader" value="'.$dismsg.'" />&nbsp;'.$sender.'&nbsp;&nbsp;&nbsp;<input type="radio" name="showorigsender" value="1" checked="checked" />'.&mt('Yes').'&nbsp;<input type="radio" name="showorigsender" value="0" />'.&mt('No').'<input type="hidden" name="multiforward" value="'.$multiforward.'" /></td></tr>
 1569: </table>
 1570: <br /><table>
 1571: <tr><td align="left">'."\n".
 1572: $latexHelp."<br />\n".
 1573: &mt("Any new text to display before the text of the original messages:").'<br />'."\n".
 1574: '<textarea name="message" id="message" cols="80" rows="5" wrap="hard"></textarea>'.
 1575: $wysiwyglink);
 1576:         my @to_forward = &Apache::loncommon::get_env_multiple('form.delmark');
 1577:         foreach my $msg (@to_forward) {
 1578:             $r->print('<input type="hidden" name="delmark" value="'.$msg.'" />');
 1579:         }
 1580:         $r->print(&submit_button_row($folder,$dismode,&mt($func1.' '.$func2{'msg'}),
 1581:                                      \%lt));
 1582:     } elsif ($broadcast ne 'upload') {
 1583:         $subj_size = '50';
 1584:         $r->print(&additional_rec_row(\%lt));
 1585:         if (&Apache::lonnet::allowed('srm',$env{'request.course.id'})
 1586:             || &Apache::lonnet::allowed('srm',$env{'request.course.id'}.
 1587:                                         '/'.$env{'request.course.sec'})) {
 1588:             $r->print(&reply_to_row(\%lt));
 1589:         }
 1590:         $r->print(&msg_subject_row($dissub,\%lt,$subj_size));
 1591:         $r->print(<<"ENDCOMP");
 1592: $attachrow
 1593: </table><br />
 1594: $latexHelp<br />
 1595: <textarea name="message" id="message" cols="80" rows="15" wrap="hard">$dismsg
 1596: </textarea>$wysiwyglink
 1597: $sendmode
 1598: $dispcrit
 1599: $disbase
 1600: ENDCOMP
 1601:         $r->print(&submit_button_row($folder,$dismode,&mt($func1.' '.$func2{'ma'}),
 1602:                                      \%lt,$hasfloat,$group));
 1603:         $r->print($citation);
 1604:         if (exists($env{'form.ref'})) {
 1605:             $r->print('<input type="hidden" name="ref" value="'.
 1606:                       $env{'form.ref'}.'" />');
 1607:         }
 1608:         if (exists($env{'form.group'})) {
 1609:             $r->print('<input type="hidden" name="group" value="'.
 1610:                       $env{'form.group'}.'" />');
 1611:         }
 1612:     } else { # $broadcast is 'upload'
 1613: 	$r->print(<<ENDBLOCK);
 1614: <input type="hidden" name="sendmode" value="upload" />
 1615: <input type="hidden" name="send" value="on" />
 1616: <h3>$lt{'gen'}</h3>
 1617: <p>
 1618: Subject: <input type="text" size="50" name="subject" />
 1619: </p>
 1620: <p>$lt{'gmt'}:<br />
 1621: <textarea name="message" id="message" cols="60" rows="10" wrap="hard">$dismsg
 1622: </textarea>$wysiwyglink</p>
 1623: <p>
 1624: $lt{'tff'}:
 1625: ENDBLOCK
 1626:        $r->print('
 1627: <pre>'."\n".
 1628: &mt('username1:domain1: text')."\n".
 1629: &mt('username2:domain2: text')."\n".
 1630: &mt('username3:domain1: text')."\n".
 1631: '</pre>
 1632: </p>
 1633: <p>
 1634: '.&mt('The messages will be assembled from all lines with the respective'."\n".'<tt>username:domain</tt>, and appended to the general message text.'));
 1635:         $r->print(<<ENDUPLOAD);
 1636: </p>
 1637: <p>
 1638: <input type="file" name="upfile" size="40" /></p><p>
 1639: $dispcrit
 1640: <input type="submit" value="$lt{'uas'}" /></p>
 1641: ENDUPLOAD
 1642:     }
 1643:     if ($env{'form.displayedcrit'}) {
 1644: 	$r->print('<input type="hidden" name="displayedcrit" value="true" />');
 1645:     }
 1646:     $r->print('</form>');
 1647:     if ($hasfloat) {
 1648:         unless($env{'environment.wysiwygeditor'} eq 'on') {
 1649:             $r->print('</div><div class="LC_clear_float_footer"></div>');
 1650:         }
 1651:     }
 1652:     $r->print(&generate_preview_form);
 1653: }
 1654: 
 1655: sub check_group_priv {
 1656:     my ($group) = @_;
 1657:     my $cid = $env{'request.course.id'};
 1658:     my $sec = $env{'request.course.sec'};
 1659:     return if !$cid;
 1660:     my $can_broadcast = &Apache::lonnet::allowed('sgb',$cid.'/'.$group);
 1661:     my $viewgrps = &Apache::lonnet::allowed('vcg',$cid.($sec?'/'.$sec:''));
 1662:     my $editgrps = &Apache::lonnet::allowed('mdg',$cid.($sec?'/'.$sec:''));
 1663:     if ($viewgrps || $editgrps || $can_broadcast) {
 1664:         return 1;
 1665:     }
 1666:     return;
 1667: }
 1668: 
 1669: sub recipient_input_row {
 1670:     my ($dom,%lt) = @_;
 1671:     my $domform = &Apache::loncommon::select_dom_form($dom,'recdomain');
 1672:     my $selectlink=
 1673:       &Apache::loncommon::selectstudent_link('compemail','recuname',
 1674:                                              'recdomain');
 1675:     my $output = <<"ENDREC";
 1676: <tr><td colspan="3"><span class="LC_nobreak"><b>$lt{'to'}</b> $lt{'us'}:&nbsp;<input type="text" size="12" name="recuname" value="$env{'form.recname'}" />&nbsp;$lt{'do'}:&nbsp;$domform&nbsp;&nbsp;$selectlink</span></td></tr>
 1677: ENDREC
 1678:     return $output;
 1679: }
 1680: 
 1681: sub reply_to_row {
 1682:     my ($lt) = @_;
 1683:     my $radioyes = &mt('Yes');
 1684:     my $radiono = &mt('No');
 1685:     my $output = <<"ENDREP";
 1686: <tr><td colspan="3"><span class="LC_nobreak"><b>$lt->{'ar'}</b>:<label><input type="radio" name="can_reply" value="Y" checked="checked" />$radioyes</label>&nbsp;&nbsp;<label><input type="radio" name="can_reply" value="N" />$radiono</label></span>&nbsp;&nbsp;&nbsp;&nbsp;<span class="LC_nobreak">$lt->{'rt'}:&nbsp;<input type="text" size="25" name="reply_to_addr" value="$env{'user.name'}:$env{'user.domain'}" /></span></td></tr>
 1687: ENDREP
 1688:     return $output;
 1689: }
 1690: 
 1691: sub additional_rec_row {
 1692:     my ($lt) = @_;
 1693:     my $cc = &mt('Cc:');
 1694:     my $bcc = &mt('Bcc:');
 1695:     my $exmpl = &mt('username:domain,username:domain,...'); 
 1696:     my $output = <<"ENDADD";
 1697: <tr><td colspan="3"><fieldset id="LC_additionalrecips"><legend>$lt->{'ad'} <tt>($exmpl)</tt></legend><table>
 1698: <tr><td>&nbsp;</td><td>$lt->{'to'}</td><td><input type="text" size="50" name="additionalrec_to" /></td></tr>
 1699: <tr><td>&nbsp;</td><td>$cc</td><td><input type="text" size="50" name="additionalrec_cc" /></td></tr> 
 1700: <tr><td>&nbsp;</td><td>$bcc</td><td><input type="text" size="50" name="additionalrec_bcc" /></td></tr></table></fieldset></td></tr>
 1701: ENDADD
 1702:     return $output;
 1703: }
 1704: 
 1705: sub submit_button_row {
 1706:     my ($folder,$dismode,$sendtext,$lt,$is_crsform,$group) = @_;
 1707:     my $pre=&mt("Show Preview and Check Spelling");
 1708:     my $value=&mt('Send');
 1709:     my $prevbutton = '<input type="button" name="preview" value="'.$pre.'" onclick="if (typeof(document.compemail.onsubmit)=='."'function'".') {document.compemail.onsubmit();};document.preview.comment.value=document.compemail.message.value;document.preview.subject.value=document.compemail.subject.value;document.preview.submit();" />';
 1710:     my $output = qq|
 1711: <input type="hidden" name="folder" value="$folder" />
 1712: <input type="hidden" name="dismode" value="$dismode" />|;
 1713:     if ($is_crsform) {
 1714:         $output .= '<input type="hidden" name="courserecips" value="" />'."\n";
 1715:         if ($group ne '') {
 1716:             $output .= '<input type="hidden" name="groupmail" value="" />'."\n";
 1717:         }
 1718:     }
 1719:     $output .= qq|
 1720: <table><tr><td align="left">
 1721: <input type="submit" name="send" value="$value" title="$sendtext" />
 1722: <input type="submit" name="cancel" value="$lt->{'ca'}" />
 1723: </td><td width="60">&nbsp;</td><td align="right">$prevbutton</td></tr></table>
 1724: |;
 1725:     return $output;
 1726: }
 1727: 
 1728: sub msg_subject_row {
 1729:     my ($dissub,$lt,$subj_size,$extra) = @_;
 1730:     my $output = '<tr><td colspan="3"><b>'.$lt->{'sb'}.'</b>:&nbsp;<input type="text" size="'.
 1731:                  $subj_size.'" name="subject" value="'.$dissub.'" />'.$extra.
 1732:                  '</td></tr>';
 1733:     return $output;
 1734: }
 1735: 
 1736: sub generate_preview_form {
 1737:     my $prevbutton = (<<ENDPREVIEW);
 1738: <form name="preview" action="/adm/feedback?preview=1" method="post" target="preview">
 1739: <input type="hidden" name="subject" />
 1740: <input type="hidden" name="comment" />
 1741: </form>
 1742: ENDPREVIEW
 1743: }
 1744: 
 1745: # ---------------------------------------------------- Display all face to face
 1746: 
 1747: sub retrieve_instructor_comments {
 1748:     my ($user,$domain)=@_;
 1749:     my $target=$env{'form.grade_target'};
 1750:     if (! $env{'request.course.id'}) { return; }
 1751:     if (! &Apache::lonnet::allowed('dff',$env{'request.course.id'})
 1752: 	&& ! &Apache::lonnet::allowed('dff',$env{'request.course.id'}.
 1753: 				      '/'.$env{'request.course.sec'})) {
 1754: 	return;
 1755:     }
 1756:     my %records=&Apache::lonnet::dump('nohist_email',
 1757: 			 $env{'course.'.$env{'request.course.id'}.'.domain'},
 1758: 			 $env{'course.'.$env{'request.course.id'}.'.num'},
 1759:                          '%255b'.$user.'%253a'.$domain.'%255d');
 1760:     my $result='';
 1761:     foreach my $key (sort(keys(%records))) {
 1762:         my %content=&Apache::lonmsg::unpackagemsg($records{$key});
 1763:         next if ($content{'senderdomain'} eq '');
 1764:         next if ($content{'subject'} !~ /^Record/);
 1765: 	# &Apache::lonfeedback::newline_to_br(\$content{'message'});
 1766: 	$result.='Recorded by '.
 1767:             $content{'sendername'}.':'.$content{'senderdomain'}."\n";
 1768:         $result.=
 1769:             &Apache::lontexconvert::msgtexconverted($content{'message'})."\n";
 1770:      }
 1771:     return $result;
 1772: }
 1773: 
 1774: sub disfacetoface {
 1775:     my ($r,$user,$domain)=@_;
 1776:     my $target=$env{'form.grade_target'};
 1777:     unless ($env{'request.course.id'}) { return; }
 1778:     if  (!&Apache::lonnet::allowed('dff',$env{'request.course.id'})
 1779: 	 && ! &Apache::lonnet::allowed('dff',$env{'request.course.id'}.
 1780: 				       '/'.$env{'request.course.sec'})) {
 1781: 	$r->print(&mt('Not allowed'));
 1782: 	return;
 1783:     }
 1784:     my %records=&Apache::lonnet::dump('nohist_email',
 1785: 			 $env{'course.'.$env{'request.course.id'}.'.domain'},
 1786: 			 $env{'course.'.$env{'request.course.id'}.'.num'},
 1787:                          '%255b'.$user.'%253a'.$domain.'%255d');
 1788:     my $result='';
 1789:     foreach my $key (sort(keys(%records))) {
 1790:         my %content=&Apache::lonmsg::unpackagemsg($records{$key});
 1791:         next if ($content{'senderdomain'} eq '');
 1792: 	&Apache::lonfeedback::newline_to_br(\$content{'message'});
 1793:         if ($content{'subject'}=~/^Record/) {
 1794: 	    $result.='<h3>'.&mt('Record').'</h3>';
 1795:         } elsif ($content{'subject'}=~/^Broadcast/) {
 1796:             $result .='<h3>'.&mt('Broadcast Message').'</h3>';
 1797:             if ($content{'subject'}=~/^Broadcast\./) {
 1798:                 if (defined($content{'coursemsgid'})) {
 1799:                     my $crsmsgid = &escape($content{'coursemsgid'});
 1800:                     my $broadcast_message = &general_message($crsmsgid);
 1801:                     $content{'message'} = '<b>'.&mt('Subject').': '.$content{'message'}.'</b><br />'.$broadcast_message;
 1802:                 } else {
 1803:                     %content=&Apache::lonmsg::unpackagemsg($content{'message'});
 1804:                     $content{'message'} =
 1805:                     '<b>'.&mt('Subject').': '.$content{'subject'}.'</b><br />'.
 1806:                     $content{'message'};
 1807:                 }
 1808:             }
 1809:         } elsif ($content{'subject'}=~/^Archive/) {
 1810:             $result.='<h3>'.&mt('Archived Message').'</h3>';
 1811:             if (defined($content{'coursemsgid'})) {
 1812:                 my $crsmsgid = &escape($content{'coursemsgid'});
 1813:                 my $archive_message = &general_message($crsmsgid);
 1814:                 $content{'message'} = '<b>'.&mt('Subject').': '.$content{'message'}.'</b><br />'.$archive_message;
 1815:             } else {
 1816:                 %content=&Apache::lonmsg::unpackagemsg($content{'message'});
 1817:                 $content{'message'} =
 1818:                 '<b>'.&mt('Subject').': '.$content{'subject'}.'</b><br
 1819: '.
 1820:                 $content{'message'};
 1821:             }
 1822:         } else {
 1823:             $result.='<h3>'.&mt('Critical Message').'</h3>';
 1824:             if (defined($content{'coursemsgid'})) {
 1825:                 my $crsmsgid=&escape($content{'coursemsgid'});
 1826:                 my $critical_message = &general_message($crsmsgid);
 1827:                 $content{'message'} = '<b>'.&mt('Subject').': '.$content{'message'}.'</b><br />'.$critical_message;
 1828:             } else {
 1829:                 %content=&Apache::lonmsg::unpackagemsg($content{'message'});
 1830:                 $content{'message'}=
 1831:                 '<b>'.&mt('Subject').': '.$content{'subject'}.'</b><br />'.
 1832: 		$content{'message'};
 1833:             }
 1834:         }
 1835:         $result.=&mt('By').': <b>'.
 1836: &Apache::loncommon::aboutmewrapper(
 1837:  &Apache::loncommon::plainname($content{'sendername'},$content{'senderdomain'}),$content{'sendername'},$content{'senderdomain'}).'</b> ('.
 1838: $content{'sendername'}.':'.
 1839:             $content{'senderdomain'}.') '.$content{'time'}.
 1840:             '<br /><pre>'.
 1841:               &Apache::lontexconvert::msgtexconverted($content{'message'}).
 1842: 	      '</pre>';
 1843:      }
 1844:     # Check to see if there were any messages.
 1845:     if ($result eq '') {
 1846:         my $lctype = &mt(lc(&Apache::loncommon::course_type()));
 1847: 	if ($target ne 'tex') { 
 1848: 	    $r->print("<p><b>".&mt('No notes, face-to-face discussion records, critical messages, or broadcast messages in this [_1].',$lctype)."</b></p>");
 1849: 	} else {
 1850: 	    $r->print('\textbf{'.&mt('No notes, face-to-face discussion records, critical messages or broadcast messages in this [_1].',$lctype).'}\\\\');
 1851: 	}
 1852:     } else {
 1853:        $r->print($result);
 1854:     }
 1855: }
 1856: 
 1857: sub general_message {
 1858:     my ($crsmsgid) = @_;
 1859:     my %general_content;
 1860:     if ($crsmsgid) { 
 1861:         my %course_content = &Apache::lonnet::get('nohist_email',[$crsmsgid],
 1862:                            $env{'course.'.$env{'request.course.id'}.'.domain'},
 1863:                            $env{'course.'.$env{'request.course.id'}.'.num'});
 1864:         %general_content = &Apache::lonmsg::unpackagemsg($course_content{$crsmsgid});
 1865:     }
 1866:     return $general_content{'message'};
 1867: }
 1868: 
 1869: # ---------------------------------------------------------------- Face to face
 1870: 
 1871: sub facetoface {
 1872:     my ($r,$stage)=@_;
 1873:     if (!&Apache::lonnet::allowed('dff',$env{'request.course.id'})
 1874: 	&& ! &Apache::lonnet::allowed('dff',$env{'request.course.id'}.
 1875: 				      '/'.$env{'request.course.sec'})) {
 1876: 	$r->print(&mt('Not allowed'));
 1877: 	return;
 1878:     }
 1879:     my $crstype = &Apache::loncommon::course_type();
 1880:     my $leaders = ($crstype eq 'Group') ? 'coordinators and leaders'
 1881:                                         : 'faculty and staff';
 1882:     &printheader($r,
 1883: 		 '/adm/email?recordftf=query',
 1884: 		 "User Notes, Face-to-Face, Critical Messages, Broadcast Messages, Archived Messages");
 1885: # from query string
 1886: 
 1887:     if ($env{'form.recname'}) { $env{'form.recuname'}=$env{'form.recname'}; }
 1888:     if ($env{'form.recdom'}) { $env{'form.recdomain'}=$env{'form.recdom'}; }
 1889: 
 1890:     my $defdom=$env{'user.domain'};
 1891: # already filled in
 1892:     if ($env{'form.recdomain'}) { $defdom=$env{'form.recdomain'}; }
 1893: # generate output
 1894:     my $domform = &Apache::loncommon::select_dom_form($defdom,'recdomain');
 1895:     my $stdbrws = &Apache::loncommon::selectstudent_link
 1896: 	('stdselect','recuname','recdomain');
 1897:     my %lt=&Apache::lonlocal::texthash('user' => 'Username',
 1898: 				       'dom' => 'Domain',
 1899: 				       'head' => "User Notes, Records of Face-To-Face Discussions, Critical Messages, Broadcast Messages and Archived Messages in $crstype",
 1900: 				       'subm' => 'Retrieve discussion and message records',
 1901: 				       'newr' => 'New Record (record is visible to '.lc($crstype).' '.$leaders.')',
 1902: 				       'post' => 'Post this Record');
 1903: 
 1904:     $r->print('<h2>'.$lt{'head'}.'</h2>'
 1905:              .'<form method="post" action="/adm/email" name="stdselect">'
 1906:              .'<input type="hidden" name="recordftf" value="retrieve" />'
 1907:              .&Apache::lonhtmlcommon::start_pick_box()
 1908:              .&Apache::lonhtmlcommon::row_title($lt{'user'})
 1909:              .'<input type="text" size="12" name="recuname" value="'.$env{'form.recuname'}.'" />'
 1910:              .' '.$stdbrws
 1911:              .&Apache::lonhtmlcommon::row_closure()
 1912:              .&Apache::lonhtmlcommon::row_title($lt{'dom'})
 1913:              .$domform
 1914:              .&Apache::lonhtmlcommon::row_closure(1)
 1915:              .&Apache::lonhtmlcommon::end_pick_box()
 1916:              .'<br />'
 1917:              .'<input type="submit" value="'.$lt{'subm'}.'" />'
 1918:     );
 1919: 
 1920:     if (($stage ne 'query') &&
 1921:         ($env{'form.recdomain'}) && ($env{'form.recuname'})) {
 1922:         chomp($env{'form.newrecord'});
 1923:         if ($env{'form.newrecord'}) {
 1924: 	    &Apache::lonmsg::store_instructor_comment($env{'form.newrecord'},
 1925: 						      $env{'form.recuname'},
 1926: 						      $env{'form.recdomain'});
 1927:         }
 1928:         my $aboutmelink=&Apache::loncommon::aboutmewrapper(
 1929:                             &Apache::loncommon::plainname($env{'form.recuname'}
 1930:                                                          ,$env{'form.recdomain'})
 1931:                            ,$env{'form.recuname'},$env{'form.recdomain'});
 1932:         $r->print('<hr />'
 1933:                  .'<h2>'
 1934:                  .&mt('Discussion and message records for [_1] ([_2])'
 1935:                      ,$aboutmelink
 1936:                      ,$env{'form.recuname'}.':'.$env{'form.recdomain'})
 1937:                  .'<h2>'
 1938:        );
 1939:         &disfacetoface($r,$env{'form.recuname'},$env{'form.recdomain'});
 1940: 	$r->print(<<ENDRHEAD);
 1941: <form method="post" action="/adm/email">
 1942: <input name="recdomain" value="$env{'form.recdomain'}" type="hidden" />
 1943: <input name="recuname" value="$env{'form.recuname'}" type="hidden" />
 1944: ENDRHEAD
 1945:         $r->print(<<ENDBFORM);
 1946: <hr />$lt{'newr'}<br />
 1947: <textarea name="newrecord" cols="80" rows="10" wrap="hard"></textarea>
 1948: <br />
 1949: <input type="hidden" name="recordftf" value="post" />
 1950: <input type="submit" value="$lt{'post'}" />
 1951: </form>
 1952: ENDBFORM
 1953:     }
 1954: }
 1955: 
 1956: # ----------------------------------------------------------- Blocking during exams
 1957: 
 1958: sub examblock {
 1959:     my ($r,$action) = @_;
 1960:     unless ($env{'request.course.id'}) { return;}
 1961:     if (!&Apache::lonnet::allowed('dcm',$env{'request.course.id'})
 1962: 	&& ! &Apache::lonnet::allowed('dcm',$env{'request.course.id'}.
 1963: 				      '/'.$env{'request.course.sec'})) {
 1964: 	$r->print('Not allowed');
 1965: 	return;
 1966:     }
 1967:     my $usertype = (&Apache::loncommon::course_type() eq 'Group') ? 'members'
 1968: 	                                                          : 'students';
 1969:     my %lt=&Apache::lonlocal::texthash(
 1970:             'comb' => 'Communication Blocking',
 1971:             'cbds' => 'Communication blocking during scheduled exams',
 1972:             '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.",
 1973:              'mecb' => 'Modify existing communication blocking periods',
 1974:              'ncbc' => 'No communication blocks currently saved',
 1975:              'stor' => 'Save',
 1976:     );
 1977: 
 1978:     my %ltext = &Apache::lonlocal::texthash(
 1979:             'dura' => 'Duration',
 1980:             'setb' => 'Set by',
 1981:             'even' => 'Event',
 1982:             'blck' => 'Blocked?',
 1983:             'actn' => 'Action',
 1984:             'star' => 'Start',
 1985:             'endd' => 'End'
 1986:     );
 1987: 
 1988:     &printheader($r,'/adm/email?block=display',$lt{'comb'});
 1989:     $r->print('<h3>'.$lt{'cbds'}.'</h3>');
 1990: 
 1991:     if ($action eq 'store') {
 1992:         &blockstore($r);
 1993:     }
 1994: 
 1995:     $r->print($lt{'desc'}.'<br /><br />
 1996:                <form name="blockform" method="post" action="/adm/email?block=store">
 1997:              ');
 1998: 
 1999:     $r->print('<h4>'.$lt{'mecb'}.'</h4>');
 2000:     my %records = ();
 2001:     my $blockcount = 0;
 2002:     my $parmcount = 0;
 2003:     &get_blockdates(\%records,\$blockcount);
 2004:     if ($blockcount > 0) {
 2005:         $parmcount = &display_blocker_status($r,\%records,\%ltext);
 2006:     } else {
 2007:         $r->print($lt{'ncbc'}.'<br /><br />');
 2008:     }
 2009:     &display_addblocker_table($r,$parmcount,\%ltext);
 2010:     my $end_page=&Apache::loncommon::end_page();
 2011:     $r->print(<<"END");
 2012: <br />
 2013: <input type="hidden" name="blocktotal" value="$blockcount" />
 2014: <input type ="submit" value="$lt{'stor'}" />
 2015: </form>
 2016: $end_page
 2017: END
 2018:     return;
 2019: }
 2020: 
 2021: sub blockstore {
 2022:     my $r = shift;
 2023:     my %lt=&Apache::lonlocal::texthash(
 2024:             'tfcm' => 'The following changes were made',
 2025:             'ncwm' => 'No changes were made.' 
 2026:     );
 2027:     my %adds = ();
 2028:     my %removals = ();
 2029:     my %cancels = ();
 2030:     my $modtotal = 0;
 2031:     my $canceltotal = 0;
 2032:     my $addtotal = 0;
 2033:     my %blocking = ();
 2034:     $r->print('<h3>'.$lt{'head'}.'</h3>');
 2035:     foreach my $envkey (keys(%env)) {
 2036:         if ($envkey =~ m/^form\.modify_(\d+)$/) {
 2037:             $adds{$1} = $1;
 2038:             $removals{$1} = $1;
 2039:             $modtotal ++;
 2040:         } elsif ($envkey =~ m/^form\.cancel_(\d+)$/) {
 2041:             $cancels{$1} = $1;
 2042:             unless ( defined($removals{$1}) ) {
 2043:                 $removals{$1} = $1;
 2044:                 $canceltotal ++;
 2045:             }
 2046:         } elsif ($envkey =~ m/^form\.add_(\d+)$/) {
 2047:             $adds{$1} = $1;
 2048:             $addtotal ++;
 2049:         } 
 2050:     }
 2051: 
 2052:     foreach my $key (keys(%removals)) {
 2053:         my $hashkey = $env{'form.key_'.$key};
 2054:         &Apache::lonnet::del('comm_block',["$hashkey"],
 2055:                          $env{'course.'.$env{'request.course.id'}.'.domain'},
 2056:                          $env{'course.'.$env{'request.course.id'}.'.num'}
 2057:                          );
 2058:     }
 2059:     foreach my $key (keys(%adds)) {
 2060:         unless ( defined($cancels{$key}) ) {
 2061:             my ($newstart,$newend) = &get_dates_from_form($key);
 2062:             my $newkey = $newstart.'____'.$newend;
 2063:             my $blocktypes = &get_block_choices($key);
 2064:             $blocking{$newkey} = {
 2065:                           setter => $env{'user.name'}.':'.$env{'user.domain'},
 2066:                           event  => &escape($env{'form.title_'.$key}),
 2067:                           blocks => $blocktypes,
 2068:                         };
 2069:         }
 2070:     }
 2071:     if ($addtotal + $modtotal > 0) {
 2072:         &Apache::lonnet::put('comm_block',\%blocking,
 2073:                      $env{'course.'.$env{'request.course.id'}.'.domain'},
 2074:                      $env{'course.'.$env{'request.course.id'}.'.num'}
 2075:                      );
 2076:     }
 2077:     my $chgestotal = $canceltotal + $modtotal + $addtotal;
 2078:     if ($chgestotal > 0) {
 2079:         $r->print($lt{'tfcm'}.'<ul>');
 2080:         if ($canceltotal > 0) {
 2081:             $r->print('<li>'.&mt('[quant,_1,communication blocking period was,communication blocking periods were] removed.',$canceltotal).'</li>');
 2082:         }
 2083:         if ($modtotal > 0) {
 2084:             $r->print('<li>'.&mt('[quant,_1,communication blocking period was,communication blocking periods were] modified.',$modtotal).'</li>');
 2085:         }
 2086:         if ($addtotal > 0) {
 2087:             $r->print('<li>'.&mt('[quant,_1,communication blocking period was,communication blocking periods were] added.',$addtotal).'</li>');
 2088:         }
 2089:         $r->print('</ul>');
 2090:     } else {
 2091:         $r->print($lt{'ncwm'});
 2092:     }
 2093:     $r->print('<br />');
 2094:     return;
 2095: }
 2096: 
 2097: sub get_dates_from_form {
 2098:     my $item = shift;
 2099:     my $startdate = &Apache::lonhtmlcommon::get_date_from_form('startdate_'.$item);
 2100:     my $enddate   = &Apache::lonhtmlcommon::get_date_from_form('enddate_'.$item);
 2101:     return ($startdate,$enddate);
 2102: }
 2103: 
 2104: sub get_blockdates {
 2105:     my ($records,$blockcount) = @_;
 2106:     $$blockcount = 0;
 2107:     %{$records} = &Apache::lonnet::dump('comm_block',
 2108:                          $env{'course.'.$env{'request.course.id'}.'.domain'},
 2109:                          $env{'course.'.$env{'request.course.id'}.'.num'}
 2110:                          );
 2111:     $$blockcount = keys(%{$records});
 2112: 
 2113:     if ((keys(%{$records}))[0] =~ /^error: 2 /) {
 2114: 	$records = {};
 2115: 	$$blockcount = 0;
 2116:     }
 2117: }
 2118: 
 2119: sub get_block_choices {
 2120:     my $item = shift;
 2121:     my $blocklist;
 2122:     my ($typeorder,$types) = &blocktype_text();
 2123:     foreach my $type (@{$typeorder}) {
 2124:         if ($env{'form.'.$type.'_'.$item}) {
 2125:             $blocklist->{$type} = 'on'; 
 2126:         } else {
 2127:             $blocklist->{$type} = 'off';
 2128:         }
 2129:     }
 2130:     return $blocklist;
 2131: }
 2132: 
 2133: sub display_blocker_status {
 2134:     my ($r,$records,$ltext) = @_;
 2135:     my $parmcount = 0;
 2136:   
 2137:     my %lt = &Apache::lonlocal::texthash(
 2138:         'modi' => 'Modify',
 2139:         'canc' => 'Cancel',
 2140:     );
 2141:     my ($typeorder,$types) = &blocktype_text();
 2142:     $r->print(&Apache::loncommon::start_data_table());
 2143:     $r->print(<<"END");
 2144:   <tr>
 2145:     <th>$ltext->{'dura'}</th>
 2146:     <th>$ltext->{'setb'}</th>
 2147:     <th>$ltext->{'even'}</th>
 2148:     <th>$ltext->{'blck'}</th>
 2149:     <th>$ltext->{'actn'}?</th>
 2150:   </tr>
 2151: END
 2152:     foreach my $record (sort(keys(%{$records}))) {
 2153:         my $onchange = 'onFocus="javascript:window.document.forms['.
 2154:                        "'blockform'].elements['modify_".$parmcount."'].".
 2155:                        'checked=true;"';
 2156:         my ($start,$end) = split(/____/,$record);
 2157:         my $startform = &Apache::lonhtmlcommon::date_setter('blockform','startdate_'.$parmcount,$start,$onchange);
 2158:         my $endform = &Apache::lonhtmlcommon::date_setter('blockform','enddate_'.$parmcount,$end,$onchange);
 2159: 	
 2160: 	my ($setuname,$setudom,$title,$blocks) = 
 2161: 	    &Apache::loncommon::parse_block_record($$records{$record});
 2162: 	$title = &HTML::Entities::encode($title,'"<>&');
 2163:         my $settername = 
 2164:            &Apache::loncommon::aboutmewrapper(
 2165:                            &Apache::loncommon::plainname($setuname,$setudom),
 2166:                            $setuname,$setudom);
 2167:         $r->print(&Apache::loncommon::start_data_table_row());
 2168:         $r->print(<<"END");
 2169:         <td>$ltext->{'star'}:&nbsp;$startform<br/>$ltext->{'endd'}:&nbsp;&nbsp;$endform</td>
 2170:         <td>$settername</td>
 2171:         <td><input type="text" name="title_$parmcount" size="15" value="$title" /><input type="hidden" name="key_$parmcount" value="$record" /></td>
 2172:         <td>
 2173: END
 2174:         foreach my $block (@{$typeorder}) {
 2175:             my $blockstatus = '';
 2176:             if ($blocks->{$block} eq 'on') {
 2177:                 $blockstatus = 'checked="checked"';
 2178:             }
 2179:             $r->print('<label><input type="checkbox" name="'.$block.'_'.$parmcount.'" '.$blockstatus.' value="1" />'.$types->{$block}.'</label><br />');
 2180:         }
 2181:         $r->print(<<"END");
 2182:         </td>      
 2183:         <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>
 2184: END
 2185:         $r->print(&Apache::loncommon::end_data_table_row());
 2186:         $parmcount++;
 2187:     }
 2188:     $r->print(<<"END");
 2189: </table>
 2190: <br />
 2191: <br />
 2192: END
 2193:     return $parmcount;
 2194: }
 2195: 
 2196: sub display_addblocker_table {
 2197:     my ($r,$parmcount,$ltext) = @_;
 2198:     my $start = time;
 2199:     my $end = $start + (60 * 60 * 2); #Default is an exam of 2 hours duration.
 2200:     my $onchange = 'onFocus="javascript:window.document.forms['.
 2201:                    "'blockform'].elements['add_".$parmcount."'].".
 2202:                    'checked=true;"';
 2203:     my $startform = &Apache::lonhtmlcommon::date_setter('blockform','startdate_'.$parmcount,$start,$onchange);
 2204:     my $endform = &Apache::lonhtmlcommon::date_setter('blockform','enddate_'.$parmcount,$end,$onchange);
 2205:     my %lt = &Apache::lonlocal::texthash(
 2206:         'addb' => 'Add block',
 2207:         'exam' => 'e.g., Exam 1',
 2208:         'addn' => 'Add new communication blocking periods'
 2209:     );
 2210:     my ($typeorder,$types) = &blocktype_text();
 2211:     $r->print(<<"END");
 2212: <h4>$lt{'addn'}</h4> 
 2213: END
 2214:     $r->print(&Apache::loncommon::start_data_table());
 2215:     $r->print(<<"END");
 2216:    <tr>
 2217:      <th>$ltext->{'dura'}</th>
 2218:      <th>$ltext->{'even'} $lt{'exam'}</th>
 2219:      <th>$ltext->{'blck'}</th>
 2220:      <th>$ltext->{'actn'}?</th>
 2221:    </tr>
 2222: END
 2223:     $r->print(&Apache::loncommon::start_data_table_row());
 2224:     $r->print(<<"END");
 2225:      <td>$ltext->{'star'}:&nbsp;$startform<br />$ltext->{'endd'}:&nbsp;&nbsp;$endform</td>
 2226:      <td><input type="text" name="title_$parmcount" size="15" value="" /></td>
 2227:      <td>
 2228: END
 2229:     foreach my $block (@{$typeorder}) {
 2230:         $r->print('<label><input type="checkbox" name="'.$block.'_'.$parmcount.'" value="1" />'.$types->{$block}.'</label><br />');
 2231:      }
 2232:      $r->print(<<"END");
 2233:      </td> 
 2234:      <td><label>$lt{'addb'}?&nbsp;<input type="checkbox" name="add_$parmcount" value="1" /></label></td>
 2235: END
 2236:     $r->print(&Apache::loncommon::end_data_table_row());
 2237:     $r->print(&Apache::loncommon::end_data_table());
 2238:     return;
 2239: }
 2240: 
 2241: sub blocktype_text {
 2242:     my %types = &Apache::lonlocal::texthash(
 2243:         'com' => 'Messaging',
 2244:         'chat' => 'Chat Room',
 2245:         'boards' => 'Discussion',
 2246:         'port' => 'Portfolio',
 2247:         'groups' => 'Groups',
 2248:         'blogs' => 'Blogs',
 2249:     );
 2250:     my $typeorder = ['com','chat','boards','port','groups','blogs'];
 2251:     return ($typeorder,\%types);
 2252: }
 2253: 
 2254: # ----------------------------------------------------------- Display a message
 2255: 
 2256: sub displaymessage {
 2257:     my ($r,$msgid,$folder,$msgstatus)=@_;
 2258:     my $suffix=&Apache::lonmsg::foldersuffix($folder);
 2259:     my %blocked = ();
 2260:     my %setters = ();
 2261:     my $numblocked = 0;
 2262:     my $crstype = &Apache::loncommon::course_type();
 2263: 
 2264: # info to generate "next" and "previous" buttons and check if message is blocked
 2265:     my ($startblock,$endblock) = &Apache::loncommon::blockcheck(\%setters,'com');
 2266:     my @messages=&sortedmessages(\%blocked,$startblock,$endblock,\$numblocked,$folder,$msgstatus);
 2267:     if ( $blocked{$msgid} eq 'ON' ) {
 2268:         &printheader($r,'/adm/email',&mt('Display a Message'));
 2269:         $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.'));
 2270:         &build_block_table($r,$startblock,$endblock,\%setters);
 2271:         return;
 2272:     }
 2273:     if ($msgstatus eq '') {
 2274:         &statuschange($msgid,'read',$folder);
 2275:     }
 2276:     my %message=&Apache::lonnet::get('nohist_email'.$suffix,[$msgid]);
 2277:     my %content=&Apache::lonmsg::unpackagemsg($message{$msgid});
 2278:     my $counter=0;
 2279:     my $escmsgid=&escape($msgid);
 2280:     foreach (@messages) {
 2281: 	if ($_->[5] eq $escmsgid){
 2282: 	    last;
 2283: 	}
 2284: 	$counter++;
 2285:     }
 2286: 
 2287:     my $see_anonymous;
 2288:     my $from_student = 0;
 2289:     if ($env{'request.course.id'} eq $content{'courseid'}) {
 2290: 	my $cdom = $env{'course.'.$env{'request.course.id'}.'.domain'};
 2291: 	my $cnum = $env{'course.'.$env{'request.course.id'}.'.num'};
 2292: 	my $username = $content{'sendername'}.':'.$content{'senderdomain'};
 2293: 	my %classlist_entry =
 2294: 	    &Apache::lonnet::get('classlist',[$username],$cdom,$cnum);
 2295: 	if (exists($classlist_entry{$username})) {
 2296: 	    $from_student = 1;
 2297: 	    $see_anonymous = &Apache::lonnet::allowed('rin',$env{'request.course.id'}.($env{'request.course.sec'}?'/'.$env{'request.course.sec'}:''));
 2298: 	}
 2299:     }
 2300: 
 2301: 
 2302:     my $number_of_messages = scalar(@messages); #subtract 1 for last index
 2303: # start output
 2304:     &printheader($r,'/adm/email?display='.&escape($msgid),'Display a Message','',$content{'baseurl'});
 2305:     my %courseinfo=&Apache::lonnet::coursedescription($content{'courseid'});
 2306: 
 2307: # Prepare available functions
 2308:     my @functionlist;
 2309:     if (!$content{'noreplies'}) {
 2310:         push(@functionlist,'<a href="/adm/email?replyto='.&escape($msgid).$sqs.'">'
 2311:                      .&mt('Reply')
 2312:                      .'</a>');
 2313:     }
 2314:     push(@functionlist,'<a href="/adm/email?forward='.&escape($msgid).$sqs.'">'
 2315:                   .&mt('Forward')
 2316:                   .'</a>');
 2317:     push(@functionlist,'<a href="/adm/email?markunread='.&escape($msgid).$sqs.'">'
 2318:                   .&mt('Mark Unread')
 2319:                   .'</a>');
 2320:     push(@functionlist,'<a href="/adm/email?markdel='.&escape($msgid).$sqs.'">'
 2321:                   .&mt('Delete')
 2322:                   .'</a>');
 2323:     push(@functionlist,'<a href="/adm/email?'.$sqs.'">'
 2324:                   .&mt('Back to Folder Display')
 2325:                   .'</a>');
 2326:     if ($counter > 0){
 2327:         push(@functionlist,'<a href="/adm/email?display='.$messages[$counter-1]->[5].$sqs.'">'
 2328:                           .&mt('Previous')
 2329:                           .'</a>');
 2330:     }
 2331:     if ($counter < $number_of_messages - 1){
 2332:         push(@functionlist,'<a href="/adm/email?display='.$messages[$counter+1]->[5].$sqs.'">'
 2333:                           .&mt('Next')
 2334:                           .'</a>');
 2335:     }
 2336: 
 2337: # Prepare available actions
 2338:     my $symb;
 2339:     if (defined($content{'symb'})) {
 2340:         $symb = $content{'symb'};
 2341:     } elsif (defined($content{'baseurl'})) {
 2342:         $symb=&Apache::lonnet::symbread($content{'baseurl'});
 2343:     }
 2344:         my @actionlist;
 2345:     if ($env{'user.adv'}) {
 2346: 
 2347: 	if (&Apache::lonnet::allowed('vgr',$env{'request.course.id'})) {
 2348: 		push(@actionlist,&Apache::loncommon::track_student_link(
 2349:                                       &mt('View recent activity')
 2350:                                      ,$content{'sendername'}
 2351:                                      ,$content{'senderdomain'}
 2352:                                      ,'check'));
 2353: 	}
 2354: 	if (&Apache::lonnet::allowed('opa',$env{'request.course.id'}) && $symb) {
 2355: 	    push(@actionlist,&Apache::loncommon::pprmlink(
 2356:                                   &mt('Set/Change parameters')
 2357:                                      ,$content{'sendername'}
 2358:                                      ,$content{'senderdomain'}
 2359:                                      ,$symb
 2360:                                      ,'check'));
 2361: 	}
 2362: 	if (&Apache::lonnet::allowed('mgr',$env{'request.course.id'}) && $symb) {
 2363: 	    push(@actionlist,&Apache::loncommon::pgrdlink(
 2364:                                  &mt('Set/Change grades')
 2365:                                      ,$content{'sendername'}
 2366:                                      ,$content{'senderdomain'}
 2367:                                      ,$symb
 2368:                                      ,'check'));
 2369: 	}
 2370:     }
 2371: 
 2372: # Print functionlist and actionlist in page header
 2373:         my $functions='<div class="LC_columnSection">';
 2374: 
 2375:         # Functionlist
 2376:         $functions.=&Apache::lonhtmlcommon::start_funclist();
 2377:         foreach my $item (@functionlist) {
 2378:             $functions.=&Apache::lonhtmlcommon::add_item_funclist($item);
 2379:         }
 2380:         $functions .= &Apache::lonhtmlcommon::end_funclist();
 2381: 
 2382:         # Actionlist
 2383:         if (@actionlist) {
 2384:             my $legendtext=&mt('Currently available actions (will open extra window)');
 2385:             $functions.=&Apache::lonhtmlcommon::start_funclist($legendtext);
 2386:             foreach my $item (@actionlist) {
 2387:                 $functions.=&Apache::lonhtmlcommon::add_item_funclist($item);
 2388:             }
 2389:             $functions.=&Apache::lonhtmlcommon::end_funclist();
 2390:         }
 2391: 
 2392:         $functions.='</div>';
 2393:         $r->print(&Apache::loncommon::head_subbox($functions));
 2394: 
 2395: 
 2396:     my ($tonum,$tolist,$cclist,$bcclist,$groupcclist,%recipients);
 2397:     if ($content{'recipid'}) {
 2398:         $tonum = &retrieve_recips('display',\%content,\%recipients);
 2399:         if (ref($recipients{'cc'}) eq 'ARRAY') {
 2400:             $cclist = join(', ',@{$recipients{'cc'}});
 2401:         }
 2402:         if (ref($recipients{'to'}) eq 'ARRAY') {
 2403:             $tolist = join(', ',@{$recipients{'to'}});
 2404:         }
 2405:         if (ref($recipients{'bcc'}) eq 'ARRAY') {
 2406:             $bcclist = join(', ',@{$recipients{'bcc'}});
 2407:         }
 2408:     }
 2409: 
 2410:     my $broadcast_link;
 2411:     if (($content{'courseid'}) && ($content{'recipid'} &&
 2412:         (ref($recipients{'course_broadcast'}) eq 'ARRAY') ||
 2413:         (ref($recipients{'group_cc_broadcast'}) eq 'ARRAY') ||
 2414:         (ref($recipients{'group_bcc_broadcast'}) eq 'ARRAY'))) {
 2415:         $broadcast_link = &recipients_link($r,\%content,\%recipients);
 2416:     }
 2417: 
 2418:     if (((!$tolist) && (!$broadcast_link)) && ref($content{'recuser'}) eq 'ARRAY') {
 2419:         my @recipients;
 2420:         for (my $i=0; $i<@{$content{'recuser'}}; $i++) {
 2421:             $recipients[$i] =  &Apache::loncommon::aboutmewrapper(
 2422:                &Apache::loncommon::plainname($content{'recuser'}[$i],
 2423:                                       $content{'recdomain'}[$i]),
 2424:                   $content{'recuser'}[$i],$content{'recdomain'}[$i]).
 2425:            ' ('.$content{'recuser'}[$i].':'.$content{'recdomain'}[$i].') ';
 2426:         }
 2427:         $tolist = join(', ',@recipients);
 2428:     }
 2429:     my ($restitle,$baseurl,$refers_to);
 2430:     if (defined($content{'resource_title'})) {
 2431:         $restitle = $content{'resource_title'};
 2432:     } else {
 2433:         if (defined($content{'baseurl'})) {
 2434:             $restitle = &Apache::lonnet::gettitle($content{'baseurl'});
 2435:         }
 2436:     }
 2437:     if (defined($content{'baseurl'})) {
 2438:         $baseurl = &Apache::lonenc::check_encrypt($content{'baseurl'});
 2439:     }
 2440:     $r->print('<div class="LC_clear_float_footer">');
 2441:     if ($from_student && $see_anonymous ) {
 2442: 	$r->print(&Apache::loncommon::student_image_tag($content{'senderdomain'},$content{'sendername'}).'</br>');
 2443:     }
 2444: 
 2445:     # Display LON-CAPA Message (Start)
 2446:     # Subject
 2447:     $r->print('</div>'
 2448:              .&Apache::lonhtmlcommon::start_pick_box()
 2449:              .&Apache::lonhtmlcommon::row_title(&mt('Subject'))
 2450:              .$content{'subject'}
 2451:              .&Apache::lonhtmlcommon::row_closure()
 2452:     );
 2453:     if ($folder eq 'sent') {
 2454:         # To
 2455:         if ($tolist) {
 2456:             $r->print(&Apache::lonhtmlcommon::row_title(&mt('To'))
 2457:                      .$tolist
 2458:                      .&Apache::lonhtmlcommon::row_closure()
 2459:             );
 2460:         }
 2461:         if ($cclist) {
 2462:             $r->print(&Apache::lonhtmlcommon::row_title(&mt('Cc'))
 2463:                      .$cclist
 2464:                      .&Apache::lonhtmlcommon::row_closure()
 2465:             );
 2466:         }
 2467:         if ($bcclist) {
 2468:             $r->print(&Apache::lonhtmlcommon::row_title(&mt('Bcc'))
 2469:                      .$bcclist
 2470:                      .&Apache::lonhtmlcommon::row_closure()
 2471:             );
 2472:         }
 2473:         if (($content{'courseid'}) && ($content{'recipid'})) {
 2474:             my %broadcast_types = 
 2475:                 &Apache::lonlocal::texthash (
 2476:                     course_broadcast    => 'Broadcast to', 
 2477:                     group_cc_broadcast  => 'Cc to group',
 2478:                     group_bcc_broadcast => 'Bcc to group',
 2479:                 );                   
 2480:             foreach my $type (sort(keys(%broadcast_types))) {
 2481:                 if (ref($recipients{$type}) eq 'ARRAY') {
 2482:                     my $num = @{$recipients{$type}};
 2483:                     my $broadcastlist = join(', ',@{$recipients{$type}});
 2484:                     if ($broadcastlist && $broadcast_link) {
 2485:                         if ($type eq 'group_cc_broadcast') {
 2486:                             $groupcclist = $broadcastlist;
 2487:                         }
 2488:                         $r->print(&Apache::lonhtmlcommon::row_title(
 2489:                                             $broadcast_types{$type})
 2490:                                   .&mt('[quant,_1,recipient]',$num)
 2491:                                   .' <a href="javascript:showBroadcastList();">['
 2492:                                   .&mt('Show').']</a>' 
 2493:                                   .&Apache::lonhtmlcommon::row_closure());
 2494:                     }
 2495:                 }
 2496:             }
 2497:         }
 2498:         if ($content{'replytoaddr'}) {
 2499:             my ($replytoname,$replytodom) = split(/:/,$content{'replytoaddr'});
 2500:             if ($replytoname ne '' && $replytodom ne '') {
 2501:                 $r->print(&Apache::lonhtmlcommon::row_title(&mt('Reply To'))
 2502:                          .$replytoname.':'.$replytodom
 2503:                          .&Apache::lonhtmlcommon::row_closure()
 2504:                 );
 2505:             }
 2506:         }
 2507:     } else {
 2508:         # From, Reply
 2509:         $r->print(&Apache::lonhtmlcommon::row_title(&mt('From'))
 2510:                  .&Apache::loncommon::aboutmewrapper(
 2511:                      &Apache::loncommon::plainname($content{'sendername'},$content{'senderdomain'}),
 2512:                                                  $content{'sendername'},$content{'senderdomain'})
 2513:         );
 2514:         if ($content{'noreplies'}) {
 2515:             $r->print(' ('.&mt('No replies to sender').')'
 2516:                      .&Apache::lonhtmlcommon::row_closure()
 2517:             );
 2518:         } else {
 2519:             if ($content{'replytoaddr'}) {
 2520:                 my ($replytoname,$replytodom) = split(/:/,$content{'replytoaddr'});
 2521:                 if ($replytoname ne '' && $replytodom ne '') {
 2522:                     $r->print(&Apache::lonhtmlcommon::row_closure()
 2523:                              .&Apache::lonhtmlcommon::row_title(&mt('Reply To'))
 2524:                              .$replytoname.':'.$replytodom
 2525:                              .&Apache::lonhtmlcommon::row_closure()
 2526:                     );
 2527:                 } else {
 2528:                     $r->print(&Apache::lonhtmlcommon::row_closure());
 2529:                 }
 2530:             } else {
 2531:                 $r->print(' ('.$content{'sendername'}.':'.$content{'senderdomain'}.') '
 2532:                          .&Apache::lonhtmlcommon::row_closure()
 2533:                 );
 2534:             }
 2535:             if ($tonum && $tolist) {
 2536:                 $r->print(&Apache::lonhtmlcommon::row_title(&mt('To'))
 2537:                          .$tolist
 2538:                          .&Apache::lonhtmlcommon::row_closure()
 2539:                     );
 2540:             }
 2541:             if ($cclist) { 
 2542:                 $r->print(&Apache::lonhtmlcommon::row_title(&mt('Cc'))
 2543:                          .$cclist
 2544:                          .&Apache::lonhtmlcommon::row_closure()
 2545:                     );
 2546:             }
 2547:             if ($content{'group'} ne '') {
 2548:                 if (&check_group_priv($content{'group'})) {
 2549:                     $groupcclist = join(', ',@{$recipients{'group_cc_broadcast'}});
 2550:                     if ($groupcclist) {
 2551:                         $r->print(&Apache::lonhtmlcommon::row_title(&mt('Group Cc'))
 2552:                                  .$groupcclist
 2553:                                  .&Apache::lonhtmlcommon::row_closure()
 2554:                         );
 2555:                     }
 2556:                 }
 2557:             }
 2558:         }
 2559:     }
 2560: 
 2561:     # Course
 2562:     if ($content{'courseid'}) {
 2563:         $r->print(&Apache::lonhtmlcommon::row_title(&mt($crstype))
 2564:                  .$courseinfo{'description'}
 2565:         );
 2566:         if ($content{'coursesec'}) {
 2567:             $r->print(' ('.&mt('Section').': '.$content{'coursesec'}.')');
 2568:         }
 2569:         $r->print(&Apache::lonhtmlcommon::row_closure());
 2570:     }
 2571:     $r->print(&Apache::lonhtmlcommon::row_title(&mt('Time'))
 2572:              .$content{'time'}
 2573:              .&Apache::lonhtmlcommon::row_closure()
 2574:     );
 2575: 
 2576:     # Refers to
 2577:     if ($baseurl) {
 2578:         if (defined($content{'courseid'}) && defined($env{'request.course.id'})) {
 2579:             if ($content{'courseid'} eq $env{'request.course.id'}) {
 2580:                 my $symblink;
 2581:                 my $showsymb = &Apache::lonenc::check_decrypt($symb);
 2582:                 my $showurl = &Apache::lonenc::check_decrypt($baseurl);
 2583:                 my $encrypturl = &Apache::lonnet::EXT('resource.0.encrypturl',
 2584:                               $showsymb,$env{'user.domain'},$env{'user.name'});
 2585:                 if ($symb) {
 2586:                     if ($encrypturl =~ /^yes$/i && !$env{'request.role.adv'}) {
 2587:                         $showsymb = &Apache::lonenc::check_encrypt($symb);
 2588:                     }
 2589:                     $symblink = '?symb='.$showsymb;
 2590:                 }
 2591:                 if ($encrypturl =~ /^yes$/i && !$env{'request.role.adv'}) {
 2592:                     $showurl = $baseurl;
 2593:                 }
 2594:                 $r->print(&Apache::lonhtmlcommon::row_title(&mt('Refers to'))
 2595:                          .'<a href="'.$showurl.$symblink.'">'.$restitle.'</a>'
 2596:                          .&Apache::lonhtmlcommon::row_closure()
 2597:                 );
 2598:                 $refers_to = 1;
 2599:             }
 2600:         }
 2601:         if (!$refers_to) {
 2602:             if ($baseurl =~ m-^/enc/-) {
 2603:                 if (defined($content{'courseid'})) {
 2604:                     if (!$env{'request.course.id'}) {
 2605:                         my $unencurl =
 2606:                            &Apache::lonenc::unencrypted($baseurl,
 2607:                                                         $content{'courseid'});
 2608:                         if ($unencurl ne '') {
 2609:                             if (&Apache::lonnet::allowed('bre',$unencurl)) {
 2610:                                 $r->print(&Apache::lonhtmlcommon::row_title(&mt('Refers to'))
 2611:                                          .'<a href="'.$unencurl.'">'.$restitle.'</a>'
 2612:                                          .&Apache::lonhtmlcommon::row_closure()
 2613:                                 );
 2614:                             }
 2615:                         }
 2616:                     }
 2617:                 }
 2618:             } else {
 2619:                 if (&Apache::lonnet::allowed('bre',$baseurl)) {
 2620:                     $r->print(&Apache::lonhtmlcommon::row_title(&mt('Refers to'))
 2621:                              .'<a href="'.$baseurl.'">'.$restitle.'</a>'
 2622:                              .&Apache::lonhtmlcommon::row_closure()
 2623:                     );
 2624: 
 2625:                 }
 2626:             }
 2627:         }
 2628:     }
 2629: 
 2630:     # Message
 2631:     $r->print(&Apache::lonhtmlcommon::row_title(&mt('Message'))
 2632:              .'<pre>'
 2633: 	     .&Apache::lontexconvert::msgtexconverted($content{'message'},1)
 2634: 	     .'</pre>'
 2635:     );
 2636:     if (&displayresource(%content)) {
 2637:         $r->print(&Apache::lonhtmlcommon::row_closure()
 2638:                  .&Apache::lonhtmlcommon::row_title(&mt('Resource Details'))
 2639:                  .&displayresource(%content)
 2640:         );
 2641:     } 
 2642:     $r->print(&Apache::lonhtmlcommon::row_closure(1).
 2643:               &Apache::lonhtmlcommon::end_pick_box());
 2644:     # Display LON-CAPA Message (End)
 2645:     return;
 2646: }
 2647: 
 2648: sub retrieve_recips {
 2649:     my ($context,$content,$recips)= @_;
 2650:     my $tonum = 0;
 2651:     if (ref($content) eq 'HASH') {
 2652:         my %reciphash =
 2653:             &Apache::lonnet::get('nohist_emailrecip',[$content->{'recipid'}],
 2654:                                  $content->{'senderdomain'},$content->{'sendername'});
 2655:         my $recipinfo = $reciphash{$content->{'recipid'}};
 2656:         if (ref($recipinfo) eq 'HASH') {
 2657:             foreach my $type ('to','cc','course_broadcast','group_cc_broadcast','group_bcc_broadcast') {
 2658:                 if (ref($recipinfo->{$type}) eq 'HASH') {
 2659:                     if ($type eq 'to') {
 2660:                         $tonum = keys(%{$recipinfo->{$type}});
 2661:                     }
 2662:                     foreach my $user (sort(keys(%{$recipinfo->{$type}}))) {
 2663:                         my ($uname,$udom) = split(/:/,$user);
 2664:                         next if (($context eq 'replying') && ($uname eq $env{'user.name'})
 2665:                                  && ($udom eq $env{'user.domain'}));
 2666:                         my $showuser ='<span class="LC_nobreak">';
 2667:                         if ($context eq 'replying') {
 2668:                             if (($type eq 'to') || ($type eq 'cc')) { 
 2669:                                 $showuser = '<label><input type="checkbox" name="replying_'.$type.'" value="'.$user.'" />';
 2670:                             } elsif ($type eq 'group_cc_broadcast') {
 2671:                                 $showuser = '<label><input type="checkbox" name="replying_groupcc" value="'.$user.'" />';
 2672:                             }
 2673:                         }
 2674:                         $showuser .= &Apache::loncommon::aboutmewrapper(
 2675:                                            &Apache::loncommon::plainname($uname,
 2676:                                            $udom),$uname,$udom);
 2677: 		        if ($context eq 'replying') {
 2678: 		            $showuser .='</label>';
 2679: 		        }
 2680: 		        $showuser .= '</span>';
 2681:                         if (ref($recips) eq 'HASH') {
 2682: 		            push(@{$recips->{$type}},$showuser);
 2683:                         }
 2684:                     }
 2685:                 }
 2686:             }
 2687:         }
 2688:     }
 2689:     return $tonum;
 2690: }
 2691: 
 2692: sub recipients_link {
 2693:     my ($r,$content,$recipients) = @_;
 2694:     my ($broadcast_link,$show);
 2695:     if ((ref($content) eq 'HASH') && (ref($recipients) eq 'HASH')) {
 2696:         if (ref($recipients->{'course_broadcast'}) eq 'ARRAY') {
 2697:             if (@{$recipients->{'course_broadcast'}} > 0) {
 2698:                 $show = 'course';
 2699:             }
 2700:         } elsif (ref($recipients->{'group_cc_broadcast'}) eq 'ARRAY') {
 2701:             if (@{$recipients->{'group_cc_broadcast'}} > 0) {
 2702:                 $show = 'group_cc';
 2703:             }
 2704:         } elsif (ref($recipients->{'group_bcc_broadcast'}) eq 'ARRAY') {
 2705:             if (@{$recipients->{'group_bcc_broadcast'}} > 0) {
 2706:                 $show = 'group_bcc';
 2707:             }
 2708:         }
 2709:         if ($show) {
 2710:             my ($nothing,$height,$width,$start_page,$end_page,$body);
 2711:             $nothing=&Apache::lonhtmlcommon::javascript_nothing();
 2712:             $height = 400;
 2713:             $width = 600;
 2714:             $start_page =
 2715:                 &Apache::loncommon::start_page('Broadcast List', undef,
 2716:                                                {only_body => 1,
 2717:                                                 js_ready  => 1,});
 2718:             $end_page = &Apache::loncommon::end_page({js_ready => 1,});
 2719:             $body = '<h3>'.&mt("Recipients of broadcast message").'</h3>'.
 2720:                     &Apache::loncommon::start_data_table();
 2721:             my $cell = 0;
 2722:             $body .= &Apache::loncommon::start_data_table_row();
 2723:             foreach my $item (@{$recipients->{$show.'_broadcast'}}) {
 2724:                 $item =~ s/'/\\'/g;
 2725:                 if (!($cell%2) && $cell > 0) {
 2726:                     $body .= &Apache::loncommon::end_data_table_row().
 2727:                              &Apache::loncommon::start_data_table_row();
 2728:                 }
 2729:                 $cell ++;
 2730:                 $body .= '<td>'.$cell.'&nbsp;'.$item.'&nbsp;&nbsp;</td>';
 2731:             }
 2732:             if ($cell%2) {
 2733:                 $body .= '<td>&nbsp;</td>';
 2734:             }
 2735:             $body .= &Apache::loncommon::end_data_table_row().
 2736:                      &Apache::loncommon::end_data_table();
 2737:             $body =~ s{</}{<\\/}g;
 2738:             $body =~ s{\n}{}g;
 2739:             $r->print(<<ENDJS);
 2740: <script type="text/javascript">
 2741: function showBroadcastList() {
 2742:     var caller = this;
 2743:     var newWindow = null;
 2744:     try {
 2745:         newWindow =  window.open($nothing,"broadcast","HEIGHT=$height,WIDTH=$width,resizable=yes,scrollbars=yes" );
 2746:     }
 2747:     catch(error) {
 2748:         writeWin(caller);
 2749:         return;
 2750:     }
 2751:     if (newWindow) {
 2752:         caller = newWindow;
 2753:     }
 2754:     writeWin(caller);
 2755:     return;
 2756: }
 2757: 
 2758: function writeWin(caller) {
 2759:     caller.document.writeln('$start_page $body $end_page');
 2760:     caller.document.close();
 2761:     caller.focus();
 2762: }
 2763: 
 2764: </script>
 2765: 
 2766: ENDJS
 2767:             $broadcast_link = 1;
 2768:         }
 2769:     }
 2770:     return $broadcast_link;
 2771: }
 2772: 
 2773: # =========================================================== Show the citation
 2774: 
 2775: sub displayresource {
 2776:     my %content=@_;
 2777: #
 2778: # If the recipient is in the same course that the message was sent from and
 2779: # has sufficient privileges, show "all details," else show citation
 2780: #
 2781:     if (($env{'request.course.id'} eq $content{'courseid'})
 2782:      && (&Apache::lonnet::allowed('vgr',$content{'courseid'}))) {
 2783:         my $symb;
 2784:         if (defined($content{'symb'})) {
 2785:             $symb = $content{'symb'};
 2786:         } else { 
 2787: 	    $symb=&Apache::lonnet::symbread($content{'baseurl'});
 2788:         }
 2789: # Could not get a symb, give up
 2790: 	unless ($symb) { return $content{'citation'}; }
 2791: # Have a symb, can render
 2792: 	return '<h2>'.&mt('Current attempts of student (if applicable)').'</h2>'.
 2793: 	    &Apache::loncommon::get_previous_attempt($symb,
 2794: 						     $content{'sendername'},
 2795: 						     $content{'senderdomain'},
 2796: 						     $content{'courseid'}).
 2797: 	    '<hr /><h2>'.&mt('Current screen output (if applicable)').'</h2>'.
 2798: 	    &Apache::loncommon::get_student_view($symb,
 2799: 						 $content{'sendername'},
 2800: 						 $content{'senderdomain'},
 2801: 						 $content{'courseid'}).
 2802: 	    '<h2>'.&mt('Correct Answer(s) (if applicable)').'</h2>'.
 2803: 	    &Apache::loncommon::get_student_answers($symb,
 2804: 						    $content{'sendername'},
 2805: 						    $content{'senderdomain'},
 2806: 						    $content{'courseid'});
 2807:     } elsif ($env{'user.adv'}) {
 2808: 	return $content{'citation'};
 2809:     }
 2810:     return '';
 2811: }
 2812: 
 2813: # ================================================================== The Header
 2814: 
 2815: sub header {
 2816:     my ($r,$title,$baseurl)=@_;
 2817:     my $extra = &Apache::loncommon::studentbrowser_javascript();
 2818:     if ($baseurl) {
 2819: 	$extra .= "<base href=\"".&Apache::lonnet::absolute_url()."/$baseurl\" />";
 2820:     }
 2821:     $r->print(&Apache::loncommon::start_page('Communication',
 2822:  					$extra));
 2823:     $r->print(&Apache::lonhtmlcommon::breadcrumbs
 2824:      		(($title?$title:'Send and Receive Messages')));
 2825: }
 2826: 
 2827: # ---------------------------------------------------------------- Print header
 2828: 
 2829: sub printheader {
 2830:     my ($r,$url,$desc,$title,$baseurl)=@_;
 2831:     &Apache::lonhtmlcommon::add_breadcrumb
 2832: 	({href=>$url,
 2833: 	  text=>$desc});
 2834:     &header($r,$title,$baseurl);
 2835: }
 2836: 
 2837: # ------------------------------------------------------------ Store the comment
 2838: 
 2839: sub storecomment {
 2840:     my ($r)=@_;
 2841:     my $msgtxt=&Apache::lonfeedback::clear_out_html($env{'form.message'});
 2842:     my $cleanmsgtxt='';
 2843:     foreach my $line (split(/[\n\r]/,$msgtxt)) {
 2844: 	unless ($line=~/^\s*(\>|\&gt\;)/) {
 2845: 	    $cleanmsgtxt.=$line."\n";
 2846: 	}
 2847:     }
 2848:     my $key=&escape($env{'form.baseurl'}).'___'.time;
 2849:     &Apache::lonnet::put('nohist_stored_comments',{ $key => $cleanmsgtxt });
 2850: }
 2851: 
 2852: sub storedcommentlisting {
 2853:     my ($r)=@_;
 2854:     my %msgs=&Apache::lonnet::dump('nohist_stored_comments',undef,undef,
 2855:        '^'.&escape(&escape($env{'form.showcommentbaseurl'})));
 2856:     $r->print(&Apache::loncommon::start_page('Saved Comment Listing',undef,
 2857: 					     {'onlybody' => 1}));
 2858:     if ((keys %msgs)[0]=~/^error\:/) {
 2859: 	$r->print(&mt('No saved comments yet.'));
 2860:     } else {
 2861: 	my $found=0;
 2862: 	foreach my $key (sort(keys(%msgs))) {
 2863: 	    $r->print("\n".$msgs{$key}."<hr />");
 2864: 	    $found=1;
 2865: 	}
 2866: 	unless ($found) {
 2867: 	    $r->print(&mt('No saved comments yet for this resource.'));
 2868: 	}
 2869:     }
 2870: }
 2871: 
 2872: # ---------------------------------------------------------------- Send an email
 2873: 
 2874: sub sendoffmail {
 2875:     my ($r,$folder)=@_;
 2876:     my $suffix=&Apache::lonmsg::foldersuffix($folder);
 2877:     my $sendstatus='';
 2878:     my %msg_status;
 2879:     my $numsent = 0;
 2880:     my $nosentstore = 1;
 2881:     my $attachmenturl;
 2882:     my $now = time;
 2883:     my ($cdom,$cnum,$group);
 2884:     if (exists($env{'form.group'})) {
 2885:         $group = $env{'form.group'};
 2886:     }
 2887:     if (exists($env{'request.course.id'})) {
 2888:         $cnum = $env{'course.'.$env{'request.course.id'}.'.num'};
 2889:         $cdom = $env{'course.'.$env{'request.course.id'}.'.domain'};
 2890:     }
 2891:     if ($env{'form.send'}) {
 2892:         if (!$env{'form.multiforward'}) { 
 2893:             if ($group eq '') {
 2894: 	        &printheader($r,'','Messages being sent.');
 2895:             } else {
 2896:                 $r->print(&groupmail_header('sending',$group));
 2897:             }
 2898:         }
 2899: 	$r->rflush();
 2900: 	my %content=();
 2901: 	undef %content;
 2902: 	if ($env{'form.forwid'}) {
 2903: 	    my $msgid=$env{'form.forwid'};
 2904: 	    my %message=&Apache::lonnet::get('nohist_email'.$suffix,[$msgid]);
 2905: 	    %content=&Apache::lonmsg::unpackagemsg($message{$msgid},1,1);
 2906: 	    &statuschange($msgid,'forwarded',$folder);
 2907:             if ($content{'attachmenturl'} ne '') {
 2908:                 $attachmenturl = $content{'attachmenturl'};
 2909:             }
 2910: 	    $env{'form.message'} .= "\n\n-- Forwarded message --\n\n".
 2911: 		                    $content{'message'};
 2912: 	}
 2913: 	if ($env{'form.replyid'}) {
 2914: 	    my $msgid=$env{'form.replyid'};
 2915: 	    my %message=&Apache::lonnet::get('nohist_email'.$suffix,[$msgid]);
 2916: 	    %content=&Apache::lonmsg::unpackagemsg($message{$msgid},1);
 2917: 	    &statuschange($msgid,'replied',$folder);
 2918: 	}
 2919: 
 2920: 	my $mode = $env{'form.sendmode'};
 2921: 	my (%toaddr,$tos,$cc,$bcc,$broadcast);
 2922: 
 2923: 	if ($mode eq 'group') {
 2924:             if (defined($env{'form.courserecips'})) {
 2925:                 my $courseusers = $env{'form.courserecips'};
 2926:                 $courseusers =~ s/^_\&\&\&_//;
 2927:                 my @to = split('_&&&_',$courseusers);
 2928:                 foreach my $dest (@to) {
 2929:                     my ($user,$domain) = split(/:/, $dest);
 2930:                     if (($user ne '') && ($domain ne '')) {
 2931:                         my $rec = $user.":".$domain;
 2932:                         $toaddr{$rec} = '';
 2933:                         $broadcast->{$rec} = '';
 2934:                     }
 2935:                 }
 2936:             }
 2937: 	} elsif ($mode eq 'upload') {
 2938:             $nosentstore = 0;
 2939: 	    foreach my $line (split(/[\n\r\f]+/,$env{'form.upfile'})) {
 2940:                 my ($rec,$txt) = ($line =~ /^([^:]+:[^:]+):(.*)$/);
 2941: 		if ($txt) {
 2942:                     $rec =~ s/^\s+//;
 2943:                     $rec =~ s/\s+$//;
 2944: 		    $toaddr{$rec}.=$txt."\n";
 2945:                     $broadcast->{$rec} = '';
 2946: 		}
 2947: 	    }
 2948: 	} else {
 2949: 	    if (($env{'form.recuname'} ne '') && ($env{'form.recdomain'} ne '')) {
 2950: 		$toaddr{$env{'form.recuname'}.':'.$env{'form.recdomain'}}='';
 2951:                 $tos->{$env{'form.recuname'}.':'.$env{'form.recdomain'}}='';
 2952: 	    }
 2953: 	}
 2954:         if ($env{'form.additionalrec_to'}) {
 2955:             foreach my $rec (split(/\s*,\s*/,$env{'form.additionalrec_to'})) {
 2956:                 my ($auname,$audom)=split(/:/,$rec);
 2957:                 if (($auname ne "") && ($audom ne "")) {
 2958:                     $toaddr{$auname.':'.$audom}='';
 2959:                     $tos->{$auname.':'.$audom}='';
 2960:                 }
 2961:             }
 2962:         }
 2963:         if ($env{'form.replying_to'}) {
 2964:             my @toreplies =
 2965:                 &Apache::loncommon::get_env_multiple('form.replying_to');
 2966:             foreach my $rec (@toreplies) {
 2967:                 my ($auname,$audom)=split(/:/,$rec);
 2968:                 if (($auname ne "") && ($audom ne "")) {
 2969:                     $toaddr{$auname.':'.$audom}='';
 2970:                     $tos->{$auname.':'.$audom}='';
 2971:                 }
 2972:             }
 2973:         }
 2974: 	if ($env{'form.additionalrec_cc'}) {
 2975: 	    foreach my $rec (split(/\s*,\s*/,$env{'form.additionalrec_cc'})) {
 2976: 		my ($auname,$audom)=split(/:/,$rec);
 2977: 		if (($auname ne "") && ($audom ne "")) {
 2978:                     $toaddr{$auname.':'.$audom}='';
 2979:                     if (!defined($tos->{$auname.':'.$audom})) {
 2980: 		        $cc->{$auname.':'.$audom}='';
 2981:                     }
 2982: 		}
 2983: 	    }
 2984: 	}
 2985:         if ($env{'form.replying_cc'}) {
 2986:             my @ccreplies =
 2987:                 &Apache::loncommon::get_env_multiple('form.replying_cc');
 2988:             foreach my $rec (@ccreplies) {
 2989:                 my ($auname,$audom)=split(/:/,$rec);
 2990:                 if (($auname ne "") && ($audom ne "")) {
 2991:                     $toaddr{$auname.':'.$audom}='';
 2992:                     if (!defined($tos->{$auname.':'.$audom})) {
 2993:                         $cc->{$auname.':'.$audom}='';
 2994:                     }
 2995:                 }
 2996:             }
 2997:         }
 2998:         if ($env{'form.replying_groupcc'}) {
 2999:             my @groupreplies =
 3000:                 &Apache::loncommon::get_env_multiple('form.replying_groupcc');
 3001:             foreach my $rec (@groupreplies) {
 3002:                 my ($auname,$audom)=split(/:/,$rec);
 3003:                 if (($auname ne "") && ($audom ne "")) {
 3004:                     $toaddr{$auname.':'.$audom}='';
 3005:                     if (!defined($tos->{$auname.':'.$audom})) {
 3006:                         $broadcast->{$auname.':'.$audom}='';
 3007:                     }
 3008:                 }
 3009:             }
 3010:         }
 3011:         if ($env{'form.additionalrec_bcc'}) {
 3012:             foreach my $rec (split(/\s*,\s*/,$env{'form.additionalrec_bcc'})) {
 3013:                 my ($auname,$audom)=split(/:/,$rec);
 3014:                 if (($auname ne "") && ($audom ne "")) {
 3015:                     $toaddr{$auname.':'.$audom}='';
 3016:                     if ((!defined($tos->{$auname.':'.$audom})) && 
 3017:                         (!defined($cc->{$auname.':'.$audom}))) {
 3018:                         $bcc->{$auname.':'.$audom}='';
 3019:                     }
 3020:                 }
 3021:             }
 3022:         }
 3023:         my $savemsg;
 3024:         my $msgtype;
 3025:         my %sentmessage;
 3026:         my $msgsubj=&Apache::lonfeedback::clear_out_html($env{'form.subject'},
 3027: 							 undef,1);
 3028:         if ((($env{'form.critmsg'}) || ($env{'form.sendbck'})) &&
 3029:             (&Apache::lonnet::allowed('srm',$env{'request.course.id'})
 3030: 	     || &Apache::lonnet::allowed('srm',$env{'request.course.id'}.
 3031: 					 '/'.$env{'request.course.sec'})
 3032: 	     )) {
 3033:             $savemsg=&Apache::lonfeedback::clear_out_html($env{'form.message'},1);
 3034:             $msgtype = 'critical';
 3035:         } else {
 3036:             $savemsg=&Apache::lonfeedback::clear_out_html($env{'form.message'});
 3037:         }
 3038:         my %reciphash = (
 3039:                            to => $tos,
 3040:                            cc => $cc,
 3041:                            bcc => $bcc,
 3042:                         );
 3043:         if ($mode eq 'group') {
 3044:             if ($group eq '') {
 3045:                 $reciphash{'course_broadcast'} = $broadcast;
 3046:             } else {
 3047:                 if ($env{'form.groupmail'} eq 'cc') {
 3048:                     $reciphash{'group_cc_broadcast'} = $broadcast;
 3049:                 } else {
 3050:                     $reciphash{'group_bcc_broadcast'} = $broadcast;
 3051:                 }
 3052:             }
 3053:         }
 3054:         my ($recipid,$recipstatus) = 
 3055:             &Apache::lonmsg::store_recipients($msgsubj,$env{'user.name'},
 3056:                                        $env{'user.domain'},\%reciphash);
 3057:         if ($recipstatus ne 'ok') {
 3058:             &Apache::lonnet::logthis('Failed to store To, Bcc and Cc recipients for '.$env{'user.name'}.':'.$env{'user.domain'});
 3059:         }
 3060:         if ($env{'form.attachment'}) {
 3061:             if (length($env{'form.attachment'})<131072) {
 3062:                 $attachmenturl=&Apache::lonnet::userfileupload('attachment',undef,'feedback/'.$now);
 3063:             } else {
 3064:                 $r->print('<p><span class="LC_warning">'.&mt('Attachment not included - exceeded permitted length').'</span><br /></p>');
 3065:             }
 3066:         } elsif ($env{'form.multiforward'}) {
 3067:             if ($env{'form.attachmenturl'} ne '') {
 3068:                 $attachmenturl = $env{'form.attachmenturl'};
 3069:             }
 3070:         }
 3071:         my @recusers;
 3072:         my @recudoms;
 3073: 	foreach my $address (sort(keys(%toaddr))) {
 3074: 	    my ($recuname,$recdomain)=split(/\:/,$address);
 3075:             my $msgtxt = $savemsg;
 3076:             if ($toaddr{$address}) {
 3077: 	        $msgtxt.='<hr />'.$toaddr{$address};
 3078:             }
 3079: 	    my @thismsg;
 3080: 	    if ($msgtype eq 'critical') {
 3081: 		$r->print(&mt('Sending critical message').' '.
 3082:                               $recuname.':'.$recdomain.': ');
 3083: 		@thismsg=
 3084: 		    &Apache::lonmsg::user_crit_msg($recuname,$recdomain,
 3085: 						   $msgsubj,$msgtxt,
 3086: 						   $env{'form.sendbck'},
 3087: 						   $env{'form.permanent'},
 3088: 						   \$sentmessage{$address},
 3089:                                                    $nosentstore,$recipid);
 3090: 	    } else {
 3091: 		$r->print(&mt('Sending').' '.$recuname.':'.$recdomain.': ');
 3092: 		@thismsg=
 3093: 		    &Apache::lonmsg::user_normal_msg($recuname,$recdomain,
 3094: 						     $msgsubj,$msgtxt,
 3095: 						     $content{'citation'},
 3096: 						     undef,$attachmenturl,
 3097: 						     $env{'form.permanent'},
 3098: 						     \$sentmessage{$address},
 3099:                                                      undef,undef,undef,
 3100:                                                      $nosentstore,$recipid);
 3101:             }
 3102: 	    $msg_status{$recuname.':'.$recdomain}=join(' ',@thismsg);
 3103: 	    if ($msg_status{$recuname.':'.$recdomain} =~ /(ok|con_delayed)/) {  
 3104: 	        $numsent++;
 3105:                 push(@recusers,$recuname);
 3106:                 push(@recudoms,$recdomain);
 3107: 	    }
 3108: 	    $sendstatus.=' '.join(' ',@thismsg);
 3109: 	}
 3110:         my $subj_prefix;
 3111:         if ($numsent > 0) {
 3112:             if (($env{'request.course.id'}) && 
 3113:                 (($mode eq 'group') ||
 3114:                  ($env{'form.courserecord'}) ||
 3115:                  ($msgtype eq 'critical')) ||
 3116:                 ($env{'form.replyid'} && 
 3117:                  (($content{'courseid'} ne '') && 
 3118:                   ($mode eq 'group')))) {
 3119:                 if ($msgtype eq 'critical') {
 3120:                     $subj_prefix = 'Critical.';
 3121:                 } elsif ($mode eq 'group') {
 3122:                     $subj_prefix = 'Broadcast.';
 3123:                 } else {
 3124:                     $subj_prefix = 'Archive';
 3125:                 }
 3126:                 my ($specialmsgid,$specialresult);
 3127:                 my $course_str;
 3128:                 if ($env{'form.replyid'}) {
 3129:                     if ($content{'courseid'} ne '') {
 3130:                         my %crsdesc = 
 3131:                             &Apache::lonnet::coursedescription($content{'courseid'},
 3132:                                                                {'one_time' => 1});
 3133:                         $course_str = &escape('['.$crsdesc{'num'}.':'.$crsdesc{'domain'}.']');
 3134:                     }
 3135:                 } elsif ($env{'request.course.id'}) {
 3136:                     $course_str = &escape('['.$cnum.':'.$cdom.']');
 3137:                 }
 3138:                 $specialresult = 
 3139:                     &Apache::lonmsg::user_normal_msg_raw($cnum,$cdom,
 3140:                         $subj_prefix.' '.$course_str,$savemsg,undef,undef,
 3141:                         $attachmenturl,undef,undef,\$specialmsgid,undef,undef,undef,
 3142:                         undef,undef,1);
 3143:                 $specialmsgid = &unescape($specialmsgid);
 3144:                 if ($specialresult eq 'ok') {
 3145:                     my ($stamp,$crssubj,$msgname,$msgdom,$msgcount,$context,$pid) =
 3146: 		        split(/\:/,&unescape($specialmsgid));
 3147: 
 3148:                     foreach my $recipient (sort(keys(%toaddr))) {
 3149:                         if ($msg_status{$recipient} =~ /\s*(ok|con_delayed)\s*/) {
 3150:                             my $usersubj = $subj_prefix.'['.$recipient.']';
 3151:                             my $usermsgid = 
 3152: 			        &Apache::lonmsg::buildmsgid($stamp,$usersubj,
 3153: 							    $msgname,$msgdom,
 3154: 							    $msgcount,$context,
 3155: 							    $pid);
 3156:                             &Apache::lonmsg::user_normal_msg_raw($cnum,$cdom,
 3157:                                 $subj_prefix.' ['.$recipient.']',$msgsubj,
 3158:                                 undef,undef,$attachmenturl,undef,$usermsgid,undef,
 3159:                                 undef,$specialmsgid,undef,undef,undef,1);
 3160:                         }
 3161:                     }
 3162:                     if (($mode ne 'upload') && (@recusers > 0)) {
 3163:                         &Apache::lonmsg::process_sent_mail($msgsubj,
 3164:                            $subj_prefix,$numsent,$stamp,$msgname,$msgdom,
 3165:                            $msgcount,$context,$pid,$savemsg,\@recusers,
 3166:                            \@recudoms,undef,$attachmenturl,'','','','',$recipid);
 3167:                     }
 3168:                 } else {
 3169:                     &Apache::lonnet::logthis('Failed to create record of critical, broadcast or archived message in '.$env{'course.'.$env{'request.course.id'}.'.num'}.' '&mt('at').' '.$env{'course.'.$env{'request.course.id'}.'.domain'}.' - no msgid generated');
 3170:                 }
 3171:             } else {
 3172:                 my $stamp = time;
 3173:                 my $msgcount = &Apache::lonmsg::get_uniq();
 3174:                 my $context = &Apache::lonmsg::get_course_context();
 3175:                 &Apache::lonmsg::process_sent_mail($msgsubj,$subj_prefix,
 3176:                        $numsent,$stamp,$env{'user.name'},
 3177:                        $env{'user.domain'},$msgcount,$context,
 3178:                        $$,$savemsg,\@recusers,\@recudoms,undef,$attachmenturl,
 3179:                        '','','','',$recipid);
 3180:             }
 3181:         }
 3182:         if (!$env{'form.multiforward'}) { 
 3183:             if ($sendstatus=~/^(\s*(?:ok|con_delayed)\s*)*$/) {
 3184: 	        $r->print('<br /><span class="LC_success">'.&mt('Completed.').
 3185:                           '</span>');
 3186: 	        if ($env{'form.displayedcrit'}) {
 3187: 	            &discrit($r);
 3188:                 }
 3189:                 if ($group ne '') {
 3190:                     $r->print(&groupmail_sent($group,$cdom,$cnum)); 
 3191: 	        } else {
 3192: 	            &Apache::loncommunicate::menu($r);
 3193: 	        }
 3194:             } else {
 3195: 	        $r->print('<p>'.&Apache::lonhtmlcommon::confirm_success(&mt('Could not deliver message'),1).'<br /> '.
 3196: 		          &mt('Please use the browser "Back" button and correct the recipient addresses ([_1]).',$sendstatus).'</p>');
 3197:             }
 3198:         }
 3199:     }
 3200:     return $sendstatus;
 3201: }
 3202: 
 3203: # ===================================================================== Handler
 3204: 
 3205: sub handler {
 3206:     my $r=shift;
 3207: 
 3208: # ----------------------------------------------------------- Set document type
 3209:     
 3210:     &Apache::loncommon::content_type($r,'text/html');
 3211:     $r->send_http_header;
 3212:     
 3213:     return OK if $r->header_only;
 3214:     
 3215: # --------------------------- Get query string for limited number of parameters
 3216:     &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'},
 3217:         ['display','replyto','forward','markread','markdel','markunread',
 3218:          'sendreply','compose','sendmail','critical','recname','recdom',
 3219:          'recordftf','sortedby','block','folder','startdis','interdis',
 3220: 	 'showcommentbaseurl','dismode','group','subject','text','ref',
 3221:          'msgstatus']);
 3222:     $sqs='&sortedby='.$env{'form.sortedby'};
 3223: 
 3224: # ------------------------------------------------------ They checked for email
 3225:     unless ($env{'form.block'}) {
 3226:         &Apache::lonnet::put('email_status',{'recnewemail'=>0});
 3227:     }
 3228: 
 3229: # ----------------------------------------------------------------- Breadcrumbs
 3230: 
 3231:     &Apache::lonhtmlcommon::clear_breadcrumbs();
 3232:     &Apache::lonhtmlcommon::add_breadcrumb
 3233:         ({href=>"/adm/communicate",
 3234:           text=>"Communication/Messages",
 3235:           faq=>12,bug=>'Communication Tools',});
 3236: 
 3237: # ------------------------------------------------------------------ Get Folder
 3238: 
 3239:     my $folder=$env{'form.folder'};
 3240:     unless ($folder) { 
 3241: 	$folder=''; 
 3242:     } else {
 3243: 	$sqs.='&folder='.&escape($folder);
 3244:     }
 3245: # ------------------------------------------------------------ Get Display Mode
 3246: 
 3247:     my $dismode=$env{'form.dismode'};
 3248:     unless ($dismode) { 
 3249: 	$dismode=''; 
 3250:     } else {
 3251: 	$sqs.='&amp;dismode='.&escape($dismode);
 3252:     }
 3253: 
 3254: # --------------------------------------------------------------------- Display
 3255:     my $msgstatus = $env{'form.msgstatus'};
 3256:     $startdis=$env{'form.startdis'};
 3257:     if ($startdis ne '') {
 3258:         $startdis--;
 3259:     }
 3260:     unless ($startdis) { $startdis=0; }
 3261: 
 3262:     if ($env{'form.firstview'}) {
 3263: 	$startdis=0;
 3264:     }
 3265:     if ($env{'form.lastview'}) {
 3266: 	$startdis=-1;
 3267:     }
 3268:     if ($env{'form.prevview'}) {
 3269: 	$startdis--;
 3270:     }
 3271:     if ($env{'form.nextview'}) {
 3272: 	$startdis++;
 3273:     }
 3274:     my $postedstartdis=$startdis+1;
 3275:     $sqs.='&startdis='.$postedstartdis;
 3276: 
 3277: # --------------------------------------------------------------- Render Output
 3278: 
 3279:     if ($env{'form.display'}) {
 3280: 	&displaymessage($r,$env{'form.display'},$folder,$msgstatus);
 3281:     } elsif ($env{'form.replyto'}) {
 3282: 	&compout($r,'',$env{'form.replyto'},undef,undef,$folder,$dismode);
 3283:     } elsif ($env{'form.confirm'}) {
 3284: 	&printheader($r,'','Confirmed Receipt');
 3285: 	my $replying = 0;
 3286: 	foreach my $envkey (keys(%env)) {
 3287: 	    if ($envkey=~/^form\.(rep)?rec\_(.*)$/) {
 3288:                 my $repchk = $1;
 3289:        		my $msgid = $2;
 3290: 		$r->print('<b>'.&mt('Confirming Receipt').':</b> ');
 3291: 		my $result = &Apache::lonmsg::user_crit_received($msgid);
 3292:                 if ($result =~ /trans:\s+ok/) {
 3293:                     &statuschange($msgid,'read');
 3294:                 }
 3295:                 $r->print($result.'<br />');
 3296:                 if ($repchk eq 'rep') {
 3297: 		    &compout($r,'','','',$msgid);
 3298: 		    $replying = 1;
 3299:                 }
 3300: 	    }
 3301: 	}
 3302: 	if (!$replying) {
 3303: 	    &discrit($r);
 3304: 	}
 3305:     } elsif ($env{'form.critical'}) {
 3306: 	&printheader($r,'','Displaying Critical Messages');
 3307: 	&discrit($r);
 3308:     } elsif ($env{'form.forward'}) {
 3309: 	&compout($r,$env{'form.forward'},undef,undef,undef,$folder);
 3310:     } elsif ($env{'form.markdel'}) {
 3311: 	&printheader($r,'','Deleted Message');
 3312: 	my ($result,$msg) = 
 3313: 	    &statuschange($env{'form.markdel'},'deleted',$folder);
 3314: 	if (!$result) {
 3315: 	    $r->print('<p class="LC_error">'.
 3316: 		      &mt('Failed to delete the message.').'</p>'.
 3317: 		      '<p class="LC_error">'.$msg."</p>\n");
 3318: 	}
 3319: 	&Apache::loncommunicate::menu($r);
 3320: 	&disall($r,($folder?$folder:$dismode),$msgstatus);
 3321:     } elsif ($env{'form.markedaction'} eq 'markedforward') {
 3322:         my $total = 0;
 3323:         my @to_forward = &Apache::loncommon::get_env_multiple('form.delmark');
 3324:         foreach my $msgid (@to_forward) {
 3325:             &statuschange(&unescape($msgid),'forwarded',$folder);
 3326:             $total ++;
 3327:         }
 3328:         if ($total > 0) {
 3329:             &compout($r,undef,undef,undef,undef,$folder,$dismode,$total);
 3330:         }
 3331:     } elsif ($env{'form.markedaction'} eq 'markedread') {
 3332:         my $total = 0;
 3333:         my @to_markread = &Apache::loncommon::get_env_multiple('form.delmark');
 3334:         foreach my $msgid (@to_markread) {
 3335:             &statuschange(&unescape($msgid),'read',$folder);
 3336:             $total ++;
 3337:         }
 3338:         &printheader($r,'','Marked Messages Read');
 3339:         $r->print(&mt('Marked [_1] message(s) read',$total).'<p>');
 3340:         &Apache::loncommunicate::menu($r);
 3341:         &disall($r,($folder?$folder:$dismode),$msgstatus);
 3342:     } elsif ($env{'form.markedaction'} eq 'markedunread') {
 3343:         my $total = 0;
 3344:         my @to_markunread = &Apache::loncommon::get_env_multiple('form.delmark');
 3345:         foreach my $msgid (@to_markunread) {
 3346:             &statuschange(&unescape($msgid),'new',$folder);
 3347:             $total ++;
 3348:         }
 3349:         &printheader($r,'','Marked Messages Unread');
 3350:         $r->print(&mt('Marked [_1] message(s) unread',$total).'<p>');
 3351:         &Apache::loncommunicate::menu($r);
 3352:         &disall($r,($folder?$folder:$dismode),$msgstatus);
 3353:     } elsif ($env{'form.markedaction'} eq 'markedmove') {
 3354:         my $destfolder = $env{'form.movetofolder'};
 3355:         my %gotfolders = &Apache::lonmsg::get_user_folders();
 3356:         &printheader($r,'','Moved Messages');
 3357:         if (!defined($gotfolders{$destfolder})) {
 3358:             $r->print(&mt('Destination folder [_1] is not a valid folder',
 3359:                       $destfolder));
 3360:         } else {
 3361: 	    my ($total,$failed,@failed_msg)=(0,0);
 3362:             my @to_move = &Apache::loncommon::get_env_multiple('form.delmark');
 3363:             foreach my $msgid (@to_move) {
 3364: 	        my ($result,$msg) = &movemsg(&unescape($msgid),$folder,
 3365: 			                     $env{'form.movetofolder'});
 3366: 	        if ($result) {
 3367: 		    $total++;
 3368: 	        } else {
 3369: 		    $failed++;
 3370: 		    push(@failed_msg,$msg);
 3371: 	        }
 3372: 	    }
 3373: 	    if ($failed) {
 3374: 	        $r->print('<p class="LC_error">
 3375:                           '.&mt('Failed to move [_1] message(s)',$failed).
 3376: 		      '</p>');
 3377: 	        $r->print('<p class="LC_error">'.
 3378: 	   	          join("</p>\n<p class=\"LC_error\">",@failed_msg).
 3379: 		          "</p>\n");
 3380: 	    }
 3381: 	    $r->print(&mt('Moved [_1] message(s)',$total).'<p>');
 3382:         }
 3383: 	&Apache::loncommunicate::menu($r);
 3384: 	&disall($r,($folder?$folder:$dismode),$msgstatus);
 3385:     } elsif ($env{'form.markedaction'} eq 'markeddel') {
 3386: 	my ($total,$failed,@failed_msg)=(0,0);
 3387:         my @to_delete = &Apache::loncommon::get_env_multiple('form.delmark');
 3388:         foreach my $msgid (@to_delete) {
 3389: 	    my ($result,$msg) = &statuschange(&unescape($msgid),'deleted', 
 3390: 				              $folder);
 3391: 	    if ($result) {
 3392: 	        $total++;
 3393: 	    } else {
 3394: 	        $failed++;
 3395: 		push(@failed_msg,$msg);
 3396: 	    }
 3397: 	}
 3398: 	&printheader($r,'','Deleted Messages');
 3399: 	if ($failed) {
 3400: 	    $r->print('<p class="LC_error">
 3401:                           '.&mt('Failed to delete [_1] message(s)',$failed).
 3402: 		      '</p>');
 3403: 	    $r->print('<p class="LC_error">'.
 3404: 		      join("</p>\n<p class=\"LC_error\">",@failed_msg).
 3405: 		      "</p>\n");
 3406: 	}
 3407: 	$r->print(&mt('Deleted [_1] message(s)',$total).'<p>');
 3408: 	&Apache::loncommunicate::menu($r);
 3409: 	&disall($r,($folder?$folder:$dismode),$msgstatus);
 3410:     } elsif ($env{'form.markunread'}) {
 3411: 	&printheader($r,'','Marked Message as Unread');
 3412: 	&statuschange($env{'form.markunread'},'new');
 3413: 	&Apache::loncommunicate::menu($r);
 3414: 	&disall($r,($folder?$folder:$dismode),$msgstatus);
 3415:     } elsif ($env{'form.compose'}) {
 3416: 	&compout($r,'','',$env{'form.compose'});
 3417:     } elsif ($env{'form.recordftf'}) {
 3418: 	&facetoface($r,$env{'form.recordftf'});
 3419:     } elsif ($env{'form.block'}) {
 3420:         &examblock($r,$env{'form.block'});
 3421:     } elsif ($env{'form.sendmail'}) {
 3422:         if ($env{'form.multiforward'}) {
 3423:             &printheader($r,'','Messages being sent.');
 3424:             my $fixed_subj = $env{'form.subject'};
 3425:             my $suffix=&Apache::lonmsg::foldersuffix($folder);
 3426:             my (%sendresult,%forwardok,%forwardfail,$fwdcount);
 3427:             my @to_forward = &Apache::loncommon::get_env_multiple('form.delmark');
 3428:             foreach my $item (@to_forward) {
 3429:                 my $msgid=&unescape($item);
 3430:                 my %message=&Apache::lonnet::get('nohist_email'.$suffix,[$msgid]);
 3431:                 my %content=&Apache::lonmsg::unpackagemsg($message{$msgid},1,1);
 3432:                 if ($env{'form.showorigsubj'}) {
 3433:                     $env{'form.subject'} = $fixed_subj.$content{'subject'};
 3434:                 } else {
 3435:                     $env{'form.subject'} = '';
 3436:                 }
 3437:                 my $uname = $content{'sendername'};
 3438:                 my $udom = $content{'senderdomain'};
 3439:                 &statuschange($msgid,'forwarded',$folder);
 3440:                 if ($env{'form.showorigsender'}) {
 3441:                     $env{'form.message'} = $env{'form.msgheader'}.' '.
 3442:                         &Apache::loncommon::plainname($uname,$udom).' ('.
 3443:                                            $uname.':'.$udom.')';
 3444:                 }
 3445:                 $env{'form.message'}.="\n\n-- Forwarded message --\n\n".
 3446:                                       $content{'message'};
 3447:                 $env{'form.attachmenturl'} = $content{'attachmenturl'};
 3448:                 $env{'form.multiforwid'} = $item;
 3449:                 $fwdcount ++;
 3450:                 $r->print($fwdcount.': '); 
 3451:                 $sendresult{$msgid} = &sendoffmail($r,$folder);
 3452:                 $r->print('<br />');
 3453:             }
 3454:             foreach my $key (keys(%sendresult)) {
 3455:                 if ($sendresult{$key} =~/^(\s*(?:ok|con_delayed)\s*)*$/) {
 3456:                     $forwardok{$key} = $sendresult{$key};
 3457:                 } else {
 3458:                     $forwardfail{$key} = $sendresult{$key}; 
 3459:                 }
 3460:             }
 3461:             if (keys(%forwardok) > 0) {
 3462:                 my $count = keys(%forwardok);
 3463:                 $r->print('<br /><span class="LC_success">'.
 3464:                           &mt('[quant,_1,message] forwarded.',$count).
 3465:                           '</span>');
 3466:             }
 3467:             if (keys(%forwardfail) > 0) {
 3468:                 my $count = keys(%forwardfail);
 3469:                 $r->print('<p><span class="LC_error">'.
 3470:                           &mt('Could not forward [quant,_1,message].',$count).
 3471:                           '</span> ');
 3472:                 foreach my $key (keys(%forwardfail)) {
 3473:                     $r->print(&mt('Could not deliver forwarded message.').'</span> '.
 3474:                               &mt('The recipient addresses may need to be corrected').' ('.$forwardfail{$key}.').<br /><br />');
 3475:                 }
 3476:             }
 3477:             &Apache::loncommunicate::menu($r);
 3478:         } else {
 3479: 	    &sendoffmail($r,$folder);
 3480:         }
 3481: 	if ($env{'form.storebasecomment'}) {
 3482: 	    &storecomment($r);
 3483:         }
 3484: 	if (($env{'form.rsspost'}) && ($env{'request.course.id'})) {
 3485: 	        &Apache::lonrss::addentry($env{'course.'.$env{'request.course.id'}.'.num'},
 3486: 				      $env{'course.'.$env{'request.course.id'}.'.domain'},
 3487: 				      'Course_Announcements',
 3488: 				      $env{'form.subject'},
 3489: 				      $env{'form.message'},'/adm/communicate','public');
 3490: 	}
 3491: 	if ((!exists($env{'form.group'})) && (!$env{'form.displayedcrit'})) {
 3492: 	    &disall($r,($folder?$folder:$dismode),$msgstatus);
 3493: 	}
 3494:     } elsif ($env{'form.newfolder'}) {
 3495: 	&printheader($r,'','New Folder');
 3496:         my $showfolder = $env{'form.newfolder'};
 3497: 	my ($makeresult,$warning) = &makefolder($env{'form.newfolder'});
 3498:         if ($makeresult eq 'ok') {
 3499:             $r->print(&mt('Folder "[_1]" created.',$showfolder).'<br />');
 3500:         } else {
 3501:             $r->print(&mt('Creation failed.').' '.$makeresult.'<br />'.
 3502:                       $warning);
 3503:             $showfolder = $folder;
 3504:         }
 3505:         &Apache::loncommunicate::menu($r);
 3506: 	&disall($r,$showfolder,$msgstatus);
 3507:     } elsif ($env{'form.showcommentbaseurl'}) {
 3508: 	&storedcommentlisting($r);
 3509:     } elsif ($env{'form.folderaction'} eq 'delete') {
 3510:         &printheader($r,'','Deleted Folder');
 3511:         my $showfolder = '';
 3512:         my $delresult = &deletefolder($folder);
 3513:         if ($delresult eq 'ok') {
 3514:             $r->print(&mt('Folder "[_1]" deleted.',$folder).'<br />');
 3515:             $env{'form.folder'} = '';
 3516:         } else {
 3517:             $r->print(&mt('Deletion failed.').' '.$delresult.'<br />');
 3518:             $showfolder = $folder;
 3519:         }
 3520:         &Apache::loncommunicate::menu($r);
 3521:         &disall($r,$showfolder,$msgstatus);
 3522:     } elsif ($env{'form.folderaction'} eq 'rename') {
 3523:         &printheader($r,'','Renamed Folder');
 3524:         my $showfolder = $env{'form.renamed'};
 3525:         my $renresult = &renamefolder($folder);
 3526:         if ($renresult eq 'ok') {
 3527:             $r->print(&mt('Folder "[_1]" renamed to "[_2]".',$folder,$showfolder).'<br />');
 3528:         } else {
 3529:             $r->print(&mt('Renaming failed.').' '.$renresult.'<br />');
 3530:             $showfolder = $folder;
 3531:         }
 3532:         &Apache::loncommunicate::menu($r);
 3533:         &disall($r,$showfolder,$msgstatus);
 3534:     } else {
 3535: 	&printheader($r,'','Display All Messages');
 3536: 	&Apache::loncommunicate::menu($r);
 3537: 	&disall($r,($folder?$folder:$dismode),$msgstatus);
 3538:     }
 3539:     $r->print(&Apache::loncommon::end_page());
 3540:     return OK;
 3541: }
 3542: # ================================================= Main program, reset counter
 3543: 
 3544: =pod
 3545: 
 3546: =cut
 3547: 
 3548: 1; 
 3549: 
 3550: __END__
 3551: 

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