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

1.1       www         1: # The LearningOnline Network with CAPA
1.26      albertel    2: # Routines for messaging
                      3: #
1.103   ! albertel    4: # $Id: lonmsg.pm,v 1.102 2004/06/02 20:40:07 raeburn 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});
                    864: 	$dispcrit.='<input type="hidden" name="forwid" value="'.
                    865: 	    $forwarding.'" />';
                    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: 
                    952: sub disfacetoface {
                    953:     my ($r,$user,$domain)=@_;
1.98      sakharuk  954:     my $target=$ENV{'form.grade_target'};
1.45      www       955:     unless ($ENV{'request.course.id'}) { return; }
                    956:     unless (&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:         $content{'message'}=~s/\n/\<br\>/g;
                    968:         if ($content{'subject'}=~/^Record/) {
1.69      www       969: 	    $result.='<h3>'.&mt('Record').'</h3>';
1.102     raeburn   970:         } elsif ($content{'subject'}=~/^Broadcast/) {
                    971:             $result .='<h3>'.&mt('Broadcast Message').'</h3>';
1.45      www       972:         } else {
1.102     raeburn   973:             $result.='<h3>'.&mt('Critical Message').'</h3>';
1.45      www       974:             %content=&unpackagemsg($content{'message'});
                    975:             $content{'message'}=
1.92      www       976:                 '<b>'.&mt('Subject').': '.$content{'subject'}.'</b><br />'.
1.45      www       977: 		$content{'message'};
                    978:         }
1.69      www       979:         $result.=&mt('By').': <b>'.
1.45      www       980: &Apache::loncommon::aboutmewrapper(
                    981:  &Apache::loncommon::plainname($content{'sendername'},$content{'senderdomain'}),$content{'sendername'},$content{'senderdomain'}).'</b> ('.
                    982: $content{'sendername'}.'@'.
                    983:             $content{'senderdomain'}.') '.$content{'time'}.
1.92      www       984:             '<br /><blockquote>'.
1.45      www       985:               &Apache::lontexconvert::msgtexconverted($content{'message'}).
                    986: 	      '</blockquote>';
                    987:      }
                    988:     # Check to see if there were any messages.
                    989:     if ($result eq '') {
1.98      sakharuk  990: 	if ($target ne 'tex') { 
1.102     raeburn   991: 	    $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  992: 	} else {
1.102     raeburn   993: 	    $r->print('\textbf{'.&mt("No notes, face-to-face discussion records, critical messages or broadcast messages in this course.").'}\\\\');
1.98      sakharuk  994: 	}
1.45      www       995:     } else {
                    996:        $r->print($result);
                    997:     }
                    998: }
                    999: 
1.44      www      1000: # ---------------------------------------------------------------- Face to face
                   1001: 
                   1002: sub facetoface {
                   1003:     my ($r,$stage)=@_;
                   1004:     unless (&Apache::lonnet::allowed('srm',$ENV{'request.course.id'})) {
                   1005: 	return;
                   1006:     }
1.89      www      1007:     &printheader($r,
                   1008: 		 '/adm/email?recordftf=query',
1.102     raeburn  1009: 		 "User Notes, Face-to-Face, Critical Messages, Broadcast Messages");
1.46      www      1010: # from query string
1.88      www      1011: 
1.46      www      1012:     if ($ENV{'form.recname'}) { $ENV{'form.recuname'}=$ENV{'form.recname'}; }
                   1013:     if ($ENV{'form.recdom'}) { $ENV{'form.recdomain'}=$ENV{'form.recdom'}; }
                   1014: 
1.44      www      1015:     my $defdom=$ENV{'user.domain'};
1.46      www      1016: # already filled in
1.44      www      1017:     if ($ENV{'form.recdomain'}) { $defdom=$ENV{'form.recdomain'}; }
1.46      www      1018: # generate output
1.44      www      1019:     my $domform = &Apache::loncommon::select_dom_form($defdom,'recdomain');
1.46      www      1020:     my $stdbrws = &Apache::loncommon::selectstudent_link
                   1021: 	('stdselect','recuname','recdomain');
1.88      www      1022:     my %lt=&Apache::lonlocal::texthash('user' => 'Username',
                   1023: 				       'dom' => 'Domain',
1.102     raeburn  1024: 				       'head' => 'User Notes, Records of Face-To-Face Discussions, Critical Messages, and Broadcast Messages in Course',
1.88      www      1025: 				       'subm' => 'Retrieve discussion and message records',
                   1026: 				       'newr' => 'New Record (record is visible to course faculty and staff)',
                   1027: 				       'post' => 'Post this Record');
1.44      www      1028:     $r->print(<<"ENDTREC");
1.88      www      1029: <h3>$lt{'head'}</h3>
1.46      www      1030: <form method="post" action="/adm/email" name="stdselect">
1.44      www      1031: <input type="hidden" name="recordftf" value="retrieve" />
                   1032: <table>
1.88      www      1033: <tr><td>$lt{'user'}:</td><td><input type="text" size="12" name="recuname" value="$ENV{'form.recuname'}" /></td>
1.44      www      1034: <td rowspan="2">
1.46      www      1035: $stdbrws
1.88      www      1036: <input type="submit" value="$lt{'subm'}" /></td>
1.44      www      1037: </tr>
1.88      www      1038: <tr><td>$lt{'dom'}:</td>
1.44      www      1039: <td>$domform</td></tr>
                   1040: </table>
                   1041: </form>
                   1042: ENDTREC
                   1043:     if (($stage ne 'query') &&
                   1044:         ($ENV{'form.recdomain'}) && ($ENV{'form.recuname'})) {
                   1045:         chomp($ENV{'form.newrecord'});
                   1046:         if ($ENV{'form.newrecord'}) {
1.45      www      1047:            &user_normal_msg_raw(
                   1048:             $ENV{'course.'.$ENV{'request.course.id'}.'.num'},
                   1049:             $ENV{'course.'.$ENV{'request.course.id'}.'.domain'},
1.88      www      1050:             &mt('Record').
                   1051: 	     ' ['.$ENV{'form.recuname'}.':'.$ENV{'form.recdomain'}.']',
1.45      www      1052: 	    $ENV{'form.newrecord'});
1.44      www      1053:         }
1.46      www      1054:         $r->print('<h3>'.&Apache::loncommon::plainname($ENV{'form.recuname'},
                   1055: 				     $ENV{'form.recdomain'}).'</h3>');
1.45      www      1056:         &disfacetoface($r,$ENV{'form.recuname'},$ENV{'form.recdomain'});
1.44      www      1057: 	$r->print(<<ENDRHEAD);
                   1058: <form method="post" action="/adm/email">
                   1059: <input name="recdomain" value="$ENV{'form.recdomain'}" type="hidden" />
                   1060: <input name="recuname" value="$ENV{'form.recuname'}" type="hidden" />
                   1061: ENDRHEAD
                   1062:         $r->print(<<ENDBFORM);
1.88      www      1063: <hr />$lt{'newr'}<br />
1.44      www      1064: <textarea name="newrecord" cols="80" rows="10" wrap="hard"></textarea>
1.45      www      1065: <br />
                   1066: <input type="hidden" name="recordftf" value="post" />
1.88      www      1067: <input type="submit" value="$lt{'post'}" />
1.44      www      1068: </form>
                   1069: ENDBFORM
                   1070:     }
                   1071: }
