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

1.1       albertel    1: # The LearningOnline Network with CAPA
                      2: # Routines for messaging display
                      3: #
1.26    ! foxr        4: # $Id: lonmsgdisplay.pm,v 1.25 2006/05/17 09:46:01 foxr 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;
1.25      foxr       31: use     Apache::lonselstudent;
1.1       albertel   32: 
                     33: =pod
                     34: 
                     35: =head1 NAME
                     36: 
                     37: Apache::lonmsg: supports internal messaging
                     38: 
                     39: =head1 SYNOPSIS
                     40: 
                     41: lonmsg provides routines for sending messages, receiving messages, and
                     42: a handler to allow users to read, send, and delete messages.
                     43: 
                     44: =head1 OVERVIEW
                     45: 
                     46: =head2 Messaging Overview
                     47: 
                     48: X<messages>LON-CAPA provides an internal messaging system similar to
                     49: email, but customized for LON-CAPA's usage. LON-CAPA implements its
                     50: own messaging system, rather then building on top of email, because of
                     51: the features LON-CAPA messages can offer that conventional e-mail can
                     52: not:
                     53: 
                     54: =over 4
                     55: 
                     56: =item * B<Critical messages>: A message the recipient B<must>
                     57: acknowlegde receipt of before they are allowed to continue using the
                     58: system, preventing a user from claiming they never got a message
                     59: 
                     60: =item * B<Receipts>: LON-CAPA can reliably send reciepts informing the
                     61: sender that it has been read; again, useful for preventing students
                     62: from claiming they did not see a message. (While conventional e-mail
                     63: has some reciept support, it's sporadic, e-mail client-specific, and
                     64: generally the receiver can opt to not send one, making it useless in
                     65: this case.)
                     66: 
                     67: =item * B<Context>: LON-CAPA knows about the sender, such as where
                     68: they are in a course. When a student mails an instructor asking for
                     69: help on the problem, the instructor receives not just the student's
                     70: question, but all submissions the student has made up to that point,
                     71: the user's rendering of the problem, and the complete view the student
                     72: saw of the resource, including discussion up to that point. Finally,
                     73: the instructor is reading all of this inside of LON-CAPA, not their
                     74: email program, so they have full access to LON-CAPA's grading
                     75: interface, or other features they may wish to use in response to the
                     76: student's query.
                     77: 
                     78: =item * B<Blocking>: LON-CAPA can block display of e-mails that are 
                     79: sent to a student during an online exam. A course coordinator or
                     80: instructor can set an open and close date/time for scheduled online
                     81: exams in a course. If a user uses the LON-CAPA internal messaging 
                     82: system to display e-mails during the scheduled blocking event,  
                     83: display of all e-mail sent during the blocking period will be 
                     84: suppressed, and a message of explanation, including details of the 
                     85: currently active blocking periods will be displayed instead. A user 
                     86: who has a course coordinator or instructor role in a course will be
                     87: unaffected by any blocking periods for the course, unless the user
                     88: also has a student role in the course, AND has selected the student role.
                     89: 
                     90: =back
                     91: 
                     92: Users can ask LON-CAPA to forward messages to conventional e-mail
                     93: addresses on their B<PREF> screen, but generally, LON-CAPA messages
                     94: are much more useful than traditional email can be made to be, even
                     95: with HTML support.
                     96: 
                     97: Right now, this document will cover just how to send a message, since
                     98: it is likely you will not need to programmatically read messages,
                     99: since lonmsg already implements that functionality.
                    100: 
                    101: The routines used to package messages and unpackage messages are not
                    102: only used by lonmsg when creating/extracting messages for LON-CAPA's
                    103: internal messaging system, but also by lonnotify.pm which is available
                    104: for use by Domain Coordinators to broadcast standard e-mail to specified
                    105: users in their domain.  The XML packaging used in the two cases is very
                    106: similar.  The differences are the use of <recuser>$uname</recuser> and 
                    107: <recdomain>$udom</recdomain> in stored internal messages, compared 
                    108: with <recipient username="$uname:$udom">$email</recipient> in stored
                    109: Domain Coordinator e-mail for the storage of information about 
                    110: recipients of the message/e-mail.
                    111: 
                    112: =head1 FUNCTIONS
                    113: 
                    114: =over 4
                    115: 
                    116: =cut
                    117: 
                    118: use strict;
                    119: use Apache::lonnet;
                    120: use HTML::TokeParser();
                    121: use Apache::Constants qw(:common);
                    122: use Apache::loncommon();
                    123: use Apache::lontexconvert();
                    124: use HTML::Entities();
                    125: use Apache::lonlocal;
                    126: use Apache::loncommunicate;
                    127: use Apache::lonfeedback;
                    128: use Apache::lonrss();
                    129: 
                    130: # Querystring component with sorting type
                    131: my $sqs;
                    132: my $startdis;
                    133: my $interdis;
                    134: 
                    135: # ============================================================ List all folders
                    136: 
                    137: sub folderlist {
                    138:     my $folder=shift;
                    139:     my @allfolders=&Apache::lonnet::getkeys('email_folders');
                    140:     if ($allfolders[0]=~/^error:/) { @allfolders=(); }
                    141:     return '<form method="post" action="/adm/email">'.
                    142: 	&mt('Folder').': '.
                    143: 	&Apache::loncommon::select_form($folder,'folder',
                    144: 			     ('' => &mt('INBOX'),'trash' => &mt('TRASH'),
                    145: 			      'new' => &mt('New Messages Only'),
                    146:                               'critical' => &mt('Critical'),
                    147: 			      'sent' => &mt('Sent Messages'),
                    148: 			      map { $_ => $_ } @allfolders)).
                    149: 			      ' '.&mt('Show').
                    150: 			      '<select name="interdis">'.
                    151: 			      join("\n",map { '<option value="'.$_.'"'.
                    152: 	 ($_==$interdis?' selected="selected"':'').'>'.$_.'</option>' }
                    153: 				   (10,20,50,100,200)).'</select>'.	
                    154:    '<input type="submit" value="'.&mt('View Folder').'" /><br />'.
                    155:     '<input type="hidden" name="sortedby" value="'.$env{'form.sortedby'}.'" />'.
                    156: 			      ($folder=~/^(new|critical)/?'</form>':'');
                    157: }
                    158: 
                    159: sub scrollbuttons {
                    160:     my ($start,$maxdis,$first,$finish,$total)=@_;
                    161:     unless ($total>0) { return ''; }
                    162:     $start++; $maxdis++;$first++;$finish++;
                    163:     return
                    164:    &mt('Page').': '. 
                    165:    '<input type="submit" name="firstview" value="'.&mt('First').'" />'.
                    166:    '<input type="submit" name="prevview" value="'.&mt('Previous').'" />'.
                    167:    '<input type="text" size="5" name="startdis" value="'.$start.'" onChange="this.form.submit()" /> of '.$maxdis.
                    168:    '<input type="submit" name="nextview" value="'.&mt('Next').'" />'.
                    169:    '<input type="submit" name="lastview" value="'.&mt('Last').'" /><br />'.
                    170:    &mt('Showing messages [_1] through [_2] of [_3]',$first,$finish,$total).'</form>';
                    171: }
                    172: # =============================================================== Status Change
                    173: 
                    174: sub statuschange {
                    175:     my ($msgid,$newstatus,$folder)=@_;
1.3       albertel  176:     my $suffix=&Apache::lonmsg::foldersuffix($folder);
1.1       albertel  177:     my %status=&Apache::lonnet::get('email_status'.$suffix,[$msgid]);
                    178:     if ($status{$msgid}=~/^error\:/) { $status{$msgid}=''; }
                    179:     unless ($status{$msgid}) { $status{$msgid}='new'; }
                    180:     unless (($status{$msgid} eq 'replied') || 
                    181:             ($status{$msgid} eq 'forwarded')) {
                    182: 	&Apache::lonnet::put('email_status'.$suffix,{$msgid => $newstatus});
                    183:     }
                    184:     if (($newstatus eq 'deleted') || ($newstatus eq 'new')) {
                    185: 	&Apache::lonnet::put('email_status'.$suffix,{$msgid => $newstatus});
                    186:     }
                    187:     if ($newstatus eq 'deleted') {
1.9       albertel  188: 	return &movemsg($msgid,$folder,'trash');
                    189:     }
                    190:     return ;
1.1       albertel  191: }
                    192: 
                    193: # ============================================================= Make new folder
                    194: 
                    195: sub makefolder {
                    196:     my ($newfolder)=@_;
                    197:     if (($newfolder eq 'sent')
                    198:      || ($newfolder eq 'critical')
                    199:      || ($newfolder eq 'trash')
                    200:      || ($newfolder eq 'new')) { return; }
                    201:     &Apache::lonnet::put('email_folders',{$newfolder => time});
                    202: }
                    203: 
                    204: # ======================================================== Move between folders
                    205: 
                    206: sub movemsg {
                    207:     my ($msgid,$srcfolder,$trgfolder)=@_;
                    208:     if ($srcfolder eq 'new') { $srcfolder=''; }
1.3       albertel  209:     my $srcsuffix=&Apache::lonmsg::foldersuffix($srcfolder);
                    210:     my $trgsuffix=&Apache::lonmsg::foldersuffix($trgfolder);
1.9       albertel  211:     if ($srcsuffix eq $trgsuffix) {
                    212: 	return (0,&mt('Message not moved, Attempted to move message to the same folder as it already is in.'));
                    213:     }
1.1       albertel  214: 
                    215: # Copy message
                    216:     my %message=&Apache::lonnet::get('nohist_email'.$srcsuffix,[$msgid]);
1.9       albertel  217:     if (!exists($message{$msgid}) || $message{$msgid} eq '') {
                    218: 	if (&Apache::slotrequest::network_error(%message)) {
                    219: 	    return (0,&mt('Message not moved, A network error occurred.'));
                    220: 	} else {
                    221: 	    return (0,&mt('Message not moved as the message is no longer in the source folder.'));
                    222: 	}
                    223:     }
                    224: 
                    225:     my $result =&Apache::lonnet::put('nohist_email'.$trgsuffix,
                    226: 				     {$msgid => $message{$msgid}});
                    227:     if (&Apache::slotrequest::network_error($result)) {
                    228: 	return (0,&mt('Message not moved, A network error occurred.'));
                    229:     }
1.1       albertel  230: 
                    231: # Copy status
                    232:     unless ($trgfolder eq 'trash') {
1.9       albertel  233:        	my %status=&Apache::lonnet::get('email_status'.$srcsuffix,[$msgid]);
                    234: 	# a non-existant status is the mark of an unread msg
                    235: 	if (&Apache::slotrequest::network_error(%status)) {
                    236: 	    return (0,&mt('Message copied to new folder but status was not, A network error occurred.'));
                    237: 	}
                    238: 	my $result=&Apache::lonnet::put('email_status'.$trgsuffix,
                    239: 					{$msgid => $status{$msgid}});
                    240: 	if (&Apache::slotrequest::network_error($result)) {
                    241: 	    return (0,&mt('Message copied to new folder but status was not, A network error occurred.'));
                    242: 	}
1.1       albertel  243:     }
1.9       albertel  244: 
1.1       albertel  245: # Delete orginals
1.9       albertel  246:     my $result_del_msg = 
                    247: 	&Apache::lonnet::del('nohist_email'.$srcsuffix,[$msgid]);
                    248:     my $result_del_stat =
                    249: 	&Apache::lonnet::del('email_status'.$srcsuffix,[$msgid]);
                    250:     if (&Apache::slotrequest::network_error($result_del_msg)) {
                    251: 	return (0,&mt('Message copied, but unable to delete the original from the source folder.'));
                    252:     }
                    253:     if (&Apache::slotrequest::network_error($result_del_stat)) {
                    254: 	return (0,&mt('Message copied, but unable to delete the original status from the source folder.'));
                    255:     }
                    256: 
                    257:     return (1);
1.1       albertel  258: }
                    259: 
                    260: # ======================================================= Display a course list
                    261: 
                    262: sub discourse {
1.25      foxr      263:     my $result;
                    264:     my ($course_personnel,
                    265: 	$current_members,
                    266: 	$expired_members,
                    267: 	$future_members) = &Apache::lonselstudent::get_people_in_class();
                    268:     unshift @$current_members, (@$course_personnel);
                    269:     my %defaultUsers;
                    270: 
                    271:     $result = &Apache::lonselstudent::render_student_list($current_members,
                    272: 							  "compemail",
                    273: 							  "current",
                    274: 							  \%defaultUsers,
                    275: 							  1,"selectedusers",1);
                    276: 
                    277:     return $result;
                    278: }
                    279: 
