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

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

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