--- loncom/interface/lonmsg.pm 2005/11/28 20:14:43 1.158 +++ loncom/interface/lonmsg.pm 2005/11/29 22:41:30 1.159 @@ -1,7 +1,7 @@ # The LearningOnline Network with CAPA # Routines for messaging # -# $Id: lonmsg.pm,v 1.158 2005/11/28 20:14:43 raeburn Exp $ +# $Id: lonmsg.pm,v 1.159 2005/11/29 22:41:30 raeburn Exp $ # # Copyright Michigan State University Board of Trustees # @@ -147,13 +147,9 @@ sub packagemsg { } my $now=time; $msgcount++; - my $partsubj=$subject; - $partsubj=&Apache::lonnet::escape($partsubj); unless(defined($msgid)) { - $msgid=&Apache::lonnet::escape( - $now.':'.$partsubj.':'.$env{'user.name'}.':'. - $env{'user.domain'}.':'.$msgcount.':'. - $course_context.':'.$$); + $msgid = &buildmsgid($now,$subject,$env{'user.name'},$env{'user.domain'}, + $msgcount,$course_context,$$); } my $result=''.$env{'user.name'}.''. ''.$env{'user.domain'}.''. @@ -229,6 +225,13 @@ sub unpackagemsg { # ======================================================= Get info out of msgid +sub buildmsgid { + my ($now,$subject,$uname,$udom,$msgcount,$course_context,$pid) = @_; + $subject=&Apache::lonnet::escape($subject); + return(&Apache::lonnet::escape($now.':'.$subject.':'.$uname.':'. + $udom.':'.$msgcount.':'.$course_context.':'.$pid)); +} + sub unpackmsgid { my ($msgid,$folder)=@_; $msgid=&Apache::lonnet::unescape($msgid); @@ -389,7 +392,7 @@ sub all_url_author_res_msg { # ================================================== Critical message to a user sub user_crit_msg_raw { - my ($user,$domain,$subject,$message,$sendback,$toperm)=@_; + my ($user,$domain,$subject,$message,$sendback,$toperm,$sentmessage)=@_; # Check if allowed missing my $status=''; my $msgid='undefined'; @@ -403,13 +406,9 @@ sub user_crit_msg_raw { 'put:'.$domain.':'.$user.':critical:'. &Apache::lonnet::escape($msgid).'='. &Apache::lonnet::escape($message),$homeserver); - if ($env{'request.course.id'}) { - &user_normal_msg_raw( - $env{'course.'.$env{'request.course.id'}.'.num'}, - $env{'course.'.$env{'request.course.id'}.'.domain'}, - 'Critical ['.$user.':'.$domain.']', - $message); - } + if (defined($sentmessage)) { + $$sentmessage = $message; + } } else { $status='no_host'; } @@ -447,7 +446,7 @@ sub user_crit_msg_raw { =cut sub user_crit_msg { - my ($user,$domain,$subject,$message,$sendback,$toperm)=@_; + my ($user,$domain,$subject,$message,$sendback,$toperm,$sentmessage)=@_; my $status=''; my %userenv = &Apache::lonnet::get('environment',['msgforward'], $domain,$user); @@ -457,10 +456,10 @@ sub user_crit_msg { my ($forwuser,$forwdomain)=split(/\:/,$_); $status.= &user_crit_msg_raw($forwuser,$forwdomain,$subject,$message, - $sendback,$toperm).' '; + $sendback,$toperm,$sentmessage).' '; } } else { - $status=&user_crit_msg_raw($user,$domain,$subject,$message,$sendback,$toperm); + $status=&user_crit_msg_raw($user,$domain,$subject,$message,$sendback,$toperm,$sentmessage); } return $status; } @@ -495,7 +494,7 @@ sub user_crit_received { sub user_normal_msg_raw { my ($user,$domain,$subject,$message,$citation,$baseurl,$attachmenturl, - $toperm,$newid)=@_; + $toperm,$currid,$newid,$sentmessage)=@_; # Check if allowed missing my $status=''; my $msgid='undefined'; @@ -504,7 +503,7 @@ sub user_normal_msg_raw { my $homeserver=&Apache::lonnet::homeserver($user,$domain); if ($homeserver ne 'no_host') { ($msgid,$message)=&packagemsg($subject,$message,$citation,$baseurl, - $attachmenturl,$user,$domain); + $attachmenturl,$user,$domain,$currid); # Store in user folder $status=&Apache::lonnet::critical( 'put:'.$domain.':'.$user.':nohist_email:'. @@ -513,8 +512,11 @@ sub user_normal_msg_raw { # Save new message received time &Apache::lonnet::put ('email_status',{'recnewemail'=>time},$domain,$user); -# Into sent-mail folder unless a broadcast message - unless (($env{'request.course.id'}) && ($env{'form.sendmode'} eq 'group')) { +# Into sent-mail folder unless a broadcast message or critical message + unless (($env{'request.course.id'}) && + (($env{'form.sendmode'} eq 'group') || + (($env{'form.critmsg'}) || ($env{'form.sendbck'})) && + (&Apache::lonnet::allowed('srm',$env{'request.course.id'})))) { $status .= &store_sent_mail($msgid,$message); } } else { @@ -523,6 +525,10 @@ sub user_normal_msg_raw { if (defined($newid)) { $$newid = $msgid; } + if (defined($sentmessage)) { + $$sentmessage = $message; + } + # Notifications my %userenv = &Apache::lonnet::get('environment',['notification', 'permanentemail'], @@ -553,7 +559,7 @@ sub user_normal_msg_raw { sub user_normal_msg { my ($user,$domain,$subject,$message,$citation,$baseurl,$attachmenturl, - $toperm)=@_; + $toperm,$sentmessage)=@_; my $status=''; my %userenv = &Apache::lonnet::get('environment',['msgforward'], $domain,$user); @@ -563,11 +569,11 @@ sub user_normal_msg { my ($forwuser,$forwdomain)=split(/\:/,$_); $status.= &user_normal_msg_raw($forwuser,$forwdomain,$subject,$message, - $citation,$baseurl,$attachmenturl,$toperm).' '; + $citation,$baseurl,$attachmenturl,$toperm,undef,undef,$sentmessage).' '; } } else { $status=&user_normal_msg_raw($user,$domain,$subject,$message, - $citation,$baseurl,$attachmenturl,$toperm); + $citation,$baseurl,$attachmenturl,$toperm,undef,undef,$sentmessage); } return $status; } @@ -1346,6 +1352,12 @@ sub disfacetoface { $result.='

