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

raeburn lon-capa-cvs@mail.lon-capa.org
Wed, 23 Nov 2005 22:32:11 -0000


This is a MIME encoded message

--raeburn1132785131
Content-Type: text/plain

raeburn		Wed Nov 23 17:32:11 2005 EDT

  Modified files:              
    /loncom/interface	lonmsg.pm 
  Log:
  Bug 3946.  Only a single copy of a broadcast message is stored in the "Sent" Folder.  To: line now continues list of (multiple) recipients.
  
  
--raeburn1132785131
Content-Type: text/plain
Content-Disposition: attachment; filename="raeburn-20051123173211.txt"

Index: loncom/interface/lonmsg.pm
diff -u loncom/interface/lonmsg.pm:1.155 loncom/interface/lonmsg.pm:1.156
--- loncom/interface/lonmsg.pm:1.155	Fri Nov 18 08:35:30 2005
+++ loncom/interface/lonmsg.pm	Wed Nov 23 17:32:11 2005
@@ -1,7 +1,7 @@
 # The LearningOnline Network with CAPA
 # Routines for messaging
 #
-# $Id: lonmsg.pm,v 1.155 2005/11/18 13:35:30 www Exp $
+# $Id: lonmsg.pm,v 1.156 2005/11/23 22:32:11 raeburn Exp $
 #
 # Copyright Michigan State University Board of Trustees
 #
@@ -126,7 +126,7 @@
 
 sub packagemsg {
     my ($subject,$message,$citation,$baseurl,$attachmenturl,
-	$recuser,$recdomain)=@_;
+	$recuser,$recdomain,$msgid)=@_;
     $message =&HTML::Entities::encode($message,'<>&"');
     $citation=&HTML::Entities::encode($citation,'<>&"');
     $subject =&HTML::Entities::encode($subject,'<>&"');
@@ -141,10 +141,12 @@
     $msgcount++;
     my $partsubj=$subject;
     $partsubj=&Apache::lonnet::escape($partsubj);
-    my $msgid=&Apache::lonnet::escape(
+    unless(defined($msgid)) {
+        $msgid=&Apache::lonnet::escape(
            $now.':'.$partsubj.':'.$env{'user.name'}.':'.
            $env{'user.domain'}.':'.$msgcount.':'.
            $env{'request.course.id'}.':'.$$);
+    }
     my $result='<sendername>'.$env{'user.name'}.'</sendername>'.
            '<senderdomain>'.$env{'user.domain'}.'</senderdomain>'.
            '<subject>'.$subject.'</subject>'.
@@ -161,10 +163,17 @@
 	   '<coursesec>'.$env{'request.course.sec'}.'</coursesec>'.
 	   '<role>'.$env{'request.role'}.'</role>'.
 	   '<resource>'.$env{'request.filename'}.'</resource>'.
-           '<msgid>'.$msgid.'</msgid>'.
-	   '<recuser>'.$recuser.'</recuser>'.
-	   '<recdomain>'.$recdomain.'</recdomain>'.
-	   '<message>'.$message.'</message>';
+           '<msgid>'.$msgid.'</msgid>';
+    if (ref($recuser) eq 'ARRAY') {
+        for (my $i=0; $i<@{$recuser}; $i++) {
+            $result .= '<recuser>'.$$recuser[$i].'</recuser>'.
+                       '<recdomain>'.$$recdomain[$i].'</recdomain>';
+        }
+    } else {
+        $result .= '<recuser>'.$recuser.'</recuser>'.
+                   '<recdomain>'.$recdomain.'</recdomain>';
+    }
+    $result .= '<message>'.$message.'</message>';
     if (defined($citation)) {
 	$result.='<citation>'.$citation.'</citation>';
     }
@@ -188,7 +197,11 @@
        if ($token->[0] eq 'S') {
 	   my $entry=$token->[1];
            my $value=$parser->get_text('/'.$entry);
-           $content{$entry}=$value;
+           if (($entry eq 'recuser') || ($entry eq 'recdomain')) {
+               push(@{$content{$entry}},$value);
+           } else {
+               $content{$entry}=$value;
+           }
        }
     }
     if ($content{'attachmenturl'}) {
@@ -474,7 +487,7 @@
 
 sub user_normal_msg_raw {
     my ($user,$domain,$subject,$message,$citation,$baseurl,$attachmenturl,
-	$toperm)=@_;
+	$toperm,$newid)=@_;
 # Check if allowed missing
     my $status='';
     my $msgid='undefined';
@@ -492,15 +505,16 @@
 # Save new message received time
        &Apache::lonnet::put
                          ('email_status',{'recnewemail'=>time},$domain,$user);
-# Into sent-mail folder
-       $status.=' '.&Apache::lonnet::critical(
-           'put:'.$env{'user.domain'}.':'.$env{'user.name'}.
-					      ':nohist_email_sent:'.
-           &Apache::lonnet::escape($msgid).'='.
-           &Apache::lonnet::escape($message),$env{'user.home'});
+# Into sent-mail folder unless a broadcast message
+       unless (($env{'request.course.id'}) && ($env{'form.sendmode'} eq 'group')) {
+           $status .= &store_sent_mail($msgid,$message);
+       }
     } else {
        $status='no_host';
     }
+    if (defined($newid)) {
+        $$newid = $msgid;
+    }
 # Notifications
     my %userenv = &Apache::lonnet::get('environment',['notification',
                                                       'permanentemail'],
@@ -550,6 +564,15 @@
     return $status;
 }
 
