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

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

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