[LON-CAPA-cvs] cvs: loncom /interface lonmsg.pm

raeburn lon-capa-cvs@mail.lon-capa.org
Tue, 29 Nov 2005 22:41:32 -0000


This is a MIME encoded message

--raeburn1133304092
Content-Type: text/plain

raeburn		Tue Nov 29 17:41:32 2005 EDT

  Modified files:              
    /loncom/interface	lonmsg.pm 
  Log:
  Bug 3999.  Critical and Broadcast messages now handled similarly.  For each recipient a copy of the message is stored in the nohist_email db for the course, with subject Critical. [$user:$domain] or Broadcast. [$user:$domain] to allow for straightfoward construction of the Record page for each user in a course. A separate copy (with a different subjeca)t is stored in the nohist_email_sent db file, to alllow a more meaningful isubject in the sent mail folder display. The timestamp, courseID, and process ID parts of the msgIDs are common to the messages in the two db files.  Also double escaping of message content is no$w used for both Critical and Broadcast so User records will display subject and message in both cases.  Legacy broadcast messages still supported through use of Broadcast. as identifier of new style (compared with Broadcast for old style.           
  
  
--raeburn1133304092
Content-Type: text/plain
Content-Disposition: attachment; filename="raeburn-20051129174132.txt"

Index: loncom/interface/lonmsg.pm
diff -u loncom/interface/lonmsg.pm:1.158 loncom/interface/lonmsg.pm:1.159
--- loncom/interface/lonmsg.pm:1.158	Mon Nov 28 15:14:43 2005
+++ loncom/interface/lonmsg.pm	Tue Nov 29 17:41:30 2005
@@ -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 @@
     }
     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='<sendername>'.$env{'user.name'}.'</sendername>'.
            '<senderdomain>'.$env{'user.domain'}.'</senderdomain>'.
@@ -229,6 +225,13 @@
 
 # ======================================================= 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 @@
 # ================================================== 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 @@
            '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 @@
 =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 @@
 	 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_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 @@
     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 @@
 # 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 @@
     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 {
     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 @@
 	 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 @@
 	    $result.='<h3>'.&mt('Record').'</h3>';
         } elsif ($content{'subject'}=~/^Broadcast/) {
             $result .='<h3>'.&mt('Broadcast Message').'</h3>';
+            if ($content{'subject'}=~/^Broadcast\./) {
+                %content=&unpackagemsg($content{'message'});
+                $content{'message'}=
+                    '<b>'.&mt('Subject').': '.$content{'subject'}.'</b><br />'.
+                    $content{'message'};
+            }    
         } else {
             $result.='<h3>'.&mt('Critical Message').'</h3>';
             %content=&unpackagemsg($content{'message'});
@@ -1421,12 +1433,13 @@
         ($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('<h3>'.&Apache::loncommon::plainname($env{'form.recuname'},
 				     $env{'form.recdomain'}).'</h3>');
@@ -2017,8 +2030,8 @@
     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 @@
 	    }
 	}
 
-        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.='<hr />'.$toaddr{$_}; }
 	    my $thismsg;
 	    if ((($env{'form.critmsg'}) || ($env{'form.sendbck'})) && 
@@ -2085,57 +2099,77 @@
 		$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.'<br />');
 	    $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 {

--raeburn1133304092--