[LON-CAPA-cvs] cvs: loncom /homework grades.pm

foxr lon-capa-cvs-allow@mail.lon-capa.org
Tue, 04 Mar 2008 11:23:14 -0000


foxr		Tue Mar  4 06:23:14 2008 EDT

  Modified files:              
    /loncom/homework	grades.pm 
  Log:
  Added ssi error reporting to the user.
  
  
Index: loncom/homework/grades.pm
diff -u loncom/homework/grades.pm:1.512 loncom/homework/grades.pm:1.513
--- loncom/homework/grades.pm:1.512	Mon Mar  3 18:36:30 2008
+++ loncom/homework/grades.pm	Tue Mar  4 06:23:12 2008
@@ -1,7 +1,7 @@
 # The LearningOnline Network with CAPA
 # The LON-CAPA Grading handler
 #
-# $Id: grades.pm,v 1.512 2008/03/03 23:36:30 www Exp $
+# $Id: grades.pm,v 1.513 2008/03/04 11:23:12 foxr Exp $
 #
 # Copyright Michigan State University Board of Trustees
 #
@@ -47,8 +47,82 @@
 use POSIX qw(floor);
 
 
+
 my %perm=();
 
+#  These variables are used to recover from ssi errors
+
+my $ssi_retries = 5;
+my $ssi_error;
+my $ssi_error_resource;
+my $ssi_error_message;
+
+
+#  Do an ssi with retries:
+#  While I'd love to factor out this with the vesrion in lonprintout,
+#  that would either require a data coupling between modules, which I refuse to perpetuate
+#  (there's quite enough of that already), or would require the invention of another infrastructure
+#  I'm not quite ready to invent (e.g. an ssi_with_retry object).
+#
+# At least the logic that drives this has been pulled out into loncommon.
+
+
+#
+#   ssi_with_retries - Does the server side include of a resource.
+#                      if the ssi call returns an error we'll retry it up to
+#                      the number of times requested by the caller.
+#                      If we still have a proble, no text is appended to the
+#                      output and we set some global variables.
+#                      to indicate to the caller an SSI error occured.  
+#                      All of this is supposed to deal with the issues described
+#                      in LonCAPA BZ 5631 see:
+#                      http://bugs.lon-capa.org/show_bug.cgi?id=5631
+#                      by informing the user that this happened.
+#
+# Parameters:
+#   resource   - The resource to include.  This is passed directly, without
+#                interpretation to lonnet::ssi.
+#   form       - The form hash parameters that guide the interpretation of the resource
+#                
+#   retries    - Number of retries allowed before giving up completely.
+# Returns:
+#   On success, returns the rendered resource identified by the resource parameter.
+# Side Effects:
+#   The following global variables can be set:
+#    ssi_error                - If an unrecoverable error occured this becomes true.
+#                               It is up to the caller to initialize this to false
+#                               if desired.
+#    ssi_last_error_resource  - If an unrecoverable error occured, this is the value
+#                               of the resource that could not be rendered by the ssi
+#                               call.
+#    ssi_last_error           - The error string fetched from the ssi response
+#                               in the event of an error.
+#
+sub ssi_with_retries {
+    my ($resource, $retries, %form) = @_;
+    my ($content, $response) = &Apache::loncommon::ssi_with_retries($resource, $retries, %form);
+    if ($response->is_error) {
+	$ssi_error          = 1;
+	$ssi_error_resource = $resource;
+	$ssi_error_message  = $response->code . " " . $response->message;
+    }
+
+    return $content;
+
+}
+#
+#  Prodcuces an ssi retry failure error message to the user:
+#
+
+sub ssi_print_error {
+    my ($r) = @_;
+    $r->print('<h2>Unrecoverable network error</h2>');
+    $r->print('<p>Unable to perform a resource fetch from a server: <br />');
+    $r->print("Resource: $ssi_error_resource <br />");
+    $r->print("Error: $ssi_error_message <br /> Try again later.");
+    $r->print('If errors persist, contact LonCAPA support for assistance</p>');
+}
+
 #
 # --- Retrieve the parts from the metadata file.---
 sub getpartlist {
@@ -201,7 +275,7 @@
 
 	my (undef,undef,$url)=&Apache::lonnet::decode_symb($symb);
 	$url=&Apache::lonnet::clutter($url);
-	my $subresult=&Apache::lonnet::ssi($url,
+	my $subresult=&ssi_with_retries($url, $ssi_retries,
 					   ('grade_target' => 'analyze'),
 					   ('grade_domain' => $udom),
 					   ('grade_symb' => $symb),
@@ -4881,6 +4955,8 @@
     my $CODE_unique=&scantron_CODEunique();
     my $result;
 
+    $ssi_error = 0;
+
     # Chunk of form to prompt for a file to grade and how:
 
     $result.= '
@@ -7167,7 +7243,7 @@
                 }
             }
         }