+sub store_sent_mail {
+    my ($msgid,$message) = @_;
+        my $status =' '.&Apache::lonnet::critical(
+                   'put:'.$env{'user.domain'}.':'.$env{'user.name'}.
+                                              ':nohist_email_sent:'.
+                   &Apache::lonnet::escape($msgid).'='.
+                   &Apache::lonnet::escape($message),$env{'user.home'});
+    return $status;
+}
 
 # ============================================================ List all folders
 
@@ -1826,6 +1849,16 @@
 	}
 	$r->print('</tr></table>');
     }
+    my $tolist;
+    my @recipients = ();
+    for (my $i=0; $i<@{$content{'recuser'}}; $i++) {
+        $recipients[$i] =  &Apache::loncommon::aboutmewrapper(
+           &Apache::loncommon::plainname($content{'recuser'}[$i],
+                                      $content{'recdomain'}[$i]),
+              $content{'recuser'}[$i],$content{'recdomain'}[$i]).
+       ' ('.$content{'recuser'}[$i].' at '.$content{'recdomain'}[$i].') ';
+    }
+    $tolist = join(', ',@recipients);
     $r->print('<br /><b>'.&mt('Subject').':</b> '.$content{'subject'}.
 	      ($folder ne 'sent'?'<br /><b>'.&mt('From').':</b> '.
 	      &Apache::loncommon::aboutmewrapper(
@@ -1833,11 +1866,7 @@
 						 $content{'sendername'},$content{'senderdomain'}).' ('.
 	      $content{'sendername'}.' at '.
 	      $content{'senderdomain'}.') ':'<br /><b>'.&mt('To').':</b> '.
-	      &Apache::loncommon::aboutmewrapper(
-						 &Apache::loncommon::plainname($content{'recuser'},$content{'recdomain'}),
-						 $content{'recuser'},$content{'recdomain'}).' ('.
-	      $content{'recuser'}.' at '.
-	      $content{'recdomain'}.') ').
+              $tolist).
 	      ($content{'courseid'}?'<br /><b>'.&mt('Course').':</b> '.$courseinfo{'description'}.
 	       ($content{'coursesec'}?' ('.&mt('Group/Section').': '.$content{'coursesec'}.')':''):'').
 	      '<br /><b>'.&mt('Time').':</b> '.$content{'time'}.
@@ -1952,6 +1981,8 @@
     my ($r,$folder)=@_;
     my $suffix=&foldersuffix($folder);
     my $sendstatus='';
+    my %broadcast_status;
+    my $numbroadcast = 0;
     if ($env{'form.send'}) {
 	&printheader($r,'','Messages being sent.');
 	$r->rflush();
@@ -1996,18 +2027,22 @@
 		$toaddr{$auname.':'.$audom}='';
 	    }
 	}
+
+        my $basicmsg;
+        my $msgtype;
+        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)';
+        } else {
+            $basicmsg=&Apache::lonfeedback::clear_out_html($env{'form.message'});
+        }
 	
 	foreach (keys %toaddr) {
 	    my ($recuname,$recdomain)=split(/\:/,$_);
-            my $msgtxt;
-            if ((($env{'form.critmsg'}) || ($env{'form.sendbck'})) &&
-                (&Apache::lonnet::allowed('srm',$env{'request.course.id'}))) {
-                $msgtxt=&Apache::lonfeedback::clear_out_html($env{'form.message'},1);
-            } else {  
-	        $msgtxt=&Apache::lonfeedback::clear_out_html($env{'form.message'});
-            }
+            my $msgtxt = $basicmsg;
 	    if ($toaddr{$_}) { $msgtxt.='<hr />'.$toaddr{$_}; }
-	    my $thismsg;    
+	    my $thismsg;
 	    if ((($env{'form.critmsg'}) || ($env{'form.sendbck'})) && 
 		(&Apache::lonnet::allowed('srm',$env{'request.course.id'}))) {
 		$r->print(&mt('Sending critical message').' '.$recuname.'@'.$recdomain.': ');
@@ -2021,17 +2056,52 @@
 					  &Apache::lonfeedback::clear_out_html($env{'form.subject'}),
 					  $msgtxt,
 					  $content{'citation'},undef,undef,$env{'form.permanent'});
-		if (($env{'request.course.id'}) && ($env{'form.sendmode'} eq 'group')) {
-		    &user_normal_msg_raw(
-					 $env{'course.'.$env{'request.course.id'}.'.num'},
-					 $env{'course.'.$env{'request.course.id'}.'.domain'},
-					 'Broadcast ['.$recuname.':'.$recdomain.']',
-					 $msgtxt);
-		}
+            }
+	    if (($env{'request.course.id'}) && 
+                                         ($env{'form.sendmode'} eq 'group')) {
+	        $broadcast_status{$recuname.':'.$recdomain}  = $thismsg;
+                if ($thismsg eq 'ok') {
+                    $numbroadcast ++;
+                }
 	    }
 	    $r->print($thismsg.'<br />');
 	    $sendstatus.=' '.$thismsg;
 	}
+        if (($env{'request.course.id'}) && ($env{'form.sendmode'} eq 'group')) {
+            my $subj_prefix;
+            if ($msgtype eq 'critical') {
+                $subj_prefix = 'Critical broadcast';
+            } else {
+                $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);
+            }
+            if ($broadresult eq 'ok') {
+                my $record_sent;
+                my @recusers = ();
+                my @recudoms = ();
+                foreach my $recipient (sort(keys(%toaddr))) {
+                    if ($broadcast_status{$recipient} eq 'ok') {
+                        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);
+                }
+            } 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');
+            }
+        }
     } else {
 	&printheader($r,'','No messages sent.'); 
     }

--raeburn1132785131--