'.&mt('Record').'

'; } elsif ($content{'subject'}=~/^Broadcast/) { $result .='

'.&mt('Broadcast Message').'

'; + if ($content{'subject'}=~/^Broadcast\./) { + %content=&unpackagemsg($content{'message'}); + $content{'message'}= + ''.&mt('Subject').': '.$content{'subject'}.'
'. + $content{'message'}; + } } else { $result.='

'.&mt('Critical Message').'

'; %content=&unpackagemsg($content{'message'}); @@ -1421,12 +1433,13 @@ ENDTREC ($env{'form.recdomain'}) && ($env{'form.recuname'})) { chomp($env{'form.newrecord'}); if ($env{'form.newrecord'}) { + my $recordtxt = $env{'form.newrecord'}; &user_normal_msg_raw( $env{'course.'.$env{'request.course.id'}.'.num'}, $env{'course.'.$env{'request.course.id'}.'.domain'}, &mt('Record'). ' ['.$env{'form.recuname'}.':'.$env{'form.recdomain'}.']', - $env{'form.newrecord'}); + $recordtxt); } $r->print('

'.&Apache::loncommon::plainname($env{'form.recuname'}, $env{'form.recdomain'}).'

'); @@ -2017,8 +2030,8 @@ sub sendoffmail { my ($r,$folder)=@_; my $suffix=&foldersuffix($folder); my $sendstatus=''; - my %broadcast_status; - my $numbroadcast = 0; + my %specialmsg_status; + my $numspecial = 0; if ($env{'form.send'}) { &printheader($r,'','Messages being sent.'); $r->rflush(); @@ -2064,19 +2077,20 @@ sub sendoffmail { } } - my $basicmsg; + my $savemsg; my $msgtype; + my %sentmessage; if ((($env{'form.critmsg'}) || ($env{'form.sendbck'})) && (&Apache::lonnet::allowed('srm',$env{'request.course.id'}))) { - $basicmsg=&Apache::lonfeedback::clear_out_html($env{'form.message'},1); - $msgtype = '(critical)'; + $savemsg=&Apache::lonfeedback::clear_out_html($env{'form.message'},1); + $msgtype = 'critical'; } else { - $basicmsg=&Apache::lonfeedback::clear_out_html($env{'form.message'}); + $savemsg=&Apache::lonfeedback::clear_out_html($env{'form.message'}); } foreach (keys %toaddr) { my ($recuname,$recdomain)=split(/\:/,$_); - my $msgtxt = $basicmsg; + my $msgtxt = $savemsg; if ($toaddr{$_}) { $msgtxt.='
'.$toaddr{$_}; } my $thismsg; if ((($env{'form.critmsg'}) || ($env{'form.sendbck'})) && @@ -2085,57 +2099,77 @@ sub sendoffmail { $thismsg=&user_crit_msg($recuname,$recdomain, &Apache::lonfeedback::clear_out_html($env{'form.subject'}), $msgtxt, - $env{'form.sendbck'},$env{'form.permanent'}); + $env{'form.sendbck'},$env{'form.permanent'},\$sentmessage{$_}); } else { $r->print(&mt('Sending').' '.$recuname.'@'.$recdomain.': '); $thismsg=&user_normal_msg($recuname,$recdomain, &Apache::lonfeedback::clear_out_html($env{'form.subject'}), $msgtxt, - $content{'citation'},undef,undef,$env{'form.permanent'}); + $content{'citation'},undef,undef,$env{'form.permanent'},\$sentmessage{$_}); } - if (($env{'request.course.id'}) && - ($env{'form.sendmode'} eq 'group')) { - $broadcast_status{$recuname.':'.$recdomain} = $thismsg; + if (($env{'request.course.id'}) && (($msgtype eq 'critical') || + ($env{'form.sendmode'} eq 'group'))) { + $specialmsg_status{$recuname.':'.$recdomain} = $thismsg; if ($thismsg eq 'ok') { - $numbroadcast ++; + $numspecial ++; } } $r->print($thismsg.'
'); $sendstatus.=' '.$thismsg; } - if (($env{'request.course.id'}) && ($env{'form.sendmode'} eq 'group')) { + if (($env{'request.course.id'}) && (($env{'form.sendmode'} eq 'group') + || ($msgtype eq 'critical'))) { my $subj_prefix; if ($msgtype eq 'critical') { - $subj_prefix = 'Critical broadcast'; + $subj_prefix = 'Critical.'; } else { - $subj_prefix = 'Broadcast'; + $subj_prefix = 'Broadcast.'; } - my ($broadmsgid,$broadresult); - if ($numbroadcast) { - $broadresult = &user_normal_msg_raw( - $env{'course.'.$env{'request.course.id'}.'.num'}, - $env{'course.'.$env{'request.course.id'}.'.domain'}, $subj_prefix.' to: '.$env{'course.'.$env{'request.course.id'}.'.description'}. - ' ('.$numbroadcast.' sent)',$basicmsg,undef,undef,undef, - undef,\$broadmsgid); + my ($specialmsgid,$specialresult); + my $cnum = $env{'course.'.$env{'request.course.id'}.'.num'}; + my $cdom = $env{'course.'.$env{'request.course.id'}.'.domain'}; + my $course_str = &Apache::lonnet::escape('['.$cnum.':'.$cdom.']'); + + if ($numspecial) { + $specialresult = &user_normal_msg_raw($cnum,$cdom,$subj_prefix. + ' '.$course_str,$savemsg,undef,undef,undef, + undef,undef,\$specialmsgid); + $specialmsgid = &Apache::lonnet::unescape($specialmsgid); } - if ($broadresult eq 'ok') { + if ($specialresult eq 'ok') { my $record_sent; my @recusers = (); my @recudoms = (); + my ($stamp,$msgsubj,$msgname,$msgdom,$msgcount,$context,$pid) = + split(/\:/,&Apache::lonnet::unescape($specialmsgid)); foreach my $recipient (sort(keys(%toaddr))) { - if ($broadcast_status{$recipient} eq 'ok') { + if ($specialmsg_status{$recipient} eq 'ok') { + my $usersubj = $subj_prefix.'['.$recipient.']'; + my $usermsgid = &buildmsgid($stamp,$usersubj,$msgname, + $msgdom,$msgcount,$context,$pid); + &user_normal_msg_raw($cnum,$cdom,$subj_prefix. + ' ['.$recipient.']',$sentmessage{$recipient}, + undef,undef,undef,undef,$usermsgid); my ($uname,$udom) = split/:/,$recipient; push(@recusers,$uname); push(@recudoms,$udom); } } if (@recusers) { - my $broadmessage; - ($broadmsgid,$broadmessage)=&packagemsg(&Apache::lonfeedback::clear_out_html($env{'form.subject'}),$basicmsg,undef,undef,undef,\@recusers,\@recudoms,$broadmsgid); - $record_sent = &store_sent_mail($broadmsgid,$broadmessage); + my $specialmessage; + my $sentsubj = $subj_prefix.' ('.$numspecial.' sent) '. + &Apache::lonfeedback::clear_out_html($env{'form.subject'}); + $sentsubj = &HTML::Entities::encode($sentsubj,'<>&"'); + my $sentmsgid = &buildmsgid($stamp,$sentsubj,$msgname, + $msgdom,$msgcount,$context,$pid); + ($specialmsgid,$specialmessage) = + &packagemsg(&Apache::lonfeedback::clear_out_html( + $env{'form.subject'}),$savemsg,undef,undef,undef, + \@recusers,\@recudoms,$sentmsgid); + $record_sent = &store_sent_mail($specialmsgid,$specialmessage); } } else { - &Apache::lonnet::logthis('Failed to create record of broadcast in '.$env{'course.'.$env{'request.course.id'}.'.num'}.' at '.$env{'course.'.$env{'request.course.id'}.'.domain'}.' - no msgid generated'); + &Apache::lonnet::logthis('Failed to create record of critical message or broadcast in '.$env{'course.'.$env{'request.course.id'}.'.num'}.' at '.$env{'course.'.$env{'request.course.id'}.'.domain'}.' - no msgid generated'); } } } else {