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

raeburn lon-capa-cvs-allow@mail.lon-capa.org
Tue, 04 Nov 2008 06:22:53 -0000


This is a MIME encoded message

--raeburn1225779773
Content-Type: text/plain

raeburn		Tue Nov  4 06:22:53 2008 EDT

  Modified files:              
    /loncom/interface	lonmsgdisplay.pm 
  Log:
  Bug 5276.
  -  Recipient type - To: or Cc: is preserved when displaying a message you received.
  -  When replying can choose which of the To: and Cc: to include in the reply.
  -  Additional recipients box includes separate To:, Cc: and Bcc: textboxes.
  
  Message composition screen
  - Made more compact
    - "Preview and Check Spelling" on same line as "Send" and "Cancel" buttons
    - "Attachment" row moved above main composition textarea
    - Now one "Critical message" checkbox with radio buttons for return receipt option
   
  Message display
  - Link to launch pop-up window to show broadcast recipients when displaying
    a sent message.  
    
  Broadcast Messages
  - <div> used to create two column-format.
     - Selectors for broadcast recipients in left column.
     - Standard composition elements in right column.   
  - <fieldset> used to bound each status-based list of potential recipients     
  
  Group broadcast
  - Group members with sgb priv (or mdg or vgr priv) can elect broadcast message to be Cc or Bcc when sending to active group members.
  - If message recipient views message received from a group broadcast when ourse role is selected (in corresponding course), broadcast reply is possible, if group broadcast was originally Cc type, and viewer has sgb, mdg or vgr priv.
  
  - Checking of group broadcast privs moved to new routine: &check_group_priv()
  
  New keys in reciphash stored in nohist_emailrecip.db for sender:
    'course_broadcast','group_cc_broadcast','group_bcc_broadcast'
  - allow retrieval of information about broadcast recipients/type. 
  
  
--raeburn1225779773
Content-Type: text/plain
Content-Disposition: attachment; filename="raeburn-20081104062253.txt"

Index: loncom/interface/lonmsgdisplay.pm
diff -u loncom/interface/lonmsgdisplay.pm:1.94 loncom/interface/lonmsgdisplay.pm:1.95
--- loncom/interface/lonmsgdisplay.pm:1.94	Thu Oct 23 10:15:37 2008
+++ loncom/interface/lonmsgdisplay.pm	Tue Nov  4 06:22:53 2008
@@ -1,7 +1,7 @@
 # The LearningOnline Network with CAPA
 # Routines for messaging display
 #
-# $Id: lonmsgdisplay.pm,v 1.94 2008/10/23 10:15:37 bisitz Exp $
+# $Id: lonmsgdisplay.pm,v 1.95 2008/11/04 06:22:53 raeburn Exp $
 #
 # Copyright Michigan State University Board of Trustees
 #
@@ -502,7 +502,8 @@
 # ======================================================= Display a course list
 
 sub discourse {
-    my $result;
+    my ($statushash) = @_;
+    my ($result,$active,$previous,$future);
     my ($course_personnel,
 	$current_members,
 	$expired_members,
@@ -511,41 +512,50 @@
     unshift @$current_members, (@$course_personnel);
     my %defaultUsers;
     
-    $result .= '<input type="hidden" name="sendmode" value="group" />'."\n";
-
     my $tmptext;
     if ($tmptext = &Apache::lonselstudent::render_student_list($current_members,
-                                                               "compemail",
+                                                               "activeusers",
                                                                "current",
                                                                \%defaultUsers,
-                                                               1,"selectedusers",1)
+                                                               1,"selectedusers",1,'email')
        ) {
-       $result .= '<h2>'.&mt('Course members with current access').'</h2>';
-       $result .= $tmptext;
+       $result .= '<fieldset id="LC_activeusers"><legend><b>'.&mt('Bcc: course members with current access').'</b></legend><form name="activeusers">';
+       $result .= $tmptext.'</form></fieldset><br />';
+       if (ref($statushash) eq 'HASH') {
+           $statushash->{'active'} = 1;
+       }
     }
     if ($tmptext = &Apache::lonselstudent::render_student_list($expired_members,
-                                                               "compemail",
+                                                               "previoususers",
                                                                "expired",
                                                                \%defaultUsers,
-                                                               1, "selectedusers",0)
+                                                               1, "selectedusers",0,'email')
        ) {
-       $result .= '<h2>'.&mt('Course members with expired access').'</h2>';
-       $result .= $tmptext;
+       $result .= '<fieldset id="LC_previoususers"><legend><b>'.&mt('Bcc: course members with expired access').'</b></legend><form name="previoususers">';
+       $result .= $tmptext.'</form></fieldset><br />';
+       if (ref($statushash) eq 'HASH') {
+           $statushash->{'previous'} = 1;
+       }
+
     }
     if ($tmptext = &Apache::lonselstudent::render_student_list($future_members,
-                                                               "compemail",
+                                                               "futureusers",
                                                                "future",
                                                                \%defaultUsers,
-                                                               1, "selectedusers", 0)
+                                                               1, "selectedusers",0,'email')
        ) {
-       $result .= '<h2>'.&mt('Course members with future access').'</h2>';
-       $result .= $tmptext;
+       $result .= '<fieldset id="LC_futureusers"><legend><b>'.&mt('Bcc: course members with future access').'</b></legend><form name="previoususers">';
+       $result .= $tmptext.'</form></fieldset>';
+       if (ref($statushash) eq 'HASH') {
+           $statushash->{'future'} = 1;
+       }
+
     }
     return $result;
 }
 
 sub disgroup {
-    my ($cdom,$cnum,$group,$viewgrps,$editgrps) = @_;
+    my ($cdom,$cnum,$group,$access_status) = @_;
     my $result;
     #  Needs to be in a course
     if (!($env{'request.course.fn'})) {
@@ -559,6 +569,10 @@
     my ($memberinfo,$numitems) =
                  &Apache::longroup::group_memberlist($cdom,$cnum,$group,{},[]);
     my @statustypes = ('active');
+    my $viewgrps = &Apache::lonnet::allowed('vcg',$env{'request.course.id'}.
+                   ($env{'request.course.sec'}?'/'.$env{'request.course.sec'}:''));
+    my $editgrps = &Apache::lonnet::allowed('mdg',$env{'request.course.id'}.
+                   ($env{'request.course.sec'}?'/'.$env{'request.course.sec'}:''));
     if ($viewgrps || $editgrps) {
         push(@statustypes,('future','previous'));
     }
@@ -567,7 +581,6 @@
                       'recipients to select.');
         return $result;
     } else {
-        $result = &mt('Select message recipients from the group members listed below.').'<br />';  
         my %Sortby = (
                          active   => {},
                          previous => {},
@@ -577,9 +590,9 @@
                                      'name'     => 'Name',
                                      'usnm'     => 'Username',
                                      'doma'     => 'Domain',
-                                     'active'   => 'Active Members',
-                                     'previous' => 'Former Members',
-                                     'future'   => 'Future Members',
+                                     'active'   => 'Broadcast to Active Members',
+                                     'previous' => 'Broadcast (Bcc) to Former Members',
+                                     'future'   => 'Broadcast (Bcc) to Future Members',
                                     );
         foreach my $user (sort(keys(%{$memberinfo}))) {
             my $status = $$memberinfo{$user}{status};
@@ -594,26 +607,34 @@
             }
         }
         $result .= &group_check_uncheck();
