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

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

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