-	my $result=&Apache::lonnet::ssi($resource->src(),
+	my $result=&ssi_with_retries($resource->src(), $ssi_retries,
 					('symb' => $resource->symb()),
 					('grade_target' => 'analyze'),
 					('grade_courseid' => $cid),
@@ -7341,9 +7417,12 @@
 
 sub scantron_process_students {
     my ($r) = @_;
+
     my (undef,undef,$sequence)=&Apache::lonnet::decode_symb($env{'form.selectpage'});
     my ($symb)=&get_symb($r);
-    if (!$symb) {return '';}
+    if (!$symb) {
+	return '';
+    }
     my $default_form_data=&defaultFormData($symb);
 
     my %scantron_config=&get_scantron_config($env{'form.scantron_format'});
@@ -7375,6 +7454,17 @@
     my ($uname,$udom,$started);
 
     &scantron_get_maxbubble();	# Need the bubble lines array to parse.
+    
+
+    # If an ssi failed in scantron_get_maxbubble, put an error message out to
+    # the user and return.
+
+    if ($ssi_error) {
+	$r->print("</form>");
+	&ssi_print_error($r);
+	$r->print(&show_grading_menu_form($symb));
+	return '';		# Dunno why the other returns return '' rather than just returning.
+    }
 
     while ($i<$scanlines->{'count'}) {
  	($uname,$udom)=('','');
@@ -7423,10 +7513,16 @@
 		$form{'CODE'}=$scan_record->{'scantron.CODE'};
 	    } else {
 		$form{'CODE'}='';
+	    } 
+	    my $result=&ssi_with_retries($resource->src(), $ssi_retries, %form);
+	    if ($ssi_error) {
+		$ssi_error = 0;	# So end of handler error message does not trigger.
+		$r->print("</form>");
+		&ssi_print_error($r);
+		$r->print(&show_grading_menu_form($symb));
+		return '';	# Why return ''?  Beats me.
 	    }
-	    my $result=&Apache::lonnet::ssi($resource->src(),%form);
-	    if ($result ne '') {
-	    }
+
 	    if (&Apache::loncommon::connection_aborted($r)) { last; }
 	}
 	$completedstudents{$uname}={'line'=>$line};
@@ -8466,7 +8562,7 @@
 	&Apache::lonnet::logthis("grades got multiple commands ".join(':',@commands));
     }
 
-
+    $ssi_error = 0;
     $request->print(&Apache::loncommon::start_page('Grading'));
     if ($symb eq '' && $command eq '') {
 	if ($env{'user.adv'}) {
@@ -8479,7 +8575,7 @@
 		if ($tsymb) {
 		    my ($map,$id,$url)=&Apache::lonnet::decode_symb($tsymb);
 		    if (&Apache::lonnet::allowed('mgr',$tcrsid)) {
-			$request->print(&Apache::lonnet::ssi_body('/res/'.$url,
+			$request->print(&ssi_with_retries('/res/'.$url, $ssi_retries,
 					  ('grade_username' => $tuname,
 					   'grade_domain' => $tudom,
 					   'grade_courseid' => $tcrsid,
@@ -8566,6 +8662,9 @@
 	    $request->print("Access Denied ($command)");
 	}
     }
+    if ($ssi_error) {
+	&ssi_print_error($request);
+    }
     $request->print(&Apache::loncommon::end_page());
     &reset_caches();
     return '';