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

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

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