--- loncom/interface/lonmsg.pm 2005/11/29 22:41:30 1.159 +++ loncom/interface/lonmsg.pm 2006/01/09 20:07:27 1.170 @@ -1,7 +1,7 @@ # The LearningOnline Network with CAPA # Routines for messaging # -# $Id: lonmsg.pm,v 1.159 2005/11/29 22:41:30 raeburn Exp $ +# $Id: lonmsg.pm,v 1.170 2006/01/09 20:07:27 albertel Exp $ # # Copyright Michigan State University Board of Trustees # @@ -97,6 +97,17 @@ Right now, this document will cover just it is likely you will not need to programmatically read messages, since lonmsg already implements that functionality. +The routines used to package messages and unpackage messages are not +only used by lonmsg when creating/extracting messages for LON-CAPA's +internal messaging system, but also by lonnotify.pm which is available +for use by Domain Coordinators to broadcast standard e-mail to specified +users in their domain. The XML packaging used in the two cases is very +similar. The differences are the use of $uname and +$udom in stored internal messages, compared +with $email in stored +Domain Coordinator e-mail for the storage of information about +recipients of the message/e-mail. + =head1 FUNCTIONS =over 4 @@ -126,7 +137,7 @@ my $interdis; sub packagemsg { my ($subject,$message,$citation,$baseurl,$attachmenturl, - $recuser,$recdomain,$msgid)=@_; + $recuser,$recdomain,$msgid,$type)=@_; $message =&HTML::Entities::encode($message,'<>&"'); $citation=&HTML::Entities::encode($citation,'<>&"'); $subject =&HTML::Entities::encode($subject,'<>&"'); @@ -142,6 +153,14 @@ sub packagemsg { split(/\:/,&Apache::lonnet::unescape($env{'form.replyid'})); $course_context = $origcid; } + foreach my $key (keys(%env)) { + if ($key=~/^form\.(rep)?rec\_(.*)$/) { + my ($sendtime,$shortsubj,$fromname,$fromdomain,$count,$origcid) = + split(/\:/,&Apache::lonnet::unescape($2)); + $course_context = $origcid; + last; + } + } unless(defined($course_context)) { $course_context = $env{'request.course.id'}; } @@ -170,8 +189,18 @@ sub packagemsg { ''.$msgid.''; if (ref($recuser) eq 'ARRAY') { for (my $i=0; $i<@{$recuser}; $i++) { - $result .= ''.$$recuser[$i].''. - ''.$$recdomain[$i].''; + if ($type eq 'dcmail') { + my ($username,$email) = split(/:/,$$recuser[$i]); + $username = &Apache::lonnet::unescape($username); + $email = &Apache::lonnet::unescape($email); + $username = &HTML::Entities::encode($username,'<>&"'); + $email = &HTML::Entities::encode($email,'<>&"'); + $result .= ''. + $email.''; + } else { + $result .= ''.$$recuser[$i].''. + ''.$$recdomain[$i].''; + } } } else { $result .= ''.$recuser.''. @@ -203,11 +232,16 @@ sub unpackagemsg { my $value=$parser->get_text('/'.$entry); if (($entry eq 'recuser') || ($entry eq 'recdomain')) { push(@{$content{$entry}},$value); + } elsif ($entry eq 'recipient') { + my $username = $token->[2]{'username'}; + $username = &HTML::Entities::decode($username,'<>&"'); + $content{$entry}{$username} = $value; } else { $content{$entry}=$value; } } } + if (!exists($content{'recuser'})) { $content{'recuser'} = []; } if ($content{'attachmenturl'}) { my ($fname)=($content{'attachmenturl'}=~m|/([^/]+)$|); if ($notoken) { @@ -233,14 +267,22 @@ sub buildmsgid { } sub unpackmsgid { - my ($msgid,$folder)=@_; + my ($msgid,$folder,$skipstatus,$status_cache)=@_; $msgid=&Apache::lonnet::unescape($msgid); - my $suffix=&foldersuffix($folder); - my ($sendtime,$shortsubj,$fromname,$fromdomain,$count,$fromcid)=split(/\:/, - &Apache::lonnet::unescape($msgid)); - my %status=&Apache::lonnet::get('email_status'.$suffix,[$msgid]); - if ($status{$msgid}=~/^error\:/) { $status{$msgid}=''; } - unless ($status{$msgid}) { $status{$msgid}='new'; } + my ($sendtime,$shortsubj,$fromname,$fromdomain,$count,$fromcid, + $processid)=split(/\:/,&Apache::lonnet::unescape($msgid)); + if (!defined($processid)) { $fromcid = ''; } + my %status=(); + unless ($skipstatus) { + if (ref($status_cache)) { + $status{$msgid} = $status_cache->{$msgid}; + } else { + my $suffix=&foldersuffix($folder); + %status=&Apache::lonnet::get('email_status'.$suffix,[$msgid]); + } + if ($status{$msgid}=~/^error\:/) { $status{$msgid}=''; } + unless ($status{$msgid}) { $status{$msgid}='new'; } + } return ($sendtime,$shortsubj,$fromname,$fromdomain,$status{$msgid},$fromcid); } @@ -516,7 +558,9 @@ sub user_normal_msg_raw { unless (($env{'request.course.id'}) && (($env{'form.sendmode'} eq 'group') || (($env{'form.critmsg'}) || ($env{'form.sendbck'})) && - (&Apache::lonnet::allowed('srm',$env{'request.course.id'})))) { + (&Apache::lonnet::allowed('srm',$env{'request.course.id'}) + || &Apache::lonnet::allowed('srm',$env{'request.course.id'}. + '/'.$env{'request.course.sec'})))) { $status .= &store_sent_mail($msgid,$message); } } else { @@ -820,11 +864,15 @@ sub sortedmessages { my @messages = &Apache::lonnet::getkeys('nohist_email'.$suffix); #unpack the varibles and repack into temp for sorting my @temp; + my %descriptions; + my %status_cache = + &Apache::lonnet::get('email_status'.&foldersuffix($folder),\@messages); foreach (@messages) { my $msgid=&Apache::lonnet::escape($_); my ($sendtime,$shortsubj,$fromname,$fromdomain,$status,$fromcid)= - &Apache::lonmsg::unpackmsgid($msgid,$folder); - my $description = &get_course_desc($fromcid); + &Apache::lonmsg::unpackmsgid($msgid,$folder,undef, + \%status_cache); + my $description = &get_course_desc($fromcid,\%descriptions); my @temp1 = ($sendtime,$shortsubj,$fromname,$fromdomain,$status, $msgid,$description); # Check whether message was sent during blocking period. @@ -878,15 +926,24 @@ sub sortedmessages { } sub get_course_desc { - my ($fromcid) = @_; - my $description; - if (defined($env{'course.'.$fromcid.'.description'})) { - $description = $env{'course.'.$fromcid.'.description'}; + my ($fromcid,$descriptions) = @_; + my $description; + if (!$fromcid) { + return $description; } else { - my %courseinfo=&Apache::lonnet::coursedescription($fromcid); - $description = $courseinfo{'description'}; + if (defined($$descriptions{$fromcid})) { + $description = $$descriptions{$fromcid}; + } else { + if (defined($env{'course.'.$fromcid.'.description'})) { + $description = $env{'course.'.$fromcid.'.description'}; + } else { + my %courseinfo=&Apache::lonnet::coursedescription($fromcid); $description = $courseinfo{'description'}; + $description = $courseinfo{'description'}; + } + $$descriptions{$fromcid} = $description; + } + return $description; } - return $description; } # ======================================================== Display new messages @@ -903,10 +960,7 @@ sub disnew { 'op' => 'Open', 'do' => 'Domain' ); - my @msgids = sort split(/\&/,&Apache::lonnet::reply - ('keys:'.$env{'user.domain'}.':'. - $env{'user.name'}.':nohist_email', - $env{'user.home'})); + my @msgids = sort(&Apache::lonnet::getkeys('nohist_email')); my @newmsgs; my %setters = (); my $startblock = 0; @@ -915,11 +969,15 @@ sub disnew { my $numblocked = 0; # Check for blocking of display because of scheduled online exams. &blockcheck(\%setters,\$startblock,\$endblock); + my %status_cache = + &Apache::lonnet::get('email_status',\@msgids); + my %descriptions; foreach (@msgids) { + my $msgid=&Apache::lonnet::escape($_); my ($sendtime,$shortsubj,$fromname,$fromdom,$status,$fromcid)= - &Apache::lonmsg::unpackmsgid($_); + &Apache::lonmsg::unpackmsgid($msgid,undef,undef,\%status_cache); if (defined($sendtime) && $sendtime!~/error/) { - my $description = &get_course_desc($fromcid); + my $description = &get_course_desc($fromcid,\%descriptions); my $numsendtime = $sendtime; $sendtime = &Apache::lonlocal::locallocaltime($sendtime); if ($status eq 'new') { @@ -928,7 +986,7 @@ sub disnew { $numblocked ++; } else { push @newmsgs, { - msgid => $_, + msgid => $msgid, sendtime => $sendtime, shortsub => &Apache::lonnet::unescape($shortsubj), from => $fromname, @@ -1172,7 +1230,9 @@ sub compout { 'ca' => 'Cancel', 'ma' => 'Mail'); - if (&Apache::lonnet::allowed('srm',$env{'request.course.id'})) { + if (&Apache::lonnet::allowed('srm',$env{'request.course.id'}) + || &Apache::lonnet::allowed('srm',$env{'request.course.id'}. + '/'.$env{'request.course.sec'})) { my $crithelp = Apache::loncommon::help_open_topic("Course_Critical_Message"); $dispcrit= '