-        $result .= '<table border="0" cellspacing="8" cellpadding="2">'.
-                   '<tr>';
         foreach my $status (@statustypes)  {
             if (ref($numitems) eq 'HASH') {
                 if ((defined($$numitems{$status})) && ($$numitems{$status})) {
-                    $result.='<td valign="top">'.
-                             '<fieldset><legend><b>'.$lt{$status}.
-                             '</b></legend><nobr>'.
+                    my $formname = $status.'users';
+                    if (ref($access_status) eq 'HASH') {
+                        $access_status->{$status} = $$numitems{$status};
+                    }
+                    $result.='<fieldset><legend><b>'.$lt{$status}.
+                             '</b></legend><form name="'.$formname.'">'.
+                             '<span class="LC_nobreak">'.
                              '<input type="button" value="'.&mt('Check All').'" '.
-                             'onclick="javascript:toggleAll('."'".$status."','check'".')" />'.
+                             'onclick="javascript:toggleAll('."this.form,'check'".')" />'.
                              '&nbsp;&nbsp;'.
                              '<input type="button" value="'.&mt('Uncheck All').'" '.
-                             'onclick="javascript:toggleAll('."'".$status."','uncheck'".')" />'.
-                             '</nobr></fieldset><br />'.
-                             &Apache::loncommon::start_data_table().
-                             &Apache::loncommon::start_data_table_header_row();
-                    $result .= "<th>$lt{'name'}</a></th>".
+                             'onclick="javascript:toggleAll('."this.form,'uncheck'".')" />'.
+                             '</span>';
+                    if ($status eq 'active') {
+                        $result .= '&nbsp;&nbsp;&nbsp;<select name="groupmail">'.
+                                   '<option value="bcc" selected="selected">'.&mt('Bcc').'</option>'.
+                                   '<option value="cc">'.&mt('Cc').'</option>'.
+                               '</select>';
+                    }
+                    $result .= '<br />'.&Apache::loncommon::start_data_table().
+                               &Apache::loncommon::start_data_table_header_row().
+                               "<th>$lt{'name'}</a></th>".
                                "<th>$lt{'usnm'}</a></th>".
                                "<th>$lt{'doma'}</a></th>".
-                    &Apache::loncommon::end_data_table_header_row();
+                               &Apache::loncommon::end_data_table_header_row();
                     foreach my $key (sort(keys(%{$Sortby{$status}}))) {
                         foreach my $user (@{$Sortby{$status}{$key}}) {
                             $result .=
@@ -627,12 +648,10 @@
                                 &Apache::loncommon::end_data_table_row();
                         }
                     }
-                    $result .= &Apache::loncommon::end_data_table();
+                    $result .= &Apache::loncommon::end_data_table().'</form></fieldset><br />';
                 }
             }
-            $result .= '</td><td>&nbsp;&nbsp;</td>';
         }
-        $result .= '</tr></table>';
     }
     return $result;
 }
