Annotation of loncom/interface/lonmsg.pm, revision 1.105

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

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