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

1.1       albertel    1: # The LearningOnline Network with CAPA
                      2: # Routines for messaging display
                      3: #
1.77    ! raeburn     4: # $Id: lonmsgdisplay.pm,v 1.76 2007/05/03 16:55:08 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>';
                    741:         my $rec_button = &mt('Move to Inbox');
                    742:         my $reprec_button = &mt('Move to Inbox/Compose reply');
                    743:         if ($content{'sendback'}) {
                    744:             $rec_button = &mt('Confirm Receipt');
                    745:             $reprec_button = &mt('Confirm Receipt and Reply');
                    746:             $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');
                    747:         } else {
                    748:             $result .= &mt('Click one of the buttons below to move the message to your inbox').' '.&mt('Access to other pages will be prevented until you have done this.');
                    749:         }
                    750:         $result .= '</small><br />'.
                    751:             '<input type="submit" name="rec_'.$key.'" value="'.$rec_button.'" />'.
1.42      raeburn   752:             '<input type="submit" name="reprec_'.$key.'" '.
1.75      raeburn   753:                   'value="'.$reprec_button.'" />';
1.1       albertel  754:     }
                    755:     # Check to see if there were any messages.
                    756:     if ($result eq '') {
                    757:         $result = "<h2>".&mt('You have no critical messages.')."</h2>".
1.38      raeburn   758: 	    '<a href="/adm/roles">'.&mt('Select a course').'</a><br />'.
1.1       albertel  759:             '<a href="/adm/email">'.&mt('Communicate').'</a>';
                    760:     } else {
                    761:         $r->print($header);
                    762:     }
                    763:     $r->print($result);
                    764:     $r->print('<input type="hidden" name="displayedcrit" value="true" /></form>');
                    765: }
                    766: 
                    767: sub sortedmessages {
1.53      raeburn   768:     my ($blocked,$startblock,$endblock,$numblocked,$folder,$msgstatus) = @_;
1.1       albertel  769:     my $suffix=&Apache::lonmsg::foldersuffix($folder);
                    770:     my @messages = &Apache::lonnet::getkeys('nohist_email'.$suffix);
                    771:     #unpack the varibles and repack into temp for sorting
                    772:     my @temp;
                    773:     my %descriptions;
                    774:     my %status_cache = 
                    775: 	&Apache::lonnet::get('email_status'.&Apache::lonmsg::foldersuffix($folder),\@messages);
1.11      albertel  776: 
                    777:     my $get_received;
                    778:     if ($folder eq 'sent' 
                    779: 	&& ($env{'form.sortedby'} =~ m/^(rev)?(user|domain)$/)) {
                    780: 	$get_received = 1;
                    781:     }
                    782: 
                    783:     foreach my $msgid (@messages) {
1.29      www       784: 	my $esc_msgid=&escape($msgid);
1.59      raeburn   785: 	my ($sendtime,$shortsubj,$fromname,$fromdomain,$status,$fromcid,$processid,$symb,$error) =
1.11      albertel  786: 	    &Apache::lonmsg::unpackmsgid($esc_msgid,$folder,undef,
1.1       albertel  787: 					 \%status_cache);
1.53      raeburn   788:         next if ($msgstatus ne '' && $msgstatus ne $status);
1.1       albertel  789:         my $description = &get_course_desc($fromcid,\%descriptions);
                    790: 	my @temp1 = ($sendtime,$shortsubj,$fromname,$fromdomain,$status,
1.11      albertel  791: 		     $esc_msgid,$description);
                    792: 	if ($get_received) {
                    793: 	    my %message = &Apache::lonnet::get('nohist_email'.$suffix,
                    794: 					       [$msgid]);
                    795: 	    my %content = &Apache::lonmsg::unpackagemsg($message{$msgid});
                    796: 	    push(@temp1,$content{'recuser'},$content{'recdomain'});
                    797: 	}
1.1       albertel  798:         # Check whether message was sent during blocking period.
                    799:         if ($sendtime >= $startblock && ($sendtime <= $endblock && $endblock > 0) ) {
1.11      albertel  800:             $$blocked{$msgid} = 'ON';
1.1       albertel  801:             $$numblocked ++;
                    802:         } else { 
                    803:             push @temp ,\@temp1;
                    804:         }
                    805:     }
                    806:     #default sort
                    807:     @temp = sort  {$a->[0] <=> $b->[0]} @temp;    
                    808:     if ($env{'form.sortedby'} eq "date"){
                    809:         @temp = sort  {$a->[0] <=> $b->[0]} @temp;    
                    810:     }
                    811:     if ($env{'form.sortedby'} eq "revdate"){
                    812:     	@temp = sort  {$b->[0] <=> $a->[0]} @temp; 
                    813:     }
                    814:     if ($env{'form.sortedby'} eq "user"){
1.11      albertel  815: 	if ($get_received) {
                    816: 	    @temp = sort  {lc($a->[7][0]) cmp lc($b->[7][0])} @temp;
                    817: 	} else {
                    818: 	    @temp = sort  {lc($a->[2])    cmp lc($b->[2])}    @temp;
                    819: 	}
1.1       albertel  820:     }
                    821:     if ($env{'form.sortedby'} eq "revuser"){
1.11      albertel  822: 	if ($get_received) {
                    823: 	    @temp = sort  {lc($b->[7][0]) cmp lc($a->[7][0])} @temp;
                    824: 	} else {
                    825: 	    @temp = sort  {lc($b->[2])    cmp lc($a->[2])}    @temp;
                    826: 	}
1.1       albertel  827:     }
                    828:     if ($env{'form.sortedby'} eq "domain"){
1.11      albertel  829: 	if ($get_received) {
                    830: 	    @temp = sort  {$a->[8][0] cmp $b->[8][0]} @temp;
                    831: 	} else {
                    832: 	    @temp = sort  {$a->[3]    cmp $b->[3]}    @temp;
                    833: 	}
1.1       albertel  834:     }
                    835:     if ($env{'form.sortedby'} eq "revdomain"){
1.11      albertel  836: 	if ($get_received) {
                    837: 	    @temp = sort  {$b->[8][0] cmp $a->[8][0]} @temp;
                    838: 	} else {
                    839: 	    @temp = sort  {$b->[3]    cmp $a->[3]}    @temp;
                    840: 	}
1.1       albertel  841:     }
                    842:     if ($env{'form.sortedby'} eq "subject"){
                    843:         @temp = sort  {lc($a->[1]) cmp lc($b->[1])} @temp;
                    844:     }
                    845:     if ($env{'form.sortedby'} eq "revsubject"){
                    846:         @temp = sort  {lc($b->[1]) cmp lc($a->[1])} @temp;
                    847:     }
                    848:     if ($env{'form.sortedby'} eq "course"){
                    849:         @temp = sort  {lc($a->[6]) cmp lc($b->[6])} @temp;
                    850:     }
                    851:     if ($env{'form.sortedby'} eq "revcourse"){
                    852:         @temp = sort  {lc($b->[6]) cmp lc($a->[6])} @temp;
                    853:     }
                    854:     if ($env{'form.sortedby'} eq "status"){
                    855:         @temp = sort  {$a->[4] cmp $b->[4]} @temp;
                    856:     }
                    857:     if ($env{'form.sortedby'} eq "revstatus"){
                    858:         @temp = sort  {$b->[4] cmp $a->[4]} @temp;
                    859:     }
                    860:     return @temp;
                    861: }
                    862: 
                    863: sub get_course_desc {
                    864:     my ($fromcid,$descriptions) = @_;
                    865:     my $description;
                    866:     if (!$fromcid) {
                    867:         return $description;
                    868:     } else {
                    869:         if (defined($$descriptions{$fromcid})) {
                    870:             $description = $$descriptions{$fromcid};
                    871:         } else {
                    872:             if (defined($env{'course.'.$fromcid.'.description'})) {
                    873:                 $description = $env{'course.'.$fromcid.'.description'};
                    874:             } else {
1.48      albertel  875:                 my %courseinfo=&Apache::lonnet::coursedescription($fromcid);
1.1       albertel  876:                 $description = $courseinfo{'description'};
                    877:             }
                    878:             $$descriptions{$fromcid} = $description;
                    879:         }
                    880:         return $description;
                    881:     }
                    882: }
                    883: 
                    884: # ======================================================== Display all messages
                    885: 
                    886: sub disall {
1.53      raeburn   887:     my ($r,$folder,$msgstatus)=@_;
1.57      albertel  888:     my %saveable = ('folder'    => 'scalar',
                    889: 		    'msgstatus' => 'scalar',
                    890: 		    'sortedby'  => 'scalar',
                    891: 		    'interdis'  => 'scalar',
                    892: 		    );
                    893:     &Apache::loncommon::store_settings('user','mail',\%saveable);
                    894:     &Apache::loncommon::restore_settings('user','mail',\%saveable);
                    895:     $folder    ||= $env{'form.folder'};
                    896:     $msgstatus ||= $env{'form.msgstatus'};
                    897:     $env{'form.interdis'} ||= 20;
                    898: 
1.53      raeburn   899:     $r->print(&folderlist($folder,$msgstatus));
                    900:     if ($folder eq 'critical') {
1.1       albertel  901: 	&discrit($r);
                    902:     } else {
1.53      raeburn   903: 	&disfolder($r,$folder,$msgstatus);
1.1       albertel  904:     }
                    905: }
                    906: 
                    907: # ============================================================ Display a folder
                    908: 
                    909: sub disfolder {
1.53      raeburn   910:     my ($r,$folder,$msgstatus)=@_;
                    911:     my %statushash = &get_msgstatus_types();
1.1       albertel  912:     my %blocked = ();
                    913:     my %setters = ();
                    914:     my $numblocked = 0;
1.44      raeburn   915:     my ($startblock,$endblock) = &Apache::loncommon::blockcheck(\%setters,'com');
1.53      raeburn   916:     my %lt = &Apache::lonlocal::texthash(
                    917:                       sede => 'Select a destination folder to which the messages will be moved.',
                    918:                       nome => 'No messages have been selected to apply ths action to.',
                    919:                       chec => 'Check the checkbox for at least one message.',  
                    920:     );
1.64      raeburn   921:     my $jscript = &Apache::loncommon::check_uncheck_jscript();
1.1       albertel  922:     $r->print(<<ENDDISHEADER);
1.16      albertel  923: <script type="text/javascript">
1.64      raeburn   924:     $jscript
1.1       albertel  925: 
1.53      raeburn   926:     function checkfoldermove() {
                    927:         if (document.disall.checkedaction.options[document.disall.checkedaction.selectedIndex].value == 'markedmove') {
                    928:             if (document.disall.movetofolder.options[document.disall.movetofolder.selectedIndex].value == "") {
                    929:                 alert("$lt{'sede'}");
                    930:                 return;
1.1       albertel  931:             }
                    932:         }
1.53      raeburn   933:         return; 
1.1       albertel  934:     }
1.53      raeburn   935: 
                    936:     function validate_checkedaction() {
                    937:         document.disall.markedaction.value = document.disall.checkedaction.options[document.disall.checkedaction.selectedIndex].value;
                    938:         if (document.disall.checkedaction.options[document.disall.checkedaction.selectedIndex].value == 'markedmove') {
                    939:             if (document.disall.movetofolder.options[document.disall.movetofolder.selectedIndex].value == "") {
                    940:                 alert("$lt{'sede'}");
                    941:                 return;
                    942:             } 
                    943:         }
                    944:         var checktotal = 0;
1.64      raeburn   945:         if (document.forms.disall.delmark.length > 0) {
                    946:             for (var i=0; i<document.forms.disall.delmark.length; i++) {
                    947:                 if (document.forms.disall.delmark[i].checked) {
                    948:                     checktotal ++;
                    949:                 }
                    950:             }
                    951:         } else {
                    952:             if (document.forms.disall.delmark.checked) {
1.53      raeburn   953:                 checktotal ++;
                    954:             }
1.64      raeburn   955:         }   
1.53      raeburn   956:         if (checktotal == 0) {
                    957:             alert("$lt{'nome'}\\n$lt{'chec'}");
                    958:             return;
                    959:         }
                    960:         document.disall.submit();
                    961:     }
                    962: 
1.1       albertel  963: </script>
                    964: ENDDISHEADER
1.54      albertel  965: 
1.1       albertel  966:     my $fsqs='&folder='.$folder;
1.53      raeburn   967:     my @temp=&sortedmessages(\%blocked,$startblock,$endblock,\$numblocked,$folder,$msgstatus);
1.1       albertel  968:     my $totalnumber=$#temp+1;
1.53      raeburn   969:     if ($totalnumber < 1) {
                    970:         if ($msgstatus eq '') {
                    971: 	    $r->print('<h2>'.&mt('Empty Folder').'</h2>');
                    972:         } elsif ($msgstatus eq 'replied') {
                    973:             $r->print('<h2>'.&mt('You have not replied to any messages in this folder.').'</h2>');
                    974:         } else { 
1.55      albertel  975:             $r->print('<h2>'.&mt('There are no '.lc($statushash{$msgstatus}).' messages in this folder.').'</h2>');
1.53      raeburn   976:         }
1.65      raeburn   977:         if ($numblocked > 0) {
                    978:             $r->print(&blocked_in_folder($numblocked,$startblock,$endblock,
                    979:                                          \%setters));
                    980:         }
1.1       albertel  981: 	return;
                    982:     }
1.57      albertel  983:     my $interdis = $env{'form.interdis'};
1.1       albertel  984:     my $number=int($totalnumber/$interdis);
1.57      albertel  985:     if ($totalnumber%$interdis == 0) {
                    986: 	$number--; 
1.53      raeburn   987:     }
                    988: 
1.1       albertel  989:     if (($startdis<0) || ($startdis>$number)) { $startdis=$number; }
                    990:     my $firstdis=$interdis*$startdis;
                    991:     if ($firstdis>$#temp) { $firstdis=$#temp-$interdis+1; }
                    992:     my $lastdis=$firstdis+$interdis-1;
                    993:     if ($lastdis>$#temp) { $lastdis=$#temp; }
1.53      raeburn   994:     $r->print(&scrollbuttons($startdis,$number,$firstdis,$lastdis,$totalnumber,$msgstatus));
1.1       albertel  995:     $r->print('<form method="post" name="disall" action="/adm/email">'.
1.53      raeburn   996: 	      '<table class="LC_mail_list"><tr><th colspan="1">&nbsp;</th><th>');
1.1       albertel  997:     if ($env{'form.sortedby'} eq "revdate") {
                    998: 	$r->print('<a href = "?sortedby=date'.$fsqs.'">'.&mt('Date').'</a></th>');
                    999:     } else {
                   1000: 	$r->print('<a href = "?sortedby=revdate'.$fsqs.'">'.&mt('Date').'</a></th>');
                   1001:     }
                   1002:     $r->print('<th>');
                   1003:     if ($env{'form.sortedby'} eq "revuser") {
                   1004: 	$r->print('<a href = "?sortedby=user'.$fsqs.'">'.&mt('Username').'</a>');
                   1005:     } else {
                   1006: 	$r->print('<a href = "?sortedby=revuser'.$fsqs.'">'.&mt('Username').'</a>');
                   1007:     }
                   1008:     $r->print('</th><th>');
                   1009:     if ($env{'form.sortedby'} eq "revdomain") {
                   1010: 	$r->print('<a href = "?sortedby=domain'.$fsqs.'">'.&mt('Domain').'</a>');
                   1011:     } else {
                   1012: 	$r->print('<a href = "?sortedby=revdomain'.$fsqs.'">'.&mt('Domain').'</a>');
                   1013:     }
                   1014:     $r->print('</th><th>');
                   1015:     if ($env{'form.sortedby'} eq "revsubject") {
                   1016: 	$r->print('<a href = "?sortedby=subject'.$fsqs.'">'.&mt('Subject').'</a>');
                   1017:     } else {
                   1018:     	$r->print('<a href = "?sortedby=revsubject'.$fsqs.'">'.&mt('Subject').'</a>');
                   1019:     }
                   1020:     $r->print('</th><th>');
                   1021:     if ($env{'form.sortedby'} eq "revcourse") {
1.46      raeburn  1022:         $r->print('<a href = "?sortedby=course'.$fsqs.'">'.&mt('Course').'</a>');
1.1       albertel 1023:     } else {
1.46      raeburn  1024:         $r->print('<a href = "?sortedby=revcourse'.$fsqs.'">'.&mt('Course').'</a>');
1.1       albertel 1025:     }
                   1026:     $r->print('</th><th>');
                   1027:     if ($env{'form.sortedby'} eq "revstatus") {
                   1028: 	$r->print('<a href = "?sortedby=status'.$fsqs.'">'.&mt('Status').'</a></th>');
                   1029:     } else {
                   1030:      	$r->print('<a href = "?sortedby=revstatus'.$fsqs.'">'.&mt('Status').'</a></th>');
                   1031:     }
                   1032:     $r->print("</tr>\n");
1.6       albertel 1033: 
                   1034:     my $suffix = &Apache::lonmsg::foldersuffix($folder);
1.1       albertel 1035:     for (my $n=$firstdis;$n<=$lastdis;$n++) {
1.11      albertel 1036: 	my ($sendtime,$shortsubj,$fromname,$fromdomain,$status,$origID,
                   1037: 	    $description,$recv_name,$recv_domain)= 
                   1038: 		@{$temp[$n]};
1.1       albertel 1039: 	if (($status ne 'deleted') && defined($sendtime) && $sendtime!~/error/) {
                   1040: 	    if ($status eq 'new') {
1.4       albertel 1041: 		$r->print('<tr class="LC_mail_new">');
1.1       albertel 1042: 	    } elsif ($status eq 'read') {
1.4       albertel 1043: 		$r->print('<tr class="LC_mail_read">');
1.1       albertel 1044: 	    } elsif ($status eq 'replied') {
1.4       albertel 1045: 		$r->print('<tr class="LC_mail_replied">'); 
1.1       albertel 1046: 	    } else {
1.4       albertel 1047: 		$r->print('<tr class="LC_mail_other">');
1.1       albertel 1048: 	    }
1.6       albertel 1049: 	    my ($dis_name,$dis_domain) = ($fromname,$fromdomain);
                   1050: 	    if ($folder eq 'sent') {
1.69      raeburn  1051:                 if (defined($recv_name) && defined($recv_domain)) {
                   1052: 		    if (ref($recv_name) eq 'ARRAY' && 
                   1053:                         ref($recv_domain) eq 'ARRAY') {
                   1054: 		        $dis_name   = join('<br />',@{$recv_name});
                   1055: 		        $dis_domain = join('<br />',@{$recv_domain});
                   1056:                     }
1.11      albertel 1057: 		} else {
1.29      www      1058: 		    my $msg_id  = &unescape($origID);
1.11      albertel 1059: 		    my %message = &Apache::lonnet::get('nohist_email'.$suffix,
                   1060: 						       [$msg_id]);
                   1061: 		    my %content = &Apache::lonmsg::unpackagemsg($message{$msg_id});
1.69      raeburn  1062:                     if (ref($content{'recuser'}) eq 'ARRAY') {
                   1063: 		        $dis_name   = join('<br />',@{$content{'recuser'}});
                   1064:                     }
                   1065:                     if (ref($content{'recdomain'}) eq 'ARRAY') {
                   1066: 		        $dis_domain = join('<br />',@{$content{'recdomain'}});
                   1067:                     }
1.11      albertel 1068: 		}
1.6       albertel 1069: 	    }
1.53      raeburn  1070:             my $localsenttime = &Apache::lonlocal::locallocaltime($sendtime);
                   1071:             my $count = $n +1;
                   1072: 	    $r->print('<td align="right"><nobr>'.(($status eq 'new')?'<b>':'').
                   1073:                       $count.'.'.(($status eq 'new')?'</b>':'').'&nbsp;'.
                   1074:                       '<input type="checkbox" name="delmark"'. 
                   1075:                       ' value="'.$origID.'" /></nobr></td>');
                   1076:             foreach my $item ($localsenttime,$dis_name,$dis_domain,$shortsubj) {
                   1077:                 $r->print('<td>'.(($status eq 'new')?'<b>':'').
1.61      raeburn  1078:                           '<a href="/adm/email?display='.$origID.$sqs.'">'.
1.53      raeburn  1079:                           $item.(($status eq 'new')?'</b>':'').'</td>');
                   1080:             }
                   1081:             my $showstatus;
                   1082:             my %statushash = &get_msgstatus_types();
                   1083:             if ($status eq '') {
                   1084:                 $showstatus = '';
                   1085:             } else {
                   1086:                 $showstatus = $statushash{$status};
                   1087:             }
                   1088: 	    $r->print('<td>'.(($status eq 'new')?'<b>':'').$description.
                   1089:                       (($status eq 'new')?'</b>':'').'</td><td>'.
                   1090:                       (($status eq 'new')?'<b>':'').$showstatus.
                   1091:                       (($status eq 'new')?'</b>':'').'</td></tr>'."\n");
1.1       albertel 1092: 	} elsif ($status eq 'deleted') {
                   1093: # purge
1.9       albertel 1094: 	    my ($result,$msg) = 
1.29      www      1095: 		&movemsg(&unescape($origID),$folder,'trash');
1.9       albertel 1096: 	    
1.1       albertel 1097: 	}
                   1098:     }   
1.53      raeburn  1099:     $r->print("</table>\n");
                   1100:     $r->print('<table border="0" cellspacing="2" cellpadding="2">
                   1101:  <tr>
                   1102:   <td>'.
1.64      raeburn  1103:   '<input type="button" onclick="javascript:checkAll(document.disall.delmark)" value="'.&mt('Check All').'" /><br />'."\n".
                   1104:   '<input type="button" onclick="javascript:uncheckAll(document.disall.delmark)" value="'.&mt('Uncheck All').'" />'."\n".
1.53      raeburn  1105:   '<input type="hidden" name="sortedby" value="'.$env{'form.sortedby'}.'" /></td><td>&nbsp;</td>'."\n".
                   1106:   '<td align="center"><b>'.&mt('Action').'</b><br />'."\n".
                   1107:   '  <select name="checkedaction" onchange="javascript:checkfoldermove()">'."\n");
                   1108: 
1.1       albertel 1109:     if ($folder ne 'trash') {
1.53      raeburn  1110:         $r->print('    <option value="markeddel">'.&mt('Delete').'</option>'."\n");
                   1111:     }
                   1112:     if ($msgstatus ne 'read') {
                   1113:         $r->print('    <option value="markedread">'.&mt('Mark Read').'</option>."\n"');
                   1114:     }
                   1115:     if ($msgstatus ne 'unread') {
                   1116:         $r->print('    <option value="markedunread">'.&mt('Mark Unread').'</option>'."\n");
1.1       albertel 1117:     }
1.53      raeburn  1118:     $r->print('   <option value="markedforward">'.&mt('Forward').'</option>'."\n");
1.54      albertel 1119: 
                   1120:     my %gotfolders = &Apache::lonmsg::get_user_folders();
1.53      raeburn  1121:     if (keys(%gotfolders) > 0) {
                   1122:         $r->print('   <option value="markedmove">'.&mt('Move to Folder ->').
                   1123:                   '</option>');
                   1124:     }
                   1125:     $r->print("\n".'</select></td>'."\n");
1.54      albertel 1126: 
1.53      raeburn  1127:     if (keys(%gotfolders) > 0) {
                   1128:         $r->print('<td align="center"><b>'.&mt('Destination folder').'<b><br />');
1.54      albertel 1129: 	my %userfolders;
1.53      raeburn  1130:         foreach my $key (keys(%gotfolders)) {
                   1131:             $userfolders{$key} = $key;
                   1132:         }
                   1133:         $userfolders{''} = "";
                   1134:         $r->print(&Apache::loncommon::select_form('','movetofolder',%userfolders).
                   1135:                   '</td>');
                   1136:     }
                   1137:     $r->print('<td>&nbsp;</td><td>&nbsp;&nbsp;'.
                   1138:               '<input type="button" name="go" value="'.&mt('Go').
                   1139:               '" onclick="javascript:validate_checkedaction()"/></td>'."\n".
                   1140:               '</tr></table>');
1.1       albertel 1141:     my $postedstartdis=$startdis+1;
1.53      raeburn  1142:     $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 1143:     if ($numblocked > 0) {
1.65      raeburn  1144:         $r->print(&blocked_in_folder($numblocked,$startblock,$endblock,
                   1145:                                      \%setters));
1.1       albertel 1146:     }
                   1147: }
                   1148: 
1.65      raeburn  1149: sub blocked_in_folder {
                   1150:     my ($numblocked,$startblock,$endblock,$setters) = @_;
                   1151:     my $beginblock = &Apache::lonlocal::locallocaltime($startblock);
                   1152:     my $finishblock = &Apache::lonlocal::locallocaltime($endblock);
                   1153:     my $output = '<br /><br />'.
                   1154:                   &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);
                   1155:     $output .= &Apache::loncommon::build_block_table($startblock,$endblock,
                   1156:                                                      $setters);
                   1157:     return $output;
                   1158: }
                   1159: 
1.1       albertel 1160: # ============================================================== Compose output
                   1161: 
                   1162: sub compout {
1.53      raeburn  1163:     my ($r,$forwarding,$replying,$broadcast,$replycrit,$folder,$dismode,
                   1164:         $multiforward)=@_;
1.1       albertel 1165:     my $suffix=&Apache::lonmsg::foldersuffix($folder);
1.38      raeburn  1166:     my ($cdom,$cnum,$group,$refarg);
                   1167:     if (exists($env{'form.group'})) {
                   1168:         $cdom = $env{'course.'.$env{'request.course.id'}.'.domain'};
                   1169:         $cnum = $env{'course.'.$env{'request.course.id'}.'.num'};
                   1170:         $group = $env{'form.group'};
                   1171:         my $action = 'composing';
                   1172:         $r->print(&groupmail_header($action,$group,$cdom,$cnum));
                   1173:     } elsif ($broadcast eq 'individual') {
1.1       albertel 1174: 	&printheader($r,'/adm/email?compose=individual',
                   1175: 	     'Send a Message');
                   1176:     } elsif ($broadcast) {
                   1177: 	&printheader($r,'/adm/email?compose=group',
                   1178: 	     'Broadcast Message');
                   1179:     } elsif ($forwarding) {
                   1180: 	&Apache::lonhtmlcommon::add_breadcrumb
1.29      www      1181:         ({href=>"/adm/email?display=".&escape($forwarding),
1.1       albertel 1182:           text=>"Display Message"});
1.29      www      1183: 	&printheader($r,'/adm/email?forward='.&escape($forwarding),
1.1       albertel 1184: 	     'Forwarding a Message');
                   1185:     } elsif ($replying) {
                   1186: 	&Apache::lonhtmlcommon::add_breadcrumb
1.29      www      1187:         ({href=>"/adm/email?display=".&escape($replying),
1.1       albertel 1188:           text=>"Display Message"});
1.29      www      1189: 	&printheader($r,'/adm/email?replyto='.&escape($replying),
1.1       albertel 1190: 	     'Replying to a Message');
                   1191:     } elsif ($replycrit) {
                   1192: 	$r->print('<h3>'.&mt('Replying to a Critical Message').'</h3>');
                   1193: 	$replying=$replycrit;
1.53      raeburn  1194:     } elsif ($multiforward) {
                   1195:         &Apache::lonhtmlcommon::add_breadcrumb
                   1196:         ({href=>"/adm/email?folder=".&escape($folder),
                   1197:           text=>"Display All Messages"});
                   1198:         &printheader($r,'/adm/email?compose=multiforward',
                   1199:              'Forwarding Multiple Messages');
1.70      raeburn  1200:         if ($multiforward > 1) {
                   1201:             $r->print(&mt('Each of the <b>[quant,_1,message]</b> you checked
                   1202: will be forwarded to the recipient(s) you select below.',$multiforward).'<br />');
                   1203:         } else {
                   1204:             $r->print(&mt('The message you checked will be forwarded to the recipient(s) you select below.').'<br />');
                   1205:         }
                   1206: 
1.1       albertel 1207:     } else {
                   1208: 	&printheader($r,'/adm/email?compose=upload',
                   1209: 	     'Distribute from Uploaded File');
                   1210:     }
                   1211: 
                   1212:     my $dispcrit='';
                   1213:     my $dissub='';
                   1214:     my $dismsg='';
                   1215:     my $disbase='';
                   1216:     my $func=&mt('Send New');
1.50      raeburn  1217:     my %lt=&Apache::lonlocal::texthash('us'  => 'Username',
                   1218: 				       'do'  => 'Domain',
                   1219: 				       'ad'  => 'Additional Recipients',
                   1220: 				       'sb'  => 'Subject',
                   1221: 				       'ca'  => 'Cancel',
                   1222: 				       'ma'  => 'Mail',
1.53      raeburn  1223:                                        'msg' => 'Messages',
1.50      raeburn  1224:                                        'gen' => 'Generate messages from a file',
                   1225:                                        'gmt' => 'General message text',
                   1226:                                        'tff' => 'The file format for the uploaded portion of the message is',
                   1227:                                        'uas' => 'Upload and Send',
                   1228:                                       );
1.1       albertel 1229:     if (&Apache::lonnet::allowed('srm',$env{'request.course.id'})
                   1230: 	|| &Apache::lonnet::allowed('srm',$env{'request.course.id'}.
                   1231: 				    '/'.$env{'request.course.sec'})) {
                   1232: 	 my $crithelp = Apache::loncommon::help_open_topic("Course_Critical_Message");
                   1233:          $dispcrit=
                   1234:  '<p><label><input type="checkbox" name="critmsg" /> '.&mt('Send as critical message').'</label> ' . $crithelp . 
                   1235:  '</p><p>'.
                   1236:  '<label><input type="checkbox" name="sendbck" /> '.&mt('Send as critical message').'  ' .
                   1237:  &mt('and return receipt') . '</label>' . $crithelp . 
                   1238:  '</p><p><label><input type="checkbox" name="permanent" /> '.
                   1239: &mt('Send copy to permanent email address (if known)').'</label></p>'.
                   1240: '<p><label><input type="checkbox" name="rsspost" /> '.
                   1241: 		  &mt('Include in course RSS newsfeed').'</label></p>';
1.70      raeburn  1242:     }
1.71      raeburn  1243:     if ($broadcast ne 'group') {
                   1244:         if (&Apache::lonnet::allowed('dff',$env{'request.course.id'}) ||
                   1245:             &Apache::lonnet::allowed('dff',$env{'request.course.id'}.
                   1246:                                      '/'.$env{'request.course.sec'})) {
                   1247: 
                   1248:             $dispcrit.='<p><label>'.
                   1249:                        '<input type="checkbox" name="courserecord" value="1" /> '.
                   1250:                        &mt("Include in course's 'User records' for recipient(s)").
                   1251:                        '</label></p>';
                   1252:         }
1.70      raeburn  1253:     }
                   1254: 
1.1       albertel 1255:     my %message;
                   1256:     my %content;
                   1257:     my $defdom=$env{'user.domain'};
                   1258:     if ($forwarding) {
                   1259: 	%message=&Apache::lonnet::get('nohist_email'.$suffix,[$forwarding]);
                   1260: 	%content=&Apache::lonmsg::unpackagemsg($message{$forwarding},$folder);
                   1261: 	$dispcrit.='<input type="hidden" name="forwid" value="'.
                   1262: 	    $forwarding.'" />';
                   1263: 	$func=&mt('Forward');
                   1264: 	
                   1265: 	$dissub=&mt('Forwarding').': '.$content{'subject'};
                   1266: 	$dismsg=&mt('Forwarded message from').' '.
                   1267: 	    $content{'sendername'}.' '.&mt('at').' '.$content{'senderdomain'};
                   1268: 	if ($content{'baseurl'}) {
1.29      www      1269: 	    $disbase='<input type="hidden" name="baseurl" value="'.&escape($content{'baseurl'}).'" />';
1.1       albertel 1270: 	}
                   1271:     }
                   1272:     if ($replying) {
                   1273: 	%message=&Apache::lonnet::get('nohist_email'.$suffix,[$replying]);
                   1274: 	%content=&Apache::lonmsg::unpackagemsg($message{$replying},$folder);
                   1275: 	$dispcrit.='<input type="hidden" name="replyid" value="'.
                   1276: 	    $replying.'" />';
                   1277: 	$func=&mt('Send Reply to');
                   1278: 	
                   1279: 	$dissub=&mt('Reply').': '.$content{'subject'};       
                   1280: 	$dismsg='> '.$content{'message'};
                   1281: 	$dismsg=~s/\r/\n/g;
                   1282: 	$dismsg=~s/\f/\n/g;
                   1283: 	$dismsg=~s/\n+/\n\> /g;
                   1284: 	if ($content{'baseurl'}) {
1.29      www      1285: 	    $disbase='<input type="hidden" name="baseurl" value="'.&escape($content{'baseurl'}).'" />';
1.1       albertel 1286: 	    if ($env{'user.adv'}) {
1.72      albertel 1287: 		$disbase.='<label><input type="checkbox" name="storebasecomment" />'.&mt('Save message for re-use').
1.1       albertel 1288: 		    '</label> <a href="/adm/email?showcommentbaseurl='.
1.29      www      1289: 		    &escape($content{'baseurl'}).'" target="comments">'.
1.1       albertel 1290: 		    &mt('Show re-usable messages').'</a><br />';
                   1291: 	    }
                   1292: 	}
1.73      raeburn  1293:         my $jscript = &Apache::loncommon::check_uncheck_jscript();
                   1294:         $r->print(<<"ENDREPSCRIPT");
                   1295: <script type="text/javascript">
                   1296: $jscript
                   1297: </script>
                   1298: ENDREPSCRIPT
1.1       albertel 1299:     }
                   1300:     my $citation=&displayresource(%content);
1.38      raeburn  1301:     my ($can_grp_broadcast,$viewgrps,$editgrps);
1.1       albertel 1302:     if ($env{'form.recdom'}) { $defdom=$env{'form.recdom'}; }
1.23      www      1303:     if ($env{'form.text'}) { $dismsg=$env{'form.text'}; }
                   1304:     if ($env{'form.subject'}) { $dissub=$env{'form.subject'}; }
                   1305:     $r->print(
1.1       albertel 1306:                 '<form action="/adm/email"  name="compemail" method="post"'.
                   1307:                 ' enctype="multipart/form-data">'."\n".
1.38      raeburn  1308:                 '<input type="hidden" name="sendmail" value="on" />'."\n");
                   1309:     if ($broadcast eq 'group' && $env{'form.group'} ne '') {
                   1310:         $can_grp_broadcast = 
                   1311:                 &Apache::lonnet::allowed('sgb',$env{'request.course.id'}.'/'.
                   1312:                                          $group);
                   1313:         $viewgrps = 
                   1314:                &Apache::lonnet::allowed('vcg',$env{'request.course.id'}.
                   1315:                ($env{'request.course.sec'}?'/'.$env{'request.course.sec'}:''));
                   1316:         $editgrps = 
                   1317:                &Apache::lonnet::allowed('mdg',$env{'request.course.id'}.
                   1318:                ($env{'request.course.sec'}?'/'.$env{'request.course.sec'}:''));
                   1319:         if ($viewgrps || $editgrps || $can_grp_broadcast) {
                   1320:             $r->print(&disgroup($cdom,$cnum,$group,$viewgrps,$editgrps));
                   1321:         }
                   1322:     }
                   1323:     $r->print('<table>');
                   1324:     if (($broadcast eq 'group') && ($group ne '') && 
                   1325:         (!$can_grp_broadcast && !$viewgrps && !$editgrps)) {
                   1326:         $r->print(&recipient_input_row($cdom,%lt));
                   1327:     } 
                   1328:     if (($broadcast ne 'group') && ($broadcast ne 'upload')) {
1.1       albertel 1329: 	if ($replying) {
                   1330: 	    $r->print('<tr><td colspan="2">'.&mt('Replying to').' '.
                   1331: 		      &Apache::loncommon::aboutmewrapper(
                   1332: 							 &Apache::loncommon::plainname($content{'sendername'},$content{'senderdomain'}),$content{'sendername'},$content{'senderdomain'}).' ('.
1.14      albertel 1333: 		      $content{'sendername'}.':'.
1.1       albertel 1334: 		      $content{'senderdomain'}.')'.
                   1335: 		      '<input type="hidden" name="recuname" value="'.$content{'sendername'}.'" />'.
                   1336: 		      '<input type="hidden" name="recdomain" value="'.$content{'senderdomain'}.'" />'.
                   1337: 		      '</td></tr>');
1.73      raeburn  1338:             if ($content{'recipid'}) {
                   1339:                 my @ccs = &retrieve_cc_recips('replying',%content);
                   1340:                 if (@ccs > 0) {
                   1341:                     my $replyall = qq|
                   1342:  <span class="LC_nobreak">
                   1343:       <input type="button" value="check all"
                   1344:         onclick="javascript:checkAll(document.compemail.replying_cc)" />
                   1345:         &nbsp;&nbsp;
                   1346:       <input type="button" value="uncheck all"
                   1347:         onclick="javascript:uncheckAll(document.compemail.replying_cc)" />
                   1348:  </span>
                   1349: |;
                   1350:                     my $cclist = join(' ',@ccs);
                   1351:                     $r->print('<tr><td>'.&mt('Reply to other recipients').':<br />'.$replyall.'</td><td>'.$cclist.'</td></tr>');
                   1352:                 }
                   1353:             }
1.1       albertel 1354: 	} else {
1.38      raeburn  1355:             $r->print(&recipient_input_row($defdom,%lt));
1.1       albertel 1356:         }
                   1357:     }
                   1358:     my $latexHelp = Apache::loncommon::helpLatexCheatsheet();
1.53      raeburn  1359:     my $subj_size;
                   1360:     if ($multiforward) {
                   1361:         $r->print(&additional_rec_row(\%lt));
                   1362:         $r->print('<tr><td colspan="2">'.
                   1363:                   &mt('Unless you choose otherwise:').'<ul><li>'.
                   1364:         &mt("The subject in each forwarded message will be <i>'Forwarding:'</i> followed by the original subject.").'</li><li>'.
                   1365:         &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>');
                   1366:         $func=&mt('Forward');
                   1367:         $dissub = &mt('Forwarding').': ';
                   1368:         $subj_size = '10';
                   1369:         my $extra = '&lt;'.&mt('original subject').'&gt;&nbsp;&nbsp;&nbsp;'.
                   1370:                     '<input type="radio" name="showorigsubj" value="1" checked="checked" />'.&mt('Yes').'&nbsp;<input type="radio" name="showorigsubj" value="0" />'.&mt('No');
                   1371:         $dismsg = &mt('Forwarded message from ').' ';
                   1372:         my $sender = &mt("sender's name");
                   1373:         $r->print(&msg_subject_row($dissub,\%lt,$subj_size,$extra));
                   1374:         $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>
                   1375: </table>
                   1376: <br />'.
                   1377: $latexHelp.
                   1378: &mt("Any new text to display before the text of the original messages:").'<br />
                   1379: <textarea name="message" id="message" cols="80" rows="5" wrap="hard">
                   1380: </textarea></p><br />');
                   1381:         my @to_forward = &Apache::loncommon::get_env_multiple('form.delmark');
                   1382:         foreach my $msg (@to_forward) {
                   1383:             $r->print('<input type="hidden" name="delmark" value="'.$msg.'" />');
                   1384:         }
                   1385:         $r->print(&submit_button_row($folder,$dismode,$func.' '.$lt{'msg'},
                   1386:                                      \%lt));
                   1387:     } elsif ($broadcast ne 'upload') {
                   1388:         $subj_size = '50';
                   1389:         $r->print(&additional_rec_row(\%lt));
                   1390:         $r->print(&msg_subject_row($dissub,\%lt,$subj_size));
                   1391:         $r->print(<<"ENDCOMP");
                   1392: </table>
1.1       albertel 1393: $latexHelp
                   1394: <textarea name="message" id="message" cols="80" rows="15" wrap="hard">$dismsg
                   1395: </textarea></p><br />
                   1396: $dispcrit
                   1397: $disbase
                   1398: ENDCOMP
1.53      raeburn  1399:         $r->print(&submit_button_row($folder,$dismode,$func.' '.$lt{'ma'},
                   1400:                                      \%lt));
                   1401:         $r->print($citation);
1.38      raeburn  1402:         if (exists($env{'form.ref'})) {
                   1403:             $r->print('<input type="hidden" name="ref" value="'.
                   1404:                       $env{'form.ref'}.'" />');
                   1405:         }
                   1406:         if (exists($env{'form.group'})) {
                   1407:             $r->print('<input type="hidden" name="group" value="'.
                   1408:                       $env{'form.group'}.'" />');
                   1409:         }
1.1       albertel 1410:     } else { # $broadcast is 'upload'
1.50      raeburn  1411: 	$r->print(<<ENDBLOCK);
1.1       albertel 1412: <input type="hidden" name="sendmode" value="upload" />
                   1413: <input type="hidden" name="send" value="on" />
1.50      raeburn  1414: <h3>$lt{'gen'}</h3>
1.1       albertel 1415: <p>
                   1416: Subject: <input type="text" size="50" name="subject" />
                   1417: </p>
1.50      raeburn  1418: <p>$lt{'gmt'}:<br />
1.1       albertel 1419: <textarea name="message" id="message" cols="60" rows="10" wrap="hard">$dismsg
                   1420: </textarea></p>
                   1421: <p>
1.50      raeburn  1422: $lt{'tff'}:
                   1423: ENDBLOCK
                   1424:        $r->print('
                   1425: <pre>'."\n".
                   1426: &mt('username1:domain1: text')."\n".
                   1427: &mt('username2:domain2: text')."\n".
                   1428: &mt('username3:domain1: text')."\n".
                   1429: '</pre>
1.1       albertel 1430: </p>
                   1431: <p>
1.50      raeburn  1432: '.&mt('The messages will be assembled from all lines with the respective'."\n".'<tt>username:domain</tt>, and appended to the general message text.'));
                   1433:         $r->print(<<ENDUPLOAD);
                   1434: </p>
1.1       albertel 1435: <p>
                   1436: <input type="file" name="upfile" size="40" /></p><p>
                   1437: $dispcrit
1.50      raeburn  1438: <input type="submit" value="$lt{'uas'}" /></p>
1.1       albertel 1439: ENDUPLOAD
                   1440:     }
                   1441:     if ($broadcast eq 'group') {
1.38      raeburn  1442:        if ($group eq '') {
                   1443:            my $studentsel = &discourse();
                   1444:            $r->print($studentsel);
                   1445:        }
1.1       albertel 1446:     }
1.36      albertel 1447:     if ($env{'form.displayedcrit'}) {
                   1448: 	$r->print('<input type="hidden" name="displayedcrit" value="true" />');
                   1449:     }
1.1       albertel 1450:     $r->print('</form>'.
                   1451: 	      &Apache::lonfeedback::generate_preview_button('compemail','message').
                   1452: 	      &Apache::lonhtmlcommon::htmlareaselectactive('message'));
                   1453: }
                   1454: 
                   1455: # ---------------------------------------------------- Display all face to face
                   1456: 
1.38      raeburn  1457: sub recipient_input_row {
                   1458:     my ($dom,%lt) = @_;
                   1459:     my $domform = &Apache::loncommon::select_dom_form($dom,'recdomain');
                   1460:     my $selectlink=
                   1461:       &Apache::loncommon::selectstudent_link('compemail','recuname',
                   1462:                                              'recdomain');
                   1463:     my $output = <<"ENDREC";
                   1464: <tr><td>$lt{'us'}:</td><td><input type="text" size="12" name="recuname" value="$env{'form.recname'}" /></td><td rowspan="2">$selectlink</td></tr>
                   1465: <tr><td>$lt{'do'}:</td>
                   1466: <td>$domform</td></tr>
                   1467: ENDREC
                   1468:     return $output;
                   1469: }
                   1470: 
1.53      raeburn  1471: sub additional_rec_row {
                   1472:     my ($lt) = @_;
1.73      raeburn  1473:     my $cc = &mt('Cc:');
                   1474:     my $bcc = &mt('Bcc:'); 
1.53      raeburn  1475:     my $output = <<"ENDADD";
1.73      raeburn  1476: <tr><td>$lt->{'ad'} :<br /><tt>username:domain,username:domain, ...
                   1477: </tt></td><td>&nbsp;<span class="span.LC_nobreak">$cc 
                   1478: <input type="text" size="50" name="additionalrec_cc" /></nospan><br />
                   1479: <span class="span.LC_nobreak">$bcc <input type="text" size="50" name="additionalrec_bcc" /></nospan></td></tr>
1.53      raeburn  1480: ENDADD
                   1481:     return $output;
                   1482: }
                   1483: 
                   1484: sub submit_button_row {
                   1485:     my ($folder,$dismode,$sendtext,$lt) = @_;
                   1486:     my $output = qq| 
                   1487: <input type="hidden" name="folder" value="$folder" />
                   1488: <input type="hidden" name="dismode" value="$dismode" />
                   1489: <input type="submit" name="send" value="$sendtext" />
                   1490: <input type="submit" name="cancel" value="$lt->{'ca'}" /><hr />
                   1491: |;
                   1492:     return $output;
                   1493: }
                   1494: 
                   1495: sub msg_subject_row {
                   1496:     my ($dissub,$lt,$subj_size,$extra) = @_;
                   1497:     my $output = '<tr><td>'.$lt->{'sb'}.':</td><td><input type="text" size="'.
                   1498:                  $subj_size.'" name="subject" value="'.$dissub.'" />'.$extra.
                   1499:                  '</td></tr>';
                   1500:     return $output;
                   1501: }
                   1502: 
1.1       albertel 1503: sub retrieve_instructor_comments {
                   1504:     my ($user,$domain)=@_;
                   1505:     my $target=$env{'form.grade_target'};
                   1506:     if (! $env{'request.course.id'}) { return; }
1.49      albertel 1507:     if (! &Apache::lonnet::allowed('dff',$env{'request.course.id'})
                   1508: 	&& ! &Apache::lonnet::allowed('dff',$env{'request.course.id'}.
1.1       albertel 1509: 				      '/'.$env{'request.course.sec'})) {
                   1510: 	return;
                   1511:     }
                   1512:     my %records=&Apache::lonnet::dump('nohist_email',
                   1513: 			 $env{'course.'.$env{'request.course.id'}.'.domain'},
                   1514: 			 $env{'course.'.$env{'request.course.id'}.'.num'},
                   1515:                          '%255b'.$user.'%253a'.$domain.'%255d');
                   1516:     my $result='';
1.42      raeburn  1517:     foreach my $key (sort(keys(%records))) {
                   1518:         my %content=&Apache::lonmsg::unpackagemsg($records{$key});
1.1       albertel 1519:         next if ($content{'senderdomain'} eq '');
                   1520:         next if ($content{'subject'} !~ /^Record/);
                   1521: 	# &Apache::lonfeedback::newline_to_br(\$content{'message'});
                   1522: 	$result.='Recorded by '.
1.14      albertel 1523:             $content{'sendername'}.':'.$content{'senderdomain'}."\n";
1.1       albertel 1524:         $result.=
                   1525:             &Apache::lontexconvert::msgtexconverted($content{'message'})."\n";
                   1526:      }
                   1527:     return $result;
                   1528: }
                   1529: 
                   1530: sub disfacetoface {
                   1531:     my ($r,$user,$domain)=@_;
                   1532:     my $target=$env{'form.grade_target'};
                   1533:     unless ($env{'request.course.id'}) { return; }
1.49      albertel 1534:     if  (!&Apache::lonnet::allowed('dff',$env{'request.course.id'})
                   1535: 	 && ! &Apache::lonnet::allowed('dff',$env{'request.course.id'}.
1.1       albertel 1536: 				       '/'.$env{'request.course.sec'})) {
1.50      raeburn  1537: 	$r->print(&mt('Not allowed'));
1.1       albertel 1538: 	return;
                   1539:     }
                   1540:     my %records=&Apache::lonnet::dump('nohist_email',
                   1541: 			 $env{'course.'.$env{'request.course.id'}.'.domain'},
                   1542: 			 $env{'course.'.$env{'request.course.id'}.'.num'},
                   1543:                          '%255b'.$user.'%253a'.$domain.'%255d');
                   1544:     my $result='';
1.42      raeburn  1545:     foreach my $key (sort(keys(%records))) {
                   1546:         my %content=&Apache::lonmsg::unpackagemsg($records{$key});
1.1       albertel 1547:         next if ($content{'senderdomain'} eq '');
                   1548: 	&Apache::lonfeedback::newline_to_br(\$content{'message'});
                   1549:         if ($content{'subject'}=~/^Record/) {
                   1550: 	    $result.='<h3>'.&mt('Record').'</h3>';
                   1551:         } elsif ($content{'subject'}=~/^Broadcast/) {
                   1552:             $result .='<h3>'.&mt('Broadcast Message').'</h3>';
                   1553:             if ($content{'subject'}=~/^Broadcast\./) {
                   1554:                 if (defined($content{'coursemsgid'})) {
1.29      www      1555:                     my $crsmsgid = &escape($content{'coursemsgid'});
1.1       albertel 1556:                     my $broadcast_message = &general_message($crsmsgid);
                   1557:                     $content{'message'} = '<b>'.&mt('Subject').': '.$content{'message'}.'</b><br />'.$broadcast_message;
                   1558:                 } else {
                   1559:                     %content=&Apache::lonmsg::unpackagemsg($content{'message'});
                   1560:                     $content{'message'} =
                   1561:                     '<b>'.&mt('Subject').': '.$content{'subject'}.'</b><br />'.
                   1562:                     $content{'message'};
                   1563:                 }
1.70      raeburn  1564:             }
                   1565:         } elsif ($content{'subject'}=~/^Archive/) {
                   1566:             $result.='<h3>'.&mt('Archived Message').'</h3>';
                   1567:             if (defined($content{'coursemsgid'})) {
                   1568:                 my $crsmsgid = &escape($content{'coursemsgid'});
                   1569:                 my $archive_message = &general_message($crsmsgid);
                   1570:                 $content{'message'} = '<b>'.&mt('Subject').': '.$content{'message'}.'</b><br />'.$archive_message;
                   1571:             } else {
                   1572:                 %content=&Apache::lonmsg::unpackagemsg($content{'message'});
                   1573:                 $content{'message'} =
                   1574:                 '<b>'.&mt('Subject').': '.$content{'subject'}.'</b><br
                   1575: '.
                   1576:                 $content{'message'};
                   1577:             }
1.1       albertel 1578:         } else {
                   1579:             $result.='<h3>'.&mt('Critical Message').'</h3>';
                   1580:             if (defined($content{'coursemsgid'})) {
1.29      www      1581:                 my $crsmsgid=&escape($content{'coursemsgid'});
1.1       albertel 1582:                 my $critical_message = &general_message($crsmsgid);
                   1583:                 $content{'message'} = '<b>'.&mt('Subject').': '.$content{'message'}.'</b><br />'.$critical_message;
                   1584:             } else {
                   1585:                 %content=&Apache::lonmsg::unpackagemsg($content{'message'});
                   1586:                 $content{'message'}=
                   1587:                 '<b>'.&mt('Subject').': '.$content{'subject'}.'</b><br />'.
                   1588: 		$content{'message'};
                   1589:             }
                   1590:         }
                   1591:         $result.=&mt('By').': <b>'.
                   1592: &Apache::loncommon::aboutmewrapper(
                   1593:  &Apache::loncommon::plainname($content{'sendername'},$content{'senderdomain'}),$content{'sendername'},$content{'senderdomain'}).'</b> ('.
1.14      albertel 1594: $content{'sendername'}.':'.
1.1       albertel 1595:             $content{'senderdomain'}.') '.$content{'time'}.
                   1596:             '<br /><pre>'.
                   1597:               &Apache::lontexconvert::msgtexconverted($content{'message'}).
                   1598: 	      '</pre>';
                   1599:      }
                   1600:     # Check to see if there were any messages.
                   1601:     if ($result eq '') {
1.33      albertel 1602:         my $lctype = lc(&Apache::loncommon::course_type());
1.1       albertel 1603: 	if ($target ne 'tex') { 
1.30      raeburn  1604: 	    $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 1605: 	} else {
1.30      raeburn  1606: 	    $r->print('\textbf{'.&mt('No notes, face-to-face discussion records, critical messages or broadcast messages in this [_1].',$lctype).'}\\\\');
1.1       albertel 1607: 	}
                   1608:     } else {
                   1609:        $r->print($result);
                   1610:     }
                   1611: }
                   1612: 
                   1613: sub general_message {
                   1614:     my ($crsmsgid) = @_;
                   1615:     my %general_content;
                   1616:     if ($crsmsgid) { 
                   1617:         my %course_content = &Apache::lonnet::get('nohist_email',[$crsmsgid],
                   1618:                            $env{'course.'.$env{'request.course.id'}.'.domain'},
                   1619:                            $env{'course.'.$env{'request.course.id'}.'.num'});
                   1620:         %general_content = &Apache::lonmsg::unpackagemsg($course_content{$crsmsgid});
                   1621:     }
                   1622:     return $general_content{'message'};
                   1623: }
                   1624: 
                   1625: # ---------------------------------------------------------------- Face to face
                   1626: 
                   1627: sub facetoface {
                   1628:     my ($r,$stage)=@_;
1.49      albertel 1629:     if (!&Apache::lonnet::allowed('dff',$env{'request.course.id'})
                   1630: 	&& ! &Apache::lonnet::allowed('dff',$env{'request.course.id'}.
1.1       albertel 1631: 				      '/'.$env{'request.course.sec'})) {
1.50      raeburn  1632: 	$r->print(&mt('Not allowed'));
1.1       albertel 1633: 	return;
                   1634:     }
1.33      albertel 1635:     my $crstype = &Apache::loncommon::course_type();
                   1636:     my $leaders = ($crstype eq 'Group') ? 'coordinators and leaders'
                   1637:                                         : 'faculty and staff';
1.1       albertel 1638:     &printheader($r,
                   1639: 		 '/adm/email?recordftf=query',
1.70      raeburn  1640: 		 "User Notes, Face-to-Face, Critical Messages, Broadcast Messages, Archived Messages");
1.1       albertel 1641: # from query string
                   1642: 
                   1643:     if ($env{'form.recname'}) { $env{'form.recuname'}=$env{'form.recname'}; }
                   1644:     if ($env{'form.recdom'}) { $env{'form.recdomain'}=$env{'form.recdom'}; }
                   1645: 
                   1646:     my $defdom=$env{'user.domain'};
                   1647: # already filled in
                   1648:     if ($env{'form.recdomain'}) { $defdom=$env{'form.recdomain'}; }
                   1649: # generate output
                   1650:     my $domform = &Apache::loncommon::select_dom_form($defdom,'recdomain');
                   1651:     my $stdbrws = &Apache::loncommon::selectstudent_link
                   1652: 	('stdselect','recuname','recdomain');
                   1653:     my %lt=&Apache::lonlocal::texthash('user' => 'Username',
                   1654: 				       'dom' => 'Domain',
1.70      raeburn  1655: 				       'head' => "User Notes, Records of Face-To-Face Discussions, Critical Messages, Broadcast Messages and Archived Messages in $crstype",
1.1       albertel 1656: 				       'subm' => 'Retrieve discussion and message records',
1.30      raeburn  1657: 				       'newr' => 'New Record (record is visible to '.lc($crstype).' '.$leaders.')',
1.1       albertel 1658: 				       'post' => 'Post this Record');
                   1659:     $r->print(<<"ENDTREC");
                   1660: <h3>$lt{'head'}</h3>
                   1661: <form method="post" action="/adm/email" name="stdselect">
                   1662: <input type="hidden" name="recordftf" value="retrieve" />
                   1663: <table>
                   1664: <tr><td>$lt{'user'}:</td><td><input type="text" size="12" name="recuname" value="$env{'form.recuname'}" /></td>
                   1665: <td rowspan="2">
                   1666: $stdbrws
                   1667: <input type="submit" value="$lt{'subm'}" /></td>
                   1668: </tr>
                   1669: <tr><td>$lt{'dom'}:</td>
                   1670: <td>$domform</td></tr>
                   1671: </table>
                   1672: </form>
                   1673: ENDTREC
                   1674:     if (($stage ne 'query') &&
                   1675:         ($env{'form.recdomain'}) && ($env{'form.recuname'})) {
                   1676:         chomp($env{'form.newrecord'});
                   1677:         if ($env{'form.newrecord'}) {
1.31      albertel 1678: 	    &Apache::lonmsg::store_instructor_comment($env{'form.newrecord'},
                   1679: 						      $env{'form.recuname'},
                   1680: 						      $env{'form.recdomain'});
1.1       albertel 1681:         }
                   1682:         $r->print('<h3>'.&Apache::loncommon::plainname($env{'form.recuname'},
                   1683: 				     $env{'form.recdomain'}).'</h3>');
                   1684:         &disfacetoface($r,$env{'form.recuname'},$env{'form.recdomain'});
                   1685: 	$r->print(<<ENDRHEAD);
                   1686: <form method="post" action="/adm/email">
                   1687: <input name="recdomain" value="$env{'form.recdomain'}" type="hidden" />
                   1688: <input name="recuname" value="$env{'form.recuname'}" type="hidden" />
                   1689: ENDRHEAD
                   1690:         $r->print(<<ENDBFORM);
                   1691: <hr />$lt{'newr'}<br />
                   1692: <textarea name="newrecord" cols="80" rows="10" wrap="hard"></textarea>
                   1693: <br />
                   1694: <input type="hidden" name="recordftf" value="post" />
                   1695: <input type="submit" value="$lt{'post'}" />
                   1696: </form>
                   1697: ENDBFORM
                   1698:     }
                   1699: }
                   1700: 
                   1701: # ----------------------------------------------------------- Blocking during exams
                   1702: 
                   1703: sub examblock {
                   1704:     my ($r,$action) = @_;
                   1705:     unless ($env{'request.course.id'}) { return;}
1.44      raeburn  1706:     if (!&Apache::lonnet::allowed('dcm',$env{'request.course.id'})
                   1707: 	&& ! &Apache::lonnet::allowed('dcm',$env{'request.course.id'}.
1.1       albertel 1708: 				      '/'.$env{'request.course.sec'})) {
                   1709: 	$r->print('Not allowed');
                   1710: 	return;
                   1711:     }
1.34      albertel 1712:     my $usertype = (&Apache::loncommon::course_type() eq 'Group') ? 'members'
1.33      albertel 1713: 	                                                          : 'students';
1.1       albertel 1714:     my %lt=&Apache::lonlocal::texthash(
                   1715:             'comb' => 'Communication Blocking',
                   1716:             'cbds' => 'Communication blocking during scheduled exams',
1.30      raeburn  1717:             '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 1718:              'mecb' => 'Modify existing communication blocking periods',
1.72      albertel 1719:              'ncbc' => 'No communication blocks currently saved',
                   1720:              'stor' => 'Save',
1.1       albertel 1721:     );
                   1722: 
                   1723:     my %ltext = &Apache::lonlocal::texthash(
                   1724:             'dura' => 'Duration',
                   1725:             'setb' => 'Set by',
                   1726:             'even' => 'Event',
1.44      raeburn  1727:             'blck' => 'Blocked?',
1.1       albertel 1728:             'actn' => 'Action',
                   1729:             'star' => 'Start',
                   1730:             'endd' => 'End'
                   1731:     );
                   1732: 
                   1733:     &printheader($r,'/adm/email?block=display',$lt{'comb'});
                   1734:     $r->print('<h3>'.$lt{'cbds'}.'</h3>');
                   1735: 
                   1736:     if ($action eq 'store') {
                   1737:         &blockstore($r);
                   1738:     }
                   1739: 
                   1740:     $r->print($lt{'desc'}.'<br /><br />
                   1741:                <form name="blockform" method="post" action="/adm/email?block=store">
                   1742:              ');
                   1743: 
                   1744:     $r->print('<h4>'.$lt{'mecb'}.'</h4>');
                   1745:     my %records = ();
                   1746:     my $blockcount = 0;
                   1747:     my $parmcount = 0;
                   1748:     &get_blockdates(\%records,\$blockcount);
                   1749:     if ($blockcount > 0) {
                   1750:         $parmcount = &display_blocker_status($r,\%records,\%ltext);
                   1751:     } else {
                   1752:         $r->print($lt{'ncbc'}.'<br /><br />');
                   1753:     }
                   1754:     &display_addblocker_table($r,$parmcount,\%ltext);
                   1755:     my $end_page=&Apache::loncommon::end_page();
                   1756:     $r->print(<<"END");
                   1757: <br />
                   1758: <input type="hidden" name="blocktotal" value="$blockcount" />
1.50      raeburn  1759: <input type ="submit" value="$lt{'stor'}" />
1.1       albertel 1760: </form>
                   1761: $end_page
                   1762: END
                   1763:     return;
                   1764: }
                   1765: 
                   1766: sub blockstore {
                   1767:     my $r = shift;
                   1768:     my %lt=&Apache::lonlocal::texthash(
                   1769:             'tfcm' => 'The following changes were made',
                   1770:             'ncwm' => 'No changes were made.' 
                   1771:     );
                   1772:     my %adds = ();
                   1773:     my %removals = ();
                   1774:     my %cancels = ();
                   1775:     my $modtotal = 0;
                   1776:     my $canceltotal = 0;
                   1777:     my $addtotal = 0;
                   1778:     my %blocking = ();
                   1779:     $r->print('<h3>'.$lt{'head'}.'</h3>');
1.42      raeburn  1780:     foreach my $envkey (keys(%env)) {
1.44      raeburn  1781:         if ($envkey =~ m/^form\.modify_(\d+)$/) {
1.1       albertel 1782:             $adds{$1} = $1;
                   1783:             $removals{$1} = $1;
                   1784:             $modtotal ++;
1.42      raeburn  1785:         } elsif ($envkey =~ m/^form\.cancel_(\d+)$/) {
1.1       albertel 1786:             $cancels{$1} = $1;
                   1787:             unless ( defined($removals{$1}) ) {
                   1788:                 $removals{$1} = $1;
                   1789:                 $canceltotal ++;
                   1790:             }
1.42      raeburn  1791:         } elsif ($envkey =~ m/^form\.add_(\d+)$/) {
1.1       albertel 1792:             $adds{$1} = $1;
                   1793:             $addtotal ++;
1.44      raeburn  1794:         } 
1.1       albertel 1795:     }
                   1796: 
1.42      raeburn  1797:     foreach my $key (keys(%removals)) {
                   1798:         my $hashkey = $env{'form.key_'.$key};
1.1       albertel 1799:         &Apache::lonnet::del('comm_block',["$hashkey"],
                   1800:                          $env{'course.'.$env{'request.course.id'}.'.domain'},
                   1801:                          $env{'course.'.$env{'request.course.id'}.'.num'}
                   1802:                          );
                   1803:     }
1.42      raeburn  1804:     foreach my $key (keys(%adds)) {
                   1805:         unless ( defined($cancels{$key}) ) {
                   1806:             my ($newstart,$newend) = &get_dates_from_form($key);
1.1       albertel 1807:             my $newkey = $newstart.'____'.$newend;
1.44      raeburn  1808:             my $blocktypes = &get_block_choices($key);
                   1809:             $blocking{$newkey} = {
                   1810:                           setter => $env{'user.name'}.':'.$env{'user.domain'},
                   1811:                           event  => &escape($env{'form.title_'.$key}),
                   1812:                           blocks => $blocktypes,
                   1813:                         };
1.1       albertel 1814:         }
                   1815:     }
                   1816:     if ($addtotal + $modtotal > 0) {
                   1817:         &Apache::lonnet::put('comm_block',\%blocking,
                   1818:                      $env{'course.'.$env{'request.course.id'}.'.domain'},
                   1819:                      $env{'course.'.$env{'request.course.id'}.'.num'}
                   1820:                      );
                   1821:     }
                   1822:     my $chgestotal = $canceltotal + $modtotal + $addtotal;
                   1823:     if ($chgestotal > 0) {
                   1824:         $r->print($lt{'tfcm'}.'<ul>');
                   1825:         if ($canceltotal > 0) {
1.50      raeburn  1826:             $r->print('<li>'.&mt('[quant,_1,communication blocking period was,communication blocking periods were] removed.',$canceltotal).'</li>');
1.1       albertel 1827:         }
                   1828:         if ($modtotal > 0) {
1.50      raeburn  1829:             $r->print('<li>'.&mt('[quant,_1,communication blocking period was,communication blocking periods were] modified.',$modtotal).'</li>');
1.1       albertel 1830:         }
                   1831:         if ($addtotal > 0) {
1.50      raeburn  1832:             $r->print('<li>'.&mt('[quant,_1,communication blocking period was,communication blocking periods were] added.',$addtotal).'</li>');
1.1       albertel 1833:         }
                   1834:         $r->print('</ul>');
                   1835:     } else {
                   1836:         $r->print($lt{'ncwm'});
                   1837:     }
                   1838:     $r->print('<br />');
                   1839:     return;
                   1840: }
                   1841: 
                   1842: sub get_dates_from_form {
                   1843:     my $item = shift;
                   1844:     my $startdate = &Apache::lonhtmlcommon::get_date_from_form('startdate_'.$item);
                   1845:     my $enddate   = &Apache::lonhtmlcommon::get_date_from_form('enddate_'.$item);
                   1846:     return ($startdate,$enddate);
                   1847: }
                   1848: 
                   1849: sub get_blockdates {
                   1850:     my ($records,$blockcount) = @_;
                   1851:     $$blockcount = 0;
                   1852:     %{$records} = &Apache::lonnet::dump('comm_block',
                   1853:                          $env{'course.'.$env{'request.course.id'}.'.domain'},
                   1854:                          $env{'course.'.$env{'request.course.id'}.'.num'}
                   1855:                          );
1.15      albertel 1856:     $$blockcount = keys(%{$records});
                   1857: 
                   1858:     if ((keys(%{$records}))[0] =~ /^error: 2 /) {
                   1859: 	$records = {};
                   1860: 	$$blockcount = 0;
1.1       albertel 1861:     }
                   1862: }
                   1863: 
1.44      raeburn  1864: sub get_block_choices {
                   1865:     my $item = shift;
                   1866:     my $blocklist;
                   1867:     my ($typeorder,$types) = &blocktype_text();
                   1868:     foreach my $type (@{$typeorder}) {
                   1869:         if ($env{'form.'.$type.'_'.$item}) {
                   1870:             $blocklist->{$type} = 'on'; 
                   1871:         } else {
                   1872:             $blocklist->{$type} = 'off';
                   1873:         }
                   1874:     }
                   1875:     return $blocklist;
                   1876: }
                   1877: 
1.1       albertel 1878: sub display_blocker_status {
                   1879:     my ($r,$records,$ltext) = @_;
                   1880:     my $parmcount = 0;
1.16      albertel 1881:   
1.1       albertel 1882:     my %lt = &Apache::lonlocal::texthash(
                   1883:         'modi' => 'Modify',
                   1884:         'canc' => 'Cancel',
                   1885:     );
1.44      raeburn  1886:     my ($typeorder,$types) = &blocktype_text();
1.18      albertel 1887:     $r->print(&Apache::loncommon::start_data_table());
1.1       albertel 1888:     $r->print(<<"END");
1.16      albertel 1889:   <tr>
1.44      raeburn  1890:     <th>$ltext->{'dura'}</th>
                   1891:     <th>$ltext->{'setb'}</th>
                   1892:     <th>$ltext->{'even'}</th>
                   1893:     <th>$ltext->{'blck'}</th>
                   1894:     <th>$ltext->{'actn'}?</th>
1.16      albertel 1895:   </tr>
1.1       albertel 1896: END
1.18      albertel 1897:     foreach my $record (sort(keys(%{$records}))) {
1.1       albertel 1898:         my $onchange = 'onFocus="javascript:window.document.forms['.
                   1899:                        "'blockform'].elements['modify_".$parmcount."'].".
                   1900:                        'checked=true;"';
1.18      albertel 1901:         my ($start,$end) = split(/____/,$record);
1.1       albertel 1902:         my $startform = &Apache::lonhtmlcommon::date_setter('blockform','startdate_'.$parmcount,$start,$onchange);
                   1903:         my $endform = &Apache::lonhtmlcommon::date_setter('blockform','enddate_'.$parmcount,$end,$onchange);
1.15      albertel 1904: 	
1.44      raeburn  1905: 	my ($setuname,$setudom,$title,$blocks) = 
                   1906: 	    &Apache::loncommon::parse_block_record($$records{$record});
1.21      albertel 1907: 	$title = &HTML::Entities::encode($title,'"<>&');
1.44      raeburn  1908:         my $settername = 
                   1909:            &Apache::loncommon::aboutmewrapper(
                   1910:                            &Apache::loncommon::plainname($setuname,$setudom),
                   1911:                            $setuname,$setudom);
1.18      albertel 1912:         $r->print(&Apache::loncommon::start_data_table_row());
1.1       albertel 1913:         $r->print(<<"END");
1.44      raeburn  1914:         <td>$ltext->{'star'}:&nbsp;$startform<br/>$ltext->{'endd'}:&nbsp;&nbsp;$endform</td>
1.1       albertel 1915:         <td>$settername</td>
1.18      albertel 1916:         <td><input type="text" name="title_$parmcount" size="15" value="$title" /><input type="hidden" name="key_$parmcount" value="$record" /></td>
1.44      raeburn  1917:         <td>
                   1918: END
                   1919:         foreach my $block (@{$typeorder}) {
                   1920:             my $blockstatus = '';
                   1921:             if ($blocks->{$block} eq 'on') {
                   1922:                 $blockstatus = 'checked="true"';
                   1923:             }
                   1924:             $r->print('<label><input type="checkbox" name="'.$block.'_'.$parmcount.'" '.$blockstatus.' value="1" />'.$types->{$block}.'</label><br />');
                   1925:         }
                   1926:         $r->print(<<"END");
                   1927:         </td>      
1.1       albertel 1928:         <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>
                   1929: END
1.18      albertel 1930:         $r->print(&Apache::loncommon::end_data_table_row());
                   1931:         $parmcount++;
1.1       albertel 1932:     }
                   1933:     $r->print(<<"END");
                   1934: </table>
                   1935: <br />
                   1936: <br />
                   1937: END
                   1938:     return $parmcount;
                   1939: }
                   1940: 
                   1941: sub display_addblocker_table {
                   1942:     my ($r,$parmcount,$ltext) = @_;
                   1943:     my $start = time;
                   1944:     my $end = $start + (60 * 60 * 2); #Default is an exam of 2 hours duration.
                   1945:     my $onchange = 'onFocus="javascript:window.document.forms['.
                   1946:                    "'blockform'].elements['add_".$parmcount."'].".
                   1947:                    'checked=true;"';
                   1948:     my $startform = &Apache::lonhtmlcommon::date_setter('blockform','startdate_'.$parmcount,$start,$onchange);
                   1949:     my $endform = &Apache::lonhtmlcommon::date_setter('blockform','enddate_'.$parmcount,$end,$onchange);
                   1950:     my %lt = &Apache::lonlocal::texthash(
                   1951:         'addb' => 'Add block',
                   1952:         'exam' => 'e.g., Exam 1',
                   1953:         'addn' => 'Add new communication blocking periods'
                   1954:     );
1.44      raeburn  1955:     my ($typeorder,$types) = &blocktype_text();
1.1       albertel 1956:     $r->print(<<"END");
                   1957: <h4>$lt{'addn'}</h4> 
1.18      albertel 1958: END
                   1959:     $r->print(&Apache::loncommon::start_data_table());
                   1960:     $r->print(<<"END");
1.16      albertel 1961:    <tr>
1.44      raeburn  1962:      <th>$ltext->{'dura'}</th>
                   1963:      <th>$ltext->{'even'} $lt{'exam'}</th>
                   1964:      <th>$ltext->{'blck'}</th>
                   1965:      <th>$ltext->{'actn'}?</th>
1.16      albertel 1966:    </tr>
1.18      albertel 1967: END
1.44      raeburn  1968:     $r->print(&Apache::loncommon::start_data_table_row());
1.18      albertel 1969:     $r->print(<<"END");
1.44      raeburn  1970:      <td>$ltext->{'star'}:&nbsp;$startform<br />$ltext->{'endd'}:&nbsp;&nbsp;$endform</td>
1.16      albertel 1971:      <td><input type="text" name="title_$parmcount" size="15" value="" /></td>
1.44      raeburn  1972:      <td>
                   1973: END
                   1974:     foreach my $block (@{$typeorder}) {
                   1975:         $r->print('<label><input type="checkbox" name="'.$block.'_'.$parmcount.'" value="1" />'.$types->{$block}.'</label><br />');
                   1976:      }
                   1977:      $r->print(<<"END");
                   1978:      </td> 
1.16      albertel 1979:      <td><label>$lt{'addb'}?&nbsp;<input type="checkbox" name="add_$parmcount" value="1" /></label></td>
1.1       albertel 1980: END
1.18      albertel 1981:     $r->print(&Apache::loncommon::end_data_table_row());
                   1982:     $r->print(&Apache::loncommon::end_data_table());
1.1       albertel 1983:     return;
                   1984: }
                   1985: 
1.44      raeburn  1986: sub blocktype_text {
                   1987:     my %types = &Apache::lonlocal::texthash(
                   1988:         'com' => 'Messaging',
                   1989:         'chat' => 'Chat',
                   1990:         'boards' => 'Discussion',
                   1991:         'port' => 'Portfolio',
1.45      raeburn  1992:         'groups' => 'Groups',
                   1993:         'blogs' => 'Blogs',
1.18      albertel 1994:     );
1.45      raeburn  1995:     my $typeorder = ['com','chat','boards','port','groups','blogs'];
1.44      raeburn  1996:     return ($typeorder,\%types);
1.1       albertel 1997: }
                   1998: 
                   1999: # ----------------------------------------------------------- Display a message
                   2000: 
                   2001: sub displaymessage {
1.53      raeburn  2002:     my ($r,$msgid,$folder,$msgstatus)=@_;
1.1       albertel 2003:     my $suffix=&Apache::lonmsg::foldersuffix($folder);
                   2004:     my %blocked = ();
                   2005:     my %setters = ();
                   2006:     my $numblocked = 0;
1.33      albertel 2007:     my $crstype = &Apache::loncommon::course_type();
1.30      raeburn  2008: 
1.1       albertel 2009: # info to generate "next" and "previous" buttons and check if message is blocked
1.44      raeburn  2010:     my ($startblock,$endblock) = &Apache::loncommon::blockcheck(\%setters,'com');
1.53      raeburn  2011:     my @messages=&sortedmessages(\%blocked,$startblock,$endblock,\$numblocked,$folder,$msgstatus);
1.1       albertel 2012:     if ( $blocked{$msgid} eq 'ON' ) {
                   2013:         &printheader($r,'/adm/email',&mt('Display a Message'));
                   2014:         $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.'));
                   2015:         &build_block_table($r,$startblock,$endblock,\%setters);
                   2016:         return;
                   2017:     }
1.59      raeburn  2018:     if ($msgstatus eq '') {
                   2019:         &statuschange($msgid,'read',$folder);
                   2020:     }
1.1       albertel 2021:     my %message=&Apache::lonnet::get('nohist_email'.$suffix,[$msgid]);
                   2022:     my %content=&Apache::lonmsg::unpackagemsg($message{$msgid});
                   2023:     my $counter=0;
                   2024:     $r->print('<pre>');
1.29      www      2025:     my $escmsgid=&escape($msgid);
1.1       albertel 2026:     foreach (@messages) {
                   2027: 	if ($_->[5] eq $escmsgid){
                   2028: 	    last;
                   2029: 	}
                   2030: 	$counter++;
                   2031:     }
                   2032:     $r->print('</pre>');
                   2033:     my $number_of_messages = scalar(@messages); #subtract 1 for last index
                   2034: # start output
1.29      www      2035:     &printheader($r,'/adm/email?display='.&escape($msgid),'Display a Message','',$content{'baseurl'});
1.1       albertel 2036:     my %courseinfo=&Apache::lonnet::coursedescription($content{'courseid'});
                   2037: # Functions
                   2038:     $r->print('<table border="2" width="100%"><tr bgcolor="#FFFFAA"><td>'.&mt('Functions').':</td>'.
1.29      www      2039: 	      '<td><a href="/adm/email?replyto='.&escape($msgid).$sqs.
1.1       albertel 2040: 	      '"><b>'.&mt('Reply').'</b></a></td>'.
1.29      www      2041: 	      '<td><a href="/adm/email?forward='.&escape($msgid).$sqs.
1.1       albertel 2042: 	      '"><b>'.&mt('Forward').'</b></a></td>'.
1.29      www      2043: 	      '<td><a href="/adm/email?markunread='.&escape($msgid).$sqs.
1.1       albertel 2044: 	      '"><b>'.&mt('Mark Unread').'</b></a></td>'.
1.29      www      2045: 	      '<td><a href="/adm/email?markdel='.&escape($msgid).$sqs.
1.1       albertel 2046: 	      '"><b>'.&mt('Delete').'</b></a></td>'.
                   2047: 	      '<td><a href="/adm/email?'.$sqs.
                   2048: 	      '"><b>'.&mt('Back to Folder Display').'</b></a></td>');
                   2049:     if ($counter > 0){
                   2050: 	$r->print('<td><a href="/adm/email?display='.$messages[$counter-1]->[5].$sqs.
                   2051: 		  '"><b>'.&mt('Previous').'</b></a></td>');
                   2052:     }
                   2053:     if ($counter < $number_of_messages - 1){
                   2054: 	$r->print('<td><a href="/adm/email?display='.$messages[$counter+1]->[5].$sqs.
                   2055: 		  '"><b>'.&mt('Next').'</b></a></td>');
                   2056:     }
                   2057:     $r->print('</tr></table>');
1.59      raeburn  2058:     my $symb;
                   2059:     if (defined($content{'symb'})) {
                   2060:         $symb = $content{'symb'};
                   2061:     } elsif (defined($content{'baseurl'})) {
                   2062:         $symb=&Apache::lonnet::symbread($content{'baseurl'});
                   2063:     }
1.1       albertel 2064:     if ($env{'user.adv'}) {
                   2065: 	$r->print('<table border="2" width="100%"><tr bgcolor="#FFAAAA"><td>'.&mt('Currently available actions (will open extra window)').':</td>');
                   2066: 	if (&Apache::lonnet::allowed('vgr',$env{'request.course.id'})) {
                   2067: 		$r->print('<td><b>'.&Apache::loncommon::track_student_link(&mt('View recent activity'),$content{'sendername'},$content{'senderdomain'},'check').'</b></td>');
                   2068: 	    }
                   2069: 	if (&Apache::lonnet::allowed('opa',$env{'request.course.id'}) && $symb) {
                   2070: 	    $r->print('<td><b>'.&Apache::loncommon::pprmlink(&mt('Set/Change parameters'),$content{'sendername'},$content{'senderdomain'},$symb,'check').'</b></td>');
                   2071: 	}
                   2072: 	if (&Apache::lonnet::allowed('mgr',$env{'request.course.id'}) && $symb) {
                   2073: 	    $r->print('<td><b>'.&Apache::loncommon::pgrdlink(&mt('Set/Change grades'),$content{'sendername'},$content{'senderdomain'},$symb,'check').'</b></td>');
                   2074: 	}
                   2075: 	$r->print('</tr></table>');
                   2076:     }
1.73      raeburn  2077:     my ($tolist,$cclist);
                   2078:     my (@recipients,@ccs);
                   2079:     if (ref($content{'recuser'}) eq 'ARRAY') {
                   2080:         for (my $i=0; $i<@{$content{'recuser'}}; $i++) {
                   2081:             $recipients[$i] =  &Apache::loncommon::aboutmewrapper(
                   2082:                &Apache::loncommon::plainname($content{'recuser'}[$i],
1.1       albertel 2083:                                       $content{'recdomain'}[$i]),
1.73      raeburn  2084:                   $content{'recuser'}[$i],$content{'recdomain'}[$i]).
                   2085:            ' ('.$content{'recuser'}[$i].' at '.$content{'recdomain'}[$i].') ';
                   2086:         }
1.1       albertel 2087:     }
                   2088:     $tolist = join(', ',@recipients);
1.73      raeburn  2089:     if ($content{'recipid'}) {
                   2090:         @ccs = &retrieve_cc_recips('display',%content);
                   2091:         $cclist = join(', ',@ccs);
                   2092:     }
1.59      raeburn  2093:     my ($restitle,$baseurl,$refers_to);
                   2094:     if (defined($content{'resource_title'})) {
                   2095:         $restitle = $content{'resource_title'};
                   2096:     } else {
                   2097:         if (defined($content{'baseurl'})) {
                   2098:             $restitle = &Apache::lonnet::gettitle($content{'baseurl'});
                   2099:         }
                   2100:     }
                   2101:     if (defined($content{'baseurl'})) {
                   2102:         $baseurl = &Apache::lonenc::check_encrypt($content{'baseurl'});
                   2103:     }
1.68      www      2104:     $r->print(&Apache::loncommon::student_image_tag($content{'senderdomain'},$content{'sendername'}));
1.73      raeburn  2105:     $r->print('<br /><b>'.&mt('Subject').':</b> '.$content{'subject'});
                   2106:     if ($folder eq 'sent') {
                   2107:         $r->print('<br /><b>'.&mt('To').':</b> '.$tolist);
                   2108:     } else {
                   2109:         $r->print('<br /><b>'.&mt('From').':</b> '.
1.1       albertel 2110: 	      &Apache::loncommon::aboutmewrapper(
                   2111: 						 &Apache::loncommon::plainname($content{'sendername'},$content{'senderdomain'}),
                   2112: 						 $content{'sendername'},$content{'senderdomain'}).' ('.
                   2113: 	      $content{'sendername'}.' at '.
1.73      raeburn  2114: 	      $content{'senderdomain'}.') ');
                   2115:         if ($cclist) {
                   2116:             $r->print('<br /><b>'.&mt('Cc').':</b> '.$cclist);
                   2117:         }
                   2118:     }
                   2119:     if ($content{'courseid'}) {
                   2120:         $r->print('<br /><b>'.&mt($crstype).':</b> '.$courseinfo{'description'});
                   2121:         if ($content{'coursesec'}) {
                   2122:             $r->print(' ('.&mt('Section').': '.$content{'coursesec'}.')');
                   2123:         }
                   2124:     }
                   2125:     $r->print('<br /><b>'.&mt('Time').':</b> '.$content{'time'});
1.59      raeburn  2126:     if ($baseurl) {
                   2127:         if (defined($content{'courseid'}) && defined($env{'request.course.id'})) {
                   2128:             if ($content{'courseid'} eq $env{'request.course.id'}) {
                   2129:                 my $symblink;
1.62      raeburn  2130:                 my $showsymb = &Apache::lonenc::check_decrypt($symb);
                   2131:                 my $showurl = &Apache::lonenc::check_decrypt($baseurl);
                   2132:                 my $encrypturl = &Apache::lonnet::EXT('resource.0.encrypturl',
                   2133:                               $showsymb,$env{'user.domain'},$env{'user.name'});
1.59      raeburn  2134:                 if ($symb) {
1.62      raeburn  2135:                     if ($encrypturl =~ /^yes$/i && !$env{'request.role.adv'}) {
                   2136:                         $showsymb = &Apache::lonenc::check_encrypt($symb);
                   2137:                     }
                   2138:                     $symblink = '?symb='.$showsymb;
                   2139:                 }
                   2140:                 if ($encrypturl =~ /^yes$/i && !$env{'request.role.adv'}) {
                   2141:                     $showurl = $baseurl;
1.59      raeburn  2142:                 }
1.62      raeburn  2143:                 $r->print('<br /><b>'.&mt('Refers to').':</b> <a href="'.$showurl.$symblink.'">'.$restitle.'</a>');
1.59      raeburn  2144:                 $refers_to = 1;
                   2145:             }
                   2146:         }
                   2147:         if (!$refers_to) {
                   2148:             if ($baseurl =~ m-^/enc/-) {
                   2149:                 if (defined($content{'courseid'})) {
1.62      raeburn  2150:                     if (!$env{'request.course.id'}) {
                   2151:                         my $unencurl =
                   2152:                            &Apache::lonenc::unencrypted($baseurl,
                   2153:                                                         $content{'courseid'});
                   2154:                         if ($unencurl ne '') {
                   2155:                             if (&Apache::lonnet::allowed('bre',$unencurl)) {
                   2156:                                 $r->print('<br /><b>'.&mt('Refers to').
                   2157:                                           ':</b> <a href="'.$unencurl.'">'.
                   2158:                                           $restitle.'</a>');
                   2159:                             }
1.59      raeburn  2160:                         }
                   2161:                     }
                   2162:                 }
                   2163:             } else {
                   2164:                 if (&Apache::lonnet::allowed('bre',$baseurl)) {
                   2165:                     $r->print('<br /><b>'.&mt('Refers to').
                   2166:                               ':</b> <a href="'.$baseurl.
                   2167:                               '">'.$restitle.'</a>');
                   2168:                 }
                   2169:             }
                   2170:         }
                   2171:     }
                   2172:     $r->print('<p><pre>'.
1.1       albertel 2173: 	      &Apache::lontexconvert::msgtexconverted($content{'message'},1).
                   2174: 	      '</pre><hr />'.&displayresource(%content).'</p>');
1.59      raeburn  2175:     return;
1.1       albertel 2176: }
                   2177: 
1.73      raeburn  2178: sub retrieve_cc_recips {
                   2179:     my ($context,%content) = @_; 
                   2180:     my %reciphash =
                   2181:          &Apache::lonnet::get('nohist_emailrecip',[$content{'recipid'}],
                   2182:                               $content{'senderdomain'},$content{'sendername'});
                   2183:     my $recipinfo = $reciphash{$content{'recipid'}};
                   2184:     my @ccs;
                   2185:     if (ref($recipinfo) eq 'HASH') {
                   2186:         if (ref($recipinfo->{'cc'}) eq 'HASH') {
                   2187:             foreach my $cc (sort(keys(%{$recipinfo->{'cc'}}))) {
                   2188:                 my ($ccname,$ccdom) = split(/:/,$cc);
                   2189:                 if (!(($ccname eq $env{'user.name'}) &&
                   2190:                      ($ccdom eq $env{'user.domain'}))) {
                   2191:                     my $showcc ='<span class="LC_nobreak">';
                   2192:                     if ($context eq 'replying') { 
1.74      albertel 2193:                         $showcc = '<label><input type="checkbox" name="replying_cc" value="'.$cc.'" />';
1.73      raeburn  2194:                     }
                   2195:                     $showcc .= &Apache::loncommon::aboutmewrapper(
                   2196:                                        &Apache::loncommon::plainname($ccname,
1.74      albertel 2197:                                          $ccdom),$ccname,$ccdom);
                   2198: 		    if ($context eq 'replying') {
                   2199: 			$showcc .='</label>';
                   2200: 		    }
                   2201: 		    $showcc .= '</span>';
                   2202: 		    push(@ccs,$showcc);
1.73      raeburn  2203:                 }
                   2204:             }
                   2205:         }
                   2206:     }
                   2207:     return @ccs;
                   2208: }
                   2209: 
1.1       albertel 2210: # =========================================================== Show the citation
                   2211: 
                   2212: sub displayresource {
                   2213:     my %content=@_;
                   2214: #
                   2215: # If the recipient is in the same course that the message was sent from and
                   2216: # has sufficient privileges, show "all details," else show citation
                   2217: #
                   2218:     if (($env{'request.course.id'} eq $content{'courseid'})
                   2219:      && (&Apache::lonnet::allowed('vgr',$content{'courseid'}))) {
1.59      raeburn  2220:         my $symb;
                   2221:         if (defined($content{'symb'})) {
                   2222:             $symb = $content{'symb'};
                   2223:         } else { 
                   2224: 	    $symb=&Apache::lonnet::symbread($content{'baseurl'});
                   2225:         }
1.1       albertel 2226: # Could not get a symb, give up
                   2227: 	unless ($symb) { return $content{'citation'}; }
                   2228: # Have a symb, can render
                   2229: 	return '<h2>'.&mt('Current attempts of student (if applicable)').'</h2>'.
                   2230: 	    &Apache::loncommon::get_previous_attempt($symb,
                   2231: 						     $content{'sendername'},
                   2232: 						     $content{'senderdomain'},
                   2233: 						     $content{'courseid'}).
                   2234: 	    '<hr /><h2>'.&mt('Current screen output (if applicable)').'</h2>'.
                   2235: 	    &Apache::loncommon::get_student_view($symb,
                   2236: 						 $content{'sendername'},
                   2237: 						 $content{'senderdomain'},
                   2238: 						 $content{'courseid'}).
                   2239: 	    '<h2>'.&mt('Correct Answer(s) (if applicable)').'</h2>'.
                   2240: 	    &Apache::loncommon::get_student_answers($symb,
                   2241: 						    $content{'sendername'},
                   2242: 						    $content{'senderdomain'},
                   2243: 						    $content{'courseid'});
                   2244:     } elsif ($env{'user.adv'}) {
                   2245: 	return $content{'citation'};
                   2246:     }
                   2247:     return '';
                   2248: }
                   2249: 
                   2250: # ================================================================== The Header
                   2251: 
                   2252: sub header {
                   2253:     my ($r,$title,$baseurl)=@_;
                   2254:     
                   2255:     my $extra = &Apache::loncommon::studentbrowser_javascript();
                   2256:     if ($baseurl) {
1.41      albertel 2257: 	$extra .= "<base href=\"".&Apache::lonnet::absolute_url()."/$baseurl\" />";
1.1       albertel 2258:     }
                   2259:     $r->print(&Apache::loncommon::start_page('Communication and Messages',
1.38      raeburn  2260:  					$extra));
1.1       albertel 2261:     $r->print(&Apache::lonhtmlcommon::breadcrumbs
1.38      raeburn  2262:      		(($title?$title:'Communication and Messages')));
1.1       albertel 2263: }
                   2264: 
                   2265: # ---------------------------------------------------------------- Print header
                   2266: 
                   2267: sub printheader {
                   2268:     my ($r,$url,$desc,$title,$baseurl)=@_;
                   2269:     &Apache::lonhtmlcommon::add_breadcrumb
                   2270: 	({href=>$url,
                   2271: 	  text=>$desc});
                   2272:     &header($r,$title,$baseurl);
                   2273: }
                   2274: 
                   2275: # ------------------------------------------------------------ Store the comment
                   2276: 
                   2277: sub storecomment {
                   2278:     my ($r)=@_;
                   2279:     my $msgtxt=&Apache::lonfeedback::clear_out_html($env{'form.message'});
                   2280:     my $cleanmsgtxt='';
1.42      raeburn  2281:     foreach my $line (split(/[\n\r]/,$msgtxt)) {
                   2282: 	unless ($line=~/^\s*(\>|\&gt\;)/) {
                   2283: 	    $cleanmsgtxt.=$line."\n";
1.1       albertel 2284: 	}
                   2285:     }
1.29      www      2286:     my $key=&escape($env{'form.baseurl'}).'___'.time;
1.1       albertel 2287:     &Apache::lonnet::put('nohist_stored_comments',{ $key => $cleanmsgtxt });
                   2288: }
                   2289: 
                   2290: sub storedcommentlisting {
                   2291:     my ($r)=@_;
                   2292:     my %msgs=&Apache::lonnet::dump('nohist_stored_comments',undef,undef,
1.29      www      2293:        '^'.&escape(&escape($env{'form.showcommentbaseurl'})));
1.72      albertel 2294:     $r->print(&Apache::loncommon::start_page('Saved Comment Listing',undef,
1.1       albertel 2295: 					     {'onlybody' => 1}));
                   2296:     if ((keys %msgs)[0]=~/^error\:/) {
1.72      albertel 2297: 	$r->print(&mt('No saved comments yet.'));
1.1       albertel 2298:     } else {
                   2299: 	my $found=0;
1.42      raeburn  2300: 	foreach my $key (sort(keys(%msgs))) {
                   2301: 	    $r->print("\n".$msgs{$key}."<hr />");
1.1       albertel 2302: 	    $found=1;
                   2303: 	}
                   2304: 	unless ($found) {
1.72      albertel 2305: 	    $r->print(&mt('No saved comments yet for this resource.'));
1.1       albertel 2306: 	}
                   2307:     }
                   2308: }
                   2309: 
                   2310: # ---------------------------------------------------------------- Send an email
                   2311: 
                   2312: sub sendoffmail {
                   2313:     my ($r,$folder)=@_;
                   2314:     my $suffix=&Apache::lonmsg::foldersuffix($folder);
                   2315:     my $sendstatus='';
1.71      raeburn  2316:     my %msg_status;
                   2317:     my $numsent = 0;
                   2318:     my $nosentstore = 1;
1.38      raeburn  2319:     my ($cdom,$cnum,$group);
                   2320:     if (exists($env{'form.group'})) {
                   2321:         $group = $env{'form.group'};
                   2322:     }
                   2323:     if (exists($env{'request.course.id'})) {
                   2324:         $cnum = $env{'course.'.$env{'request.course.id'}.'.num'};
                   2325:         $cdom = $env{'course.'.$env{'request.course.id'}.'.domain'};
                   2326:     }
1.1       albertel 2327:     if ($env{'form.send'}) {
1.53      raeburn  2328:         if (!$env{'form.multiforward'}) { 
                   2329:             if ($group eq '') {
                   2330: 	        &printheader($r,'','Messages being sent.');
                   2331:             } else {
                   2332:                 $r->print(&groupmail_header('sending',$group));
                   2333:             }
1.38      raeburn  2334:         }
1.1       albertel 2335: 	$r->rflush();
                   2336: 	my %content=();
                   2337: 	undef %content;
                   2338: 	if ($env{'form.forwid'}) {
                   2339: 	    my $msgid=$env{'form.forwid'};
                   2340: 	    my %message=&Apache::lonnet::get('nohist_email'.$suffix,[$msgid]);
                   2341: 	    %content=&Apache::lonmsg::unpackagemsg($message{$msgid},1);
                   2342: 	    &statuschange($msgid,'forwarded',$folder);
                   2343: 	    $env{'form.message'}.="\n\n-- Forwarded message --\n\n".
                   2344: 		$content{'message'};
                   2345: 	}
                   2346: 	if ($env{'form.replyid'}) {
                   2347: 	    my $msgid=$env{'form.replyid'};
                   2348: 	    my %message=&Apache::lonnet::get('nohist_email'.$suffix,[$msgid]);
                   2349: 	    %content=&Apache::lonmsg::unpackagemsg($message{$msgid},1);
                   2350: 	    &statuschange($msgid,'replied',$folder);
                   2351: 	}
1.13      albertel 2352: 
1.35      albertel 2353: 	my @to =
1.37      raeburn  2354: 	    &Apache::loncommon::get_env_multiple('form.selectedusers_forminput');
1.25      foxr     2355: 	my $mode = $env{'form.sendmode'};
                   2356: 
1.73      raeburn  2357: 	my (%toaddr,$cc,$bcc);
1.35      albertel 2358: 	if (@to) {
                   2359: 	    foreach my $dest (@to) {
1.27      albertel 2360: 		my ($user,$domain) = split(/:/, $dest);
1.25      foxr     2361: 		if (($user ne '') && ($domain ne '')) {
                   2362: 		    my $address = $user.":".$domain; # How the code below expects it.
                   2363: 		    $toaddr{$address} = '';
                   2364: 		}
                   2365: 	    }
                   2366: 	}
                   2367: 
1.1       albertel 2368: 	if ($env{'form.sendmode'} eq 'group') {
1.25      foxr     2369: 	     foreach my $address (keys(%env)) {
                   2370: 	 	if ($address=~/^form\.send\_to\_\&\&\&[^\&]*\&\&\&\_(.+)$/) {
                   2371: 	 	    $toaddr{$1}='';
                   2372: 	 	}
1.1       albertel 2373: 	    }
                   2374: 	} elsif ($env{'form.sendmode'} eq 'upload') {
1.71      raeburn  2375:             $nosentstore = 0;
1.13      albertel 2376: 	    foreach my $line (split(/[\n\r\f]+/,$env{'form.upfile'})) {
1.43      raeburn  2377:                 my ($rec,$txt) = ($line =~ /^([^:]+:[^:]+):(.*)$/);
1.1       albertel 2378: 		if ($txt) {
1.43      raeburn  2379:                     $rec =~ s/^\s+//;
1.44      raeburn  2380:                     $rec =~ s/\s+$//;
1.1       albertel 2381: 		    $toaddr{$rec}.=$txt."\n";
                   2382: 		}
                   2383: 	    }
                   2384: 	} else {
1.25      foxr     2385: 	    if (($env{'form.recuname'} ne '') && ($env{'form.recdomain'} ne '')) {
                   2386: 		$toaddr{$env{'form.recuname'}.':'.$env{'form.recdomain'}}='';
1.73      raeburn  2387:                 $cc->{$env{'form.recuname'}.':'.$env{'form.recdomain'}}='';
1.25      foxr     2388: 	    }
1.1       albertel 2389: 	}
1.73      raeburn  2390: 	if ($env{'form.additionalrec_cc'}) {
                   2391: 	    foreach my $rec (split(/\s*,\s*/,$env{'form.additionalrec_cc'})) {
1.43      raeburn  2392: 		my ($auname,$audom)=split(/:/,$rec);
1.25      foxr     2393: 		if (($auname ne "") && ($audom ne "")) {
1.73      raeburn  2394:                     $toaddr{$auname.':'.$audom}='';
                   2395: 		    $cc->{$auname.':'.$audom}='';
1.25      foxr     2396: 		}
1.1       albertel 2397: 	    }
                   2398: 	}
1.73      raeburn  2399:         if ($env{'form.additionalrec_bcc'}) {
                   2400:             foreach my $rec (split(/\s*,\s*/,$env{'form.additionalrec_bcc'})) {
                   2401:                 my ($auname,$audom)=split(/:/,$rec);
                   2402:                 if (($auname ne "") && ($audom ne "")) {
                   2403:                     $toaddr{$auname.':'.$audom}='';
                   2404:                     $bcc->{$auname.':'.$audom}='';
                   2405:                 }
                   2406:             }
                   2407:         }
                   2408:         if ($env{'form.replying_cc'}) {
                   2409:             my @ccreplies = 
                   2410:                 &Apache::loncommon::get_env_multiple('form.replying_cc');
                   2411:             foreach my $rec (@ccreplies) {
                   2412:                 my ($auname,$audom)=split(/:/,$rec);
                   2413:                 if (($auname ne "") && ($audom ne "")) {
                   2414:                     $toaddr{$auname.':'.$audom}='';
                   2415:                     $cc->{$auname.':'.$audom}='';
                   2416:                 }
                   2417:             }
                   2418:         }
1.1       albertel 2419:         my $savemsg;
                   2420:         my $msgtype;
                   2421:         my %sentmessage;
1.7       albertel 2422:         my $msgsubj=&Apache::lonfeedback::clear_out_html($env{'form.subject'},
                   2423: 							 undef,1);
1.1       albertel 2424:         if ((($env{'form.critmsg'}) || ($env{'form.sendbck'})) &&
                   2425:             (&Apache::lonnet::allowed('srm',$env{'request.course.id'})
                   2426: 	     || &Apache::lonnet::allowed('srm',$env{'request.course.id'}.
                   2427: 					 '/'.$env{'request.course.sec'})
                   2428: 	     )) {
                   2429:             $savemsg=&Apache::lonfeedback::clear_out_html($env{'form.message'},1);
                   2430:             $msgtype = 'critical';
                   2431:         } else {
                   2432:             $savemsg=&Apache::lonfeedback::clear_out_html($env{'form.message'});
                   2433:         }
1.73      raeburn  2434:         my %reciphash = (
                   2435:                            cc => $cc,
                   2436:                            bcc => $bcc,
                   2437:                         ); 
                   2438:         my ($recipid,$recipstatus) = 
                   2439:             &Apache::lonmsg::store_recipients($msgsubj,$env{'user.name'},
                   2440:                                        $env{'user.domain'},\%reciphash);
                   2441:         if ($recipstatus ne 'ok') {
                   2442:             &Apache::lonnet::logthis('Failed to store Bcc and Cc recipients for '.$env{'user.name'}.':'.$env{'user.domain'});
                   2443:         }  
1.71      raeburn  2444:         my @recusers;
                   2445:         my @recudoms;
1.13      albertel 2446: 	foreach my $address (sort(keys(%toaddr))) {
                   2447: 	    my ($recuname,$recdomain)=split(/\:/,$address);
1.1       albertel 2448:             my $msgtxt = $savemsg;
1.73      raeburn  2449:             if ($toaddr{$address}) {
                   2450: 	        $msgtxt.='<hr />'.$toaddr{$address};
                   2451:             }
1.12      albertel 2452: 	    my @thismsg;
1.60      raeburn  2453: 	    if ($msgtype eq 'critical') {
                   2454: 		$r->print(&mt('Sending critical message').' '.
                   2455:                               $recuname.':'.$recdomain.': ');
1.13      albertel 2456: 		@thismsg=
                   2457: 		    &Apache::lonmsg::user_crit_msg($recuname,$recdomain,
                   2458: 						   $msgsubj,$msgtxt,
                   2459: 						   $env{'form.sendbck'},
                   2460: 						   $env{'form.permanent'},
1.71      raeburn  2461: 						   \$sentmessage{$address},
1.73      raeburn  2462:                                                    $nosentstore,$recipid);
1.1       albertel 2463: 	    } else {
1.14      albertel 2464: 		$r->print(&mt('Sending').' '.$recuname.':'.$recdomain.': ');
1.13      albertel 2465: 		@thismsg=
                   2466: 		    &Apache::lonmsg::user_normal_msg($recuname,$recdomain,
                   2467: 						     $msgsubj,$msgtxt,
                   2468: 						     $content{'citation'},
                   2469: 						     undef,undef,
                   2470: 						     $env{'form.permanent'},
1.71      raeburn  2471: 						     \$sentmessage{$address},
                   2472:                                                      undef,undef,undef,
1.73      raeburn  2473:                                                      $nosentstore,$recipid);
1.71      raeburn  2474:             }
                   2475: 	    $msg_status{$recuname.':'.$recdomain}=join(' ',@thismsg);
                   2476: 	    if ($msg_status{$recuname.':'.$recdomain} =~ /(ok|con_delayed)/) {  
                   2477: 	        $numsent++;
                   2478:                 push(@recusers,$recuname);
                   2479:                 push(@recudoms,$recdomain);
1.1       albertel 2480: 	    }
1.12      albertel 2481: 	    $sendstatus.=' '.join(' ',@thismsg);
1.1       albertel 2482: 	}
1.71      raeburn  2483:         my $subj_prefix;
                   2484:         if ($numsent > 0) {
                   2485:             if (($env{'request.course.id'}) && 
                   2486:                 (($env{'form.sendmode'} eq 'group') ||
                   2487:                  ($env{'form.courserecord'}) ||
                   2488:                  ($msgtype eq 'critical'))) {
                   2489:                 if ($msgtype eq 'critical') {
                   2490:                     $subj_prefix = 'Critical.';
                   2491:                 } elsif ($env{'form.sendmode'} eq 'group') {
                   2492:                     $subj_prefix = 'Broadcast.';
                   2493:                 } else {
                   2494:                     $subj_prefix = 'Archive';
                   2495:                 }
                   2496:                 my ($specialmsgid,$specialresult);
                   2497:                 my $course_str = &escape('['.$cnum.':'.$cdom.']');
1.1       albertel 2498: 
1.71      raeburn  2499:                 $specialresult = 
                   2500:                     &Apache::lonmsg::user_normal_msg_raw($cnum,$cdom,
                   2501:                         $subj_prefix.' '.$course_str,$savemsg,undef,undef,
                   2502:                         undef,undef,undef,\$specialmsgid,undef,undef,undef,
                   2503:                         undef,undef,1);
1.29      www      2504:                 $specialmsgid = &unescape($specialmsgid);
1.71      raeburn  2505:                 if ($specialresult eq 'ok') {
                   2506:                     my ($stamp,$crssubj,$msgname,$msgdom,$msgcount,$context,$pid) =
                   2507: 		        split(/\:/,&unescape($specialmsgid));
                   2508: 
                   2509:                     foreach my $recipient (sort(keys(%toaddr))) {
                   2510:                         if ($msg_status{$recipient} =~ /\s*(ok|con_delayed)\s*/) {
                   2511:                             my $usersubj = $subj_prefix.'['.$recipient.']';
                   2512:                             my $usermsgid = 
                   2513: 			        &Apache::lonmsg::buildmsgid($stamp,$usersubj,
                   2514: 							    $msgname,$msgdom,
                   2515: 							    $msgcount,$context,
                   2516: 							    $pid);
                   2517:                             &Apache::lonmsg::user_normal_msg_raw($cnum,$cdom,
                   2518:                                 $subj_prefix.' ['.$recipient.']',$msgsubj,
                   2519:                                 undef,undef,undef,undef,$usermsgid,undef,
                   2520:                                 undef,$specialmsgid,undef,undef,undef,1);
                   2521:                         }
                   2522:                     }
                   2523:                     if (($env{'form.sendmode'} ne 'upload') && (@recusers > 0)) {
                   2524:                         &Apache::lonmsg::process_sent_mail($msgsubj,
                   2525:                            $subj_prefix,$numsent,$stamp,$msgname,$msgdom,
                   2526:                            $msgcount,$context,$pid,$savemsg,\@recusers,
                   2527:                            \@recudoms);
1.1       albertel 2528:                     }
1.71      raeburn  2529:                 } else {
                   2530:                     &Apache::lonnet::logthis('Failed to create record of critical, broadcast or archived message in '.$env{'course.'.$env{'request.course.id'}.'.num'}.' at '.$env{'course.'.$env{'request.course.id'}.'.domain'}.' - no msgid generated');
1.1       albertel 2531:                 }
                   2532:             } else {
1.71      raeburn  2533:                 my $stamp = time;
                   2534:                 my $msgcount = &Apache::lonmsg::get_uniq();
                   2535:                 my $context = &Apache::lonmsg::get_course_context();
                   2536:                 &Apache::lonmsg::process_sent_mail($msgsubj,$subj_prefix,
                   2537:                        $numsent,$stamp,$env{'user.name'},
                   2538:                        $env{'user.domain'},$msgcount,$context,
                   2539:                        $$,$savemsg,\@recusers,\@recudoms);
1.1       albertel 2540:             }
                   2541:         }
1.71      raeburn  2542:         if (!$env{'form.multiforward'}) { 
                   2543:             if ($sendstatus=~/^(\s*(?:ok|con_delayed)\s*)*$/) {
                   2544: 	        $r->print('<br /><span class="LC_success">'.&mt('Completed.').
                   2545:                           '</span>');
                   2546: 	        if ($env{'form.displayedcrit'}) {
                   2547: 	            &discrit($r);
                   2548:                 }
                   2549:                 if ($group ne '') {
                   2550:                     $r->print(&groupmail_sent($group,$cdom,$cnum)); 
                   2551: 	        } else {
                   2552: 	            &Apache::loncommunicate::menu($r);
                   2553: 	        }
                   2554:             } else {
                   2555: 	        $r->print('<p><span class="LC_error">'.&mt('Could not deliver message').'</span> '.
                   2556: 		          &mt('Please use the browser "Back" button and correct the recipient addresses '."($sendstatus)").'</p>');
1.53      raeburn  2557:             }
1.38      raeburn  2558:         }
1.1       albertel 2559:     }
1.53      raeburn  2560:     return $sendstatus;
1.1       albertel 2561: }
                   2562: 
                   2563: # ===================================================================== Handler
                   2564: 
                   2565: sub handler {
                   2566:     my $r=shift;
                   2567: 
                   2568: # ----------------------------------------------------------- Set document type
                   2569:     
                   2570:     &Apache::loncommon::content_type($r,'text/html');
                   2571:     $r->send_http_header;
                   2572:     
                   2573:     return OK if $r->header_only;
                   2574:     
                   2575: # --------------------------- Get query string for limited number of parameters
                   2576:     &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'},
                   2577:         ['display','replyto','forward','markread','markdel','markunread',
                   2578:          'sendreply','compose','sendmail','critical','recname','recdom',
                   2579:          'recordftf','sortedby','block','folder','startdis','interdis',
1.53      raeburn  2580: 	 'showcommentbaseurl','dismode','group','subject','text','ref',
                   2581:          'msgstatus']);
1.1       albertel 2582:     $sqs='&sortedby='.$env{'form.sortedby'};
                   2583: 
                   2584: # ------------------------------------------------------ They checked for email
                   2585:     unless ($env{'form.block'}) {
                   2586:         &Apache::lonnet::put('email_status',{'recnewemail'=>0});
                   2587:     }
                   2588: 
                   2589: # ----------------------------------------------------------------- Breadcrumbs
                   2590: 
                   2591:     &Apache::lonhtmlcommon::clear_breadcrumbs();
                   2592:     &Apache::lonhtmlcommon::add_breadcrumb
                   2593:         ({href=>"/adm/communicate",
                   2594:           text=>"Communication/Messages",
                   2595:           faq=>12,bug=>'Communication Tools',});
                   2596: 
                   2597: # ------------------------------------------------------------------ Get Folder
                   2598: 
                   2599:     my $folder=$env{'form.folder'};
                   2600:     unless ($folder) { 
                   2601: 	$folder=''; 
                   2602:     } else {
1.29      www      2603: 	$sqs.='&folder='.&escape($folder);
1.1       albertel 2604:     }
                   2605: # ------------------------------------------------------------ Get Display Mode
                   2606: 
                   2607:     my $dismode=$env{'form.dismode'};
                   2608:     unless ($dismode) { 
                   2609: 	$dismode=''; 
                   2610:     } else {
1.48      albertel 2611: 	$sqs.='&amp;dismode='.&escape($dismode);
1.1       albertel 2612:     }
                   2613: 
                   2614: # --------------------------------------------------------------------- Display
1.53      raeburn  2615:     my $msgstatus = $env{'form.msgstatus'};
1.1       albertel 2616:     $startdis=$env{'form.startdis'};
1.53      raeburn  2617:     if ($startdis ne '') {
                   2618:         $startdis--;
                   2619:     }
1.1       albertel 2620:     unless ($startdis) { $startdis=0; }
                   2621: 
                   2622:     if ($env{'form.firstview'}) {
                   2623: 	$startdis=0;
                   2624:     }
                   2625:     if ($env{'form.lastview'}) {
                   2626: 	$startdis=-1;
                   2627:     }
                   2628:     if ($env{'form.prevview'}) {
                   2629: 	$startdis--;
                   2630:     }
                   2631:     if ($env{'form.nextview'}) {
                   2632: 	$startdis++;
                   2633:     }
                   2634:     my $postedstartdis=$startdis+1;
                   2635:     $sqs.='&startdis='.$postedstartdis;
                   2636: 
                   2637: # --------------------------------------------------------------- Render Output
                   2638: 
                   2639:     if ($env{'form.display'}) {
1.53      raeburn  2640: 	&displaymessage($r,$env{'form.display'},$folder,$msgstatus);
1.1       albertel 2641:     } elsif ($env{'form.replyto'}) {
                   2642: 	&compout($r,'',$env{'form.replyto'},undef,undef,$folder,$dismode);
                   2643:     } elsif ($env{'form.confirm'}) {
                   2644: 	&printheader($r,'','Confirmed Receipt');
1.36      albertel 2645: 	my $replying = 0;
1.42      raeburn  2646: 	foreach my $envkey (keys(%env)) {
1.75      raeburn  2647: 	    if ($envkey=~/^form\.(rep)?rec\_(.*)$/) {
1.77    ! raeburn  2648:                 my $repchk = $1;
1.75      raeburn  2649:        		my $msgid = $2;
                   2650: 		$r->print('<b>'.&mt('Confirming Receipt').':</b> ');
                   2651: 		my $result = &Apache::lonmsg::user_crit_received($msgid);
                   2652:                 if ($result =~ /trans:\s+ok/) {
                   2653:                     &statuschange($msgid,'read');
                   2654:                 }
1.76      albertel 2655:                 $r->print($result.'<br />');
1.77    ! raeburn  2656:                 if ($repchk eq 'rep') {
1.75      raeburn  2657: 		    &compout($r,'','','',$msgid);
                   2658: 		    $replying = 1;
                   2659:                 }
1.1       albertel 2660: 	    }
                   2661: 	}
1.36      albertel 2662: 	if (!$replying) {
                   2663: 	    &discrit($r);
                   2664: 	}
1.1       albertel 2665:     } elsif ($env{'form.critical'}) {
                   2666: 	&printheader($r,'','Displaying Critical Messages');
                   2667: 	&discrit($r);
                   2668:     } elsif ($env{'form.forward'}) {
                   2669: 	&compout($r,$env{'form.forward'},undef,undef,undef,$folder);
                   2670:     } elsif ($env{'form.markdel'}) {
                   2671: 	&printheader($r,'','Deleted Message');
1.9       albertel 2672: 	my ($result,$msg) = 
                   2673: 	    &statuschange($env{'form.markdel'},'deleted',$folder);
                   2674: 	if (!$result) {
1.10      albertel 2675: 	    $r->print('<p class="LC_error">'.
                   2676: 		      &mt('Failed to delete the message.').'</p>'.
1.9       albertel 2677: 		      '<p class="LC_error">'.$msg."</p>\n");
                   2678: 	}
1.1       albertel 2679: 	&Apache::loncommunicate::menu($r);
1.53      raeburn  2680: 	&disall($r,($folder?$folder:$dismode),$msgstatus);
                   2681:     } elsif ($env{'form.markedaction'} eq 'markedforward') {
                   2682:         my $total = 0;
                   2683:         my @to_forward = &Apache::loncommon::get_env_multiple('form.delmark');
                   2684:         foreach my $msgid (@to_forward) {
                   2685:             &statuschange(&unescape($msgid),'forwarded',$folder);
                   2686:             $total ++;
                   2687:         }
                   2688:         if ($total > 0) {
                   2689:             &compout($r,undef,undef,undef,undef,$folder,$dismode,$total);
                   2690:         }
                   2691:     } elsif ($env{'form.markedaction'} eq 'markedread') {
                   2692:         my $total = 0;
                   2693:         my @to_markread = &Apache::loncommon::get_env_multiple('form.delmark');
                   2694:         foreach my $msgid (@to_markread) {
                   2695:             &statuschange(&unescape($msgid),'read',$folder);
                   2696:             $total ++;
                   2697:         }
                   2698:         &printheader($r,'','Marked Messages Read');
                   2699:         $r->print(&mt('Marked [_1] message(s) read',$total).'<p>');
                   2700:         &Apache::loncommunicate::menu($r);
                   2701:         &disall($r,($folder?$folder:$dismode),$msgstatus);
                   2702:     } elsif ($env{'form.markedaction'} eq 'markedunread') {
                   2703:         my $total = 0;
                   2704:         my @to_markunread = &Apache::loncommon::get_env_multiple('form.delmark');
                   2705:         foreach my $msgid (@to_markunread) {
                   2706:             &statuschange(&unescape($msgid),'new',$folder);
                   2707:             $total ++;
                   2708:         }
                   2709:         &printheader($r,'','Marked Messages Unread');
                   2710:         $r->print(&mt('Marked [_1] message(s) unread',$total).'<p>');
                   2711:         &Apache::loncommunicate::menu($r);
                   2712:         &disall($r,($folder?$folder:$dismode),$msgstatus);
                   2713:     } elsif ($env{'form.markedaction'} eq 'markedmove') {
                   2714:         my $destfolder = $env{'form.movetofolder'};
                   2715:         my %gotfolders = &Apache::lonmsg::get_user_folders();
                   2716:         &printheader($r,'','Moved Messages');
                   2717:         if (!defined($gotfolders{$destfolder})) {
                   2718:             $r->print(&mt('Destination folder [_1] is not a valid folder',
                   2719:                       $destfolder));
                   2720:         } else {
                   2721: 	    my ($total,$failed,@failed_msg)=(0,0);
                   2722:             my @to_move = &Apache::loncommon::get_env_multiple('form.delmark');
                   2723:             foreach my $msgid (@to_move) {
                   2724: 	        my ($result,$msg) = &movemsg(&unescape($msgid),$folder,
                   2725: 			                     $env{'form.movetofolder'});
                   2726: 	        if ($result) {
1.9       albertel 2727: 		    $total++;
1.53      raeburn  2728: 	        } else {
1.9       albertel 2729: 		    $failed++;
                   2730: 		    push(@failed_msg,$msg);
1.53      raeburn  2731: 	        }
1.1       albertel 2732: 	    }
1.53      raeburn  2733: 	    if ($failed) {
                   2734: 	        $r->print('<p class="LC_error">
1.10      albertel 2735:                           '.&mt('Failed to move [_1] message(s)',$failed).
                   2736: 		      '</p>');
1.53      raeburn  2737: 	        $r->print('<p class="LC_error">'.
                   2738: 	   	          join("</p>\n<p class=\"LC_error\">",@failed_msg).
                   2739: 		          "</p>\n");
                   2740: 	    }
                   2741: 	    $r->print(&mt('Moved [_1] message(s)',$total).'<p>');
                   2742:         }
1.1       albertel 2743: 	&Apache::loncommunicate::menu($r);
1.53      raeburn  2744: 	&disall($r,($folder?$folder:$dismode),$msgstatus);
                   2745:     } elsif ($env{'form.markedaction'} eq 'markeddel') {
1.9       albertel 2746: 	my ($total,$failed,@failed_msg)=(0,0);
1.53      raeburn  2747:         my @to_delete = &Apache::loncommon::get_env_multiple('form.delmark');
                   2748:         foreach my $msgid (@to_delete) {
                   2749: 	    my ($result,$msg) = &statuschange(&unescape($msgid),'deleted', 
                   2750: 				              $folder);
                   2751: 	    if ($result) {
                   2752: 	        $total++;
                   2753: 	    } else {
                   2754: 	        $failed++;
                   2755: 		push(@failed_msg,$msg);
1.1       albertel 2756: 	    }
                   2757: 	}
                   2758: 	&printheader($r,'','Deleted Messages');
1.9       albertel 2759: 	if ($failed) {
                   2760: 	    $r->print('<p class="LC_error">
1.10      albertel 2761:                           '.&mt('Failed to delete [_1] message(s)',$failed).
                   2762: 		      '</p>');
1.9       albertel 2763: 	    $r->print('<p class="LC_error">'.
                   2764: 		      join("</p>\n<p class=\"LC_error\">",@failed_msg).
                   2765: 		      "</p>\n");
                   2766: 	}
1.10      albertel 2767: 	$r->print(&mt('Deleted [_1] message(s)',$total).'<p>');
1.1       albertel 2768: 	&Apache::loncommunicate::menu($r);
1.53      raeburn  2769: 	&disall($r,($folder?$folder:$dismode),$msgstatus);
1.1       albertel 2770:     } elsif ($env{'form.markunread'}) {
                   2771: 	&printheader($r,'','Marked Message as Unread');
                   2772: 	&statuschange($env{'form.markunread'},'new');
                   2773: 	&Apache::loncommunicate::menu($r);
1.53      raeburn  2774: 	&disall($r,($folder?$folder:$dismode),$msgstatus);
1.1       albertel 2775:     } elsif ($env{'form.compose'}) {
                   2776: 	&compout($r,'','',$env{'form.compose'});
                   2777:     } elsif ($env{'form.recordftf'}) {
                   2778: 	&facetoface($r,$env{'form.recordftf'});
                   2779:     } elsif ($env{'form.block'}) {
                   2780:         &examblock($r,$env{'form.block'});
                   2781:     } elsif ($env{'form.sendmail'}) {
1.53      raeburn  2782:         if ($env{'form.multiforward'}) {
                   2783:             &printheader($r,'','Messages being sent.');
                   2784:             my $fixed_subj = $env{'form.subject'};
                   2785:             my $suffix=&Apache::lonmsg::foldersuffix($folder);
                   2786:             my (%sendresult,%forwardok,%forwardfail,$fwdcount);
                   2787:             my @to_forward = &Apache::loncommon::get_env_multiple('form.delmark');
                   2788:             foreach my $item (@to_forward) {
                   2789:                 my $msgid=&unescape($item);
                   2790:                 my %message=&Apache::lonnet::get('nohist_email'.$suffix,[$msgid]);
                   2791:                 my %content=&Apache::lonmsg::unpackagemsg($message{$msgid},1);
                   2792:                 if ($env{'form.showorigsubj'}) {
                   2793:                     $env{'form.subject'} = $fixed_subj.$content{'subject'};
                   2794:                 } else {
                   2795:                     $env{'form.subject'} = '';
                   2796:                 }
                   2797:                 my $uname = $content{'sendername'};
                   2798:                 my $udom = $content{'senderdomain'};
                   2799:                 &statuschange($msgid,'forwarded',$folder);
                   2800:                 if ($env{'form.showorigsender'}) {
                   2801:                     $env{'form.message'} = $env{'form.msgheader'}.' '.
                   2802:                         &Apache::loncommon::plainname($uname,$udom).' ('.
                   2803:                                            $uname.':'.$udom.')';
                   2804:                 }
                   2805:                 $env{'form.message'} .= "\n\n-- Forwarded message --\n\n".
                   2806:                                         $content{'message'};
                   2807:                 $fwdcount ++;
                   2808:                 $r->print($fwdcount.': '); 
                   2809:                 $sendresult{$msgid} = &sendoffmail($r,$folder);
                   2810:                 $r->print('<br />');
                   2811:             }
                   2812:             foreach my $key (keys(%sendresult)) {
                   2813:                 if ($sendresult{$key} =~/^(\s*(?:ok|con_delayed)\s*)*$/) {
                   2814:                     $forwardok{$key} = $sendresult{$key};
                   2815:                 } else {
                   2816:                     $forwardfail{$key} = $sendresult{$key}; 
                   2817:                 }
                   2818:             }
                   2819:             if (keys(%forwardok) > 0) {
                   2820:                 my $count = keys(%forwardok);
                   2821:                 $r->print('<br /><span class="LC_success">'.
                   2822:                           &mt('[quant,_1,message] forwarded.',$count).
                   2823:                           '</span>');
                   2824:             }
                   2825:             if (keys(%forwardfail) > 0) {
                   2826:                 my $count = keys(%forwardfail);
                   2827:                 $r->print('<p><span class="LC_error">'.
                   2828:                           &mt('Could not forward [quant,_1,message].',$count).
                   2829:                           '</span> ');
                   2830:                 foreach my $key (keys(%forwardfail)) {
                   2831:                     $r->print(&mt('Could not deliver forwarded message.').'</span> '.
                   2832:                               &mt('The recipient addresses may need to be corrected').' ('.$forwardfail{$key}.').<br /><br />');
                   2833:                 }
                   2834:             }
                   2835:             &Apache::loncommunicate::menu($r);
                   2836:         } else {
                   2837: 	    &sendoffmail($r,$folder);
                   2838:         }
1.1       albertel 2839: 	if ($env{'form.storebasecomment'}) {
                   2840: 	    &storecomment($r);
1.38      raeburn  2841:         }
1.1       albertel 2842: 	if (($env{'form.rsspost'}) && ($env{'request.course.id'})) {
1.38      raeburn  2843: 	        &Apache::lonrss::addentry($env{'course.'.$env{'request.course.id'}.'.num'},
1.1       albertel 2844: 				      $env{'course.'.$env{'request.course.id'}.'.domain'},
                   2845: 				      'Course_Announcements',
                   2846: 				      $env{'form.subject'},
                   2847: 				      $env{'form.message'},'/adm/communicate','public');
                   2848: 	}
1.38      raeburn  2849: 	if ((!exists($env{'form.group'})) && (!$env{'form.displayedcrit'})) {
1.53      raeburn  2850: 	    &disall($r,($folder?$folder:$dismode),$msgstatus);
1.36      albertel 2851: 	}
1.1       albertel 2852:     } elsif ($env{'form.newfolder'}) {
                   2853: 	&printheader($r,'','New Folder');
1.46      raeburn  2854:         my $showfolder = $env{'form.newfolder'};
                   2855: 	my ($makeresult,$warning) = &makefolder($env{'form.newfolder'});
                   2856:         if ($makeresult eq 'ok') {
                   2857:             $r->print(&mt('Mail folder "[_1]" created.',$showfolder).'<br />');
                   2858:         } else {
                   2859:             $r->print(&mt('Creation failed.').' '.$makeresult.'<br />'.
                   2860:                       $warning);
                   2861:             $showfolder = $folder;
                   2862:         }
                   2863:         &Apache::loncommunicate::menu($r);
1.53      raeburn  2864: 	&disall($r,$showfolder,$msgstatus);
1.1       albertel 2865:     } elsif ($env{'form.showcommentbaseurl'}) {
                   2866: 	&storedcommentlisting($r);
1.46      raeburn  2867:     } elsif ($env{'form.folderaction'} eq 'delete') {
                   2868:         &printheader($r,'','Deleted Folder');
                   2869:         my $showfolder = '';
                   2870:         my $delresult = &deletefolder($folder);
                   2871:         if ($delresult eq 'ok') {
                   2872:             $r->print(&mt('Mail folder "[_1]" deleted.',$folder).'<br />');
1.65      raeburn  2873:             $env{'form.folder'} = '';
1.46      raeburn  2874:         } else {
                   2875:             $r->print(&mt('Deletion failed.').' '.$delresult.'<br />');
                   2876:             $showfolder = $folder;
                   2877:         }
                   2878:         &Apache::loncommunicate::menu($r);
1.53      raeburn  2879:         &disall($r,$showfolder,$msgstatus);
1.46      raeburn  2880:     } elsif ($env{'form.folderaction'} eq 'rename') {
                   2881:         &printheader($r,'','Renamed Folder');
                   2882:         my $showfolder = $env{'form.renamed'};
                   2883:         my $renresult = &renamefolder($folder);
                   2884:         if ($renresult eq 'ok') {
                   2885:             $r->print(&mt('Mail folder "[_1]" renamed "[_2]".',$folder,$showfolder).'<br />');
                   2886:         } else {
                   2887:             $r->print(&mt('Renaming failed.').' '.$renresult.'<br />');
                   2888:             $showfolder = $folder;
                   2889:         }
                   2890:         &Apache::loncommunicate::menu($r);
1.53      raeburn  2891:         &disall($r,$showfolder,$msgstatus);
1.1       albertel 2892:     } else {
                   2893: 	&printheader($r,'','Display All Messages');
1.44      raeburn  2894: 	&Apache::loncommunicate::menu($r);
1.53      raeburn  2895: 	&disall($r,($folder?$folder:$dismode),$msgstatus);
1.1       albertel 2896:     }
                   2897:     $r->print(&Apache::loncommon::end_page());
                   2898:     return OK;
                   2899: }
                   2900: # ================================================= Main program, reset counter
                   2901: 
                   2902: =pod
                   2903: 
                   2904: =cut
                   2905: 
                   2906: 1; 
                   2907: 
                   2908: __END__
                   2909: 

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