@@ -640,24 +659,19 @@
 sub group_check_uncheck {
     my $output = qq|
 <script type="text/javascript">
-function toggleAll(caller,action) {
-    var pattern = new RegExp(":"+caller+"\$");
-    if (typeof(document.compemail.selectedusers_forminput.length)=="undefined") {
-        if (document.compemail.selectedusers_forminput.value.match(pattern)) {
-            if (action == 'check') {
-                document.groupmail.selectedusers_forminput.checked = true;
-            } else {
-                document.groupmail.selectedusers_forminput.checked = false;
-            }
+function toggleAll(form,action) {
+    if (typeof(form.selectedusers_forminput.length)=="undefined") {
+         if (action == 'check') {
+            form.selectedusers_forminput.checked = true;
+         } else {
+            form.selectedusers_forminput.checked = false;
         }
     } else {
-        for (var i=0; i<document.compemail.selectedusers_forminput.length; i++) {
-            if (document.compemail.selectedusers_forminput[i].value.match(pattern)) {
-                if (action == 'check') {
-                    document.compemail.selectedusers_forminput[i].checked = true;
-                } else {
-                    document.compemail.selectedusers_forminput[i].checked = false;
-                }
+        for (var i=0; i<form.selectedusers_forminput.length; i++) {
+            if (action == 'check') {
+                form.selectedusers_forminput[i].checked = true;
+            } else {
+                form.selectedusers_forminput[i].checked = false;
             }
         }
     }
@@ -1256,43 +1270,99 @@
                                        'tff' => 'The file format for the uploaded portion of the message is',
                                        'uas' => 'Upload and Send',
                                        'atta' => 'Attachment',
+                                       'to'   => 'To:',
                                       );
     my %attachmax = (
                      text => &mt('(128 KB max size)'),
                      num  => 131072,
                     );
     if (!$forwarding && !$multiforward) {
-        $attachrow = '<br />'.$lt{'atta'}.' '.$attachmax{'text'}.': <input type="file" name="attachment" />';
+        $attachrow = '<tr><td colspan="3"><b>'.$lt{'atta'}.'</b> '.$attachmax{'text'}.': <input type="file" name="attachment" /></td></tr>';
     }
     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=
- '<p><label><input type="checkbox" name="critmsg" /> '.&mt('Send as critical message').'</label> ' . $crithelp . 
- '</p><p>'.
- '<label><input type="checkbox" name="sendbck" /> '.&mt('Send as critical message').'  ' .
- &mt('and return receipt') . '</label>' . $crithelp . 
- '</p><p><label><input type="checkbox" name="permanent" /> '.
-&mt('Send copy to permanent email address (if known)').'</label></p>'.
-'<p><label><input type="checkbox" name="rsspost" /> '.
-		  &mt('Include in course RSS newsfeed').'</label></p>';
+ '<span class="LC_nobreak"><label><input type="checkbox" name="critmsg" /> '.&mt('Send as critical message').'.</label>'.$crithelp.'&nbsp;&nbsp;'.&mt('Require return receipt?').'<label><input type="radio" name="sendbck" value="1" />'.&mt('Yes').'</label>&nbsp;&nbsp;<label><input type="radio" name="sendbck" value="" checked="checked" />'.&mt('No').'</label></span><br />'.
+ '<label><input type="checkbox" name="permanent" /> '.
+&mt('Send copy to permanent email address (if known)').'</label><br />'.
+'<label><input type="checkbox" name="rsspost" /> '.
+		  &mt('Include in course RSS newsfeed').'</label><br />';
     }
     if ($broadcast ne 'group') {
         if (&Apache::lonnet::allowed('dff',$env{'request.course.id'}) ||
             &Apache::lonnet::allowed('dff',$env{'request.course.id'}.
                                      '/'.$env{'request.course.sec'})) {
 
-            $dispcrit.='<p><label>'.
+            $dispcrit.='<label>'.
                        '<input type="checkbox" name="courserecord" value="1" /> '.
                        &mt("Include in course's 'User records' for recipient(s)").
-                       '</label></p>';
+                       '</label><br />';
         }
     }
 
     my %message;
     my %content;
+    my ($hasfloat,$broadcast_js,$sendmode,$can_grp_broadcast);
     my $defdom=$env{'user.domain'};
+    if ($broadcast eq 'group') {
+        my %access_status = (
+                           active => 0,
+                           previous => 0,
+                           future => 0,
+                      );
+ 
+        if ($group eq '') {
+            my $studentsel = &discourse(\%access_status);
+            if ($studentsel) {
+                $r->print('<div class="LC_left_float">'.$studentsel.'</div>');
+                $hasfloat = 1;
+            }
+        } else {
+            $can_grp_broadcast = &check_group_priv($group);
+            if ($can_grp_broadcast) {
+                $r->print('<div class="LC_left_float">'.
+                          &disgroup($cdom,$cnum,$group,\%access_status).
+                         '</div>');
+                $hasfloat = 1;
+            }
+        }
+        if ($hasfloat) {
+            $sendmode = '<input type="hidden" name="sendmode" value="group" />'."\n";
+            $broadcast_js = qq|
+<script type="text/javascript">
+function courseRecipients() {
+|;
+        foreach my $type (keys(%access_status)) {
+            if ($access_status{$type}) {
+                my $formname = $type.'users';
+                if ($type eq 'active' && $group ne '') {
+                    $broadcast_js .= qq|
+                    document.compemail.groupmail.value = document.$formname.groupmail[document.$formname.groupmail.selectedIndex].value;
+|;
+                }
+                $broadcast_js .= qq|
+    if (typeof(document.$formname.selectedusers_forminput.length)=="undefined") {
+        document.compemail.courserecips.value += '_&&&_'+document.$formname.selectedusers_forminput.value;
+    } else {
+        for (var i=0; i<document.$formname.selectedusers_forminput.length; i++) {
+            if (document.$formname.selectedusers_forminput[i].checked) {
+                document.compemail.courserecips.value += '_&&&_'+document.$formname.selectedusers_forminput[i].value;  
+            }
+        }
+    }
+                |;
+            }
+        }
+        $broadcast_js .= qq|
+    return;
+}
+</script>
+
+|;
+        }
+    }
     if ($forwarding) {
 	%message=&Apache::lonnet::get('nohist_email'.$suffix,[$forwarding]);
 	%content=&Apache::lonmsg::unpackagemsg($message{$forwarding},$folder);
@@ -1336,40 +1406,29 @@
 ENDREPSCRIPT
     }
     my $citation=&displayresource(%content);
-    my ($can_grp_broadcast,$viewgrps,$editgrps);
+    my $onsubmit;
     if ($env{'form.recdom'}) { $defdom=$env{'form.recdom'}; }
     if ($env{'form.text'}) { $dismsg=$env{'form.text'}; }
     if ($env{'form.subject'}) { $dissub=$env{'form.subject'}; }
+    if ($hasfloat) {
+        $r->print($broadcast_js.'<div class="LC_left_float">');
+        $onsubmit = ' onsubmit="javascript:courseRecipients();" ';
+    }
     $r->print(
                 '<form action="/adm/email"  name="compemail" method="post"'.
-                ' enctype="multipart/form-data">'."\n".
-                '<input type="hidden" name="sendmail" value="on" />'."\n");
-    if ($broadcast eq 'group' && $env{'form.group'} ne '') {
-        $can_grp_broadcast = 
-                &Apache::lonnet::allowed('sgb',$env{'request.course.id'}.'/'.
-                                         $group);
-        $viewgrps = 
-               &Apache::lonnet::allowed('vcg',$env{'request.course.id'}.
-               ($env{'request.course.sec'}?'/'.$env{'request.course.sec'}:''));
-        $editgrps = 
-               &Apache::lonnet::allowed('mdg',$env{'request.course.id'}.
-               ($env{'request.course.sec'}?'/'.$env{'request.course.sec'}:''));
-        if ($viewgrps || $editgrps || $can_grp_broadcast) {
-            $r->print(&disgroup($cdom,$cnum,$group,$viewgrps,$editgrps));
-        }
-    }
-    $r->print('<table>');
-    if (($broadcast eq 'group') && ($group ne '') && 
-        (!$can_grp_broadcast && !$viewgrps && !$editgrps)) {
+                ' enctype="multipart/form-data"'.$onsubmit.'>'."\n".
+                '<input type="hidden" name="sendmail" value="on" />'."\n".
+                '<table>');
+    if (($broadcast eq 'group') && ($group ne '') && (!$can_grp_broadcast)) {
         $r->print(&recipient_input_row($cdom,%lt));
-    } 
+    }
     if (($broadcast ne 'group') && ($broadcast ne 'upload')) {
 	if ($replying) {
             if ($content{'noreplies'}) {
                 $r->print('<tr><td>'.&mt('This message was designated by the sender not to allow replies.').'</td></tr></table></form>');
                 return;
             }
-            $r->print('<tr><td colspan="2">'.&mt('Replying to').' ');
+            $r->print('<tr><td colspan="3"><b>'.&mt('Replying to').'</b> ');
             if ($content{'replytoaddr'}) {
                 my ($replytoname,$replytodom) = split(/:/,$content{'replytoaddr'});
                 if ($replytoname ne '' && $replytodom ne '') {
@@ -1396,9 +1455,31 @@
                           $content{'senderdomain'}.'" /></td></tr>');
             }
             if ($content{'recipid'}) {
-                my @ccs = &retrieve_cc_recips('replying',%content);
-                if (@ccs > 0) {
-                    my $replyall = qq|
+                my %recips;
+                &retrieve_recips('replying',\%content,\%recips);
+                if (ref($recips{'to'}) eq 'ARRAY') {
+                    if (@{$recips{'to'}} > 0) {
+                        my $replyall;
+                        if (@{$recips{'to'}} > 1) {
+                            $replyall = qq|
+ <span class="LC_nobreak">
+      <input type="button" value="check all"
+        onclick="javascript:checkAll(document.compemail.replying_to)" />
+        &nbsp;&nbsp;
+      <input type="button" value="uncheck all"
+        onclick="javascript:uncheckAll(document.compemail.replying_to)" />
+ </span>
+|;
+                        }
+                        my $tolist = join(' ',@{$recips{'to'}});
+                        $r->print('<tr><td colspan="3"><table><tr><td>'.&mt('[_1]Send reply[_2] to other recipients','<b>','</b>').':<br />'.$replyall.'</td><td>'.$tolist.'</td></tr></table></td></tr>');
+                    }
+                }
+                if (ref($recips{'cc'}) eq 'ARRAY') {
+                    if (@{$recips{'cc'}} > 0) {
+                        my $replyall;
+                        if (@{$recips{'cc'}} > 1) {
+                            $replyall = qq|
  <span class="LC_nobreak">
       <input type="button" value="check all"
         onclick="javascript:checkAll(document.compemail.replying_cc)" />
@@ -1407,15 +1488,40 @@
         onclick="javascript:uncheckAll(document.compemail.replying_cc)" />
  </span>
 |;
-                    my $cclist = join(' ',@ccs);
-                    $r->print('<tr><td>'.&mt('Reply to other recipients').':<br />'.$replyall.'</td><td>'.$cclist.'</td></tr>');
+                        }
+                        my $cclist = join(' ',@{$recips{'cc'}});
+                        $r->print('<tr><td colspan="3"><table><tr><td>'.&mt('[_1]Cc[_2] to other copied recipients','<b>','</b>').':<br />'.$replyall.'</td><td>'.$cclist.'</td></tr></table></td></tr>');
+                    }
+                }
+                if ($content{'group'} ne '') {
+                    if (&check_group_priv($content{'group'})) {
+                        if (ref($recips{'group_cc_broadcast'}) eq 'ARRAY') {
+                            if (@{$recips{'group_cc_broadcast'}} > 0) {
+                                my $replyall;
+                                if (@{$recips{'group_cc_broadcast'}} > 1) {
+                                    $replyall = qq|
+ <span class="LC_nobreak">
+      <input type="button" value="check all"
+        onclick="javascript:checkAll(document.compemail.replying_groupcc)" />
+        &nbsp;&nbsp;
+      <input type="button" value="uncheck all"
+        onclick="javascript:uncheckAll(document.compemail.replying_groupcc)" />
+ </span>
+|;
+                                }
+                                my $groupcclist = join(' ',@{$recips{'group_cc_broadcast'}});
+                                $r->print('<tr><td colspan="3"><table><tr><td>'.&mt('[_1]Cc[_2] to other copied group members','<b>','</b>').':<br />'.$replyall.'</td><td>'.$groupcclist.'<input type="hidden" name="group" value="'.$content{'group'}.'" /><input type="hidden" name="sendmode" value="group" /><input type="hidden" name="groupmail" value="cc" /></td></tr></table></td></tr>');
+                            }
+                        }
+                    }
                 }
             }
 	} else {
             $r->print(&recipient_input_row($defdom,%lt));
         }
     }
-    my $latexHelp = Apache::loncommon::helpLatexCheatsheet();
+    my $latexHelp = &Apache::loncommon::helpLatexCheatsheet();
+    my $wysiwyglink=&Apache::lonhtmlcommon::htmlareaselectactive('message').'<br />';
     my $subj_size;
     if ($multiforward) {
         $r->print(&additional_rec_row(\%lt));
@@ -1433,11 +1539,12 @@
         $r->print(&msg_subject_row($dissub,\%lt,$subj_size,$extra));
         $r->print('<tr><td>'.&mt('Message begins with:').'</td><td><input type="text" name="msgheader" value="'.$dismsg.'" />&nbsp;'.$sender.'&nbsp;&nbsp;&nbsp;<input type="radio" name="showorigsender" value="1" checked="checked" />'.&mt('Yes').'&nbsp;<input type="radio" name="showorigsender" value="0" />'.&mt('No').'<input type="hidden" name="multiforward" value="'.$multiforward.'" /></td></tr>
 </table>
-<br />'.
-$latexHelp.
-&mt("Any new text to display before the text of the original messages:").'<br />
-<textarea name="message" id="message" cols="80" rows="5" wrap="hard">
-</textarea></p><br />');
+<br /><table>
+<tr><td align="left">'."\n".
+$latexHelp."<br />\n".
+&mt("Any new text to display before the text of the original messages:").'<br />'."\n".
+'<textarea name="message" id="message" cols="80" rows="5" wrap="hard"></textarea>'.
+$wysiwyglink);
         my @to_forward = &Apache::loncommon::get_env_multiple('form.delmark');
         foreach my $msg (@to_forward) {
             $r->print('<input type="hidden" name="delmark" value="'.$msg.'" />');
@@ -1454,18 +1561,17 @@
         }
         $r->print(&msg_subject_row($dissub,\%lt,$subj_size));
         $r->print(<<"ENDCOMP");
-</table>
+$attachrow
+</table><br />
 $latexHelp
 <textarea name="message" id="message" cols="80" rows="15" wrap="hard">$dismsg
-</textarea>
-$attachrow
-</p>
-<br />
+</textarea>$wysiwyglink
+$sendmode
 $dispcrit
 $disbase
 ENDCOMP
         $r->print(&submit_button_row($folder,$dismode,&mt($func1.' '.$func2{'ma'}),
-                                     \%lt));
+                                     \%lt,$hasfloat,$group));
         $r->print($citation);
         if (exists($env{'form.ref'})) {
             $r->print('<input type="hidden" name="ref" value="'.
@@ -1485,7 +1591,7 @@
 </p>
 <p>$lt{'gmt'}:<br />
 <textarea name="message" id="message" cols="60" rows="10" wrap="hard">$dismsg
-</textarea></p>
+</textarea>$wysiwyglink</p>
 <p>
 $lt{'tff'}:
 ENDBLOCK
@@ -1506,22 +1612,30 @@
 <input type="submit" value="$lt{'uas'}" /></p>
 ENDUPLOAD
     }
-    if ($broadcast eq 'group') {
-       if ($group eq '') {
-           my $studentsel = &discourse();
-           $r->print($studentsel);
-       }
-    }
     if ($env{'form.displayedcrit'}) {
 	$r->print('<input type="hidden" name="displayedcrit" value="true" />');
     }
-    $r->print('</form>'.
-	      &Apache::lonfeedback::generate_preview_button('compemail','message').
-	      &Apache::lonhtmlcommon::htmlareaselectactive('message'));
+    $r->print('</form>');
+    if ($hasfloat) {
+        $r->print('</div><div class="LC_clear_float_footer"></div>');
+    }
+    $r->print(&generate_preview_form);
+}
+
+sub check_group_priv {
+    my ($group) = @_;
+    my $cid = $env{'request.course.id'};
+    my $sec = $env{'request.course.sec'};
+    return if !$cid;
+    my $can_broadcast = &Apache::lonnet::allowed('sgb',$cid.'/'.$group);
+    my $viewgrps = &Apache::lonnet::allowed('vcg',$cid.($sec?'/'.$sec:''));
+    my $editgrps = &Apache::lonnet::allowed('mdg',$cid.($sec?'/'.$sec:''));
+    if ($viewgrps || $editgrps || $can_broadcast) {
+        return 1;
+    }
+    return;
 }
 
-# ---------------------------------------------------- Display all face to face
-
 sub recipient_input_row {
     my ($dom,%lt) = @_;
     my $domform = &Apache::loncommon::select_dom_form($dom,'recdomain');
@@ -1529,7 +1643,7 @@
       &Apache::loncommon::selectstudent_link('compemail','recuname',
                                              'recdomain');
     my $output = <<"ENDREC";
-<tr><td colspan="2"><span class="LC_nobreak">$lt{'us'}:&nbsp;<input type="text" size="12" name="recuname" value="$env{'form.recname'}" />&nbsp;$lt{'do'}:&nbsp;$domform&nbsp;&nbsp;$selectlink</span></td></tr>
+<tr><td colspan="3"><span class="LC_nobreak"><b>$lt{'to'}</b> $lt{'us'}:&nbsp;<input type="text" size="12" name="recuname" value="$env{'form.recname'}" />&nbsp;$lt{'do'}:&nbsp;$domform&nbsp;&nbsp;$selectlink</span></td></tr>
 ENDREC
     return $output;
 }
@@ -1539,7 +1653,7 @@
     my $radioyes = &mt('Yes');
     my $radiono = &mt('No');
     my $output = <<"ENDREP";
-<tr><td colspan="2"><span class="LC_nobreak">$lt->{'ar'}:<label><input type="radio" name="can_reply" value="Y" checked="checked" />$radioyes</label>&nbsp;&nbsp;<label><input type="radio" name="can_reply" value="N" />$radiono</label></span>&nbsp;&nbsp;&nbsp;&nbsp;<span class="LC_nobreak">$lt->{'rt'}:&nbsp;<input type="text" size="25" name="reply_to_addr" value="$env{'user.name'}:$env{'user.domain'}" /></span></td></tr>
+<tr><td colspan="3"><span class="LC_nobreak"><b>$lt->{'ar'}</b>:<label><input type="radio" name="can_reply" value="Y" checked="checked" />$radioyes</label>&nbsp;&nbsp;<label><input type="radio" name="can_reply" value="N" />$radiono</label></span>&nbsp;&nbsp;&nbsp;&nbsp;<span class="LC_nobreak">$lt->{'rt'}:&nbsp;<input type="text" size="25" name="reply_to_addr" value="$env{'user.name'}:$env{'user.domain'}" /></span></td></tr>
 ENDREP
     return $output;
 }
@@ -1550,33 +1664,55 @@
     my $bcc = &mt('Bcc:');
     my $exmpl = &mt('username:domain,username:domain,...'); 
     my $output = <<"ENDADD";
-<tr><td>$lt->{'ad'} :<br /><tt>($exmpl)
-</tt></td><td>&nbsp;<span class="LC_nobreak">$cc 
-<input type="text" size="50" name="additionalrec_cc" /></span><br />
-<span class="LC_nobreak">$bcc <input type="text" size="50" name="additionalrec_bcc" /></span></td></tr>
+<tr><td colspan="3"><fieldset id="LC_additionalrecips"><legend><b>$lt->{'ad'}</b> <tt>($exmpl)</tt>:</legend><table>
+<tr><td>&nbsp;</td><td>$lt->{'to'}</td><td><input type="text" size="50" name="additionalrec_to" /></td></tr>
+<tr><td>&nbsp;</td><td>$cc</td><td><input type="text" size="50" name="additionalrec_cc" /></td></tr> 
+<tr><td>&nbsp;</td><td>$bcc</td><td><input type="text" size="50" name="additionalrec_bcc" /></td></tr></table></fieldset>
 ENDADD
     return $output;
 }
 
 sub submit_button_row {
-    my ($folder,$dismode,$sendtext,$lt) = @_;
-    my $output = qq| 
+    my ($folder,$dismode,$sendtext,$lt,$is_crsform,$group) = @_;
+    my $pre=&mt("Show Preview and Check Spelling");
+    my $prevbutton = '<input type="button" name="preview" value="'.$pre.'" onclick="if (typeof(document.compemail.onsubmit)=='."'function'".') {document.compemail.onsubmit();};document.preview.comment.value=document.compemail.message.value;document.preview.subject.value=document.compemail.subject.value;document.preview.submit();" />';
+    my $output = qq|
 <input type="hidden" name="folder" value="$folder" />
-<input type="hidden" name="dismode" value="$dismode" />
+<input type="hidden" name="dismode" value="$dismode" />|;
+    if ($is_crsform) {
+        $output .= '<input type="hidden" name="courserecips" value="" />'."\n";
+        if ($group ne '') {
+            $output .= '<input type="hidden" name="groupmail" value="" />'."\n";
+        }
+    }
+    $output .= qq|
+<table><tr><td align="left">
 <input type="submit" name="send" value="$sendtext" />
-<input type="submit" name="cancel" value="$lt->{'ca'}" /><hr />
+<input type="submit" name="cancel" value="$lt->{'ca'}" />
+</td><td width="60">&nbsp;</td><td align="right">$prevbutton</td></tr></table>
 |;
     return $output;
 }
 
 sub msg_subject_row {
     my ($dissub,$lt,$subj_size,$extra) = @_;
-    my $output = '<tr><td colspan="2">'.$lt->{'sb'}.':&nbsp;<input type="text" size="'.
+    my $output = '<tr><td colspan="3"><b>'.$lt->{'sb'}.'</b>:&nbsp;<input type="text" size="'.
                  $subj_size.'" name="subject" value="'.$dissub.'" />'.$extra.
                  '</td></tr>';
     return $output;
 }
 
+sub generate_preview_form {
+    my $prevbutton = (<<ENDPREVIEW);
+<form name="preview" action="/adm/feedback?preview=1" method="post" target="preview">
+<input type="hidden" name="subject" />
+<input type="hidden" name="comment" />
+</form>
+ENDPREVIEW
+}
+
+# ---------------------------------------------------- Display all face to face
+
 sub retrieve_instructor_comments {
     my ($user,$domain)=@_;
     my $target=$env{'form.grade_target'};
@@ -2183,21 +2319,29 @@
                      .'</tr></table>');
         }
     }
