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

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

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