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

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

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