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

1.1       albertel    1: # The LearningOnline Network with CAPA
                      2: # Routines for messaging display
                      3: #
1.55    ! albertel    4: # $Id: lonmsgdisplay.pm,v 1.54 2006/12/18 23:09:37 albertel Exp $
1.1       albertel    5: #
                      6: # Copyright Michigan State University Board of Trustees
                      7: #
                      8: # This file is part of the LearningOnline Network with CAPA (LON-CAPA).
                      9: #
                     10: # LON-CAPA is free software; you can redistribute it and/or modify
                     11: # it under the terms of the GNU General Public License as published by
                     12: # the Free Software Foundation; either version 2 of the License, or
                     13: # (at your option) any later version.
                     14: #
                     15: # LON-CAPA is distributed in the hope that it will be useful,
                     16: # but WITHOUT ANY WARRANTY; without even the implied warranty of
                     17: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
                     18: # GNU General Public License for more details.
                     19: #
                     20: # You should have received a copy of the GNU General Public License
                     21: # along with LON-CAPA; if not, write to the Free Software
                     22: # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
                     23: #
                     24: # /home/httpd/html/adm/gpl.txt
                     25: #
                     26: # http://www.lon-capa.org/
                     27: #
                     28: 
                     29: 
                     30: package Apache::lonmsgdisplay;
                     31: 
                     32: =pod
                     33: 
                     34: =head1 NAME
                     35: 
                     36: Apache::lonmsg: supports internal messaging
                     37: 
                     38: =head1 SYNOPSIS
                     39: 
                     40: lonmsg provides routines for sending messages, receiving messages, and
                     41: a handler to allow users to read, send, and delete messages.
                     42: 
                     43: =head1 OVERVIEW
                     44: 
                     45: =head2 Messaging Overview
                     46: 
                     47: X<messages>LON-CAPA provides an internal messaging system similar to
                     48: email, but customized for LON-CAPA's usage. LON-CAPA implements its
                     49: own messaging system, rather then building on top of email, because of
                     50: the features LON-CAPA messages can offer that conventional e-mail can
                     51: not:
                     52: 
                     53: =over 4
                     54: 
                     55: =item * B<Critical messages>: A message the recipient B<must>
                     56: acknowlegde receipt of before they are allowed to continue using the
                     57: system, preventing a user from claiming they never got a message
                     58: 
                     59: =item * B<Receipts>: LON-CAPA can reliably send reciepts informing the
                     60: sender that it has been read; again, useful for preventing students
                     61: from claiming they did not see a message. (While conventional e-mail
                     62: has some reciept support, it's sporadic, e-mail client-specific, and
                     63: generally the receiver can opt to not send one, making it useless in
                     64: this case.)
                     65: 
                     66: =item * B<Context>: LON-CAPA knows about the sender, such as where
                     67: they are in a course. When a student mails an instructor asking for
                     68: help on the problem, the instructor receives not just the student's
                     69: question, but all submissions the student has made up to that point,
                     70: the user's rendering of the problem, and the complete view the student
                     71: saw of the resource, including discussion up to that point. Finally,
                     72: the instructor is reading all of this inside of LON-CAPA, not their
                     73: email program, so they have full access to LON-CAPA's grading
                     74: interface, or other features they may wish to use in response to the
                     75: student's query.
                     76: 
1.50      raeburn    77: =item * B<Blocking>: LON-CAPA can block selected communication 
                     78: features for students during an online exam. A course coordinator or
1.1       albertel   79: instructor can set an open and close date/time for scheduled online
                     80: exams in a course. If a user uses the LON-CAPA internal messaging 
                     81: system to display e-mails during the scheduled blocking event,  
                     82: display of all e-mail sent during the blocking period will be 
                     83: suppressed, and a message of explanation, including details of the 
                     84: currently active blocking periods will be displayed instead. A user 
                     85: who has a course coordinator or instructor role in a course will be
                     86: unaffected by any blocking periods for the course, unless the user
                     87: also has a student role in the course, AND has selected the student role.
                     88: 
                     89: =back
                     90: 
                     91: Users can ask LON-CAPA to forward messages to conventional e-mail
                     92: addresses on their B<PREF> screen, but generally, LON-CAPA messages
                     93: are much more useful than traditional email can be made to be, even
                     94: with HTML support.
                     95: 
                     96: Right now, this document will cover just how to send a message, since
                     97: it is likely you will not need to programmatically read messages,
                     98: since lonmsg already implements that functionality.
                     99: 
                    100: The routines used to package messages and unpackage messages are not
                    101: only used by lonmsg when creating/extracting messages for LON-CAPA's
                    102: internal messaging system, but also by lonnotify.pm which is available
                    103: for use by Domain Coordinators to broadcast standard e-mail to specified
                    104: users in their domain.  The XML packaging used in the two cases is very
                    105: similar.  The differences are the use of <recuser>$uname</recuser> and 
                    106: <recdomain>$udom</recdomain> in stored internal messages, compared 
                    107: with <recipient username="$uname:$udom">$email</recipient> in stored
                    108: Domain Coordinator e-mail for the storage of information about 
                    109: recipients of the message/e-mail.
                    110: 
                    111: =head1 FUNCTIONS
                    112: 
                    113: =over 4
                    114: 
                    115: =cut
                    116: 
                    117: use strict;
                    118: use Apache::lonnet;
                    119: use HTML::TokeParser();
                    120: use Apache::Constants qw(:common);
                    121: use Apache::loncommon();
                    122: use Apache::lontexconvert();
                    123: use HTML::Entities();
                    124: use Apache::lonlocal;
                    125: use Apache::loncommunicate;
                    126: use Apache::lonfeedback;
                    127: use Apache::lonrss();
1.27      albertel  128: use Apache::lonselstudent();
1.29      www       129: use lib '/home/httpd/lib/perl/';
                    130: use LONCAPA;
1.1       albertel  131: 
                    132: # Querystring component with sorting type
                    133: my $sqs;
                    134: my $startdis;
                    135: my $interdis;
                    136: 
                    137: # ============================================================ List all folders
                    138: 
                    139: sub folderlist {
1.53      raeburn   140:     my ($folder,$msgstatus) = @_;
1.46      raeburn   141:     my %lt = &Apache::lonlocal::texthash(
                    142:                 actn => 'Action',
                    143:                 fold => 'Folder',
                    144:                 show => 'Show',
1.53      raeburn   145:                 status => 'Message Status',
1.46      raeburn   146:                 go   => 'Go',
1.50      raeburn   147:                 nnff => 'New Name for Folder',
                    148:                 newn => 'New Name',
                    149:                 thfm => 'The folder may not be renamed',
                    150:                 fmnb => 'folder may not be renamed as it is a folder provided by the system.',
                    151:                 asth => 'as this name is already in use for a system-provided or user-defined folder.',
                    152:                 the => 'The',
                    153:                 tnfm => 'The new folder may not be named',
                    154: 
1.46      raeburn   155:     );
                    156: 
                    157:     my %actions = &Apache::lonlocal::texthash(
                    158:                                 view => 'View Folder',
                    159:                                 rename => 'Rename Folder',
                    160:                                 delete => 'Delete Folder',
                    161:     );
                    162:     $actions{'select_form_order'} = ['view','rename','delete'];
                    163: 
1.53      raeburn   164:     my %statushash = &get_msgstatus_types();
                    165: 
                    166:     $statushash{'select_form_order'} = ['','new','read','replied','forwarded'];
                    167: 
1.46      raeburn   168:     my %permfolders = &get_permanent_folders();
                    169:     my $permlist = join("','",sort(keys(%permfolders)));
                    170:     my ($permlistkeys,$permlistvals);
                    171:     foreach my $key (sort(keys(%permfolders))) {
                    172:         $permlistvals .= $permfolders{$key}."','";
                    173:         $permlistkeys .= $key."','";
                    174:     }
                    175:     $permlistvals =~ s/','$//;
                    176:     $permlistkeys =~ s/','$//;
                    177:     my %gotfolders = &Apache::lonmsg::get_user_folders();
                    178:     my %userfolders;
                    179: 
                    180:     foreach my $key (keys(%gotfolders)) {
1.54      albertel  181:         $key =~ s/(['"])/\$1/g; #' stupid emacs
1.46      raeburn   182:         $userfolders{$key} = $key;
                    183:     }
                    184:     my @userorder = sort(keys(%userfolders));
                    185:     my %formhash = (%permfolders,%userfolders);
                    186:     my $folderlist = join("','",@userorder);
                    187:     $folderlist .= "','".$permlistvals;
                    188: 
1.53      raeburn   189:     $formhash{'select_form_order'} = ['','critical',@userorder,'sent','trash'];
1.46      raeburn   190:     my $output = qq|<script type="text/javascript">
                    191: function folder_choice(targetform,caller) {
                    192:     var permfolders_keys = new Array('$permlistkeys');
                    193:     var permfolders_vals = new Array('$permlistvals');
                    194:     var allfolders = new Array('$folderlist');
                    195:     if (caller == 'change') {
                    196:         if (targetform.folderaction.options[targetform.folderaction.selectedIndex].value == 'rename') {
                    197:             for (var i=0; i<permfolders_keys.length; i++) {
                    198:                 if (permfolders_keys[i] == targetform.folder.value) {
1.50      raeburn   199:                     alert("$lt{'the'} '"+permfolders_vals[i]+"' $lt{'fmnb'}");
1.46      raeburn   200:                     return;
                    201:                 }
                    202:             }
1.50      raeburn   203:             var foldername=prompt('$lt{'nnff'}','$lt{'newn'}');
1.46      raeburn   204:             if (foldername) {
                    205:                 targetform.renamed.value=foldername;
                    206:                 for (var i=0; i<allfolders.length; i++) {
                    207:                     if (allfolders[i] == foldername) {
1.50      raeburn   208:                         alert("$lt{'thfm'} '"+foldername+"' $lt{'asth'}");
1.46      raeburn   209:                         return;
                    210:                     }
                    211:                 }
                    212:                 targetform.submit();
                    213:             }
                    214:         }
                    215:         else {
                    216:             targetform.submit();
                    217:         }
                    218:     }
                    219:     if (caller == 'new') {
                    220:         var newname=targetform.newfolder.value;
                    221:         if (newname) {
                    222:             for (var i=0; i<allfolders.length; i++) {
                    223:                 if (allfolders[i] == newname) {
1.50      raeburn   224:                     alert("$lt{'tnfm'} '"+newname+"' $lt{'asth'}");
1.46      raeburn   225:                     return;
                    226:                 }
                    227:             }
                    228:             targetform.submit();
                    229:         }
                    230:     }
                    231: }
                    232: </script>|;
                    233:     $output .= '
                    234: <form method="post" action="/adm/email" name="folderlist">
                    235: <table border="0" cellspacing="2" cellpadding="2">
                    236:  <tr>
                    237:   <td align="left">
                    238:    <table border="0" cellspacing="2" cellpadding="2">
                    239:     <tr>
                    240:      <td align="center"><b>'.$lt{'fold'}.'</b><br />'."\n".
1.47      albertel  241:          &Apache::loncommon::select_form($folder,'folder',%formhash).'
1.46      raeburn   242:      </td>
                    243:      <td align="center"><b>'.$lt{'show'}.'</b><br />
                    244:       <select name="interdis">'.
1.47      albertel  245:           join("\n",map { '<option value="'.$_.'"'.
                    246: 			      ($_==$interdis?' selected="selected"':'').'>'.$_.'</option>' 
                    247: 			}
                    248: 	       (10,20,50,100,200)).'</select>
1.46      raeburn   249:      </td>
1.53      raeburn   250:      <td align="center"><b>'.$lt{'status'}.'</b><br />'."\n".
                    251:        &Apache::loncommon::select_form($msgstatus,'msgstatus',%statushash).'
                    252:      </td>
1.46      raeburn   253:      <td align="center"><b>'.$lt{'actn'}.'</b><br />'.
1.47      albertel  254:          &Apache::loncommon::select_form('view','folderaction',%actions).'
1.46      raeburn   255:      </td><td><br />'.
                    256:     '<input type="button" value="'.$lt{'go'}.'" onClick="javascript:folder_choice(this.form,'."'change'".');" />
                    257:      </td>
                    258:     </tr>
                    259:    </table>
                    260:   </td><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td>
                    261:   <td align="right">
                    262:    <table><tr><td><br />
                    263:     <input type="button" value="'.&mt('Make New Folder').
                    264:     '" onClick="javascript:folder_choice(this.form,'."'new'".');" /></td>'.
1.48      albertel  265:     '<td align="center"><b>'.&mt('New Folder').'</b><br />'.
1.46      raeburn   266:     '<input type="text" size="15" name="newfolder" value="" />
                    267:     </td></tr></table>
                    268:   </td>
                    269:  </tr>
                    270: </table>'."\n".
1.1       albertel  271:     '<input type="hidden" name="sortedby" value="'.$env{'form.sortedby'}.'" />'.
1.46      raeburn   272:     '<input type="hidden" name="renamed" value="" />'.
1.53      raeburn   273:         ($folder=~/^critical/?'</form>':'');
1.46      raeburn   274:     return $output;
                    275: }
                    276: 
                    277: sub get_permanent_folders {
1.47      albertel  278:     my %permfolders = 
                    279: 	&Apache::lonlocal::texthash(''         => 'INBOX',
                    280: 				    'trash'    => 'TRASH',
                    281: 				    'critical' => 'Critical',
                    282: 				    'sent'     => 'Sent Messages',
                    283: 				    );
1.46      raeburn   284:     return %permfolders;
1.1       albertel  285: }
                    286: 
1.53      raeburn   287: sub get_msgstatus_types {
                    288:     my %statushash = &Apache::lonlocal::texthash(
                    289:                                 '' => 'Any',
                    290:                                 new => 'Unread',
                    291:                                 read => 'Read',
                    292:                                 replied => 'Replied to',
                    293:                                 forwarded => 'Forwarded',
                    294:     );
                    295:     return %statushash;
                    296: }
                    297: 
1.1       albertel  298: sub scrollbuttons {
1.53      raeburn   299:     my ($start,$maxdis,$first,$finish,$total,$msgstatus)=@_;
1.1       albertel  300:     unless ($total>0) { return ''; }
                    301:     $start++; $maxdis++;$first++;$finish++;
1.53      raeburn   302: 
                    303:     my %statushash = &get_msgstatus_types();
                    304:     my $status;
                    305:     if ($msgstatus eq '') {
                    306:         $status = &mt('All');
                    307:     } else {
                    308:         $status = $statushash{$msgstatus};
                    309:     }
1.1       albertel  310:     return
1.53      raeburn   311:    '<b>'.&mt('Page').'</b>: '. 
1.1       albertel  312:    '<input type="submit" name="firstview" value="'.&mt('First').'" />'.
                    313:    '<input type="submit" name="prevview" value="'.&mt('Previous').'" />'.
                    314:    '<input type="text" size="5" name="startdis" value="'.$start.'" onChange="this.form.submit()" /> of '.$maxdis.
                    315:    '<input type="submit" name="nextview" value="'.&mt('Next').'" />'.
                    316:    '<input type="submit" name="lastview" value="'.&mt('Last').'" /><br />'.
1.53      raeburn   317:    &mt('<b>[_1] messages</b>: showing messages [_2] through [_3] of [_4].',$status,$first,$finish,$total).'</form>';
1.1       albertel  318: }
                    319: # =============================================================== Status Change
                    320: 
                    321: sub statuschange {
                    322:     my ($msgid,$newstatus,$folder)=@_;
1.3       albertel  323:     my $suffix=&Apache::lonmsg::foldersuffix($folder);
1.1       albertel  324:     my %status=&Apache::lonnet::get('email_status'.$suffix,[$msgid]);
                    325:     if ($status{$msgid}=~/^error\:/) { $status{$msgid}=''; }
                    326:     unless ($status{$msgid}) { $status{$msgid}='new'; }
                    327:     unless (($status{$msgid} eq 'replied') || 
                    328:             ($status{$msgid} eq 'forwarded')) {
                    329: 	&Apache::lonnet::put('email_status'.$suffix,{$msgid => $newstatus});
                    330:     }
                    331:     if (($newstatus eq 'deleted') || ($newstatus eq 'new')) {
                    332: 	&Apache::lonnet::put('email_status'.$suffix,{$msgid => $newstatus});
                    333:     }
                    334:     if ($newstatus eq 'deleted') {
1.9       albertel  335: 	return &movemsg($msgid,$folder,'trash');
                    336:     }
                    337:     return ;
1.1       albertel  338: }
                    339: 
                    340: # ============================================================= Make new folder
                    341: 
                    342: sub makefolder {
1.46      raeburn   343:     my ($newfolder) = @_;
                    344:     my %permfolders = &get_permanent_folders();
                    345:     my %userfolders = &Apache::lonmsg::get_user_folders();
                    346:     my ($outcome,$warning);
                    347:     if (defined($userfolders{$newfolder})) {
                    348:         return &mt('The folder name: "[_1]" is already in use for an existing folder.',$newfolder);
                    349:     }
                    350:     foreach my $perm (keys(%permfolders)) {
                    351:         if ($permfolders{$perm} eq $newfolder) {
                    352:             return &mt('The folder name: "[_1]" is already used for one of the folders automatically generated by the system.',$newfolder);
                    353:         }
                    354:     } 
                    355:     if (&get_msgfolder_lock() eq 'ok') {
                    356:         my %counter_hash = &Apache::lonnet::get('email_folders',["\0".'idcount']);
                    357:         my $lastcount = $counter_hash{"\0".'idcount'};
                    358:         my $folder_id = $lastcount + 1;
                    359:         while (defined($userfolders{$folder_id})) {
                    360:             $folder_id ++;
                    361:         }
1.47      albertel  362:         my %folderinfo = ( id      => $folder_id,
                    363:                            created => time, );
1.46      raeburn   364:         $outcome =  
1.47      albertel  365: 	    &Apache::lonnet::put('email_folders',{$newfolder => \%folderinfo,
                    366: 						  "\0".'idcount' => $folder_id});
1.46      raeburn   367:         my $releaseresult = &release_msgfolder_lock();
                    368:         if ($releaseresult ne 'ok') {
                    369:             $warning = $releaseresult;
                    370:         }
                    371:     } else {
1.47      albertel  372:         $outcome = 
                    373: 	    &mt('Error - could not obtain lock on email folders record.');
1.46      raeburn   374:     }
                    375:     return ($outcome,$warning);
1.1       albertel  376: }
                    377: 
1.46      raeburn   378: # ============================================================= Delete folder
                    379: 
                    380: sub deletefolder {
                    381:     my ($folder)=@_;
                    382:     my %permfolders = &get_permanent_folders();
                    383:     if (defined($permfolders{$folder})) {
                    384:         return &mt('The folder [_1] may not be deleted',$folder); 
                    385:     }
                    386:     my %userfolders = &Apache::lonmsg::get_user_folders();
                    387:     if (!defined($userfolders{$folder})) {
                    388:         return &mt('The folder [_1] does not exist so deletion is not required.',
                    389:                    $folder);
                    390:     }
                    391:     # check folder is empty;
                    392:     my $suffix=&Apache::lonmsg::foldersuffix($folder);
                    393:     my @messages = &Apache::lonnet::getkeys('nohist_email'.$suffix);
                    394:     if (@messages > 0) {
                    395:         return &mt('The folder [_1] contains messages so it may not be deleted.').
                    396:                '<br />'.
                    397:                &mt('Delete or move the messages to a different folder first.');
                    398:     }
                    399:     my $delresult = &Apache::lonnet::del('email_folders',[$folder]);
                    400:     return $delresult;
                    401: }
                    402: 
                    403: sub renamefolder {
                    404:     my ($folder) = @_;
                    405:     my $newname = $env{'form.renamed'};
                    406:     my %permfolders = &get_permanent_folders();
                    407:     if ($env{'form.renamed'} eq '') {
                    408:         return &mt('The folder "[_1]" may not be renamed to "[_2]" as the new name you requested is an invalid name.',$folder,$newname);
                    409:     }
                    410:     if (defined($permfolders{$newname})) {
                    411:         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);
                    412:     }
                    413:     my %userfolders = &Apache::lonmsg::get_user_folders();
                    414:     if (defined($userfolders{$newname})) {
                    415:         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);
                    416:     }
                    417:     if (!defined($userfolders{$folder})) {
                    418:         return &mt('The folder "[_1]" could not be renamed to "[_2]" because the folder does not exist.',$folder,$newname);
                    419:     }
                    420:     my %folderinfo;
                    421:     if (ref($userfolders{$folder}) eq 'HASH') {
                    422:         %folderinfo = %{$userfolders{$folder}};
                    423:     } else {
1.47      albertel  424:         %folderinfo = ( id      => $folder,
                    425:                         created => $userfolders{$folder},);
1.46      raeburn   426:     }
                    427:     my $outcome =
                    428:      &Apache::lonnet::put('email_folders',{$newname => \%folderinfo});
                    429:     if ($outcome eq 'ok') {
                    430:         $outcome = &Apache::lonnet::del('email_folders',[$folder]);
                    431:     }
                    432:     return $outcome;
                    433: }
                    434: 
                    435: sub get_msgfolder_lock {
                    436:     # get lock for mail folder counter.
1.47      albertel  437:     my $lockhash = { "\0".'lock_counter' => time, };
1.46      raeburn   438:     my $tries = 0;
                    439:     my $gotlock = &Apache::lonnet::newput('email_folders',$lockhash);
                    440:     while (($gotlock ne 'ok') && $tries <3) {
                    441:         $tries ++;
1.47      albertel  442:         sleep(1);
1.46      raeburn   443:         $gotlock = &Apache::lonnet::newput('email_folders',$lockhash);
                    444:     }
                    445:     return $gotlock;
                    446: }
                    447: 
                    448: sub release_msgfolder_lock {
                    449:     #  remove lock
                    450:     my @del_lock = ("\0".'lock_counter');
                    451:     my $dellockoutcome=&Apache::lonnet::del('email_folders',\@del_lock);
                    452:     if ($dellockoutcome ne 'ok') {
                    453:         return ('<br />'.&mt('Warning: failed to release lock for counter').'<br />');
                    454:     } else {
                    455:         return 'ok';
                    456:     }
                    457: }
                    458: 
                    459: 