' . $crithelp . @@ -1181,8 +1241,8 @@ sub compout { &mt('and return receipt') . '' . $crithelp . '

'. -'

'; } +''; } my %message; my %content; my $defdom=$env{'user.domain'}; @@ -1298,7 +1358,7 @@ $dispcrit ENDUPLOAD } if ($broadcast eq 'group') { - &discourse; + &discourse($r); } $r->print(''. &Apache::lonfeedback::generate_preview_button('compemail','message'). @@ -1311,7 +1371,9 @@ sub retrieve_instructor_comments { my ($user,$domain)=@_; my $target=$env{'form.grade_target'}; if (! $env{'request.course.id'}) { return; } - if (! &Apache::lonnet::allowed('srm',$env{'request.course.id'})) { + if (! &Apache::lonnet::allowed('srm',$env{'request.course.id'}) + && ! &Apache::lonnet::allowed('srm',$env{'request.course.id'}. + '/'.$env{'request.course.sec'})) { return; } my %records=&Apache::lonnet::dump('nohist_email', @@ -1336,7 +1398,10 @@ sub disfacetoface { my ($r,$user,$domain)=@_; my $target=$env{'form.grade_target'}; unless ($env{'request.course.id'}) { return; } - unless (&Apache::lonnet::allowed('srm',$env{'request.course.id'})) { + if (!&Apache::lonnet::allowed('srm',$env{'request.course.id'}) + && ! &Apache::lonnet::allowed('srm',$env{'request.course.id'}. + '/'.$env{'request.course.sec'})) { + $r->print('Not allowed'); return; } my %records=&Apache::lonnet::dump('nohist_email', @@ -1390,7 +1455,10 @@ $content{'sendername'}.'@'. sub facetoface { my ($r,$stage)=@_; - unless (&Apache::lonnet::allowed('srm',$env{'request.course.id'})) { + if (!&Apache::lonnet::allowed('srm',$env{'request.course.id'}) + && ! &Apache::lonnet::allowed('srm',$env{'request.course.id'}. + '/'.$env{'request.course.sec'})) { + $r->print('Not allowed'); return; } &printheader($r, @@ -1465,7 +1533,12 @@ ENDBFORM sub examblock { my ($r,$action) = @_; unless ($env{'request.course.id'}) { return;} - unless (&Apache::lonnet::allowed('srm',$env{'request.course.id'})) { $r->print('Not allowed'); } + if (!&Apache::lonnet::allowed('srm',$env{'request.course.id'}) + && ! &Apache::lonnet::allowed('srm',$env{'request.course.id'}. + '/'.$env{'request.course.sec'})) { + $r->print('Not allowed'); + return; + } my %lt=&Apache::lonlocal::texthash( 'comb' => 'Communication Blocking', 'cbds' => 'Communication blocking during scheduled exams', @@ -2081,7 +2154,10 @@ sub sendoffmail { my $msgtype; my %sentmessage; if ((($env{'form.critmsg'}) || ($env{'form.sendbck'})) && - (&Apache::lonnet::allowed('srm',$env{'request.course.id'}))) { + (&Apache::lonnet::allowed('srm',$env{'request.course.id'}) + || &Apache::lonnet::allowed('srm',$env{'request.course.id'}. + '/'.$env{'request.course.sec'}) + )) { $savemsg=&Apache::lonfeedback::clear_out_html($env{'form.message'},1); $msgtype = 'critical'; } else { @@ -2094,7 +2170,9 @@ sub sendoffmail { if ($toaddr{$_}) { $msgtxt.='
'.$toaddr{$_}; } my $thismsg; if ((($env{'form.critmsg'}) || ($env{'form.sendbck'})) && - (&Apache::lonnet::allowed('srm',$env{'request.course.id'}))) { + (&Apache::lonnet::allowed('srm',$env{'request.course.id'}) + || &Apache::lonnet::allowed('srm',$env{'request.course.id'}. + '/'.$env{'request.course.sec'}))) { $r->print(&mt('Sending critical message').' '.$recuname.'@'.$recdomain.': '); $thismsg=&user_crit_msg($recuname,$recdomain, &Apache::lonfeedback::clear_out_html($env{'form.subject'}),