-    my ($tolist,$cclist);
-    my (@recipients,@ccs);
-    if (ref($content{'recuser'}) eq 'ARRAY') {
+    my ($tonum,$tolist,$cclist,$bcclist,$groupcclist,%recipients);
+    if ($content{'recipid'}) {
+        $tonum = &retrieve_recips('display',\%content,\%recipients);
+        if (ref($recipients{'cc'}) eq 'ARRAY') {
+            $cclist = join(', ',@{$recipients{'cc'}});
+        }
+        if (ref($recipients{'to'}) eq 'ARRAY') {
+            $tolist = join(', ',@{$recipients{'to'}});
+        }
+        if (ref($recipients{'bcc'}) eq 'ARRAY') {
+            $bcclist = join(', ',@{$recipients{'bcc'}});
+        }
+    }
+    if (!$tolist && ref($content{'recuser'}) eq 'ARRAY') {
+        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].' '.&mt('at').' '.$content{'recdomain'}[$i].') ';
+           ' ('.$content{'recuser'}[$i].':'.$content{'recdomain'}[$i].') ';
         }
-    }
-    $tolist = join(', ',@recipients);
-    if ($content{'recipid'}) {
-        @ccs = &retrieve_cc_recips('display',%content);
-        $cclist = join(', ',@ccs);
+        $tolist = join(', ',@recipients);
     }
     my ($restitle,$baseurl,$refers_to);
     if (defined($content{'resource_title'})) {
@@ -2214,6 +2358,14 @@
 	$r->print(&Apache::loncommon::student_image_tag($content{'senderdomain'},$content{'sendername'}));
     }
 
