--- loncom/interface/lonmsgdisplay.pm 2006/04/22 17:10:07 1.4 +++ loncom/interface/lonmsgdisplay.pm 2006/04/25 20:02:30 1.14 @@ -1,7 +1,7 @@ # The LearningOnline Network with CAPA # Routines for messaging display # -# $Id: lonmsgdisplay.pm,v 1.4 2006/04/22 17:10:07 albertel Exp $ +# $Id: lonmsgdisplay.pm,v 1.14 2006/04/25 20:02:30 albertel Exp $ # # Copyright Michigan State University Board of Trustees # @@ -184,8 +184,9 @@ sub statuschange { &Apache::lonnet::put('email_status'.$suffix,{$msgid => $newstatus}); } if ($newstatus eq 'deleted') { - &movemsg(&Apache::lonnet::unescape($msgid),$folder,'trash'); - } + return &movemsg($msgid,$folder,'trash'); + } + return ; } # ============================================================= Make new folder @@ -206,19 +207,53 @@ sub movemsg { if ($srcfolder eq 'new') { $srcfolder=''; } my $srcsuffix=&Apache::lonmsg::foldersuffix($srcfolder); my $trgsuffix=&Apache::lonmsg::foldersuffix($trgfolder); + if ($srcsuffix eq $trgsuffix) { + return (0,&mt('Message not moved, Attempted to move message to the same folder as it already is in.')); + } # Copy message my %message=&Apache::lonnet::get('nohist_email'.$srcsuffix,[$msgid]); - &Apache::lonnet::put('nohist_email'.$trgsuffix,{$msgid => $message{$msgid}}); + if (!exists($message{$msgid}) || $message{$msgid} eq '') { + if (&Apache::slotrequest::network_error(%message)) { + return (0,&mt('Message not moved, A network error occurred.')); + } else { + return (0,&mt('Message not moved as the message is no longer in the source folder.')); + } + } + + my $result =&Apache::lonnet::put('nohist_email'.$trgsuffix, + {$msgid => $message{$msgid}}); + if (&Apache::slotrequest::network_error($result)) { + return (0,&mt('Message not moved, A network error occurred.')); + } # Copy status unless ($trgfolder eq 'trash') { - my %status=&Apache::lonnet::get('email_status'.$srcsuffix,[$msgid]); - &Apache::lonnet::put('email_status'.$trgsuffix,{$msgid => $status{$msgid}}); + my %status=&Apache::lonnet::get('email_status'.$srcsuffix,[$msgid]); + # a non-existant status is the mark of an unread msg + if (&Apache::slotrequest::network_error(%status)) { + return (0,&mt('Message copied to new folder but status was not, A network error occurred.')); + } + my $result=&Apache::lonnet::put('email_status'.$trgsuffix, + {$msgid => $status{$msgid}}); + if (&Apache::slotrequest::network_error($result)) { + return (0,&mt('Message copied to new folder but status was not, A network error occurred.')); + } } + # Delete orginals - &Apache::lonnet::del('nohist_email'.$srcsuffix,[$msgid]); - &Apache::lonnet::del('email_status'.$srcsuffix,[$msgid]); + my $result_del_msg = + &Apache::lonnet::del('nohist_email'.$srcsuffix,[$msgid]); + my $result_del_stat = + &Apache::lonnet::del('email_status'.$srcsuffix,[$msgid]); + if (&Apache::slotrequest::network_error($result_del_msg)) { + return (0,&mt('Message copied, but unable to delete the original from the source folder.')); + } + if (&Apache::slotrequest::network_error($result_del_stat)) { + return (0,&mt('Message copied, but unable to delete the original status from the source folder.')); + } + + return (1); } # ======================================================= Display a course list @@ -303,7 +338,7 @@ ENDDISHEADER if (! defined($fullname) || $fullname eq '') { $fullname = $sname; } $r->print(''.$sname.'@'.$sdom.''.$section. + $fullname.''.$sname.':'.$sdom.''.$section. ''); } $r->print(''); @@ -313,7 +348,7 @@ ENDDISHEADER sub discrit { my $r=shift; - my $header = '

