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

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

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