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

1.1       albertel    1: # The LearningOnline Network with CAPA
                      2: # Routines for messaging display
                      3: #
1.29    ! www         4: # $Id: lonmsgdisplay.pm,v 1.28 2006/05/23 21:42:09 foxr Exp $
1.1       albertel    5: #
                      6: # Copyright Michigan State University Board of Trustees
                      7: #
                      8: # This file is part of the LearningOnline Network with CAPA (LON-CAPA).
                      9: #
                     10: # LON-CAPA is free software; you can redistribute it and/or modify
                     11: # it under the terms of the GNU General Public License as published by
                     12: # the Free Software Foundation; either version 2 of the License, or
                     13: # (at your option) any later version.
                     14: #
                     15: # LON-CAPA is distributed in the hope that it will be useful,
                     16: # but WITHOUT ANY WARRANTY; without even the implied warranty of
                     17: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
                     18: # GNU General Public License for more details.
                     19: #
                     20: # You should have received a copy of the GNU General Public License
                     21: # along with LON-CAPA; if not, write to the Free Software
                     22: # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
                     23: #
                     24: # /home/httpd/html/adm/gpl.txt
                     25: #
                     26: # http://www.lon-capa.org/
                     27: #
                     28: 
                     29: 
                     30: package Apache::lonmsgdisplay;
                     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>".
                    323: 	    '<a href="/adm/roles">'.&mt('Select a course').'</a><br />'.
                    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',
                    456:                                        'co' => 'Course',
                    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 
                    574:           (document.forms.disall.elements[i].name.indexOf('delmark_')==0) {
                    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") {
                    625:         $r->print('<a href = "?sortedby=course'.$fsqs.'">'.&mt('Course').'</a>');
                    626:     } else {
                    627:         $r->print('<a href = "?sortedby=revcourse'.$fsqs.'">'.&mt('Course').'</a>');
                    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 '') {
                    979: 	if ($target ne 'tex') { 
                    980: 	    $r->print("<p><b>".&mt("No notes, face-to-face discussion records, critical messages, or broadcast messages in this course.")."</b></p>");
                    981: 	} else {
                    982: 	    $r->print('\textbf{'.&mt("No notes, face-to-face discussion records, critical messages or broadcast messages in this course.").'}\\\\');
                    983: 	}
                    984:     } else {
                    985:        $r->print($result);
                    986:     }
                    987: }
                    988: 
                    989: sub general_message {
                    990:     my ($crsmsgid) = @_;
                    991:     my %general_content;
                    992:     if ($crsmsgid) { 
                    993:         my %course_content = &Apache::lonnet::get('nohist_email',[$crsmsgid],
                    994:                            $env{'course.'.$env{'request.course.id'}.'.domain'},
                    995:                            $env{'course.'.$env{'request.course.id'}.'.num'});
                    996:         %general_content = &Apache::lonmsg::unpackagemsg($course_content{$crsmsgid});
                    997:     }
                    998:     return $general_content{'message'};
                    999: }
                   1000: 
                   1001: # ---------------------------------------------------------------- Face to face
                   1002: 
                   1003: sub facetoface {
                   1004:     my ($r,$stage)=@_;
                   1005:     if (!&Apache::lonnet::allowed('srm',$env{'request.course.id'})
                   1006: 	&& ! &Apache::lonnet::allowed('srm',$env{'request.course.id'}.
                   1007: 				      '/'.$env{'request.course.sec'})) {
                   1008: 	$r->print('Not allowed');
                   1009: 	return;
                   1010:     }
                   1011:     &printheader($r,
                   1012: 		 '/adm/email?recordftf=query',
                   1013: 		 "User Notes, Face-to-Face, Critical Messages, Broadcast Messages");
                   1014: # from query string
                   1015: 
                   1016:     if ($env{'form.recname'}) { $env{'form.recuname'}=$env{'form.recname'}; }
                   1017:     if ($env{'form.recdom'}) { $env{'form.recdomain'}=$env{'form.recdom'}; }
                   1018: 
                   1019:     my $defdom=$env{'user.domain'};
                   1020: # already filled in
                   1021:     if ($env{'form.recdomain'}) { $defdom=$env{'form.recdomain'}; }
                   1022: # generate output
                   1023:     my $domform = &Apache::loncommon::select_dom_form($defdom,'recdomain');
                   1024:     my $stdbrws = &Apache::loncommon::selectstudent_link
                   1025: 	('stdselect','recuname','recdomain');
                   1026:     my %lt=&Apache::lonlocal::texthash('user' => 'Username',
                   1027: 				       'dom' => 'Domain',
                   1028: 				       'head' => 'User Notes, Records of Face-To-Face Discussions, Critical Messages, and Broadcast Messages in Course',
                   1029: 				       'subm' => 'Retrieve discussion and message records',
                   1030: 				       'newr' => 'New Record (record is visible to course faculty and staff)',
                   1031: 				       'post' => 'Post this Record');
                   1032:     $r->print(<<"ENDTREC");
                   1033: <h3>$lt{'head'}</h3>
                   1034: <form method="post" action="/adm/email" name="stdselect">
                   1035: <input type="hidden" name="recordftf" value="retrieve" />
                   1036: <table>
                   1037: <tr><td>$lt{'user'}:</td><td><input type="text" size="12" name="recuname" value="$env{'form.recuname'}" /></td>
                   1038: <td rowspan="2">
                   1039: $stdbrws
                   1040: <input type="submit" value="$lt{'subm'}" /></td>
                   1041: </tr>
                   1042: <tr><td>$lt{'dom'}:</td>
                   1043: <td>$domform</td></tr>
                   1044: </table>
                   1045: </form>
                   1046: ENDTREC
                   1047:     if (($stage ne 'query') &&
                   1048:         ($env{'form.recdomain'}) && ($env{'form.recuname'})) {
                   1049:         chomp($env{'form.newrecord'});
                   1050:         if ($env{'form.newrecord'}) {
                   1051:            my $recordtxt = $env{'form.newrecord'};
                   1052:            &Apache::lonmsg::user_normal_msg_raw(
                   1053:             $env{'course.'.$env{'request.course.id'}.'.num'},
                   1054:             $env{'course.'.$env{'request.course.id'}.'.domain'},
                   1055:             &mt('Record').
                   1056: 	     ' ['.$env{'form.recuname'}.':'.$env{'form.recdomain'}.']',
                   1057: 	    $recordtxt);
                   1058:         }
                   1059:         $r->print('<h3>'.&Apache::loncommon::plainname($env{'form.recuname'},
                   1060: 				     $env{'form.recdomain'}).'</h3>');
                   1061:         &disfacetoface($r,$env{'form.recuname'},$env{'form.recdomain'});
                   1062: 	$r->print(<<ENDRHEAD);
                   1063: <form method="post" action="/adm/email">
                   1064: <input name="recdomain" value="$env{'form.recdomain'}" type="hidden" />
                   1065: <input name="recuname" value="$env{'form.recuname'}" type="hidden" />
                   1066: ENDRHEAD
                   1067:         $r->print(<<ENDBFORM);
                   1068: <hr />$lt{'newr'}<br />
                   1069: <textarea name="newrecord" cols="80" rows="10" wrap="hard"></textarea>
                   1070: <br />
                   1071: <input type="hidden" name="recordftf" value="post" />
                   1072: <input type="submit" value="$lt{'post'}" />
                   1073: </form>
                   1074: ENDBFORM
                   1075:     }
                   1076: }
                   1077: 
                   1078: # ----------------------------------------------------------- Blocking during exams
                   1079: 
                   1080: sub examblock {
                   1081:     my ($r,$action) = @_;
                   1082:     unless ($env{'request.course.id'}) { return;}
                   1083:     if (!&Apache::lonnet::allowed('srm',$env{'request.course.id'})
                   1084: 	&& ! &Apache::lonnet::allowed('srm',$env{'request.course.id'}.
                   1085: 				      '/'.$env{'request.course.sec'})) {
                   1086: 	$r->print('Not allowed');
                   1087: 	return;
                   1088:     }
                   1089:     my %lt=&Apache::lonlocal::texthash(
                   1090:             'comb' => 'Communication Blocking',
                   1091:             'cbds' => 'Communication blocking during scheduled exams',
                   1092:             'desc' => 'You can use communication blocking to prevent students enrolled in this course from displaying LON-CAPA messages sent by other students during an online exam. As blocking of communication could potentially interrupt legitimate communication between students who are also both enrolled in a different LON-CAPA course, please be careful that you select the correct start and end times for your scheduled exam when setting or modifying these parameters.',
                   1093:              'mecb' => 'Modify existing communication blocking periods',
                   1094:              'ncbc' => 'No communication blocks currently stored'
                   1095:     );
                   1096: 
                   1097:     my %ltext = &Apache::lonlocal::texthash(
                   1098:             'dura' => 'Duration',
                   1099:             'setb' => 'Set by',
                   1100:             'even' => 'Event',
                   1101:             'actn' => 'Action',
                   1102:             'star' => 'Start',
                   1103:             'endd' => 'End'
                   1104:     );
                   1105: 
                   1106:     &printheader($r,'/adm/email?block=display',$lt{'comb'});
                   1107:     $r->print('<h3>'.$lt{'cbds'}.'</h3>');
                   1108: 
                   1109:     if ($action eq 'store') {
                   1110:         &blockstore($r);
                   1111:     }
                   1112: 
                   1113:     $r->print($lt{'desc'}.'<br /><br />
                   1114:                <form name="blockform" method="post" action="/adm/email?block=store">
                   1115:              ');
                   1116: 
                   1117:     $r->print('<h4>'.$lt{'mecb'}.'</h4>');
                   1118:     my %records = ();
                   1119:     my $blockcount = 0;
                   1120:     my $parmcount = 0;
                   1121:     &get_blockdates(\%records,\$blockcount);
                   1122:     if ($blockcount > 0) {
                   1123:         $parmcount = &display_blocker_status($r,\%records,\%ltext);
                   1124:     } else {
                   1125:         $r->print($lt{'ncbc'}.'<br /><br />');
                   1126:     }
                   1127:     &display_addblocker_table($r,$parmcount,\%ltext);
                   1128:     my $end_page=&Apache::loncommon::end_page();
                   1129:     $r->print(<<"END");
                   1130: <br />
                   1131: <input type="hidden" name="blocktotal" value="$blockcount" />
                   1132: <input type ="submit" value="Save Changes" />
                   1133: </form>
                   1134: $end_page
                   1135: END
                   1136:     return;
                   1137: }
                   1138: 
                   1139: sub blockstore {
                   1140:     my $r = shift;
                   1141:     my %lt=&Apache::lonlocal::texthash(
                   1142:             'tfcm' => 'The following changes were made',
                   1143:             'cbps' => 'communication blocking period(s)',
                   1144:             'werm' => 'was/were removed',
                   1145:             'wemo' => 'was/were modified',
                   1146:             'wead' => 'was/were added',
                   1147:             'ncwm' => 'No changes were made.' 
                   1148:     );
                   1149:     my %adds = ();
                   1150:     my %removals = ();
                   1151:     my %cancels = ();
                   1152:     my $modtotal = 0;
                   1153:     my $canceltotal = 0;
                   1154:     my $addtotal = 0;
                   1155:     my %blocking = ();
                   1156:     $r->print('<h3>'.$lt{'head'}.'</h3>');
                   1157:     foreach (keys %env) {
                   1158:         if ($_ =~ m/^form\.modify_(\w+)$/) {
                   1159:             $adds{$1} = $1;
                   1160:             $removals{$1} = $1;
                   1161:             $modtotal ++;
                   1162:         } elsif ($_ =~ m/^form\.cancel_(\d+)$/) {
                   1163:             $cancels{$1} = $1;
                   1164:             unless ( defined($removals{$1}) ) {
                   1165:                 $removals{$1} = $1;
                   1166:                 $canceltotal ++;
                   1167:             }
                   1168:         } elsif ($_ =~ m/^form\.add_(\d+)$/) {
                   1169:             $adds{$1} = $1;
                   1170:             $addtotal ++;
                   1171:         }
                   1172:     }
                   1173: 
                   1174:     foreach (keys %removals) {
                   1175:         my $hashkey = $env{'form.key_'.$_};
                   1176:         &Apache::lonnet::del('comm_block',["$hashkey"],
                   1177:                          $env{'course.'.$env{'request.course.id'}.'.domain'},
                   1178:                          $env{'course.'.$env{'request.course.id'}.'.num'}
                   1179:                          );
                   1180:     }
                   1181:     foreach (keys %adds) {
                   1182:         unless ( defined($cancels{$_}) ) {
                   1183:             my ($newstart,$newend) = &get_dates_from_form($_);
                   1184:             my $newkey = $newstart.'____'.$newend;
1.14      albertel 1185:             $blocking{$newkey} = $env{'user.name'}.':'.$env{'user.domain'}.':'.$env{'form.title_'.$_};
1.1       albertel 1186:         }
                   1187:     }
                   1188:     if ($addtotal + $modtotal > 0) {
                   1189:         &Apache::lonnet::put('comm_block',\%blocking,
                   1190:                      $env{'course.'.$env{'request.course.id'}.'.domain'},
                   1191:                      $env{'course.'.$env{'request.course.id'}.'.num'}
                   1192:                      );
                   1193:     }
                   1194:     my $chgestotal = $canceltotal + $modtotal + $addtotal;
                   1195:     if ($chgestotal > 0) {
                   1196:         $r->print($lt{'tfcm'}.'<ul>');
                   1197:         if ($canceltotal > 0) {
                   1198:             $r->print('<li>'.$canceltotal.' '.$lt{'cbps'},' '.$lt{'werm'}.'</li>');
                   1199:         }
                   1200:         if ($modtotal > 0) {
                   1201:             $r->print('<li>'.$modtotal.' '.$lt{'cbps'},' '.$lt{'wemo'}.'</li>');
                   1202:         }
                   1203:         if ($addtotal > 0) {
                   1204:             $r->print('<li>'.$addtotal.' '.$lt{'cbps'},' '.$lt{'wead'}.'</li>');
                   1205:         }
                   1206:         $r->print('</ul>');
                   1207:     } else {
                   1208:         $r->print($lt{'ncwm'});
                   1209:     }
                   1210:     $r->print('<br />');
                   1211:     return;
                   1212: }
                   1213: 
                   1214: sub get_dates_from_form {
                   1215:     my $item = shift;
                   1216:     my $startdate = &Apache::lonhtmlcommon::get_date_from_form('startdate_'.$item);
                   1217:     my $enddate   = &Apache::lonhtmlcommon::get_date_from_form('enddate_'.$item);
                   1218:     return ($startdate,$enddate);
                   1219: }
                   1220: 
                   1221: sub get_blockdates {
                   1222:     my ($records,$blockcount) = @_;
                   1223:     $$blockcount = 0;
                   1224:     %{$records} = &Apache::lonnet::dump('comm_block',
                   1225:                          $env{'course.'.$env{'request.course.id'}.'.domain'},
                   1226:                          $env{'course.'.$env{'request.course.id'}.'.num'}
                   1227:                          );
1.15      albertel 1228:     $$blockcount = keys(%{$records});
                   1229: 
                   1230:     if ((keys(%{$records}))[0] =~ /^error: 2 /) {
                   1231: 	$records = {};
                   1232: 	$$blockcount = 0;
1.1       albertel 1233:     }
                   1234: }
                   1235: 
                   1236: sub display_blocker_status {
                   1237:     my ($r,$records,$ltext) = @_;
                   1238:     my $parmcount = 0;
1.16      albertel 1239:   
1.1       albertel 1240:     my %lt = &Apache::lonlocal::texthash(
                   1241:         'modi' => 'Modify',
                   1242:         'canc' => 'Cancel',
                   1243:     );
1.18      albertel 1244:     $r->print(&Apache::loncommon::start_data_table());
1.1       albertel 1245:     $r->print(<<"END");
1.16      albertel 1246:   <tr>
                   1247:     <th>$$ltext{'dura'}</th>
                   1248:     <th>$$ltext{'setb'}</th>
                   1249:     <th>$$ltext{'even'}</th>
                   1250:     <th>$$ltext{'actn'}?</th>
                   1251:   </tr>
1.1       albertel 1252: END
1.18      albertel 1253:     foreach my $record (sort(keys(%{$records}))) {
1.1       albertel 1254:         my $onchange = 'onFocus="javascript:window.document.forms['.
                   1255:                        "'blockform'].elements['modify_".$parmcount."'].".
                   1256:                        'checked=true;"';
1.18      albertel 1257:         my ($start,$end) = split(/____/,$record);
1.1       albertel 1258:         my $startform = &Apache::lonhtmlcommon::date_setter('blockform','startdate_'.$parmcount,$start,$onchange);
                   1259:         my $endform = &Apache::lonhtmlcommon::date_setter('blockform','enddate_'.$parmcount,$end,$onchange);
1.15      albertel 1260: 	
1.18      albertel 1261: 	my ($setuname,$setudom,$title) = 
                   1262: 	    &parse_block_record($$records{$record});
1.21      albertel 1263: 	$title = &HTML::Entities::encode($title,'"<>&');
1.1       albertel 1264:         my $settername = &Apache::loncommon::plainname($setuname,$setudom);
1.18      albertel 1265:         $r->print(&Apache::loncommon::start_data_table_row());
1.1       albertel 1266:         $r->print(<<"END");
                   1267:         <td>$$ltext{'star'}:&nbsp;$startform<br/>$$ltext{'endd'}:&nbsp;&nbsp;$endform</td>
                   1268:         <td>$settername</td>
1.18      albertel 1269:         <td><input type="text" name="title_$parmcount" size="15" value="$title" /><input type="hidden" name="key_$parmcount" value="$record" /></td>
1.1       albertel 1270:         <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>
                   1271: END
1.18      albertel 1272:         $r->print(&Apache::loncommon::end_data_table_row());
                   1273:         $parmcount++;
1.1       albertel 1274:     }
                   1275:     $r->print(<<"END");
                   1276: </table>
                   1277: <br />
                   1278: <br />
                   1279: END
                   1280:     return $parmcount;
                   1281: }
                   1282: 
1.15      albertel 1283: sub parse_block_record {
                   1284:     my ($record) = @_;
                   1285:     my ($setuname,$setudom,$title);
                   1286:     my @data = split(/:/,$record,3);
                   1287:     if (scalar(@data) eq 2) {
                   1288: 	$title = $data[1];
                   1289: 	($setuname,$setudom) = split(/@/,$data[0]);
                   1290:     } else {
                   1291: 	($setuname,$setudom,$title) = @data;
                   1292:     }
                   1293:     return ($setuname,$setudom,$title);
                   1294: }
                   1295: 
1.1       albertel 1296: sub display_addblocker_table {
                   1297:     my ($r,$parmcount,$ltext) = @_;
                   1298:     my $start = time;
                   1299:     my $end = $start + (60 * 60 * 2); #Default is an exam of 2 hours duration.
                   1300:     my $onchange = 'onFocus="javascript:window.document.forms['.
                   1301:                    "'blockform'].elements['add_".$parmcount."'].".
                   1302:                    'checked=true;"';
                   1303:     my $startform = &Apache::lonhtmlcommon::date_setter('blockform','startdate_'.$parmcount,$start,$onchange);
                   1304:     my $endform = &Apache::lonhtmlcommon::date_setter('blockform','enddate_'.$parmcount,$end,$onchange);
                   1305:     my %lt = &Apache::lonlocal::texthash(
                   1306:         'addb' => 'Add block',
                   1307:         'exam' => 'e.g., Exam 1',
                   1308:         'addn' => 'Add new communication blocking periods'
                   1309:     );
                   1310:     $r->print(<<"END");
                   1311: <h4>$lt{'addn'}</h4> 
1.18      albertel 1312: END
                   1313:     $r->print(&Apache::loncommon::start_data_table());
                   1314:     $r->print(<<"END");
1.16      albertel 1315:    <tr>
                   1316:      <th>$$ltext{'dura'}</th>
                   1317:      <th>$$ltext{'even'} $lt{'exam'}</th>
                   1318:      <th>$$ltext{'actn'}?</th>
                   1319:    </tr>
1.18      albertel 1320: END
                   1321:    $r->print(&Apache::loncommon::start_data_table_row());
                   1322:     $r->print(<<"END");
1.16      albertel 1323:      <td>$$ltext{'star'}:&nbsp;$startform<br />$$ltext{'endd'}:&nbsp;&nbsp;$endform</td>
                   1324:      <td><input type="text" name="title_$parmcount" size="15" value="" /></td>
                   1325:      <td><label>$lt{'addb'}?&nbsp;<input type="checkbox" name="add_$parmcount" value="1" /></label></td>
1.1       albertel 1326: END
1.18      albertel 1327:     $r->print(&Apache::loncommon::end_data_table_row());
                   1328:     $r->print(&Apache::loncommon::end_data_table());
1.1       albertel 1329:     return;
                   1330: }
                   1331: 
                   1332: sub blockcheck {
                   1333:     my ($setters,$startblock,$endblock) = @_;
                   1334:     # Retrieve active student roles and active course coordinator/instructor roles
1.15      albertel 1335:     my %live_courses =
                   1336: 	map { $_ => 1} &Apache::loncommon::findallcourses();
                   1337:     # FIXME should really probe for apriv, but ::allowed can only probe the 
                   1338:     #       currently active role
                   1339:     my %staff_of =
                   1340: 	map { $_ => 1} &Apache::loncommon::findallcourses(['cc','in']);
                   1341: 
                   1342:     # Retrieve blocking times and identity of blocker for active courses
                   1343:     # for students.
                   1344:     return if (!%live_courses);
                   1345: 
                   1346:     foreach my $course (keys(%live_courses)) {
1.19      albertel 1347: 	my $cdom = $env{'course.'.$course.'.domain'};
                   1348: 	my $cnum = $env{'course.'.$course.'.num'};
1.15      albertel 1349: 
                   1350: 	# if they are a staff member and are currently not playing student
                   1351: 	next if ( $staff_of{$course} 
                   1352: 		  && ($env{'request.role'} !~ m{^st\./$cdom/$cnum}));
                   1353: 
                   1354: 	$setters->{$course} = {};
                   1355: 	$setters->{$course}{'staff'} = [];
                   1356: 	$setters->{$course}{'times'} = [];
                   1357: 	my %records = &Apache::lonnet::dump('comm_block',$cdom,$cnum);
                   1358: 	foreach my $record (keys %records) {
                   1359: 	    my ($start,$end) = ($record =~ m/^(\d+)____(\d+)$/);
                   1360: 	    if ($start <= time && $end >= time) {
                   1361: 		my ($staff_name,$staff_dom,$title) = 
                   1362: 		    &parse_block_record($records{$record});
                   1363: 		push(@{$$setters{$course}{'staff'}}, [$staff_name,$staff_dom]);
                   1364: 		push(@{$$setters{$course}{'times'}}, [$start,$end]);
                   1365: 		if ( ($$startblock == 0) || ($$startblock > $1) ) {
                   1366: 		    $$startblock = $1;
                   1367: 		}
                   1368: 		if ( ($$endblock == 0) || ($$endblock < $2) ) {
                   1369: 		    $$endblock = $2;
                   1370: 		}
                   1371: 	    }
                   1372: 	}
1.1       albertel 1373:     }
                   1374: }
                   1375: 
                   1376: sub build_block_table {
                   1377:     my ($r,$startblock,$endblock,$setters) = @_;
                   1378:     my %lt = &Apache::lonlocal::texthash(
                   1379:         'cacb' => 'Currently active communication blocks',
                   1380:         'cour' => 'Course',
                   1381:         'dura' => 'Duration',
                   1382:         'blse' => 'Block set by'
1.18      albertel 1383:     );
                   1384:     $r->print(<<"END");
                   1385: <br /><br />$lt{'cacb'}:<br /><br />
                   1386: END
                   1387:     $r->print(&Apache::loncommon::start_data_table());
1.1       albertel 1388:     $r->print(<<"END");
1.16      albertel 1389: <tr>
                   1390:  <th>$lt{'cour'}</th>
                   1391:  <th>$lt{'dura'}</th>
                   1392:  <th>$lt{'blse'}</th>
                   1393: </tr>
1.1       albertel 1394: END
1.18      albertel 1395:     foreach my $course (keys(%{$setters})) {
                   1396:         my %courseinfo=&Apache::lonnet::coursedescription($course);
                   1397:         for (my $i=0; $i<@{$$setters{$course}{staff}}; $i++) {
                   1398:             my ($uname,$udom) = @{$$setters{$course}{staff}[$i]};
1.1       albertel 1399:             my $fullname = &Apache::loncommon::plainname($uname,$udom);
1.18      albertel 1400:             my ($openblock,$closeblock) = @{$$setters{$course}{times}[$i]};
1.1       albertel 1401:             $openblock = &Apache::lonlocal::locallocaltime($openblock);
                   1402:             $closeblock= &Apache::lonlocal::locallocaltime($closeblock);
1.18      albertel 1403:             $r->print(&Apache::loncommon::start_data_table_row().
                   1404: 		      '<td>'.$courseinfo{'description'}.'</td>'.
1.1       albertel 1405:                       '<td>'.$openblock.' to '.$closeblock.'</td>'.
1.14      albertel 1406:                       '<td>'.$fullname.' ('.$uname.':'.$udom.
1.18      albertel 1407:                       ')</td>'.
                   1408: 		       &Apache::loncommon::end_data_table_row());
1.1       albertel 1409:         }
                   1410:     }
1.18      albertel 1411:     $r->print(&Apache::loncommon::end_data_table());
1.1       albertel 1412: }
                   1413: 
                   1414: # ----------------------------------------------------------- Display a message
                   1415: 
                   1416: sub displaymessage {
                   1417:     my ($r,$msgid,$folder)=@_;
                   1418:     my $suffix=&Apache::lonmsg::foldersuffix($folder);
                   1419:     my %blocked = ();
                   1420:     my %setters = ();
                   1421:     my $startblock = 0;
                   1422:     my $endblock = 0;
                   1423:     my $numblocked = 0;
                   1424: # info to generate "next" and "previous" buttons and check if message is blocked
                   1425:     &blockcheck(\%setters,\$startblock,\$endblock);
                   1426:     my @messages=&sortedmessages(\%blocked,$startblock,$endblock,\$numblocked,$folder);
                   1427:     if ( $blocked{$msgid} eq 'ON' ) {
                   1428:         &printheader($r,'/adm/email',&mt('Display a Message'));
                   1429:         $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.'));
                   1430:         &build_block_table($r,$startblock,$endblock,\%setters);
                   1431:         return;
                   1432:     }
                   1433:     &statuschange($msgid,'read',$folder);
                   1434:     my %message=&Apache::lonnet::get('nohist_email'.$suffix,[$msgid]);
                   1435:     my %content=&Apache::lonmsg::unpackagemsg($message{$msgid});
                   1436: 
                   1437:     my $counter=0;
                   1438:     $r->print('<pre>');
1.29    ! www      1439:     my $escmsgid=&escape($msgid);
1.1       albertel 1440:     foreach (@messages) {
                   1441: 	if ($_->[5] eq $escmsgid){
                   1442: 	    last;
                   1443: 	}
                   1444: 	$counter++;
                   1445:     }
                   1446:     $r->print('</pre>');
                   1447:     my $number_of_messages = scalar(@messages); #subtract 1 for last index
                   1448: # start output
1.29    ! www      1449:     &printheader($r,'/adm/email?display='.&escape($msgid),'Display a Message','',$content{'baseurl'});
1.1       albertel 1450:     my %courseinfo=&Apache::lonnet::coursedescription($content{'courseid'});
                   1451: # Functions
                   1452:     $r->print('<table border="2" width="100%"><tr bgcolor="#FFFFAA"><td>'.&mt('Functions').':</td>'.
1.29    ! www      1453: 	      '<td><a href="/adm/email?replyto='.&escape($msgid).$sqs.
1.1       albertel 1454: 	      '"><b>'.&mt('Reply').'</b></a></td>'.
1.29    ! www      1455: 	      '<td><a href="/adm/email?forward='.&escape($msgid).$sqs.
1.1       albertel 1456: 	      '"><b>'.&mt('Forward').'</b></a></td>'.
1.29    ! www      1457: 	      '<td><a href="/adm/email?markunread='.&escape($msgid).$sqs.
1.1       albertel 1458: 	      '"><b>'.&mt('Mark Unread').'</b></a></td>'.
1.29    ! www      1459: 	      '<td><a href="/adm/email?markdel='.&escape($msgid).$sqs.
1.1       albertel 1460: 	      '"><b>'.&mt('Delete').'</b></a></td>'.
                   1461: 	      '<td><a href="/adm/email?'.$sqs.
                   1462: 	      ($env{'form.dismode'} eq 'new'?'&folder=new':'').
                   1463: 	      '"><b>'.&mt('Back to Folder Display').'</b></a></td>');
                   1464:     if ($counter > 0){
                   1465: 	$r->print('<td><a href="/adm/email?display='.$messages[$counter-1]->[5].$sqs.
                   1466: 		  '"><b>'.&mt('Previous').'</b></a></td>');
                   1467:     }
                   1468:     if ($counter < $number_of_messages - 1){
                   1469: 	$r->print('<td><a href="/adm/email?display='.$messages[$counter+1]->[5].$sqs.
                   1470: 		  '"><b>'.&mt('Next').'</b></a></td>');
                   1471:     }
                   1472:     $r->print('</tr></table>');
                   1473:     if ($env{'user.adv'}) {
                   1474: 	$r->print('<table border="2" width="100%"><tr bgcolor="#FFAAAA"><td>'.&mt('Currently available actions (will open extra window)').':</td>');
                   1475: 	my $symb=&Apache::lonnet::symbread($content{'baseurl'});      
                   1476: 	if (&Apache::lonnet::allowed('vgr',$env{'request.course.id'})) {
                   1477: 		$r->print('<td><b>'.&Apache::loncommon::track_student_link(&mt('View recent activity'),$content{'sendername'},$content{'senderdomain'},'check').'</b></td>');
                   1478: 	    }
                   1479: 	if (&Apache::lonnet::allowed('opa',$env{'request.course.id'}) && $symb) {
                   1480: 	    $r->print('<td><b>'.&Apache::loncommon::pprmlink(&mt('Set/Change parameters'),$content{'sendername'},$content{'senderdomain'},$symb,'check').'</b></td>');
                   1481: 	}
                   1482: 	if (&Apache::lonnet::allowed('mgr',$env{'request.course.id'}) && $symb) {
                   1483: 	    $r->print('<td><b>'.&Apache::loncommon::pgrdlink(&mt('Set/Change grades'),$content{'sendername'},$content{'senderdomain'},$symb,'check').'</b></td>');
                   1484: 	}
                   1485: 	$r->print('</tr></table>');
                   1486:     }
                   1487:     my $tolist;
                   1488:     my @recipients = ();
                   1489:     for (my $i=0; $i<@{$content{'recuser'}}; $i++) {
                   1490:         $recipients[$i] =  &Apache::loncommon::aboutmewrapper(
                   1491:            &Apache::loncommon::plainname($content{'recuser'}[$i],
                   1492:                                       $content{'recdomain'}[$i]),
                   1493:               $content{'recuser'}[$i],$content{'recdomain'}[$i]).
                   1494:        ' ('.$content{'recuser'}[$i].' at '.$content{'recdomain'}[$i].') ';
                   1495:     }
                   1496:     $tolist = join(', ',@recipients);
                   1497:     $r->print('<br /><b>'.&mt('Subject').':</b> '.$content{'subject'}.
                   1498: 	      ($folder ne 'sent'?'<br /><b>'.&mt('From').':</b> '.
                   1499: 	      &Apache::loncommon::aboutmewrapper(
                   1500: 						 &Apache::loncommon::plainname($content{'sendername'},$content{'senderdomain'}),
                   1501: 						 $content{'sendername'},$content{'senderdomain'}).' ('.
                   1502: 	      $content{'sendername'}.' at '.
                   1503: 	      $content{'senderdomain'}.') ':'<br /><b>'.&mt('To').':</b> '.
                   1504:               $tolist).
                   1505: 	      ($content{'courseid'}?'<br /><b>'.&mt('Course').':</b> '.$courseinfo{'description'}.
                   1506: 	       ($content{'coursesec'}?' ('.&mt('Group/Section').': '.$content{'coursesec'}.')':''):'').
                   1507: 	      '<br /><b>'.&mt('Time').':</b> '.$content{'time'}.
                   1508: 	      ($content{'baseurl'}?'<br /><b>'.&mt('Refers to').':</b> <a href="'.$content{'baseurl'}.'">'.
                   1509: 	       $content{'baseurl'}.' ('.&Apache::lonnet::gettitle($content{'baseurl'}).')</a>':'').
                   1510: 	      '<p><pre>'.
                   1511: 	      &Apache::lontexconvert::msgtexconverted($content{'message'},1).
                   1512: 	      '</pre><hr />'.&displayresource(%content).'</p>');
                   1513:     return;   
                   1514: }
                   1515: 
                   1516: # =========================================================== Show the citation
                   1517: 
                   1518: sub displayresource {
                   1519:     my %content=@_;
                   1520: #
                   1521: # If the recipient is in the same course that the message was sent from and
                   1522: # has sufficient privileges, show "all details," else show citation
                   1523: #
                   1524:     if (($env{'request.course.id'} eq $content{'courseid'})
                   1525:      && (&Apache::lonnet::allowed('vgr',$content{'courseid'}))) {
                   1526: 	my $symb=&Apache::lonnet::symbread($content{'baseurl'});
                   1527: # Could not get a symb, give up
                   1528: 	unless ($symb) { return $content{'citation'}; }
                   1529: # Have a symb, can render
                   1530: 	return '<h2>'.&mt('Current attempts of student (if applicable)').'</h2>'.
                   1531: 	    &Apache::loncommon::get_previous_attempt($symb,
                   1532: 						     $content{'sendername'},
                   1533: 						     $content{'senderdomain'},
                   1534: 						     $content{'courseid'}).
                   1535: 	    '<hr /><h2>'.&mt('Current screen output (if applicable)').'</h2>'.
                   1536: 	    &Apache::loncommon::get_student_view($symb,
                   1537: 						 $content{'sendername'},
                   1538: 						 $content{'senderdomain'},
                   1539: 						 $content{'courseid'}).
                   1540: 	    '<h2>'.&mt('Correct Answer(s) (if applicable)').'</h2>'.
                   1541: 	    &Apache::loncommon::get_student_answers($symb,
                   1542: 						    $content{'sendername'},
                   1543: 						    $content{'senderdomain'},
                   1544: 						    $content{'courseid'});
                   1545:     } elsif ($env{'user.adv'}) {
                   1546: 	return $content{'citation'};
                   1547:     }
                   1548:     return '';
                   1549: }
                   1550: 
                   1551: # ================================================================== The Header
                   1552: 
                   1553: sub header {
                   1554:     my ($r,$title,$baseurl)=@_;
                   1555:     
                   1556:     my $extra = &Apache::loncommon::studentbrowser_javascript();
                   1557:     if ($baseurl) {
                   1558: 	$extra .= "<base href=\"http://$ENV{'SERVER_NAME'}/$baseurl\" />";
                   1559:     }
                   1560:     $r->print(&Apache::loncommon::start_page('Communication and Messages',
                   1561: 					     $extra));
                   1562:     $r->print(&Apache::lonhtmlcommon::breadcrumbs
1.22      albertel 1563: 	      (($title?$title:'Communication and Messages')));
1.1       albertel 1564: 
                   1565: }
                   1566: 
                   1567: # ---------------------------------------------------------------- Print header
                   1568: 
                   1569: sub printheader {
                   1570:     my ($r,$url,$desc,$title,$baseurl)=@_;
                   1571:     &Apache::lonhtmlcommon::add_breadcrumb
                   1572: 	({href=>$url,
                   1573: 	  text=>$desc});
                   1574:     &header($r,$title,$baseurl);
                   1575: }
                   1576: 
                   1577: # ------------------------------------------------------------ Store the comment
                   1578: 
                   1579: sub storecomment {
                   1580:     my ($r)=@_;
                   1581:     my $msgtxt=&Apache::lonfeedback::clear_out_html($env{'form.message'});
                   1582:     my $cleanmsgtxt='';
                   1583:     foreach (split(/[\n\r]/,$msgtxt)) {
                   1584: 	unless ($_=~/^\s*(\>|\&gt\;)/) {
                   1585: 	    $cleanmsgtxt.=$_."\n";
                   1586: 	}
                   1587:     }
1.29    ! www      1588:     my $key=&escape($env{'form.baseurl'}).'___'.time;
1.1       albertel 1589:     &Apache::lonnet::put('nohist_stored_comments',{ $key => $cleanmsgtxt });
                   1590: }
                   1591: 
                   1592: sub storedcommentlisting {
                   1593:     my ($r)=@_;
                   1594:     my %msgs=&Apache::lonnet::dump('nohist_stored_comments',undef,undef,
1.29    ! www      1595:        '^'.&escape(&escape($env{'form.showcommentbaseurl'})));
1.1       albertel 1596:     $r->print(&Apache::loncommon::start_page('Stored Comment Listing',undef,
                   1597: 					     {'onlybody' => 1}));
                   1598:     if ((keys %msgs)[0]=~/^error\:/) {
                   1599: 	$r->print(&mt('No stored comments yet.'));
                   1600:     } else {
                   1601: 	my $found=0;
                   1602: 	foreach (sort keys %msgs) {
                   1603: 	    $r->print("\n".$msgs{$_}."<hr />");
                   1604: 	    $found=1;
                   1605: 	}
                   1606: 	unless ($found) {
                   1607: 	    $r->print(&mt('No stored comments yet for this resource.'));
                   1608: 	}
                   1609:     }
                   1610: }
                   1611: 
                   1612: # ---------------------------------------------------------------- Send an email
                   1613: 
                   1614: sub sendoffmail {
                   1615:     my ($r,$folder)=@_;
                   1616:     my $suffix=&Apache::lonmsg::foldersuffix($folder);
                   1617:     my $sendstatus='';
                   1618:     my %specialmsg_status;
                   1619:     my $numspecial = 0;
                   1620:     if ($env{'form.send'}) {
                   1621: 	&printheader($r,'','Messages being sent.');
                   1622: 	$r->rflush();
                   1623: 	my %content=();
                   1624: 	undef %content;
                   1625: 	if ($env{'form.forwid'}) {
                   1626: 	    my $msgid=$env{'form.forwid'};
                   1627: 	    my %message=&Apache::lonnet::get('nohist_email'.$suffix,[$msgid]);
                   1628: 	    %content=&Apache::lonmsg::unpackagemsg($message{$msgid},1);
                   1629: 	    &statuschange($msgid,'forwarded',$folder);
                   1630: 	    $env{'form.message'}.="\n\n-- Forwarded message --\n\n".
                   1631: 		$content{'message'};
                   1632: 	}
                   1633: 	if ($env{'form.replyid'}) {
                   1634: 	    my $msgid=$env{'form.replyid'};
                   1635: 	    my %message=&Apache::lonnet::get('nohist_email'.$suffix,[$msgid]);
                   1636: 	    %content=&Apache::lonmsg::unpackagemsg($message{$msgid},1);
                   1637: 	    &statuschange($msgid,'replied',$folder);
                   1638: 	}
1.13      albertel 1639: 
1.25      foxr     1640: 	my $to = $env{'form.selectedusers.forminput'};
                   1641: 	my $mode = $env{'form.sendmode'};
                   1642: 
1.13      albertel 1643: 	my %toaddr;
1.25      foxr     1644: 	if ($to) {
                   1645: 	    foreach my $dest (@$to) {
1.27      albertel 1646: 		my ($user,$domain) = split(/:/, $dest);
1.25      foxr     1647: 		if (($user ne '') && ($domain ne '')) {
                   1648: 		    my $address = $user.":".$domain; # How the code below expects it.
                   1649: 		    $toaddr{$address} = '';
                   1650: 		}
                   1651: 	    }
                   1652: 	}
                   1653: 
1.1       albertel 1654: 	if ($env{'form.sendmode'} eq 'group') {
1.25      foxr     1655: 	     foreach my $address (keys(%env)) {
                   1656: 	 	if ($address=~/^form\.send\_to\_\&\&\&[^\&]*\&\&\&\_(.+)$/) {
                   1657: 	 	    $toaddr{$1}='';
                   1658: 	 	}
1.1       albertel 1659: 	    }
                   1660: 	} elsif ($env{'form.sendmode'} eq 'upload') {
1.13      albertel 1661: 	    foreach my $line (split(/[\n\r\f]+/,$env{'form.upfile'})) {
                   1662: 		my ($rec,$txt)=split(/\s*\:\s*/,$line);
1.1       albertel 1663: 		if ($txt) {
                   1664: 		    $rec=~s/\@/\:/;
                   1665: 		    $toaddr{$rec}.=$txt."\n";
                   1666: 		}
                   1667: 	    }
                   1668: 	} else {
1.25      foxr     1669: 	    if (($env{'form.recuname'} ne '') && ($env{'form.recdomain'} ne '')) {
                   1670: 		$toaddr{$env{'form.recuname'}.':'.$env{'form.recdomain'}}='';
                   1671: 	    }
1.1       albertel 1672: 	}
                   1673: 	if ($env{'form.additionalrec'}) {
                   1674: 	    foreach (split(/\,/,$env{'form.additionalrec'})) {
                   1675: 		my ($auname,$audom)=split(/\@/,$_);
1.25      foxr     1676: 		if (($auname ne "") && ($audom ne "")) {
                   1677: 		    $toaddr{$auname.':'.$audom}='';
                   1678: 		}
1.1       albertel 1679: 	    }
                   1680: 	}
                   1681: 
                   1682:         my $savemsg;
                   1683:         my $msgtype;
                   1684:         my %sentmessage;
1.7       albertel 1685:         my $msgsubj=&Apache::lonfeedback::clear_out_html($env{'form.subject'},
                   1686: 							 undef,1);
1.1       albertel 1687:         if ((($env{'form.critmsg'}) || ($env{'form.sendbck'})) &&
                   1688:             (&Apache::lonnet::allowed('srm',$env{'request.course.id'})
                   1689: 	     || &Apache::lonnet::allowed('srm',$env{'request.course.id'}.
                   1690: 					 '/'.$env{'request.course.sec'})
                   1691: 	     )) {
                   1692:             $savemsg=&Apache::lonfeedback::clear_out_html($env{'form.message'},1);
                   1693:             $msgtype = 'critical';
                   1694:         } else {
                   1695:             $savemsg=&Apache::lonfeedback::clear_out_html($env{'form.message'});
                   1696:         }
                   1697: 	
1.13      albertel 1698: 	foreach my $address (sort(keys(%toaddr))) {
                   1699: 	    my ($recuname,$recdomain)=split(/\:/,$address);
1.1       albertel 1700:             my $msgtxt = $savemsg;
1.13      albertel 1701: 	    if ($toaddr{$address}) { $msgtxt.='<hr />'.$toaddr{$address}; }
1.12      albertel 1702: 	    my @thismsg;
1.1       albertel 1703: 	    if ((($env{'form.critmsg'}) || ($env{'form.sendbck'})) && 
                   1704: 		(&Apache::lonnet::allowed('srm',$env{'request.course.id'})
                   1705: 		 || &Apache::lonnet::allowed('srm',$env{'request.course.id'}.
                   1706: 					     '/'.$env{'request.course.sec'}))) {
1.13      albertel 1707: 		$r->print(&mt('Sending critical message').' '.$recuname.':'.$recdomain.': ');
                   1708: 		@thismsg=
                   1709: 		    &Apache::lonmsg::user_crit_msg($recuname,$recdomain,
                   1710: 						   $msgsubj,$msgtxt,
                   1711: 						   $env{'form.sendbck'},
                   1712: 						   $env{'form.permanent'},
                   1713: 						   \$sentmessage{$address});
1.1       albertel 1714: 	    } else {
1.14      albertel 1715: 		$r->print(&mt('Sending').' '.$recuname.':'.$recdomain.': ');
1.13      albertel 1716: 		@thismsg=
                   1717: 		    &Apache::lonmsg::user_normal_msg($recuname,$recdomain,
                   1718: 						     $msgsubj,$msgtxt,
                   1719: 						     $content{'citation'},
                   1720: 						     undef,undef,
                   1721: 						     $env{'form.permanent'},
                   1722: 						     \$sentmessage{$address});
1.1       albertel 1723:             }
                   1724: 	    if (($env{'request.course.id'}) && (($msgtype eq 'critical') || 
                   1725:                                          ($env{'form.sendmode'} eq 'group'))) {
1.12      albertel 1726: 	        $specialmsg_status{$recuname.':'.$recdomain} =
                   1727: 		    join(' ',@thismsg);
                   1728: 		foreach my $result (@thismsg) {
                   1729: 		    if ($result eq 'ok') {
                   1730: 			$numspecial++;
                   1731: 		    }
                   1732: 		}
1.1       albertel 1733: 	    }
1.12      albertel 1734: 	    $sendstatus.=' '.join(' ',@thismsg);
1.1       albertel 1735: 	}
                   1736:         if (($env{'request.course.id'}) && (($env{'form.sendmode'} eq 'group')
                   1737:                                               || ($msgtype eq 'critical'))) {
                   1738:             my $subj_prefix;
                   1739:             if ($msgtype eq 'critical') {
                   1740:                 $subj_prefix = 'Critical.';
                   1741:             } else {
                   1742:                 $subj_prefix = 'Broadcast.';
                   1743:             }
                   1744:             my ($specialmsgid,$specialresult);
                   1745:             my $cnum = $env{'course.'.$env{'request.course.id'}.'.num'};
                   1746:             my $cdom = $env{'course.'.$env{'request.course.id'}.'.domain'};
1.29    ! www      1747:             my $course_str = &escape('['.$cnum.':'.$cdom.']');
1.1       albertel 1748: 
                   1749:             if ($numspecial) {
                   1750:                 $specialresult = &Apache::lonmsg::user_normal_msg_raw($cnum,$cdom,$subj_prefix.
                   1751:                     ' '.$course_str,$savemsg,undef,undef,undef,
                   1752:                     undef,undef,\$specialmsgid);
1.29    ! www      1753:                 $specialmsgid = &unescape($specialmsgid);
1.1       albertel 1754:             }
                   1755:             if ($specialresult eq 'ok') {
                   1756:                 my $record_sent;
1.13      albertel 1757:                 my @recusers;
                   1758:                 my @recudoms;
                   1759:                 my ($stamp,$crssubj,$msgname,$msgdom,$msgcount,$context,$pid) =
1.29    ! www      1760: 		    split(/\:/,&unescape($specialmsgid));
1.13      albertel 1761: 
1.1       albertel 1762:                 foreach my $recipient (sort(keys(%toaddr))) {
                   1763:                     if ($specialmsg_status{$recipient} eq 'ok') {
                   1764:                         my $usersubj = $subj_prefix.'['.$recipient.']';
                   1765:                         my $usermsgid = 
                   1766: 			    &Apache::lonmsg::buildmsgid($stamp,$usersubj,
                   1767: 							$msgname,$msgdom,
                   1768: 							$msgcount,$context,
                   1769: 							$pid);
                   1770:                         &Apache::lonmsg::user_normal_msg_raw($cnum,$cdom,$subj_prefix.
                   1771:                                              ' ['.$recipient.']',$msgsubj,undef,
                   1772:                         undef,undef,undef,$usermsgid,undef,undef,$specialmsgid);
1.13      albertel 1773:                         my ($uname,$udom) = split(/:/,$recipient);
1.1       albertel 1774:                         push(@recusers,$uname);
                   1775:                         push(@recudoms,$udom);
                   1776:                     }
                   1777:                 }
                   1778:                 if (@recusers) {
                   1779:                     my $specialmessage;
1.13      albertel 1780:                     my $sentsubj = 
                   1781: 			$subj_prefix.' ('.$numspecial.' sent) '.$msgsubj;
1.1       albertel 1782:                     $sentsubj = &HTML::Entities::encode($sentsubj,'<>&"');
                   1783:                     my $sentmsgid = 
                   1784: 			&Apache::lonmsg::buildmsgid($stamp,$sentsubj,$msgname,
                   1785: 						    $msgdom,$msgcount,$context,
                   1786: 						    $pid);
                   1787:                     ($specialmsgid,$specialmessage) = &Apache::lonmsg::packagemsg($msgsubj,$savemsg,
                   1788:                             undef,undef,undef,\@recusers,\@recudoms,$sentmsgid);
                   1789:                     $record_sent = &Apache::lonmsg::store_sent_mail($specialmsgid,$specialmessage);
                   1790:                 }
                   1791:             } else {
                   1792:                 &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');
                   1793:             }
                   1794:         }
                   1795:     } else {
                   1796: 	&printheader($r,'','No messages sent.'); 
                   1797:     }
                   1798:     if ($sendstatus=~/^(\s*(?:ok|con_delayed)\s*)*$/) {
1.5       albertel 1799: 	$r->print('<br /><span class="LC_success">'.&mt('Completed.').'</span>');
1.1       albertel 1800: 	if ($env{'form.displayedcrit'}) {
                   1801: 	    &discrit($r);
                   1802: 	} else {
                   1803: 	    &Apache::loncommunicate::menu($r);
                   1804: 	}
                   1805:     } else {
1.5       albertel 1806: 	$r->print('<p><span class="LC_error">'.&mt('Could not deliver message').'</span> '.
1.25      foxr     1807: 		  &mt('Please use the browser "Back" button and correct the recipient addresses '."($sendstatus)").'</p>');
1.1       albertel 1808:     }
                   1809: }
                   1810: 
                   1811: # ===================================================================== Handler
                   1812: 
                   1813: sub handler {
                   1814:     my $r=shift;
                   1815: 
                   1816: # ----------------------------------------------------------- Set document type
                   1817:     
                   1818:     &Apache::loncommon::content_type($r,'text/html');
                   1819:     $r->send_http_header;
                   1820:     
                   1821:     return OK if $r->header_only;
                   1822:     
                   1823: # --------------------------- Get query string for limited number of parameters
                   1824:     &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'},
                   1825:         ['display','replyto','forward','markread','markdel','markunread',
                   1826:          'sendreply','compose','sendmail','critical','recname','recdom',
                   1827:          'recordftf','sortedby','block','folder','startdis','interdis',
1.23      www      1828: 	 'showcommentbaseurl','dismode','group','subject','text']);
1.1       albertel 1829:     $sqs='&sortedby='.$env{'form.sortedby'};
                   1830: 
                   1831: # ------------------------------------------------------ They checked for email
                   1832:     unless ($env{'form.block'}) {
                   1833:         &Apache::lonnet::put('email_status',{'recnewemail'=>0});
                   1834:     }
                   1835: 
                   1836: # ----------------------------------------------------------------- Breadcrumbs
                   1837: 
                   1838:     &Apache::lonhtmlcommon::clear_breadcrumbs();
                   1839:     &Apache::lonhtmlcommon::add_breadcrumb
                   1840:         ({href=>"/adm/communicate",
                   1841:           text=>"Communication/Messages",
                   1842:           faq=>12,bug=>'Communication Tools',});
                   1843: 
                   1844: # ------------------------------------------------------------------ Get Folder
                   1845: 
                   1846:     my $folder=$env{'form.folder'};
                   1847:     unless ($folder) { 
                   1848: 	$folder=''; 
                   1849:     } else {
1.29    ! www      1850: 	$sqs.='&folder='.&escape($folder);
1.1       albertel 1851:     }
                   1852: # ------------------------------------------------------------ Get Display Mode
                   1853: 
                   1854:     my $dismode=$env{'form.dismode'};
                   1855:     unless ($dismode) { 
                   1856: 	$dismode=''; 
                   1857:     } else {
1.29    ! www      1858: 	$sqs.='&dismode='.&escape($dismode);
1.1       albertel 1859:     }
                   1860: 
                   1861: # --------------------------------------------------------------------- Display
                   1862: 
                   1863:     $startdis=$env{'form.startdis'};
                   1864:     $startdis--;
                   1865:     unless ($startdis) { $startdis=0; }
                   1866: 
                   1867:     $interdis=$env{'form.interdis'};
                   1868:     unless ($interdis) { $interdis=20; }
                   1869:     $sqs.='&interdis='.$interdis;
                   1870: 
                   1871:     if ($env{'form.firstview'}) {
                   1872: 	$startdis=0;
                   1873:     }
                   1874:     if ($env{'form.lastview'}) {
                   1875: 	$startdis=-1;
                   1876:     }
                   1877:     if ($env{'form.prevview'}) {
                   1878: 	$startdis--;
                   1879:     }
                   1880:     if ($env{'form.nextview'}) {
                   1881: 	$startdis++;
                   1882:     }
                   1883:     my $postedstartdis=$startdis+1;
                   1884:     $sqs.='&startdis='.$postedstartdis;
                   1885: 
                   1886: # --------------------------------------------------------------- Render Output
                   1887: 
                   1888:     if ($env{'form.display'}) {
                   1889: 	&displaymessage($r,$env{'form.display'},$folder);
                   1890:     } elsif ($env{'form.replyto'}) {
                   1891: 	&compout($r,'',$env{'form.replyto'},undef,undef,$folder,$dismode);
                   1892:     } elsif ($env{'form.confirm'}) {
                   1893: 	&printheader($r,'','Confirmed Receipt');
                   1894: 	foreach (keys %env) {
                   1895: 	    if ($_=~/^form\.rec\_(.*)$/) {
                   1896: 		$r->print('<b>'.&mt('Confirming Receipt').':</b> '.
                   1897: 			  &Apache::lonmsg::user_crit_received($1).'<br>');
                   1898: 	    }
                   1899: 	    if ($_=~/^form\.reprec\_(.*)$/) {
                   1900: 		my $msgid=$1;
                   1901: 		$r->print('<b>'.&mt('Confirming Receipt').':</b> '.
                   1902: 			  &Apache::lonmsg::user_crit_received($msgid).'<br>');
                   1903: 		&compout($r,'','','',$msgid);
                   1904: 	    }
                   1905: 	}
                   1906: 	&discrit($r);
                   1907:     } elsif ($env{'form.critical'}) {
                   1908: 	&printheader($r,'','Displaying Critical Messages');
                   1909: 	&discrit($r);
                   1910:     } elsif ($env{'form.forward'}) {
                   1911: 	&compout($r,$env{'form.forward'},undef,undef,undef,$folder);
                   1912:     } elsif ($env{'form.markdel'}) {
                   1913: 	&printheader($r,'','Deleted Message');
1.9       albertel 1914: 	my ($result,$msg) = 
                   1915: 	    &statuschange($env{'form.markdel'},'deleted',$folder);
                   1916: 	if (!$result) {
1.10      albertel 1917: 	    $r->print('<p class="LC_error">'.
                   1918: 		      &mt('Failed to delete the message.').'</p>'.
1.9       albertel 1919: 		      '<p class="LC_error">'.$msg."</p>\n");
                   1920: 	}
1.1       albertel 1921: 	&Apache::loncommunicate::menu($r);
                   1922: 	&disall($r,($folder?$folder:$dismode));
                   1923:     } elsif ($env{'form.markedmove'}) {
1.9       albertel 1924: 	my ($total,$failed,@failed_msg)=(0,0);
                   1925: 	foreach my $key (keys(%env)) {
                   1926: 	    if ($key=~/^form\.delmark_(.*)$/) {
                   1927: 		my ($result,$msg) =
1.29    ! www      1928: 		    &movemsg(&unescape($1),$folder,
1.9       albertel 1929: 			     $env{'form.movetofolder'});
                   1930: 		if ($result) {
                   1931: 		    $total++;
                   1932: 		} else {
                   1933: 		    $failed++;
                   1934: 		    push(@failed_msg,$msg);
                   1935: 		}
1.1       albertel 1936: 	    }
                   1937: 	}
                   1938: 	&printheader($r,'','Moved Messages');
1.9       albertel 1939: 	if ($failed) {
                   1940: 	    $r->print('<p class="LC_error">
1.10      albertel 1941:                           '.&mt('Failed to move [_1] message(s)',$failed).
                   1942: 		      '</p>');
1.9       albertel 1943: 	    $r->print('<p class="LC_error">'.
                   1944: 		      join("</p>\n<p class=\"LC_error\">",@failed_msg).
                   1945: 		      "</p>\n");
                   1946: 	}
1.10      albertel 1947: 	$r->print(&mt('Moved [_1] message(s)',$total).'<p>');
1.1       albertel 1948: 	&Apache::loncommunicate::menu($r);
                   1949: 	&disall($r,($folder?$folder:$dismode));
                   1950:     } elsif ($env{'form.markeddel'}) {
1.9       albertel 1951: 	my ($total,$failed,@failed_msg)=(0,0);
                   1952: 	foreach my $key (keys(%env)) {
                   1953: 	    if ($key=~/^form\.delmark_(.*)$/) {
                   1954: 		my ($result,$msg) = 
1.29    ! www      1955: 		    &statuschange(&unescape($1),'deleted',
1.9       albertel 1956: 				  $folder);
                   1957: 		if ($result) {
                   1958: 		    $total++;
                   1959: 		} else {
                   1960: 		    $failed++;
                   1961: 		    push(@failed_msg,$msg);
                   1962: 		}
1.1       albertel 1963: 	    }
                   1964: 	}
                   1965: 	&printheader($r,'','Deleted Messages');
1.9       albertel 1966: 	if ($failed) {
                   1967: 	    $r->print('<p class="LC_error">
1.10      albertel 1968:                           '.&mt('Failed to delete [_1] message(s)',$failed).
                   1969: 		      '</p>');
1.9       albertel 1970: 	    $r->print('<p class="LC_error">'.
                   1971: 		      join("</p>\n<p class=\"LC_error\">",@failed_msg).
                   1972: 		      "</p>\n");
                   1973: 	}
1.10      albertel 1974: 	$r->print(&mt('Deleted [_1] message(s)',$total).'<p>');
1.1       albertel 1975: 	&Apache::loncommunicate::menu($r);
                   1976: 	&disall($r,($folder?$folder:$dismode));
                   1977:     } elsif ($env{'form.markunread'}) {
                   1978: 	&printheader($r,'','Marked Message as Unread');
                   1979: 	&statuschange($env{'form.markunread'},'new');
                   1980: 	&Apache::loncommunicate::menu($r);
                   1981: 	&disall($r,($folder?$folder:$dismode));
                   1982:     } elsif ($env{'form.compose'}) {
                   1983: 	&compout($r,'','',$env{'form.compose'});
                   1984:     } elsif ($env{'form.recordftf'}) {
                   1985: 	&facetoface($r,$env{'form.recordftf'});
                   1986:     } elsif ($env{'form.block'}) {
                   1987:         &examblock($r,$env{'form.block'});
                   1988:     } elsif ($env{'form.sendmail'}) {
                   1989: 	&sendoffmail($r,$folder);
                   1990: 	if ($env{'form.storebasecomment'}) {
                   1991: 	    &storecomment($r);
                   1992: 	}
                   1993: 	if (($env{'form.rsspost'}) && ($env{'request.course.id'})) {
                   1994: 	    &Apache::lonrss::addentry($env{'course.'.$env{'request.course.id'}.'.num'},
                   1995: 				      $env{'course.'.$env{'request.course.id'}.'.domain'},
                   1996: 				      'Course_Announcements',
                   1997: 				      $env{'form.subject'},
                   1998: 				      $env{'form.message'},'/adm/communicate','public');
                   1999: 	}
                   2000: 	&disall($r,($folder?$folder:$dismode));
                   2001:     } elsif ($env{'form.newfolder'}) {
                   2002: 	&printheader($r,'','New Folder');
                   2003: 	&makefolder($env{'form.newfolder'});
                   2004: 	&Apache::loncommunicate::menu($r);
                   2005: 	&disall($r,$env{'form.newfolder'});
                   2006:     } elsif ($env{'form.showcommentbaseurl'}) {
                   2007: 	&storedcommentlisting($r);
                   2008:     } else {
                   2009: 	&printheader($r,'','Display All Messages');
                   2010: 	&Apache::loncommunicate::menu($r); 
                   2011: 	&disall($r,($folder?$folder:$dismode));
                   2012:     }
                   2013:     $r->print(&Apache::loncommon::end_page());
                   2014:     return OK;
                   2015: }
                   2016: # ================================================= Main program, reset counter
                   2017: 
                   2018: =pod
                   2019: 
                   2020: =back
                   2021: 
                   2022: =cut
                   2023: 
                   2024: 1; 
                   2025: 
                   2026: __END__
                   2027: 
                   2028: 
                   2029: 
                   2030: 
                   2031: 
                   2032: 
                   2033: 

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