1.1       albertel  460: # ======================================================== Move between folders
                    461: 
                    462: sub movemsg {
                    463:     my ($msgid,$srcfolder,$trgfolder)=@_;
                    464:     if ($srcfolder eq 'new') { $srcfolder=''; }
1.3       albertel  465:     my $srcsuffix=&Apache::lonmsg::foldersuffix($srcfolder);
                    466:     my $trgsuffix=&Apache::lonmsg::foldersuffix($trgfolder);
1.9       albertel  467:     if ($srcsuffix eq $trgsuffix) {
                    468: 	return (0,&mt('Message not moved, Attempted to move message to the same folder as it already is in.'));
                    469:     }
1.1       albertel  470: 
                    471: # Copy message
                    472:     my %message=&Apache::lonnet::get('nohist_email'.$srcsuffix,[$msgid]);
1.9       albertel  473:     if (!exists($message{$msgid}) || $message{$msgid} eq '') {
1.32      albertel  474: 	if (&Apache::lonnet::error(%message)) {
1.9       albertel  475: 	    return (0,&mt('Message not moved, A network error occurred.'));
                    476: 	} else {
                    477: 	    return (0,&mt('Message not moved as the message is no longer in the source folder.'));
                    478: 	}
                    479:     }
                    480: 
                    481:     my $result =&Apache::lonnet::put('nohist_email'.$trgsuffix,
                    482: 				     {$msgid => $message{$msgid}});
1.32      albertel  483:     if (&Apache::lonnet::error($result)) {
1.9       albertel  484: 	return (0,&mt('Message not moved, A network error occurred.'));
                    485:     }
1.1       albertel  486: 
                    487: # Copy status
                    488:     unless ($trgfolder eq 'trash') {
1.9       albertel  489:        	my %status=&Apache::lonnet::get('email_status'.$srcsuffix,[$msgid]);
                    490: 	# a non-existant status is the mark of an unread msg
1.32      albertel  491: 	if (&Apache::lonnet::error(%status)) {
1.9       albertel  492: 	    return (0,&mt('Message copied to new folder but status was not, A network error occurred.'));
                    493: 	}
                    494: 	my $result=&Apache::lonnet::put('email_status'.$trgsuffix,
                    495: 					{$msgid => $status{$msgid}});
1.32      albertel  496: 	if (&Apache::lonnet::error($result)) {
1.9       albertel  497: 	    return (0,&mt('Message copied to new folder but status was not, A network error occurred.'));
                    498: 	}
1.1       albertel  499:     }
1.9       albertel  500: 
1.1       albertel  501: # Delete orginals
1.9       albertel  502:     my $result_del_msg = 
                    503: 	&Apache::lonnet::del('nohist_email'.$srcsuffix,[$msgid]);
                    504:     my $result_del_stat =
                    505: 	&Apache::lonnet::del('email_status'.$srcsuffix,[$msgid]);
1.32      albertel  506:     if (&Apache::lonnet::error($result_del_msg)) {
1.9       albertel  507: 	return (0,&mt('Message copied, but unable to delete the original from the source folder.'));
                    508:     }
1.32      albertel  509:     if (&Apache::lonnet::error($result_del_stat)) {
1.9       albertel  510: 	return (0,&mt('Message copied, but unable to delete the original status from the source folder.'));
                    511:     }
                    512: 
                    513:     return (1);
1.1       albertel  514: }
                    515: 
                    516: # ======================================================= Display a course list
                    517: 
                    518: sub discourse {
1.25      foxr      519:     my $result;
                    520:     my ($course_personnel,
                    521: 	$current_members,
                    522: 	$expired_members,
1.28      foxr      523: 	$future_members) = 
                    524: 	    &Apache::lonselstudent::get_people_in_class($env{'request.course.sec'});
1.25      foxr      525:     unshift @$current_members, (@$course_personnel);
                    526:     my %defaultUsers;
1.40      albertel  527:     
                    528:     $result .= '<input type="hidden" name="sendmode" value="group" />'."\n";
1.25      foxr      529: 
1.40      albertel  530:     $result .= &Apache::lonselstudent::render_student_list($current_members,
                    531: 							   "compemail",
                    532: 							   "current",
                    533: 							   \%defaultUsers,
                    534: 							   1,"selectedusers",1);
1.25      foxr      535: 
1.28      foxr      536:     $result .= &Apache::lonselstudent::render_student_list($expired_members,
                    537: 							   "compemail",
                    538: 							   "expired",
                    539: 							   \%defaultUsers,
                    540: 							   1, "selectedusers",0);
                    541:     $result .= &Apache::lonselstudent::render_student_list($future_members,
                    542: 							   "compemail",
                    543: 							   "future",
                    544: 							   \%defaultUsers,
                    545: 							   1, "selectedusers", 0);
1.25      foxr      546:     return $result;
                    547: }
                    548: 
1.38      raeburn   549: sub disgroup {
                    550:     my ($cdom,$cnum,$group,$viewgrps,$editgrps) = @_;
                    551:     my $result;
                    552:     #  Needs to be in a course
                    553:     if (!($env{'request.course.fn'})) {
                    554:         $result = &mt('Error: you must have a course role selected to be able to send a broadcast message to a group in the course.');
                    555:         return $result;
                    556:     }
                    557:     if ($cdom eq '' || $cnum eq '') {
                    558:         $result = &mt('Error: could not determine domain or number of course');
                    559:         return $result;
                    560:     }
                    561:     my ($memberinfo,$numitems) =
                    562:                  &Apache::longroup::group_memberlist($cdom,$cnum,$group,{},[]);
                    563:     my @statustypes = ('active');
                    564:     if ($viewgrps || $editgrps) {
                    565:         push(@statustypes,('future','previous'));
                    566:     }
                    567:     if (keys(%{$memberinfo}) == 0) {
                    568:         $result = &mt('As this group has no members, there are no '.
                    569:                       'recipients to select.');
                    570:         return $result;
                    571:     } else {
                    572:         $result = &mt('Select message recipients from the group members listed below.<br />');  
                    573:         my %Sortby = (
                    574:                          active   => {},
                    575:                          previous => {},
                    576:                          future   => {},
                    577:                      );
                    578:         my %lt = &Apache::lonlocal::texthash(
                    579:                                      'name'     => 'Name',
                    580:                                      'usnm'     => 'Username',
                    581:                                      'doma'     => 'Domain',
                    582:                                      'active'   => 'Active Members',
                    583:                                      'previous' => 'Former Members',
                    584:                                      'future'   => 'Future Members',
                    585:                                     );
                    586:         foreach my $user (sort(keys(%{$memberinfo}))) {
                    587:             my $status = $$memberinfo{$user}{status};
                    588:             if ($env{'form.'.$status.'.sortby'} eq 'fullname') {
                    589:                 push(@{$Sortby{$status}{$$memberinfo{$user}{fullname}}},$user);
                    590:             } elsif ($env{'form.'.$status.'.sortby'} eq 'username') {
                    591:                 push(@{$Sortby{$status}{$$memberinfo{$user}{uname}}},$user);
                    592:             } elsif ($env{'form.'.$status.'.sortby'} eq 'domain') {
                    593:                 push(@{$Sortby{$status}{$$memberinfo{$user}{udom}}},$user);
                    594:             } else {
                    595:                 push(@{$Sortby{$status}{$$memberinfo{$user}{fullname}}},$user);
                    596:             }
                    597:         }
                    598:         $result .= &group_check_uncheck();
                    599:         $result .= '<table border="0" cellspacing="8" cellpadding="2">'.
                    600:                    '<tr>';
                    601:         foreach my $status (@statustypes)  {
                    602:             if (ref($numitems) eq 'HASH') {
                    603:                 if ((defined($$numitems{$status})) && ($$numitems{$status})) {
1.39      raeburn   604:                     $result.='<td valign="top">'.
1.38      raeburn   605:                              '<fieldset><legend><b>'.$lt{$status}.
                    606:                              '</b></legend><nobr>'.
1.50      raeburn   607:                              '<input type="button" value="'.&mt('Check All').'" '.
1.38      raeburn   608:                              'onclick="javascript:toggleAll('."'".$status."','check'".')" />'.
                    609:                              '&nbsp;&nbsp;'.
1.50      raeburn   610:                              '<input type="button" value="'.&mt('Uncheck All').'" '.
1.38      raeburn   611:                              'onclick="javascript:toggleAll('."'".$status."','uncheck'".')" />'.
                    612:                              '</nobr></fieldset><br />'.
                    613:                              &Apache::loncommon::start_data_table().
                    614:                              &Apache::loncommon::start_data_table_header_row();
1.39      raeburn   615:                     $result .= "<th>$lt{'name'}</a></th>".
                    616:                                "<th>$lt{'usnm'}</a></th>".
                    617:                                "<th>$lt{'doma'}</a></th>".
1.38      raeburn   618:                     &Apache::loncommon::end_data_table_header_row();
                    619:                     foreach my $key (sort(keys(%{$Sortby{$status}}))) {
                    620:                         foreach my $user (@{$Sortby{$status}{$key}}) {
                    621:                             $result .=
                    622:                                 &Apache::loncommon::start_data_table_row().
                    623:                                 '<td><input type="checkbox" '.
                    624:                                 'name="selectedusers_forminput" value="'.
                    625:                                 $user.':'.$status.'" />'.
                    626:                                 $$memberinfo{$user}{'fullname'}.'</td>'.
                    627:                                 '<td>'.$$memberinfo{$user}{'uname'}.'</td>'.
                    628:                                 '<td>'.$$memberinfo{$user}{'udom'}.'</td>'.
                    629:                                 &Apache::loncommon::end_data_table_row();
                    630:                         }
                    631:                     }
                    632:                     $result .= &Apache::loncommon::end_data_table();
                    633:                 }
                    634:             }
                    635:             $result .= '</td><td>&nbsp;&nbsp;</td>';
                    636:         }
                    637:         $result .= '</tr></table>';
                    638:     }
                    639:     return $result;
                    640: }
                    641: 
                    642: sub group_check_uncheck {
                    643:     my $output = qq|
                    644: <script type="text/javascript">
                    645: function toggleAll(caller,action) {
                    646:     var pattern = new RegExp(":"+caller+"\$");
                    647:     if (typeof(document.compemail.selectedusers_forminput.length)=="undefined") {
                    648:         if (document.compemail.selectedusers_forminput.value.match(pattern)) {
                    649:             if (action == 'check') {
                    650:                 document.groupmail.selectedusers_forminput.checked = true;
                    651:             } else {
                    652:                 document.groupmail.selectedusers_forminput.checked = false;
                    653:             }
                    654:         }
                    655:     } else {
                    656:         for (var i=0; i<document.compemail.selectedusers_forminput.length; i++) {
                    657:             if (document.compemail.selectedusers_forminput[i].value.match(pattern)) {
                    658:                 if (action == 'check') {
                    659:                     document.compemail.selectedusers_forminput[i].checked = true;
                    660:                 } else {
                    661:                     document.compemail.selectedusers_forminput[i].checked = false;
                    662:                 }
                    663:             }
                    664:         }
                    665:     }
                    666: }
                    667: </script>
                    668:     |;
                    669: }
                    670: 
                    671: sub groupmail_header {
                    672:     my ($action,$group,$cdom,$cnum) = @_;
                    673:     my ($description,$refarg);
                    674:     if (!$cdom || !$cnum) {
                    675:         $cdom = $env{'course.'.$env{'request.course.id'}.'.domain'};
                    676:         $cnum = $env{'course.'.$env{'request.course.id'}.'.num'};
                    677:     }
                    678:     if (exists($env{'form.ref'})) {
                    679:         $refarg = 'ref='.$env{'form.ref'};
                    680:     }
                    681:     if (!$group) {
                    682:         $group = $env{'form.group'};
                    683:     }
                    684:     if ($group eq '') {
                    685:         return  '';
                    686:     } else {
                    687:         my %curr_groups = &Apache::longroup::coursegroups($cdom,$cnum,$group);
                    688:         if (defined($curr_groups{$group})) {
                    689:             my %groupinfo =
                    690:                     &Apache::longroup::get_group_settings($curr_groups{$group});
                    691:             $description = &unescape($groupinfo{'description'});
                    692:         }
                    693:     }
                    694:     &Apache::lonhtmlcommon::clear_breadcrumbs();
                    695:     if ($refarg) {
                    696:         &Apache::lonhtmlcommon::add_breadcrumb
                    697:             ({href=>"/adm/coursegroups",
                    698:               text=>"Groups",
                    699:               title=>"View course groups"});
                    700:     }
                    701:     &Apache::lonhtmlcommon::add_breadcrumb
                    702:         ({href=>"/adm/$cdom/$cnum/$group/smppg?$refarg",
                    703:           text=>"Group: $description",
                    704:           title=>"Go to group's home page"},
                    705:          {href=>"/adm/email?compose=group&amp;group=".
                    706:                 "$env{'form.group'}&amp;$refarg",
                    707:           text=>"Send a Message in a Group",
                    708:           title=>"Compose Group Email Message"},);
                    709:     if ($action eq 'sending') {
                    710:             &Apache::lonhtmlcommon::add_breadcrumb
                    711:                          ({text=>"Messages being sent.",
                    712:                            title=>"Messages sent"},);
                    713:     }
                    714:     my $groupheader = &Apache::loncommon::start_page('Group Email');
                    715:     $groupheader .= &Apache::lonhtmlcommon::breadcrumbs
                    716:                 ('Group - '.$env{'form.group'}.' Email');
                    717:     return $groupheader;
                    718: }
                    719: 
                    720: sub groupmail_sent {
                    721:     my ($group,$cdom,$cnum) = @_;
                    722:     my $refarg;
                    723:     if (exists($env{'form.ref'})) {
                    724:         $refarg = 'ref='.$env{'form.ref'};
                    725:     }
                    726:     my $output .= '<br /><br /><a href="/adm/email?compose=group&amp;group='.
                    727:                   $group.'&amp;'.$refarg.'">'.
                    728:                   &mt('Send another group email').'</a>'.'&nbsp;&nbsp;&nbsp;'.
                    729:                   '<a href="/adm/'.$cdom.'/'.$cnum.'/'.$group.'/smppg?'.
                    730:                   $refarg.'">'. &mt('Return to group page').'</a>';
                    731:     return $output;
                    732: }
                    733: 
