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

raeburn raeburn@source.lon-capa.org
Wed, 09 Dec 2009 17:53:55 -0000


This is a MIME encoded message

--raeburn1260381235
Content-Type: text/plain

raeburn		Wed Dec  9 17:53:55 2009 EDT

  Modified files:              
    /loncom/homework	grades.pm 
  Log:
  - Eliminate some ISE occurrences.
  - Check attempt to create a navmap object resulted in an object. 
    - In case of failure display an error message which includes a link to re-initialize the course.
  
  
--raeburn1260381235
Content-Type: text/plain
Content-Disposition: attachment; filename="raeburn-20091209175355.txt"

Index: loncom/homework/grades.pm
diff -u loncom/homework/grades.pm:1.581 loncom/homework/grades.pm:1.582
--- loncom/homework/grades.pm:1.581	Sat Nov 21 16:41:41 2009
+++ loncom/homework/grades.pm	Wed Dec  9 17:53:55 2009
@@ -1,7 +1,7 @@
 # The LearningOnline Network with CAPA
 # The LON-CAPA Grading handler
 #
-# $Id: grades.pm,v 1.581 2009/11/21 16:41:41 www Exp $
+# $Id: grades.pm,v 1.582 2009/12/09 17:53:55 raeburn Exp $
 #
 # Copyright Michigan State University Board of Trustees
 #