+    my $broadcast_link;
+    if (($content{'courseid'}) && ($content{'recipid'} && 
+        (ref($recipients{'course_broadcast'}) eq 'ARRAY') || 
+        (ref($recipients{'group_cc_broadcast'}) eq 'ARRAY') ||
+        (ref($recipients{'group_bcc_broadcast'}) eq 'ARRAY'))) {
+        $broadcast_link = &recipients_link($r,\%content,\%recipients);
+    }
+
     # Display LON-CAPA Message (Start)
     # Subject
     $r->print('<br />'
@@ -2228,11 +2380,48 @@
                  .$tolist
                  .&Apache::lonhtmlcommon::row_closure()
         );
+        if ($cclist) {
+            $r->print(&Apache::lonhtmlcommon::row_title(&mt('Cc'))
+                     .$cclist
+                     .&Apache::lonhtmlcommon::row_closure()
+            );
+        }
+        if ($bcclist) {
+            $r->print(&Apache::lonhtmlcommon::row_title(&mt('Bcc'))
+                     .$bcclist
+                     .&Apache::lonhtmlcommon::row_closure()
+            );
+        }
+        if (($content{'courseid'}) && ($content{'recipid'})) {
+            my %broadcast_types = 
+                &Apache::lonlocal::texthash (
+                    course_broadcast    => 'Broadcast to', 
+                    group_cc_broadcast  => 'Cc to group',
+                    group_bcc_broadcast => 'Bcc to group',
+                );                   
+            foreach my $type (sort(keys(%broadcast_types))) {
+                if (ref($recipients{$type}) eq 'ARRAY') {
+                    my $num = @{$recipients{$type}};
+                    my $broadcastlist = join(', ',@{$recipients{$type}});
+                    if ($broadcastlist && $broadcast_link) {
+                        if ($type eq 'group_cc_broadcast') {
+                            $groupcclist = $broadcastlist;
+                        }
+                        $r->print(&Apache::lonhtmlcommon::row_title(
+                                            $broadcast_types{$type})
+                                  .&mt('[quant,_1,recipient]',$num)
+                                  .' <a href="javascript:showBroadcastList();">['
+                                  .&mt('Show').']</a>' 
+                                  .&Apache::lonhtmlcommon::row_closure());
+                    }
+                }
+            }
+        }
         if ($content{'replytoaddr'}) {
             my ($replytoname,$replytodom) = split(/:/,$content{'replytoaddr'});
             if ($replytoname ne '' && $replytodom ne '') {
                 $r->print(&Apache::lonhtmlcommon::row_title(&mt('Reply To'))
-                         .&mt('[_1] at [_2]',$replytoname,$replytodom)
+                         .$replytoname.':'.$replytodom
                          .&Apache::lonhtmlcommon::row_closure()
                 );
             }
@@ -2247,30 +2436,46 @@
         if ($content{'noreplies'}) {
             $r->print(' ('.&mt('No replies to sender').')'
                      .&Apache::lonhtmlcommon::row_closure()
-            ); 
+            );
         } else {
             if ($content{'replytoaddr'}) {
                 my ($replytoname,$replytodom) = split(/:/,$content{'replytoaddr'});
                 if ($replytoname ne '' && $replytodom ne '') {
                     $r->print(&Apache::lonhtmlcommon::row_closure()
                              .&Apache::lonhtmlcommon::row_title(&mt('Reply To'))
-                             .&mt('[_1] at [_2]',$replytoname,$replytodom)
+                             .$replytoname.':'.$replytodom
                              .&Apache::lonhtmlcommon::row_closure()
                     );
                 } else {
                     $r->print(&Apache::lonhtmlcommon::row_closure());
                 }
             } else {
-                $r->print(' ('.&mt('[_1] at [_2]',$content{'sendername'},$content{'senderdomain'}).') '
+                $r->print(' ('.$content{'sendername'}.':'.$content{'senderdomain'}.') '
                          .&Apache::lonhtmlcommon::row_closure()
                 );
             }
-            if ($cclist) {
+            if ($tonum && $tolist) {
+                $r->print(&Apache::lonhtmlcommon::row_title(&mt('To'))
+                         .$tolist
+                         .&Apache::lonhtmlcommon::row_closure()
+                    );
+            }
+            if ($cclist) { 
                 $r->print(&Apache::lonhtmlcommon::row_title(&mt('Cc'))
                          .$cclist
                          .&Apache::lonhtmlcommon::row_closure()
                     );
-
+            }
+            if ($content{'group'} ne '') {
+                if (&check_group_priv($content{'group'})) {
+                    $groupcclist = join(', ',@{$recipients{'group_cc_broadcast'}});
+                    if ($groupcclist) {
+                        $r->print(&Apache::lonhtmlcommon::row_title(&mt('Group Cc'))
+                                 .$groupcclist
+                                 .&Apache::lonhtmlcommon::row_closure()
+                        );
+                    }
+                }
             }
         }
     }
@@ -2362,36 +2567,129 @@
     return;
 }
 