1.1       albertel  280: # ==================================================== Display Critical Message
                    281: 
                    282: sub discrit {
                    283:     my $r=shift;
1.5       albertel  284:     my $header = '<h1><font color="red">'.&mt('Critical Messages').'</font></h1>'.
1.1       albertel  285:         '<form action="/adm/email" method="POST">'.
                    286:         '<input type="hidden" name="confirm" value="true" />';
                    287:     my %what=&Apache::lonnet::dump('critical');
                    288:     my $result = '';
                    289:     foreach (sort keys %what) {
                    290:         my %content=&Apache::lonmsg::unpackagemsg($what{$_});
                    291:         next if ($content{'senderdomain'} eq '');
                    292:         $result.='<hr />'.&mt('From').': <b>'.
                    293: &Apache::loncommon::aboutmewrapper(
                    294:  &Apache::loncommon::plainname($content{'sendername'},$content{'senderdomain'}),$content{'sendername'},$content{'senderdomain'}).'</b> ('.
1.14      albertel  295: $content{'sendername'}.':'.
1.1       albertel  296:             $content{'senderdomain'}.') '.$content{'time'}.
                    297:             '<br />'.&mt('Subject').': '.$content{'subject'}.
                    298:             '<br /><pre>'.
                    299:               &Apache::lontexconvert::msgtexconverted($content{'message'}).
                    300:             '</pre><small>'.
                    301: &mt('You have to confirm that you received this message. After confirmation, this message will be moved to your regular inbox').
                    302:             '</small><br />'.
                    303:             '<input type="submit" name="rec_'.$_.'" value="'.&mt('Confirm Receipt').'" />'.
                    304:             '<input type="submit" name="reprec_'.$_.'" '.
                    305:                   'value="'.&mt('Confirm Receipt and Reply').'" />';
                    306:     }
                    307:     # Check to see if there were any messages.
                    308:     if ($result eq '') {
                    309:         $result = "<h2>".&mt('You have no critical messages.')."</h2>".
                    310: 	    '<a href="/adm/roles">'.&mt('Select a course').'</a><br />'.
                    311:             '<a href="/adm/email">'.&mt('Communicate').'</a>';
                    312:     } else {
                    313:         $r->print($header);
                    314:     }
                    315:     $r->print($result);
                    316:     $r->print('<input type="hidden" name="displayedcrit" value="true" /></form>');
                    317: }
                    318: 
                    319: sub sortedmessages {
                    320:     my ($blocked,$startblock,$endblock,$numblocked,$folder) = @_;
                    321:     my $suffix=&Apache::lonmsg::foldersuffix($folder);
                    322:     my @messages = &Apache::lonnet::getkeys('nohist_email'.$suffix);
                    323:     #unpack the varibles and repack into temp for sorting
                    324:     my @temp;
                    325:     my %descriptions;
                    326:     my %status_cache = 
                    327: 	&Apache::lonnet::get('email_status'.&Apache::lonmsg::foldersuffix($folder),\@messages);
1.11      albertel  328: 
                    329:     my $get_received;
                    330:     if ($folder eq 'sent' 
                    331: 	&& ($env{'form.sortedby'} =~ m/^(rev)?(user|domain)$/)) {
                    332: 	$get_received = 1;
                    333:     }
                    334: 
                    335:     foreach my $msgid (@messages) {
                    336: 	my $esc_msgid=&Apache::lonnet::escape($msgid);
1.1       albertel  337: 	my ($sendtime,$shortsubj,$fromname,$fromdomain,$status,$fromcid)=
1.11      albertel  338: 	    &Apache::lonmsg::unpackmsgid($esc_msgid,$folder,undef,
1.1       albertel  339: 					 \%status_cache);
                    340:         my $description = &get_course_desc($fromcid,\%descriptions);
                    341: 	my @temp1 = ($sendtime,$shortsubj,$fromname,$fromdomain,$status,
1.11      albertel  342: 		     $esc_msgid,$description);
                    343: 	if ($get_received) {
                    344: 	    my %message = &Apache::lonnet::get('nohist_email'.$suffix,
                    345: 					       [$msgid]);
                    346: 	    my %content = &Apache::lonmsg::unpackagemsg($message{$msgid});
                    347: 	    push(@temp1,$content{'recuser'},$content{'recdomain'});
                    348: 	}
1.1       albertel  349:         # Check whether message was sent during blocking period.
                    350:         if ($sendtime >= $startblock && ($sendtime <= $endblock && $endblock > 0) ) {
1.11      albertel  351:             $$blocked{$msgid} = 'ON';
1.1       albertel  352:             $$numblocked ++;
                    353:         } else { 
                    354:             push @temp ,\@temp1;
                    355:         }
                    356:     }
                    357:     #default sort
                    358:     @temp = sort  {$a->[0] <=> $b->[0]} @temp;    
                    359:     if ($env{'form.sortedby'} eq "date"){
                    360:         @temp = sort  {$a->[0] <=> $b->[0]} @temp;    
                    361:     }
                    362:     if ($env{'form.sortedby'} eq "revdate"){
                    363:     	@temp = sort  {$b->[0] <=> $a->[0]} @temp; 
                    364:     }
                    365:     if ($env{'form.sortedby'} eq "user"){
1.11      albertel  366: 	if ($get_received) {
                    367: 	    @temp = sort  {lc($a->[7][0]) cmp lc($b->[7][0])} @temp;
                    368: 	} else {
                    369: 	    @temp = sort  {lc($a->[2])    cmp lc($b->[2])}    @temp;
                    370: 	}
1.1       albertel  371:     }
                    372:     if ($env{'form.sortedby'} eq "revuser"){
1.11      albertel  373: 	if ($get_received) {
                    374: 	    @temp = sort  {lc($b->[7][0]) cmp lc($a->[7][0])} @temp;
                    375: 	} else {
                    376: 	    @temp = sort  {lc($b->[2])    cmp lc($a->[2])}    @temp;
                    377: 	}
1.1       albertel  378:     }
                    379:     if ($env{'form.sortedby'} eq "domain"){
1.11      albertel  380: 	if ($get_received) {
                    381: 	    @temp = sort  {$a->[8][0] cmp $b->[8][0]} @temp;
                    382: 	} else {
                    383: 	    @temp = sort  {$a->[3]    cmp $b->[3]}    @temp;
                    384: 	}
1.1       albertel  385:     }
                    386:     if ($env{'form.sortedby'} eq "revdomain"){
1.11      albertel  387: 	if ($get_received) {
                    388: 	    @temp = sort  {$b->[8][0] cmp $a->[8][0]} @temp;
                    389: 	} else {
                    390: 	    @temp = sort  {$b->[3]    cmp $a->[3]}    @temp;
                    391: 	}
1.1       albertel  392:     }
                    393:     if ($env{'form.sortedby'} eq "subject"){
                    394:         @temp = sort  {lc($a->[1]) cmp lc($b->[1])} @temp;
                    395:     }
                    396:     if ($env{'form.sortedby'} eq "revsubject"){
                    397:         @temp = sort  {lc($b->[1]) cmp lc($a->[1])} @temp;
                    398:     }
                    399:     if ($env{'form.sortedby'} eq "course"){
                    400:         @temp = sort  {lc($a->[6]) cmp lc($b->[6])} @temp;
                    401:     }
                    402:     if ($env{'form.sortedby'} eq "revcourse"){
                    403:         @temp = sort  {lc($b->[6]) cmp lc($a->[6])} @temp;
                    404:     }
                    405:     if ($env{'form.sortedby'} eq "status"){
                    406:         @temp = sort  {$a->[4] cmp $b->[4]} @temp;
                    407:     }
                    408:     if ($env{'form.sortedby'} eq "revstatus"){
                    409:         @temp = sort  {$b->[4] cmp $a->[4]} @temp;
                    410:     }
                    411:     return @temp;
                    412: }
                    413: 
                    414: sub get_course_desc {
                    415:     my ($fromcid,$descriptions) = @_;
                    416:     my $description;
                    417:     if (!$fromcid) {
                    418:         return $description;
                    419:     } else {
                    420:         if (defined($$descriptions{$fromcid})) {
                    421:             $description = $$descriptions{$fromcid};
                    422:         } else {
                    423:             if (defined($env{'course.'.$fromcid.'.description'})) {
                    424:                 $description = $env{'course.'.$fromcid.'.description'};
                    425:             } else {
                    426:                 my %courseinfo=&Apache::lonnet::coursedescription($fromcid);                $description = $courseinfo{'description'};
                    427:                 $description = $courseinfo{'description'};
                    428:             }
                    429:             $$descriptions{$fromcid} = $description;
                    430:         }
                    431:         return $description;
                    432:     }
                    433: }
                    434: 
                    435: # ======================================================== Display new messages
                    436: 
                    437: 
                    438: sub disnew {
                    439:     my $r=shift;
                    440:     my %lt=&Apache::lonlocal::texthash(
                    441: 				       'nm' => 'New Messages',
                    442: 				       'su' => 'Subject',
                    443:                                        'co' => 'Course',
                    444: 				       'da' => 'Date',
                    445: 				       'us' => 'Username',
                    446: 				       'op' => 'Open',
                    447: 				       'do' => 'Domain'
                    448: 				       );
                    449:     my @msgids = sort(&Apache::lonnet::getkeys('nohist_email'));
                    450:     my @newmsgs;
                    451:     my %setters = ();
                    452:     my $startblock = 0;
                    453:     my $endblock = 0;
                    454:     my %blocked = ();
                    455:     my $numblocked = 0;
                    456:     # Check for blocking of display because of scheduled online exams.
                    457:     &blockcheck(\%setters,\$startblock,\$endblock);
                    458:     my %status_cache = 
                    459: 	&Apache::lonnet::get('email_status',\@msgids);
                    460:     my %descriptions;
                    461:     foreach (@msgids) {
                    462: 	my $msgid=&Apache::lonnet::escape($_);
                    463:         my ($sendtime,$shortsubj,$fromname,$fromdom,$status,$fromcid)=
                    464: 	    &Apache::lonmsg::unpackmsgid($msgid,undef,undef,\%status_cache);
                    465:         if (defined($sendtime) && $sendtime!~/error/) {
                    466:             my $description = &get_course_desc($fromcid,\%descriptions);
                    467:             my $numsendtime = $sendtime;
                    468:             $sendtime = &Apache::lonlocal::locallocaltime($sendtime);
                    469:             if ($status eq 'new') {
                    470:                 if ($numsendtime >= $startblock && ($numsendtime <= $endblock && $endblock > 0) ) {
                    471:                     $blocked{$_} = 'ON';
                    472:                     $numblocked ++;
                    473:                 } else {
                    474:                     push @newmsgs, { 
                    475:                         msgid    => $msgid,
                    476:                         sendtime => $sendtime,
1.8       albertel  477:                         shortsub => $shortsubj,
1.1       albertel  478:                         from     => $fromname,
                    479:                         fromdom  => $fromdom,
                    480:                         course   => $description 
                    481:                         }
                    482:                 }
                    483:             }
                    484:         }
                    485:     }
                    486:     if ($#newmsgs >= 0) {
                    487:         $r->print(<<TABLEHEAD);
                    488: <h2>$lt{'nm'}</h2>
1.4       albertel  489: <table class="LC_mail_list"><tr><th>&nbsp</th>
1.1       albertel  490: <th>$lt{'da'}</th><th>$lt{'us'}</th><th>$lt{'do'}</th><th>$lt{'su'}</th><th>$lt{'co'}</th></tr>
                    491: TABLEHEAD
                    492:         foreach my $msg (@newmsgs) {
                    493:             $r->print(<<"ENDLINK");
1.4       albertel  494: <tr class="LC_mail_new">
1.1       albertel  495: <td><a href="/adm/email?dismode=new&display=$msg->{'msgid'}">$lt{'op'}</a></td>
                    496: ENDLINK
                    497:             foreach ('sendtime','from','fromdom','shortsub','course') {
                    498:                 $r->print("<td>$msg->{$_}</td>");
                    499:             }
                    500:             $r->print("</td></tr>");
                    501:         }
                    502:         $r->print('</table>');
                    503:     } elsif ($numblocked == 0) {
                    504:         $r->print("<h3>".&mt('You have no unread messages')."</h3>");
                    505:     }
                    506:     if ($numblocked > 0) {
                    507:         my $beginblock = &Apache::lonlocal::locallocaltime($startblock);
                    508:         my $finishblock = &Apache::lonlocal::locallocaltime($endblock);
                    509:         if ($numblocked == 1) {
                    510:             $r->print("<h3>".&mt('You have').' '.$numblocked.' '.&mt('blocked unread message').".</h3>");
                    511:             $r->print(&mt('This message is not viewable because').' ');
                    512:         } else {
                    513:             $r->print("<h3>".&mt('You have').' '.$numblocked.' '.&mt('blocked unread messages').".</h3>");
                    514:             $r->print(&mt('These').' '.$numblocked.' '.&mt('messages are not viewable because '));
                    515:         }
                    516:         $r->print(
                    517: &mt('display of LON-CAPA messages sent to you by other students between').' '.$beginblock.' '.&mt('and').' '.$finishblock.' '.&mt('is currently being blocked because of online exams').'.');
                    518:         &build_block_table($r,$startblock,$endblock,\%setters);
                    519:     }
                    520: }
                    521: 
                    522: 
                    523: # ======================================================== Display all messages
                    524: 
                    525: sub disall {
                    526:     my ($r,$folder)=@_;
                    527:     $r->print(&folderlist($folder));
                    528:     if ($folder eq 'new') {
                    529: 	&disnew($r);
                    530:     } elsif ($folder eq 'critical') {
                    531: 	&discrit($r);
                    532:     } else {
                    533: 	&disfolder($r,$folder);
                    534:     }
                    535: }
                    536: 
                    537: # ============================================================ Display a folder
                    538: 
                    539: sub disfolder {
                    540:     my ($r,$folder)=@_;
                    541:     my %blocked = ();
                    542:     my %setters = ();
                    543:     my $startblock;
                    544:     my $endblock;
                    545:     my $numblocked = 0;
                    546:     &blockcheck(\%setters,\$startblock,\$endblock);
                    547:     $r->print(<<ENDDISHEADER);
1.16      albertel  548: <script type="text/javascript">
1.1       albertel  549:     function checkall() {
                    550: 	for (i=0; i<document.forms.disall.elements.length; i++) {
                    551:             if 
                    552:           (document.forms.disall.elements[i].name.indexOf('delmark_')==0) {
                    553: 	      document.forms.disall.elements[i].checked=true;
                    554:             }
                    555:         }
                    556:     }
                    557: 
                    558:     function uncheckall() {
                    559: 	for (i=0; i<document.forms.disall.elements.length; i++) {
                    560:             if 
                    561:           (document.forms.disall.elements[i].name.indexOf('delmark_')==0) {
                    562: 	      document.forms.disall.elements[i].checked=false;
                    563:             }
                    564:         }
                    565:     }
                    566: </script>
                    567: ENDDISHEADER
                    568:     my $fsqs='&folder='.$folder;
                    569:     my @temp=sortedmessages(\%blocked,$startblock,$endblock,\$numblocked,$folder);
                    570:     my $totalnumber=$#temp+1;
                    571:     unless ($totalnumber>0) {
                    572: 	$r->print('<h2>'.&mt('Empty Folder').'</h2>');
                    573: 	return;
                    574:     }
                    575:     unless ($interdis) {
                    576: 	$interdis=20;
                    577:     }
                    578:     my $number=int($totalnumber/$interdis);
                    579:     if (($startdis<0) || ($startdis>$number)) { $startdis=$number; }
                    580:     my $firstdis=$interdis*$startdis;
                    581:     if ($firstdis>$#temp) { $firstdis=$#temp-$interdis+1; }
                    582:     my $lastdis=$firstdis+$interdis-1;
                    583:     if ($lastdis>$#temp) { $lastdis=$#temp; }
                    584:     $r->print(&scrollbuttons($startdis,$number,$firstdis,$lastdis,$totalnumber));
                    585:     $r->print('<form method="post" name="disall" action="/adm/email">'.
1.4       albertel  586: 	      '<table class="LC_mail_list"><tr><th colspan="3">&nbsp</th><th>');
1.1       albertel  587:     if ($env{'form.sortedby'} eq "revdate") {
                    588: 	$r->print('<a href = "?sortedby=date'.$fsqs.'">'.&mt('Date').'</a></th>');
                    589:     } else {
                    590: 	$r->print('<a href = "?sortedby=revdate'.$fsqs.'">'.&mt('Date').'</a></th>');
                    591:     }
                    592:     $r->print('<th>');
                    593:     if ($env{'form.sortedby'} eq "revuser") {
                    594: 	$r->print('<a href = "?sortedby=user'.$fsqs.'">'.&mt('Username').'</a>');
                    595:     } else {
                    596: 	$r->print('<a href = "?sortedby=revuser'.$fsqs.'">'.&mt('Username').'</a>');
                    597:     }
                    598:     $r->print('</th><th>');
                    599:     if ($env{'form.sortedby'} eq "revdomain") {
                    600: 	$r->print('<a href = "?sortedby=domain'.$fsqs.'">'.&mt('Domain').'</a>');
                    601:     } else {
                    602: 	$r->print('<a href = "?sortedby=revdomain'.$fsqs.'">'.&mt('Domain').'</a>');
                    603:     }
                    604:     $r->print('</th><th>');
                    605:     if ($env{'form.sortedby'} eq "revsubject") {
                    606: 	$r->print('<a href = "?sortedby=subject'.$fsqs.'">'.&mt('Subject').'</a>');
                    607:     } else {
                    608:     	$r->print('<a href = "?sortedby=revsubject'.$fsqs.'">'.&mt('Subject').'</a>');
                    609:     }
                    610:     $r->print('</th><th>');
                    611:     if ($env{'form.sortedby'} eq "revcourse") {
                    612:         $r->print('<a href = "?sortedby=course'.$fsqs.'">'.&mt('Course').'</a>');
                    613:     } else {
                    614:         $r->print('<a href = "?sortedby=revcourse'.$fsqs.'">'.&mt('Course').'</a>');
                    615:     }
                    616:     $r->print('</th><th>');
                    617:     if ($env{'form.sortedby'} eq "revstatus") {
                    618: 	$r->print('<a href = "?sortedby=status'.$fsqs.'">'.&mt('Status').'</a></th>');
                    619:     } else {
                    620:      	$r->print('<a href = "?sortedby=revstatus'.$fsqs.'">'.&mt('Status').'</a></th>');
                    621:     }
                    622:     $r->print("</tr>\n");
1.6       albertel  623: 
                    624:     my $suffix = &Apache::lonmsg::foldersuffix($folder);
1.1       albertel  625:     for (my $n=$firstdis;$n<=$lastdis;$n++) {
1.11      albertel  626: 	my ($sendtime,$shortsubj,$fromname,$fromdomain,$status,$origID,
                    627: 	    $description,$recv_name,$recv_domain)= 
                    628: 		@{$temp[$n]};
1.1       albertel  629: 	if (($status ne 'deleted') && defined($sendtime) && $sendtime!~/error/) {
                    630: 	    if ($status eq 'new') {
1.4       albertel  631: 		$r->print('<tr class="LC_mail_new">');
1.1       albertel  632: 	    } elsif ($status eq 'read') {
1.4       albertel  633: 		$r->print('<tr class="LC_mail_read">');
1.1       albertel  634: 	    } elsif ($status eq 'replied') {
1.4       albertel  635: 		$r->print('<tr class="LC_mail_replied">'); 
1.1       albertel  636: 	    } else {
1.4       albertel  637: 		$r->print('<tr class="LC_mail_other">');
1.1       albertel  638: 	    }
1.6       albertel  639: 	    my ($dis_name,$dis_domain) = ($fromname,$fromdomain);
                    640: 	    if ($folder eq 'sent') {
1.11      albertel  641: 		if (defined($recv_name) && !defined($recv_domain)) {
                    642: 		    $dis_name   = join('<br />',@{$recv_name});
                    643: 		    $dis_domain = join('<br />',@{$recv_domain});
                    644: 		} else {
                    645: 		    my $msg_id  = &Apache::lonnet::unescape($origID);
                    646: 		    my %message = &Apache::lonnet::get('nohist_email'.$suffix,
                    647: 						       [$msg_id]);
                    648: 		    my %content = &Apache::lonmsg::unpackagemsg($message{$msg_id});
                    649: 		    $dis_name   = join('<br />',@{$content{'recuser'}});
                    650: 		    $dis_domain = join('<br />',@{$content{'recdomain'}});
                    651: 		}
1.6       albertel  652: 	    }
1.1       albertel  653: 	    $r->print('<td><input type="checkbox" name="delmark_'.$origID.'" /></td><td><a href="/adm/email?display='.$origID.$sqs. 
                    654: 		      '">'.&mt('Open').'</a></td><td>'.
                    655: 		      ($folder ne 'trash'?'<a href="/adm/email?markdel='.$origID.$sqs.
                    656: 		      '">'.&mt('Delete'):'&nbsp').'</a></td>'.
                    657: 		      '<td>'.&Apache::lonlocal::locallocaltime($sendtime).'</td><td>'.
1.6       albertel  658: 		      $dis_name.'</td><td>'.$dis_domain.'</td><td>'.
1.8       albertel  659: 		      $shortsubj.'</td><td>'.
1.1       albertel  660:                       $description.'</td><td>'.$status.'</td></tr>'."\n");
                    661: 	} elsif ($status eq 'deleted') {
                    662: # purge
1.9       albertel  663: 	    my ($result,$msg) = 
                    664: 		&movemsg(&Apache::lonnet::unescape($origID),$folder,'trash');
                    665: 	    
1.1       albertel  666: 	}
                    667:     }   
                    668:     $r->print("</table>\n<p>".
                    669:   '<a href="javascript:checkall()">'.&mt('Check All').'</a>&nbsp;'.
                    670:   '<a href="javascript:uncheckall()">'.&mt('Uncheck All').'</a></p>'.
                    671:   '<input type="hidden" name="sortedby" value="'.$env{'form.sortedby'}.'" />');
                    672:     if ($folder ne 'trash') {
                    673: 	$r->print(
                    674: 	      '<p><input type="submit" name="markeddel" value="'.&mt('Delete Checked').'" /></p>');
                    675:     }
                    676:     $r->print('<p><input type="submit" name="markedmove" value="'.&mt('Move Checked to Folder').'" />');
                    677:     my @allfolders=&Apache::lonnet::getkeys('email_folders');
                    678:     if ($allfolders[0]=~/^error:/) { @allfolders=(); }
                    679:     $r->print(
                    680: 	&Apache::loncommon::select_form('','movetofolder',
                    681: 			     ( map { $_ => $_ } @allfolders))
                    682: 	      );
                    683:     my $postedstartdis=$startdis+1;
                    684:     $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>');
                    685:     if ($numblocked > 0) {
                    686:         my $beginblock = &Apache::lonlocal::locallocaltime($startblock);
                    687:         my $finishblock = &Apache::lonlocal::locallocaltime($endblock);
                    688:         $r->print('<br /><br />'.
                    689:                   $numblocked.' '.&mt('message(s) is/are not viewable because display of LON-CAPA messages sent to you by other students between').' '.$beginblock.' '.&mt('and').' '.$finishblock.' '.&mt('is currently being blocked because of online exams.'));
                    690:         &build_block_table($r,$startblock,$endblock,\%setters);
                    691:     }
                    692: }
                    693: 
                    694: # ============================================================== Compose output
                    695: 
                    696: sub compout {
                    697:     my ($r,$forwarding,$replying,$broadcast,$replycrit,$folder,$dismode)=@_;
                    698:     my $suffix=&Apache::lonmsg::foldersuffix($folder);
                    699: 
                    700:     if ($broadcast eq 'individual') {
                    701: 	&printheader($r,'/adm/email?compose=individual',
                    702: 	     'Send a Message');
                    703:     } elsif ($broadcast) {
                    704: 	&printheader($r,'/adm/email?compose=group',
                    705: 	     'Broadcast Message');
                    706:     } elsif ($forwarding) {
                    707: 	&Apache::lonhtmlcommon::add_breadcrumb
                    708:         ({href=>"/adm/email?display=".&Apache::lonnet::escape($forwarding),
                    709:           text=>"Display Message"});
                    710: 	&printheader($r,'/adm/email?forward='.&Apache::lonnet::escape($forwarding),
                    711: 	     'Forwarding a Message');
                    712:     } elsif ($replying) {
                    713: 	&Apache::lonhtmlcommon::add_breadcrumb
                    714:         ({href=>"/adm/email?display=".&Apache::lonnet::escape($replying),
                    715:           text=>"Display Message"});
                    716: 	&printheader($r,'/adm/email?replyto='.&Apache::lonnet::escape($replying),
                    717: 	     'Replying to a Message');
                    718:     } elsif ($replycrit) {
                    719: 	$r->print('<h3>'.&mt('Replying to a Critical Message').'</h3>');
                    720: 	$replying=$replycrit;
                    721:     } else {
                    722: 	&printheader($r,'/adm/email?compose=upload',
                    723: 	     'Distribute from Uploaded File');
                    724:     }
                    725: 
                    726:     my $dispcrit='';
                    727:     my $dissub='';
                    728:     my $dismsg='';
                    729:     my $disbase='';
                    730:     my $func=&mt('Send New');
                    731:     my %lt=&Apache::lonlocal::texthash('us' => 'Username',
                    732: 				       'do' => 'Domain',
                    733: 				       'ad' => 'Additional Recipients',
                    734: 				       'sb' => 'Subject',
                    735: 				       'ca' => 'Cancel',
                    736: 				       'ma' => 'Mail');
                    737: 
                    738:     if (&Apache::lonnet::allowed('srm',$env{'request.course.id'})
                    739: 	|| &Apache::lonnet::allowed('srm',$env{'request.course.id'}.
                    740: 				    '/'.$env{'request.course.sec'})) {
                    741: 	 my $crithelp = Apache::loncommon::help_open_topic("Course_Critical_Message");
                    742:          $dispcrit=
                    743:  '<p><label><input type="checkbox" name="critmsg" /> '.&mt('Send as critical message').'</label> ' . $crithelp . 
                    744:  '</p><p>'.
                    745:  '<label><input type="checkbox" name="sendbck" /> '.&mt('Send as critical message').'  ' .
                    746:  &mt('and return receipt') . '</label>' . $crithelp . 
                    747:  '</p><p><label><input type="checkbox" name="permanent" /> '.
                    748: &mt('Send copy to permanent email address (if known)').'</label></p>'.
                    749: '<p><label><input type="checkbox" name="rsspost" /> '.
                    750: 		  &mt('Include in course RSS newsfeed').'</label></p>';
                    751:      }
                    752:     my %message;
                    753:     my %content;
                    754:     my $defdom=$env{'user.domain'};
                    755:     if ($forwarding) {
                    756: 	%message=&Apache::lonnet::get('nohist_email'.$suffix,[$forwarding]);
                    757: 	%content=&Apache::lonmsg::unpackagemsg($message{$forwarding},$folder);
                    758: 	$dispcrit.='<input type="hidden" name="forwid" value="'.
                    759: 	    $forwarding.'" />';
                    760: 	$func=&mt('Forward');
                    761: 	
                    762: 	$dissub=&mt('Forwarding').': '.$content{'subject'};
                    763: 	$dismsg=&mt('Forwarded message from').' '.
                    764: 	    $content{'sendername'}.' '.&mt('at').' '.$content{'senderdomain'};
                    765: 	if ($content{'baseurl'}) {
                    766: 	    $disbase='<input type="hidden" name="baseurl" value="'.&Apache::lonnet::escape($content{'baseurl'}).'" />';
                    767: 	}
                    768:     }
                    769:     if ($replying) {
                    770: 	%message=&Apache::lonnet::get('nohist_email'.$suffix,[$replying]);
                    771: 	%content=&Apache::lonmsg::unpackagemsg($message{$replying},$folder);
                    772: 	$dispcrit.='<input type="hidden" name="replyid" value="'.
                    773: 	    $replying.'" />';
                    774: 	$func=&mt('Send Reply to');
                    775: 	
                    776: 	$dissub=&mt('Reply').': '.$content{'subject'};       
                    777: 	$dismsg='> '.$content{'message'};
                    778: 	$dismsg=~s/\r/\n/g;
                    779: 	$dismsg=~s/\f/\n/g;
                    780: 	$dismsg=~s/\n+/\n\> /g;
                    781: 	if ($content{'baseurl'}) {
                    782: 	    $disbase='<input type="hidden" name="baseurl" value="'.&Apache::lonnet::escape($content{'baseurl'}).'" />';
                    783: 	    if ($env{'user.adv'}) {
                    784: 		$disbase.='<label><input type="checkbox" name="storebasecomment" />'.&mt('Store message for re-use').
                    785: 		    '</label> <a href="/adm/email?showcommentbaseurl='.
                    786: 		    &Apache::lonnet::escape($content{'baseurl'}).'" target="comments">'.
                    787: 		    &mt('Show re-usable messages').'</a><br />';
                    788: 	    }
                    789: 	}
                    790:     }
                    791:     my $citation=&displayresource(%content);
                    792:     if ($env{'form.recdom'}) { $defdom=$env{'form.recdom'}; }
1.23      www       793:     if ($env{'form.text'}) { $dismsg=$env{'form.text'}; }
                    794:     if ($env{'form.subject'}) { $dissub=$env{'form.subject'}; }
                    795:     $r->print(
1.1       albertel  796:                 '<form action="/adm/email"  name="compemail" method="post"'.
                    797:                 ' enctype="multipart/form-data">'."\n".
                    798:                 '<input type="hidden" name="sendmail" value="on" />'."\n".
                    799:                 '<table>');
                    800:     unless (($broadcast eq 'group') || ($broadcast eq 'upload')) {
                    801: 	if ($replying) {
                    802: 	    $r->print('<tr><td colspan="2">'.&mt('Replying to').' '.
                    803: 		      &Apache::loncommon::aboutmewrapper(
                    804: 							 &Apache::loncommon::plainname($content{'sendername'},$content{'senderdomain'}),$content{'sendername'},$content{'senderdomain'}).' ('.
1.14      albertel  805: 		      $content{'sendername'}.':'.
1.1       albertel  806: 		      $content{'senderdomain'}.')'.
                    807: 		      '<input type="hidden" name="recuname" value="'.$content{'sendername'}.'" />'.
                    808: 		      '<input type="hidden" name="recdomain" value="'.$content{'senderdomain'}.'" />'.
                    809: 		      '</td></tr>');
                    810: 	} else {
                    811: 	    my $domform = &Apache::loncommon::select_dom_form($defdom,'recdomain');
                    812: 	    my $selectlink=&Apache::loncommon::selectstudent_link
                    813: 	    ('compemail','recuname','recdomain');
                    814: 	    $r->print(<<"ENDREC");
                    815: <tr><td>$lt{'us'}:</td><td><input type="text" size="12" name="recuname" value="$env{'form.recname'}" /></td><td rowspan="2">$selectlink</td></tr>
                    816: <tr><td>$lt{'do'}:</td>
                    817: <td>$domform</td></tr>
                    818: ENDREC
                    819:         }
                    820:     }
                    821:     my $latexHelp = Apache::loncommon::helpLatexCheatsheet();
                    822:     if ($broadcast ne 'upload') {
                    823:        $r->print(<<"ENDCOMP");
                    824: <tr><td>$lt{'ad'}<br /><tt>username\@domain,username\@domain, ...
                    825: </tt></td><td>
                    826: <input type="text" size="50" name="additionalrec" /></td></tr>
                    827: <tr><td>$lt{'sb'}:</td><td><input type="text" size="50" name="subject" value="$dissub" />
                    828: </td></tr></table>
                    829: $latexHelp
                    830: <textarea name="message" id="message" cols="80" rows="15" wrap="hard">$dismsg
                    831: </textarea></p><br />
                    832: $dispcrit
                    833: $disbase
                    834: <input type="hidden" name="folder" value="$folder" />
                    835: <input type="hidden" name="dismode" value="$dismode" />
                    836: <input type="submit" name="send" value="$func $lt{'ma'}" />
                    837: <input type="submit" name="cancel" value="$lt{'ca'}" /><hr />
                    838: $citation
                    839: ENDCOMP
                    840:     } else { # $broadcast is 'upload'
                    841: 	$r->print(<<ENDUPLOAD);
                    842: <input type="hidden" name="sendmode" value="upload" />
                    843: <input type="hidden" name="send" value="on" />
                    844: <h3>Generate messages from a file</h3>
                    845: <p>
                    846: Subject: <input type="text" size="50" name="subject" />
                    847: </p>
                    848: <p>General message text<br />
                    849: <textarea name="message" id="message" cols="60" rows="10" wrap="hard">$dismsg
                    850: </textarea></p>
                    851: <p>
                    852: The file format for the uploaded portion of the message is:
                    853: <pre>
                    854: username1\@domain1: text
                    855: username2\@domain2: text
                    856: username3\@domain1: text
                    857: </pre>
                    858: </p>
                    859: <p>
                    860: The messages will be assembled from all lines with the respective 
                    861: <tt>username\@domain</tt>, and appended to the general message text.</p>
                    862: <p>
                    863: <input type="file" name="upfile" size="40" /></p><p>
                    864: $dispcrit
                    865: <input type="submit" value="Upload and Send" /></p>
                    866: ENDUPLOAD
                    867:     }
                    868:     if ($broadcast eq 'group') {
                    869:        &discourse($r);
1.25      foxr      870:        my $studentsel = &discourse();
                    871:        $r->print($studentsel);
1.1       albertel  872:     }
                    873:     $r->print('</form>'.
                    874: 	      &Apache::lonfeedback::generate_preview_button('compemail','message').
                    875: 	      &Apache::lonhtmlcommon::htmlareaselectactive('message'));
                    876: }
                    877: 
                    878: # ---------------------------------------------------- Display all face to face
                    879: 
                    880: sub retrieve_instructor_comments {
                    881:     my ($user,$domain)=@_;
                    882:     my $target=$env{'form.grade_target'};
                    883:     if (! $env{'request.course.id'}) { return; }
                    884:     if (! &Apache::lonnet::allowed('srm',$env{'request.course.id'})
                    885: 	&& ! &Apache::lonnet::allowed('srm',$env{'request.course.id'}.
                    886: 				      '/'.$env{'request.course.sec'})) {
                    887: 	return;
                    888:     }
                    889:     my %records=&Apache::lonnet::dump('nohist_email',
                    890: 			 $env{'course.'.$env{'request.course.id'}.'.domain'},
                    891: 			 $env{'course.'.$env{'request.course.id'}.'.num'},
                    892:                          '%255b'.$user.'%253a'.$domain.'%255d');
                    893:     my $result='';
                    894:     foreach (sort(keys(%records))) {
                    895:         my %content=&Apache::lonmsg::unpackagemsg($records{$_});
                    896:         next if ($content{'senderdomain'} eq '');
                    897:         next if ($content{'subject'} !~ /^Record/);
                    898: 	# &Apache::lonfeedback::newline_to_br(\$content{'message'});
                    899: 	$result.='Recorded by '.
1.14      albertel  900:             $content{'sendername'}.':'.$content{'senderdomain'}."\n";
1.1       albertel  901:         $result.=
                    902:             &Apache::lontexconvert::msgtexconverted($content{'message'})."\n";
                    903:      }
                    904:     return $result;
                    905: }
                    906: 
                    907: sub disfacetoface {
                    908:     my ($r,$user,$domain)=@_;
                    909:     my $target=$env{'form.grade_target'};
                    910:     unless ($env{'request.course.id'}) { return; }
                    911:     if  (!&Apache::lonnet::allowed('srm',$env{'request.course.id'})
                    912: 	 && ! &Apache::lonnet::allowed('srm',$env{'request.course.id'}.
                    913: 				       '/'.$env{'request.course.sec'})) {
                    914: 	$r->print('Not allowed');
                    915: 	return;
                    916:     }
                    917:     my %records=&Apache::lonnet::dump('nohist_email',
                    918: 			 $env{'course.'.$env{'request.course.id'}.'.domain'},
                    919: 			 $env{'course.'.$env{'request.course.id'}.'.num'},
                    920:                          '%255b'.$user.'%253a'.$domain.'%255d');
                    921:     my $result='';
                    922:     foreach (sort keys %records) {
                    923:         my %content=&Apache::lonmsg::unpackagemsg($records{$_});
                    924:         next if ($content{'senderdomain'} eq '');
                    925: 	&Apache::lonfeedback::newline_to_br(\$content{'message'});
                    926:         if ($content{'subject'}=~/^Record/) {
                    927: 	    $result.='<h3>'.&mt('Record').'</h3>';
                    928:         } elsif ($content{'subject'}=~/^Broadcast/) {
                    929:             $result .='<h3>'.&mt('Broadcast Message').'</h3>';
                    930:             if ($content{'subject'}=~/^Broadcast\./) {
                    931:                 if (defined($content{'coursemsgid'})) {
                    932:                     my $crsmsgid = &Apache::lonnet::escape($content{'coursemsgid'});
                    933:                     my $broadcast_message = &general_message($crsmsgid);
                    934:                     $content{'message'} = '<b>'.&mt('Subject').': '.$content{'message'}.'</b><br />'.$broadcast_message;
                    935:                 } else {
                    936:                     %content=&Apache::lonmsg::unpackagemsg($content{'message'});
                    937:                     $content{'message'} =
                    938:                     '<b>'.&mt('Subject').': '.$content{'subject'}.'</b><br />'.
                    939:                     $content{'message'};
                    940:                 }
                    941:             }    
                    942:         } else {
                    943:             $result.='<h3>'.&mt('Critical Message').'</h3>';
                    944:             if (defined($content{'coursemsgid'})) {
                    945:                 my $crsmsgid=&Apache::lonnet::escape($content{'coursemsgid'});
                    946:                 my $critical_message = &general_message($crsmsgid);
                    947:                 $content{'message'} = '<b>'.&mt('Subject').': '.$content{'message'}.'</b><br />'.$critical_message;
                    948:             } else {
                    949:                 %content=&Apache::lonmsg::unpackagemsg($content{'message'});
                    950:                 $content{'message'}=
                    951:                 '<b>'.&mt('Subject').': '.$content{'subject'}.'</b><br />'.
                    952: 		$content{'message'};
                    953:             }
                    954:         }
                    955:         $result.=&mt('By').': <b>'.
                    956: &Apache::loncommon::aboutmewrapper(
                    957:  &Apache::loncommon::plainname($content{'sendername'},$content{'senderdomain'}),$content{'sendername'},$content{'senderdomain'}).'</b> ('.
1.14      albertel  958: $content{'sendername'}.':'.
1.1       albertel  959:             $content{'senderdomain'}.') '.$content{'time'}.
                    960:             '<br /><pre>'.
                    961:               &Apache::lontexconvert::msgtexconverted($content{'message'}).
                    962: 	      '</pre>';
                    963:      }
                    964:     # Check to see if there were any messages.
                    965:     if ($result eq '') {
                    966: 	if ($target ne 'tex') { 
                    967: 	    $r->print("<p><b>".&mt("No notes, face-to-face discussion records, critical messages, or broadcast messages in this course.")."</b></p>");
                    968: 	} else {
                    969: 	    $r->print('\textbf{'.&mt("No notes, face-to-face discussion records, critical messages or broadcast messages in this course.").'}\\\\');
                    970: 	}
                    971:     } else {
                    972:        $r->print($result);
                    973:     }
                    974: }
                    975: 
                    976: sub general_message {
                    977:     my ($crsmsgid) = @_;
                    978:     my %general_content;
                    979:     if ($crsmsgid) { 
                    980:         my %course_content = &Apache::lonnet::get('nohist_email',[$crsmsgid],
                    981:                            $env{'course.'.$env{'request.course.id'}.'.domain'},
                    982:                            $env{'course.'.$env{'request.course.id'}.'.num'});
                    983:         %general_content = &Apache::lonmsg::unpackagemsg($course_content{$crsmsgid});
                    984:     }
                    985:     return $general_content{'message'};
                    986: }
                    987: 
                    988: # ---------------------------------------------------------------- Face to face
                    989: 
                    990: sub facetoface {
                    991:     my ($r,$stage)=@_;
                    992:     if (!&Apache::lonnet::allowed('srm',$env{'request.course.id'})
                    993: 	&& ! &Apache::lonnet::allowed('srm',$env{'request.course.id'}.
                    994: 				      '/'.$env{'request.course.sec'})) {
                    995: 	$r->print('Not allowed');
                    996: 	return;
                    997:     }
                    998:     &printheader($r,
                    999: 		 '/adm/email?recordftf=query',
                   1000: 		 "User Notes, Face-to-Face, Critical Messages, Broadcast Messages");
                   1001: # from query string
                   1002: 
                   1003:     if ($env{'form.recname'}) { $env{'form.recuname'}=$env{'form.recname'}; }
                   1004:     if ($env{'form.recdom'}) { $env{'form.recdomain'}=$env{'form.recdom'}; }
                   1005: 
                   1006:     my $defdom=$env{'user.domain'};
                   1007: # already filled in
                   1008:     if ($env{'form.recdomain'}) { $defdom=$env{'form.recdomain'}; }
                   1009: # generate output
                   1010:     my $domform = &Apache::loncommon::select_dom_form($defdom,'recdomain');
                   1011:     my $stdbrws = &Apache::loncommon::selectstudent_link
                   1012: 	('stdselect','recuname','recdomain');
                   1013:     my %lt=&Apache::lonlocal::texthash('user' => 'Username',
                   1014: 				       'dom' => 'Domain',
                   1015: 				       'head' => 'User Notes, Records of Face-To-Face Discussions, Critical Messages, and Broadcast Messages in Course',
                   1016: 				       'subm' => 'Retrieve discussion and message records',
                   1017: 				       'newr' => 'New Record (record is visible to course faculty and staff)',
                   1018: 				       'post' => 'Post this Record');
                   1019:     $r->print(<<"ENDTREC");
                   1020: <h3>$lt{'head'}</h3>
                   1021: <form method="post" action="/adm/email" name="stdselect">
                   1022: <input type="hidden" name="recordftf" value="retrieve" />
                   1023: <table>
                   1024: <tr><td>$lt{'user'}:</td><td><input type="text" size="12" name="recuname" value="$env{'form.recuname'}" /></td>
                   1025: <td rowspan="2">
                   1026: $stdbrws
                   1027: <input type="submit" value="$lt{'subm'}" /></td>
                   1028: </tr>
                   1029: <tr><td>$lt{'dom'}:</td>
                   1030: <td>$domform</td></tr>
                   1031: </table>
                   1032: </form>
                   1033: ENDTREC
                   1034:     if (($stage ne 'query') &&
                   1035:         ($env{'form.recdomain'}) && ($env{'form.recuname'})) {
                   1036:         chomp($env{'form.newrecord'});
                   1037:         if ($env{'form.newrecord'}) {
                   1038:            my $recordtxt = $env{'form.newrecord'};
                   1039:            &Apache::lonmsg::user_normal_msg_raw(
                   1040:             $env{'course.'.$env{'request.course.id'}.'.num'},
                   1041:             $env{'course.'.$env{'request.course.id'}.'.domain'},
                   1042:             &mt('Record').
                   1043: 	     ' ['.$env{'form.recuname'}.':'.$env{'form.recdomain'}.']',
                   1044: 	    $recordtxt);
                   1045:         }
                   1046:         $r->print('<h3>'.&Apache::loncommon::plainname($env{'form.recuname'},
                   1047: 				     $env{'form.recdomain'}).'</h3>');
                   1048:         &disfacetoface($r,$env{'form.recuname'},$env{'form.recdomain'});
                   1049: 	$r->print(<<ENDRHEAD);
                   1050: <form method="post" action="/adm/email">
                   1051: <input name="recdomain" value="$env{'form.recdomain'}" type="hidden" />
                   1052: <input name="recuname" value="$env{'form.recuname'}" type="hidden" />
                   1053: ENDRHEAD
                   1054:         $r->print(<<ENDBFORM);
                   1055: <hr />$lt{'newr'}<br />
                   1056: <textarea name="newrecord" cols="80" rows="10" wrap="hard"></textarea>
                   1057: <br />
                   1058: <input type="hidden" name="recordftf" value="post" />
                   1059: <input type="submit" value="$lt{'post'}" />
                   1060: </form>
                   1061: ENDBFORM
                   1062:     }
                   1063: }
                   1064: 
                   1065: # ----------------------------------------------------------- Blocking during exams
                   1066: 
                   1067: sub examblock {
                   1068:     my ($r,$action) = @_;
                   1069:     unless ($env{'request.course.id'}) { return;}
                   1070:     if (!&Apache::lonnet::allowed('srm',$env{'request.course.id'})
                   1071: 	&& ! &Apache::lonnet::allowed('srm',$env{'request.course.id'}.
                   1072: 				      '/'.$env{'request.course.sec'})) {
                   1073: 	$r->print('Not allowed');
                   1074: 	return;
                   1075:     }
                   1076:     my %lt=&Apache::lonlocal::texthash(
                   1077:             'comb' => 'Communication Blocking',
                   1078:             'cbds' => 'Communication blocking during scheduled exams',
                   1079:             'desc' => 'You can use communication blocking to prevent students enrolled in this course from displaying LON-CAPA messages sent by other students during an online exam. As blocking of communication could potentially interrupt legitimate communication between students 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.',
                   1080:              'mecb' => 'Modify existing communication blocking periods',
                   1081:              'ncbc' => 'No communication blocks currently stored'
                   1082:     );
                   1083: 
                   1084:     my %ltext = &Apache::lonlocal::texthash(
                   1085:             'dura' => 'Duration',
                   1086:             'setb' => 'Set by',
                   1087:             'even' => 'Event',
                   1088:             'actn' => 'Action',
                   1089:             'star' => 'Start',
                   1090:             'endd' => 'End'
                   1091:     );
                   1092: 
                   1093:     &printheader($r,'/adm/email?block=display',$lt{'comb'});
                   1094:     $r->print('<h3>'.$lt{'cbds'}.'</h3>');
                   1095: 
                   1096:     if ($action eq 'store') {
                   1097:         &blockstore($r);
                   1098:     }
                   1099: 
                   1100:     $r->print($lt{'desc'}.'<br /><br />
                   1101:                <form name="blockform" method="post" action="/adm/email?block=store">
                   1102:              ');
                   1103: 
                   1104:     $r->print('<h4>'.$lt{'mecb'}.'</h4>');
                   1105:     my %records = ();
                   1106:     my $blockcount = 0;
                   1107:     my $parmcount = 0;
                   1108:     &get_blockdates(\%records,\$blockcount);
                   1109:     if ($blockcount > 0) {
                   1110:         $parmcount = &display_blocker_status($r,\%records,\%ltext);
                   1111:     } else {
                   1112:         $r->print($lt{'ncbc'}.'<br /><br />');
                   1113:     }
                   1114:     &display_addblocker_table($r,$parmcount,\%ltext);
                   1115:     my $end_page=&Apache::loncommon::end_page();
                   1116:     $r->print(<<"END");
                   1117: <br />
                   1118: <input type="hidden" name="blocktotal" value="$blockcount" />
                   1119: <input type ="submit" value="Save Changes" />
                   1120: </form>
                   1121: $end_page
                   1122: END
                   1123:     return;
                   1124: }
                   1125: 
                   1126: sub blockstore {
                   1127:     my $r = shift;
                   1128:     my %lt=&Apache::lonlocal::texthash(
                   1129:             'tfcm' => 'The following changes were made',
                   1130:             'cbps' => 'communication blocking period(s)',
                   1131:             'werm' => 'was/were removed',
                   1132:             'wemo' => 'was/were modified',
                   1133:             'wead' => 'was/were added',
                   1134:             'ncwm' => 'No changes were made.' 
                   1135:     );
                   1136:     my %adds = ();
                   1137:     my %removals = ();
                   1138:     my %cancels = ();
                   1139:     my $modtotal = 0;
                   1140:     my $canceltotal = 0;
                   1141:     my $addtotal = 0;
                   1142:     my %blocking = ();
                   1143:     $r->print('<h3>'.$lt{'head'}.'</h3>');
                   1144:     foreach (keys %env) {
                   1145:         if ($_ =~ m/^form\.modify_(\w+)$/) {
                   1146:             $adds{$1} = $1;
                   1147:             $removals{$1} = $1;
                   1148:             $modtotal ++;
                   1149:         } elsif ($_ =~ m/^form\.cancel_(\d+)$/) {
                   1150:             $cancels{$1} = $1;
                   1151:             unless ( defined($removals{$1}) ) {
                   1152:                 $removals{$1} = $1;
                   1153:                 $canceltotal ++;
                   1154:             }
                   1155:         } elsif ($_ =~ m/^form\.add_(\d+)$/) {
                   1156:             $adds{$1} = $1;
                   1157:             $addtotal ++;
                   1158:         }
                   1159:     }
                   1160: 
                   1161:     foreach (keys %removals) {
                   1162:         my $hashkey = $env{'form.key_'.$_};
                   1163:         &Apache::lonnet::del('comm_block',["$hashkey"],
                   1164:                          $env{'course.'.$env{'request.course.id'}.'.domain'},
                   1165:                          $env{'course.'.$env{'request.course.id'}.'.num'}
                   1166:                          );
                   1167:     }
                   1168:     foreach (keys %adds) {
                   1169:         unless ( defined($cancels{$_}) ) {
                   1170:             my ($newstart,$newend) = &get_dates_from_form($_);
                   1171:             my $newkey = $newstart.'____'.$newend;
1.14      albertel 1172:             $blocking{$newkey} = $env{'user.name'}.':'.$env{'user.domain'}.':'.$env{'form.title_'.$_};
1.1       albertel 1173:         }
                   1174:     }
                   1175:     if ($addtotal + $modtotal > 0) {
                   1176:         &Apache::lonnet::put('comm_block',\%blocking,
                   1177:                      $env{'course.'.$env{'request.course.id'}.'.domain'},
                   1178:                      $env{'course.'.$env{'request.course.id'}.'.num'}
                   1179:                      );
                   1180:     }
                   1181:     my $chgestotal = $canceltotal + $modtotal + $addtotal;
                   1182:     if ($chgestotal > 0) {
                   1183:         $r->print($lt{'tfcm'}.'<ul>');
                   1184:         if ($canceltotal > 0) {
                   1185:             $r->print('<li>'.$canceltotal.' '.$lt{'cbps'},' '.$lt{'werm'}.'</li>');
                   1186:         }
                   1187:         if ($modtotal > 0) {
                   1188:             $r->print('<li>'.$modtotal.' '.$lt{'cbps'},' '.$lt{'wemo'}.'</li>');
                   1189:         }
                   1190:         if ($addtotal > 0) {
                   1191:             $r->print('<li>'.$addtotal.' '.$lt{'cbps'},' '.$lt{'wead'}.'</li>');
                   1192:         }
                   1193:         $r->print('</ul>');
                   1194:     } else {
                   1195:         $r->print($lt{'ncwm'});
                   1196:     }
                   1197:     $r->print('<br />');
                   1198:     return;
                   1199: }
                   1200: 
                   1201: sub get_dates_from_form {
                   1202:     my $item = shift;
                   1203:     my $startdate = &Apache::lonhtmlcommon::get_date_from_form('startdate_'.$item);
                   1204:     my $enddate   = &Apache::lonhtmlcommon::get_date_from_form('enddate_'.$item);
                   1205:     return ($startdate,$enddate);
                   1206: }
                   1207: 
                   1208: sub get_blockdates {
                   1209:     my ($records,$blockcount) = @_;
                   1210:     $$blockcount = 0;
                   1211:     %{$records} = &Apache::lonnet::dump('comm_block',
                   1212:                          $env{'course.'.$env{'request.course.id'}.'.domain'},
                   1213:                          $env{'course.'.$env{'request.course.id'}.'.num'}
                   1214:                          );
1.15      albertel 1215:     $$blockcount = keys(%{$records});
                   1216: 
                   1217:     if ((keys(%{$records}))[0] =~ /^error: 2 /) {
                   1218: 	$records = {};
                   1219: 	$$blockcount = 0;
1.1       albertel 1220:     }
                   1221: }
                   1222: 
                   1223: sub display_blocker_status {
                   1224:     my ($r,$records,$ltext) = @_;
                   1225:     my $parmcount = 0;
1.16      albertel 1226:   
1.1       albertel 1227:     my %lt = &Apache::lonlocal::texthash(
                   1228:         'modi' => 'Modify',
                   1229:         'canc' => 'Cancel',
                   1230:     );
1.18      albertel 1231:     $r->print(&Apache::loncommon::start_data_table());
1.1       albertel 1232:     $r->print(<<"END");
1.16      albertel 1233:   <tr>
                   1234:     <th>$$ltext{'dura'}</th>
                   1235:     <th>$$ltext{'setb'}</th>
                   1236:     <th>$$ltext{'even'}</th>
                   1237:     <th>$$ltext{'actn'}?</th>
                   1238:   </tr>
1.1       albertel 1239: END
1.18      albertel 1240:     foreach my $record (sort(keys(%{$records}))) {
1.1       albertel 1241:         my $onchange = 'onFocus="javascript:window.document.forms['.
                   1242:                        "'blockform'].elements['modify_".$parmcount."'].".
                   1243:                        'checked=true;"';
1.18      albertel 1244:         my ($start,$end) = split(/____/,$record);
1.1       albertel 1245:         my $startform = &Apache::lonhtmlcommon::date_setter('blockform','startdate_'.$parmcount,$start,$onchange);
                   1246:         my $endform = &Apache::lonhtmlcommon::date_setter('blockform','enddate_'.$parmcount,$end,$onchange);
1.15      albertel 1247: 	
1.18      albertel 1248: 	my ($setuname,$setudom,$title) = 
                   1249: 	    &parse_block_record($$records{$record});
1.21      albertel 1250: 	$title = &HTML::Entities::encode($title,'"<>&');
1.1       albertel 1251:         my $settername = &Apache::loncommon::plainname($setuname,$setudom);
1.18      albertel 1252:         $r->print(&Apache::loncommon::start_data_table_row());
1.1       albertel 1253:         $r->print(<<"END");
                   1254:         <td>$$ltext{'star'}:&nbsp;$startform<br/>$$ltext{'endd'}:&nbsp;&nbsp;$endform</td>
                   1255:         <td>$settername</td>
1.18      albertel 1256:         <td><input type="text" name="title_$parmcount" size="15" value="$title" /><input type="hidden" name="key_$parmcount" value="$record" /></td>
1.1       albertel 1257:         <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>
                   1258: END
1.18      albertel 1259:         $r->print(&Apache::loncommon::end_data_table_row());
                   1260:         $parmcount++;
1.1       albertel 1261:     }
                   1262:     $r->print(<<"END");
                   1263: </table>
                   1264: <br />
                   1265: <br />
                   1266: END
                   1267:     return $parmcount;
                   1268: }
                   1269: 
1.15      albertel 1270: sub parse_block_record {
                   1271:     my ($record) = @_;
                   1272:     my ($setuname,$setudom,$title);
                   1273:     my @data = split(/:/,$record,3);
                   1274:     if (scalar(@data) eq 2) {
                   1275: 	$title = $data[1];
                   1276: 	($setuname,$setudom) = split(/@/,$data[0]);
                   1277:     } else {
                   1278: 	($setuname,$setudom,$title) = @data;
                   1279:     }
                   1280:     return ($setuname,$setudom,$title);
                   1281: }
                   1282: 
1.1       albertel 1283: sub display_addblocker_table {
                   1284:     my ($r,$parmcount,$ltext) = @_;
                   1285:     my $start = time;
                   1286:     my $end = $start + (60 * 60 * 2); #Default is an exam of 2 hours duration.
                   1287:     my $onchange = 'onFocus="javascript:window.document.forms['.
                   1288:                    "'blockform'].elements['add_".$parmcount."'].".
                   1289:                    'checked=true;"';
                   1290:     my $startform = &Apache::lonhtmlcommon::date_setter('blockform','startdate_'.$parmcount,$start,$onchange);
                   1291:     my $endform = &Apache::lonhtmlcommon::date_setter('blockform','enddate_'.$parmcount,$end,$onchange);
                   1292:     my %lt = &Apache::lonlocal::texthash(
                   1293:         'addb' => 'Add block',
                   1294:         'exam' => 'e.g., Exam 1',
                   1295:         'addn' => 'Add new communication blocking periods'
                   1296:     );
                   1297:     $r->print(<<"END");
                   1298: <h4>$lt{'addn'}</h4> 
1.18      albertel 1299: END
                   1300:     $r->print(&Apache::loncommon::start_data_table());
                   1301:     $r->print(<<"END");
1.16      albertel 1302:    <tr>
                   1303:      <th>$$ltext{'dura'}</th>
                   1304:      <th>$$ltext{'even'} $lt{'exam'}</th>
                   1305:      <th>$$ltext{'actn'}?</th>
                   1306:    </tr>
1.18      albertel 1307: END
                   1308:    $r->print(&Apache::loncommon::start_data_table_row());
                   1309:     $r->print(<<"END");
1.16      albertel 1310:      <td>$$ltext{'star'}:&nbsp;$startform<br />$$ltext{'endd'}:&nbsp;&nbsp;$endform</td>
                   1311:      <td><input type="text" name="title_$parmcount" size="15" value="" /></td>
                   1312:      <td><label>$lt{'addb'}?&nbsp;<input type="checkbox" name="add_$parmcount" value="1" /></label></td>
1.1       albertel 1313: END
1.18      albertel 1314:     $r->print(&Apache::loncommon::end_data_table_row());
                   1315:     $r->print(&Apache::loncommon::end_data_table());
1.1       albertel 1316:     return;
                   1317: }
                   1318: 
                   1319: sub blockcheck {
                   1320:     my ($setters,$startblock,$endblock) = @_;
                   1321:     # Retrieve active student roles and active course coordinator/instructor roles
1.15      albertel 1322:     my %live_courses =
                   1323: 	map { $_ => 1} &Apache::loncommon::findallcourses();
                   1324:     # FIXME should really probe for apriv, but ::allowed can only probe the 
                   1325:     #       currently active role
                   1326:     my %staff_of =
                   1327: 	map { $_ => 1} &Apache::loncommon::findallcourses(['cc','in']);
                   1328: 
                   1329:     # Retrieve blocking times and identity of blocker for active courses
                   1330:     # for students.
                   1331:     return if (!%live_courses);
                   1332: 
                   1333:     foreach my $course (keys(%live_courses)) {
1.19      albertel 1334: 	my $cdom = $env{'course.'.$course.'.domain'};
                   1335: 	my $cnum = $env{'course.'.$course.'.num'};
1.15      albertel 1336: 
                   1337: 	# if they are a staff member and are currently not playing student
                   1338: 	next if ( $staff_of{$course} 
                   1339: 		  && ($env{'request.role'} !~ m{^st\./$cdom/$cnum}));
                   1340: 
                   1341: 	$setters->{$course} = {};
                   1342: 	$setters->{$course}{'staff'} = [];
                   1343: 	$setters->{$course}{'times'} = [];
                   1344: 	my %records = &Apache::lonnet::dump('comm_block',$cdom,$cnum);
                   1345: 	foreach my $record (keys %records) {
                   1346: 	    my ($start,$end) = ($record =~ m/^(\d+)____(\d+)$/);
                   1347: 	    if ($start <= time && $end >= time) {
                   1348: 		my ($staff_name,$staff_dom,$title) = 
                   1349: 		    &parse_block_record($records{$record});
                   1350: 		push(@{$$setters{$course}{'staff'}}, [$staff_name,$staff_dom]);
                   1351: 		push(@{$$setters{$course}{'times'}}, [$start,$end]);
                   1352: 		if ( ($$startblock == 0) || ($$startblock > $1) ) {
                   1353: 		    $$startblock = $1;
                   1354: 		}
                   1355: 		if ( ($$endblock == 0) || ($$endblock < $2) ) {
                   1356: 		    $$endblock = $2;
                   1357: 		}
                   1358: 	    }
                   1359: 	}
1.1       albertel 1360:     }
                   1361: }
                   1362: 
                   1363: sub build_block_table {
                   1364:     my ($r,$startblock,$endblock,$setters) = @_;
                   1365:     my %lt = &Apache::lonlocal::texthash(
                   1366:         'cacb' => 'Currently active communication blocks',
                   1367:         'cour' => 'Course',
                   1368:         'dura' => 'Duration',
                   1369:         'blse' => 'Block set by'
1.18      albertel 1370:     );
                   1371:     $r->print(<<"END");
                   1372: <br /><br />$lt{'cacb'}:<br /><br />
                   1373: END
                   1374:     $r->print(&Apache::loncommon::start_data_table());
1.1       albertel 1375:     $r->print(<<"END");
1.16      albertel 1376: <tr>
                   1377:  <th>$lt{'cour'}</th>
                   1378:  <th>$lt{'dura'}</th>
                   1379:  <th>$lt{'blse'}</th>
                   1380: </tr>
1.1       albertel 1381: END
1.18      albertel 1382:     foreach my $course (keys(%{$setters})) {
                   1383:         my %courseinfo=&Apache::lonnet::coursedescription($course);
                   1384:         for (my $i=0; $i<@{$$setters{$course}{staff}}; $i++) {
                   1385:             my ($uname,$udom) = @{$$setters{$course}{staff}[$i]};
1.1       albertel 1386:             my $fullname = &Apache::loncommon::plainname($uname,$udom);
1.18      albertel 1387:             my ($openblock,$closeblock) = @{$$setters{$course}{times}[$i]};
1.1       albertel 1388:             $openblock = &Apache::lonlocal::locallocaltime($openblock);
                   1389:             $closeblock= &Apache::lonlocal::locallocaltime($closeblock);
1.18      albertel 1390:             $r->print(&Apache::loncommon::start_data_table_row().
                   1391: 		      '<td>'.$courseinfo{'description'}.'</td>'.
1.1       albertel 1392:                       '<td>'.$openblock.' to '.$closeblock.'</td>'.
1.14      albertel 1393:                       '<td>'.$fullname.' ('.$uname.':'.$udom.
1.18      albertel 1394:                       ')</td>'.
                   1395: 		       &Apache::loncommon::end_data_table_row());
1.1       albertel 1396:         }
                   1397:     }
1.18      albertel 1398:     $r->print(&Apache::loncommon::end_data_table());
1.1       albertel 1399: }
                   1400: 
                   1401: # ----------------------------------------------------------- Display a message
                   1402: 
                   1403: sub displaymessage {
                   1404:     my ($r,$msgid,$folder)=@_;
                   1405:     my $suffix=&Apache::lonmsg::foldersuffix($folder);
                   1406:     my %blocked = ();
                   1407:     my %setters = ();
                   1408:     my $startblock = 0;
                   1409:     my $endblock = 0;
                   1410:     my $numblocked = 0;
                   1411: # info to generate "next" and "previous" buttons and check if message is blocked
                   1412:     &blockcheck(\%setters,\$startblock,\$endblock);
                   1413:     my @messages=&sortedmessages(\%blocked,$startblock,$endblock,\$numblocked,$folder);
                   1414:     if ( $blocked{$msgid} eq 'ON' ) {
                   1415:         &printheader($r,'/adm/email',&mt('Display a Message'));
                   1416:         $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.'));
                   1417:         &build_block_table($r,$startblock,$endblock,\%setters);
                   1418:         return;
                   1419:     }
                   1420:     &statuschange($msgid,'read',$folder);
                   1421:     my %message=&Apache::lonnet::get('nohist_email'.$suffix,[$msgid]);
                   1422:     my %content=&Apache::lonmsg::unpackagemsg($message{$msgid});
                   1423: 
                   1424:     my $counter=0;
                   1425:     $r->print('<pre>');
                   1426:     my $escmsgid=&Apache::lonnet::escape($msgid);
                   1427:     foreach (@messages) {
                   1428: 	if ($_->[5] eq $escmsgid){
                   1429: 	    last;
                   1430: 	}
                   1431: 	$counter++;
                   1432:     }
                   1433:     $r->print('</pre>');
                   1434:     my $number_of_messages = scalar(@messages); #subtract 1 for last index
                   1435: # start output
                   1436:     &printheader($r,'/adm/email?display='.&Apache::lonnet::escape($msgid),'Display a Message','',$content{'baseurl'});
                   1437:     my %courseinfo=&Apache::lonnet::coursedescription($content{'courseid'});
                   1438: # Functions
                   1439:     $r->print('<table border="2" width="100%"><tr bgcolor="#FFFFAA"><td>'.&mt('Functions').':</td>'.
                   1440: 	      '<td><a href="/adm/email?replyto='.&Apache::lonnet::escape($msgid).$sqs.
                   1441: 	      '"><b>'.&mt('Reply').'</b></a></td>'.
                   1442: 	      '<td><a href="/adm/email?forward='.&Apache::lonnet::escape($msgid).$sqs.
                   1443: 	      '"><b>'.&mt('Forward').'</b></a></td>'.
                   1444: 	      '<td><a href="/adm/email?markunread='.&Apache::lonnet::escape($msgid).$sqs.
                   1445: 	      '"><b>'.&mt('Mark Unread').'</b></a></td>'.
                   1446: 	      '<td><a href="/adm/email?markdel='.&Apache::lonnet::escape($msgid).$sqs.
                   1447: 	      '"><b>'.&mt('Delete').'</b></a></td>'.
                   1448: 	      '<td><a href="/adm/email?'.$sqs.
                   1449: 	      ($env{'form.dismode'} eq 'new'?'&folder=new':'').
                   1450: 	      '"><b>'.&mt('Back to Folder Display').'</b></a></td>');
                   1451:     if ($counter > 0){
                   1452: 	$r->print('<td><a href="/adm/email?display='.$messages[$counter-1]->[5].$sqs.
                   1453: 		  '"><b>'.&mt('Previous').'</b></a></td>');
                   1454:     }
                   1455:     if ($counter < $number_of_messages - 1){
                   1456: 	$r->print('<td><a href="/adm/email?display='.$messages[$counter+1]->[5].$sqs.
                   1457: 		  '"><b>'.&mt('Next').'</b></a></td>');
                   1458:     }
                   1459:     $r->print('</tr></table>');
                   1460:     if ($env{'user.adv'}) {
                   1461: 	$r->print('<table border="2" width="100%"><tr bgcolor="#FFAAAA"><td>'.&mt('Currently available actions (will open extra window)').':</td>');
                   1462: 	my $symb=&Apache::lonnet::symbread($content{'baseurl'});      
                   1463: 	if (&Apache::lonnet::allowed('vgr',$env{'request.course.id'})) {
                   1464: 		$r->print('<td><b>'.&Apache::loncommon::track_student_link(&mt('View recent activity'),$content{'sendername'},$content{'senderdomain'},'check').'</b></td>');
                   1465: 	    }
                   1466: 	if (&Apache::lonnet::allowed('opa',$env{'request.course.id'}) && $symb) {
                   1467: 	    $r->print('<td><b>'.&Apache::loncommon::pprmlink(&mt('Set/Change parameters'),$content{'sendername'},$content{'senderdomain'},$symb,'check').'</b></td>');
                   1468: 	}
                   1469: 	if (&Apache::lonnet::allowed('mgr',$env{'request.course.id'}) && $symb) {
                   1470: 	    $r->print('<td><b>'.&Apache::loncommon::pgrdlink(&mt('Set/Change grades'),$content{'sendername'},$content{'senderdomain'},$symb,'check').'</b></td>');
                   1471: 	}
                   1472: 	$r->print('</tr></table>');
                   1473:     }
                   1474:     my $tolist;
                   1475:     my @recipients = ();
                   1476:     for (my $i=0; $i<@{$content{'recuser'}}; $i++) {
                   1477:         $recipients[$i] =  &Apache::loncommon::aboutmewrapper(
                   1478:            &Apache::loncommon::plainname($content{'recuser'}[$i],
                   1479:                                       $content{'recdomain'}[$i]),
                   1480:               $content{'recuser'}[$i],$content{'recdomain'}[$i]).
                   1481:        ' ('.$content{'recuser'}[$i].' at '.$content{'recdomain'}[$i].') ';
                   1482:     }
                   1483:     $tolist = join(', ',@recipients);
                   1484:     $r->print('<br /><b>'.&mt('Subject').':</b> '.$content{'subject'}.
                   1485: 	      ($folder ne 'sent'?'<br /><b>'.&mt('From').':</b> '.
                   1486: 	      &Apache::loncommon::aboutmewrapper(
                   1487: 						 &Apache::loncommon::plainname($content{'sendername'},$content{'senderdomain'}),
                   1488: 						 $content{'sendername'},$content{'senderdomain'}).' ('.
                   1489: 	      $content{'sendername'}.' at '.
                   1490: 	      $content{'senderdomain'}.') ':'<br /><b>'.&mt('To').':</b> '.
                   1491:               $tolist).
                   1492: 	      ($content{'courseid'}?'<br /><b>'.&mt('Course').':</b> '.$courseinfo{'description'}.
                   1493: 	       ($content{'coursesec'}?' ('.&mt('Group/Section').': '.$content{'coursesec'}.')':''):'').
                   1494: 	      '<br /><b>'.&mt('Time').':</b> '.$content{'time'}.
                   1495: 	      ($content{'baseurl'}?'<br /><b>'.&mt('Refers to').':</b> <a href="'.$content{'baseurl'}.'">'.
                   1496: 	       $content{'baseurl'}.' ('.&Apache::lonnet::gettitle($content{'baseurl'}).')</a>':'').
                   1497: 	      '<p><pre>'.
                   1498: 	      &Apache::lontexconvert::msgtexconverted($content{'message'},1).
                   1499: 	      '</pre><hr />'.&displayresource(%content).'</p>');
                   1500:     return;   
                   1501: }
                   1502: 
                   1503: # =========================================================== Show the citation
                   1504: 
                   1505: sub displayresource {
                   1506:     my %content=@_;
                   1507: #
                   1508: # If the recipient is in the same course that the message was sent from and
                   1509: # has sufficient privileges, show "all details," else show citation
                   1510: #
                   1511:     if (($env{'request.course.id'} eq $content{'courseid'})
                   1512:      && (&Apache::lonnet::allowed('vgr',$content{'courseid'}))) {
                   1513: 	my $symb=&Apache::lonnet::symbread($content{'baseurl'});
                   1514: # Could not get a symb, give up
                   1515: 	unless ($symb) { return $content{'citation'}; }
                   1516: # Have a symb, can render
                   1517: 	return '<h2>'.&mt('Current attempts of student (if applicable)').'</h2>'.
                   1518: 	    &Apache::loncommon::get_previous_attempt($symb,
                   1519: 						     $content{'sendername'},
                   1520: 						     $content{'senderdomain'},
                   1521: 						     $content{'courseid'}).
                   1522: 	    '<hr /><h2>'.&mt('Current screen output (if applicable)').'</h2>'.
                   1523: 	    &Apache::loncommon::get_student_view($symb,
                   1524: 						 $content{'sendername'},
                   1525: 						 $content{'senderdomain'},
                   1526: 						 $content{'courseid'}).
                   1527: 	    '<h2>'.&mt('Correct Answer(s) (if applicable)').'</h2>'.
                   1528: 	    &Apache::loncommon::get_student_answers($symb,
                   1529: 						    $content{'sendername'},
                   1530: 						    $content{'senderdomain'},
                   1531: 						    $content{'courseid'});
                   1532:     } elsif ($env{'user.adv'}) {
                   1533: 	return $content{'citation'};
                   1534:     }
                   1535:     return '';
                   1536: }
                   1537: 
                   1538: # ================================================================== The Header
                   1539: 
                   1540: sub header {
                   1541:     my ($r,$title,$baseurl)=@_;
                   1542:     
                   1543:     my $extra = &Apache::loncommon::studentbrowser_javascript();
                   1544:     if ($baseurl) {
                   1545: 	$extra .= "<base href=\"http://$ENV{'SERVER_NAME'}/$baseurl\" />";
                   1546:     }
                   1547:     $r->print(&Apache::loncommon::start_page('Communication and Messages',
                   1548: 					     $extra));
                   1549:     $r->print(&Apache::lonhtmlcommon::breadcrumbs
1.22      albertel 1550: 	      (($title?$title:'Communication and Messages')));
1.1       albertel 1551: 
                   1552: }
                   1553: 
                   1554: # ---------------------------------------------------------------- Print header
                   1555: 
                   1556: sub printheader {
                   1557:     my ($r,$url,$desc,$title,$baseurl)=@_;
                   1558:     &Apache::lonhtmlcommon::add_breadcrumb
                   1559: 	({href=>$url,
                   1560: 	  text=>$desc});
                   1561:     &header($r,$title,$baseurl);
                   1562: }
                   1563: 
                   1564: # ------------------------------------------------------------ Store the comment
                   1565: 
                   1566: sub storecomment {
                   1567:     my ($r)=@_;
                   1568:     my $msgtxt=&Apache::lonfeedback::clear_out_html($env{'form.message'});
                   1569:     my $cleanmsgtxt='';
                   1570:     foreach (split(/[\n\r]/,$msgtxt)) {
                   1571: 	unless ($_=~/^\s*(\>|\&gt\;)/) {
                   1572: 	    $cleanmsgtxt.=$_."\n";
                   1573: 	}
                   1574:     }
                   1575:     my $key=&Apache::lonnet::escape($env{'form.baseurl'}).'___'.time;
                   1576:     &Apache::lonnet::put('nohist_stored_comments',{ $key => $cleanmsgtxt });
                   1577: }
                   1578: 
                   1579: sub storedcommentlisting {
                   1580:     my ($r)=@_;
                   1581:     my %msgs=&Apache::lonnet::dump('nohist_stored_comments',undef,undef,
                   1582:        '^'.&Apache::lonnet::escape(&Apache::lonnet::escape($env{'form.showcommentbaseurl'})));
                   1583:     $r->print(&Apache::loncommon::start_page('Stored Comment Listing',undef,
                   1584: 					     {'onlybody' => 1}));
                   1585:     if ((keys %msgs)[0]=~/^error\:/) {
                   1586: 	$r->print(&mt('No stored comments yet.'));
                   1587:     } else {
                   1588: 	my $found=0;
                   1589: 	foreach (sort keys %msgs) {
                   1590: 	    $r->print("\n".$msgs{$_}."<hr />");
                   1591: 	    $found=1;
                   1592: 	}
                   1593: 	unless ($found) {
                   1594: 	    $r->print(&mt('No stored comments yet for this resource.'));
                   1595: 	}
                   1596:     }
                   1597: }
                   1598: 
                   1599: # ---------------------------------------------------------------- Send an email
                   1600: 
                   1601: sub sendoffmail {
                   1602:     my ($r,$folder)=@_;
                   1603:     my $suffix=&Apache::lonmsg::foldersuffix($folder);
                   1604:     my $sendstatus='';
                   1605:     my %specialmsg_status;
                   1606:     my $numspecial = 0;
                   1607:     if ($env{'form.send'}) {
                   1608: 	&printheader($r,'','Messages being sent.');
                   1609: 	$r->rflush();
                   1610: 	my %content=();
                   1611: 	undef %content;
                   1612: 	if ($env{'form.forwid'}) {
                   1613: 	    my $msgid=$env{'form.forwid'};
                   1614: 	    my %message=&Apache::lonnet::get('nohist_email'.$suffix,[$msgid]);
                   1615: 	    %content=&Apache::lonmsg::unpackagemsg($message{$msgid},1);
                   1616: 	    &statuschange($msgid,'forwarded',$folder);
                   1617: 	    $env{'form.message'}.="\n\n-- Forwarded message --\n\n".
                   1618: 		$content{'message'};
                   1619: 	}
                   1620: 	if ($env{'form.replyid'}) {
                   1621: 	    my $msgid=$env{'form.replyid'};
                   1622: 	    my %message=&Apache::lonnet::get('nohist_email'.$suffix,[$msgid]);
                   1623: 	    %content=&Apache::lonmsg::unpackagemsg($message{$msgid},1);
                   1624: 	    &statuschange($msgid,'replied',$folder);
                   1625: 	}
1.13      albertel 1626: 
1.25      foxr     1627: 	my $to = $env{'form.selectedusers.forminput'};
                   1628: 	my $mode = $env{'form.sendmode'};
                   1629: 
1.13      albertel 1630: 	my %toaddr;
1.25      foxr     1631: 	if ($to) {
                   1632: 	    foreach my $dest (@$to) {
                   1633: 		my ($user,$domain) = split /:/, $dest;
                   1634: 		if (($user ne '') && ($domain ne '')) {
                   1635: 		    my $address = $user.":".$domain; # How the code below expects it.
                   1636: 		    $toaddr{$address} = '';
                   1637: 		}
                   1638: 	    }
                   1639: 	}
                   1640: 
1.1       albertel 1641: 	if ($env{'form.sendmode'} eq 'group') {
1.25      foxr     1642: 	     foreach my $address (keys(%env)) {
                   1643: 	 	if ($address=~/^form\.send\_to\_\&\&\&[^\&]*\&\&\&\_(.+)$/) {
                   1644: 	 	    $toaddr{$1}='';
                   1645: 	 	}
1.1       albertel 1646: 	    }
                   1647: 	} elsif ($env{'form.sendmode'} eq 'upload') {
1.13      albertel 1648: 	    foreach my $line (split(/[\n\r\f]+/,$env{'form.upfile'})) {
                   1649: 		my ($rec,$txt)=split(/\s*\:\s*/,$line);
1.1       albertel 1650: 		if ($txt) {
                   1651: 		    $rec=~s/\@/\:/;
                   1652: 		    $toaddr{$rec}.=$txt."\n";
                   1653: 		}
                   1654: 	    }
                   1655: 	} else {
1.25      foxr     1656: 	    if (($env{'form.recuname'} ne '') && ($env{'form.recdomain'} ne '')) {
                   1657: 		$toaddr{$env{'form.recuname'}.':'.$env{'form.recdomain'}}='';
                   1658: 	    }
1.1       albertel 1659: 	}
                   1660: 	if ($env{'form.additionalrec'}) {
                   1661: 	    foreach (split(/\,/,$env{'form.additionalrec'})) {
                   1662: 		my ($auname,$audom)=split(/\@/,$_);
1.25      foxr     1663: 		if (($auname ne "") && ($audom ne "")) {
                   1664: 		    $toaddr{$auname.':'.$audom}='';
                   1665: 		}
1.1       albertel 1666: 	    }
                   1667: 	}
                   1668: 
                   1669:         my $savemsg;
                   1670:         my $msgtype;
                   1671:         my %sentmessage;
1.7       albertel 1672:         my $msgsubj=&Apache::lonfeedback::clear_out_html($env{'form.subject'},
                   1673: 							 undef,1);
1.1       albertel 1674:         if ((($env{'form.critmsg'}) || ($env{'form.sendbck'})) &&
                   1675:             (&Apache::lonnet::allowed('srm',$env{'request.course.id'})
                   1676: 	     || &Apache::lonnet::allowed('srm',$env{'request.course.id'}.
                   1677: 					 '/'.$env{'request.course.sec'})
                   1678: 	     )) {
                   1679:             $savemsg=&Apache::lonfeedback::clear_out_html($env{'form.message'},1);
                   1680:             $msgtype = 'critical';
                   1681:         } else {
                   1682:             $savemsg=&Apache::lonfeedback::clear_out_html($env{'form.message'});
                   1683:         }
                   1684: 	
1.13      albertel 1685: 	foreach my $address (sort(keys(%toaddr))) {
                   1686: 	    my ($recuname,$recdomain)=split(/\:/,$address);
1.1       albertel 1687:             my $msgtxt = $savemsg;
1.13      albertel 1688: 	    if ($toaddr{$address}) { $msgtxt.='<hr />'.$toaddr{$address}; }
1.12      albertel 1689: 	    my @thismsg;
1.1       albertel 1690: 	    if ((($env{'form.critmsg'}) || ($env{'form.sendbck'})) && 
                   1691: 		(&Apache::lonnet::allowed('srm',$env{'request.course.id'})
                   1692: 		 || &Apache::lonnet::allowed('srm',$env{'request.course.id'}.
                   1693: 					     '/'.$env{'request.course.sec'}))) {
1.13      albertel 1694: 		$r->print(&mt('Sending critical message').' '.$recuname.':'.$recdomain.': ');
                   1695: 		@thismsg=
                   1696: 		    &Apache::lonmsg::user_crit_msg($recuname,$recdomain,
                   1697: 						   $msgsubj,$msgtxt,
                   1698: 						   $env{'form.sendbck'},
                   1699: 						   $env{'form.permanent'},
                   1700: 						   \$sentmessage{$address});
1.1       albertel 1701: 	    } else {
1.14      albertel 1702: 		$r->print(&mt('Sending').' '.$recuname.':'.$recdomain.': ');
1.13      albertel 1703: 		@thismsg=
                   1704: 		    &Apache::lonmsg::user_normal_msg($recuname,$recdomain,
                   1705: 						     $msgsubj,$msgtxt,
                   1706: 						     $content{'citation'},
                   1707: 						     undef,undef,
                   1708: 						     $env{'form.permanent'},
                   1709: 						     \$sentmessage{$address});
1.1       albertel 1710:             }
                   1711: 	    if (($env{'request.course.id'}) && (($msgtype eq 'critical') || 
                   1712:                                          ($env{'form.sendmode'} eq 'group'))) {
1.12      albertel 1713: 	        $specialmsg_status{$recuname.':'.$recdomain} =
                   1714: 		    join(' ',@thismsg);
                   1715: 		foreach my $result (@thismsg) {
                   1716: 		    if ($result eq 'ok') {
                   1717: 			$numspecial++;
                   1718: 		    }
                   1719: 		}
1.1       albertel 1720: 	    }
1.12      albertel 1721: 	    $sendstatus.=' '.join(' ',@thismsg);
1.1       albertel 1722: 	}
                   1723:         if (($env{'request.course.id'}) && (($env{'form.sendmode'} eq 'group')
                   1724:                                               || ($msgtype eq 'critical'))) {
                   1725:             my $subj_prefix;
                   1726:             if ($msgtype eq 'critical') {
                   1727:                 $subj_prefix = 'Critical.';
                   1728:             } else {
                   1729:                 $subj_prefix = 'Broadcast.';
                   1730:             }
                   1731:             my ($specialmsgid,$specialresult);
                   1732:             my $cnum = $env{'course.'.$env{'request.course.id'}.'.num'};
                   1733:             my $cdom = $env{'course.'.$env{'request.course.id'}.'.domain'};
                   1734:             my $course_str = &Apache::lonnet::escape('['.$cnum.':'.$cdom.']');
                   1735: 
                   1736:             if ($numspecial) {
                   1737:                 $specialresult = &Apache::lonmsg::user_normal_msg_raw($cnum,$cdom,$subj_prefix.
                   1738:                     ' '.$course_str,$savemsg,undef,undef,undef,
                   1739:                     undef,undef,\$specialmsgid);
                   1740:                 $specialmsgid = &Apache::lonnet::unescape($specialmsgid);
                   1741:             }
                   1742:             if ($specialresult eq 'ok') {
                   1743:                 my $record_sent;
1.13      albertel 1744:                 my @recusers;
                   1745:                 my @recudoms;
                   1746:                 my ($stamp,$crssubj,$msgname,$msgdom,$msgcount,$context,$pid) =
                   1747: 		    split(/\:/,&Apache::lonnet::unescape($specialmsgid));
                   1748: 
1.1       albertel 1749:                 foreach my $recipient (sort(keys(%toaddr))) {
                   1750:                     if ($specialmsg_status{$recipient} eq 'ok') {
                   1751:                         my $usersubj = $subj_prefix.'['.$recipient.']';
                   1752:                         my $usermsgid = 
                   1753: 			    &Apache::lonmsg::buildmsgid($stamp,$usersubj,
                   1754: 							$msgname,$msgdom,
                   1755: 							$msgcount,$context,
                   1756: 							$pid);
                   1757:                         &Apache::lonmsg::user_normal_msg_raw($cnum,$cdom,$subj_prefix.
                   1758:                                              ' ['.$recipient.']',$msgsubj,undef,
                   1759:                         undef,undef,undef,$usermsgid,undef,undef,$specialmsgid);
1.13      albertel 1760:                         my ($uname,$udom) = split(/:/,$recipient);
1.1       albertel 1761:                         push(@recusers,$uname);
                   1762:                         push(@recudoms,$udom);
                   1763:                     }
                   1764:                 }
                   1765:                 if (@recusers) {
                   1766:                     my $specialmessage;
1.13      albertel 1767:                     my $sentsubj = 
                   1768: 			$subj_prefix.' ('.$numspecial.' sent) '.$msgsubj;
1.1       albertel 1769:                     $sentsubj = &HTML::Entities::encode($sentsubj,'<>&"');
                   1770:                     my $sentmsgid = 
                   1771: 			&Apache::lonmsg::buildmsgid($stamp,$sentsubj,$msgname,
                   1772: 						    $msgdom,$msgcount,$context,
                   1773: 						    $pid);
                   1774:                     ($specialmsgid,$specialmessage) = &Apache::lonmsg::packagemsg($msgsubj,$savemsg,
                   1775:                             undef,undef,undef,\@recusers,\@recudoms,$sentmsgid);
                   1776:                     $record_sent = &Apache::lonmsg::store_sent_mail($specialmsgid,$specialmessage);
                   1777:                 }
                   1778:             } else {
                   1779:                 &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');
                   1780:             }
                   1781:         }
                   1782:     } else {
                   1783: 	&printheader($r,'','No messages sent.'); 
                   1784:     }
                   1785:     if ($sendstatus=~/^(\s*(?:ok|con_delayed)\s*)*$/) {
1.5       albertel 1786: 	$r->print('<br /><span class="LC_success">'.&mt('Completed.').'</span>');
1.1       albertel 1787: 	if ($env{'form.displayedcrit'}) {
                   1788: 	    &discrit($r);
                   1789: 	} else {
                   1790: 	    &Apache::loncommunicate::menu($r);
                   1791: 	}
                   1792:     } else {
1.5       albertel 1793: 	$r->print('<p><span class="LC_error">'.&mt('Could not deliver message').'</span> '.
1.25      foxr     1794: 		  &mt('Please use the browser "Back" button and correct the recipient addresses '."($sendstatus)").'</p>');
1.1       albertel 1795:     }
                   1796: }
                   1797: 
                   1798: # ===================================================================== Handler
                   1799: 
                   1800: sub handler {
                   1801:     my $r=shift;
                   1802: 
                   1803: # ----------------------------------------------------------- Set document type
                   1804:     
                   1805:     &Apache::loncommon::content_type($r,'text/html');
                   1806:     $r->send_http_header;
                   1807:     
                   1808:     return OK if $r->header_only;
                   1809:     
                   1810: # --------------------------- Get query string for limited number of parameters
                   1811:     &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'},
                   1812:         ['display','replyto','forward','markread','markdel','markunread',
                   1813:          'sendreply','compose','sendmail','critical','recname','recdom',
                   1814:          'recordftf','sortedby','block','folder','startdis','interdis',
1.23      www      1815: 	 'showcommentbaseurl','dismode','group','subject','text']);
1.1       albertel 1816:     $sqs='&sortedby='.$env{'form.sortedby'};
                   1817: 
                   1818: # ------------------------------------------------------ They checked for email
                   1819:     unless ($env{'form.block'}) {
                   1820:         &Apache::lonnet::put('email_status',{'recnewemail'=>0});
                   1821:     }
                   1822: 
                   1823: # ----------------------------------------------------------------- Breadcrumbs
                   1824: 
                   1825:     &Apache::lonhtmlcommon::clear_breadcrumbs();
                   1826:     &Apache::lonhtmlcommon::add_breadcrumb
                   1827:         ({href=>"/adm/communicate",
                   1828:           text=>"Communication/Messages",
                   1829:           faq=>12,bug=>'Communication Tools',});
                   1830: 
                   1831: # ------------------------------------------------------------------ Get Folder
                   1832: 
                   1833:     my $folder=$env{'form.folder'};
                   1834:     unless ($folder) { 
                   1835: 	$folder=''; 
                   1836:     } else {
                   1837: 	$sqs.='&folder='.&Apache::lonnet::escape($folder);
                   1838:     }
                   1839: # ------------------------------------------------------------ Get Display Mode
                   1840: 
                   1841:     my $dismode=$env{'form.dismode'};
                   1842:     unless ($dismode) { 
                   1843: 	$dismode=''; 
                   1844:     } else {
                   1845: 	$sqs.='&dismode='.&Apache::lonnet::escape($dismode);
                   1846:     }
                   1847: 
                   1848: # --------------------------------------------------------------------- Display
                   1849: 
                   1850:     $startdis=$env{'form.startdis'};
                   1851:     $startdis--;
                   1852:     unless ($startdis) { $startdis=0; }
                   1853: 
                   1854:     $interdis=$env{'form.interdis'};
                   1855:     unless ($interdis) { $interdis=20; }
                   1856:     $sqs.='&interdis='.$interdis;
                   1857: 
                   1858:     if ($env{'form.firstview'}) {
                   1859: 	$startdis=0;
                   1860:     }
                   1861:     if ($env{'form.lastview'}) {
                   1862: 	$startdis=-1;
                   1863:     }
                   1864:     if ($env{'form.prevview'}) {
                   1865: 	$startdis--;
                   1866:     }
                   1867:     if ($env{'form.nextview'}) {
                   1868: 	$startdis++;
                   1869:     }
                   1870:     my $postedstartdis=$startdis+1;
                   1871:     $sqs.='&startdis='.$postedstartdis;
                   1872: 
                   1873: # --------------------------------------------------------------- Render Output
                   1874: 
                   1875:     if ($env{'form.display'}) {
                   1876: 	&displaymessage($r,$env{'form.display'},$folder);
                   1877:     } elsif ($env{'form.replyto'}) {
                   1878: 	&compout($r,'',$env{'form.replyto'},undef,undef,$folder,$dismode);
                   1879:     } elsif ($env{'form.confirm'}) {
                   1880: 	&printheader($r,'','Confirmed Receipt');
                   1881: 	foreach (keys %env) {
                   1882: 	    if ($_=~/^form\.rec\_(.*)$/) {
                   1883: 		$r->print('<b>'.&mt('Confirming Receipt').':</b> '.
                   1884: 			  &Apache::lonmsg::user_crit_received($1).'<br>');
                   1885: 	    }
                   1886: 	    if ($_=~/^form\.reprec\_(.*)$/) {
                   1887: 		my $msgid=$1;
                   1888: 		$r->print('<b>'.&mt('Confirming Receipt').':</b> '.
                   1889: 			  &Apache::lonmsg::user_crit_received($msgid).'<br>');
                   1890: 		&compout($r,'','','',$msgid);
                   1891: 	    }
                   1892: 	}
                   1893: 	&discrit($r);
                   1894:     } elsif ($env{'form.critical'}) {
                   1895: 	&printheader($r,'','Displaying Critical Messages');
                   1896: 	&discrit($r);
                   1897:     } elsif ($env{'form.forward'}) {
                   1898: 	&compout($r,$env{'form.forward'},undef,undef,undef,$folder);
                   1899:     } elsif ($env{'form.markdel'}) {
                   1900: 	&printheader($r,'','Deleted Message');
1.9       albertel 1901: 	my ($result,$msg) = 
                   1902: 	    &statuschange($env{'form.markdel'},'deleted',$folder);
                   1903: 	if (!$result) {
1.10      albertel 1904: 	    $r->print('<p class="LC_error">'.
                   1905: 		      &mt('Failed to delete the message.').'</p>'.
1.9       albertel 1906: 		      '<p class="LC_error">'.$msg."</p>\n");
                   1907: 	}
1.1       albertel 1908: 	&Apache::loncommunicate::menu($r);
                   1909: 	&disall($r,($folder?$folder:$dismode));
                   1910:     } elsif ($env{'form.markedmove'}) {
1.9       albertel 1911: 	my ($total,$failed,@failed_msg)=(0,0);
                   1912: 	foreach my $key (keys(%env)) {
                   1913: 	    if ($key=~/^form\.delmark_(.*)$/) {
                   1914: 		my ($result,$msg) =
                   1915: 		    &movemsg(&Apache::lonnet::unescape($1),$folder,
                   1916: 			     $env{'form.movetofolder'});
                   1917: 		if ($result) {
                   1918: 		    $total++;
                   1919: 		} else {
                   1920: 		    $failed++;
                   1921: 		    push(@failed_msg,$msg);
                   1922: 		}
1.1       albertel 1923: 	    }
                   1924: 	}
                   1925: 	&printheader($r,'','Moved Messages');
1.9       albertel 1926: 	if ($failed) {
                   1927: 	    $r->print('<p class="LC_error">
1.10      albertel 1928:                           '.&mt('Failed to move [_1] message(s)',$failed).
                   1929: 		      '</p>');
1.9       albertel 1930: 	    $r->print('<p class="LC_error">'.
                   1931: 		      join("</p>\n<p class=\"LC_error\">",@failed_msg).
                   1932: 		      "</p>\n");
                   1933: 	}
1.10      albertel 1934: 	$r->print(&mt('Moved [_1] message(s)',$total).'<p>');
1.1       albertel 1935: 	&Apache::loncommunicate::menu($r);
                   1936: 	&disall($r,($folder?$folder:$dismode));
                   1937:     } elsif ($env{'form.markeddel'}) {
1.9       albertel 1938: 	my ($total,$failed,@failed_msg)=(0,0);
                   1939: 	foreach my $key (keys(%env)) {
                   1940: 	    if ($key=~/^form\.delmark_(.*)$/) {
                   1941: 		my ($result,$msg) = 
                   1942: 		    &statuschange(&Apache::lonnet::unescape($1),'deleted',
                   1943: 				  $folder);
                   1944: 		if ($result) {
                   1945: 		    $total++;
                   1946: 		} else {
                   1947: 		    $failed++;
                   1948: 		    push(@failed_msg,$msg);
                   1949: 		}
1.1       albertel 1950: 	    }
                   1951: 	}
                   1952: 	&printheader($r,'','Deleted Messages');
1.9       albertel 1953: 	if ($failed) {
                   1954: 	    $r->print('<p class="LC_error">
1.10      albertel 1955:                           '.&mt('Failed to delete [_1] message(s)',$failed).
                   1956: 		      '</p>');
1.9       albertel 1957: 	    $r->print('<p class="LC_error">'.
                   1958: 		      join("</p>\n<p class=\"LC_error\">",@failed_msg).
                   1959: 		      "</p>\n");
                   1960: 	}
1.10      albertel 1961: 	$r->print(&mt('Deleted [_1] message(s)',$total).'<p>');
1.1       albertel 1962: 	&Apache::loncommunicate::menu($r);
                   1963: 	&disall($r,($folder?$folder:$dismode));
                   1964:     } elsif ($env{'form.markunread'}) {
                   1965: 	&printheader($r,'','Marked Message as Unread');
                   1966: 	&statuschange($env{'form.markunread'},'new');
                   1967: 	&Apache::loncommunicate::menu($r);
                   1968: 	&disall($r,($folder?$folder:$dismode));
                   1969:     } elsif ($env{'form.compose'}) {
                   1970: 	&compout($r,'','',$env{'form.compose'});
                   1971:     } elsif ($env{'form.recordftf'}) {
                   1972: 	&facetoface($r,$env{'form.recordftf'});
                   1973:     } elsif ($env{'form.block'}) {
                   1974:         &examblock($r,$env{'form.block'});
                   1975:     } elsif ($env{'form.sendmail'}) {
                   1976: 	&sendoffmail($r,$folder);
                   1977: 	if ($env{'form.storebasecomment'}) {
                   1978: 	    &storecomment($r);
                   1979: 	}
                   1980: 	if (($env{'form.rsspost'}) && ($env{'request.course.id'})) {
                   1981: 	    &Apache::lonrss::addentry($env{'course.'.$env{'request.course.id'}.'.num'},
                   1982: 				      $env{'course.'.$env{'request.course.id'}.'.domain'},
                   1983: 				      'Course_Announcements',
                   1984: 				      $env{'form.subject'},
                   1985: 				      $env{'form.message'},'/adm/communicate','public');
                   1986: 	}
                   1987: 	&disall($r,($folder?$folder:$dismode));
                   1988:     } elsif ($env{'form.newfolder'}) {
                   1989: 	&printheader($r,'','New Folder');
                   1990: 	&makefolder($env{'form.newfolder'});
                   1991: 	&Apache::loncommunicate::menu($r);
                   1992: 	&disall($r,$env{'form.newfolder'});
                   1993:     } elsif ($env{'form.showcommentbaseurl'}) {
                   1994: 	&storedcommentlisting($r);
                   1995:     } else {
                   1996: 	&printheader($r,'','Display All Messages');
                   1997: 	&Apache::loncommunicate::menu($r); 
                   1998: 	&disall($r,($folder?$folder:$dismode));
                   1999:     }
                   2000:     $r->print(&Apache::loncommon::end_page());
                   2001:     return OK;
                   2002: }
                   2003: # ================================================= Main program, reset counter
                   2004: 
                   2005: =pod
                   2006: 
                   2007: =back
                   2008: 
                   2009: =cut
                   2010: 
                   2011: 1; 
                   2012: 
                   2013: __END__
                   2014: 
                   2015: 
                   2016: 
                   2017: 
                   2018: 
                   2019: 
                   2020: 

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