@@ -97,9 +97,15 @@
 #
 # --- Retrieve the parts from the metadata file.---
 sub getpartlist {
-    my ($symb) = @_;
+    my ($symb,$errorref) = @_;
 
     my $navmap   = Apache::lonnavmaps::navmap->new();
+    unless (ref($navmap)) {
+        if (ref($errorref)) { 
+            $$errorref = 'navmap';
+            return;
+        }
+    }
     my $res      = $navmap->getBySymb($symb);
     my $partlist = $res->parts();
     my $url      = $res->src();
@@ -144,9 +150,15 @@
 #--- Get the partlist and the response type for a given problem. ---
 #--- Indicate if a response type is coded handgraded or not. ---
 sub response_type {
-    my ($symb) = shift;
+    my ($symb,$response_error) = @_;
 
     my $navmap = Apache::lonnavmaps::navmap->new();
+    unless (ref($navmap)) {
+        if (ref($response_error)) {
+            $$response_error = 1;
+        }
+        return;
+    }
     my $res = $navmap->getBySymb($symb);
     my $partlist = $res->parts();
     my %vPart = 
@@ -194,12 +206,17 @@
 #--- Show resource title
 #--- and parts and response type
 sub showResourceInfo {
-    my ($symb,$probTitle,$checkboxes) = @_;
+    my ($symb,$probTitle,$checkboxes,$res_error) = @_;
     my $col=3;
     if ($checkboxes) { $col=4; }
     my $result = '<h3>'.&mt('Current Resource').': '.$probTitle.'</h3>'."\n";
+    my ($partlist,$handgrade,$responseType) = &response_type($symb,$res_error);
+    if (ref($res_error)) {
+        if ($$res_error) {
+            return;
+        }
+    }
     $result .='<table border="0">';
-    my ($partlist,$handgrade,$responseType) = &response_type($symb);
     my %resptype = ();
     my $hdgrade='no';
     my %partsseen;
@@ -767,7 +784,13 @@
     if ($env{"course.$courseid.receiptalg"} eq 'receipt2' ||
 	$env{"course.$courseid.receiptalg"} eq 'receipt3') { $receiptparts=1; }
     my $parts=['0'];
-    if ($receiptparts) { ($parts)=&response_type($symb); }
+    if ($receiptparts) {
+        my $res_error; 
+        ($parts)=&response_type($symb,\$res_error);
+        if ($res_error) {
+            return &navmap_errormsg();
+        } 
+    }
     
     my $header = 
 	&Apache::loncommon::start_data_table().
@@ -1745,13 +1768,17 @@
         $$record{'resource.'.$partid.'.tries'}.'" />'."\n".
         '<input type="hidden" name="aggtries'.$counter.'_'.$partid.'" value="'.
         $aggtries.'" />'."\n";
-    $result.=&handback_box($symb,$uname,$udom,$counter,$partid,$record);
+    my $res_error;
+    $result.=&handback_box($symb,$uname,$udom,$counter,$partid,$record,\$res_error);
+    if ($res_error) {
+        return &navmap_errormsg();
+    }
     return $result;
 }
 
 sub handback_box {
-    my ($symb,$uname,$udom,$counter,$partid,$record) = @_;
-    my ($partlist,$handgrade,$responseType) = &response_type($symb);
+    my ($symb,$uname,$udom,$counter,$partid,$record,$res_error) = @_;
+    my ($partlist,$handgrade,$responseType) = &response_type($symb,$res_error);
     my (@respids);
      my @part_response_id = &flatten_responseType($responseType);
     foreach my $part_response_id (@part_response_id) {
@@ -2046,7 +2073,12 @@
     }
 
     my %record = &Apache::lonnet::restore($symb,$env{'request.course.id'},$udom,$uname);
-    my ($partlist,$handgrade,$responseType) = &response_type($symb);
+    my $res_error;
+    my ($partlist,$handgrade,$responseType) = &response_type($symb,\$res_error);
+    if ($res_error) {
+        $request->print(&navmap_errormsg());
+        return;
+    }
 
     # Display student info
     $request->print(($counter == 0 ? '' : '<br />'));
@@ -2597,7 +2629,12 @@
     }
     $ctr = 0;
     @parsedlist = reverse @parsedlist if ($button eq 'Previous');
-    my ($partlist) = &response_type($symb);
+    my $res_error;
+    my ($partlist) = &response_type($symb,\$res_error);
+    if ($res_error) {
+        $request->print(&navmap_errormsg());
+        return;
+    }
     foreach my $student (@parsedlist) {
 	my $submitonly=$env{'form.submitonly'};
 	my ($uname,$udom) = split(/:/,$student);
@@ -2795,8 +2832,12 @@
 sub handback_files {
     my ($request,$symb,$stuname,$domain,$newflg,$new_part,$newrecord) = @_;
     my $portfolio_root = '/userfiles/portfolio';
-    my ($partlist,$handgrade,$responseType) = &response_type($symb);
-
+    my $res_error;
+    my ($partlist,$handgrade,$responseType) = &response_type($symb,\$res_error);
+    if ($res_error) {
+        $request->print('<br />'.&navmap_errormsg().'<br />');
+        return;
+    }
     my @part_response_id = &flatten_responseType($responseType);
     foreach my $part_response_id (@part_response_id) {
     	my ($part_id,$resp_id) = @{ $part_response_id };
@@ -3254,7 +3295,11 @@
     $result.= '<h3>'.$common_header.'</h3>'.&Apache::loncommon::start_data_table();
     #radio buttons/text box for assigning points for a section or class.
     #handles different parts of a problem
-    my ($partlist,$handgrade,$responseType) = &response_type($symb);
+    my $res_error;
+    my ($partlist,$handgrade,$responseType) = &response_type($symb,\$res_error);
+    if ($res_error) {
+        return &navmap_errormsg();
+    }
     my %weight = ();
     my $ctsparts = 0;
     my %seen = ();
@@ -3314,7 +3359,11 @@
 	      &Apache::loncommon::start_data_table_header_row().
 	      '<th>'.&mt('No.').'</th>'.
 	      '<th>'.&nameUserString('header')."</th>\n";
-    my (@parts) = sort(&getpartlist($symb));
+    my $partserror;
+    my (@parts) = sort(&getpartlist($symb,\$partserror));
+    if ($partserror) {
+        return &navmap_errormsg();
+    }
     my (undef,undef,$url)=&Apache::lonnet::decode_symb($symb);
     my @partids = ();
     foreach my $part (@parts) {
@@ -3473,7 +3522,11 @@
     my %columns = ();
     my ($i,$ctr,$count,$rec_update) = (0,0,0,0);
 
-    my (@parts) = sort(&getpartlist($symb));
+    my $partserror;
+    my (@parts) = sort(&getpartlist($symb,\$partserror));
+    if ($partserror) {
+        return &navmap_errormsg();
+    }
     my $header;
     while ($ctr < $env{'form.totalparts'}) {
 	my $partid = $env{'form.partid_'.$ctr};
@@ -3805,8 +3858,14 @@
 }
 
 sub csvupload_fields {
-    my ($symb) = @_;
-    my (@parts) = &getpartlist($symb);
+    my ($symb,$errorref) = @_;
+    my (@parts) = &getpartlist($symb,$errorref);
+    if (ref($errorref)) {
+        if ($$errorref) {
+            return;
+        }
+    }
+
     my @fields=(['ID','Student/Employee ID'],
 		['username','Student Username'],
 		['domain','Student Domain']);
@@ -3906,8 +3965,12 @@
     &csvuploadmap_header($request,$symb,$datatoken,$#records+1);
     my ($i,$keyfields);
     if (@records) {
-	my @fields=&csvupload_fields($symb);
-
+        my $fieldserror;
+	my @fields=&csvupload_fields($symb,\$fieldserror);
+        if ($fieldserror) {
+            $request->print(&navmap_errormsg());
+            return;
+        }
 	if ($env{'form.upfile_associate'} eq 'reverse') {	
 	    &Apache::loncommon::csv_print_samples($request,\@records);
 	    $i=&Apache::loncommon::csv_print_select_table($request,\@records,
@@ -4143,7 +4206,12 @@
 	&mt('Manual Grading by Page or Sequence').'</span></h3>';
 
     $result.='<form action="/adm/grades" method="post" name="displayPage">'."\n";
-    my ($titles,$symbx) = &getSymbMap();
+    my $map_error;
+    my ($titles,$symbx) = &getSymbMap($map_error);
+    if ($map_error) {
+        $request->print(&navmap_errormsg());
+        return; 
+    }
     my ($curpage) =&Apache::lonnet::decode_symb($symb); 
 #    my ($curpage,$mapId) =&Apache::lonnet::decode_symb($symb); 
 #    my $type=($curpage =~ /\.(page|sequence)/);
@@ -4238,8 +4306,14 @@
 }
 
 sub getSymbMap {
+    my ($map_error) = @_;
     my $navmap = Apache::lonnavmaps::navmap->new();
-
+    unless (ref($navmap)) {
+        if (ref($map_error)) {
+            $$map_error = 'navmap';
+        }
+        return;
+    }
     my %symbx = ();
     my @titles = ();
     my $minder = 0;
@@ -4298,6 +4372,11 @@
     $request->print($result);
 
     my $navmap = Apache::lonnavmaps::navmap->new();
+    unless (ref($navmap)) {
+        $request->print(&navmap_errormsg());
+        $request->print(&show_grading_menu_form($symb));
+        return;
+    }
     my ($mapUrl, $id, $resUrl)=&Apache::lonnet::decode_symb($env{'form.page'});
     my $map = $navmap->getResourceByUrl($resUrl); # add to navmaps
     if (!$map) {
@@ -4552,7 +4631,12 @@
 
     $request->print($result);
 
+
     my $navmap = Apache::lonnavmaps::navmap->new();
+    unless (ref($navmap)) {
+        $request->print(&navmap_errormsg());
+        return;
+    }
     my ($mapUrl, $id, $resUrl) = &Apache::lonnet::decode_symb( $env{'form.page'});
     my $map = $navmap->getResourceByUrl($resUrl); # add to navmaps
     if (!$map) {
@@ -4775,14 +4859,19 @@
    Return html dropdown of possible sequences to grade
  
  Arguments:
-   $symb - $symb of the current resource 
+   $symb - $symb of the current resource
+   $map_error - ref to scalar which will container error if
+                $navmap object is unavailable in &getSymbMap().
 
 =cut
 
 sub getSequenceDropDown {
-    my ($symb)=@_;
+    my ($symb,$map_error)=@_;
     my $result='<select name="selectpage">'."\n";
-    my ($titles,$symbx) = &getSymbMap();
+    my ($titles,$symbx) = &getSymbMap($map_error);
+    if (ref($map_error)) {
+        return if ($$map_error);
+    }
     my ($curpage)=&Apache::lonnet::decode_symb($symb); 
     my $ctr=0;
     foreach (@$titles) {
@@ -5048,7 +5137,12 @@
     my ($r,$file2grade) = @_;
     my ($symb)=&get_symb($r);
     if (!$symb) {return '';}
-    my $sequence_selector=&getSequenceDropDown($symb);
+    my $map_error;
+    my $sequence_selector=&getSequenceDropDown($symb,\$map_error);
+    if ($map_error) {
+        $r->print('<br />'.&navmap_errormsg().'<br />');
+        return;
+    }
     my $default_form_data=&defaultFormData($symb);
     my $grading_menu_button=&show_grading_menu_form($symb);
     my $file_selector=&scantron_uploads($file2grade);
@@ -6222,7 +6316,12 @@
     $r->print('<p>'.&mt('Gathering necessary information.').'</p>');$r->rflush();
     #get the student pick code ready
     $r->print(&Apache::loncommon::studentbrowser_javascript());
-    my $max_bubble=&scantron_get_maxbubble();
+    my $nav_error;
+    my $max_bubble=&scantron_get_maxbubble(\$nav_error);
+    if ($nav_error) {
+        $r->print(&navmap_errormsg());
+        return '';
+    }
     my $result=&scantron_form_start($max_bubble).$default_form_data;
     $r->print($result);
     
@@ -6634,6 +6733,10 @@
     my ($r,$currentphase) = @_;
 
     my $navmap=Apache::lonnavmaps::navmap->new();
+    unless (ref($navmap)) {
+        $r->print(&navmap_errormsg());
+        return (1,$currentphase);
+    }
     my (undef,undef,$sequence)=
 	&Apache::lonnet::decode_symb($env{'form.selectpage'});
 
@@ -6665,8 +6768,13 @@
     #get scantron line setup
     my %scantron_config=&get_scantron_config($env{'form.scantron_format'});
     my ($scanlines,$scan_data)=&scantron_getfile();
-    
-    &scantron_get_maxbubble();	# parse needs the bubble_lines.. array.
+
+    my $nav_error;
+    &scantron_get_maxbubble(\$nav_error); # parse needs the bubble_lines.. array.
+    if ($nav_error) {
+        $r->print(&navmap_errormsg());
+        return(1,$currentphase);
+    }
 
     my %found=('ids'=>{},'usernames'=>{});
     for (my $i=0;$i<=$scanlines->{'count'};$i++) {
@@ -7219,7 +7327,12 @@
 
     my %allcodes=&get_codes();
 
-    &scantron_get_maxbubble();	# parse needs the lines per response array.
+    my $nav_error;
+    &scantron_get_maxbubble(\$nav_error); # parse needs the lines per response array.
+    if ($nav_error) {
+        $r->print(&navmap_errormsg());
+        return(1,$currentphase);
+    }
 
     my ($scanlines,$scan_data)=&scantron_getfile();
     for (my $i=0;$i<=$scanlines->{'count'};$i++) {
@@ -7291,6 +7404,7 @@
 
 
 sub scantron_get_maxbubble {
+    my ($nav_error) = @_;
     if (defined($env{'form.scantron_maxbubble'}) &&
 	$env{'form.scantron_maxbubble'}) {
 	&restore_bubble_lines();
@@ -7301,6 +7415,11 @@
 	&Apache::lonnet::decode_symb($env{'form.selectpage'});
 
     my $navmap=Apache::lonnavmaps::navmap->new();
+    unless (ref($navmap)) {
+        if (ref($nav_error)) {
+            $$nav_error = 1;
+        }
+    }
     my $map=$navmap->getResourceByUrl($sequence);
     my @resources=$navmap->retrieveResources($map,\&scantron_filter,1,0);
 
@@ -7390,7 +7509,11 @@
     #get scantron line setup
     my %scantron_config=&get_scantron_config($env{'form.scantron_format'});
     my ($scanlines,$scan_data)=&scantron_getfile();
-    my $max_bubble=&scantron_get_maxbubble();
+    my $nav_error;
+    my $max_bubble=&scantron_get_maxbubble(\$nav_error);
+    if ($nav_error) {
+        return(1,$currentphase);
+    }
     if (!$max_bubble) { $max_bubble=2**31; }
     for (my $i=0;$i<=$scanlines->{'count'};$i++) {
 	my $line=&scantron_get_line($scanlines,$scan_data,$i);
@@ -7450,6 +7573,10 @@
     my $classlist=&Apache::loncoursedata::get_classlist();
     my %idmap=&username_to_idmap($classlist);
     my $navmap=Apache::lonnavmaps::navmap->new();
+    unless (ref($navmap)) {
+        $r->print(&navmap_errormsg());
+        return '';
+    }  
     my $map=$navmap->getResourceByUrl($sequence);
     my @resources=$navmap->retrieveResources($map,\&scantron_filter,1,0);
     my (%grader_partids_by_symb,%grader_randomlists_by_symb);
@@ -7492,8 +7619,16 @@
     my $i=-1;
     my $started;
 
+    my $nav_error;
+    &scantron_get_maxbubble(\$nav_error); # Need the bubble lines array to parse.
+    if ($nav_error) {
+        $r->print(&navmap_errormsg());
+        return '';
+    }
+
     &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.
 
@@ -7976,6 +8111,10 @@
     my $classlist=&Apache::loncoursedata::get_classlist();
     my %idmap=&Apache::grades::username_to_idmap($classlist);
     my $navmap=Apache::lonnavmaps::navmap->new();
+    unless (ref($navmap)) {
+        $r->print(&navmap_errormsg());
+        return '';
+    }
     my $map=$navmap->getResourceByUrl($sequence);
     my @resources=$navmap->retrieveResources($map,\&scantron_filter,1,0);
     my (%grader_partids_by_symb,%grader_randomlists_by_symb);
@@ -7994,8 +8133,12 @@
                                     'Progress of Bubblesheet Data/Submission Records Comparison',$count,
                                     'inline',undef,'checkscantron');
     my ($username,$domain,$started);
-
-    &scantron_get_maxbubble();  # Need the bubble lines array to parse.
+    my $nav_error;
+    &scantron_get_maxbubble(\$nav_error); # Need the bubble lines array to parse.
+    if ($nav_error) {
+        $r->print(&navmap_errormsg());
+        return '';
+    }
 
     &Apache::lonhtmlcommon::Update_PrgWin($r,\%prog_state,
                                           'Processing first student');
@@ -8989,7 +9132,11 @@
     my ($symb)=&get_symb($r);
     if (!$symb) {return '';}
 # See which part we are saving to
-    my ($partlist,$handgrade,$responseType) = &response_type($symb);
+    my $res_error;
+    my ($partlist,$handgrade,$responseType) = &response_type($symb,\$res_error);
+    if ($res_error) {
+        return &navmap_errormsg();
+    }
 # FIXME: This should probably look for the first handgradeable part
     my $part=$$partlist[0];
 # Start screen output
@@ -9093,6 +9240,13 @@
     return $result.&show_grading_menu_form($symb);
 }
 
+sub navmap_errormsg {
+    return '<div class="LC_error">'.
+           &mt('An error occurred retrieving information about resources in the course.').'<br />'.
+           &mt('It is recommended that you [_1]re-initialize the course[_2] and then return to this grading page..','<a href="/adm/roles?selectrole=1&newrole='.$env{'request.role'}.'">','</a>').
+           '</div>';
+}
+
 sub handler {
     my $request=$_[0];
     &reset_caches();
@@ -9318,6 +9472,13 @@
 
 =item  scantron_get_maxbubble() : 
 
+   Arguments:
+       $nav_error  - Reference to scalar which is a flag to indicate a
+                      failure to retrieve a navmap object.
+       if $nav_error is set to 1 by scantron_get_maxbubble(), the 
+       calling routine should trap the error condition and display the warning
+       found in &navmap_errormsg().
+
    Returns the maximum number of bubble lines that are expected to
    occur. Does this by walking the selected sequence rendering the
    resource and then checking &Apache::lonxml::get_problem_counter()
@@ -9383,6 +9544,11 @@
    Validates all scanlines in the selected file to not have any
    invalid or underspecified student/employee IDs
 
+=item navmap_errormsg() :
+
+   Returns HTML mark-up inside a <div></div> with a link to re-initialize the course.
+   Should be called whenever the request to instantiate a navmap object fails.  
+
 =back
 
 =cut

--raeburn1260381235--