-sub retrieve_cc_recips {
-    my ($context,%content) = @_; 
-    my %reciphash =
-         &Apache::lonnet::get('nohist_emailrecip',[$content{'recipid'}],
-                              $content{'senderdomain'},$content{'sendername'});
-    my $recipinfo = $reciphash{$content{'recipid'}};
-    my @ccs;
-    if (ref($recipinfo) eq 'HASH') {
-        if (ref($recipinfo->{'cc'}) eq 'HASH') {
-            foreach my $cc (sort(keys(%{$recipinfo->{'cc'}}))) {
-                my ($ccname,$ccdom) = split(/:/,$cc);
-                if (!(($ccname eq $env{'user.name'}) &&
-                     ($ccdom eq $env{'user.domain'}))) {
-                    my $showcc ='<span class="LC_nobreak">';
-                    if ($context eq 'replying') { 
-                        $showcc = '<label><input type="checkbox" name="replying_cc" value="'.$cc.'" />';
-                    }
-                    $showcc .= &Apache::loncommon::aboutmewrapper(
-                                       &Apache::loncommon::plainname($ccname,
-                                         $ccdom),$ccname,$ccdom);
-		    if ($context eq 'replying') {
-			$showcc .='</label>';
-		    }
-		    $showcc .= '</span>';
-		    push(@ccs,$showcc);
+sub retrieve_recips {
+    my ($context,$content,$recips)= @_;
+    my $tonum = 0;
+    if (ref($content) eq 'HASH') {
+        my %reciphash =
+            &Apache::lonnet::get('nohist_emailrecip',[$content->{'recipid'}],
+                                 $content->{'senderdomain'},$content->{'sendername'});
+        my $recipinfo = $reciphash{$content->{'recipid'}};
+        if (ref($recipinfo) eq 'HASH') {
+            foreach my $type ('to','cc','course_broadcast','group_cc_broadcast','group_bcc_broadcast') {
+                if (ref($recipinfo->{$type}) eq 'HASH') {
+                    if ($type eq 'to') {
+                        $tonum = keys(%{$recipinfo->{$type}});
+                    }
+                    foreach my $user (sort(keys(%{$recipinfo->{$type}}))) {
+                        my ($uname,$udom) = split(/:/,$user);
+                        next if (($context eq 'replying') && ($uname eq $env{'user.name'})
+                                 && ($udom eq $env{'user.domain'}));
+                        my $showuser ='<span class="LC_nobreak">';
+                        if ($context eq 'replying') {
+                            if (($type eq 'to') || ($type eq 'cc')) { 
+                                $showuser = '<label><input type="checkbox" name="replying_'.$type.'" value="'.$user.'" />';
+                            } elsif ($type eq 'group_cc_broadcast') {
+                                $showuser = '<label><input type="checkbox" name="replying_groupcc" value="'.$user.'" />';
+                            }
+                        }
+                        $showuser .= &Apache::loncommon::aboutmewrapper(
+                                           &Apache::loncommon::plainname($uname,
+                                           $udom),$uname,$udom);
+		        if ($context eq 'replying') {
+		            $showuser .='</label>';
+		        }
+		        $showuser .= '</span>';
+                        if (ref($recips) eq 'HASH') {
+		            push(@{$recips->{$type}},$showuser);
+                        }
+                    }
                 }
             }
         }
     }
-    return @ccs;
+    return $tonum;
+}
+
+sub recipients_link {
+    my ($r,$content,$recipients) = @_;
+    my ($broadcast_link,$show);
+    if ((ref($content) eq 'HASH') && (ref($recipients) eq 'HASH')) {
+        if (ref($recipients->{'course_broadcast'}) eq 'ARRAY') {
+            if (@{$recipients->{'course_broadcast'}} > 0) {
+                $show = 'course';
+            }
+        } elsif (ref($recipients->{'group_cc_broadcast'}) eq 'ARRAY') {
+            if (@{$recipients->{'group_cc_broadcast'}} > 0) {
+                $show = 'group_cc';
+            }
+        } elsif (ref($recipients->{'group_bcc_broadcast'}) eq 'ARRAY') {
+            if (@{$recipients->{'group_bcc_broadcast'}} > 0) {
+                $show = 'group_bcc';
+            }
+        }
+        if ($show) {
+            my ($nothing,$height,$width,$start_page,$end_page,$body);
+            $nothing=&Apache::lonhtmlcommon::javascript_nothing();
+            $height = 400;
+            $width = 600;
+            my $start_page =
+                    &Apache::loncommon::start_page('Broadcast List', undef,
+                                       {only_body => 1,
+                                        js_ready  => 1,});
+            my $end_page = &Apache::loncommon::end_page({js_ready => 1,});
+            my $body = '<h3>'.&mt("Recipients of broadcast message").'</h3>'.
+                       &Apache::loncommon::start_data_table();
+            my $cell = 0;
+            $body .= &Apache::loncommon::start_data_table_row();
+            foreach my $item (@{$recipients->{$show.'_broadcast'}}) {
+                $item =~ s/'/\\'/g;
+                if (!($cell%2) && $cell > 0) {
+                    $body .= &Apache::loncommon::end_data_table_row().
+                             &Apache::loncommon::start_data_table_row();
+                }
+                $cell ++;
+                $body .= '<td>'.$cell.'&nbsp;'.$item.'&nbsp;&nbsp;</td>';
+            }
+            if ($cell%2) {
+                $body .= '<td>&nbsp;</td>';
+            }
+            $body .= &Apache::loncommon::end_data_table_row().
+                     &Apache::loncommon::end_data_table();
+            $body =~ s{</}{<\\/}g;
+            $body =~ s{\n}{}g;
+            $r->print(<<ENDJS);
+<script type="text/javascript">
+function showBroadcastList() {
+    var caller = this;
+    var newWindow = null;
+    try {
+        newWindow =  window.open($nothing,"broadcast","HEIGHT=$height,WIDTH=$width,resizable=yes,scrollbars=yes" );
+    }
+    catch(error) {
+        writeWin(caller);
+        return;
+    }
+    if (newWindow) {
+        caller = newWindow;
+    }
+    writeWin(caller);
+    return;
+}
+
+function writeWin(caller) {
+    caller.document.writeln('$start_page $body $end_page');
+    caller.document.close();
+    caller.focus();
+}
+
+</script>
+
+ENDJS
+            $broadcast_link = 1;
+        }
+    }
+    return $broadcast_link;
 }
 
 # =========================================================== Show the citation
@@ -2542,28 +2840,24 @@
 	    &statuschange($msgid,'replied',$folder);
 	}
 
-	my @to =
-	    &Apache::loncommon::get_env_multiple('form.selectedusers_forminput');
 	my $mode = $env{'form.sendmode'};
+	my (%toaddr,$tos,$cc,$bcc,$broadcast);
 
-	my (%toaddr,$cc,$bcc);
-	if (@to) {
-	    foreach my $dest (@to) {
-		my ($user,$domain) = split(/:/, $dest);
-		if (($user ne '') && ($domain ne '')) {
-		    my $address = $user.":".$domain; # How the code below expects it.
-		    $toaddr{$address} = '';
-		}
-	    }
-	}
-
-	if ($env{'form.sendmode'} eq 'group') {
-	     foreach my $address (keys(%env)) {
-	 	if ($address=~/^form\.send\_to\_\&\&\&[^\&]*\&\&\&\_(.+)$/) {
-	 	    $toaddr{$1}='';
-	 	}
-	    }
-	} elsif ($env{'form.sendmode'} eq 'upload') {
+	if ($mode eq 'group') {
+            if (defined($env{'form.courserecips'})) {
+                my $courseusers = $env{'form.courserecips'};
+                $courseusers =~ s/^_\&\&\&_//;
+                my @to = split('_&&&_',$courseusers);
+                foreach my $dest (@to) {
+                    my ($user,$domain) = split(/:/, $dest);
+                    if (($user ne '') && ($domain ne '')) {
+                        my $rec = $user.":".$domain;
+                        $toaddr{$rec} = '';
+                        $broadcast->{$rec} = '';
+                    }
+                }
+            }
+	} elsif ($mode eq 'upload') {
             $nosentstore = 0;
 	    foreach my $line (split(/[\n\r\f]+/,$env{'form.upfile'})) {
                 my ($rec,$txt) = ($line =~ /^([^:]+:[^:]+):(.*)$/);
@@ -2571,40 +2865,81 @@
                     $rec =~ s/^\s+//;
                     $rec =~ s/\s+$//;
 		    $toaddr{$rec}.=$txt."\n";
+                    $broadcast->{$rec} = '';
 		}
 	    }
 	} else {
 	    if (($env{'form.recuname'} ne '') && ($env{'form.recdomain'} ne '')) {
 		$toaddr{$env{'form.recuname'}.':'.$env{'form.recdomain'}}='';
-                $cc->{$env{'form.recuname'}.':'.$env{'form.recdomain'}}='';
+                $tos->{$env{'form.recuname'}.':'.$env{'form.recdomain'}}='';
 	    }
 	}
+        if ($env{'form.additionalrec_to'}) {
+            foreach my $rec (split(/\s*,\s*/,$env{'form.additionalrec_to'})) {
+                my ($auname,$audom)=split(/:/,$rec);
+                if (($auname ne "") && ($audom ne "")) {
+                    $toaddr{$auname.':'.$audom}='';
+                    $tos->{$auname.':'.$audom}='';
+                }
+            }
+        }
+        if ($env{'form.replying_to'}) {
+            my @toreplies =
+                &Apache::loncommon::get_env_multiple('form.replying_to');
+            foreach my $rec (@toreplies) {
+                my ($auname,$audom)=split(/:/,$rec);
+                if (($auname ne "") && ($audom ne "")) {
+                    $toaddr{$auname.':'.$audom}='';
+                    $tos->{$auname.':'.$audom}='';
+                }
+            }
+        }
 	if ($env{'form.additionalrec_cc'}) {
 	    foreach my $rec (split(/\s*,\s*/,$env{'form.additionalrec_cc'})) {
 		my ($auname,$audom)=split(/:/,$rec);
 		if (($auname ne "") && ($audom ne "")) {
                     $toaddr{$auname.':'.$audom}='';
-		    $cc->{$auname.':'.$audom}='';
+                    if (!defined($tos->{$auname.':'.$audom})) {
+		        $cc->{$auname.':'.$audom}='';
+                    }
 		}
 	    }
 	}
-        if ($env{'form.additionalrec_bcc'}) {
-            foreach my $rec (split(/\s*,\s*/,$env{'form.additionalrec_bcc'})) {
+        if ($env{'form.replying_cc'}) {
+            my @ccreplies =
+                &Apache::loncommon::get_env_multiple('form.replying_cc');
+            foreach my $rec (@ccreplies) {
+                my ($auname,$audom)=split(/:/,$rec);
+                if (($auname ne "") && ($audom ne "")) {
+                    $toaddr{$auname.':'.$audom}='';
+                    if (!defined($tos->{$auname.':'.$audom})) {
+                        $cc->{$auname.':'.$audom}='';
+                    }
+                }
+            }
+        }
+        if ($env{'form.replying_groupcc'}) {
+            my @groupreplies =
+                &Apache::loncommon::get_env_multiple('form.replying_groupcc');
+            foreach my $rec (@groupreplies) {
                 my ($auname,$audom)=split(/:/,$rec);
                 if (($auname ne "") && ($audom ne "")) {
                     $toaddr{$auname.':'.$audom}='';
-                    $bcc->{$auname.':'.$audom}='';
+                    if (!defined($tos->{$auname.':'.$audom})) {
+                        $broadcast->{$auname.':'.$audom}='';
+                    }
                 }
             }
         }
-        if ($env{'form.replying_cc'}) {
-            my @ccreplies = 
-                &Apache::loncommon::get_env_multiple('form.replying_cc');
-            foreach my $rec (@ccreplies) {
+        if ($env{'form.additionalrec_bcc'}) {
+            foreach my $rec (split(/\s*,\s*/,$env{'form.additionalrec_bcc'})) {
                 my ($auname,$audom)=split(/:/,$rec);
                 if (($auname ne "") && ($audom ne "")) {
                     $toaddr{$auname.':'.$audom}='';
-                    $cc->{$auname.':'.$audom}='';
+                    if ((!defined($tos->{$auname.':'.$audom})) && 
+                        (!defined($cc->{$auname.':'.$audom}))) {
+                        $bcc->{$auname.':'.$audom}='';
+                    }
                 }
             }
         }
@@ -2624,14 +2959,26 @@
             $savemsg=&Apache::lonfeedback::clear_out_html($env{'form.message'});
         }
         my %reciphash = (
+                           to => $tos,
                            cc => $cc,
                            bcc => $bcc,
-                        ); 
+                        );
+        if ($mode eq 'group') {
+            if ($group eq '') {
+                $reciphash{'course_broadcast'} = $broadcast;
+            } else {
+                if ($env{'form.groupmail'} eq 'cc') {
+                    $reciphash{'group_cc_broadcast'} = $broadcast;
+                } else {
+                    $reciphash{'group_bcc_broadcast'} = $broadcast;
+                }
+            }
+        }
         my ($recipid,$recipstatus) = 
             &Apache::lonmsg::store_recipients($msgsubj,$env{'user.name'},
                                        $env{'user.domain'},\%reciphash);
         if ($recipstatus ne 'ok') {
-            &Apache::lonnet::logthis('Failed to store Bcc and Cc recipients for '.$env{'user.name'}.':'.$env{'user.domain'});
+            &Apache::lonnet::logthis('Failed to store To, Bcc and Cc recipients for '.$env{'user.name'}.':'.$env{'user.domain'});
         }
         if ($env{'form.attachment'}) {
             if (length($env{'form.attachment'})<131072) {
@@ -2686,19 +3033,31 @@
         my $subj_prefix;
         if ($numsent > 0) {
             if (($env{'request.course.id'}) && 
-                (($env{'form.sendmode'} eq 'group') ||
+                (($mode eq 'group') ||
                  ($env{'form.courserecord'}) ||
-                 ($msgtype eq 'critical'))) {
+                 ($msgtype eq 'critical')) ||
+                ($env{'form.replyid'} && 
+                 (($content{'courseid'} ne '') && 
+                  ($mode eq 'group')))) {
                 if ($msgtype eq 'critical') {
                     $subj_prefix = 'Critical.';
-                } elsif ($env{'form.sendmode'} eq 'group') {
+                } elsif ($mode eq 'group') {
                     $subj_prefix = 'Broadcast.';
                 } else {
                     $subj_prefix = 'Archive';
                 }
                 my ($specialmsgid,$specialresult);
-                my $course_str = &escape('['.$cnum.':'.$cdom.']');
-
+                my $course_str;
+                if ($env{'form.replyid'}) {
+                    if ($content{'courseid'} ne '') {
+                        my %crsdesc = 
+                            &Apache::lonnet::coursedescription($content{'courseid'},
+                                                               {'one_time' => 1});
+                        $course_str = &escape('['.$crsdesc{'num'}.':'.$crsdesc{'domain'}.']');
+                    }
+                } elsif ($env{'request.course.id'}) {
+                    $course_str = &escape('['.$cnum.':'.$cdom.']');
+                }
                 $specialresult = 
                     &Apache::lonmsg::user_normal_msg_raw($cnum,$cdom,
                         $subj_prefix.' '.$course_str,$savemsg,undef,undef,
@@ -2723,11 +3082,11 @@
                                 undef,$specialmsgid,undef,undef,undef,1);
                         }
                     }
-                    if (($env{'form.sendmode'} ne 'upload') && (@recusers > 0)) {
+                    if (($mode ne 'upload') && (@recusers > 0)) {
                         &Apache::lonmsg::process_sent_mail($msgsubj,
                            $subj_prefix,$numsent,$stamp,$msgname,$msgdom,
                            $msgcount,$context,$pid,$savemsg,\@recusers,
-                           \@recudoms,undef,$attachmenturl);
+                           \@recudoms,undef,$attachmenturl,'','','','',$recipid);
                     }
                 } else {
                     &Apache::lonnet::logthis('Failed to create record of critical, broadcast or archived message in '.$env{'course.'.$env{'request.course.id'}.'.num'}.' '&mt('at').' '.$env{'course.'.$env{'request.course.id'}.'.domain'}.' - no msgid generated');
@@ -2739,7 +3098,8 @@
                 &Apache::lonmsg::process_sent_mail($msgsubj,$subj_prefix,
                        $numsent,$stamp,$env{'user.name'},
                        $env{'user.domain'},$msgcount,$context,
-                       $$,$savemsg,\@recusers,\@recudoms,undef,$attachmenturl);
+                       $$,$savemsg,\@recusers,\@recudoms,undef,$attachmenturl,
+                       '','','','',$recipid);
             }
         }
         if (!$env{'form.multiforward'}) { 

--raeburn1225779773--