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

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

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