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

albertel lon-capa-cvs@mail.lon-capa.org
Sun, 23 Apr 2006 05:34:45 -0000


albertel		Sun Apr 23 01:34:45 2006 EDT

  Modified files:              
    /loncom/interface	lonmsgdisplay.pm 
  Log:
  - BUG#4741, a complete failure to check any aspect of moving a message
              between folders  meant it was very easy to completly lose
              messages
     - checks if source and target folder are the same
     - checks for any error mesages from get / put and stops as soon as
       something goes wrong
     - requires the msgid to be moved to actually have a msg body to be moved
  
  - additionally, was failing to move messages to the trash in &storechange
    because of a double unescape of the msgid
   
  
  
Index: loncom/interface/lonmsgdisplay.pm
diff -u loncom/interface/lonmsgdisplay.pm:1.8 loncom/interface/lonmsgdisplay.pm:1.9
--- loncom/interface/lonmsgdisplay.pm:1.8	Sun Apr 23 00:04:38 2006
+++ loncom/interface/lonmsgdisplay.pm	Sun Apr 23 01:34:45 2006
@@ -1,7 +1,7 @@
 # The LearningOnline Network with CAPA
 # Routines for messaging display
 #
-# $Id: lonmsgdisplay.pm,v 1.8 2006/04/23 04:04:38 albertel Exp $
+# $Id: lonmsgdisplay.pm,v 1.9 2006/04/23 05:34:45 albertel Exp $
 #
 # Copyright Michigan State University Board of Trustees
 #
@@ -184,8 +184,9 @@
 	&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 @@
     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
@@ -657,7 +692,9 @@
                       $description.'</td><td>'.$status.'</td></tr>'."\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("</table>\n<p>".
@@ -1894,31 +1931,63 @@
 	&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('<p class="LC_error">Failed to delete the message.</p>'.
+		      '<p class="LC_error">'.$msg."</p>\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');
+	if ($failed) {
+	    $r->print('<p class="LC_error">
+                          Failed to move '.$failed.' message(s)</p>');
+	    $r->print('<p class="LC_error">'.
+		      join("</p>\n<p class=\"LC_error\">",@failed_msg).
+		      "</p>\n");
+	}
 	$r->print('Moved '.$total.' message(s)<p>');
 	&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');
+	if ($failed) {
+	    $r->print('<p class="LC_error">
+                          Failed to delete '.$failed.' message(s)</p>');
+	    $r->print('<p class="LC_error">'.
+		      join("</p>\n<p class=\"LC_error\">",@failed_msg).
+		      "</p>\n");
+	}
 	$r->print('Deleted '.$total.' message(s)<p>');
 	&Apache::loncommunicate::menu($r);
 	&disall($r,($folder?$folder:$dismode));