File:  [LON-CAPA] / loncom / interface / lonmsg.pm
Revision 1.101: download - view: text, annotated - select for diffs
Mon May 17 19:00:45 2004 UTC (20 years ago) by raeburn
Branches: MAIN
CVS tags: HEAD
Course Coordinators and Instructors can schedule blocking periods for student display of e-mail.
During a blocking period students will be unable to display LON-CAPA e-mail sent during the current
blocking period. If Course Coodinators or Instructors need to contact students during this time, they
should send a critical message (these will still be viewable). Note: because COM functions can be
used outside a course context, blocking prevents display of all messages sent to the student during the
blocking period (including other courses).

    1: # The LearningOnline Network with CAPA
    2: # Routines for messaging
    3: #
    4: # $Id: lonmsg.pm,v 1.101 2004/05/17 19:00:45 raeburn Exp $
    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::lonmsg;
   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 then 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: =head1 FUNCTIONS
  101: 
  102: =over 4
  103: 
  104: =cut
  105: 
  106: use strict;
  107: use Apache::lonnet();
  108: use vars qw($msgcount);
  109: use HTML::TokeParser();
  110: use Apache::Constants qw(:common);
  111: use Apache::loncommon();
  112: use Apache::lontexconvert();
  113: use HTML::Entities();
  114: use Mail::Send;
  115: use Apache::lonlocal;
  116: use Apache::loncommunicate;
  117: 
  118: # Querystring component with sorting type
  119: my $sqs;
  120: 
  121: # ===================================================================== Package
  122: 
  123: sub packagemsg {
  124:     my ($subject,$message,$citation,$baseurl,$attachmenturl)=@_;
  125:     $message =&HTML::Entities::encode($message,'<>&"');
  126:     $citation=&HTML::Entities::encode($citation,'<>&"');
  127:     $subject =&HTML::Entities::encode($subject,'<>&"');
  128:     #remove machine specification
  129:     $baseurl =~ s|^http://[^/]+/|/|;
  130:     $baseurl =&HTML::Entities::encode($baseurl,'<>&"');
  131:     #remove machine specification
  132:     $attachmenturl =~ s|^http://[^/]+/|/|;
  133:     $attachmenturl =&HTML::Entities::encode($attachmenturl,'<>&"');
  134: 
  135:     my $now=time;
  136:     $msgcount++;
  137:     my $partsubj=$subject;
  138:     $partsubj=&Apache::lonnet::escape($partsubj);
  139:     my $msgid=&Apache::lonnet::escape(
  140:            $now.':'.$partsubj.':'.$ENV{'user.name'}.':'.
  141:            $ENV{'user.domain'}.':'.$msgcount.':'.$$);
  142:     my $result='<sendername>'.$ENV{'user.name'}.'</sendername>'.
  143:            '<senderdomain>'.$ENV{'user.domain'}.'</senderdomain>'.
  144:            '<subject>'.$subject.'</subject>'.
  145: 	   '<time>'.&Apache::lonlocal::locallocaltime($now).'</time>'.
  146: 	   '<servername>'.$ENV{'SERVER_NAME'}.'</servername>'.
  147:            '<host>'.$ENV{'HTTP_HOST'}.'</host>'.
  148: 	   '<client>'.$ENV{'REMOTE_ADDR'}.'</client>'.
  149: 	   '<browsertype>'.$ENV{'browser.type'}.'</browsertype>'.
  150: 	   '<browseros>'.$ENV{'browser.os'}.'</browseros>'.
  151: 	   '<browserversion>'.$ENV{'browser.version'}.'</browserversion>'.
  152:            '<browsermathml>'.$ENV{'browser.mathml'}.'</browsermathml>'.
  153: 	   '<browserraw>'.$ENV{'HTTP_USER_AGENT'}.'</browserraw>'.
  154: 	   '<courseid>'.$ENV{'request.course.id'}.'</courseid>'.
  155: 	   '<coursesec>'.$ENV{'request.course.sec'}.'</coursesec>'.
  156: 	   '<role>'.$ENV{'request.role'}.'</role>'.
  157: 	   '<resource>'.$ENV{'request.filename'}.'</resource>'.
  158:            '<msgid>'.$msgid.'</msgid>'.
  159: 	   '<message>'.$message.'</message>';
  160:     if (defined($citation)) {
  161: 	$result.='<citation>'.$citation.'</citation>';
  162:     }
  163:     if (defined($baseurl)) {
  164: 	$result.= '<baseurl>'.$baseurl.'</baseurl>';
  165:     }
  166:     if (defined($attachmenturl)) {
  167: 	$result.= '<attachmenturl>'.$attachmenturl.'</attachmenturl>';
  168:     }
  169:     return $msgid,$result;
  170: }
  171: 
  172: # ================================================== Unpack message into a hash
  173: 
  174: sub unpackagemsg {
  175:     my ($message,$notoken)=@_;
  176:     my %content=();
  177:     my $parser=HTML::TokeParser->new(\$message);
  178:     my $token;
  179:     while ($token=$parser->get_token) {
  180:        if ($token->[0] eq 'S') {
  181: 	   my $entry=$token->[1];
  182:            my $value=$parser->get_text('/'.$entry);
  183:            $content{$entry}=$value;
  184:        }
  185:     }
  186:     if ($content{'attachmenturl'}) {
  187:        my ($fname)=($content{'attachmenturl'}=~m|/([^/]+)$|);
  188:        if ($notoken) {
  189: 	   $content{'message'}.='<p>'.&mt('Attachment').': <tt>'.$fname.'</tt>';
  190:        } else {
  191: 	   &Apache::lonnet::allowuploaded('/adm/msg',
  192: 					  $content{'attachmenturl'});
  193: 	   $content{'message'}.='<p>'.&mt('Attachment').
  194: 	       ': <a href="'.$content{'attachmenturl'}.'"><tt>'.
  195: 	       $fname.'</tt></a>';
  196:        }
  197:     }
  198:     return %content;
  199: }
  200: 
  201: # ======================================================= Get info out of msgid
  202: 
  203: sub unpackmsgid {
  204:     my $msgid=&Apache::lonnet::unescape(shift);
  205:     my ($sendtime,$shortsubj,$fromname,$fromdomain)=split(/\:/,
  206:                           &Apache::lonnet::unescape($msgid));
  207:     my %status=&Apache::lonnet::get('email_status',[$msgid]);
  208:     if ($status{$msgid}=~/^error\:/) { $status{$msgid}=''; }
  209:     unless ($status{$msgid}) { $status{$msgid}='new'; }
  210:     return ($sendtime,$shortsubj,$fromname,$fromdomain,$status{$msgid});
  211: } 
  212: 
  213: 
  214: sub sendemail {
  215:     my ($to,$subject,$body)=@_;
  216:     $body=
  217:     "*** ".&mt('This is an automatic message generated by the LON-CAPA system.')."\n".
  218:     "*** ".&mt('Please do not reply to this address.')."\n\n".$body;
  219:     my $msg = new Mail::Send;
  220:     $msg->to($to);
  221:     $msg->subject('[LON-CAPA] '.$subject);
  222:     if (my $fh = $msg->open()) {
  223: 	print $fh $body;
  224: 	$fh->close;
  225:     }
  226: }
  227: 
  228: # ==================================================== Send notification emails
  229: 
  230: sub sendnotification {
  231:     my ($to,$touname,$toudom,$subj,$crit)=@_;
  232:     my $sender=$ENV{'environment.firstname'}.' '.$ENV{'environment.lastname'};
  233:     my $critical=($crit?' critical':'');
  234:     my $url='http://'.
  235:       $Apache::lonnet::hostname{&Apache::lonnet::homeserver($touname,$toudom)}.
  236:       '/adm/email?username='.$touname.'&domain='.$toudom;
  237:     my $body=(<<ENDMSG);
  238: You received a$critical message from $sender in LON-CAPA. The subject is
  239: 
  240:  $subj
  241: 
  242: Use
  243: 
  244:  $url
  245: 
  246: to access this message.
  247: ENDMSG
  248:     &sendemail($to,'New'.$critical.' message from '.$sender,$body);
  249: }
  250: # ============================================================= Check for email
  251: 
  252: sub newmail {
  253:     if ((time-$ENV{'user.mailcheck.time'})>300) {
  254:         my %what=&Apache::lonnet::get('email_status',['recnewemail']);
  255:         &Apache::lonnet::appenv('user.mailcheck.time'=>time);
  256:         if ($what{'recnewemail'}>0) { return 1; }
  257:     }
  258:     return 0;
  259: }
  260: 
  261: # =============================== Automated message to the author of a resource
  262: 
  263: =pod
  264: 
  265: =item * B<author_res_msg($filename, $message)>: Sends message $message to the owner
  266:     of the resource with the URI $filename.
  267: 
  268: =cut
  269: 
  270: sub author_res_msg {
  271:     my ($filename,$message)=@_;
  272:     unless ($message) { return 'empty'; }
  273:     $filename=&Apache::lonnet::declutter($filename);
  274:     my ($domain,$author,@dummy)=split(/\//,$filename);
  275:     my $homeserver=&Apache::lonnet::homeserver($author,$domain);
  276:     if ($homeserver ne 'no_host') {
  277:        my $id=unpack("%32C*",$message);
  278:        my $msgid;
  279:        ($msgid,$message)=&packagemsg($filename,$message);
  280:        return &Apache::lonnet::reply('put:'.$domain.':'.$author.
  281:          ':nohist_res_msgs:'.
  282:           &Apache::lonnet::escape($filename.'_'.$id).'='.
  283:           &Apache::lonnet::escape($message),$homeserver);
  284:     }
  285:     return 'no_host';
  286: }
  287: 
  288: # =========================================== Retrieve author resource messages
  289: 
  290: sub retrieve_author_res_msg {
  291:     my $url=shift;
  292:     $url=&Apache::lonnet::declutter($url);
  293:     my ($domain,$author)=($url=~/^(\w+)\/(\w+)\//);
  294:     my %errormsgs=&Apache::lonnet::dump('nohist_res_msgs',$domain,$author);
  295:     my $msgs='';
  296:     foreach (keys %errormsgs) {
  297: 	if ($_=~/^\Q$url\E\_\d+$/) {
  298: 	    my %content=&unpackagemsg($errormsgs{$_});
  299: 	    $msgs.='<p><img src="/adm/lonMisc/bomb.gif" /><b>'.
  300: 		$content{'time'}.'</b>: '.$content{'message'}.
  301: 		'<br /></p>';
  302: 	}
  303:     } 
  304:     return $msgs;     
  305: }
  306: 
  307: 
  308: # =============================== Delete all author messages related to one URL
  309: 
  310: sub del_url_author_res_msg {
  311:     my $url=shift;
  312:     $url=&Apache::lonnet::declutter($url);
  313:     my ($domain,$author)=($url=~/^(\w+)\/(\w+)\//);
  314:     my @delmsgs=();
  315:     foreach (&Apache::lonnet::getkeys('nohist_res_msgs',$domain,$author)) {
  316: 	if ($_=~/^\Q$url\E\_\d+$/) {
  317: 	    push (@delmsgs,$_);
  318: 	}
  319:     }
  320:     return &Apache::lonnet::del('nohist_res_msgs',\@delmsgs,$domain,$author);
  321: }
  322: 
  323: # ================= Return hash with URLs for which there is a resource message
  324: 
  325: sub all_url_author_res_msg {
  326:     my ($author,$domain)=@_;
  327:     my %returnhash=();
  328:     foreach (&Apache::lonnet::getkeys('nohist_res_msgs',$domain,$author)) {
  329: 	$_=~/^(.+)\_\d+/;
  330: 	$returnhash{$1}=1;
  331:     }
  332:     return %returnhash;
  333: }
  334: 
  335: # ================================================== Critical message to a user
  336: 
  337: sub user_crit_msg_raw {
  338:     my ($user,$domain,$subject,$message,$sendback)=@_;
  339: # Check if allowed missing
  340:     my $status='';
  341:     my $msgid='undefined';
  342:     unless (($message)&&($user)&&($domain)) { $status='empty'; };
  343:     my $homeserver=&Apache::lonnet::homeserver($user,$domain);
  344:     if ($homeserver ne 'no_host') {
  345:        ($msgid,$message)=&packagemsg($subject,$message);
  346:        if ($sendback) { $message.='<sendback>true</sendback>'; }
  347:        $status=&Apache::lonnet::critical(
  348:            'put:'.$domain.':'.$user.':critical:'.
  349:            &Apache::lonnet::escape($msgid).'='.
  350:            &Apache::lonnet::escape($message),$homeserver);
  351:        if ($ENV{'request.course.id'}) {
  352:           &user_normal_msg_raw(
  353:             $ENV{'course.'.$ENV{'request.course.id'}.'.num'},
  354:             $ENV{'course.'.$ENV{'request.course.id'}.'.domain'},
  355:             'Critical ['.$user.':'.$domain.']',
  356: 	    $message);
  357:        }
  358:     } else {
  359:        $status='no_host';
  360:     }
  361: # Notifications
  362:     my %userenv = &Apache::lonnet::get('environment',['critnotification'],
  363:                                        $domain,$user);
  364:     if ($userenv{'critnotification'}) {
  365:       &sendnotification($userenv{'critnotification'},$user,$domain,$subject,1);
  366:     }
  367: # Log this
  368:     &Apache::lonnet::logthis(
  369:       'Sending critical email '.$msgid.
  370:       ', log status: '.
  371:       &Apache::lonnet::log($ENV{'user.domain'},$ENV{'user.name'},
  372:                          $ENV{'user.home'},
  373:       'Sending critical '.$msgid.' to '.$user.' at '.$domain.' with status: '
  374:       .$status));
  375:     return $status;
  376: }
  377: 
  378: # New routine that respects "forward" and calls old routine
  379: 
  380: =pod
  381: 
  382: =item * B<user_crit_msg($user, $domain, $subject, $message, $sendback)>: Sends
  383:     a critical message $message to the $user at $domain. If $sendback is true,
  384:     a reciept will be sent to the current user when $user recieves the message.
  385: 
  386: =cut
  387: 
  388: sub user_crit_msg {
  389:     my ($user,$domain,$subject,$message,$sendback)=@_;
  390:     my $status='';
  391:     my %userenv = &Apache::lonnet::get('environment',['msgforward'],
  392:                                        $domain,$user);
  393:     my $msgforward=$userenv{'msgforward'};
  394:     if ($msgforward) {
  395:        foreach (split(/\,/,$msgforward)) {
  396: 	 my ($forwuser,$forwdomain)=split(/\:/,$_);
  397:          $status.=
  398: 	   &user_crit_msg_raw($forwuser,$forwdomain,$subject,$message,
  399:                 $sendback).' ';
  400:        }
  401:     } else { 
  402: 	$status=&user_crit_msg_raw($user,$domain,$subject,$message,$sendback);
  403:     }
  404:     return $status;
  405: }
  406: 
  407: # =================================================== Critical message received
  408: 
  409: sub user_crit_received {
  410:     my $msgid=shift;
  411:     my %message=&Apache::lonnet::get('critical',[$msgid]);
  412:     my %contents=&unpackagemsg($message{$msgid},1);
  413:     my $status='rec: '.($contents{'sendback'}?
  414:      &user_normal_msg($contents{'sendername'},$contents{'senderdomain'},
  415:                      &mt('Receipt').': '.$ENV{'user.name'}.' '.&mt('at').' '.$ENV{'user.domain'}.', '.$contents{'subject'},
  416:                      &mt('User').' '.$ENV{'user.name'}.' '.&mt('at').' '.$ENV{'user.domain'}.
  417:                      ' acknowledged receipt of message'."\n".'   "'.
  418:                      $contents{'subject'}.'"'."\n".&mt('dated').' '.
  419:                      $contents{'time'}.".\n"
  420:                      ):'no msg req');
  421:     $status.=' trans: '.
  422:      &Apache::lonnet::put(
  423:      'nohist_email',{$contents{'msgid'} => $message{$msgid}});
  424:     $status.=' del: '.
  425:      &Apache::lonnet::del('critical',[$contents{'msgid'}]);
  426:     &Apache::lonnet::log($ENV{'user.domain'},$ENV{'user.name'},
  427:                          $ENV{'user.home'},'Received critical message '.
  428:                          $contents{'msgid'}.
  429:                          ', '.$status);
  430:     return $status;
  431: }
  432: 
  433: # ======================================================== Normal communication
  434: 
  435: sub user_normal_msg_raw {
  436:     my ($user,$domain,$subject,$message,$citation,$baseurl,$attachmenturl)=@_;
  437: # Check if allowed missing
  438:     my $status='';
  439:     my $msgid='undefined';
  440:     unless (($message)&&($user)&&($domain)) { $status='empty'; };
  441:     my $homeserver=&Apache::lonnet::homeserver($user,$domain);
  442:     if ($homeserver ne 'no_host') {
  443:        ($msgid,$message)=&packagemsg($subject,$message,$citation,$baseurl,
  444:                                      $attachmenturl);
  445:        $status=&Apache::lonnet::critical(
  446:            'put:'.$domain.':'.$user.':nohist_email:'.
  447:            &Apache::lonnet::escape($msgid).'='.
  448:            &Apache::lonnet::escape($message),$homeserver);
  449:        &Apache::lonnet::put
  450:                          ('email_status',{'recnewemail'=>time},$domain,$user);
  451:     } else {
  452:        $status='no_host';
  453:     }
  454: # Notifications
  455:     my %userenv = &Apache::lonnet::get('environment',['notification'],
  456:                                        $domain,$user);
  457:     if ($userenv{'notification'}) {
  458: 	&sendnotification($userenv{'notification'},$user,$domain,$subject,0);
  459:     }
  460:     &Apache::lonnet::log($ENV{'user.domain'},$ENV{'user.name'},
  461:                          $ENV{'user.home'},
  462:       'Sending '.$msgid.' to '.$user.' at '.$domain.' with status: '.$status);
  463:     return $status;
  464: }
  465: 
  466: # New routine that respects "forward" and calls old routine
  467: 
  468: =pod
  469: 
  470: =item * B<user_normal_msg($user, $domain, $subject, $message,
  471:     $citation, $baseurl, $attachmenturl)>: Sends a message to the
  472:     $user at $domain, with subject $subject and message $message.
  473: 
  474: =cut
  475: 
  476: sub user_normal_msg {
  477:     my ($user,$domain,$subject,$message,$citation,$baseurl,$attachmenturl)=@_;
  478:     my $status='';
  479:     my %userenv = &Apache::lonnet::get('environment',['msgforward'],
  480:                                        $domain,$user);
  481:     my $msgforward=$userenv{'msgforward'};
  482:     if ($msgforward) {
  483:        foreach (split(/\,/,$msgforward)) {
  484: 	 my ($forwuser,$forwdomain)=split(/\:/,$_);
  485:          $status.=
  486: 	  &user_normal_msg_raw($forwuser,$forwdomain,$subject,$message,
  487: 			       $citation,$baseurl,$attachmenturl).' ';
  488:        }
  489:     } else { 
  490: 	$status=&user_normal_msg_raw($user,$domain,$subject,$message,
  491: 				     $citation,$baseurl,$attachmenturl);
  492:     }
  493:     return $status;
  494: }
  495: 
  496: 
  497: # =============================================================== Status Change
  498: 
  499: sub statuschange {
  500:     my ($msgid,$newstatus)=@_;
  501:     my %status=&Apache::lonnet::get('email_status',[$msgid]);
  502:     if ($status{$msgid}=~/^error\:/) { $status{$msgid}=''; }
  503:     unless ($status{$msgid}) { $status{$msgid}='new'; }
  504:     unless (($status{$msgid} eq 'replied') || 
  505:             ($status{$msgid} eq 'forwarded')) {
  506: 	&Apache::lonnet::put('email_status',{$msgid => $newstatus});
  507:     }
  508:     if (($newstatus eq 'deleted') || ($newstatus eq 'new')) {
  509: 	&Apache::lonnet::put('email_status',{$msgid => $newstatus});
  510:     }
  511: }
  512: 
  513: # ======================================================= Display a course list
  514: 
  515: sub discourse {
  516:     my $r=shift;
  517:     my %courselist=&Apache::lonnet::dump(
  518:                    'classlist',
  519: 		   $ENV{'course.'.$ENV{'request.course.id'}.'.domain'},
  520: 		   $ENV{'course.'.$ENV{'request.course.id'}.'.num'});
  521:     my $now=time;
  522:     my %lt=&Apache::lonlocal::texthash('cfa' => 'Check for All',
  523:             'cfs' => 'Check for Section/Group',
  524:             'cfn' => 'Check for None');
  525:     $r->print(<<ENDDISHEADER);
  526: <input type="hidden" name="sendmode" value="group" />
  527: <script>
  528:     function checkall() {
  529: 	for (i=0; i<document.forms.compemail.elements.length; i++) {
  530:             if 
  531:           (document.forms.compemail.elements[i].name.indexOf('send_to_')==0) {
  532: 	      document.forms.compemail.elements[i].checked=true;
  533:             }
  534:         }
  535:     }
  536: 
  537:     function checksec() {
  538: 	for (i=0; i<document.forms.compemail.elements.length; i++) {
  539:             if 
  540:           (document.forms.compemail.elements[i].name.indexOf
  541:            ('send_to_&&&'+document.forms.compemail.chksec.value)==0) {
  542: 	      document.forms.compemail.elements[i].checked=true;
  543:             }
  544:         }
  545:     }
  546: 
  547:     function uncheckall() {
  548: 	for (i=0; i<document.forms.compemail.elements.length; i++) {
  549:             if 
  550:           (document.forms.compemail.elements[i].name.indexOf('send_to_')==0) {
  551: 	      document.forms.compemail.elements[i].checked=false;
  552:             }
  553:         }
  554:     }
  555: </script>
  556: <input type="button" onClick="checkall()" value="$lt{'cfa'}" />&nbsp;
  557: <input type="button" onClick="checksec()" value="$lt{'cfs'}" />
  558: <input type="text" size="5" name=chksec />&nbsp;
  559: <input type="button" onClick="uncheckall()" value="$lt{'cfn'}" />
  560: <p>
  561: ENDDISHEADER
  562:     my %coursepersonnel=
  563:        &Apache::lonnet::get_course_adv_roles();
  564:     foreach my $role (sort keys %coursepersonnel) {
  565:        foreach (split(/\,/,$coursepersonnel{$role})) {
  566: 	   my ($puname,$pudom)=split(/\:/,$_);
  567: 	   $r->print(
  568:              '<br /><input type="checkbox" name="send_to_&&&&&&_'.
  569:              $puname.':'.$pudom.'" /> '.
  570: 		     &Apache::loncommon::plainname($puname,
  571:                           $pudom).' ('.$_.'), <i>'.$role.'</i>');
  572: 	}
  573:     }
  574: 
  575:     foreach (sort keys %courselist) {
  576:         my ($end,$start)=split(/\:/,$courselist{$_});
  577:         my $active=1;
  578:         if (($end) && ($now>$end)) { $active=0; }
  579:         if ($active) {
  580:            my ($sname,$sdom)=split(/\:/,$_);
  581:            my %reply=&Apache::lonnet::get('environment',
  582:               ['firstname','middlename','lastname','generation'],
  583:               $sdom,$sname);
  584:            my $section=&Apache::lonnet::usection
  585: 	       ($sdom,$sname,$ENV{'request.course.id'});
  586:            $r->print(
  587:         '<br><input type=checkbox name="send_to_&&&'.$section.'&&&_'.$_.'"> '.
  588: 		      $reply{'firstname'}.' '. 
  589:                       $reply{'middlename'}.' '.
  590:                       $reply{'lastname'}.' '.
  591:                       $reply{'generation'}.
  592:                       ' ('.$_.') '.$section);
  593:         } 
  594:     }
  595: }
  596: 
  597: # ==================================================== Display Critical Message
  598: 
  599: sub discrit {
  600:     my $r=shift;
  601:     my $header = '<h1><font color=red>'.&mt('Critical Messages').'</font></h1>'.
  602:         '<form action=/adm/email method=post>'.
  603:         '<input type=hidden name=confirm value=true>';
  604:     my %what=&Apache::lonnet::dump('critical');
  605:     my $result = '';
  606:     foreach (sort keys %what) {
  607:         my %content=&unpackagemsg($what{$_});
  608:         next if ($content{'senderdomain'} eq '');
  609:         $content{'message'}=~s/\n/\<br\>/g;
  610:         $result.='<hr>'.&mt('From').': <b>'.
  611: &Apache::loncommon::aboutmewrapper(
  612:  &Apache::loncommon::plainname($content{'sendername'},$content{'senderdomain'}),$content{'sendername'},$content{'senderdomain'}).'</b> ('.
  613: $content{'sendername'}.'@'.
  614:             $content{'senderdomain'}.') '.$content{'time'}.
  615:             '<br>'.&mt('Subject').': '.$content{'subject'}.
  616:             '<br><blockquote>'.
  617:               &Apache::lontexconvert::msgtexconverted($content{'message'}).
  618:             '</blockquote><small>'.
  619: &mt('You have to confirm that you received this message. After confirmation, this message will be moved to your regular inbox').
  620:             '</small><br />'.
  621:             '<input type=submit name="rec_'.$_.'" value="'.&mt('Confirm Receipt').'">'.
  622:             '<input type=submit name="reprec_'.$_.'" '.
  623:                   'value="'.&mt('Confirm Receipt and Reply').'">';
  624:     }
  625:     # Check to see if there were any messages.
  626:     if ($result eq '') {
  627:         $result = "<h2>".&mt('You have no critical messages.')."</h2>".
  628: 	    '<a href="/adm/roles">'.&mt('Select a course').'</a>';
  629:     } else {
  630:         $r->print($header);
  631:     }
  632:     $r->print($result);
  633:     $r->print('<input type=hidden name="displayedcrit" value="true"></form>');
  634: }
  635: 
  636: sub sortedmessages {
  637:     my ($blocked,$startblock,$endblock,$numblocked) = @_;
  638:     my @messages = &Apache::lonnet::getkeys('nohist_email');
  639:     #unpack the varibles and repack into temp for sorting
  640:     my @temp;
  641:     foreach (@messages) {
  642: 	my $msgid=&Apache::lonnet::escape($_);
  643: 	my ($sendtime,$shortsubj,$fromname,$fromdomain,$status)=
  644: 	    &Apache::lonmsg::unpackmsgid($msgid);
  645: 	my @temp1 = ($sendtime,$shortsubj,$fromname,$fromdomain,$status,
  646: 		     $msgid);
  647:         # Check whether message was sent during blocking period.
  648:         if ($sendtime >= $startblock && ($sendtime <= $endblock && $endblock > 0) ) {
  649:             my $escid = &Apache::lonnet::unescape($msgid);
  650:             $$blocked{$escid} = 'ON';
  651:             $$numblocked ++;
  652:         } else { 
  653:             push @temp ,\@temp1;
  654:         }
  655:     }
  656:     #default sort
  657:     @temp = sort  {$a->[0] <=> $b->[0]} @temp;    
  658:     if ($ENV{'form.sortedby'} eq "date"){
  659:         @temp = sort  {$a->[0] <=> $b->[0]} @temp;    
  660:     }
  661:     if ($ENV{'form.sortedby'} eq "revdate"){
  662:     	@temp = sort  {$b->[0] <=> $a->[0]} @temp; 
  663:     }
  664:     if ($ENV{'form.sortedby'} eq "user"){
  665: 	@temp = sort  {lc($a->[2]) cmp lc($b->[2])} @temp;
  666:     }
  667:     if ($ENV{'form.sortedby'} eq "revuser"){
  668: 	@temp = sort  {lc($b->[2]) cmp lc($a->[2])} @temp;
  669:     }
  670:     if ($ENV{'form.sortedby'} eq "domain"){
  671:         @temp = sort  {$a->[3] cmp $b->[3]} @temp;
  672:     }
  673:     if ($ENV{'form.sortedby'} eq "revdomain"){
  674:         @temp = sort  {$b->[3] cmp $a->[3]} @temp;
  675:     }
  676:     if ($ENV{'form.sortedby'} eq "subject"){
  677:         @temp = sort  {lc($a->[1]) cmp lc($b->[1])} @temp;
  678:     }
  679:     if ($ENV{'form.sortedby'} eq "revsubject"){
  680:         @temp = sort  {lc($b->[1]) cmp lc($a->[1])} @temp;
  681:     }
  682:     if ($ENV{'form.sortedby'} eq "status"){
  683:         @temp = sort  {$a->[4] cmp $b->[4]} @temp;
  684:     }
  685:     if ($ENV{'form.sortedby'} eq "revstatus"){
  686:         @temp = sort  {$b->[4] cmp $a->[4]} @temp;
  687:     }
  688:     return @temp;
  689: }
  690: 
  691: # ======================================================== Display all messages
  692: 
  693: sub disall {
  694:     my $r=shift;
  695:     my %blocked = ();
  696:     my %setters = ();
  697:     my $startblock;
  698:     my $endblock;
  699:     my $numblocked = 0;
  700:     &blockcheck(\%setters,\$startblock,\$endblock);
  701:     $r->print(<<ENDDISHEADER);
  702: <script>
  703:     function checkall() {
  704: 	for (i=0; i<document.forms.disall.elements.length; i++) {
  705:             if 
  706:           (document.forms.disall.elements[i].name.indexOf('delmark_')==0) {
  707: 	      document.forms.disall.elements[i].checked=true;
  708:             }
  709:         }
  710:     }
  711: 
  712:     function uncheckall() {
  713: 	for (i=0; i<document.forms.disall.elements.length; i++) {
  714:             if 
  715:           (document.forms.disall.elements[i].name.indexOf('delmark_')==0) {
  716: 	      document.forms.disall.elements[i].checked=false;
  717:             }
  718:         }
  719:     }
  720: </script>
  721: ENDDISHEADER
  722:     $r->print('<h1>'.&mt('Display All Messages').'</h1><form method=post name=disall '.
  723: 	      'action="/adm/email">'.
  724: 	      '<table border=2><tr><th colspan=2>&nbsp</th><th>');
  725:     if ($ENV{'form.sortedby'} eq "revdate") {
  726: 	$r->print('<a href = "?sortedby=date">'.&mt('Date').'</a></th>');
  727:     } else {
  728: 	$r->print('<a href = "?sortedby=revdate">'.&mt('Date').'</a></th>');
  729:     }
  730:     $r->print('<th>');
  731:     if ($ENV{'form.sortedby'} eq "revuser") {
  732: 	$r->print('<a href = "?sortedby=user">'.&mt('Username').'</a>');
  733:     } else {
  734: 	$r->print('<a href = "?sortedby=revuser">'.&mt('Username').'</a>');
  735:     }
  736:     $r->print('</th><th>');
  737:     if ($ENV{'form.sortedby'} eq "revdomain") {
  738: 	$r->print('<a href = "?sortedby=domain">'.&mt('Domain').'</a>');
  739:     } else {
  740: 	$r->print('<a href = "?sortedby=revdomain">'.&mt('Domain').'</a>');
  741:     }
  742:     $r->print('</th><th>');
  743:     if ($ENV{'form.sortedby'} eq "revsubject") {
  744: 	$r->print('<a href = "?sortedby=subject">'.&mt('Subject').'</a>');
  745:     } else {
  746:     	$r->print('<a href = "?sortedby=revsubject">'.&mt('Subject').'</a>');
  747:     }
  748:     $r->print('</th><th>');
  749:     if ($ENV{'form.sortedby'} eq "revstatus") {
  750: 	$r->print('<a href = "?sortedby=status">'.&mt('Status').'</th>');
  751:     } else {
  752:      	$r->print('<a href = "?sortedby=revstatus">'.&mt('Status').'</th>');
  753:     }
  754:     $r->print('</tr>');
  755:     my @temp=sortedmessages(\%blocked,$startblock,$endblock,\$numblocked);
  756:     foreach (@temp){
  757: 	my ($sendtime,$shortsubj,$fromname,$fromdomain,$status,$origID)= @$_;
  758: 	if (($status ne 'deleted') && defined($sendtime) && $sendtime!~/error/) {
  759: 	    if ($status eq 'new') {
  760: 		$r->print('<tr bgcolor="#FFBB77">');
  761: 	    } elsif ($status eq 'read') {
  762: 		$r->print('<tr bgcolor="#BBBB77">');
  763: 	    } elsif ($status eq 'replied') {
  764: 		$r->print('<tr bgcolor="#AAAA88">'); 
  765: 	    } else {
  766: 		$r->print('<tr bgcolor="#99BBBB">');
  767: 	    }
  768: 	    $r->print('<td><a href="/adm/email?display='.$origID.$sqs. 
  769: 		      '">'.&mt('Open').'</a></td><td><a href="/adm/email?markdel='.$origID.$sqs.
  770: 		      '">'.&mt('Delete').'</a><input type=checkbox name="delmark_'.$origID.'" /></td>'.
  771: 		      '<td>'.&Apache::lonlocal::locallocaltime($sendtime).'</td><td>'.
  772: 		      $fromname.'</td><td>'.$fromdomain.'</td><td>'.
  773: 		      &Apache::lonnet::unescape($shortsubj).'</td><td>'.
  774:                       $status.'</td></tr>');
  775: 	}
  776:     }   
  777:     $r->print('</table><p>'.
  778:               '<a href="javascript:checkall()">'.&mt('Check All').'</a>&nbsp;'.
  779:               '<a href="javascript:uncheckall()">'.&mt('Uncheck All').'</a><p>'.
  780: 	      '<input type="hidden" name="sortedby" value="'.$ENV{'form.sortedby'}.'" />'.
  781:               '<input type="submit" name="markeddel" value="'.&mt('Delete Checked').'" />'.
  782:               '</form>');
  783:     if ($numblocked > 0) {
  784:         my $beginblock = &Apache::lonlocal::locallocaltime($startblock);
  785:         my $finishblock = &Apache::lonlocal::locallocaltime($endblock);
  786:         $r->print('<br /><br />'.
  787:                   $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.'));
  788:         &build_block_table($r,$startblock,$endblock,\%setters);
  789:     }
  790:     $r->print('</body></html>');
  791: }
  792: 
  793: # ============================================================== Compose output
  794: 
  795: sub compout {
  796:     my ($r,$forwarding,$replying,$broadcast,$replycrit)=@_;
  797: 
  798:     if ($broadcast eq 'individual') {
  799: 	&printheader($r,'/adm/email?compose=individual',
  800: 	     'Send a Message');
  801:     } elsif ($broadcast) {
  802: 	&printheader($r,'/adm/email?compose=group',
  803: 	     'Broadcast Message');
  804:     } elsif ($forwarding) {
  805: 	&Apache::lonhtmlcommon::add_breadcrumb
  806:         ({href=>"/adm/email?display=".&Apache::lonnet::escape($forwarding),
  807:           text=>"Display Message"});
  808: 	&printheader($r,'/adm/email?forward='.&Apache::lonnet::escape($forwarding),
  809: 	     'Forwarding a Message');
  810:     } elsif ($replying) {
  811: 	&Apache::lonhtmlcommon::add_breadcrumb
  812:         ({href=>"/adm/email?display=".&Apache::lonnet::escape($replying),
  813:           text=>"Display Message"});
  814: 	&printheader($r,'/adm/email?replyto='.&Apache::lonnet::escape($replying),
  815: 	     'Replying to a Message');
  816:     } elsif ($replycrit) {
  817: 	$r->print('<h3>'.&mt('Replying to a Critical Message').'</h3>');
  818: 	$replying=$replycrit;
  819:     } else {
  820: 	&printheader($r,'/adm/email?compose=upload',
  821: 	     'Distribute from Uploaded File');
  822:     }
  823: 
  824:     my $dispcrit='';
  825:     my $dissub='';
  826:     my $dismsg='';
  827:     my $func=&mt('Send New');
  828:     my %lt=&Apache::lonlocal::texthash('us' => 'Username',
  829: 				       'do' => 'Domain',
  830: 				       'ad' => 'Additional Recipients',
  831: 				       'sb' => 'Subject',
  832: 				       'ca' => 'Cancel',
  833: 				       'ma' => 'Mail');
  834: 
  835:     if (&Apache::lonnet::allowed('srm',$ENV{'request.course.id'})) {
  836: 	 my $crithelp = Apache::loncommon::help_open_topic("Course_Critical_Message");
  837:          $dispcrit=
  838:  '<input type="checkbox" name="critmsg" /> '.&mt('Send as critical message').' ' . $crithelp . 
  839:  '<br>'.
  840:  '<input type="checkbox" name="sendbck" /> '.&mt('Send as critical message').'  ' .
  841:  &mt('and return receipt') . $crithelp . '<p>';
  842:      }
  843:     my %message;
  844:     my %content;
  845:     my $defdom=$ENV{'user.domain'};
  846:     if ($forwarding) {
  847: 	%message=&Apache::lonnet::get('nohist_email',[$forwarding]);
  848: 	%content=&unpackagemsg($message{$forwarding});
  849: 	$dispcrit.='<input type="hidden" name="forwid" value="'.
  850: 	    $forwarding.'" />';
  851: 	$func=&mt('Forward');
  852: 	
  853: 	$dissub=&mt('Forwarding').': '.$content{'subject'};
  854: 	$dismsg=&mt('Forwarded message from').' '.
  855: 	    $content{'sendername'}.' '.&mt('at').' '.$content{'senderdomain'};
  856:     }
  857:     if ($replying) {
  858: 	%message=&Apache::lonnet::get('nohist_email',[$replying]);
  859: 	%content=&unpackagemsg($message{$replying});
  860: 	$dispcrit.='<input type="hidden" name="forwid" value="'.
  861: 	    $forwarding.'" />';
  862: 	$func=&mt('Replying to');
  863: 	
  864: 	$dissub=&mt('Reply').': '.$content{'subject'};       
  865: 	$dismsg='> '.$content{'message'};
  866: 	$dismsg=~s/\r/\n/g;
  867: 	$dismsg=~s/\f/\n/g;
  868: 	$dismsg=~s/\n+/\n\> /g;
  869:     }
  870:     if ($ENV{'form.recdom'}) { $defdom=$ENV{'form.recdom'}; }
  871:       $r->print(
  872:                 '<form action="/adm/email"  name="compemail" method="post"'.
  873:                 ' enctype="multipart/form-data">'."\n".
  874:                 '<input type="hidden" name="sendmail" value="on" />'."\n".
  875:                 '<table>');
  876:     unless (($broadcast eq 'group') || ($broadcast eq 'upload')) {
  877: 	if ($replying) {
  878: 	    $r->print('<tr><td colspan="2">'.&mt('Replying to').' '.
  879: 		      &Apache::loncommon::aboutmewrapper(
  880: 							 &Apache::loncommon::plainname($content{'sendername'},$content{'senderdomain'}),$content{'sendername'},$content{'senderdomain'}).' ('.
  881: 		      $content{'sendername'}.'@'.
  882: 		      $content{'senderdomain'}.')'.
  883: 		      '<input type="hidden" name="recuname" value="'.$content{'sendername'}.'" />'.
  884: 		      '<input type="hidden" name="recdomain" value="'.$content{'senderdomain'}.'" />'.
  885: 		      '</td></tr>');
  886: 	} else {
  887: 	    my $domform = &Apache::loncommon::select_dom_form($defdom,'recdomain');
  888: 	    my $selectlink=&Apache::loncommon::selectstudent_link
  889: 	    ('compemail','recuname','recdomain');
  890: 	    $r->print(<<"ENDREC");
  891: <tr><td>$lt{'us'}:</td><td><input type="text" size="12" name="recuname" value="$ENV{'form.recname'}"></td><td rowspan="2">$selectlink</td></tr>
  892: <tr><td>$lt{'do'}:</td>
  893: <td>$domform</td></tr>
  894: ENDREC
  895:         }
  896:     }
  897:     my $latexHelp = Apache::loncommon::helpLatexCheatsheet();
  898:     if ($broadcast ne 'upload') {
  899:        $r->print(<<"ENDCOMP");
  900: <tr><td>$lt{'ad'}<br /><tt>username\@domain,username\@domain, ...
  901: </tt></td><td>
  902: <input type="text" size="50" name="additionalrec" /></td></tr>
  903: <tr><td>$lt{'sb'}:</td><td><input type="text" size="50" name="subject" value="$dissub" />
  904: </td></tr></table>
  905: $latexHelp
  906: <textarea name="message" cols="80" rows="15" wrap="hard">$dismsg
  907: </textarea></p><br />
  908: $dispcrit
  909: <input type="submit" name="send" value="$func $lt{'ma'}" />
  910: <input type="submit" name="cancel" value="$lt{'ca'}" />
  911: ENDCOMP
  912:     } else { # $broadcast is 'upload'
  913: 	$r->print(<<ENDUPLOAD);
  914: <input type="hidden" name="sendmode" value="upload" />
  915: <input type="hidden" name="send" value="on" />
  916: <h3>Generate messages from a file</h3>
  917: <p>
  918: Subject: <input type="text" size="50" name="subject" />
  919: </p>
  920: <p>General message text<br />
  921: <textarea name="message" cols="60" rows="10" wrap="hard">$dismsg
  922: </textarea></p>
  923: <p>
  924: The file format for the uploaded portion of the message is:
  925: <pre>
  926: username1\@domain1: text
  927: username2\@domain2: text
  928: username3\@domain1: text
  929: </pre>
  930: </p>
  931: <p>
  932: The messages will be assembled from all lines with the respective 
  933: <tt>username\@domain</tt>, and appended to the general message text.</p>
  934: <p>
  935: <input type="file" name="upfile" size="40" /></p><p>
  936: $dispcrit
  937: <input type="submit" value="Upload and Send" /></p>
  938: ENDUPLOAD
  939:     }
  940:     if ($broadcast eq 'group') {
  941:        &discourse;
  942:     }
  943:     $r->print('</form>');
  944: }
  945: 
  946: # ---------------------------------------------------- Display all face to face
  947: 
  948: sub disfacetoface {
  949:     my ($r,$user,$domain)=@_;
  950:     my $target=$ENV{'form.grade_target'};
  951:     unless ($ENV{'request.course.id'}) { return; }
  952:     unless (&Apache::lonnet::allowed('srm',$ENV{'request.course.id'})) {
  953: 	return;
  954:     }
  955:     my %records=&Apache::lonnet::dump('nohist_email',
  956: 			 $ENV{'course.'.$ENV{'request.course.id'}.'.domain'},
  957: 			 $ENV{'course.'.$ENV{'request.course.id'}.'.num'},
  958:                          '%255b'.$user.'%253a'.$domain.'%255d');
  959:     my $result='';
  960:     foreach (sort keys %records) {
  961:         my %content=&unpackagemsg($records{$_});
  962:         next if ($content{'senderdomain'} eq '');
  963:         $content{'message'}=~s/\n/\<br\>/g;
  964:         if ($content{'subject'}=~/^Record/) {
  965: 	    $result.='<h3>'.&mt('Record').'</h3>';
  966:         } else {
  967:             $result.='<h3>'.&mt('Sent Message').'</h3>';
  968:             %content=&unpackagemsg($content{'message'});
  969:             $content{'message'}=
  970:                 '<b>'.&mt('Subject').': '.$content{'subject'}.'</b><br />'.
  971: 		$content{'message'};
  972:         }
  973:         $result.=&mt('By').': <b>'.
  974: &Apache::loncommon::aboutmewrapper(
  975:  &Apache::loncommon::plainname($content{'sendername'},$content{'senderdomain'}),$content{'sendername'},$content{'senderdomain'}).'</b> ('.
  976: $content{'sendername'}.'@'.
  977:             $content{'senderdomain'}.') '.$content{'time'}.
  978:             '<br /><blockquote>'.
  979:               &Apache::lontexconvert::msgtexconverted($content{'message'}).
  980: 	      '</blockquote>';
  981:      }
  982:     # Check to see if there were any messages.
  983:     if ($result eq '') {
  984: 	if ($target ne 'tex') { 
  985: 	    $r->print("<p><b>".&mt("No notes, face-to-face discussion records, or critical messages in this course.")."</b></p>");
  986: 	} else {
  987: 	    $r->print('\textbf{'.&mt("No notes, face-to-face discussion records, or critical messages in this course.").'}\\\\');
  988: 	}
  989:     } else {
  990:        $r->print($result);
  991:     }
  992: }
  993: 
  994: # ---------------------------------------------------------------- Face to face
  995: 
  996: sub facetoface {
  997:     my ($r,$stage)=@_;
  998:     unless (&Apache::lonnet::allowed('srm',$ENV{'request.course.id'})) {
  999: 	return;
 1000:     }
 1001:     &printheader($r,
 1002: 		 '/adm/email?recordftf=query',
 1003: 		 "User Notes, Face-to-Face, Critical Messages");
 1004: # from query string
 1005: 
 1006:     if ($ENV{'form.recname'}) { $ENV{'form.recuname'}=$ENV{'form.recname'}; }
 1007:     if ($ENV{'form.recdom'}) { $ENV{'form.recdomain'}=$ENV{'form.recdom'}; }
 1008: 
 1009:     my $defdom=$ENV{'user.domain'};
 1010: # already filled in
 1011:     if ($ENV{'form.recdomain'}) { $defdom=$ENV{'form.recdomain'}; }
 1012: # generate output
 1013:     my $domform = &Apache::loncommon::select_dom_form($defdom,'recdomain');
 1014:     my $stdbrws = &Apache::loncommon::selectstudent_link
 1015: 	('stdselect','recuname','recdomain');
 1016:     my %lt=&Apache::lonlocal::texthash('user' => 'Username',
 1017: 				       'dom' => 'Domain',
 1018: 				       'head' => 'User Notes, Records of Face-To-Face Discussions, and Critical Messages in Course',
 1019: 				       'subm' => 'Retrieve discussion and message records',
 1020: 				       'newr' => 'New Record (record is visible to course faculty and staff)',
 1021: 				       'post' => 'Post this Record');
 1022:     $r->print(<<"ENDTREC");
 1023: <h3>$lt{'head'}</h3>
 1024: <form method="post" action="/adm/email" name="stdselect">
 1025: <input type="hidden" name="recordftf" value="retrieve" />
 1026: <table>
 1027: <tr><td>$lt{'user'}:</td><td><input type="text" size="12" name="recuname" value="$ENV{'form.recuname'}" /></td>
 1028: <td rowspan="2">
 1029: $stdbrws
 1030: <input type="submit" value="$lt{'subm'}" /></td>
 1031: </tr>
 1032: <tr><td>$lt{'dom'}:</td>
 1033: <td>$domform</td></tr>
 1034: </table>
 1035: </form>
 1036: ENDTREC
 1037:     if (($stage ne 'query') &&
 1038:         ($ENV{'form.recdomain'}) && ($ENV{'form.recuname'})) {
 1039:         chomp($ENV{'form.newrecord'});
 1040:         if ($ENV{'form.newrecord'}) {
 1041:            &user_normal_msg_raw(
 1042:             $ENV{'course.'.$ENV{'request.course.id'}.'.num'},
 1043:             $ENV{'course.'.$ENV{'request.course.id'}.'.domain'},
 1044:             &mt('Record').
 1045: 	     ' ['.$ENV{'form.recuname'}.':'.$ENV{'form.recdomain'}.']',
 1046: 	    $ENV{'form.newrecord'});
 1047:         }
 1048:         $r->print('<h3>'.&Apache::loncommon::plainname($ENV{'form.recuname'},
 1049: 				     $ENV{'form.recdomain'}).'</h3>');
 1050:         &disfacetoface($r,$ENV{'form.recuname'},$ENV{'form.recdomain'});
 1051: 	$r->print(<<ENDRHEAD);
 1052: <form method="post" action="/adm/email">
 1053: <input name="recdomain" value="$ENV{'form.recdomain'}" type="hidden" />
 1054: <input name="recuname" value="$ENV{'form.recuname'}" type="hidden" />
 1055: ENDRHEAD
 1056:         $r->print(<<ENDBFORM);
 1057: <hr />$lt{'newr'}<br />
 1058: <textarea name="newrecord" cols="80" rows="10" wrap="hard"></textarea>
 1059: <br />
 1060: <input type="hidden" name="recordftf" value="post" />
 1061: <input type="submit" value="$lt{'post'}" />
 1062: </form>
 1063: ENDBFORM
 1064:     }
 1065: }
 1066: 
 1067: # ----------------------------------------------------------- Blocking during exams
 1068: 
 1069: sub examblock {
 1070:     my ($r,$action) = @_;
 1071:     unless ($ENV{'request.course.id'}) { return;}
 1072:     unless (&Apache::lonnet::allowed('srm',$ENV{'request.course.id'})) { $r->print('Not allowed'); }
 1073:     my %lt=&Apache::lonlocal::texthash(
 1074:             'comb' => 'Communication Blocking',
 1075:             'cbds' => 'Communication blocking during scheduled exams',
 1076:             '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.',
 1077:              'mecb' => 'Modify existing communication blocking periods',
 1078:              'ncbc' => 'No communication blocks currently stored'
 1079:     );
 1080: 
 1081:     my %ltext = &Apache::lonlocal::texthash(
 1082:             'dura' => 'Duration',
 1083:             'setb' => 'Set by',
 1084:             'even' => 'Event',
 1085:             'actn' => 'Action',
 1086:             'star' => 'Start',
 1087:             'endd' => 'End'
 1088:     );
 1089: 
 1090:     &printheader($r,'/adm/email?block=display',$lt{'comb'});
 1091:     $r->print('<h3>'.$lt{'cbds'}.'</h3>');
 1092: 
 1093:     if ($action eq 'store') {
 1094:         &blockstore($r);
 1095:     }
 1096: 
 1097:     $r->print($lt{'desc'}.'<br /><br />
 1098:                <form name="blockform" method="post" action="/adm/email?block=store">
 1099:              ');
 1100: 
 1101:     $r->print('<h4>'.$lt{'mecb'}.'</h4>');
 1102:     my %records = ();
 1103:     my $blockcount = 0;
 1104:     my $parmcount = 0;
 1105:     &get_blockdates(\%records,\$blockcount);
 1106:     if ($blockcount > 0) {
 1107:         $parmcount = &display_blocker_status($r,\%records,\%ltext);
 1108:     } else {
 1109:         $r->print($lt{'ncbc'}.'<br /><br />');
 1110:     }
 1111:     &display_addblocker_table($r,$parmcount,\%ltext);
 1112:     $r->print(<<"END");
 1113: <br />
 1114: <input type="hidden" name="blocktotal" value="$blockcount" />
 1115: <input type ="submit" value="Save Changes" />
 1116: </form>
 1117: </body>
 1118: </html>
 1119: END
 1120:     return;
 1121: }
 1122: 
 1123: sub blockstore {
 1124:     my $r = shift;
 1125:     my %lt=&Apache::lonlocal::texthash(
 1126:             'tfcm' => 'The following changes were made',
 1127:             'cbps' => 'communication blocking period(s)',
 1128:             'werm' => 'was/were removed',
 1129:             'wemo' => 'was/were modified',
 1130:             'wead' => 'was/were added',
 1131:             'ncwm' => 'No changes were made.' 
 1132:     );
 1133:     my %adds = ();
 1134:     my %removals = ();
 1135:     my %cancels = ();
 1136:     my $modtotal = 0;
 1137:     my $canceltotal = 0;
 1138:     my $addtotal = 0;
 1139:     my %blocking = ();
 1140:     $r->print('<h3>'.$lt{'head'}.'</h3>');
 1141:     foreach (keys %ENV) {
 1142:         if ($_ =~ m/^form\.modify_(\w+)$/) {
 1143:             $adds{$1} = $1;
 1144:             $removals{$1} = $1;
 1145:             $modtotal ++;
 1146:         } elsif ($_ =~ m/^form\.cancel_(\d+)$/) {
 1147:             $cancels{$1} = $1;
 1148:             unless ( defined($removals{$1}) ) {
 1149:                 $removals{$1} = $1;
 1150:                 $canceltotal ++;
 1151:             }
 1152:         } elsif ($_ =~ m/^form\.add_(\d+)$/) {
 1153:             $adds{$1} = $1;
 1154:             $addtotal ++;
 1155:         }
 1156:     }
 1157: 
 1158:     foreach (keys %removals) {
 1159:         my $hashkey = $ENV{'form.key_'.$_};
 1160:         &Apache::lonnet::del('comm_block',["$hashkey"],
 1161:                          $ENV{'course.'.$ENV{'request.course.id'}.'.domain'},
 1162:                          $ENV{'course.'.$ENV{'request.course.id'}.'.num'}
 1163:                          );
 1164:     }
 1165:     foreach (keys %adds) {
 1166:         unless ( defined($cancels{$_}) ) {
 1167:             my ($newstart,$newend) = &get_dates_from_form($_);
 1168:             my $newkey = $newstart.'____'.$newend;
 1169:             $blocking{$newkey} = $ENV{'user.name'}.'@'.$ENV{'user.domain'}.':'.$ENV{'form.title_'.$_};
 1170:         }
 1171:     }
 1172:     if ($addtotal + $modtotal > 0) {
 1173:         &Apache::lonnet::put('comm_block',\%blocking,
 1174:                      $ENV{'course.'.$ENV{'request.course.id'}.'.domain'},
 1175:                      $ENV{'course.'.$ENV{'request.course.id'}.'.num'}
 1176:                      );
 1177:     }
 1178:     my $chgestotal = $canceltotal + $modtotal + $addtotal;
 1179:     if ($chgestotal > 0) {
 1180:         $r->print($lt{'tfcm'}.'<ul>');
 1181:         if ($canceltotal > 0) {
 1182:             $r->print('<li>'.$canceltotal.' '.$lt{'cbps'},' '.$lt{'werm'}.'</li>');
 1183:         }
 1184:         if ($modtotal > 0) {
 1185:             $r->print('<li>'.$modtotal.' '.$lt{'cbps'},' '.$lt{'wemo'}.'</li>');
 1186:         }
 1187:         if ($addtotal > 0) {
 1188:             $r->print('<li>'.$addtotal.' '.$lt{'cbps'},' '.$lt{'wead'}.'</li>');
 1189:         }
 1190:         $r->print('</ul>');
 1191:     } else {
 1192:         $r->print($lt{'ncwm'});
 1193:     }
 1194:     $r->print('<br />');
 1195:     return;
 1196: }
 1197: 
 1198: sub get_dates_from_form {
 1199:     my $item = shift;
 1200:     my $startdate = &Apache::lonhtmlcommon::get_date_from_form('startdate_'.$item);
 1201:     my $enddate   = &Apache::lonhtmlcommon::get_date_from_form('enddate_'.$item);
 1202:     return ($startdate,$enddate);
 1203: }
 1204: 
 1205: sub get_blockdates {
 1206:     my ($records,$blockcount) = @_;
 1207:     $$blockcount = 0;
 1208:     %{$records} = &Apache::lonnet::dump('comm_block',
 1209:                          $ENV{'course.'.$ENV{'request.course.id'}.'.domain'},
 1210:                          $ENV{'course.'.$ENV{'request.course.id'}.'.num'}
 1211:                          );
 1212:     $$blockcount = keys %{$records};
 1213:                                                                                                              
 1214:     foreach (keys %{$records}) {
 1215:         if ($_ eq 'error: 2 tie(GDBM) Failed while attempting dump') {
 1216:             $$blockcount = 0;
 1217:             last;
 1218:         }
 1219:     }
 1220: }
 1221: 
 1222: sub display_blocker_status {
 1223:     my ($r,$records,$ltext) = @_;
 1224:     my $parmcount = 0;
 1225:     my @bgcols = ("#eeeeee","#dddddd");
 1226:     my $function = &Apache::loncommon::get_users_function();
 1227:     my $color = &Apache::loncommon::designparm($function.'.tabbg',
 1228:                                                     $ENV{'user.domain'});
 1229:     my %lt = &Apache::lonlocal::texthash(
 1230:         'modi' => 'Modify',
 1231:         'canc' => 'Cancel',
 1232:     );
 1233:     $r->print(<<"END");
 1234: <table border="0" cellpadding="0" cellspacing="0">
 1235:  <tr>
 1236:   <td width="100%" bgcolor="#000000">
 1237:    <table width="100%" border="0" cellpadding="1" cellspacing="0">
 1238:     <tr>
 1239:      <td width="100%" bgcolor="#000000">
 1240:       <table border="0" cellpadding="3" cellspacing="3" bgcolor="#FFFFFF">
 1241:        <tr bgcolor="$color">
 1242:         <td><b>$$ltext{'dura'}</b></td>
 1243:         <td><b>$$ltext{'setb'}</b></td>
 1244:         <td><b>$$ltext{'even'}</b></td>
 1245:         <td><b>$$ltext{'actn'}?</b></td>
 1246:        </tr>
 1247: END
 1248:     foreach (sort keys %{$records}) {
 1249:         my $iter = $parmcount%2;
 1250:         my $onchange = 'onFocus="javascript:window.document.forms['.
 1251:                        "'blockform'].elements['modify_".$parmcount."'].".
 1252:                        'checked=true;"';
 1253:         my ($start,$end) = split/____/,$_;
 1254:         my $startform = &Apache::lonhtmlcommon::date_setter('blockform','startdate_'.$parmcount,$start,$onchange);
 1255:         my $endform = &Apache::lonhtmlcommon::date_setter('blockform','enddate_'.$parmcount,$end,$onchange);
 1256:         my ($setter,$title) = split/:/,$$records{$_};
 1257:         my ($setuname,$setudom) = split/@/,$setter;
 1258:         my $settername = &Apache::loncommon::plainname($setuname,$setudom);
 1259:         $r->print(<<"END");
 1260:        <tr bgcolor="$bgcols[$iter]">
 1261:         <td>$$ltext{'star'}:&nbsp;$startform<br/>$$ltext{'endd'}:&nbsp;&nbsp;$endform</td>
 1262:         <td>$settername</td>
 1263:         <td><input type="text" name="title_$parmcount" size="15" value="$title"/><input type="hidden" name="key_$parmcount" value="$_"></td>
 1264:         <td>$lt{'modi'}?&nbsp;<input type="checkbox" name="modify_$parmcount"/><br />$lt{'canc'}?&nbsp;&nbsp;<input type="checkbox" name="cancel_$parmcount"/>
 1265:        </tr>
 1266: END
 1267:         $parmcount ++;
 1268:     }
 1269:     $r->print(<<"END");
 1270:       </table>
 1271:      </td>
 1272:     </tr>
 1273:    </table>
 1274:   </td>
 1275:  </tr>
 1276: </table>
 1277: <br />
 1278: <br />
 1279: END
 1280:     return $parmcount;
 1281: }
 1282: 
 1283: sub display_addblocker_table {
 1284:     my ($r,$parmcount,$ltext) = @_;
 1285:     my $start = time;
 1286:     my $end = $start + (60 * 60 * 2); #Default is an exam of 2 hours duration.
 1287:     my $onchange = 'onFocus="javascript:window.document.forms['.
 1288:                    "'blockform'].elements['add_".$parmcount."'].".
 1289:                    'checked=true;"';
 1290:     my $startform = &Apache::lonhtmlcommon::date_setter('blockform','startdate_'.$parmcount,$start,$onchange);
 1291:     my $endform = &Apache::lonhtmlcommon::date_setter('blockform','enddate_'.$parmcount,$end,$onchange);
 1292:     my $function = &Apache::loncommon::get_users_function();
 1293:     my $color = &Apache::loncommon::designparm($function.'.tabbg',
 1294:                                                     $ENV{'user.domain'});
 1295:     my %lt = &Apache::lonlocal::texthash(
 1296:         'addb' => 'Add block',
 1297:         'exam' => 'e.g., Exam 1',
 1298:         'addn' => 'Add new communication blocking periods'
 1299:     );
 1300:     $r->print(<<"END");
 1301: <h4>$lt{'addn'}</h4> 
 1302: <table border="0" cellpadding="0" cellspacing="0">
 1303:  <tr>
 1304:   <td width="100%" bgcolor="#000000">
 1305:    <table width="100%" border="0" cellpadding="1" cellspacing="0">
 1306:     <tr>
 1307:      <td width="100%" bgcolor="#000000">
 1308:       <table border="0" cellpadding="3" cellspacing="3" bgcolor="#FFFFFF">
 1309:        <tr bgcolor="#CCCCFF">
 1310:         <td><b>$$ltext{'dura'}</b></td>
 1311:         <td><b>$$ltext{'even'} $lt{'exam'}</b></td>
 1312:         <td><b>$$ltext{'actn'}?</b></td>
 1313:        </tr>
 1314:        <tr bgcolor="#eeeeee">
 1315:         <td>$$ltext{'star'}:&nbsp;$startform<br />$$ltext{'endd'}:&nbsp;&nbsp;$endform</td>
 1316:         <td><input type="text" name="title_$parmcount" size="15" value=""/></td>
 1317:         <td>$lt{'addb'}?&nbsp;<input type="checkbox" name="add_$parmcount" value="1"/></td>
 1318:        </tr>
 1319:       </table>
 1320:      </td>
 1321:     </tr>
 1322:    </table>
 1323:   </td>
 1324:  </tr>
 1325: </table>
 1326: END
 1327:     return;
 1328: }
 1329: 
 1330: sub blockcheck {
 1331:     my ($setters,$startblock,$endblock) = @_;
 1332:     # Retrieve active student roles and active course coordinator/instructor roles
 1333:     my @livecses = ();
 1334:     my @staffcses = ();
 1335:     $$startblock = 0;
 1336:     $$endblock = 0;
 1337:     foreach (keys %ENV) {
 1338:         if ($_ =~ m-^user\.role\.(st|cc|in)\./(.+)$-) {
 1339:             my $role = $1;
 1340:             my $cse = $2;
 1341:             $cse =~ s|/|_|;
 1342:             if ($ENV{$_} =~ m/^(\d*)\.(\d*)$/) {
 1343:                 unless (($2 > 0 && $2 < time) || ($1 > time)) {
 1344:                     if ($role eq 'st') {
 1345:                         push @livecses, $cse;
 1346:                     } else {
 1347:                         unless (grep/^$cse$/,@staffcses) {
 1348:                             push @staffcses, $cse;
 1349:                         }
 1350:                     }
 1351:                 }
 1352:             }
 1353:         } elsif ($_ =~ m-user\.role\.cr/(\w+)/(\w+)/([^/]+)\./(.+)$- ) { 
 1354:             my $rolepriv = $ENV{'user.role..rolesdef_'.$3};
 1355:         }
 1356:     }
 1357:     # Retrieve blocking times and identity of blocker for active courses for students.
 1358:     if (@livecses > 0) {
 1359:         foreach my $cse (@livecses) {
 1360:             my ($cdom,$crs) = split/_/,$cse;
 1361:             if ( (grep/^$cse$/,@staffcses) && ($ENV{'request.role'} !~ m-^st\./$cdom/$crs$-) ) {
 1362:                 next;
 1363:             } else {
 1364:                 %{$$setters{$cse}} = ();
 1365:                 @{$$setters{$cse}{'staff'}} = ();
 1366:                 @{$$setters{$cse}{'times'}} = ();
 1367:                 my %records = &Apache::lonnet::dump('comm_block',$cdom,$crs);
 1368:                 foreach (keys %records) {
 1369:                     if ($_ =~ m/^(\d+)____(\d+)$/) {
 1370:                         if ($1 <= time && $2 >= time) {
 1371:                             my ($staff,$title) = split/:/,$records{$_};
 1372:                             push @{$$setters{$cse}{'staff'}}, $staff;
 1373:                             push @{$$setters{$cse}{'times'}}, $_;
 1374:                             if ( ($$startblock == 0) || ($$startblock > $1) ) {
 1375:                                 $$startblock = $1;
 1376:                             }
 1377:                             if ( ($$endblock == 0) || ($$endblock < $2) ) {
 1378:                                 $$endblock = $2;
 1379:                             }
 1380:                         }
 1381:                     }
 1382:                 }
 1383:             }
 1384:         }
 1385:     }
 1386: }
 1387: 
 1388: sub build_block_table {
 1389:     my ($r,$startblock,$endblock,$setters) = @_;
 1390:     my $function = &Apache::loncommon::get_users_function();
 1391:     my $color = &Apache::loncommon::designparm($function.'.tabbg',
 1392:                                                     $ENV{'user.domain'});
 1393:     my %lt = &Apache::lonlocal::texthash(
 1394:         'cacb' => 'Currently active communication blocks',
 1395:         'cour' => 'Course',
 1396:         'dura' => 'Duration',
 1397:         'blse' => 'Block set by'
 1398:     ); 
 1399:     $r->print(<<"END");
 1400: <br /<br />$lt{'cacb'}:<br /><br />
 1401: <table border="0" cellpadding="0" cellspacing="0">
 1402:  <tr>
 1403:   <td width="100%" bgcolor="#000000">
 1404:    <table width="100%" border="0" cellpadding="1" cellspacing="0">
 1405:     <tr>
 1406:      <td width="100%" bgcolor="#000000">
 1407:       <table border="0" cellpadding="3" cellspacing="3" bgcolor="#FFFFFF">
 1408:        <tr bgcolor="$color">
 1409:         <td><b>$lt{'cour'}</b></td>
 1410:         <td><b>$lt{'dura'}</b></td>
 1411:         <td><b>$lt{'blse'}</b></td>
 1412:        </tr>
 1413: END
 1414:     foreach (keys %{$setters}) {
 1415:         my %courseinfo=&Apache::lonnet::coursedescription($_);
 1416:         for (my $i=0; $i<@{$$setters{$_}{staff}}; $i++) {
 1417:             my ($uname,$udom) = split/\@/,$$setters{$_}{staff}[$i];
 1418:             my $fullname = &Apache::loncommon::plainname($uname,$udom);
 1419:             my ($openblock,$closeblock) = split/____/,$$setters{$_}{times}[$i];
 1420:             $openblock = &Apache::lonlocal::locallocaltime($openblock);
 1421:             $closeblock= &Apache::lonlocal::locallocaltime($closeblock);
 1422:             $r->print('<tr><td>'.$courseinfo{'description'}.'</td>'.
 1423:                       '<td>'.$openblock.' to '.$closeblock.'</td>'.
 1424:                       '<td>'.$fullname.' ('.$uname.'@'.$udom.
 1425:                       ')</td></tr>');
 1426:         }
 1427:     }
 1428:     $r->print('</table></td></tr></table></td></tr></table>');
 1429: }
 1430: 
 1431: # ----------------------------------------------------------- Display a message
 1432: 
 1433: sub displaymessage {
 1434:     my ($r,$msgid)=@_;
 1435:     my %blocked = ();
 1436:     my %setters = ();
 1437:     my $startblock = 0;
 1438:     my $endblock = 0;
 1439:     my $numblocked = 0;
 1440: # info to generate "next" and "previous" buttons and check if message is blocked
 1441:     &blockcheck(\%setters,\$startblock,\$endblock);
 1442:     my @messages=&sortedmessages(\%blocked,$startblock,$endblock,\$numblocked);
 1443:     if ( $blocked{$msgid} eq 'ON' ) {
 1444:         &printheader($r,'/adm/email',&mt('Display a Message'));
 1445:         $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.'));
 1446:         &build_block_table($r,$startblock,$endblock,\%setters);
 1447:         return;
 1448:     }
 1449:     &statuschange($msgid,'read');
 1450:     my %message=&Apache::lonnet::get('nohist_email',[$msgid]);
 1451:     my %content=&unpackagemsg($message{$msgid});
 1452:     my $counter=0;
 1453:     $r->print('<pre>');
 1454:     my $escmsgid=&Apache::lonnet::escape($msgid);
 1455:     foreach (@messages) {
 1456: 	if ($_->[5] eq $escmsgid){
 1457: 	    last;
 1458: 	}
 1459: 	$counter++;
 1460:     }
 1461:     $r->print('</pre>');
 1462:     my $number_of_messages = scalar(@messages); #subtract 1 for last index
 1463: # start output
 1464:     &printheader($r,'/adm/email?display='.&Apache::lonnet::escape($msgid),'Display a Message','',$content{'baseurl'});
 1465:     my %courseinfo=&Apache::lonnet::coursedescription($content{'courseid'});
 1466: # Functions
 1467:     $r->print('<table border="2" width="100%"><tr bgcolor="#FFFFAA"><td>'.&mt('Functions').':</td>'.
 1468: 	      '<td><a href="/adm/email?replyto='.&Apache::lonnet::escape($msgid).$sqs.
 1469: 	      '"><b>'.&mt('Reply').'</b></a></td>'.
 1470: 	      '<td><a href="/adm/email?forward='.&Apache::lonnet::escape($msgid).$sqs.
 1471: 	      '"><b>'.&mt('Forward').'</b></a></td>'.
 1472: 	      '<td><a href="/adm/email?markunread='.&Apache::lonnet::escape($msgid).$sqs.
 1473: 	      '"><b>'.&mt('Mark Unread').'</b></a></td>'.
 1474: 	      '<td><a href="/adm/email?markdel='.&Apache::lonnet::escape($msgid).$sqs.
 1475: 	      '"><b>Delete</b></a></td>'.
 1476: 	      '<td><a href="/adm/email?sortedby='.$ENV{'form.sortedby'}.
 1477: 	      '"><b>'.&mt('Display all Messages').'</b></a></td>');
 1478:     if ($counter > 0){
 1479: 	$r->print('<td><a href="/adm/email?display='.$messages[$counter-1]->[5].$sqs.
 1480: 		  '"><b>'.&mt('Previous').'</b></a></td>');
 1481:     }
 1482:     if ($counter < $number_of_messages - 1){
 1483: 	$r->print('<td><a href="/adm/email?display='.$messages[$counter+1]->[5].$sqs.
 1484: 		  '"><b>'.&mt('Next').'</b></a></td>');
 1485:     }
 1486:     $r->print('</tr></table>');
 1487:     $r->print('<br /><b>'.&mt('Subject').':</b> '.$content{'subject'}.
 1488: 	      '<br /><b>'.&mt('From').':</b> '.
 1489: 	      &Apache::loncommon::aboutmewrapper(
 1490: 						 &Apache::loncommon::plainname($content{'sendername'},$content{'senderdomain'}),
 1491: 						 $content{'sendername'},$content{'senderdomain'}).' ('.
 1492: 	      $content{'sendername'}.' at '.
 1493: 	      $content{'senderdomain'}.') '.
 1494: 	      ($content{'courseid'}?'<br /><b>'.&mt('Course').':</b> '.$courseinfo{'description'}.
 1495: 	       ($content{'coursesec'}?' ('.&mt('Group/Section').': '.$content{'coursesec'}.')':''):'').
 1496: 	      '<br /><b>'.&mt('Time').':</b> '.$content{'time'}.
 1497: 	      '<p><pre>'.
 1498: 	      &Apache::lontexconvert::msgtexconverted($content{'message'},1).
 1499: 	      '</pre><hr />'.$content{'citation'}.'</p>');
 1500:     return;   
 1501: }
 1502: 
 1503: # ================================================================== The Header
 1504: 
 1505: sub header {
 1506:     my ($r,$title,$baseurl)=@_;
 1507:     $r->print('<html><head><title>Communication and Messages</title>');
 1508:     if ($baseurl) {
 1509: 	$r->print("<base href=\"http://$ENV{'SERVER_NAME'}/$baseurl\" />");
 1510:     }
 1511:     $r->print(&Apache::loncommon::studentbrowser_javascript().'</head>'.
 1512: 	      &Apache::loncommon::bodytag('Communication and Messages'));
 1513:         $r->print(&Apache::lonhtmlcommon::breadcrumbs
 1514:                   (undef,($title?$title:'Communication and Messages')));
 1515: 
 1516: }
 1517: 
 1518: # ---------------------------------------------------------------- Print header
 1519: 
 1520: sub printheader {
 1521:     my ($r,$url,$desc,$title,$baseurl)=@_;
 1522:     &Apache::lonhtmlcommon::add_breadcrumb
 1523: 	({href=>$url,
 1524: 	  text=>$desc});
 1525:     &header($r,$title,$baseurl);
 1526: }
 1527: 
 1528: 
 1529: # ===================================================================== Handler
 1530: 
 1531: sub handler {
 1532:     my $r=shift;
 1533: 
 1534: # ----------------------------------------------------------- Set document type
 1535:     
 1536:     &Apache::loncommon::content_type($r,'text/html');
 1537:     $r->send_http_header;
 1538:     
 1539:     return OK if $r->header_only;
 1540:     
 1541: # --------------------------- Get query string for limited number of parameters
 1542:     &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'},
 1543:         ['display','replyto','forward','markread','markdel','markunread',
 1544:          'sendreply','compose','sendmail','critical','recname','recdom',
 1545:          'recordftf','sortedby','block']);
 1546:     $sqs='&sortedby='.$ENV{'form.sortedby'};
 1547: # ------------------------------------------------------ They checked for email
 1548:     unless ($ENV{'form.block'}) {
 1549:         &Apache::lonnet::put('email_status',{'recnewemail'=>0});
 1550:     }
 1551: 
 1552: # ----------------------------------------------------------------- Breadcrumbs
 1553: 
 1554:     &Apache::lonhtmlcommon::clear_breadcrumbs();
 1555:     &Apache::lonhtmlcommon::add_breadcrumb
 1556:         ({href=>"/adm/communicate",
 1557:           text=>"Communication/Messages",
 1558:           faq=>12,bug=>'Communication Tools',});
 1559: 
 1560: # --------------------------------------------------------------- Render Output
 1561: 
 1562:     if ($ENV{'form.display'}) {
 1563: 	&displaymessage($r,$ENV{'form.display'});
 1564:     } elsif ($ENV{'form.replyto'}) {
 1565: 	&compout($r,'',$ENV{'form.replyto'});
 1566:     } elsif ($ENV{'form.confirm'}) {
 1567: 	&printheader($r,'','Confirmed Receipt');
 1568: 	foreach (keys %ENV) {
 1569: 	    if ($_=~/^form\.rec\_(.*)$/) {
 1570: 		$r->print('<b>'.&mt('Confirming Receipt').':</b> '.
 1571: 			  &user_crit_received($1).'<br>');
 1572: 	    }
 1573: 	    if ($_=~/^form\.reprec\_(.*)$/) {
 1574: 		my $msgid=$1;
 1575: 		$r->print('<b>'.&mt('Confirming Receipt').':</b> '.
 1576: 			  &user_crit_received($msgid).'<br>');
 1577: 		&compout($r,'','','',$msgid);
 1578: 	    }
 1579: 	}
 1580: 	&discrit($r);
 1581:     } elsif ($ENV{'form.critical'}) {
 1582: 	&printheader($r,'','Displaying Critical Messages');
 1583: 	&discrit($r);
 1584:     } elsif ($ENV{'form.forward'}) {
 1585: 	&compout($r,$ENV{'form.forward'});
 1586:     } elsif ($ENV{'form.markdel'}) {
 1587: 	&printheader($r,'','Deleted Message');
 1588: 	&statuschange($ENV{'form.markdel'},'deleted');
 1589: 	&disall($r);
 1590:     } elsif ($ENV{'form.markeddel'}) {
 1591: 	my $total=0;
 1592: 	foreach (keys %ENV) {
 1593: 	    if ($_=~/^form\.delmark_(.*)$/) {
 1594: 		&statuschange(&Apache::lonnet::unescape($1),'deleted');
 1595: 		$total++;
 1596: 	    }
 1597: 	}
 1598: 	&printheader($r,'','Deleted Messages');
 1599: 	$r->print('Deleted '.$total.' message(s)<p>');
 1600: 	&disall($r);
 1601:     } elsif ($ENV{'form.markunread'}) {
 1602: 	&printheader($r,'','Marked Message as Unread');
 1603: 	&statuschange($ENV{'form.markunread'},'new');
 1604: 	&disall($r);
 1605:     } elsif ($ENV{'form.compose'}) {
 1606: 	&compout($r,'','',$ENV{'form.compose'});
 1607:     } elsif ($ENV{'form.recordftf'}) {
 1608: 	&facetoface($r,$ENV{'form.recordftf'});
 1609:     } elsif ($ENV{'form.block'}) {
 1610:         &examblock($r,$ENV{'form.block'});
 1611:     } elsif ($ENV{'form.sendmail'}) {
 1612: 	my $sendstatus='';
 1613: 	if ($ENV{'form.send'}) {
 1614: 	    &printheader($r,'','Messages being sent.');
 1615: 	    $r->rflush();
 1616: 	    my %content=();
 1617: 	    undef %content;
 1618: 	    if ($ENV{'form.forwid'}) {
 1619: 		my $msgid=$ENV{'form.forwid'};
 1620: 		my %message=&Apache::lonnet::get('nohist_email',[$msgid]);
 1621: 		%content=&unpackagemsg($message{$msgid},1);
 1622: 		&statuschange($msgid,'forwarded');
 1623: 		$ENV{'form.message'}.="\n\n-- Forwarded message --\n\n".
 1624: 		    $content{'message'};
 1625: 	    }
 1626: 	    my %toaddr=();
 1627: 	    undef %toaddr;
 1628: 	    if ($ENV{'form.sendmode'} eq 'group') {
 1629: 		foreach (keys %ENV) {
 1630: 		    if ($_=~/^form\.send\_to\_\&\&\&[^\&]*\&\&\&\_(.+)$/) {
 1631: 			$toaddr{$1}='';
 1632: 		    }
 1633: 		}
 1634: 	    } elsif ($ENV{'form.sendmode'} eq 'upload') {
 1635: 		foreach (split(/[\n\r\f]+/,$ENV{'form.upfile'})) {
 1636: 		    my ($rec,$txt)=split(/\s*\:\s*/,$_);
 1637: 		    if ($txt) {
 1638: 			$rec=~s/\@/\:/;
 1639: 			$toaddr{$rec}.=$txt."\n";
 1640: 		    }
 1641: 		}
 1642: 	    } else {
 1643: 		$toaddr{$ENV{'form.recuname'}.':'.$ENV{'form.recdomain'}}='';
 1644: 	    }
 1645: 	    if ($ENV{'form.additionalrec'}) {
 1646: 		foreach (split(/\,/,$ENV{'form.additionalrec'})) {
 1647: 		    my ($auname,$audom)=split(/\@/,$_);
 1648: 		    $toaddr{$auname.':'.$audom}='';
 1649: 		}
 1650: 	    }
 1651: 
 1652: 	    foreach (keys %toaddr) {
 1653: 		my ($recuname,$recdomain)=split(/\:/,$_);
 1654: 		my $msgtxt=&Apache::lonfeedback::clear_out_html($ENV{'form.message'});
 1655: 		if ($toaddr{$_}) { $msgtxt.='<hr />'.$toaddr{$_}; }
 1656: 		my $thismsg;    
 1657: 		if ((($ENV{'form.critmsg'}) || ($ENV{'form.sendbck'})) && 
 1658: 		    (&Apache::lonnet::allowed('srm',$ENV{'request.course.id'}))) {
 1659: 		    $r->print(&mt('Sending critical message').' '.$recuname.'@'.$recdomain.': ');
 1660: 		    $thismsg=&user_crit_msg($recuname,$recdomain,
 1661: 						    &Apache::lonfeedback::clear_out_html($ENV{'form.subject'}),
 1662: 						    $msgtxt,
 1663: 						    $ENV{'form.sendbck'});
 1664: 		} else {
 1665: 		    $r->print(&mt('Sending').' '.$recuname.'@'.$recdomain.': ');
 1666: 		    $thismsg=&user_normal_msg($recuname,$recdomain,
 1667: 						      &Apache::lonfeedback::clear_out_html($ENV{'form.subject'}),
 1668: 						      $msgtxt,
 1669: 						      $content{'citation'});
 1670: 		}
 1671: 		$r->print($thismsg.'<br />');
 1672: 		$sendstatus.=' '.$thismsg;
 1673: 	    }
 1674: 	} else {
 1675: 	    &printheader($r,'','No messages sent.'); 
 1676: 	}
 1677: 	if ($sendstatus=~/^(\s*(?:ok|con_delayed)\s*)*$/) {
 1678: 	    $r->print('<br /><font color="green">'.&mt('Completed.').'</font>');
 1679: 	    if ($ENV{'form.displayedcrit'}) {
 1680: 		&discrit($r);
 1681: 	    } else {
 1682: 		&Apache::loncommunicate::menu($r);
 1683: 	    }
 1684: 	} else {
 1685: 	    $r->print(
 1686: 		      '<h2><font color="red">'.&mt('Could not deliver message').'</font></h2>'.
 1687: 		      &mt('Please use the browser "Back" button and correct the recipient addresses')
 1688: 		      );
 1689: 	}
 1690:     } else {
 1691: 	&printheader($r,'','Display All Messages');
 1692: 	&disall($r);
 1693:     }
 1694:     $r->print('</body></html>');
 1695:     return OK;
 1696: }
 1697: # ================================================= Main program, reset counter
 1698: 
 1699: BEGIN {
 1700:     $msgcount=0;
 1701: }
 1702: 
 1703: =pod
 1704: 
 1705: =back
 1706: 
 1707: =cut
 1708: 
 1709: 1; 
 1710: 
 1711: __END__
 1712: 
 1713: 
 1714: 
 1715: 
 1716: 
 1717: 
 1718: 

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