'.&mt('Critical Messages').'

'. + my $header = '

'.&mt('Critical Messages').'

'. '
'. ''; my %what=&Apache::lonnet::dump('critical'); @@ -324,7 +359,7 @@ sub discrit { $result.='
'.&mt('From').': '. &Apache::loncommon::aboutmewrapper( &Apache::loncommon::plainname($content{'sendername'},$content{'senderdomain'}),$content{'sendername'},$content{'senderdomain'}).' ('. -$content{'sendername'}.'@'. +$content{'sendername'}.':'. $content{'senderdomain'}.') '.$content{'time'}. '
'.&mt('Subject').': '.$content{'subject'}. '
'.
@@ -357,18 +392,30 @@ sub sortedmessages {
     my %descriptions;
     my %status_cache = 
 	&Apache::lonnet::get('email_status'.&Apache::lonmsg::foldersuffix($folder),\@messages);
-    foreach (@messages) {
-	my $msgid=&Apache::lonnet::escape($_);
+
+    my $get_received;
+    if ($folder eq 'sent' 
+	&& ($env{'form.sortedby'} =~ m/^(rev)?(user|domain)$/)) {
+	$get_received = 1;
+    }
+
+    foreach my $msgid (@messages) {
+	my $esc_msgid=&Apache::lonnet::escape($msgid);
 	my ($sendtime,$shortsubj,$fromname,$fromdomain,$status,$fromcid)=
-	    &Apache::lonmsg::unpackmsgid($msgid,$folder,undef,
+	    &Apache::lonmsg::unpackmsgid($esc_msgid,$folder,undef,
 					 \%status_cache);
         my $description = &get_course_desc($fromcid,\%descriptions);
 	my @temp1 = ($sendtime,$shortsubj,$fromname,$fromdomain,$status,
-		     $msgid,$description);
+		     $esc_msgid,$description);
+	if ($get_received) {
+	    my %message = &Apache::lonnet::get('nohist_email'.$suffix,
+					       [$msgid]);
+	    my %content = &Apache::lonmsg::unpackagemsg($message{$msgid});
+	    push(@temp1,$content{'recuser'},$content{'recdomain'});
+	}
         # Check whether message was sent during blocking period.
         if ($sendtime >= $startblock && ($sendtime <= $endblock && $endblock > 0) ) {
-            my $escid = &Apache::lonnet::unescape($msgid);
-            $$blocked{$escid} = 'ON';
+            $$blocked{$msgid} = 'ON';
             $$numblocked ++;
         } else { 
             push @temp ,\@temp1;
@@ -383,16 +430,32 @@ sub sortedmessages {
     	@temp = sort  {$b->[0] <=> $a->[0]} @temp; 
     }
     if ($env{'form.sortedby'} eq "user"){
-	@temp = sort  {lc($a->[2]) cmp lc($b->[2])} @temp;
+	if ($get_received) {
+	    @temp = sort  {lc($a->[7][0]) cmp lc($b->[7][0])} @temp;
+	} else {
+	    @temp = sort  {lc($a->[2])    cmp lc($b->[2])}    @temp;
+	}
     }
     if ($env{'form.sortedby'} eq "revuser"){
-	@temp = sort  {lc($b->[2]) cmp lc($a->[2])} @temp;
+	if ($get_received) {
+	    @temp = sort  {lc($b->[7][0]) cmp lc($a->[7][0])} @temp;
+	} else {
+	    @temp = sort  {lc($b->[2])    cmp lc($a->[2])}    @temp;
+	}
     }
     if ($env{'form.sortedby'} eq "domain"){
-        @temp = sort  {$a->[3] cmp $b->[3]} @temp;
+	if ($get_received) {
+	    @temp = sort  {$a->[8][0] cmp $b->[8][0]} @temp;
+	} else {
+	    @temp = sort  {$a->[3]    cmp $b->[3]}    @temp;
+	}
     }
     if ($env{'form.sortedby'} eq "revdomain"){
-        @temp = sort  {$b->[3] cmp $a->[3]} @temp;
+	if ($get_received) {
+	    @temp = sort  {$b->[8][0] cmp $a->[8][0]} @temp;
+	} else {
+	    @temp = sort  {$b->[3]    cmp $a->[3]}    @temp;
+	}
     }
     if ($env{'form.sortedby'} eq "subject"){
         @temp = sort  {lc($a->[1]) cmp lc($b->[1])} @temp;
@@ -478,7 +541,7 @@ sub disnew {
                     push @newmsgs, { 
                         msgid    => $msgid,
                         sendtime => $sendtime,
-                        shortsub => &Apache::lonnet::unescape($shortsubj),
+                        shortsub => $shortsubj,
                         from     => $fromname,
                         fromdom  => $fromdom,
                         course   => $description 
@@ -624,8 +687,12 @@ ENDDISHEADER
      	$r->print(''.&mt('Status').'');
     }
     $r->print("\n");
+
+    my $suffix = &Apache::lonmsg::foldersuffix($folder);
     for (my $n=$firstdis;$n<=$lastdis;$n++) {
-	my ($sendtime,$shortsubj,$fromname,$fromdomain,$status,$origID,$description)= @{$temp[$n]};
+	my ($sendtime,$shortsubj,$fromname,$fromdomain,$status,$origID,
+	    $description,$recv_name,$recv_domain)= 
+		@{$temp[$n]};
 	if (($status ne 'deleted') && defined($sendtime) && $sendtime!~/error/) {
 	    if ($status eq 'new') {
 		$r->print('');
@@ -636,17 +703,33 @@ ENDDISHEADER
 	    } else {
 		$r->print('');
 	    }
+	    my ($dis_name,$dis_domain) = ($fromname,$fromdomain);
+	    if ($folder eq 'sent') {
+		if (defined($recv_name) && !defined($recv_domain)) {
+		    $dis_name   = join('
',@{$recv_name}); + $dis_domain = join('
',@{$recv_domain}); + } else { + my $msg_id = &Apache::lonnet::unescape($origID); + my %message = &Apache::lonnet::get('nohist_email'.$suffix, + [$msg_id]); + my %content = &Apache::lonmsg::unpackagemsg($message{$msg_id}); + $dis_name = join('
',@{$content{'recuser'}}); + $dis_domain = join('
',@{$content{'recdomain'}}); + } + } $r->print(''.&mt('Open').''. ($folder ne 'trash'?''.&mt('Delete'):' ').''. ''.&Apache::lonlocal::locallocaltime($sendtime).''. - $fromname.''.$fromdomain.''. - &Apache::lonnet::unescape($shortsubj).''. + $dis_name.''.$dis_domain.''. + $shortsubj.''. $description.''.$status.''."\n"); } elsif ($status eq 'deleted') { # purge - &movemsg(&Apache::lonnet::unescape($origID),$folder,'trash'); + my ($result,$msg) = + &movemsg(&Apache::lonnet::unescape($origID),$folder,'trash'); + } } $r->print("\n

". @@ -784,7 +867,7 @@ sub compout { $r->print(''.&mt('Replying to').' '. &Apache::loncommon::aboutmewrapper( &Apache::loncommon::plainname($content{'sendername'},$content{'senderdomain'}),$content{'sendername'},$content{'senderdomain'}).' ('. - $content{'sendername'}.'@'. + $content{'sendername'}.':'. $content{'senderdomain'}.')'. ''. ''. @@ -877,7 +960,7 @@ sub retrieve_instructor_comments { next if ($content{'subject'} !~ /^Record/); # &Apache::lonfeedback::newline_to_br(\$content{'message'}); $result.='Recorded by '. - $content{'sendername'}.'@'.$content{'senderdomain'}."\n"; + $content{'sendername'}.':'.$content{'senderdomain'}."\n"; $result.= &Apache::lontexconvert::msgtexconverted($content{'message'})."\n"; } @@ -935,7 +1018,7 @@ sub disfacetoface { $result.=&mt('By').': '. &Apache::loncommon::aboutmewrapper( &Apache::loncommon::plainname($content{'sendername'},$content{'senderdomain'}),$content{'sendername'},$content{'senderdomain'}).' ('. -$content{'sendername'}.'@'. +$content{'sendername'}.':'. $content{'senderdomain'}.') '.$content{'time'}. '

'.
               &Apache::lontexconvert::msgtexconverted($content{'message'}).
@@ -1149,7 +1232,7 @@ sub blockstore {
         unless ( defined($cancels{$_}) ) {
             my ($newstart,$newend) = &get_dates_from_form($_);
             my $newkey = $newstart.'____'.$newend;
-            $blocking{$newkey} = $env{'user.name'}.'@'.$env{'user.domain'}.':'.$env{'form.title_'.$_};
+            $blocking{$newkey} = $env{'user.name'}.':'.$env{'user.domain'}.':'.$env{'form.title_'.$_};
         }
     }
     if ($addtotal + $modtotal > 0) {
@@ -1236,8 +1319,14 @@ END
         my ($start,$end) = split/____/,$_;
         my $startform = &Apache::lonhtmlcommon::date_setter('blockform','startdate_'.$parmcount,$start,$onchange);
         my $endform = &Apache::lonhtmlcommon::date_setter('blockform','enddate_'.$parmcount,$end,$onchange);
-        my ($setter,$title) = split/:/,$$records{$_};
-        my ($setuname,$setudom) = split/@/,$setter;
+	my (@data,$setuname,$setudom,$title);
+	@data = split(/:/,$$records{$_},3);
+	if (scalar(@data) eq 2) {
+	    $title = $data[1];
+	    ($setuname,$setudom) = split(/@/,$data[0]);
+	} else {
+	    ($setuname,$setudom,$title) = @data;
+	}
         my $settername = &Apache::loncommon::plainname($setuname,$setudom);
         $r->print(<<"END");
        
@@ -1404,7 +1493,7 @@ END
             $closeblock= &Apache::lonlocal::locallocaltime($closeblock);
             $r->print(''.$courseinfo{'description'}.''.
                       ''.$openblock.' to '.$closeblock.''.
-                      ''.$fullname.' ('.$uname.'@'.$udom.
+                      ''.$fullname.' ('.$uname.':'.$udom.
                       ')');
         }
     }
@@ -1636,17 +1725,17 @@ sub sendoffmail {
 	    %content=&Apache::lonmsg::unpackagemsg($message{$msgid},1);
 	    &statuschange($msgid,'replied',$folder);
 	}
-	my %toaddr=();
-	undef %toaddr;
+
+	my %toaddr;
 	if ($env{'form.sendmode'} eq 'group') {
-	    foreach (keys %env) {
-		if ($_=~/^form\.send\_to\_\&\&\&[^\&]*\&\&\&\_(.+)$/) {
+	    foreach my $address (keys(%env)) {
+		if ($address=~/^form\.send\_to\_\&\&\&[^\&]*\&\&\&\_(.+)$/) {
 		    $toaddr{$1}='';
 		}
 	    }
 	} elsif ($env{'form.sendmode'} eq 'upload') {
-	    foreach (split(/[\n\r\f]+/,$env{'form.upfile'})) {
-		my ($rec,$txt)=split(/\s*\:\s*/,$_);
+	    foreach my $line (split(/[\n\r\f]+/,$env{'form.upfile'})) {
+		my ($rec,$txt)=split(/\s*\:\s*/,$line);
 		if ($txt) {
 		    $rec=~s/\@/\:/;
 		    $toaddr{$rec}.=$txt."\n";
@@ -1665,7 +1754,8 @@ sub sendoffmail {
         my $savemsg;
         my $msgtype;
         my %sentmessage;
-        my $msgsubj=&Apache::lonfeedback::clear_out_html($env{'form.subject'});
+        my $msgsubj=&Apache::lonfeedback::clear_out_html($env{'form.subject'},
+							 undef,1);
         if ((($env{'form.critmsg'}) || ($env{'form.sendbck'})) &&
             (&Apache::lonnet::allowed('srm',$env{'request.course.id'})
 	     || &Apache::lonnet::allowed('srm',$env{'request.course.id'}.
@@ -1677,33 +1767,43 @@ sub sendoffmail {
             $savemsg=&Apache::lonfeedback::clear_out_html($env{'form.message'});
         }
 	
-	foreach (keys %toaddr) {
-	    my ($recuname,$recdomain)=split(/\:/,$_);
+	foreach my $address (sort(keys(%toaddr))) {
+	    my ($recuname,$recdomain)=split(/\:/,$address);
             my $msgtxt = $savemsg;
-	    if ($toaddr{$_}) { $msgtxt.='
'.$toaddr{$_}; } - my $thismsg; + if ($toaddr{$address}) { $msgtxt.='
'.$toaddr{$address}; } + my @thismsg; if ((($env{'form.critmsg'}) || ($env{'form.sendbck'})) && (&Apache::lonnet::allowed('srm',$env{'request.course.id'}) || &Apache::lonnet::allowed('srm',$env{'request.course.id'}. '/'.$env{'request.course.sec'}))) { - $r->print(&mt('Sending critical message').' '.$recuname.'@'.$recdomain.': '); - $thismsg=&Apache::lonmsg::user_crit_msg($recuname,$recdomain,$msgsubj,$msgtxt, - $env{'form.sendbck'},$env{'form.permanent'}, - \$sentmessage{$_}); + $r->print(&mt('Sending critical message').' '.$recuname.':'.$recdomain.': '); + @thismsg= + &Apache::lonmsg::user_crit_msg($recuname,$recdomain, + $msgsubj,$msgtxt, + $env{'form.sendbck'}, + $env{'form.permanent'}, + \$sentmessage{$address}); } else { - $r->print(&mt('Sending').' '.$recuname.'@'.$recdomain.': '); - $thismsg=&Apache::lonmsg::user_normal_msg($recuname,$recdomain,$msgsubj,$msgtxt, - $content{'citation'},undef,undef,$env{'form.permanent'},\$sentmessage{$_}); + $r->print(&mt('Sending').' '.$recuname.':'.$recdomain.': '); + @thismsg= + &Apache::lonmsg::user_normal_msg($recuname,$recdomain, + $msgsubj,$msgtxt, + $content{'citation'}, + undef,undef, + $env{'form.permanent'}, + \$sentmessage{$address}); } if (($env{'request.course.id'}) && (($msgtype eq 'critical') || ($env{'form.sendmode'} eq 'group'))) { - $specialmsg_status{$recuname.':'.$recdomain} = $thismsg; - if ($thismsg eq 'ok') { - $numspecial ++; - } + $specialmsg_status{$recuname.':'.$recdomain} = + join(' ',@thismsg); + foreach my $result (@thismsg) { + if ($result eq 'ok') { + $numspecial++; + } + } } - $r->print($thismsg.'
'); - $sendstatus.=' '.$thismsg; + $sendstatus.=' '.join(' ',@thismsg); } if (($env{'request.course.id'}) && (($env{'form.sendmode'} eq 'group') || ($msgtype eq 'critical'))) { @@ -1726,10 +1826,11 @@ sub sendoffmail { } if ($specialresult eq 'ok') { my $record_sent; - my @recusers = (); - my @recudoms = (); - my ($stamp,$crssubj,$msgname,$msgdom,$msgcount,$context,$pid) = - split(/\:/,&Apache::lonnet::unescape($specialmsgid)); + my @recusers; + my @recudoms; + my ($stamp,$crssubj,$msgname,$msgdom,$msgcount,$context,$pid) = + split(/\:/,&Apache::lonnet::unescape($specialmsgid)); + foreach my $recipient (sort(keys(%toaddr))) { if ($specialmsg_status{$recipient} eq 'ok') { my $usersubj = $subj_prefix.'['.$recipient.']'; @@ -1741,15 +1842,15 @@ sub sendoffmail { &Apache::lonmsg::user_normal_msg_raw($cnum,$cdom,$subj_prefix. ' ['.$recipient.']',$msgsubj,undef, undef,undef,undef,$usermsgid,undef,undef,$specialmsgid); - my ($uname,$udom) = split/:/,$recipient; + my ($uname,$udom) = split(/:/,$recipient); push(@recusers,$uname); push(@recudoms,$udom); } } if (@recusers) { my $specialmessage; - my $sentsubj = $subj_prefix.' ('.$numspecial.' sent) '. - $msgsubj; + my $sentsubj = + $subj_prefix.' ('.$numspecial.' sent) '.$msgsubj; $sentsubj = &HTML::Entities::encode($sentsubj,'<>&"'); my $sentmsgid = &Apache::lonmsg::buildmsgid($stamp,$sentsubj,$msgname, @@ -1767,17 +1868,15 @@ sub sendoffmail { &printheader($r,'','No messages sent.'); } if ($sendstatus=~/^(\s*(?:ok|con_delayed)\s*)*$/) { - $r->print('
'.&mt('Completed.').''); + $r->print('
'.&mt('Completed.').''); if ($env{'form.displayedcrit'}) { &discrit($r); } else { &Apache::loncommunicate::menu($r); } } else { - $r->print( - '

'.&mt('Could not deliver message').'

'. - &mt('Please use the browser "Back" button and correct the recipient addresses') - ); + $r->print('

'.&mt('Could not deliver message').' '. + &mt('Please use the browser "Back" button and correct the recipient addresses').'

'); } } @@ -1884,32 +1983,67 @@ sub handler { &compout($r,$env{'form.forward'},undef,undef,undef,$folder); } elsif ($env{'form.markdel'}) { &printheader($r,'','Deleted Message'); - &statuschange($env{'form.markdel'},'deleted',$folder); + my ($result,$msg) = + &statuschange($env{'form.markdel'},'deleted',$folder); + if (!$result) { + $r->print('

'. + &mt('Failed to delete the message.').'

'. + '

'.$msg."

\n"); + } &Apache::loncommunicate::menu($r); &disall($r,($folder?$folder:$dismode)); } elsif ($env{'form.markedmove'}) { - my $total=0; - foreach (keys %env) { - if ($_=~/^form\.delmark_(.*)$/) { - &movemsg(&Apache::lonnet::unescape($1),$folder, - $env{'form.movetofolder'}); - $total++; + my ($total,$failed,@failed_msg)=(0,0); + foreach my $key (keys(%env)) { + if ($key=~/^form\.delmark_(.*)$/) { + my ($result,$msg) = + &movemsg(&Apache::lonnet::unescape($1),$folder, + $env{'form.movetofolder'}); + if ($result) { + $total++; + } else { + $failed++; + push(@failed_msg,$msg); + } } } &printheader($r,'','Moved Messages'); - $r->print('Moved '.$total.' message(s)

'); + if ($failed) { + $r->print('

+ '.&mt('Failed to move [_1] message(s)',$failed). + '

'); + $r->print('

'. + join("

\n

",@failed_msg). + "

\n"); + } + $r->print(&mt('Moved [_1] message(s)',$total).'

'); &Apache::loncommunicate::menu($r); &disall($r,($folder?$folder:$dismode)); } elsif ($env{'form.markeddel'}) { - my $total=0; - foreach (keys %env) { - if ($_=~/^form\.delmark_(.*)$/) { - &statuschange(&Apache::lonnet::unescape($1),'deleted',$folder); - $total++; + my ($total,$failed,@failed_msg)=(0,0); + foreach my $key (keys(%env)) { + if ($key=~/^form\.delmark_(.*)$/) { + my ($result,$msg) = + &statuschange(&Apache::lonnet::unescape($1),'deleted', + $folder); + if ($result) { + $total++; + } else { + $failed++; + push(@failed_msg,$msg); + } } } &printheader($r,'','Deleted Messages'); - $r->print('Deleted '.$total.' message(s)

'); + if ($failed) { + $r->print('

+ '.&mt('Failed to delete [_1] message(s)',$failed). + '

'); + $r->print('

'. + join("

\n

",@failed_msg). + "

\n"); + } + $r->print(&mt('Deleted [_1] message(s)',$total).'

'); &Apache::loncommunicate::menu($r); &disall($r,($folder?$folder:$dismode)); } elsif ($env{'form.markunread'}) {