1.91      www      1072: 
1.101     raeburn  1073: # ----------------------------------------------------------- Blocking during exams
                   1074: 
                   1075: sub examblock {
                   1076:     my ($r,$action) = @_;
                   1077:     unless ($ENV{'request.course.id'}) { return;}
                   1078:     unless (&Apache::lonnet::allowed('srm',$ENV{'request.course.id'})) { $r->print('Not allowed'); }
                   1079:     my %lt=&Apache::lonlocal::texthash(
                   1080:             'comb' => 'Communication Blocking',
                   1081:             'cbds' => 'Communication blocking during scheduled exams',
                   1082:             '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.',
                   1083:              'mecb' => 'Modify existing communication blocking periods',
                   1084:              'ncbc' => 'No communication blocks currently stored'
                   1085:     );
                   1086: 
                   1087:     my %ltext = &Apache::lonlocal::texthash(
                   1088:             'dura' => 'Duration',
                   1089:             'setb' => 'Set by',
                   1090:             'even' => 'Event',
                   1091:             'actn' => 'Action',
                   1092:             'star' => 'Start',
                   1093:             'endd' => 'End'
                   1094:     );
                   1095: 
                   1096:     &printheader($r,'/adm/email?block=display',$lt{'comb'});
                   1097:     $r->print('<h3>'.$lt{'cbds'}.'</h3>');
                   1098: 
                   1099:     if ($action eq 'store') {
                   1100:         &blockstore($r);
                   1101:     }
                   1102: 
                   1103:     $r->print($lt{'desc'}.'<br /><br />
                   1104:                <form name="blockform" method="post" action="/adm/email?block=store">
                   1105:              ');
                   1106: 
                   1107:     $r->print('<h4>'.$lt{'mecb'}.'</h4>');
                   1108:     my %records = ();
                   1109:     my $blockcount = 0;
                   1110:     my $parmcount = 0;
                   1111:     &get_blockdates(\%records,\$blockcount);
                   1112:     if ($blockcount > 0) {
                   1113:         $parmcount = &display_blocker_status($r,\%records,\%ltext);
                   1114:     } else {
                   1115:         $r->print($lt{'ncbc'}.'<br /><br />');
                   1116:     }
                   1117:     &display_addblocker_table($r,$parmcount,\%ltext);
                   1118:     $r->print(<<"END");
                   1119: <br />
                   1120: <input type="hidden" name="blocktotal" value="$blockcount" />
                   1121: <input type ="submit" value="Save Changes" />
                   1122: </form>
                   1123: </body>
                   1124: </html>
                   1125: END
                   1126:     return;
                   1127: }
                   1128: 
                   1129: sub blockstore {
                   1130:     my $r = shift;
                   1131:     my %lt=&Apache::lonlocal::texthash(
                   1132:             'tfcm' => 'The following changes were made',
                   1133:             'cbps' => 'communication blocking period(s)',
                   1134:             'werm' => 'was/were removed',
                   1135:             'wemo' => 'was/were modified',
                   1136:             'wead' => 'was/were added',
                   1137:             'ncwm' => 'No changes were made.' 
                   1138:     );
                   1139:     my %adds = ();
                   1140:     my %removals = ();
                   1141:     my %cancels = ();
                   1142:     my $modtotal = 0;
                   1143:     my $canceltotal = 0;
                   1144:     my $addtotal = 0;
                   1145:     my %blocking = ();
                   1146:     $r->print('<h3>'.$lt{'head'}.'</h3>');
                   1147:     foreach (keys %ENV) {
                   1148:         if ($_ =~ m/^form\.modify_(\w+)$/) {
                   1149:             $adds{$1} = $1;
                   1150:             $removals{$1} = $1;
                   1151:             $modtotal ++;
                   1152:         } elsif ($_ =~ m/^form\.cancel_(\d+)$/) {
                   1153:             $cancels{$1} = $1;
                   1154:             unless ( defined($removals{$1}) ) {
                   1155:                 $removals{$1} = $1;
                   1156:                 $canceltotal ++;
                   1157:             }
                   1158:         } elsif ($_ =~ m/^form\.add_(\d+)$/) {
                   1159:             $adds{$1} = $1;
                   1160:             $addtotal ++;
                   1161:         }
                   1162:     }
                   1163: 
                   1164:     foreach (keys %removals) {
                   1165:         my $hashkey = $ENV{'form.key_'.$_};
                   1166:         &Apache::lonnet::del('comm_block',["$hashkey"],
                   1167:                          $ENV{'course.'.$ENV{'request.course.id'}.'.domain'},
                   1168:                          $ENV{'course.'.$ENV{'request.course.id'}.'.num'}
                   1169:                          );
                   1170:     }
                   1171:     foreach (keys %adds) {
                   1172:         unless ( defined($cancels{$_}) ) {
                   1173:             my ($newstart,$newend) = &get_dates_from_form($_);
                   1174:             my $newkey = $newstart.'____'.$newend;
                   1175:             $blocking{$newkey} = $ENV{'user.name'}.'@'.$ENV{'user.domain'}.':'.$ENV{'form.title_'.$_};
                   1176:         }
                   1177:     }
                   1178:     if ($addtotal + $modtotal > 0) {
                   1179:         &Apache::lonnet::put('comm_block',\%blocking,
                   1180:                      $ENV{'course.'.$ENV{'request.course.id'}.'.domain'},
                   1181:                      $ENV{'course.'.$ENV{'request.course.id'}.'.num'}
                   1182:                      );
                   1183:     }
                   1184:     my $chgestotal = $canceltotal + $modtotal + $addtotal;
                   1185:     if ($chgestotal > 0) {
                   1186:         $r->print($lt{'tfcm'}.'<ul>');
                   1187:         if ($canceltotal > 0) {
                   1188:             $r->print('<li>'.$canceltotal.' '.$lt{'cbps'},' '.$lt{'werm'}.'</li>');
                   1189:         }
                   1190:         if ($modtotal > 0) {
                   1191:             $r->print('<li>'.$modtotal.' '.$lt{'cbps'},' '.$lt{'wemo'}.'</li>');
                   1192:         }
                   1193:         if ($addtotal > 0) {
                   1194:             $r->print('<li>'.$addtotal.' '.$lt{'cbps'},' '.$lt{'wead'}.'</li>');
                   1195:         }
                   1196:         $r->print('</ul>');
                   1197:     } else {
                   1198:         $r->print($lt{'ncwm'});
                   1199:     }
                   1200:     $r->print('<br />');
                   1201:     return;
                   1202: }
                   1203: 
                   1204: sub get_dates_from_form {
                   1205:     my $item = shift;
                   1206:     my $startdate = &Apache::lonhtmlcommon::get_date_from_form('startdate_'.$item);
                   1207:     my $enddate   = &Apache::lonhtmlcommon::get_date_from_form('enddate_'.$item);
                   1208:     return ($startdate,$enddate);
                   1209: }
                   1210: 
                   1211: sub get_blockdates {
                   1212:     my ($records,$blockcount) = @_;
                   1213:     $$blockcount = 0;
                   1214:     %{$records} = &Apache::lonnet::dump('comm_block',
                   1215:                          $ENV{'course.'.$ENV{'request.course.id'}.'.domain'},
                   1216:                          $ENV{'course.'.$ENV{'request.course.id'}.'.num'}
                   1217:                          );
                   1218:     $$blockcount = keys %{$records};
                   1219:                                                                                                              
                   1220:     foreach (keys %{$records}) {
                   1221:         if ($_ eq 'error: 2 tie(GDBM) Failed while attempting dump') {
                   1222:             $$blockcount = 0;
                   1223:             last;
                   1224:         }
                   1225:     }
                   1226: }
                   1227: 
                   1228: sub display_blocker_status {
                   1229:     my ($r,$records,$ltext) = @_;
                   1230:     my $parmcount = 0;
                   1231:     my @bgcols = ("#eeeeee","#dddddd");
                   1232:     my $function = &Apache::loncommon::get_users_function();
                   1233:     my $color = &Apache::loncommon::designparm($function.'.tabbg',
                   1234:                                                     $ENV{'user.domain'});
                   1235:     my %lt = &Apache::lonlocal::texthash(
                   1236:         'modi' => 'Modify',
                   1237:         'canc' => 'Cancel',
                   1238:     );
                   1239:     $r->print(<<"END");
                   1240: <table border="0" cellpadding="0" cellspacing="0">
                   1241:  <tr>
                   1242:   <td width="100%" bgcolor="#000000">
                   1243:    <table width="100%" border="0" cellpadding="1" cellspacing="0">
                   1244:     <tr>
                   1245:      <td width="100%" bgcolor="#000000">
                   1246:       <table border="0" cellpadding="3" cellspacing="3" bgcolor="#FFFFFF">
                   1247:        <tr bgcolor="$color">
                   1248:         <td><b>$$ltext{'dura'}</b></td>
                   1249:         <td><b>$$ltext{'setb'}</b></td>
                   1250:         <td><b>$$ltext{'even'}</b></td>
                   1251:         <td><b>$$ltext{'actn'}?</b></td>
                   1252:        </tr>
                   1253: END
                   1254:     foreach (sort keys %{$records}) {
                   1255:         my $iter = $parmcount%2;
                   1256:         my $onchange = 'onFocus="javascript:window.document.forms['.
                   1257:                        "'blockform'].elements['modify_".$parmcount."'].".
                   1258:                        'checked=true;"';
                   1259:         my ($start,$end) = split/____/,$_;
                   1260:         my $startform = &Apache::lonhtmlcommon::date_setter('blockform','startdate_'.$parmcount,$start,$onchange);
                   1261:         my $endform = &Apache::lonhtmlcommon::date_setter('blockform','enddate_'.$parmcount,$end,$onchange);
                   1262:         my ($setter,$title) = split/:/,$$records{$_};
                   1263:         my ($setuname,$setudom) = split/@/,$setter;
                   1264:         my $settername = &Apache::loncommon::plainname($setuname,$setudom);
                   1265:         $r->print(<<"END");
                   1266:        <tr bgcolor="$bgcols[$iter]">
                   1267:         <td>$$ltext{'star'}:&nbsp;$startform<br/>$$ltext{'endd'}:&nbsp;&nbsp;$endform</td>
                   1268:         <td>$settername</td>
                   1269:         <td><input type="text" name="title_$parmcount" size="15" value="$title"/><input type="hidden" name="key_$parmcount" value="$_"></td>
                   1270:         <td>$lt{'modi'}?&nbsp;<input type="checkbox" name="modify_$parmcount"/><br />$lt{'canc'}?&nbsp;&nbsp;<input type="checkbox" name="cancel_$parmcount"/>
                   1271:        </tr>
                   1272: END
                   1273:         $parmcount ++;
                   1274:     }
                   1275:     $r->print(<<"END");
                   1276:       </table>
                   1277:      </td>
                   1278:     </tr>
                   1279:    </table>
                   1280:   </td>
                   1281:  </tr>
                   1282: </table>
                   1283: <br />
                   1284: <br />
                   1285: END
                   1286:     return $parmcount;
                   1287: }
                   1288: 
                   1289: sub display_addblocker_table {
                   1290:     my ($r,$parmcount,$ltext) = @_;
                   1291:     my $start = time;
                   1292:     my $end = $start + (60 * 60 * 2); #Default is an exam of 2 hours duration.
                   1293:     my $onchange = 'onFocus="javascript:window.document.forms['.
                   1294:                    "'blockform'].elements['add_".$parmcount."'].".
                   1295:                    'checked=true;"';
                   1296:     my $startform = &Apache::lonhtmlcommon::date_setter('blockform','startdate_'.$parmcount,$start,$onchange);
                   1297:     my $endform = &Apache::lonhtmlcommon::date_setter('blockform','enddate_'.$parmcount,$end,$onchange);
                   1298:     my $function = &Apache::loncommon::get_users_function();
                   1299:     my $color = &Apache::loncommon::designparm($function.'.tabbg',
                   1300:                                                     $ENV{'user.domain'});
                   1301:     my %lt = &Apache::lonlocal::texthash(
                   1302:         'addb' => 'Add block',
                   1303:         'exam' => 'e.g., Exam 1',
                   1304:         'addn' => 'Add new communication blocking periods'
                   1305:     );
                   1306:     $r->print(<<"END");
                   1307: <h4>$lt{'addn'}</h4> 
                   1308: <table border="0" cellpadding="0" cellspacing="0">
                   1309:  <tr>
                   1310:   <td width="100%" bgcolor="#000000">
                   1311:    <table width="100%" border="0" cellpadding="1" cellspacing="0">
                   1312:     <tr>
                   1313:      <td width="100%" bgcolor="#000000">
                   1314:       <table border="0" cellpadding="3" cellspacing="3" bgcolor="#FFFFFF">
                   1315:        <tr bgcolor="#CCCCFF">
                   1316:         <td><b>$$ltext{'dura'}</b></td>
                   1317:         <td><b>$$ltext{'even'} $lt{'exam'}</b></td>
                   1318:         <td><b>$$ltext{'actn'}?</b></td>
                   1319:        </tr>
                   1320:        <tr bgcolor="#eeeeee">
                   1321:         <td>$$ltext{'star'}:&nbsp;$startform<br />$$ltext{'endd'}:&nbsp;&nbsp;$endform</td>
                   1322:         <td><input type="text" name="title_$parmcount" size="15" value=""/></td>
                   1323:         <td>$lt{'addb'}?&nbsp;<input type="checkbox" name="add_$parmcount" value="1"/></td>
                   1324:        </tr>
                   1325:       </table>
                   1326:      </td>
                   1327:     </tr>
                   1328:    </table>
                   1329:   </td>
                   1330:  </tr>
                   1331: </table>
                   1332: END
                   1333:     return;
                   1334: }
                   1335: 
                   1336: sub blockcheck {
                   1337:     my ($setters,$startblock,$endblock) = @_;
                   1338:     # Retrieve active student roles and active course coordinator/instructor roles
                   1339:     my @livecses = ();
                   1340:     my @staffcses = ();
                   1341:     $$startblock = 0;
                   1342:     $$endblock = 0;
                   1343:     foreach (keys %ENV) {
                   1344:         if ($_ =~ m-^user\.role\.(st|cc|in)\./(.+)$-) {
                   1345:             my $role = $1;
                   1346:             my $cse = $2;
                   1347:             $cse =~ s|/|_|;
                   1348:             if ($ENV{$_} =~ m/^(\d*)\.(\d*)$/) {
                   1349:                 unless (($2 > 0 && $2 < time) || ($1 > time)) {
                   1350:                     if ($role eq 'st') {
                   1351:                         push @livecses, $cse;
                   1352:                     } else {
                   1353:                         unless (grep/^$cse$/,@staffcses) {
                   1354:                             push @staffcses, $cse;
                   1355:                         }
                   1356:                     }
                   1357:                 }
                   1358:             }
                   1359:         } elsif ($_ =~ m-user\.role\.cr/(\w+)/(\w+)/([^/]+)\./(.+)$- ) { 
                   1360:             my $rolepriv = $ENV{'user.role..rolesdef_'.$3};
                   1361:         }
                   1362:     }
                   1363:     # Retrieve blocking times and identity of blocker for active courses for students.
                   1364:     if (@livecses > 0) {
                   1365:         foreach my $cse (@livecses) {
                   1366:             my ($cdom,$crs) = split/_/,$cse;
                   1367:             if ( (grep/^$cse$/,@staffcses) && ($ENV{'request.role'} !~ m-^st\./$cdom/$crs$-) ) {
                   1368:                 next;
                   1369:             } else {
                   1370:                 %{$$setters{$cse}} = ();
                   1371:                 @{$$setters{$cse}{'staff'}} = ();
                   1372:                 @{$$setters{$cse}{'times'}} = ();
                   1373:                 my %records = &Apache::lonnet::dump('comm_block',$cdom,$crs);
                   1374:                 foreach (keys %records) {
                   1375:                     if ($_ =~ m/^(\d+)____(\d+)$/) {
                   1376:                         if ($1 <= time && $2 >= time) {
                   1377:                             my ($staff,$title) = split/:/,$records{$_};
                   1378:                             push @{$$setters{$cse}{'staff'}}, $staff;
                   1379:                             push @{$$setters{$cse}{'times'}}, $_;
                   1380:                             if ( ($$startblock == 0) || ($$startblock > $1) ) {
                   1381:                                 $$startblock = $1;
                   1382:                             }
                   1383:                             if ( ($$endblock == 0) || ($$endblock < $2) ) {
                   1384:                                 $$endblock = $2;
                   1385:                             }
                   1386:                         }
                   1387:                     }
                   1388:                 }
                   1389:             }
                   1390:         }
                   1391:     }
                   1392: }
                   1393: 
                   1394: sub build_block_table {
                   1395:     my ($r,$startblock,$endblock,$setters) = @_;
                   1396:     my $function = &Apache::loncommon::get_users_function();
                   1397:     my $color = &Apache::loncommon::designparm($function.'.tabbg',
                   1398:                                                     $ENV{'user.domain'});
                   1399:     my %lt = &Apache::lonlocal::texthash(
                   1400:         'cacb' => 'Currently active communication blocks',
                   1401:         'cour' => 'Course',
                   1402:         'dura' => 'Duration',
                   1403:         'blse' => 'Block set by'
                   1404:     ); 
                   1405:     $r->print(<<"END");
                   1406: <br /<br />$lt{'cacb'}:<br /><br />
                   1407: <table border="0" cellpadding="0" cellspacing="0">
                   1408:  <tr>
                   1409:   <td width="100%" bgcolor="#000000">
                   1410:    <table width="100%" border="0" cellpadding="1" cellspacing="0">
                   1411:     <tr>
                   1412:      <td width="100%" bgcolor="#000000">
                   1413:       <table border="0" cellpadding="3" cellspacing="3" bgcolor="#FFFFFF">
                   1414:        <tr bgcolor="$color">
                   1415:         <td><b>$lt{'cour'}</b></td>
                   1416:         <td><b>$lt{'dura'}</b></td>
                   1417:         <td><b>$lt{'blse'}</b></td>
                   1418:        </tr>
                   1419: END
                   1420:     foreach (keys %{$setters}) {
                   1421:         my %courseinfo=&Apache::lonnet::coursedescription($_);
                   1422:         for (my $i=0; $i<@{$$setters{$_}{staff}}; $i++) {
                   1423:             my ($uname,$udom) = split/\@/,$$setters{$_}{staff}[$i];
                   1424:             my $fullname = &Apache::loncommon::plainname($uname,$udom);
                   1425:             my ($openblock,$closeblock) = split/____/,$$setters{$_}{times}[$i];
                   1426:             $openblock = &Apache::lonlocal::locallocaltime($openblock);
                   1427:             $closeblock= &Apache::lonlocal::locallocaltime($closeblock);
                   1428:             $r->print('<tr><td>'.$courseinfo{'description'}.'</td>'.
                   1429:                       '<td>'.$openblock.' to '.$closeblock.'</td>'.
                   1430:                       '<td>'.$fullname.' ('.$uname.'@'.$udom.
                   1431:                       ')</td></tr>');
                   1432:         }
                   1433:     }
                   1434:     $r->print('</table></td></tr></table></td></tr></table>');
                   1435: }
                   1436: 
1.90      www      1437: # ----------------------------------------------------------- Display a message
                   1438: 
                   1439: sub displaymessage {
                   1440:     my ($r,$msgid)=@_;
1.101     raeburn  1441:     my %blocked = ();
                   1442:     my %setters = ();
                   1443:     my $startblock = 0;
                   1444:     my $endblock = 0;
                   1445:     my $numblocked = 0;
                   1446: # info to generate "next" and "previous" buttons and check if message is blocked
                   1447:     &blockcheck(\%setters,\$startblock,\$endblock);
                   1448:     my @messages=&sortedmessages(\%blocked,$startblock,$endblock,\$numblocked);
                   1449:     if ( $blocked{$msgid} eq 'ON' ) {
                   1450:         &printheader($r,'/adm/email',&mt('Display a Message'));
                   1451:         $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.'));
                   1452:         &build_block_table($r,$startblock,$endblock,\%setters);
                   1453:         return;
                   1454:     }
1.90      www      1455:     &statuschange($msgid,'read');
                   1456:     my %message=&Apache::lonnet::get('nohist_email',[$msgid]);
                   1457:     my %content=&unpackagemsg($message{$msgid});
                   1458:     my $counter=0;
                   1459:     $r->print('<pre>');
                   1460:     my $escmsgid=&Apache::lonnet::escape($msgid);
                   1461:     foreach (@messages) {
                   1462: 	if ($_->[5] eq $escmsgid){
                   1463: 	    last;
                   1464: 	}
                   1465: 	$counter++;
                   1466:     }
                   1467:     $r->print('</pre>');
                   1468:     my $number_of_messages = scalar(@messages); #subtract 1 for last index
                   1469: # start output
1.92      www      1470:     &printheader($r,'/adm/email?display='.&Apache::lonnet::escape($msgid),'Display a Message','',$content{'baseurl'});
1.90      www      1471:     my %courseinfo=&Apache::lonnet::coursedescription($content{'courseid'});
                   1472: # Functions
                   1473:     $r->print('<table border="2" width="100%"><tr bgcolor="#FFFFAA"><td>'.&mt('Functions').':</td>'.
                   1474: 	      '<td><a href="/adm/email?replyto='.&Apache::lonnet::escape($msgid).$sqs.
                   1475: 	      '"><b>'.&mt('Reply').'</b></a></td>'.
                   1476: 	      '<td><a href="/adm/email?forward='.&Apache::lonnet::escape($msgid).$sqs.
                   1477: 	      '"><b>'.&mt('Forward').'</b></a></td>'.
                   1478: 	      '<td><a href="/adm/email?markunread='.&Apache::lonnet::escape($msgid).$sqs.
                   1479: 	      '"><b>'.&mt('Mark Unread').'</b></a></td>'.
                   1480: 	      '<td><a href="/adm/email?markdel='.&Apache::lonnet::escape($msgid).$sqs.
                   1481: 	      '"><b>Delete</b></a></td>'.
                   1482: 	      '<td><a href="/adm/email?sortedby='.$ENV{'form.sortedby'}.
                   1483: 	      '"><b>'.&mt('Display all Messages').'</b></a></td>');
                   1484:     if ($counter > 0){
                   1485: 	$r->print('<td><a href="/adm/email?display='.$messages[$counter-1]->[5].$sqs.
                   1486: 		  '"><b>'.&mt('Previous').'</b></a></td>');
                   1487:     }
                   1488:     if ($counter < $number_of_messages - 1){
                   1489: 	$r->print('<td><a href="/adm/email?display='.$messages[$counter+1]->[5].$sqs.
                   1490: 		  '"><b>'.&mt('Next').'</b></a></td>');
                   1491:     }
                   1492:     $r->print('</tr></table>');
                   1493:     $r->print('<br /><b>'.&mt('Subject').':</b> '.$content{'subject'}.
                   1494: 	      '<br /><b>'.&mt('From').':</b> '.
                   1495: 	      &Apache::loncommon::aboutmewrapper(
                   1496: 						 &Apache::loncommon::plainname($content{'sendername'},$content{'senderdomain'}),
                   1497: 						 $content{'sendername'},$content{'senderdomain'}).' ('.
                   1498: 	      $content{'sendername'}.' at '.
                   1499: 	      $content{'senderdomain'}.') '.
                   1500: 	      ($content{'courseid'}?'<br /><b>'.&mt('Course').':</b> '.$courseinfo{'description'}.
                   1501: 	       ($content{'coursesec'}?' ('.&mt('Group/Section').': '.$content{'coursesec'}.')':''):'').
                   1502: 	      '<br /><b>'.&mt('Time').':</b> '.$content{'time'}.
                   1503: 	      '<p><pre>'.
                   1504: 	      &Apache::lontexconvert::msgtexconverted($content{'message'},1).
                   1505: 	      '</pre><hr />'.$content{'citation'}.'</p>');
                   1506:     return;   
                   1507: }
1.44      www      1508: 
1.88      www      1509: # ================================================================== The Header
                   1510: 
                   1511: sub header {
1.90      www      1512:     my ($r,$title,$baseurl)=@_;
1.88      www      1513:     $r->print('<html><head><title>Communication and Messages</title>');
                   1514:     if ($baseurl) {
                   1515: 	$r->print("<base href=\"http://$ENV{'SERVER_NAME'}/$baseurl\" />");
                   1516:     }
                   1517:     $r->print(&Apache::loncommon::studentbrowser_javascript().'</head>'.
                   1518: 	      &Apache::loncommon::bodytag('Communication and Messages'));
                   1519:         $r->print(&Apache::lonhtmlcommon::breadcrumbs
1.90      www      1520:                   (undef,($title?$title:'Communication and Messages')));
1.88      www      1521: 
                   1522: }
                   1523: 
1.90      www      1524: # ---------------------------------------------------------------- Print header
                   1525: 
                   1526: sub printheader {
                   1527:     my ($r,$url,$desc,$title,$baseurl)=@_;
                   1528:     &Apache::lonhtmlcommon::add_breadcrumb
                   1529: 	({href=>$url,
                   1530: 	  text=>$desc});
                   1531:     &header($r,$title,$baseurl);
                   1532: }
                   1533: 
                   1534: 
1.13      www      1535: # ===================================================================== Handler
                   1536: 
1.5       www      1537: sub handler {
                   1538:     my $r=shift;
                   1539: 
                   1540: # ----------------------------------------------------------- Set document type
1.87      www      1541:     
                   1542:     &Apache::loncommon::content_type($r,'text/html');
                   1543:     $r->send_http_header;
                   1544:     
                   1545:     return OK if $r->header_only;
                   1546:     
1.6       www      1547: # --------------------------- Get query string for limited number of parameters
1.32      matthew  1548:     &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'},
                   1549:         ['display','replyto','forward','markread','markdel','markunread',
1.44      www      1550:          'sendreply','compose','sendmail','critical','recname','recdom',
1.101     raeburn  1551:          'recordftf','sortedby','block']);
1.65      www      1552:     $sqs='&sortedby='.$ENV{'form.sortedby'};
1.40      www      1553: # ------------------------------------------------------ They checked for email
1.101     raeburn  1554:     unless ($ENV{'form.block'}) {
                   1555:         &Apache::lonnet::put('email_status',{'recnewemail'=>0});
                   1556:     }
1.88      www      1557: 
                   1558: # ----------------------------------------------------------------- Breadcrumbs
                   1559: 
                   1560:     &Apache::lonhtmlcommon::clear_breadcrumbs();
                   1561:     &Apache::lonhtmlcommon::add_breadcrumb
                   1562:         ({href=>"/adm/communicate",
                   1563:           text=>"Communication/Messages",
                   1564:           faq=>12,bug=>'Communication Tools',});
                   1565: 
1.5       www      1566: # --------------------------------------------------------------- Render Output
1.88      www      1567: 
1.87      www      1568:     if ($ENV{'form.display'}) {
1.90      www      1569: 	&displaymessage($r,$ENV{'form.display'});
1.87      www      1570:     } elsif ($ENV{'form.replyto'}) {
1.92      www      1571: 	&compout($r,'',$ENV{'form.replyto'});
1.87      www      1572:     } elsif ($ENV{'form.confirm'}) {
1.92      www      1573: 	&printheader($r,'','Confirmed Receipt');
1.87      www      1574: 	foreach (keys %ENV) {
                   1575: 	    if ($_=~/^form\.rec\_(.*)$/) {
1.92      www      1576: 		$r->print('<b>'.&mt('Confirming Receipt').':</b> '.
1.87      www      1577: 			  &user_crit_received($1).'<br>');
                   1578: 	    }
                   1579: 	    if ($_=~/^form\.reprec\_(.*)$/) {
                   1580: 		my $msgid=$1;
1.92      www      1581: 		$r->print('<b>'.&mt('Confirming Receipt').':</b> '.
1.87      www      1582: 			  &user_crit_received($msgid).'<br>');
1.94      www      1583: 		&compout($r,'','','',$msgid);
1.87      www      1584: 	    }
                   1585: 	}
                   1586: 	&discrit($r);
                   1587:     } elsif ($ENV{'form.critical'}) {
1.92      www      1588: 	&printheader($r,'','Displaying Critical Messages');
1.87      www      1589: 	&discrit($r);
                   1590:     } elsif ($ENV{'form.forward'}) {
                   1591: 	&compout($r,$ENV{'form.forward'});
                   1592:     } elsif ($ENV{'form.markdel'}) {
1.92      www      1593: 	&printheader($r,'','Deleted Message');
1.87      www      1594: 	&statuschange($ENV{'form.markdel'},'deleted');
                   1595: 	&disall($r);
                   1596:     } elsif ($ENV{'form.markeddel'}) {
                   1597: 	my $total=0;
                   1598: 	foreach (keys %ENV) {
                   1599: 	    if ($_=~/^form\.delmark_(.*)$/) {
                   1600: 		&statuschange(&Apache::lonnet::unescape($1),'deleted');
                   1601: 		$total++;
                   1602: 	    }
                   1603: 	}
1.92      www      1604: 	&printheader($r,'','Deleted Messages');
1.87      www      1605: 	$r->print('Deleted '.$total.' message(s)<p>');
                   1606: 	&disall($r);
                   1607:     } elsif ($ENV{'form.markunread'}) {
1.92      www      1608: 	&printheader($r,'','Marked Message as Unread');
1.87      www      1609: 	&statuschange($ENV{'form.markunread'},'new');
                   1610: 	&disall($r);
                   1611:     } elsif ($ENV{'form.compose'}) {
1.92      www      1612: 	&compout($r,'','',$ENV{'form.compose'});
1.87      www      1613:     } elsif ($ENV{'form.recordftf'}) {
                   1614: 	&facetoface($r,$ENV{'form.recordftf'});
1.101     raeburn  1615:     } elsif ($ENV{'form.block'}) {
                   1616:         &examblock($r,$ENV{'form.block'});
1.87      www      1617:     } elsif ($ENV{'form.sendmail'}) {
                   1618: 	my $sendstatus='';
                   1619: 	if ($ENV{'form.send'}) {
1.92      www      1620: 	    &printheader($r,'','Messages being sent.');
                   1621: 	    $r->rflush();
1.87      www      1622: 	    my %content=();
                   1623: 	    undef %content;
                   1624: 	    if ($ENV{'form.forwid'}) {
                   1625: 		my $msgid=$ENV{'form.forwid'};
                   1626: 		my %message=&Apache::lonnet::get('nohist_email',[$msgid]);
                   1627: 		%content=&unpackagemsg($message{$msgid},1);
                   1628: 		&statuschange($msgid,'forwarded');
                   1629: 		$ENV{'form.message'}.="\n\n-- Forwarded message --\n\n".
                   1630: 		    $content{'message'};
                   1631: 	    }
                   1632: 	    my %toaddr=();
                   1633: 	    undef %toaddr;
                   1634: 	    if ($ENV{'form.sendmode'} eq 'group') {
                   1635: 		foreach (keys %ENV) {
                   1636: 		    if ($_=~/^form\.send\_to\_\&\&\&[^\&]*\&\&\&\_(.+)$/) {
                   1637: 			$toaddr{$1}='';
                   1638: 		    }
                   1639: 		}
                   1640: 	    } elsif ($ENV{'form.sendmode'} eq 'upload') {
                   1641: 		foreach (split(/[\n\r\f]+/,$ENV{'form.upfile'})) {
                   1642: 		    my ($rec,$txt)=split(/\s*\:\s*/,$_);
                   1643: 		    if ($txt) {
                   1644: 			$rec=~s/\@/\:/;
                   1645: 			$toaddr{$rec}.=$txt."\n";
                   1646: 		    }
                   1647: 		}
                   1648: 	    } else {
                   1649: 		$toaddr{$ENV{'form.recuname'}.':'.$ENV{'form.recdomain'}}='';
                   1650: 	    }
                   1651: 	    if ($ENV{'form.additionalrec'}) {
                   1652: 		foreach (split(/\,/,$ENV{'form.additionalrec'})) {
                   1653: 		    my ($auname,$audom)=split(/\@/,$_);
                   1654: 		    $toaddr{$auname.':'.$audom}='';
                   1655: 		}
                   1656: 	    }
1.92      www      1657: 
1.87      www      1658: 	    foreach (keys %toaddr) {
                   1659: 		my ($recuname,$recdomain)=split(/\:/,$_);
                   1660: 		my $msgtxt=&Apache::lonfeedback::clear_out_html($ENV{'form.message'});
1.92      www      1661: 		if ($toaddr{$_}) { $msgtxt.='<hr />'.$toaddr{$_}; }
                   1662: 		my $thismsg;    
1.87      www      1663: 		if ((($ENV{'form.critmsg'}) || ($ENV{'form.sendbck'})) && 
                   1664: 		    (&Apache::lonnet::allowed('srm',$ENV{'request.course.id'}))) {
1.92      www      1665: 		    $r->print(&mt('Sending critical message').' '.$recuname.'@'.$recdomain.': ');
                   1666: 		    $thismsg=&user_crit_msg($recuname,$recdomain,
1.87      www      1667: 						    &Apache::lonfeedback::clear_out_html($ENV{'form.subject'}),
                   1668: 						    $msgtxt,
                   1669: 						    $ENV{'form.sendbck'});
                   1670: 		} else {
1.92      www      1671: 		    $r->print(&mt('Sending').' '.$recuname.'@'.$recdomain.': ');
                   1672: 		    $thismsg=&user_normal_msg($recuname,$recdomain,
1.87      www      1673: 						      &Apache::lonfeedback::clear_out_html($ENV{'form.subject'}),
                   1674: 						      $msgtxt,
                   1675: 						      $content{'citation'});
1.102     raeburn  1676:                     if (($ENV{'request.course.id'}) && ($ENV{'form.sendmode'} eq 'group')) {
                   1677:                         &user_normal_msg_raw(
                   1678:                         $ENV{'course.'.$ENV{'request.course.id'}.'.num'},
                   1679:                         $ENV{'course.'.$ENV{'request.course.id'}.'.domain'},
                   1680:                         'Broadcast ['.$recuname.':'.$recdomain.']',
                   1681:                         $msgtxt);
                   1682:                     }
1.87      www      1683: 		}
1.92      www      1684: 		$r->print($thismsg.'<br />');
                   1685: 		$sendstatus.=' '.$thismsg;
1.87      www      1686: 	    }
1.95      www      1687: 	} else {
                   1688: 	    &printheader($r,'','No messages sent.'); 
1.87      www      1689: 	}
                   1690: 	if ($sendstatus=~/^(\s*(?:ok|con_delayed)\s*)*$/) {
                   1691: 	    $r->print('<br /><font color="green">'.&mt('Completed.').'</font>');
                   1692: 	    if ($ENV{'form.displayedcrit'}) {
                   1693: 		&discrit($r);
                   1694: 	    } else {
1.95      www      1695: 		&Apache::loncommunicate::menu($r);
1.87      www      1696: 	    }
                   1697: 	} else {
                   1698: 	    $r->print(
                   1699: 		      '<h2><font color="red">'.&mt('Could not deliver message').'</font></h2>'.
                   1700: 		      &mt('Please use the browser "Back" button and correct the recipient addresses')
                   1701: 		      );
                   1702: 	}
                   1703:     } else {
1.92      www      1704: 	&printheader($r,'','Display All Messages');
1.87      www      1705: 	&disall($r);
                   1706:     }
                   1707:     $r->print('</body></html>');
                   1708:     return OK;
1.5       www      1709: }
1.2       www      1710: # ================================================= Main program, reset counter
                   1711: 
1.27      www      1712: BEGIN {
1.2       www      1713:     $msgcount=0;
1.1       www      1714: }
1.58      bowersj2 1715: 
                   1716: =pod
                   1717: 
                   1718: =back
                   1719: 
1.59      bowersj2 1720: =cut
                   1721: 
                   1722: 1; 
1.1       www      1723: 
                   1724: __END__
                   1725: 
                   1726: 
                   1727: 
                   1728: 
                   1729: 
                   1730: 
                   1731: 

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