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

albertel lon-capa-cvs-allow@mail.lon-capa.org
Fri, 24 Aug 2007 07:15:30 -0000


albertel		Fri Aug 24 03:15:30 2007 EDT

  Modified files:              (Branch: version_2_5_X)
    /loncom/homework	grades.pm 
  Log:
  - backport 1.434
  
  
Index: loncom/homework/grades.pm
diff -u loncom/homework/grades.pm:1.428 loncom/homework/grades.pm:1.428.2.1
--- loncom/homework/grades.pm:1.428	Sat Aug 11 09:32:23 2007
+++ loncom/homework/grades.pm	Fri Aug 24 03:15:27 2007
@@ -1,7 +1,7 @@
 # The LearningOnline Network with CAPA
 # The LON-CAPA Grading handler
 #
-# $Id: grades.pm,v 1.428 2007/08/11 13:32:23 www Exp $
+# $Id: grades.pm,v 1.428.2.1 2007/08/24 07:15:27 albertel Exp $
 #
 # Copyright Michigan State University Board of Trustees
 #
@@ -45,7 +45,7 @@
 
 use POSIX qw(floor);
 
-my %perm=();
+my %perm;
 
 # ----- These first few routines are general use routines.----
 #
@@ -194,22 +194,54 @@
     return $result,$responseType,$hdgrade,$partlist,$handgrade;
 }
 
+sub reset_caches {
+    &reset_analyze_cache();
+    &reset_perm();
+}
 
-sub get_order {
-    my ($partid,$respid,$symb,$uname,$udom)=@_;
-    my (undef,undef,$url)=&Apache::lonnet::decode_symb($symb);
-    $url=&Apache::lonnet::clutter($url);
-    my $subresult=&Apache::lonnet::ssi($url,
-				       ('grade_target' => 'analyze'),
-				       ('grade_domain' => $udom),
-				       ('grade_symb' => $symb),
-				       ('grade_courseid' => 
-					        $env{'request.course.id'}),
-				       ('grade_username' => $uname));
-    (undef,$subresult)=split(/_HASH_REF__/,$subresult,2);
-    my %analyze=&Apache::lonnet::str2hash($subresult);
-    return ($analyze{"$partid.$respid.shown"});
+{
+    my %analyze_cache;
+
+    sub reset_analyze_cache {
+	undef(%analyze_cache);
+    }
+
+    sub get_analyze {
+	my ($symb,$uname,$udom)=@_;
+	my $key = "$symb\0$uname\0$udom";
+	return $analyze_cache{$key} if (exists($analyze_cache{$key}));
+
+	my (undef,undef,$url)=&Apache::lonnet::decode_symb($symb);
+	$url=&Apache::lonnet::clutter($url);
+	my $subresult=&Apache::lonnet::ssi($url,
+					   ('grade_target' => 'analyze'),
+					   ('grade_domain' => $udom),
+					   ('grade_symb' => $symb),
+					   ('grade_courseid' => 
+					    $env{'request.course.id'}),
+					   ('grade_username' => $uname));
+	(undef,$subresult)=split(/_HASH_REF__/,$subresult,2);
+	my %analyze=&Apache::lonnet::str2hash($subresult);
+	return $analyze_cache{$key} = \%analyze;
+    }
+
+    sub get_order {
+	my ($partid,$respid,$symb,$uname,$udom)=@_;
+	my $analyze = &get_analyze($symb,$uname,$udom);
+	return $analyze->{"$partid.$respid.shown"};
+    }
+
+    sub get_radiobutton_correct_foil {
+	my ($partid,$respid,$symb,$uname,$udom)=@_;
+	my $analyze = &get_analyze($symb,$uname,$udom);
+	foreach my $foil (@{&get_order($partid,$respid,$symb,$uname,$udom)}) {
+	    if ($analyze->{"$partid.$respid.foil.value.$foil"} eq 'true') {
+		return $foil;
+	    }
+	}
+    }
 }
+
 #--- Clean response type for display
 #--- Currently filters option/rank/radiobutton/match/essay/Task
 #        response types only.
@@ -258,11 +290,11 @@
     } elsif ($response eq 'radiobutton') {
 	my %answer=&Apache::lonnet::str2hash($answer);
 	my ($toprow,$bottomrow);
-	my $correct=($order->[0])+1;
-	for (my $i=1;$i<=$#$order;$i++) {
-	    my $foil=$order->[$i];
+	my $correct = 
+	    &get_radiobutton_correct_foil($partid,$respid,$symb,$uname,$udom);
+	foreach my $foil (@$order) {
 	    if (exists($answer{$foil})) {
-		if ($i == $correct) {
+		if ($foil eq $correct) {
 		    $toprow.='<td><b>true</b></td>';
 		} else {
 		    $toprow.='<td><i>true</i></td>';
@@ -7500,7 +7532,7 @@
 sub handler {
     my $request=$_[0];
 
-    &reset_perm();
+    &reset_caches();
     if ($env{'browser.mathml'}) {
 	&Apache::loncommon::content_type($request,'text/xml');
     } else {
@@ -7613,6 +7645,7 @@
 	}
     }
     $request->print(&Apache::loncommon::end_page());
+    &reset_caches();
     return '';
 }