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

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

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