1.1       albertel  734: # ==================================================== Display Critical Message
                    735: 
                    736: sub discrit {
                    737:     my $r=shift;
1.5       albertel  738:     my $header = '<h1><font color="red">'.&mt('Critical Messages').'</font></h1>'.
1.1       albertel  739:         '<form action="/adm/email" method="POST">'.
                    740:         '<input type="hidden" name="confirm" value="true" />';
                    741:     my %what=&Apache::lonnet::dump('critical');
                    742:     my $result = '';
1.42      raeburn   743:     foreach my $key (sort(keys(%what))) {
                    744:         my %content=&Apache::lonmsg::unpackagemsg($what{$key});
1.1       albertel  745:         next if ($content{'senderdomain'} eq '');
                    746:         $result.='<hr />'.&mt('From').': <b>'.
                    747: &Apache::loncommon::aboutmewrapper(
                    748:  &Apache::loncommon::plainname($content{'sendername'},$content{'senderdomain'}),$content{'sendername'},$content{'senderdomain'}).'</b> ('.
1.14      albertel  749: $content{'sendername'}.':'.
1.1       albertel  750:             $content{'senderdomain'}.') '.$content{'time'}.
                    751:             '<br />'.&mt('Subject').': '.$content{'subject'}.
                    752:             '<br /><pre>'.
                    753:               &Apache::lontexconvert::msgtexconverted($content{'message'}).
                    754:             '</pre><small>'.
                    755: &mt('You have to confirm that you received this message. After confirmation, this message will be moved to your regular inbox').
                    756:             '</small><br />'.
1.42      raeburn   757:             '<input type="submit" name="rec_'.$key.'" value="'.&mt('Confirm Receipt').'" />'.
                    758:             '<input type="submit" name="reprec_'.$key.'" '.
1.1       albertel  759:                   'value="'.&mt('Confirm Receipt and Reply').'" />';
                    760:     }
                    761:     # Check to see if there were any messages.
                    762:     if ($result eq '') {
                    763:         $result = "<h2>".&mt('You have no critical messages.')."</h2>".
1.38      raeburn   764: 	    '<a href="/adm/roles">'.&mt('Select a course').'</a><br />'.
1.1       albertel  765:             '<a href="/adm/email">'.&mt('Communicate').'</a>';
                    766:     } else {
                    767:         $r->print($header);
                    768:     }
                    769:     $r->print($result);
                    770:     $r->print('<input type="hidden" name="displayedcrit" value="true" /></form>');
                    771: }
                    772: 
                    773: sub sortedmessages {
1.53      raeburn   774:     my ($blocked,$startblock,$endblock,$numblocked,$folder,$msgstatus) = @_;
1.1       albertel  775:     my $suffix=&Apache::lonmsg::foldersuffix($folder);
                    776:     my @messages = &Apache::lonnet::getkeys('nohist_email'.$suffix);
1.46      raeburn   777: 
1.1       albertel  778:     #unpack the varibles and repack into temp for sorting
                    779:     my @temp;
                    780:     my %descriptions;
                    781:     my %status_cache = 
                    782: 	&Apache::lonnet::get('email_status'.&Apache::lonmsg::foldersuffix($folder),\@messages);
1.11      albertel  783: 
                    784:     my $get_received;
                    785:     if ($folder eq 'sent' 
                    786: 	&& ($env{'form.sortedby'} =~ m/^(rev)?(user|domain)$/)) {
                    787: 	$get_received = 1;
                    788:     }
                    789: 
                    790:     foreach my $msgid (@messages) {
1.29      www       791: 	my $esc_msgid=&escape($msgid);
1.1       albertel  792: 	my ($sendtime,$shortsubj,$fromname,$fromdomain,$status,$fromcid)=
1.11      albertel  793: 	    &Apache::lonmsg::unpackmsgid($esc_msgid,$folder,undef,
1.1       albertel  794: 					 \%status_cache);
1.53      raeburn   795:         next if ($msgstatus ne '' && $msgstatus ne $status);
1.1       albertel  796:         my $description = &get_course_desc($fromcid,\%descriptions);
                    797: 	my @temp1 = ($sendtime,$shortsubj,$fromname,$fromdomain,$status,
1.11      albertel  798: 		     $esc_msgid,$description);
                    799: 	if ($get_received) {
                    800: 	    my %message = &Apache::lonnet::get('nohist_email'.$suffix,
                    801: 					       [$msgid]);
                    802: 	    my %content = &Apache::lonmsg::unpackagemsg($message{$msgid});
                    803: 	    push(@temp1,$content{'recuser'},$content{'recdomain'});
                    804: 	}
1.1       albertel  805:         # Check whether message was sent during blocking period.
                    806:         if ($sendtime >= $startblock && ($sendtime <= $endblock && $endblock > 0) ) {
1.11      albertel  807:             $$blocked{$msgid} = 'ON';
1.1       albertel  808:             $$numblocked ++;
                    809:         } else { 
                    810:             push @temp ,\@temp1;
                    811:         }
                    812:     }
                    813:     #default sort
                    814:     @temp = sort  {$a->[0] <=> $b->[0]} @temp;    
                    815:     if ($env{'form.sortedby'} eq "date"){
                    816:         @temp = sort  {$a->[0] <=> $b->[0]} @temp;    
                    817:     }
                    818:     if ($env{'form.sortedby'} eq "revdate"){
                    819:     	@temp = sort  {$b->[0] <=> $a->[0]} @temp; 
                    820:     }
                    821:     if ($env{'form.sortedby'} eq "user"){
1.11      albertel  822: 	if ($get_received) {
                    823: 	    @temp = sort  {lc($a->[7][0]) cmp lc($b->[7][0])} @temp;
                    824: 	} else {
                    825: 	    @temp = sort  {lc($a->[2])    cmp lc($b->[2])}    @temp;
                    826: 	}
1.1       albertel  827:     }
                    828:     if ($env{'form.sortedby'} eq "revuser"){
1.11      albertel  829: 	if ($get_received) {
                    830: 	    @temp = sort  {lc($b->[7][0]) cmp lc($a->[7][0])} @temp;
                    831: 	} else {
                    832: 	    @temp = sort  {lc($b->[2])    cmp lc($a->[2])}    @temp;
                    833: 	}
1.1       albertel  834:     }
                    835:     if ($env{'form.sortedby'} eq "domain"){
1.11      albertel  836: 	if ($get_received) {
                    837: 	    @temp = sort  {$a->[8][0] cmp $b->[8][0]} @temp;
                    838: 	} else {
                    839: 	    @temp = sort  {$a->[3]    cmp $b->[3]}    @temp;
                    840: 	}
1.1       albertel  841:     }
                    842:     if ($env{'form.sortedby'} eq "revdomain"){
1.11      albertel  843: 	if ($get_received) {
                    844: 	    @temp = sort  {$b->[8][0] cmp $a->[8][0]} @temp;
                    845: 	} else {
                    846: 	    @temp = sort  {$b->[3]    cmp $a->[3]}    @temp;
                    847: 	}
1.1       albertel  848:     }
                    849:     if ($env{'form.sortedby'} eq "subject"){
                    850:         @temp = sort  {lc($a->[1]) cmp lc($b->[1])} @temp;
                    851:     }
                    852:     if ($env{'form.sortedby'} eq "revsubject"){
                    853:         @temp = sort  {lc($b->[1]) cmp lc($a->[1])} @temp;
                    854:     }
                    855:     if ($env{'form.sortedby'} eq "course"){
                    856:         @temp = sort  {lc($a->[6]) cmp lc($b->[6])} @temp;
                    857:     }
                    858:     if ($env{'form.sortedby'} eq "revcourse"){
                    859:         @temp = sort  {lc($b->[6]) cmp lc($a->[6])} @temp;
                    860:     }
                    861:     if ($env{'form.sortedby'} eq "status"){
                    862:         @temp = sort  {$a->[4] cmp $b->[4]} @temp;
                    863:     }
                    864:     if ($env{'form.sortedby'} eq "revstatus"){
                    865:         @temp = sort  {$b->[4] cmp $a->[4]} @temp;
                    866:     }
                    867:     return @temp;
                    868: }
                    869: 
                    870: sub get_course_desc {
                    871:     my ($fromcid,$descriptions) = @_;
                    872:     my $description;
                    873:     if (!$fromcid) {
                    874:         return $description;
                    875:     } else {
                    876:         if (defined($$descriptions{$fromcid})) {
                    877:             $description = $$descriptions{$fromcid};
                    878:         } else {
                    879:             if (defined($env{'course.'.$fromcid.'.description'})) {
                    880:                 $description = $env{'course.'.$fromcid.'.description'};
                    881:             } else {
1.48      albertel  882:                 my %courseinfo=&Apache::lonnet::coursedescription($fromcid);
1.1       albertel  883:                 $description = $courseinfo{'description'};
                    884:             }
                    885:             $$descriptions{$fromcid} = $description;
                    886:         }
                    887:         return $description;
                    888:     }
                    889: }
                    890: 
                    891: # ======================================================== Display all messages
                    892: 
                    893: sub disall {
1.53      raeburn   894:     my ($r,$folder,$msgstatus)=@_;
                    895:     $r->print(&folderlist($folder,$msgstatus));
                    896:     if ($folder eq 'critical') {
1.1       albertel  897: 	&discrit($r);
                    898:     } else {
1.53      raeburn   899: 	&disfolder($r,$folder,$msgstatus);
1.1       albertel  900:     }
                    901: }
                    902: 
                    903: # ============================================================ Display a folder
                    904: 
                    905: sub disfolder {
1.53      raeburn   906:     my ($r,$folder,$msgstatus)=@_;
                    907:     my %statushash = &get_msgstatus_types();
1.1       albertel  908:     my %blocked = ();
                    909:     my %setters = ();
                    910:     my $numblocked = 0;
1.44      raeburn   911:     my ($startblock,$endblock) = &Apache::loncommon::blockcheck(\%setters,'com');
1.53      raeburn   912:     my %lt = &Apache::lonlocal::texthash(
                    913:                       sede => 'Select a destination folder to which the messages will be moved.',
                    914:                       nome => 'No messages have been selected to apply ths action to.',
                    915:                       chec => 'Check the checkbox for at least one message.',  
                    916:     );
1.1       albertel  917:     $r->print(<<ENDDISHEADER);
1.16      albertel  918: <script type="text/javascript">
1.1       albertel  919:     function checkall() {
1.53      raeburn   920: 	for (i=0; i<document.forms.disall.delmark.length; i++) {
                    921: 	    document.forms.disall.delmark[i].checked=true;
1.1       albertel  922:         }
                    923:     }
                    924: 
                    925:     function uncheckall() {
1.53      raeburn   926: 	for (i=0; i<document.forms.disall.delmark.length; i++) {
                    927: 	    document.forms.disall.delmark[i].checked=false;
                    928:         }
                    929:     }
                    930:     function checkfoldermove() {
                    931:         if (document.disall.checkedaction.options[document.disall.checkedaction.selectedIndex].value == 'markedmove') {
                    932:             if (document.disall.movetofolder.options[document.disall.movetofolder.selectedIndex].value == "") {
                    933:                 alert("$lt{'sede'}");
                    934:                 return;
1.1       albertel  935:             }
                    936:         }
1.53      raeburn   937:         return; 
1.1       albertel  938:     }
1.53      raeburn   939: 
                    940:     function validate_checkedaction() {
                    941:         document.disall.markedaction.value = document.disall.checkedaction.options[document.disall.checkedaction.selectedIndex].value;
                    942:         if (document.disall.checkedaction.options[document.disall.checkedaction.selectedIndex].value == 'markedmove') {
                    943:             if (document.disall.movetofolder.options[document.disall.movetofolder.selectedIndex].value == "") {
                    944:                 alert("$lt{'sede'}");
                    945:                 return;
                    946:             } 
                    947:         }
                    948:         var checktotal = 0;
                    949:         for (var i=0; i<document.forms.disall.delmark.length; i++) {
                    950:             if (document.forms.disall.delmark[i].checked) {
                    951:                 checktotal ++;
                    952:             }
                    953:         }
                    954:         if (checktotal == 0) {
                    955:             alert("$lt{'nome'}\\n$lt{'chec'}");
                    956:             return;
                    957:         }
                    958:         document.disall.submit();
                    959:     }
                    960: 
1.1       albertel  961: </script>
                    962: ENDDISHEADER
1.54      albertel  963: 
1.1       albertel  964:     my $fsqs='&folder='.$folder;
1.53      raeburn   965:     my @temp=&sortedmessages(\%blocked,$startblock,$endblock,\$numblocked,$folder,$msgstatus);
1.1       albertel  966:     my $totalnumber=$#temp+1;
1.53      raeburn   967:     if ($totalnumber < 1) {
                    968:         if ($msgstatus eq '') {
                    969: 	    $r->print('<h2>'.&mt('Empty Folder').'</h2>');
                    970:         } elsif ($msgstatus eq 'replied') {
                    971:             $r->print('<h2>'.&mt('You have not replied to any messages in this folder.').'</h2>');
                    972:         } else { 
1.55    ! albertel  973:             $r->print('<h2>'.&mt('There are no '.lc($statushash{$msgstatus}).' messages in this folder.').'</h2>');
1.53      raeburn   974:         }
1.1       albertel  975: 	return;
                    976:     }
                    977:     unless ($interdis) {
                    978: 	$interdis=20;
                    979:     }
                    980:     my $number=int($totalnumber/$interdis);
1.53      raeburn   981:     if ($interdis) {
                    982:         if ($totalnumber%$interdis == 0) {
                    983:             $number--; 
                    984:         }
                    985:     }
                    986: 
1.1       albertel  987:     if (($startdis<0) || ($startdis>$number)) { $startdis=$number; }
                    988:     my $firstdis=$interdis*$startdis;
                    989:     if ($firstdis>$#temp) { $firstdis=$#temp-$interdis+1; }
                    990:     my $lastdis=$firstdis+$interdis-1;
                    991:     if ($lastdis>$#temp) { $lastdis=$#temp; }
1.53      raeburn   992:     $r->print(&scrollbuttons($startdis,$number,$firstdis,$lastdis,$totalnumber,$msgstatus));
1.1       albertel  993:     $r->print('<form method="post" name="disall" action="/adm/email">'.
1.53      raeburn   994: 	      '<table class="LC_mail_list"><tr><th colspan="1">&nbsp;</th><th>');
1.1       albertel  995:     if ($env{'form.sortedby'} eq "revdate") {
                    996: 	$r->print('<a href = "?sortedby=date'.$fsqs.'">'.&mt('Date').'</a></th>');
                    997:     } else {
                    998: 	$r->print('<a href = "?sortedby=revdate'.$fsqs.'">'.&mt('Date').'</a></th>');
                    999:     }
                   1000:     $r->print('<th>');
                   1001:     if ($env{'form.sortedby'} eq "revuser") {
                   1002: 	$r->print('<a href = "?sortedby=user'.$fsqs.'">'.&mt('Username').'</a>');
                   1003:     } else {
                   1004: 	$r->print('<a href = "?sortedby=revuser'.$fsqs.'">'.&mt('Username').'</a>');
                   1005:     }
                   1006:     $r->print('</th><th>');
                   1007:     if ($env{'form.sortedby'} eq "revdomain") {
                   1008: 	$r->print('<a href = "?sortedby=domain'.$fsqs.'">'.&mt('Domain').'</a>');
                   1009:     } else {
                   1010: 	$r->print('<a href = "?sortedby=revdomain'.$fsqs.'">'.&mt('Domain').'</a>');
                   1011:     }
                   1012:     $r->print('</th><th>');
                   1013:     if ($env{'form.sortedby'} eq "revsubject") {
                   1014: 	$r->print('<a href = "?sortedby=subject'.$fsqs.'">'.&mt('Subject').'</a>');
                   1015:     } else {
                   1016:     	$r->print('<a href = "?sortedby=revsubject'.$fsqs.'">'.&mt('Subject').'</a>');
                   1017:     }
                   1018:     $r->print('</th><th>');
                   1019:     if ($env{'form.sortedby'} eq "revcourse") {
1.46      raeburn  1020:         $r->print('<a href = "?sortedby=course'.$fsqs.'">'.&mt('Course').'</a>');
1.1       albertel 1021:     } else {
1.46      raeburn  1022:         $r->print('<a href = "?sortedby=revcourse'.$fsqs.'">'.&mt('Course').'</a>');
1.1       albertel 1023:     }
                   1024:     $r->print('</th><th>');
                   1025:     if ($env{'form.sortedby'} eq "revstatus") {
                   1026: 	$r->print('<a href = "?sortedby=status'.$fsqs.'">'.&mt('Status').'</a></th>');
                   1027:     } else {
                   1028:      	$r->print('<a href = "?sortedby=revstatus'.$fsqs.'">'.&mt('Status').'</a></th>');
                   1029:     }
                   1030:     $r->print("</tr>\n");
1.6       albertel 1031: 
                   1032:     my $suffix = &Apache::lonmsg::foldersuffix($folder);
1.1       albertel 1033:     for (my $n=$firstdis;$n<=$lastdis;$n++) {
1.11      albertel 1034: 	my ($sendtime,$shortsubj,$fromname,$fromdomain,$status,$origID,
                   1035: 	    $description,$recv_name,$recv_domain)= 
                   1036: 		@{$temp[$n]};
1.1       albertel 1037: 	if (($status ne 'deleted') && defined($sendtime) && $sendtime!~/error/) {
                   1038: 	    if ($status eq 'new') {
1.4       albertel 1039: 		$r->print('<tr class="LC_mail_new">');
1.1       albertel 1040: 	    } elsif ($status eq 'read') {
1.4       albertel 1041: 		$r->print('<tr class="LC_mail_read">');
1.1       albertel 1042: 	    } elsif ($status eq 'replied') {
1.4       albertel 1043: 		$r->print('<tr class="LC_mail_replied">'); 
1.1       albertel 1044: 	    } else {
1.4       albertel 1045: 		$r->print('<tr class="LC_mail_other">');
1.1       albertel 1046: 	    }
1.6       albertel 1047: 	    my ($dis_name,$dis_domain) = ($fromname,$fromdomain);
                   1048: 	    if ($folder eq 'sent') {
1.11      albertel 1049: 		if (defined($recv_name) && !defined($recv_domain)) {
                   1050: 		    $dis_name   = join('<br />',@{$recv_name});
                   1051: 		    $dis_domain = join('<br />',@{$recv_domain});
                   1052: 		} else {
1.29      www      1053: 		    my $msg_id  = &unescape($origID);
1.11      albertel 1054: 		    my %message = &Apache::lonnet::get('nohist_email'.$suffix,
                   1055: 						       [$msg_id]);
                   1056: 		    my %content = &Apache::lonmsg::unpackagemsg($message{$msg_id});
                   1057: 		    $dis_name   = join('<br />',@{$content{'recuser'}});
                   1058: 		    $dis_domain = join('<br />',@{$content{'recdomain'}});
                   1059: 		}
1.6       albertel 1060: 	    }
1.53      raeburn  1061:             my $localsenttime = &Apache::lonlocal::locallocaltime($sendtime);
                   1062:             my $count = $n +1;
                   1063: 	    $r->print('<td align="right"><nobr>'.(($status eq 'new')?'<b>':'').
                   1064:                       $count.'.'.(($status eq 'new')?'</b>':'').'&nbsp;'.
                   1065:                       '<input type="checkbox" name="delmark"'. 
                   1066:                       ' value="'.$origID.'" /></nobr></td>');
                   1067:             foreach my $item ($localsenttime,$dis_name,$dis_domain,$shortsubj) {
                   1068:                 $r->print('<td>'.(($status eq 'new')?'<b>':'').
                   1069:                           '<a href="/adm/email?display='.$origID.$sqs.'">'.
                   1070:                           $item.(($status eq 'new')?'</b>':'').'</td>');
                   1071:             }
                   1072:             my $showstatus;
                   1073:             my %statushash = &get_msgstatus_types();
                   1074:             if ($status eq '') {
                   1075:                 $showstatus = '';
                   1076:             } else {
                   1077:                 $showstatus = $statushash{$status};
                   1078:             }
                   1079: 	    $r->print('<td>'.(($status eq 'new')?'<b>':'').$description.
                   1080:                       (($status eq 'new')?'</b>':'').'</td><td>'.
                   1081:                       (($status eq 'new')?'<b>':'').$showstatus.
                   1082:                       (($status eq 'new')?'</b>':'').'</td></tr>'."\n");
1.1       albertel 1083: 	} elsif ($status eq 'deleted') {
                   1084: # purge
1.9       albertel 1085: 	    my ($result,$msg) = 
1.29      www      1086: 		&movemsg(&unescape($origID),$folder,'trash');
1.9       albertel 1087: 	    
1.1       albertel 1088: 	}
                   1089:     }   
1.53      raeburn  1090:     $r->print("</table>\n");
                   1091:     $r->print('<table border="0" cellspacing="2" cellpadding="2">
                   1092:  <tr>
                   1093:   <td>'.
                   1094:   '<input type="button" onclick="javascript:checkall()" value="'.&mt('Check All').'" /><br />'."\n".
                   1095:   '<input type="button" onclick="javascript:uncheckall()" value="'.&mt('Uncheck All').'" />'."\n".
                   1096:   '<input type="hidden" name="sortedby" value="'.$env{'form.sortedby'}.'" /></td><td>&nbsp;</td>'."\n".
                   1097:   '<td align="center"><b>'.&mt('Action').'</b><br />'."\n".
                   1098:   '  <select name="checkedaction" onchange="javascript:checkfoldermove()">'."\n");
                   1099: 
1.1       albertel 1100:     if ($folder ne 'trash') {
1.53      raeburn  1101:         $r->print('    <option value="markeddel">'.&mt('Delete').'</option>'."\n");
                   1102:     }
                   1103:     if ($msgstatus ne 'read') {
                   1104:         $r->print('    <option value="markedread">'.&mt('Mark Read').'</option>."\n"');
                   1105:     }
                   1106:     if ($msgstatus ne 'unread') {
                   1107:         $r->print('    <option value="markedunread">'.&mt('Mark Unread').'</option>'."\n");
1.1       albertel 1108:     }
1.53      raeburn  1109:     $r->print('   <option value="markedforward">'.&mt('Forward').'</option>'."\n");
1.54      albertel 1110: 
                   1111:     my %gotfolders = &Apache::lonmsg::get_user_folders();
1.53      raeburn  1112:     if (keys(%gotfolders) > 0) {
                   1113:         $r->print('   <option value="markedmove">'.&mt('Move to Folder ->').
                   1114:                   '</option>');
                   1115:     }
                   1116:     $r->print("\n".'</select></td>'."\n");
1.54      albertel 1117: 
1.53      raeburn  1118:     if (keys(%gotfolders) > 0) {
                   1119:         $r->print('<td align="center"><b>'.&mt('Destination folder').'<b><br />');
1.54      albertel 1120: 	my %userfolders;
1.53      raeburn  1121:         foreach my $key (keys(%gotfolders)) {
                   1122:             $userfolders{$key} = $key;
                   1123:         }
                   1124:         $userfolders{''} = "";
                   1125:         $r->print(&Apache::loncommon::select_form('','movetofolder',%userfolders).
                   1126:                   '</td>');
                   1127:     }
                   1128:     $r->print('<td>&nbsp;</td><td>&nbsp;&nbsp;'.
                   1129:               '<input type="button" name="go" value="'.&mt('Go').
                   1130:               '" onclick="javascript:validate_checkedaction()"/></td>'."\n".
                   1131:               '</tr></table>');
1.1       albertel 1132:     my $postedstartdis=$startdis+1;
1.53      raeburn  1133:     $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>');
1.1       albertel 1134:     if ($numblocked > 0) {
                   1135:         my $beginblock = &Apache::lonlocal::locallocaltime($startblock);
                   1136:         my $finishblock = &Apache::lonlocal::locallocaltime($endblock);
                   1137:         $r->print('<br /><br />'.
1.51      raeburn  1138:                   &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));
1.44      raeburn  1139:         $r->print(&Apache::loncommon::build_block_table($startblock,$endblock,
                   1140:                                                         \%setters));
1.1       albertel 1141:     }
                   1142: }
                   1143: 
                   1144: # ============================================================== Compose output
                   1145: 
                   1146: sub compout {
1.53      raeburn  1147:     my ($r,$forwarding,$replying,$broadcast,$replycrit,$folder,$dismode,
                   1148:         $multiforward)=@_;
1.1       albertel 1149:     my $suffix=&Apache::lonmsg::foldersuffix($folder);
1.38      raeburn  1150:     my ($cdom,$cnum,$group,$refarg);
                   1151:     if (exists($env{'form.group'})) {
                   1152:         $cdom = $env{'course.'.$env{'request.course.id'}.'.domain'};
                   1153:         $cnum = $env{'course.'.$env{'request.course.id'}.'.num'};
                   1154:         $group = $env{'form.group'};
                   1155:         my $action = 'composing';
                   1156:         $r->print(&groupmail_header($action,$group,$cdom,$cnum));
                   1157:     } elsif ($broadcast eq 'individual') {
1.1       albertel 1158: 	&printheader($r,'/adm/email?compose=individual',
                   1159: 	     'Send a Message');
                   1160:     } elsif ($broadcast) {
                   1161: 	&printheader($r,'/adm/email?compose=group',
                   1162: 	     'Broadcast Message');
                   1163:     } elsif ($forwarding) {
                   1164: 	&Apache::lonhtmlcommon::add_breadcrumb
1.29      www      1165:         ({href=>"/adm/email?display=".&escape($forwarding),
1.1       albertel 1166:           text=>"Display Message"});
1.29      www      1167: 	&printheader($r,'/adm/email?forward='.&escape($forwarding),
1.1       albertel 1168: 	     'Forwarding a Message');
                   1169:     } elsif ($replying) {
                   1170: 	&Apache::lonhtmlcommon::add_breadcrumb
1.29      www      1171:         ({href=>"/adm/email?display=".&escape($replying),
1.1       albertel 1172:           text=>"Display Message"});
1.29      www      1173: 	&printheader($r,'/adm/email?replyto='.&escape($replying),
1.1       albertel 1174: 	     'Replying to a Message');
                   1175:     } elsif ($replycrit) {
                   1176: 	$r->print('<h3>'.&mt('Replying to a Critical Message').'</h3>');
                   1177: 	$replying=$replycrit;
1.53      raeburn  1178:     } elsif ($multiforward) {
                   1179:         &Apache::lonhtmlcommon::add_breadcrumb
                   1180:         ({href=>"/adm/email?folder=".&escape($folder),
                   1181:           text=>"Display All Messages"});
                   1182:         &printheader($r,'/adm/email?compose=multiforward',
                   1183:              'Forwarding Multiple Messages');
                   1184:         $r->print(&mt('Each of the <b>[quant,_1,message]</b> you checked will be forwarded to the recipient(s) you select below.',$multiforward).'<br />');
1.1       albertel 1185:     } else {
                   1186: 	&printheader($r,'/adm/email?compose=upload',
                   1187: 	     'Distribute from Uploaded File');
                   1188:     }
                   1189: 
                   1190:     my $dispcrit='';
                   1191:     my $dissub='';
                   1192:     my $dismsg='';
                   1193:     my $disbase='';
                   1194:     my $func=&mt('Send New');
1.50      raeburn  1195:     my %lt=&Apache::lonlocal::texthash('us'  => 'Username',
                   1196: 				       'do'  => 'Domain',
                   1197: 				       'ad'  => 'Additional Recipients',
                   1198: 				       'sb'  => 'Subject',
                   1199: 				       'ca'  => 'Cancel',
                   1200: 				       'ma'  => 'Mail',
1.53      raeburn  1201:                                        'msg' => 'Messages',
1.50      raeburn  1202:                                        'gen' => 'Generate messages from a file',
                   1203:                                        'gmt' => 'General message text',
                   1204:                                        'tff' => 'The file format for the uploaded portion of the message is',
                   1205:                                        'uas' => 'Upload and Send',
                   1206:                                       );
1.1       albertel 1207:     if (&Apache::lonnet::allowed('srm',$env{'request.course.id'})
                   1208: 	|| &Apache::lonnet::allowed('srm',$env{'request.course.id'}.
                   1209: 				    '/'.$env{'request.course.sec'})) {
                   1210: 	 my $crithelp = Apache::loncommon::help_open_topic("Course_Critical_Message");
                   1211:          $dispcrit=
                   1212:  '<p><label><input type="checkbox" name="critmsg" /> '.&mt('Send as critical message').'</label> ' . $crithelp . 
                   1213:  '</p><p>'.
                   1214:  '<label><input type="checkbox" name="sendbck" /> '.&mt('Send as critical message').'  ' .
                   1215:  &mt('and return receipt') . '</label>' . $crithelp . 
                   1216:  '</p><p><label><input type="checkbox" name="permanent" /> '.
                   1217: &mt('Send copy to permanent email address (if known)').'</label></p>'.
                   1218: '<p><label><input type="checkbox" name="rsspost" /> '.
                   1219: 		  &mt('Include in course RSS newsfeed').'</label></p>';
                   1220:      }
                   1221:     my %message;
                   1222:     my %content;
                   1223:     my $defdom=$env{'user.domain'};
                   1224:     if ($forwarding) {
                   1225: 	%message=&Apache::lonnet::get('nohist_email'.$suffix,[$forwarding]);
                   1226: 	%content=&Apache::lonmsg::unpackagemsg($message{$forwarding},$folder);
                   1227: 	$dispcrit.='<input type="hidden" name="forwid" value="'.
                   1228: 	    $forwarding.'" />';
                   1229: 	$func=&mt('Forward');
                   1230: 	
                   1231: 	$dissub=&mt('Forwarding').': '.$content{'subject'};
                   1232: 	$dismsg=&mt('Forwarded message from').' '.
                   1233: 	    $content{'sendername'}.' '.&mt('at').' '.$content{'senderdomain'};
                   1234: 	if ($content{'baseurl'}) {
1.29      www      1235: 	    $disbase='<input type="hidden" name="baseurl" value="'.&escape($content{'baseurl'}).'" />';
1.1       albertel 1236: 	}
                   1237:     }
                   1238:     if ($replying) {
                   1239: 	%message=&Apache::lonnet::get('nohist_email'.$suffix,[$replying]);
                   1240: 	%content=&Apache::lonmsg::unpackagemsg($message{$replying},$folder);
                   1241: 	$dispcrit.='<input type="hidden" name="replyid" value="'.
                   1242: 	    $replying.'" />';
                   1243: 	$func=&mt('Send Reply to');
                   1244: 	
                   1245: 	$dissub=&mt('Reply').': '.$content{'subject'};       
                   1246: 	$dismsg='> '.$content{'message'};
                   1247: 	$dismsg=~s/\r/\n/g;
                   1248: 	$dismsg=~s/\f/\n/g;
                   1249: 	$dismsg=~s/\n+/\n\> /g;
                   1250: 	if ($content{'baseurl'}) {
1.29      www      1251: 	    $disbase='<input type="hidden" name="baseurl" value="'.&escape($content{'baseurl'}).'" />';
1.1       albertel 1252: 	    if ($env{'user.adv'}) {
                   1253: 		$disbase.='<label><input type="checkbox" name="storebasecomment" />'.&mt('Store message for re-use').
                   1254: 		    '</label> <a href="/adm/email?showcommentbaseurl='.
1.29      www      1255: 		    &escape($content{'baseurl'}).'" target="comments">'.
1.1       albertel 1256: 		    &mt('Show re-usable messages').'</a><br />';
                   1257: 	    }
                   1258: 	}
                   1259:     }
                   1260:     my $citation=&displayresource(%content);
1.38      raeburn  1261:     my ($can_grp_broadcast,$viewgrps,$editgrps);
1.1       albertel 1262:     if ($env{'form.recdom'}) { $defdom=$env{'form.recdom'}; }
1.23      www      1263:     if ($env{'form.text'}) { $dismsg=$env{'form.text'}; }
                   1264:     if ($env{'form.subject'}) { $dissub=$env{'form.subject'}; }
                   1265:     $r->print(
1.1       albertel 1266:                 '<form action="/adm/email"  name="compemail" method="post"'.
                   1267:                 ' enctype="multipart/form-data">'."\n".
1.38      raeburn  1268:                 '<input type="hidden" name="sendmail" value="on" />'."\n");
                   1269:     if ($broadcast eq 'group' && $env{'form.group'} ne '') {
                   1270:         $can_grp_broadcast = 
                   1271:                 &Apache::lonnet::allowed('sgb',$env{'request.course.id'}.'/'.
                   1272:                                          $group);
                   1273:         $viewgrps = 
                   1274:                &Apache::lonnet::allowed('vcg',$env{'request.course.id'}.
                   1275:                ($env{'request.course.sec'}?'/'.$env{'request.course.sec'}:''));
                   1276:         $editgrps = 
                   1277:                &Apache::lonnet::allowed('mdg',$env{'request.course.id'}.
                   1278:                ($env{'request.course.sec'}?'/'.$env{'request.course.sec'}:''));
                   1279:         if ($viewgrps || $editgrps || $can_grp_broadcast) {
                   1280:             $r->print(&disgroup($cdom,$cnum,$group,$viewgrps,$editgrps));
                   1281:         }
                   1282:     }
                   1283:     $r->print('<table>');
                   1284:     if (($broadcast eq 'group') && ($group ne '') && 
                   1285:         (!$can_grp_broadcast && !$viewgrps && !$editgrps)) {
                   1286:         $r->print(&recipient_input_row($cdom,%lt));
                   1287:     } 
                   1288:     if (($broadcast ne 'group') && ($broadcast ne 'upload')) {
1.1       albertel 1289: 	if ($replying) {
                   1290: 	    $r->print('<tr><td colspan="2">'.&mt('Replying to').' '.
                   1291: 		      &Apache::loncommon::aboutmewrapper(
                   1292: 							 &Apache::loncommon::plainname($content{'sendername'},$content{'senderdomain'}),$content{'sendername'},$content{'senderdomain'}).' ('.
1.14      albertel 1293: 		      $content{'sendername'}.':'.
1.1       albertel 1294: 		      $content{'senderdomain'}.')'.
                   1295: 		      '<input type="hidden" name="recuname" value="'.$content{'sendername'}.'" />'.
                   1296: 		      '<input type="hidden" name="recdomain" value="'.$content{'senderdomain'}.'" />'.
                   1297: 		      '</td></tr>');
                   1298: 	} else {
1.38      raeburn  1299:             $r->print(&recipient_input_row($defdom,%lt));
1.1       albertel 1300:         }
                   1301:     }
                   1302:     my $latexHelp = Apache::loncommon::helpLatexCheatsheet();
1.53      raeburn  1303:     my $subj_size;
                   1304:     if ($multiforward) {
                   1305:         $r->print(&additional_rec_row(\%lt));
                   1306:         $r->print('<tr><td colspan="2">'.
                   1307:                   &mt('Unless you choose otherwise:').'<ul><li>'.
                   1308:         &mt("The subject in each forwarded message will be <i>'Forwarding:'</i> followed by the original subject.").'</li><li>'.
                   1309:         &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>');
                   1310:         $func=&mt('Forward');
                   1311:         $dissub = &mt('Forwarding').': ';
                   1312:         $subj_size = '10';
                   1313:         my $extra = '&lt;'.&mt('original subject').'&gt;&nbsp;&nbsp;&nbsp;'.
                   1314:                     '<input type="radio" name="showorigsubj" value="1" checked="checked" />'.&mt('Yes').'&nbsp;<input type="radio" name="showorigsubj" value="0" />'.&mt('No');
                   1315:         $dismsg = &mt('Forwarded message from ').' ';
                   1316:         my $sender = &mt("sender's name");
                   1317:         $r->print(&msg_subject_row($dissub,\%lt,$subj_size,$extra));
                   1318:         $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>
                   1319: </table>
                   1320: <br />'.
                   1321: $latexHelp.
                   1322: &mt("Any new text to display before the text of the original messages:").'<br />
                   1323: <textarea name="message" id="message" cols="80" rows="5" wrap="hard">
                   1324: </textarea></p><br />');
                   1325:         my @to_forward = &Apache::loncommon::get_env_multiple('form.delmark');
                   1326:         foreach my $msg (@to_forward) {
                   1327:             $r->print('<input type="hidden" name="delmark" value="'.$msg.'" />');
                   1328:         }
                   1329:         $r->print(&submit_button_row($folder,$dismode,$func.' '.$lt{'msg'},
                   1330:                                      \%lt));
                   1331:     } elsif ($broadcast ne 'upload') {
                   1332:         $subj_size = '50';
                   1333:         $r->print(&additional_rec_row(\%lt));
                   1334:         $r->print(&msg_subject_row($dissub,\%lt,$subj_size));
                   1335:         $r->print(<<"ENDCOMP");
                   1336: </table>
1.1       albertel 1337: $latexHelp
                   1338: <textarea name="message" id="message" cols="80" rows="15" wrap="hard">$dismsg
                   1339: </textarea></p><br />
                   1340: $dispcrit
                   1341: $disbase
                   1342: ENDCOMP
1.53      raeburn  1343:         $r->print(&submit_button_row($folder,$dismode,$func.' '.$lt{'ma'},
                   1344:                                      \%lt));
                   1345:         $r->print($citation);
1.38      raeburn  1346:         if (exists($env{'form.ref'})) {
                   1347:             $r->print('<input type="hidden" name="ref" value="'.
                   1348:                       $env{'form.ref'}.'" />');
                   1349:         }
                   1350:         if (exists($env{'form.group'})) {
                   1351:             $r->print('<input type="hidden" name="group" value="'.
                   1352:                       $env{'form.group'}.'" />');
                   1353:         }
1.1       albertel 1354:     } else { # $broadcast is 'upload'
1.50      raeburn  1355: 	$r->print(<<ENDBLOCK);
1.1       albertel 1356: <input type="hidden" name="sendmode" value="upload" />
                   1357: <input type="hidden" name="send" value="on" />
1.50      raeburn  1358: <h3>$lt{'gen'}</h3>
1.1       albertel 1359: <p>
                   1360: Subject: <input type="text" size="50" name="subject" />
                   1361: </p>
1.50      raeburn  1362: <p>$lt{'gmt'}:<br />
1.1       albertel 1363: <textarea name="message" id="message" cols="60" rows="10" wrap="hard">$dismsg
                   1364: </textarea></p>
                   1365: <p>
1.50      raeburn  1366: $lt{'tff'}:
                   1367: ENDBLOCK
                   1368:        $r->print('
                   1369: <pre>'."\n".
                   1370: &mt('username1:domain1: text')."\n".
                   1371: &mt('username2:domain2: text')."\n".
                   1372: &mt('username3:domain1: text')."\n".
                   1373: '</pre>
1.1       albertel 1374: </p>
                   1375: <p>
1.50      raeburn  1376: '.&mt('The messages will be assembled from all lines with the respective'."\n".'<tt>username:domain</tt>, and appended to the general message text.'));
                   1377:         $r->print(<<ENDUPLOAD);
                   1378: </p>
1.1       albertel 1379: <p>
                   1380: <input type="file" name="upfile" size="40" /></p><p>
                   1381: $dispcrit
1.50      raeburn  1382: <input type="submit" value="$lt{'uas'}" /></p>
1.1       albertel 1383: ENDUPLOAD
                   1384:     }
                   1385:     if ($broadcast eq 'group') {
1.38      raeburn  1386:        if ($group eq '') {
                   1387:            my $studentsel = &discourse();
                   1388:            $r->print($studentsel);
                   1389:        }
1.1       albertel 1390:     }
1.36      albertel 1391:     if ($env{'form.displayedcrit'}) {
                   1392: 	$r->print('<input type="hidden" name="displayedcrit" value="true" />');
                   1393:     }
1.1       albertel 1394:     $r->print('</form>'.
                   1395: 	      &Apache::lonfeedback::generate_preview_button('compemail','message').
                   1396: 	      &Apache::lonhtmlcommon::htmlareaselectactive('message'));
                   1397: }
                   1398: 
                   1399: # ---------------------------------------------------- Display all face to face
                   1400: 
1.38      raeburn  1401: sub recipient_input_row {
                   1402:     my ($dom,%lt) = @_;
                   1403:     my $domform = &Apache::loncommon::select_dom_form($dom,'recdomain');
                   1404:     my $selectlink=
                   1405:       &Apache::loncommon::selectstudent_link('compemail','recuname',
                   1406:                                              'recdomain');
                   1407:     my $output = <<"ENDREC";
                   1408: <tr><td>$lt{'us'}:</td><td><input type="text" size="12" name="recuname" value="$env{'form.recname'}" /></td><td rowspan="2">$selectlink</td></tr>
                   1409: <tr><td>$lt{'do'}:</td>
                   1410: <td>$domform</td></tr>
                   1411: ENDREC
                   1412:     return $output;
                   1413: }
                   1414: 
1.53      raeburn  1415: sub additional_rec_row {
                   1416:     my ($lt) = @_;
                   1417:     my $output = <<"ENDADD";
                   1418: <tr><td>$lt->{'ad'}:<br /><tt>username:domain,username:domain, ...
                   1419: </tt></td><td>
                   1420: <input type="text" size="50" name="additionalrec" /></td></tr>
                   1421: ENDADD
                   1422:     return $output;
                   1423: }
                   1424: 
                   1425: sub submit_button_row {
                   1426:     my ($folder,$dismode,$sendtext,$lt) = @_;
                   1427:     my $output = qq| 
                   1428: <input type="hidden" name="folder" value="$folder" />
                   1429: <input type="hidden" name="dismode" value="$dismode" />
                   1430: <input type="submit" name="send" value="$sendtext" />
                   1431: <input type="submit" name="cancel" value="$lt->{'ca'}" /><hr />
                   1432: |;
                   1433:     return $output;
                   1434: }
                   1435: 
                   1436: sub msg_subject_row {
                   1437:     my ($dissub,$lt,$subj_size,$extra) = @_;
                   1438:     my $output = '<tr><td>'.$lt->{'sb'}.':</td><td><input type="text" size="'.
                   1439:                  $subj_size.'" name="subject" value="'.$dissub.'" />'.$extra.
                   1440:                  '</td></tr>';
                   1441:     return $output;
                   1442: }
                   1443: 
1.1       albertel 1444: sub retrieve_instructor_comments {
                   1445:     my ($user,$domain)=@_;
                   1446:     my $target=$env{'form.grade_target'};
                   1447:     if (! $env{'request.course.id'}) { return; }
1.49      albertel 1448:     if (! &Apache::lonnet::allowed('dff',$env{'request.course.id'})
                   1449: 	&& ! &Apache::lonnet::allowed('dff',$env{'request.course.id'}.
1.1       albertel 1450: 				      '/'.$env{'request.course.sec'})) {
                   1451: 	return;
                   1452:     }
                   1453:     my %records=&Apache::lonnet::dump('nohist_email',
                   1454: 			 $env{'course.'.$env{'request.course.id'}.'.domain'},
                   1455: 			 $env{'course.'.$env{'request.course.id'}.'.num'},
                   1456:                          '%255b'.$user.'%253a'.$domain.'%255d');
                   1457:     my $result='';
1.42      raeburn  1458:     foreach my $key (sort(keys(%records))) {
                   1459:         my %content=&Apache::lonmsg::unpackagemsg($records{$key});
1.1       albertel 1460:         next if ($content{'senderdomain'} eq '');
                   1461:         next if ($content{'subject'} !~ /^Record/);
                   1462: 	# &Apache::lonfeedback::newline_to_br(\$content{'message'});
                   1463: 	$result.='Recorded by '.
1.14      albertel 1464:             $content{'sendername'}.':'.$content{'senderdomain'}."\n";
1.1       albertel 1465:         $result.=
                   1466:             &Apache::lontexconvert::msgtexconverted($content{'message'})."\n";
                   1467:      }
                   1468:     return $result;
                   1469: }
                   1470: 
                   1471: sub disfacetoface {
                   1472:     my ($r,$user,$domain)=@_;
                   1473:     my $target=$env{'form.grade_target'};
                   1474:     unless ($env{'request.course.id'}) { return; }
1.49      albertel 1475:     if  (!&Apache::lonnet::allowed('dff',$env{'request.course.id'})
                   1476: 	 && ! &Apache::lonnet::allowed('dff',$env{'request.course.id'}.
1.1       albertel 1477: 				       '/'.$env{'request.course.sec'})) {
1.50      raeburn  1478: 	$r->print(&mt('Not allowed'));
1.1       albertel 1479: 	return;
                   1480:     }
                   1481:     my %records=&Apache::lonnet::dump('nohist_email',
                   1482: 			 $env{'course.'.$env{'request.course.id'}.'.domain'},
                   1483: 			 $env{'course.'.$env{'request.course.id'}.'.num'},
                   1484:                          '%255b'.$user.'%253a'.$domain.'%255d');
                   1485:     my $result='';
1.42      raeburn  1486:     foreach my $key (sort(keys(%records))) {
                   1487:         my %content=&Apache::lonmsg::unpackagemsg($records{$key});
1.1       albertel 1488:         next if ($content{'senderdomain'} eq '');
                   1489: 	&Apache::lonfeedback::newline_to_br(\$content{'message'});
                   1490:         if ($content{'subject'}=~/^Record/) {
                   1491: 	    $result.='<h3>'.&mt('Record').'</h3>';
                   1492:         } elsif ($content{'subject'}=~/^Broadcast/) {
                   1493:             $result .='<h3>'.&mt('Broadcast Message').'</h3>';
                   1494:             if ($content{'subject'}=~/^Broadcast\./) {
                   1495:                 if (defined($content{'coursemsgid'})) {
1.29      www      1496:                     my $crsmsgid = &escape($content{'coursemsgid'});
1.1       albertel 1497:                     my $broadcast_message = &general_message($crsmsgid);
                   1498:                     $content{'message'} = '<b>'.&mt('Subject').': '.$content{'message'}.'</b><br />'.$broadcast_message;
                   1499:                 } else {
                   1500:                     %content=&Apache::lonmsg::unpackagemsg($content{'message'});
                   1501:                     $content{'message'} =
                   1502:                     '<b>'.&mt('Subject').': '.$content{'subject'}.'</b><br />'.
                   1503:                     $content{'message'};
                   1504:                 }
                   1505:             }    
                   1506:         } else {
                   1507:             $result.='<h3>'.&mt('Critical Message').'</h3>';
                   1508:             if (defined($content{'coursemsgid'})) {
1.29      www      1509:                 my $crsmsgid=&escape($content{'coursemsgid'});
1.1       albertel 1510:                 my $critical_message = &general_message($crsmsgid);
                   1511:                 $content{'message'} = '<b>'.&mt('Subject').': '.$content{'message'}.'</b><br />'.$critical_message;
                   1512:             } else {
                   1513:                 %content=&Apache::lonmsg::unpackagemsg($content{'message'});
                   1514:                 $content{'message'}=
                   1515:                 '<b>'.&mt('Subject').': '.$content{'subject'}.'</b><br />'.
                   1516: 		$content{'message'};
                   1517:             }
                   1518:         }
                   1519:         $result.=&mt('By').': <b>'.
                   1520: &Apache::loncommon::aboutmewrapper(
                   1521:  &Apache::loncommon::plainname($content{'sendername'},$content{'senderdomain'}),$content{'sendername'},$content{'senderdomain'}).'</b> ('.
1.14      albertel 1522: $content{'sendername'}.':'.
1.1       albertel 1523:             $content{'senderdomain'}.') '.$content{'time'}.
                   1524:             '<br /><pre>'.
                   1525:               &Apache::lontexconvert::msgtexconverted($content{'message'}).
                   1526: 	      '</pre>';
                   1527:      }
                   1528:     # Check to see if there were any messages.
                   1529:     if ($result eq '') {
1.33      albertel 1530:         my $lctype = lc(&Apache::loncommon::course_type());
1.1       albertel 1531: 	if ($target ne 'tex') { 
1.30      raeburn  1532: 	    $r->print("<p><b>".&mt('No notes, face-to-face discussion records, critical messages, or broadcast messages in this [_1].',$lctype)."</b></p>");
1.1       albertel 1533: 	} else {
1.30      raeburn  1534: 	    $r->print('\textbf{'.&mt('No notes, face-to-face discussion records, critical messages or broadcast messages in this [_1].',$lctype).'}\\\\');
1.1       albertel 1535: 	}
                   1536:     } else {
                   1537:        $r->print($result);
                   1538:     }
                   1539: }
                   1540: 
                   1541: sub general_message {
                   1542:     my ($crsmsgid) = @_;
                   1543:     my %general_content;
                   1544:     if ($crsmsgid) { 
                   1545:         my %course_content = &Apache::lonnet::get('nohist_email',[$crsmsgid],
                   1546:                            $env{'course.'.$env{'request.course.id'}.'.domain'},
                   1547:                            $env{'course.'.$env{'request.course.id'}.'.num'});
                   1548:         %general_content = &Apache::lonmsg::unpackagemsg($course_content{$crsmsgid});
                   1549:     }
                   1550:     return $general_content{'message'};
                   1551: }
                   1552: 
                   1553: # ---------------------------------------------------------------- Face to face
                   1554: 
                   1555: sub facetoface {
                   1556:     my ($r,$stage)=@_;
1.49      albertel 1557:     if (!&Apache::lonnet::allowed('dff',$env{'request.course.id'})
                   1558: 	&& ! &Apache::lonnet::allowed('dff',$env{'request.course.id'}.
1.1       albertel 1559: 				      '/'.$env{'request.course.sec'})) {
1.50      raeburn  1560: 	$r->print(&mt('Not allowed'));
1.1       albertel 1561: 	return;
                   1562:     }
1.33      albertel 1563:     my $crstype = &Apache::loncommon::course_type();
                   1564:     my $leaders = ($crstype eq 'Group') ? 'coordinators and leaders'
                   1565:                                         : 'faculty and staff';
1.1       albertel 1566:     &printheader($r,
                   1567: 		 '/adm/email?recordftf=query',
                   1568: 		 "User Notes, Face-to-Face, Critical Messages, Broadcast Messages");
                   1569: # from query string
                   1570: 
                   1571:     if ($env{'form.recname'}) { $env{'form.recuname'}=$env{'form.recname'}; }
                   1572:     if ($env{'form.recdom'}) { $env{'form.recdomain'}=$env{'form.recdom'}; }
                   1573: 
                   1574:     my $defdom=$env{'user.domain'};
                   1575: # already filled in
                   1576:     if ($env{'form.recdomain'}) { $defdom=$env{'form.recdomain'}; }
                   1577: # generate output
                   1578:     my $domform = &Apache::loncommon::select_dom_form($defdom,'recdomain');
                   1579:     my $stdbrws = &Apache::loncommon::selectstudent_link
                   1580: 	('stdselect','recuname','recdomain');
                   1581:     my %lt=&Apache::lonlocal::texthash('user' => 'Username',
                   1582: 				       'dom' => 'Domain',
1.30      raeburn  1583: 				       'head' => "User Notes, Records of Face-To-Face Discussions, Critical Messages, and Broadcast Messages in $crstype",
1.1       albertel 1584: 				       'subm' => 'Retrieve discussion and message records',
1.30      raeburn  1585: 				       'newr' => 'New Record (record is visible to '.lc($crstype).' '.$leaders.')',
1.1       albertel 1586: 				       'post' => 'Post this Record');
                   1587:     $r->print(<<"ENDTREC");
                   1588: <h3>$lt{'head'}</h3>
                   1589: <form method="post" action="/adm/email" name="stdselect">
                   1590: <input type="hidden" name="recordftf" value="retrieve" />
                   1591: <table>
                   1592: <tr><td>$lt{'user'}:</td><td><input type="text" size="12" name="recuname" value="$env{'form.recuname'}" /></td>
                   1593: <td rowspan="2">
                   1594: $stdbrws
                   1595: <input type="submit" value="$lt{'subm'}" /></td>
                   1596: </tr>
                   1597: <tr><td>$lt{'dom'}:</td>
                   1598: <td>$domform</td></tr>
                   1599: </table>
                   1600: </form>
                   1601: ENDTREC
                   1602:     if (($stage ne 'query') &&
                   1603:         ($env{'form.recdomain'}) && ($env{'form.recuname'})) {
                   1604:         chomp($env{'form.newrecord'});
                   1605:         if ($env{'form.newrecord'}) {
1.31      albertel 1606: 	    &Apache::lonmsg::store_instructor_comment($env{'form.newrecord'},
                   1607: 						      $env{'form.recuname'},
                   1608: 						      $env{'form.recdomain'});
1.1       albertel 1609:         }
                   1610:         $r->print('<h3>'.&Apache::loncommon::plainname($env{'form.recuname'},
                   1611: 				     $env{'form.recdomain'}).'</h3>');
                   1612:         &disfacetoface($r,$env{'form.recuname'},$env{'form.recdomain'});
                   1613: 	$r->print(<<ENDRHEAD);
                   1614: <form method="post" action="/adm/email">
                   1615: <input name="recdomain" value="$env{'form.recdomain'}" type="hidden" />
                   1616: <input name="recuname" value="$env{'form.recuname'}" type="hidden" />
                   1617: ENDRHEAD
                   1618:         $r->print(<<ENDBFORM);
                   1619: <hr />$lt{'newr'}<br />
                   1620: <textarea name="newrecord" cols="80" rows="10" wrap="hard"></textarea>
                   1621: <br />
                   1622: <input type="hidden" name="recordftf" value="post" />
                   1623: <input type="submit" value="$lt{'post'}" />
                   1624: </form>
                   1625: ENDBFORM
                   1626:     }
                   1627: }
                   1628: 
                   1629: # ----------------------------------------------------------- Blocking during exams
                   1630: 
                   1631: sub examblock {
                   1632:     my ($r,$action) = @_;
                   1633:     unless ($env{'request.course.id'}) { return;}
1.44      raeburn  1634:     if (!&Apache::lonnet::allowed('dcm',$env{'request.course.id'})
                   1635: 	&& ! &Apache::lonnet::allowed('dcm',$env{'request.course.id'}.
1.1       albertel 1636: 				      '/'.$env{'request.course.sec'})) {
                   1637: 	$r->print('Not allowed');
                   1638: 	return;
                   1639:     }
1.34      albertel 1640:     my $usertype = (&Apache::loncommon::course_type() eq 'Group') ? 'members'
1.33      albertel 1641: 	                                                          : 'students';
1.1       albertel 1642:     my %lt=&Apache::lonlocal::texthash(
                   1643:             'comb' => 'Communication Blocking',
                   1644:             'cbds' => 'Communication blocking during scheduled exams',
1.30      raeburn  1645:             'desc' => "You can use communication blocking to prevent $usertype enrolled in this course from displaying LON-CAPA messages sent by other $usertype during an online exam. As blocking of communication could potentially interrupt legitimate communication between $usertype who are also both enrolled in a different LON-CAPA course, please be careful that you select the correct start and end times for your scheduled exam when setting or modifying these parameters.",
1.1       albertel 1646:              'mecb' => 'Modify existing communication blocking periods',
1.50      raeburn  1647:              'ncbc' => 'No communication blocks currently stored',
                   1648:              'stor' => 'Store',
1.1       albertel 1649:     );
                   1650: 
                   1651:     my %ltext = &Apache::lonlocal::texthash(
                   1652:             'dura' => 'Duration',
                   1653:             'setb' => 'Set by',
                   1654:             'even' => 'Event',
1.44      raeburn  1655:             'blck' => 'Blocked?',
1.1       albertel 1656:             'actn' => 'Action',
                   1657:             'star' => 'Start',
                   1658:             'endd' => 'End'
                   1659:     );
                   1660: 
                   1661:     &printheader($r,'/adm/email?block=display',$lt{'comb'});
                   1662:     $r->print('<h3>'.$lt{'cbds'}.'</h3>');
                   1663: 
                   1664:     if ($action eq 'store') {
                   1665:         &blockstore($r);
                   1666:     }
                   1667: 
                   1668:     $r->print($lt{'desc'}.'<br /><br />
                   1669:                <form name="blockform" method="post" action="/adm/email?block=store">
                   1670:              ');
                   1671: 
                   1672:     $r->print('<h4>'.$lt{'mecb'}.'</h4>');
                   1673:     my %records = ();
                   1674:     my $blockcount = 0;
                   1675:     my $parmcount = 0;
                   1676:     &get_blockdates(\%records,\$blockcount);
                   1677:     if ($blockcount > 0) {
                   1678:         $parmcount = &display_blocker_status($r,\%records,\%ltext);
                   1679:     } else {
                   1680:         $r->print($lt{'ncbc'}.'<br /><br />');
                   1681:     }
                   1682:     &display_addblocker_table($r,$parmcount,\%ltext);
                   1683:     my $end_page=&Apache::loncommon::end_page();
                   1684:     $r->print(<<"END");
                   1685: <br />
                   1686: <input type="hidden" name="blocktotal" value="$blockcount" />
1.50      raeburn  1687: <input type ="submit" value="$lt{'stor'}" />
1.1       albertel 1688: </form>
                   1689: $end_page
                   1690: END
                   1691:     return;
                   1692: }
                   1693: 
                   1694: sub blockstore {
                   1695:     my $r = shift;
                   1696:     my %lt=&Apache::lonlocal::texthash(
                   1697:             'tfcm' => 'The following changes were made',
                   1698:             'ncwm' => 'No changes were made.' 
                   1699:     );
                   1700:     my %adds = ();
                   1701:     my %removals = ();
                   1702:     my %cancels = ();
                   1703:     my $modtotal = 0;
                   1704:     my $canceltotal = 0;
                   1705:     my $addtotal = 0;
                   1706:     my %blocking = ();
                   1707:     $r->print('<h3>'.$lt{'head'}.'</h3>');
1.42      raeburn  1708:     foreach my $envkey (keys(%env)) {
1.44      raeburn  1709:         if ($envkey =~ m/^form\.modify_(\d+)$/) {
1.1       albertel 1710:             $adds{$1} = $1;
                   1711:             $removals{$1} = $1;
                   1712:             $modtotal ++;
1.42      raeburn  1713:         } elsif ($envkey =~ m/^form\.cancel_(\d+)$/) {
1.1       albertel 1714:             $cancels{$1} = $1;
                   1715:             unless ( defined($removals{$1}) ) {
                   1716:                 $removals{$1} = $1;
                   1717:                 $canceltotal ++;
                   1718:             }
1.42      raeburn  1719:         } elsif ($envkey =~ m/^form\.add_(\d+)$/) {
1.1       albertel 1720:             $adds{$1} = $1;
                   1721:             $addtotal ++;
1.44      raeburn  1722:         } 
1.1       albertel 1723:     }
                   1724: 
1.42      raeburn  1725:     foreach my $key (keys(%removals)) {
                   1726:         my $hashkey = $env{'form.key_'.$key};
1.1       albertel 1727:         &Apache::lonnet::del('comm_block',["$hashkey"],
                   1728:                          $env{'course.'.$env{'request.course.id'}.'.domain'},
                   1729:                          $env{'course.'.$env{'request.course.id'}.'.num'}
                   1730:                          );
                   1731:     }
1.42      raeburn  1732:     foreach my $key (keys(%adds)) {
                   1733:         unless ( defined($cancels{$key}) ) {
                   1734:             my ($newstart,$newend) = &get_dates_from_form($key);
1.1       albertel 1735:             my $newkey = $newstart.'____'.$newend;
1.44      raeburn  1736:             my $blocktypes = &get_block_choices($key);
                   1737:             $blocking{$newkey} = {
                   1738:                           setter => $env{'user.name'}.':'.$env{'user.domain'},
                   1739:                           event  => &escape($env{'form.title_'.$key}),
                   1740:                           blocks => $blocktypes,
                   1741:                         };
1.1       albertel 1742:         }
                   1743:     }
                   1744:     if ($addtotal + $modtotal > 0) {
                   1745:         &Apache::lonnet::put('comm_block',\%blocking,
                   1746:                      $env{'course.'.$env{'request.course.id'}.'.domain'},
                   1747:                      $env{'course.'.$env{'request.course.id'}.'.num'}
                   1748:                      );
                   1749:     }
                   1750:     my $chgestotal = $canceltotal + $modtotal + $addtotal;
                   1751:     if ($chgestotal > 0) {
                   1752:         $r->print($lt{'tfcm'}.'<ul>');
                   1753:         if ($canceltotal > 0) {
1.50      raeburn  1754:             $r->print('<li>'.&mt('[quant,_1,communication blocking period was,communication blocking periods were] removed.',$canceltotal).'</li>');
1.1       albertel 1755:         }
                   1756:         if ($modtotal > 0) {
1.50      raeburn  1757:             $r->print('<li>'.&mt('[quant,_1,communication blocking period was,communication blocking periods were] modified.',$modtotal).'</li>');
1.1       albertel 1758:         }
                   1759:         if ($addtotal > 0) {
1.50      raeburn  1760:             $r->print('<li>'.&mt('[quant,_1,communication blocking period was,communication blocking periods were] added.',$addtotal).'</li>');
1.1       albertel 1761:         }
                   1762:         $r->print('</ul>');
                   1763:     } else {
                   1764:         $r->print($lt{'ncwm'});
                   1765:     }
                   1766:     $r->print('<br />');
                   1767:     return;
                   1768: }
                   1769: 
                   1770: sub get_dates_from_form {
                   1771:     my $item = shift;
                   1772:     my $startdate = &Apache::lonhtmlcommon::get_date_from_form('startdate_'.$item);
                   1773:     my $enddate   = &Apache::lonhtmlcommon::get_date_from_form('enddate_'.$item);
                   1774:     return ($startdate,$enddate);
                   1775: }
                   1776: 
                   1777: sub get_blockdates {
                   1778:     my ($records,$blockcount) = @_;
                   1779:     $$blockcount = 0;
                   1780:     %{$records} = &Apache::lonnet::dump('comm_block',
                   1781:                          $env{'course.'.$env{'request.course.id'}.'.domain'},
                   1782:                          $env{'course.'.$env{'request.course.id'}.'.num'}
                   1783:                          );
1.15      albertel 1784:     $$blockcount = keys(%{$records});
                   1785: 
                   1786:     if ((keys(%{$records}))[0] =~ /^error: 2 /) {
                   1787: 	$records = {};
                   1788: 	$$blockcount = 0;
1.1       albertel 1789:     }
                   1790: }
                   1791: 
1.44      raeburn  1792: sub get_block_choices {
                   1793:     my $item = shift;
                   1794:     my $blocklist;
                   1795:     my ($typeorder,$types) = &blocktype_text();
                   1796:     foreach my $type (@{$typeorder}) {
                   1797:         if ($env{'form.'.$type.'_'.$item}) {
                   1798:             $blocklist->{$type} = 'on'; 
                   1799:         } else {
                   1800:             $blocklist->{$type} = 'off';
                   1801:         }
                   1802:     }
                   1803:     return $blocklist;
                   1804: }
                   1805: 
1.1       albertel 1806: sub display_blocker_status {
                   1807:     my ($r,$records,$ltext) = @_;
                   1808:     my $parmcount = 0;
1.16      albertel 1809:   
1.1       albertel 1810:     my %lt = &Apache::lonlocal::texthash(
                   1811:         'modi' => 'Modify',
                   1812:         'canc' => 'Cancel',
                   1813:     );
1.44      raeburn  1814:     my ($typeorder,$types) = &blocktype_text();
1.18      albertel 1815:     $r->print(&Apache::loncommon::start_data_table());
1.1       albertel 1816:     $r->print(<<"END");
1.16      albertel 1817:   <tr>
1.44      raeburn  1818:     <th>$ltext->{'dura'}</th>
                   1819:     <th>$ltext->{'setb'}</th>
                   1820:     <th>$ltext->{'even'}</th>
                   1821:     <th>$ltext->{'blck'}</th>
                   1822:     <th>$ltext->{'actn'}?</th>
1.16      albertel 1823:   </tr>
1.1       albertel 1824: END
1.18      albertel 1825:     foreach my $record (sort(keys(%{$records}))) {
1.1       albertel 1826:         my $onchange = 'onFocus="javascript:window.document.forms['.
                   1827:                        "'blockform'].elements['modify_".$parmcount."'].".
                   1828:                        'checked=true;"';
1.18      albertel 1829:         my ($start,$end) = split(/____/,$record);
1.1       albertel 1830:         my $startform = &Apache::lonhtmlcommon::date_setter('blockform','startdate_'.$parmcount,$start,$onchange);
                   1831:         my $endform = &Apache::lonhtmlcommon::date_setter('blockform','enddate_'.$parmcount,$end,$onchange);
1.15      albertel 1832: 	
1.44      raeburn  1833: 	my ($setuname,$setudom,$title,$blocks) = 
                   1834: 	    &Apache::loncommon::parse_block_record($$records{$record});
1.21      albertel 1835: 	$title = &HTML::Entities::encode($title,'"<>&');
1.44      raeburn  1836:         my $settername = 
                   1837:            &Apache::loncommon::aboutmewrapper(
                   1838:                            &Apache::loncommon::plainname($setuname,$setudom),
                   1839:                            $setuname,$setudom);
1.18      albertel 1840:         $r->print(&Apache::loncommon::start_data_table_row());
1.1       albertel 1841:         $r->print(<<"END");
1.44      raeburn  1842:         <td>$ltext->{'star'}:&nbsp;$startform<br/>$ltext->{'endd'}:&nbsp;&nbsp;$endform</td>
1.1       albertel 1843:         <td>$settername</td>
1.18      albertel 1844:         <td><input type="text" name="title_$parmcount" size="15" value="$title" /><input type="hidden" name="key_$parmcount" value="$record" /></td>
1.44      raeburn  1845:         <td>
                   1846: END
                   1847:         foreach my $block (@{$typeorder}) {
                   1848:             my $blockstatus = '';
                   1849:             if ($blocks->{$block} eq 'on') {
                   1850:                 $blockstatus = 'checked="true"';
                   1851:             }
                   1852:             $r->print('<label><input type="checkbox" name="'.$block.'_'.$parmcount.'" '.$blockstatus.' value="1" />'.$types->{$block}.'</label><br />');
                   1853:         }
                   1854:         $r->print(<<"END");
                   1855:         </td>      
1.1       albertel 1856:         <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>
                   1857: END
1.18      albertel 1858:         $r->print(&Apache::loncommon::end_data_table_row());
                   1859:         $parmcount++;
1.1       albertel 1860:     }
                   1861:     $r->print(<<"END");
                   1862: </table>
                   1863: <br />
                   1864: <br />
                   1865: END
                   1866:     return $parmcount;
                   1867: }
                   1868: 
                   1869: sub display_addblocker_table {
                   1870:     my ($r,$parmcount,$ltext) = @_;
                   1871:     my $start = time;
                   1872:     my $end = $start + (60 * 60 * 2); #Default is an exam of 2 hours duration.
                   1873:     my $onchange = 'onFocus="javascript:window.document.forms['.
                   1874:                    "'blockform'].elements['add_".$parmcount."'].".
                   1875:                    'checked=true;"';
                   1876:     my $startform = &Apache::lonhtmlcommon::date_setter('blockform','startdate_'.$parmcount,$start,$onchange);
                   1877:     my $endform = &Apache::lonhtmlcommon::date_setter('blockform','enddate_'.$parmcount,$end,$onchange);
                   1878:     my %lt = &Apache::lonlocal::texthash(
                   1879:         'addb' => 'Add block',
                   1880:         'exam' => 'e.g., Exam 1',
                   1881:         'addn' => 'Add new communication blocking periods'
                   1882:     );
1.44      raeburn  1883:     my ($typeorder,$types) = &blocktype_text();
1.1       albertel 1884:     $r->print(<<"END");
                   1885: <h4>$lt{'addn'}</h4> 
1.18      albertel 1886: END
                   1887:     $r->print(&Apache::loncommon::start_data_table());
                   1888:     $r->print(<<"END");
1.16      albertel 1889:    <tr>
1.44      raeburn  1890:      <th>$ltext->{'dura'}</th>
                   1891:      <th>$ltext->{'even'} $lt{'exam'}</th>
                   1892:      <th>$ltext->{'blck'}</th>
                   1893:      <th>$ltext->{'actn'}?</th>
1.16      albertel 1894:    </tr>
1.18      albertel 1895: END
1.44      raeburn  1896:     $r->print(&Apache::loncommon::start_data_table_row());
1.18      albertel 1897:     $r->print(<<"END");
1.44      raeburn  1898:      <td>$ltext->{'star'}:&nbsp;$startform<br />$ltext->{'endd'}:&nbsp;&nbsp;$endform</td>
1.16      albertel 1899:      <td><input type="text" name="title_$parmcount" size="15" value="" /></td>
1.44      raeburn  1900:      <td>
                   1901: END
                   1902:     foreach my $block (@{$typeorder}) {
                   1903:         $r->print('<label><input type="checkbox" name="'.$block.'_'.$parmcount.'" value="1" />'.$types->{$block}.'</label><br />');
                   1904:      }
                   1905:      $r->print(<<"END");
                   1906:      </td> 
1.16      albertel 1907:      <td><label>$lt{'addb'}?&nbsp;<input type="checkbox" name="add_$parmcount" value="1" /></label></td>
1.1       albertel 1908: END
1.18      albertel 1909:     $r->print(&Apache::loncommon::end_data_table_row());
                   1910:     $r->print(&Apache::loncommon::end_data_table());
1.1       albertel 1911:     return;
                   1912: }
                   1913: 
1.44      raeburn  1914: sub blocktype_text {
                   1915:     my %types = &Apache::lonlocal::texthash(
                   1916:         'com' => 'Messaging',
                   1917:         'chat' => 'Chat',
                   1918:         'boards' => 'Discussion',
                   1919:         'port' => 'Portfolio',
1.45      raeburn  1920:         'groups' => 'Groups',
                   1921:         'blogs' => 'Blogs',
1.18      albertel 1922:     );
1.45      raeburn  1923:     my $typeorder = ['com','chat','boards','port','groups','blogs'];
1.44      raeburn  1924:     return ($typeorder,\%types);
1.1       albertel 1925: }
                   1926: 
                   1927: # ----------------------------------------------------------- Display a message
                   1928: 
                   1929: sub displaymessage {
1.53      raeburn  1930:     my ($r,$msgid,$folder,$msgstatus)=@_;
1.1       albertel 1931:     my $suffix=&Apache::lonmsg::foldersuffix($folder);
                   1932:     my %blocked = ();
                   1933:     my %setters = ();
                   1934:     my $numblocked = 0;
1.33      albertel 1935:     my $crstype = &Apache::loncommon::course_type();
1.30      raeburn  1936: 
1.1       albertel 1937: # info to generate "next" and "previous" buttons and check if message is blocked
1.44      raeburn  1938:     my ($startblock,$endblock) = &Apache::loncommon::blockcheck(\%setters,'com');
1.53      raeburn  1939:     my @messages=&sortedmessages(\%blocked,$startblock,$endblock,\$numblocked,$folder,$msgstatus);
1.1       albertel 1940:     if ( $blocked{$msgid} eq 'ON' ) {
                   1941:         &printheader($r,'/adm/email',&mt('Display a Message'));
                   1942:         $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.'));
                   1943:         &build_block_table($r,$startblock,$endblock,\%setters);
                   1944:         return;
                   1945:     }
                   1946:     &statuschange($msgid,'read',$folder);
                   1947:     my %message=&Apache::lonnet::get('nohist_email'.$suffix,[$msgid]);
                   1948:     my %content=&Apache::lonmsg::unpackagemsg($message{$msgid});
                   1949: 
                   1950:     my $counter=0;
                   1951:     $r->print('<pre>');
1.29      www      1952:     my $escmsgid=&escape($msgid);
1.1       albertel 1953:     foreach (@messages) {
                   1954: 	if ($_->[5] eq $escmsgid){
                   1955: 	    last;
                   1956: 	}
                   1957: 	$counter++;
                   1958:     }
                   1959:     $r->print('</pre>');
                   1960:     my $number_of_messages = scalar(@messages); #subtract 1 for last index
                   1961: # start output
1.29      www      1962:     &printheader($r,'/adm/email?display='.&escape($msgid),'Display a Message','',$content{'baseurl'});
1.1       albertel 1963:     my %courseinfo=&Apache::lonnet::coursedescription($content{'courseid'});
                   1964: # Functions
                   1965:     $r->print('<table border="2" width="100%"><tr bgcolor="#FFFFAA"><td>'.&mt('Functions').':</td>'.
1.29      www      1966: 	      '<td><a href="/adm/email?replyto='.&escape($msgid).$sqs.
1.1       albertel 1967: 	      '"><b>'.&mt('Reply').'</b></a></td>'.
1.29      www      1968: 	      '<td><a href="/adm/email?forward='.&escape($msgid).$sqs.
1.1       albertel 1969: 	      '"><b>'.&mt('Forward').'</b></a></td>'.
1.29      www      1970: 	      '<td><a href="/adm/email?markunread='.&escape($msgid).$sqs.
1.1       albertel 1971: 	      '"><b>'.&mt('Mark Unread').'</b></a></td>'.
1.29      www      1972: 	      '<td><a href="/adm/email?markdel='.&escape($msgid).$sqs.
1.1       albertel 1973: 	      '"><b>'.&mt('Delete').'</b></a></td>'.
                   1974: 	      '<td><a href="/adm/email?'.$sqs.
                   1975: 	      '"><b>'.&mt('Back to Folder Display').'</b></a></td>');
                   1976:     if ($counter > 0){
                   1977: 	$r->print('<td><a href="/adm/email?display='.$messages[$counter-1]->[5].$sqs.
                   1978: 		  '"><b>'.&mt('Previous').'</b></a></td>');
                   1979:     }
                   1980:     if ($counter < $number_of_messages - 1){
                   1981: 	$r->print('<td><a href="/adm/email?display='.$messages[$counter+1]->[5].$sqs.
                   1982: 		  '"><b>'.&mt('Next').'</b></a></td>');
                   1983:     }
                   1984:     $r->print('</tr></table>');
                   1985:     if ($env{'user.adv'}) {
                   1986: 	$r->print('<table border="2" width="100%"><tr bgcolor="#FFAAAA"><td>'.&mt('Currently available actions (will open extra window)').':</td>');
                   1987: 	my $symb=&Apache::lonnet::symbread($content{'baseurl'});      
                   1988: 	if (&Apache::lonnet::allowed('vgr',$env{'request.course.id'})) {
                   1989: 		$r->print('<td><b>'.&Apache::loncommon::track_student_link(&mt('View recent activity'),$content{'sendername'},$content{'senderdomain'},'check').'</b></td>');
                   1990: 	    }
                   1991: 	if (&Apache::lonnet::allowed('opa',$env{'request.course.id'}) && $symb) {
                   1992: 	    $r->print('<td><b>'.&Apache::loncommon::pprmlink(&mt('Set/Change parameters'),$content{'sendername'},$content{'senderdomain'},$symb,'check').'</b></td>');
                   1993: 	}
                   1994: 	if (&Apache::lonnet::allowed('mgr',$env{'request.course.id'}) && $symb) {
                   1995: 	    $r->print('<td><b>'.&Apache::loncommon::pgrdlink(&mt('Set/Change grades'),$content{'sendername'},$content{'senderdomain'},$symb,'check').'</b></td>');
                   1996: 	}
                   1997: 	$r->print('</tr></table>');
                   1998:     }
                   1999:     my $tolist;
                   2000:     my @recipients = ();
                   2001:     for (my $i=0; $i<@{$content{'recuser'}}; $i++) {
                   2002:         $recipients[$i] =  &Apache::loncommon::aboutmewrapper(
                   2003:            &Apache::loncommon::plainname($content{'recuser'}[$i],
                   2004:                                       $content{'recdomain'}[$i]),
                   2005:               $content{'recuser'}[$i],$content{'recdomain'}[$i]).
                   2006:        ' ('.$content{'recuser'}[$i].' at '.$content{'recdomain'}[$i].') ';
                   2007:     }
                   2008:     $tolist = join(', ',@recipients);
                   2009:     $r->print('<br /><b>'.&mt('Subject').':</b> '.$content{'subject'}.
                   2010: 	      ($folder ne 'sent'?'<br /><b>'.&mt('From').':</b> '.
                   2011: 	      &Apache::loncommon::aboutmewrapper(
                   2012: 						 &Apache::loncommon::plainname($content{'sendername'},$content{'senderdomain'}),
                   2013: 						 $content{'sendername'},$content{'senderdomain'}).' ('.
                   2014: 	      $content{'sendername'}.' at '.
                   2015: 	      $content{'senderdomain'}.') ':'<br /><b>'.&mt('To').':</b> '.
                   2016:               $tolist).
1.30      raeburn  2017: 	      ($content{'courseid'}?'<br /><b>'.&mt($crstype).':</b> '.$courseinfo{'description'}.
                   2018: 	       ($content{'coursesec'}?' ('.&mt('Section').': '.$content{'coursesec'}.')':''):'').
1.1       albertel 2019: 	      '<br /><b>'.&mt('Time').':</b> '.$content{'time'}.
                   2020: 	      ($content{'baseurl'}?'<br /><b>'.&mt('Refers to').':</b> <a href="'.$content{'baseurl'}.'">'.
                   2021: 	       $content{'baseurl'}.' ('.&Apache::lonnet::gettitle($content{'baseurl'}).')</a>':'').
                   2022: 	      '<p><pre>'.
                   2023: 	      &Apache::lontexconvert::msgtexconverted($content{'message'},1).
                   2024: 	      '</pre><hr />'.&displayresource(%content).'</p>');
                   2025:     return;   
                   2026: }
                   2027: 
                   2028: # =========================================================== Show the citation
                   2029: 
                   2030: sub displayresource {
                   2031:     my %content=@_;
                   2032: #
                   2033: # If the recipient is in the same course that the message was sent from and
                   2034: # has sufficient privileges, show "all details," else show citation
                   2035: #
                   2036:     if (($env{'request.course.id'} eq $content{'courseid'})
                   2037:      && (&Apache::lonnet::allowed('vgr',$content{'courseid'}))) {
                   2038: 	my $symb=&Apache::lonnet::symbread($content{'baseurl'});
                   2039: # Could not get a symb, give up
                   2040: 	unless ($symb) { return $content{'citation'}; }
                   2041: # Have a symb, can render
                   2042: 	return '<h2>'.&mt('Current attempts of student (if applicable)').'</h2>'.
                   2043: 	    &Apache::loncommon::get_previous_attempt($symb,
                   2044: 						     $content{'sendername'},
                   2045: 						     $content{'senderdomain'},
                   2046: 						     $content{'courseid'}).
                   2047: 	    '<hr /><h2>'.&mt('Current screen output (if applicable)').'</h2>'.
                   2048: 	    &Apache::loncommon::get_student_view($symb,
                   2049: 						 $content{'sendername'},
                   2050: 						 $content{'senderdomain'},
                   2051: 						 $content{'courseid'}).
                   2052: 	    '<h2>'.&mt('Correct Answer(s) (if applicable)').'</h2>'.
                   2053: 	    &Apache::loncommon::get_student_answers($symb,
                   2054: 						    $content{'sendername'},
                   2055: 						    $content{'senderdomain'},
                   2056: 						    $content{'courseid'});
                   2057:     } elsif ($env{'user.adv'}) {
                   2058: 	return $content{'citation'};
                   2059:     }
                   2060:     return '';
                   2061: }
                   2062: 
                   2063: # ================================================================== The Header
                   2064: 
                   2065: sub header {
                   2066:     my ($r,$title,$baseurl)=@_;
                   2067:     
                   2068:     my $extra = &Apache::loncommon::studentbrowser_javascript();
                   2069:     if ($baseurl) {
1.41      albertel 2070: 	$extra .= "<base href=\"".&Apache::lonnet::absolute_url()."/$baseurl\" />";
1.1       albertel 2071:     }
                   2072:     $r->print(&Apache::loncommon::start_page('Communication and Messages',
1.38      raeburn  2073:  					$extra));
1.1       albertel 2074:     $r->print(&Apache::lonhtmlcommon::breadcrumbs
1.38      raeburn  2075:      		(($title?$title:'Communication and Messages')));
1.1       albertel 2076: }
                   2077: 
                   2078: # ---------------------------------------------------------------- Print header
                   2079: 
                   2080: sub printheader {
                   2081:     my ($r,$url,$desc,$title,$baseurl)=@_;
                   2082:     &Apache::lonhtmlcommon::add_breadcrumb
                   2083: 	({href=>$url,
                   2084: 	  text=>$desc});
                   2085:     &header($r,$title,$baseurl);
                   2086: }
                   2087: 
                   2088: # ------------------------------------------------------------ Store the comment
                   2089: 
                   2090: sub storecomment {
                   2091:     my ($r)=@_;
                   2092:     my $msgtxt=&Apache::lonfeedback::clear_out_html($env{'form.message'});
                   2093:     my $cleanmsgtxt='';
1.42      raeburn  2094:     foreach my $line (split(/[\n\r]/,$msgtxt)) {
                   2095: 	unless ($line=~/^\s*(\>|\&gt\;)/) {
                   2096: 	    $cleanmsgtxt.=$line."\n";
1.1       albertel 2097: 	}
                   2098:     }
1.29      www      2099:     my $key=&escape($env{'form.baseurl'}).'___'.time;
1.1       albertel 2100:     &Apache::lonnet::put('nohist_stored_comments',{ $key => $cleanmsgtxt });
                   2101: }
                   2102: 
                   2103: sub storedcommentlisting {
                   2104:     my ($r)=@_;
                   2105:     my %msgs=&Apache::lonnet::dump('nohist_stored_comments',undef,undef,
1.29      www      2106:        '^'.&escape(&escape($env{'form.showcommentbaseurl'})));
1.1       albertel 2107:     $r->print(&Apache::loncommon::start_page('Stored Comment Listing',undef,
                   2108: 					     {'onlybody' => 1}));
                   2109:     if ((keys %msgs)[0]=~/^error\:/) {
                   2110: 	$r->print(&mt('No stored comments yet.'));
                   2111:     } else {
                   2112: 	my $found=0;
1.42      raeburn  2113: 	foreach my $key (sort(keys(%msgs))) {
                   2114: 	    $r->print("\n".$msgs{$key}."<hr />");
1.1       albertel 2115: 	    $found=1;
                   2116: 	}
                   2117: 	unless ($found) {
                   2118: 	    $r->print(&mt('No stored comments yet for this resource.'));
                   2119: 	}
                   2120:     }
                   2121: }
                   2122: 
                   2123: # ---------------------------------------------------------------- Send an email
                   2124: 
                   2125: sub sendoffmail {
                   2126:     my ($r,$folder)=@_;
                   2127:     my $suffix=&Apache::lonmsg::foldersuffix($folder);
                   2128:     my $sendstatus='';
                   2129:     my %specialmsg_status;
                   2130:     my $numspecial = 0;
1.38      raeburn  2131:     my ($cdom,$cnum,$group);
                   2132:     if (exists($env{'form.group'})) {
                   2133:         $group = $env{'form.group'};
                   2134:     }
                   2135:     if (exists($env{'request.course.id'})) {
                   2136:         $cnum = $env{'course.'.$env{'request.course.id'}.'.num'};
                   2137:         $cdom = $env{'course.'.$env{'request.course.id'}.'.domain'};
                   2138:     }
1.1       albertel 2139:     if ($env{'form.send'}) {
1.53      raeburn  2140:         if (!$env{'form.multiforward'}) { 
                   2141:             if ($group eq '') {
                   2142: 	        &printheader($r,'','Messages being sent.');
                   2143:             } else {
                   2144:                 $r->print(&groupmail_header('sending',$group));
                   2145:             }
1.38      raeburn  2146:         }
1.1       albertel 2147: 	$r->rflush();
                   2148: 	my %content=();
                   2149: 	undef %content;
                   2150: 	if ($env{'form.forwid'}) {
                   2151: 	    my $msgid=$env{'form.forwid'};
                   2152: 	    my %message=&Apache::lonnet::get('nohist_email'.$suffix,[$msgid]);
                   2153: 	    %content=&Apache::lonmsg::unpackagemsg($message{$msgid},1);
                   2154: 	    &statuschange($msgid,'forwarded',$folder);
                   2155: 	    $env{'form.message'}.="\n\n-- Forwarded message --\n\n".
                   2156: 		$content{'message'};
                   2157: 	}
                   2158: 	if ($env{'form.replyid'}) {
                   2159: 	    my $msgid=$env{'form.replyid'};
                   2160: 	    my %message=&Apache::lonnet::get('nohist_email'.$suffix,[$msgid]);
                   2161: 	    %content=&Apache::lonmsg::unpackagemsg($message{$msgid},1);
                   2162: 	    &statuschange($msgid,'replied',$folder);
                   2163: 	}
1.13      albertel 2164: 
1.35      albertel 2165: 	my @to =
1.37      raeburn  2166: 	    &Apache::loncommon::get_env_multiple('form.selectedusers_forminput');
1.25      foxr     2167: 	my $mode = $env{'form.sendmode'};
                   2168: 
1.13      albertel 2169: 	my %toaddr;
1.35      albertel 2170: 	if (@to) {
                   2171: 	    foreach my $dest (@to) {
1.27      albertel 2172: 		my ($user,$domain) = split(/:/, $dest);
1.25      foxr     2173: 		if (($user ne '') && ($domain ne '')) {
                   2174: 		    my $address = $user.":".$domain; # How the code below expects it.
                   2175: 		    $toaddr{$address} = '';
                   2176: 		}
                   2177: 	    }
                   2178: 	}
                   2179: 
1.1       albertel 2180: 	if ($env{'form.sendmode'} eq 'group') {
1.25      foxr     2181: 	     foreach my $address (keys(%env)) {
                   2182: 	 	if ($address=~/^form\.send\_to\_\&\&\&[^\&]*\&\&\&\_(.+)$/) {
                   2183: 	 	    $toaddr{$1}='';
                   2184: 	 	}
1.1       albertel 2185: 	    }
                   2186: 	} elsif ($env{'form.sendmode'} eq 'upload') {
1.13      albertel 2187: 	    foreach my $line (split(/[\n\r\f]+/,$env{'form.upfile'})) {
1.43      raeburn  2188:                 my ($rec,$txt) = ($line =~ /^([^:]+:[^:]+):(.*)$/);
1.1       albertel 2189: 		if ($txt) {
1.43      raeburn  2190:                     $rec =~ s/^\s+//;
1.44      raeburn  2191:                     $rec =~ s/\s+$//;
1.1       albertel 2192: 		    $toaddr{$rec}.=$txt."\n";
                   2193: 		}
                   2194: 	    }
                   2195: 	} else {
1.25      foxr     2196: 	    if (($env{'form.recuname'} ne '') && ($env{'form.recdomain'} ne '')) {
                   2197: 		$toaddr{$env{'form.recuname'}.':'.$env{'form.recdomain'}}='';
                   2198: 	    }
1.1       albertel 2199: 	}
                   2200: 	if ($env{'form.additionalrec'}) {
1.42      raeburn  2201: 	    foreach my $rec (split(/\,/,$env{'form.additionalrec'})) {
1.43      raeburn  2202: 		my ($auname,$audom)=split(/:/,$rec);
1.25      foxr     2203: 		if (($auname ne "") && ($audom ne "")) {
                   2204: 		    $toaddr{$auname.':'.$audom}='';
                   2205: 		}
1.1       albertel 2206: 	    }
                   2207: 	}
                   2208: 
                   2209:         my $savemsg;
                   2210:         my $msgtype;
                   2211:         my %sentmessage;
1.7       albertel 2212:         my $msgsubj=&Apache::lonfeedback::clear_out_html($env{'form.subject'},
                   2213: 							 undef,1);
1.1       albertel 2214:         if ((($env{'form.critmsg'}) || ($env{'form.sendbck'})) &&
                   2215:             (&Apache::lonnet::allowed('srm',$env{'request.course.id'})
                   2216: 	     || &Apache::lonnet::allowed('srm',$env{'request.course.id'}.
                   2217: 					 '/'.$env{'request.course.sec'})
                   2218: 	     )) {
                   2219:             $savemsg=&Apache::lonfeedback::clear_out_html($env{'form.message'},1);
                   2220:             $msgtype = 'critical';
                   2221:         } else {
                   2222:             $savemsg=&Apache::lonfeedback::clear_out_html($env{'form.message'});
                   2223:         }
                   2224: 	
1.13      albertel 2225: 	foreach my $address (sort(keys(%toaddr))) {
                   2226: 	    my ($recuname,$recdomain)=split(/\:/,$address);
1.1       albertel 2227:             my $msgtxt = $savemsg;
1.13      albertel 2228: 	    if ($toaddr{$address}) { $msgtxt.='<hr />'.$toaddr{$address}; }
1.12      albertel 2229: 	    my @thismsg;
1.1       albertel 2230: 	    if ((($env{'form.critmsg'}) || ($env{'form.sendbck'})) && 
                   2231: 		(&Apache::lonnet::allowed('srm',$env{'request.course.id'})
                   2232: 		 || &Apache::lonnet::allowed('srm',$env{'request.course.id'}.
                   2233: 					     '/'.$env{'request.course.sec'}))) {
1.13      albertel 2234: 		$r->print(&mt('Sending critical message').' '.$recuname.':'.$recdomain.': ');
                   2235: 		@thismsg=
                   2236: 		    &Apache::lonmsg::user_crit_msg($recuname,$recdomain,
                   2237: 						   $msgsubj,$msgtxt,
                   2238: 						   $env{'form.sendbck'},
                   2239: 						   $env{'form.permanent'},
                   2240: 						   \$sentmessage{$address});
1.1       albertel 2241: 	    } else {
1.14      albertel 2242: 		$r->print(&mt('Sending').' '.$recuname.':'.$recdomain.': ');
1.13      albertel 2243: 		@thismsg=
                   2244: 		    &Apache::lonmsg::user_normal_msg($recuname,$recdomain,
                   2245: 						     $msgsubj,$msgtxt,
                   2246: 						     $content{'citation'},
                   2247: 						     undef,undef,
                   2248: 						     $env{'form.permanent'},
                   2249: 						     \$sentmessage{$address});
1.1       albertel 2250:             }
                   2251: 	    if (($env{'request.course.id'}) && (($msgtype eq 'critical') || 
                   2252:                                          ($env{'form.sendmode'} eq 'group'))) {
1.12      albertel 2253: 	        $specialmsg_status{$recuname.':'.$recdomain} =
                   2254: 		    join(' ',@thismsg);
                   2255: 		foreach my $result (@thismsg) {
                   2256: 		    if ($result eq 'ok') {
                   2257: 			$numspecial++;
                   2258: 		    }
                   2259: 		}
1.1       albertel 2260: 	    }
1.12      albertel 2261: 	    $sendstatus.=' '.join(' ',@thismsg);
1.1       albertel 2262: 	}
                   2263:         if (($env{'request.course.id'}) && (($env{'form.sendmode'} eq 'group')
                   2264:                                               || ($msgtype eq 'critical'))) {
                   2265:             my $subj_prefix;
                   2266:             if ($msgtype eq 'critical') {
                   2267:                 $subj_prefix = 'Critical.';
                   2268:             } else {
                   2269:                 $subj_prefix = 'Broadcast.';
                   2270:             }
                   2271:             my ($specialmsgid,$specialresult);
1.29      www      2272:             my $course_str = &escape('['.$cnum.':'.$cdom.']');
1.1       albertel 2273: 
                   2274:             if ($numspecial) {
                   2275:                 $specialresult = &Apache::lonmsg::user_normal_msg_raw($cnum,$cdom,$subj_prefix.
                   2276:                     ' '.$course_str,$savemsg,undef,undef,undef,
                   2277:                     undef,undef,\$specialmsgid);
1.29      www      2278:                 $specialmsgid = &unescape($specialmsgid);
1.1       albertel 2279:             }
                   2280:             if ($specialresult eq 'ok') {
                   2281:                 my $record_sent;
1.13      albertel 2282:                 my @recusers;
                   2283:                 my @recudoms;
                   2284:                 my ($stamp,$crssubj,$msgname,$msgdom,$msgcount,$context,$pid) =
1.29      www      2285: 		    split(/\:/,&unescape($specialmsgid));
1.13      albertel 2286: 
1.1       albertel 2287:                 foreach my $recipient (sort(keys(%toaddr))) {
                   2288:                     if ($specialmsg_status{$recipient} eq 'ok') {
                   2289:                         my $usersubj = $subj_prefix.'['.$recipient.']';
                   2290:                         my $usermsgid = 
                   2291: 			    &Apache::lonmsg::buildmsgid($stamp,$usersubj,
                   2292: 							$msgname,$msgdom,
                   2293: 							$msgcount,$context,
                   2294: 							$pid);
                   2295:                         &Apache::lonmsg::user_normal_msg_raw($cnum,$cdom,$subj_prefix.
                   2296:                                              ' ['.$recipient.']',$msgsubj,undef,
                   2297:                         undef,undef,undef,$usermsgid,undef,undef,$specialmsgid);
1.13      albertel 2298:                         my ($uname,$udom) = split(/:/,$recipient);
1.1       albertel 2299:                         push(@recusers,$uname);
                   2300:                         push(@recudoms,$udom);
                   2301:                     }
                   2302:                 }
                   2303:                 if (@recusers) {
                   2304:                     my $specialmessage;
1.13      albertel 2305:                     my $sentsubj = 
                   2306: 			$subj_prefix.' ('.$numspecial.' sent) '.$msgsubj;
1.1       albertel 2307:                     $sentsubj = &HTML::Entities::encode($sentsubj,'<>&"');
                   2308:                     my $sentmsgid = 
                   2309: 			&Apache::lonmsg::buildmsgid($stamp,$sentsubj,$msgname,
                   2310: 						    $msgdom,$msgcount,$context,
                   2311: 						    $pid);
                   2312:                     ($specialmsgid,$specialmessage) = &Apache::lonmsg::packagemsg($msgsubj,$savemsg,
                   2313:                             undef,undef,undef,\@recusers,\@recudoms,$sentmsgid);
                   2314:                     $record_sent = &Apache::lonmsg::store_sent_mail($specialmsgid,$specialmessage);
                   2315:                 }
                   2316:             } else {
                   2317:                 &Apache::lonnet::logthis('Failed to create record of critical message or broadcast in '.$env{'course.'.$env{'request.course.id'}.'.num'}.' at '.$env{'course.'.$env{'request.course.id'}.'.domain'}.' - no msgid generated');
                   2318:             }
                   2319:         }
                   2320:     } else {
                   2321: 	&printheader($r,'','No messages sent.'); 
                   2322:     }
1.53      raeburn  2323:     if (!$env{'form.multiforward'}) { 
                   2324:         if ($sendstatus=~/^(\s*(?:ok|con_delayed)\s*)*$/) {
                   2325: 	    $r->print('<br /><span class="LC_success">'.&mt('Completed.').
                   2326:                       '</span>');
                   2327: 	    if ($env{'form.displayedcrit'}) {
                   2328: 	        &discrit($r);
                   2329:             }
                   2330:             if ($group ne '') {
                   2331:                 $r->print(&groupmail_sent($group,$cdom,$cnum)); 
                   2332: 	    } else {
                   2333: 	        &Apache::loncommunicate::menu($r);
                   2334: 	    }
                   2335:         } else {
                   2336: 	    $r->print('<p><span class="LC_error">'.&mt('Could not deliver message').'</span> '.
                   2337: 		  &mt('Please use the browser "Back" button and correct the recipient addresses '."($sendstatus)").'</p>');
1.38      raeburn  2338:         }
1.1       albertel 2339:     }
1.53      raeburn  2340:     return $sendstatus;
1.1       albertel 2341: }
                   2342: 
                   2343: # ===================================================================== Handler
                   2344: 
                   2345: sub handler {
                   2346:     my $r=shift;
                   2347: 
                   2348: # ----------------------------------------------------------- Set document type
                   2349:     
                   2350:     &Apache::loncommon::content_type($r,'text/html');
                   2351:     $r->send_http_header;
                   2352:     
                   2353:     return OK if $r->header_only;
                   2354:     
                   2355: # --------------------------- Get query string for limited number of parameters
                   2356:     &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'},
                   2357:         ['display','replyto','forward','markread','markdel','markunread',
                   2358:          'sendreply','compose','sendmail','critical','recname','recdom',
                   2359:          'recordftf','sortedby','block','folder','startdis','interdis',
1.53      raeburn  2360: 	 'showcommentbaseurl','dismode','group','subject','text','ref',
                   2361:          'msgstatus']);
1.1       albertel 2362:     $sqs='&sortedby='.$env{'form.sortedby'};
                   2363: 
                   2364: # ------------------------------------------------------ They checked for email
                   2365:     unless ($env{'form.block'}) {
                   2366:         &Apache::lonnet::put('email_status',{'recnewemail'=>0});
                   2367:     }
                   2368: 
                   2369: # ----------------------------------------------------------------- Breadcrumbs
                   2370: 
                   2371:     &Apache::lonhtmlcommon::clear_breadcrumbs();
                   2372:     &Apache::lonhtmlcommon::add_breadcrumb
                   2373:         ({href=>"/adm/communicate",
                   2374:           text=>"Communication/Messages",
                   2375:           faq=>12,bug=>'Communication Tools',});
                   2376: 
                   2377: # ------------------------------------------------------------------ Get Folder
                   2378: 
                   2379:     my $folder=$env{'form.folder'};
                   2380:     unless ($folder) { 
                   2381: 	$folder=''; 
                   2382:     } else {
1.29      www      2383: 	$sqs.='&folder='.&escape($folder);
1.1       albertel 2384:     }
                   2385: # ------------------------------------------------------------ Get Display Mode
                   2386: 
                   2387:     my $dismode=$env{'form.dismode'};
                   2388:     unless ($dismode) { 
                   2389: 	$dismode=''; 
                   2390:     } else {
1.48      albertel 2391: 	$sqs.='&amp;dismode='.&escape($dismode);
1.1       albertel 2392:     }
                   2393: 
                   2394: # --------------------------------------------------------------------- Display
1.53      raeburn  2395:     my $msgstatus = $env{'form.msgstatus'};
1.1       albertel 2396:     $startdis=$env{'form.startdis'};
1.53      raeburn  2397:     if ($startdis ne '') {
                   2398:         $startdis--;
                   2399:     }
1.1       albertel 2400:     unless ($startdis) { $startdis=0; }
                   2401: 
                   2402:     $interdis=$env{'form.interdis'};
                   2403:     unless ($interdis) { $interdis=20; }
                   2404:     $sqs.='&interdis='.$interdis;
                   2405: 
                   2406:     if ($env{'form.firstview'}) {
                   2407: 	$startdis=0;
                   2408:     }
                   2409:     if ($env{'form.lastview'}) {
                   2410: 	$startdis=-1;
                   2411:     }
                   2412:     if ($env{'form.prevview'}) {
                   2413: 	$startdis--;
                   2414:     }
                   2415:     if ($env{'form.nextview'}) {
                   2416: 	$startdis++;
                   2417:     }
                   2418:     my $postedstartdis=$startdis+1;
                   2419:     $sqs.='&startdis='.$postedstartdis;
                   2420: 
                   2421: # --------------------------------------------------------------- Render Output
                   2422: 
                   2423:     if ($env{'form.display'}) {
1.53      raeburn  2424: 	&displaymessage($r,$env{'form.display'},$folder,$msgstatus);
1.1       albertel 2425:     } elsif ($env{'form.replyto'}) {
                   2426: 	&compout($r,'',$env{'form.replyto'},undef,undef,$folder,$dismode);
                   2427:     } elsif ($env{'form.confirm'}) {
                   2428: 	&printheader($r,'','Confirmed Receipt');
1.36      albertel 2429: 	my $replying = 0;
1.42      raeburn  2430: 	foreach my $envkey (keys(%env)) {
                   2431: 	    if ($envkey=~/^form\.rec\_(.*)$/) {
1.1       albertel 2432: 		$r->print('<b>'.&mt('Confirming Receipt').':</b> '.
                   2433: 			  &Apache::lonmsg::user_crit_received($1).'<br>');
                   2434: 	    }
1.42      raeburn  2435: 	    if ($envkey=~/^form\.reprec\_(.*)$/) {
1.1       albertel 2436: 		my $msgid=$1;
                   2437: 		$r->print('<b>'.&mt('Confirming Receipt').':</b> '.
                   2438: 			  &Apache::lonmsg::user_crit_received($msgid).'<br>');
                   2439: 		&compout($r,'','','',$msgid);
1.36      albertel 2440: 		$replying = 1;
1.1       albertel 2441: 	    }
                   2442: 	}
1.36      albertel 2443: 	if (!$replying) {
                   2444: 	    &discrit($r);
                   2445: 	}
1.1       albertel 2446:     } elsif ($env{'form.critical'}) {
                   2447: 	&printheader($r,'','Displaying Critical Messages');
                   2448: 	&discrit($r);
                   2449:     } elsif ($env{'form.forward'}) {
                   2450: 	&compout($r,$env{'form.forward'},undef,undef,undef,$folder);
                   2451:     } elsif ($env{'form.markdel'}) {
                   2452: 	&printheader($r,'','Deleted Message');
1.9       albertel 2453: 	my ($result,$msg) = 
                   2454: 	    &statuschange($env{'form.markdel'},'deleted',$folder);
                   2455: 	if (!$result) {
1.10      albertel 2456: 	    $r->print('<p class="LC_error">'.
                   2457: 		      &mt('Failed to delete the message.').'</p>'.
1.9       albertel 2458: 		      '<p class="LC_error">'.$msg."</p>\n");
                   2459: 	}
1.1       albertel 2460: 	&Apache::loncommunicate::menu($r);
1.53      raeburn  2461: 	&disall($r,($folder?$folder:$dismode),$msgstatus);
                   2462:     } elsif ($env{'form.markedaction'} eq 'markedforward') {
                   2463:         my $total = 0;
                   2464:         my @to_forward = &Apache::loncommon::get_env_multiple('form.delmark');
                   2465:         foreach my $msgid (@to_forward) {
                   2466:             &statuschange(&unescape($msgid),'forwarded',$folder);
                   2467:             $total ++;
                   2468:         }
                   2469:         if ($total > 0) {
                   2470:             &compout($r,undef,undef,undef,undef,$folder,$dismode,$total);
                   2471:         }
                   2472:     } elsif ($env{'form.markedaction'} eq 'markedread') {
                   2473:         my $total = 0;
                   2474:         my @to_markread = &Apache::loncommon::get_env_multiple('form.delmark');
                   2475:         foreach my $msgid (@to_markread) {
                   2476:             &statuschange(&unescape($msgid),'read',$folder);
                   2477:             $total ++;
                   2478:         }
                   2479:         &printheader($r,'','Marked Messages Read');
                   2480:         $r->print(&mt('Marked [_1] message(s) read',$total).'<p>');
                   2481:         &Apache::loncommunicate::menu($r);
                   2482:         &disall($r,($folder?$folder:$dismode),$msgstatus);
                   2483:     } elsif ($env{'form.markedaction'} eq 'markedunread') {
                   2484:         my $total = 0;
                   2485:         my @to_markunread = &Apache::loncommon::get_env_multiple('form.delmark');
                   2486:         foreach my $msgid (@to_markunread) {
                   2487:             &statuschange(&unescape($msgid),'new',$folder);
                   2488:             $total ++;
                   2489:         }
                   2490:         &printheader($r,'','Marked Messages Unread');
                   2491:         $r->print(&mt('Marked [_1] message(s) unread',$total).'<p>');
                   2492:         &Apache::loncommunicate::menu($r);
                   2493:         &disall($r,($folder?$folder:$dismode),$msgstatus);
                   2494:     } elsif ($env{'form.markedaction'} eq 'markedmove') {
                   2495:         my $destfolder = $env{'form.movetofolder'};
                   2496:         my %gotfolders = &Apache::lonmsg::get_user_folders();
                   2497:         &printheader($r,'','Moved Messages');
                   2498:         if (!defined($gotfolders{$destfolder})) {
                   2499:             $r->print(&mt('Destination folder [_1] is not a valid folder',
                   2500:                       $destfolder));
                   2501:         } else {
                   2502: 	    my ($total,$failed,@failed_msg)=(0,0);
                   2503:             my @to_move = &Apache::loncommon::get_env_multiple('form.delmark');
                   2504:             foreach my $msgid (@to_move) {
                   2505: 	        my ($result,$msg) = &movemsg(&unescape($msgid),$folder,
                   2506: 			                     $env{'form.movetofolder'});
                   2507: 	        if ($result) {
1.9       albertel 2508: 		    $total++;
1.53      raeburn  2509: 	        } else {
1.9       albertel 2510: 		    $failed++;
                   2511: 		    push(@failed_msg,$msg);
1.53      raeburn  2512: 	        }
1.1       albertel 2513: 	    }
1.53      raeburn  2514: 	    if ($failed) {
                   2515: 	        $r->print('<p class="LC_error">
1.10      albertel 2516:                           '.&mt('Failed to move [_1] message(s)',$failed).
                   2517: 		      '</p>');
1.53      raeburn  2518: 	        $r->print('<p class="LC_error">'.
                   2519: 	   	          join("</p>\n<p class=\"LC_error\">",@failed_msg).
                   2520: 		          "</p>\n");
                   2521: 	    }
                   2522: 	    $r->print(&mt('Moved [_1] message(s)',$total).'<p>');
                   2523:         }
1.1       albertel 2524: 	&Apache::loncommunicate::menu($r);
1.53      raeburn  2525: 	&disall($r,($folder?$folder:$dismode),$msgstatus);
                   2526:     } elsif ($env{'form.markedaction'} eq 'markeddel') {
1.9       albertel 2527: 	my ($total,$failed,@failed_msg)=(0,0);
1.53      raeburn  2528:         my @to_delete = &Apache::loncommon::get_env_multiple('form.delmark');
                   2529:         foreach my $msgid (@to_delete) {
                   2530: 	    my ($result,$msg) = &statuschange(&unescape($msgid),'deleted', 
                   2531: 				              $folder);
                   2532: 	    if ($result) {
                   2533: 	        $total++;
                   2534: 	    } else {
                   2535: 	        $failed++;
                   2536: 		push(@failed_msg,$msg);
1.1       albertel 2537: 	    }
                   2538: 	}
                   2539: 	&printheader($r,'','Deleted Messages');
1.9       albertel 2540: 	if ($failed) {
                   2541: 	    $r->print('<p class="LC_error">
1.10      albertel 2542:                           '.&mt('Failed to delete [_1] message(s)',$failed).
                   2543: 		      '</p>');
1.9       albertel 2544: 	    $r->print('<p class="LC_error">'.
                   2545: 		      join("</p>\n<p class=\"LC_error\">",@failed_msg).
                   2546: 		      "</p>\n");
                   2547: 	}
1.10      albertel 2548: 	$r->print(&mt('Deleted [_1] message(s)',$total).'<p>');
1.1       albertel 2549: 	&Apache::loncommunicate::menu($r);
1.53      raeburn  2550: 	&disall($r,($folder?$folder:$dismode),$msgstatus);
1.1       albertel 2551:     } elsif ($env{'form.markunread'}) {
                   2552: 	&printheader($r,'','Marked Message as Unread');
                   2553: 	&statuschange($env{'form.markunread'},'new');
                   2554: 	&Apache::loncommunicate::menu($r);
1.53      raeburn  2555: 	&disall($r,($folder?$folder:$dismode),$msgstatus);
1.1       albertel 2556:     } elsif ($env{'form.compose'}) {
                   2557: 	&compout($r,'','',$env{'form.compose'});
                   2558:     } elsif ($env{'form.recordftf'}) {
                   2559: 	&facetoface($r,$env{'form.recordftf'});
                   2560:     } elsif ($env{'form.block'}) {
                   2561:         &examblock($r,$env{'form.block'});
                   2562:     } elsif ($env{'form.sendmail'}) {
1.53      raeburn  2563:         if ($env{'form.multiforward'}) {
                   2564:             &printheader($r,'','Messages being sent.');
                   2565:             my $fixed_subj = $env{'form.subject'};
                   2566:             my $suffix=&Apache::lonmsg::foldersuffix($folder);
                   2567:             my (%sendresult,%forwardok,%forwardfail,$fwdcount);
                   2568:             my @to_forward = &Apache::loncommon::get_env_multiple('form.delmark');
                   2569:             foreach my $item (@to_forward) {
                   2570:                 my $msgid=&unescape($item);
                   2571:                 my %message=&Apache::lonnet::get('nohist_email'.$suffix,[$msgid]);
                   2572:                 my %content=&Apache::lonmsg::unpackagemsg($message{$msgid},1);
                   2573:                 if ($env{'form.showorigsubj'}) {
                   2574:                     $env{'form.subject'} = $fixed_subj.$content{'subject'};
                   2575:                 } else {
                   2576:                     $env{'form.subject'} = '';
                   2577:                 }
                   2578:                 my $uname = $content{'sendername'};
                   2579:                 my $udom = $content{'senderdomain'};
                   2580:                 &statuschange($msgid,'forwarded',$folder);
                   2581:                 if ($env{'form.showorigsender'}) {
                   2582:                     $env{'form.message'} = $env{'form.msgheader'}.' '.
                   2583:                         &Apache::loncommon::plainname($uname,$udom).' ('.
                   2584:                                            $uname.':'.$udom.')';
                   2585:                 }
                   2586:                 $env{'form.message'} .= "\n\n-- Forwarded message --\n\n".
                   2587:                                         $content{'message'};
                   2588:                 $fwdcount ++;
                   2589:                 $r->print($fwdcount.': '); 
                   2590:                 $sendresult{$msgid} = &sendoffmail($r,$folder);
                   2591:                 $r->print('<br />');
                   2592:             }
                   2593:             foreach my $key (keys(%sendresult)) {
                   2594:                 if ($sendresult{$key} =~/^(\s*(?:ok|con_delayed)\s*)*$/) {
                   2595:                     $forwardok{$key} = $sendresult{$key};
                   2596:                 } else {
                   2597:                     $forwardfail{$key} = $sendresult{$key}; 
                   2598:                 }
                   2599:             }
                   2600:             if (keys(%forwardok) > 0) {
                   2601:                 my $count = keys(%forwardok);
                   2602:                 $r->print('<br /><span class="LC_success">'.
                   2603:                           &mt('[quant,_1,message] forwarded.',$count).
                   2604:                           '</span>');
                   2605:             }
                   2606:             if (keys(%forwardfail) > 0) {
                   2607:                 my $count = keys(%forwardfail);
                   2608:                 $r->print('<p><span class="LC_error">'.
                   2609:                           &mt('Could not forward [quant,_1,message].',$count).
                   2610:                           '</span> ');
                   2611:                 foreach my $key (keys(%forwardfail)) {
                   2612:                     $r->print(&mt('Could not deliver forwarded message.').'</span> '.
                   2613:                               &mt('The recipient addresses may need to be corrected').' ('.$forwardfail{$key}.').<br /><br />');
                   2614:                 }
                   2615:             }
                   2616:             &Apache::loncommunicate::menu($r);
                   2617:         } else {
                   2618: 	    &sendoffmail($r,$folder);
                   2619:         }
1.1       albertel 2620: 	if ($env{'form.storebasecomment'}) {
                   2621: 	    &storecomment($r);
1.38      raeburn  2622:         }
1.1       albertel 2623: 	if (($env{'form.rsspost'}) && ($env{'request.course.id'})) {
1.38      raeburn  2624: 	        &Apache::lonrss::addentry($env{'course.'.$env{'request.course.id'}.'.num'},
1.1       albertel 2625: 				      $env{'course.'.$env{'request.course.id'}.'.domain'},
                   2626: 				      'Course_Announcements',
                   2627: 				      $env{'form.subject'},
                   2628: 				      $env{'form.message'},'/adm/communicate','public');
                   2629: 	}
1.38      raeburn  2630: 	if ((!exists($env{'form.group'})) && (!$env{'form.displayedcrit'})) {
1.53      raeburn  2631: 	    &disall($r,($folder?$folder:$dismode),$msgstatus);
1.36      albertel 2632: 	}
1.1       albertel 2633:     } elsif ($env{'form.newfolder'}) {
                   2634: 	&printheader($r,'','New Folder');
1.46      raeburn  2635:         my $showfolder = $env{'form.newfolder'};
                   2636: 	my ($makeresult,$warning) = &makefolder($env{'form.newfolder'});
                   2637:         if ($makeresult eq 'ok') {
                   2638:             $r->print(&mt('Mail folder "[_1]" created.',$showfolder).'<br />');
                   2639:         } else {
                   2640:             $r->print(&mt('Creation failed.').' '.$makeresult.'<br />'.
                   2641:                       $warning);
                   2642:             $showfolder = $folder;
                   2643:         }
                   2644:         &Apache::loncommunicate::menu($r);
1.53      raeburn  2645: 	&disall($r,$showfolder,$msgstatus);
1.1       albertel 2646:     } elsif ($env{'form.showcommentbaseurl'}) {
                   2647: 	&storedcommentlisting($r);
1.46      raeburn  2648:     } elsif ($env{'form.folderaction'} eq 'delete') {
                   2649:         &printheader($r,'','Deleted Folder');
                   2650:         my $showfolder = '';
                   2651:         my $delresult = &deletefolder($folder);
                   2652:         if ($delresult eq 'ok') {
                   2653:             $r->print(&mt('Mail folder "[_1]" deleted.',$folder).'<br />');
                   2654:         } else {
                   2655:             $r->print(&mt('Deletion failed.').' '.$delresult.'<br />');
                   2656:             $showfolder = $folder;
                   2657:         }
                   2658:         &Apache::loncommunicate::menu($r);
1.53      raeburn  2659:         &disall($r,$showfolder,$msgstatus);
1.46      raeburn  2660:     } elsif ($env{'form.folderaction'} eq 'rename') {
                   2661:         &printheader($r,'','Renamed Folder');
                   2662:         my $showfolder = $env{'form.renamed'};
                   2663:         my $renresult = &renamefolder($folder);
                   2664:         if ($renresult eq 'ok') {
                   2665:             $r->print(&mt('Mail folder "[_1]" renamed "[_2]".',$folder,$showfolder).'<br />');
                   2666:         } else {
                   2667:             $r->print(&mt('Renaming failed.').' '.$renresult.'<br />');
                   2668:             $showfolder = $folder;
                   2669:         }
                   2670:         &Apache::loncommunicate::menu($r);
1.53      raeburn  2671:         &disall($r,$showfolder,$msgstatus);
1.1       albertel 2672:     } else {
                   2673: 	&printheader($r,'','Display All Messages');
1.44      raeburn  2674: 	&Apache::loncommunicate::menu($r);
1.53      raeburn  2675: 	&disall($r,($folder?$folder:$dismode),$msgstatus);
1.1       albertel 2676:     }
                   2677:     $r->print(&Apache::loncommon::end_page());
                   2678:     return OK;
                   2679: }
                   2680: # ================================================= Main program, reset counter
                   2681: 
                   2682: =pod
                   2683: 
                   2684: =back
                   2685: 
                   2686: =cut
                   2687: 
                   2688: 1; 
                   2689: 
                   2690: __END__
                   2691: 

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