--- loncom/homework/grades.pm	2010/04/14 00:38:09	1.618
+++ loncom/homework/grades.pm	2010/04/18 18:45:41	1.623
@@ -1,7 +1,7 @@
 # The LearningOnline Network with CAPA
 # The LON-CAPA Grading handler
 #
-# $Id: grades.pm,v 1.618 2010/04/14 00:38:09 www Exp $
+# $Id: grades.pm,v 1.623 2010/04/18 18:45:41 www Exp $
 #
 # Copyright Michigan State University Board of Trustees
 #
@@ -43,6 +43,7 @@ use Apache::lonmsg();
 use Apache::Constants qw(:common);
 use Apache::lonlocal;
 use Apache::lonenc;
+use Apache::lonstathelpers;
 use String::Similarity;
 use LONCAPA;
 
@@ -137,6 +138,7 @@ sub nameUserString {
 
 #--- Get the partlist and the response type for a given problem. ---
 #--- Indicate if a response type is coded handgraded or not. ---
+#--- Sets response_error pointer to "1" if navmaps object broken ---
 sub response_type {
     my ($symb,$response_error) = @_;
 
@@ -821,10 +823,8 @@ sub listStudents {
     my $result='<h3><span class="LC_info">&nbsp;'
 	.&mt("View/Grade/Regrade Submissions for a Student or a Group of Students")
 	.'</span></h3>';
-
-    my ($partlist,$handgrade,$responseType) = &response_type($symb
-#,$res_error
-    );
+    my $res_error;
+    my ($partlist,$handgrade,$responseType) = &response_type($symb,\$res_error);
 
     my %lt = &Apache::lonlocal::texthash (
 		'multiple' => 'Please select a student or group of students before clicking on the Next button.',
@@ -1129,7 +1129,7 @@ sub check_buttons {
 
 #     Displays the submissions for one student or a group of students
 sub processGroup {
-    my ($request)  = shift;
+    my ($request,$symb)  = @_;
     my $ctr        = 0;
     my @stuchecked = &Apache::loncommon::get_env_multiple('form.stuinfo');
     my $total      = scalar(@stuchecked)-1;
@@ -1139,7 +1139,7 @@ sub processGroup {
 	$env{'form.student'}        = $uname;
 	$env{'form.userdom'}        = $udom;
 	$env{'form.fullname'}       = $fullname;
-	&submission($request,$ctr,$total);
+	&submission($request,$ctr,$total,$symb);
 	$ctr++;
     }
     return '';
@@ -1740,8 +1740,8 @@ sub gradeBox {
 }
 
 sub handback_box {
-    my ($symb,$uname,$udom,$counter,$partid,$record,$res_error) = @_;
-    my ($partlist,$handgrade,$responseType) = &response_type($symb,$res_error);
+    my ($symb,$uname,$udom,$counter,$partid,$record,$res_error_pointer) = @_;
+    my ($partlist,$handgrade,$responseType) = &response_type($symb,$res_error_pointer);
     my (@respids);
      my @part_response_id = &flatten_responseType($responseType);
     foreach my $part_response_id (@part_response_id) {
@@ -1845,6 +1845,11 @@ sub files_exist {
 
 sub download_all_link {
     my ($r,$symb) = @_;
+    unless (&files_exist($r, $symb)) {
+       $r->print(&mt('There are currently no submitted documents.'));
+       return;
+    }
+
     my $all_students = 
 	join("\n", &Apache::loncommon::get_env_multiple('form.stuinfo'));
 
@@ -1857,7 +1862,14 @@ sub download_all_link {
                              'cgi.'.$identifier.'.parts' => $parts,});
     $r->print('<a href="/cgi-bin/multidownload.pl?'.$identifier.'">'.
 	      &mt('Download All Submitted Documents').'</a>');
-    return
+    return;
+}
+
+sub submit_download_link {
+    my ($request,$symb) = @_;
+    if (!$symb) { return ''; }
+#FIXME: Figure out which type of problem this is and provide appropriate download
+    &download_all_link($request,$symb);
 }
 
 sub build_section_inputs {
@@ -1903,10 +1915,7 @@ sub submission {
     # header info
     if ($counter == 0) {
 	&sub_page_js($request);
-	&sub_page_kw_js($request) if ($env{'form.handgrade'} eq 'yes');
-	if ($env{'form.handgrade'} eq 'yes' && &files_exist($request, $symb)) {
-	    &download_all_link($request, $symb);
-	}
+	&sub_page_kw_js($request);
 	$request->print('<h3>&nbsp;<span class="LC_info">'.&mt('Submission Record').'</span></h3>');
 
 	# option to display problem, only once else it cause problems 
@@ -2120,7 +2129,7 @@ KEYWORDS
                     $lastsubonly.="\n".'<div class="LC_grade_submission_part">'.
                         '<b>'.&mt('Part: [_1]',$display_part).'</b>'.
                         ' <span class="LC_internal_info">'.
-                        '('.&mt('Part ID: [_1]',$respid).')'.
+                        '('.&mt('Response ID: [_1]',$respid).')'.
                         '</span>&nbsp; &nbsp;'.
 			'<span class="LC_warning">'.&mt('Nothing submitted - no attempts.').'</span><br /><br /></div>';
 		    next;
@@ -2165,7 +2174,7 @@ KEYWORDS
                         $lastsubonly.='<div class="LC_grade_submission_part">'.
                             '<b>'.&mt('Part: [_1]',$display_part).'</b>'.
                             ' <span class="LC_internal_info">'.
-                            '('.&mt('Part ID: [_1]',$respid).')'.
+                            '('.&mt('Response ID: [_1]',$respid).')'.
                             '</span>&nbsp; &nbsp;';
 			my $files=&get_submitted_files($udom,$uname,$partid,$respid,\%record);
 			if (@$files) {
@@ -2196,7 +2205,7 @@ KEYWORDS
 	}
 	$request->print($lastsubonly);
    } elsif ($env{'form.lastSub'} eq 'datesub') {
-        my ($parts,$handgrade,$responseType) = &response_type($symb);
+        my ($parts,$handgrade,$responseType) = &response_type($symb,\$res_error);
 	$request->print(&displaySubByDates($symb,\%record,$parts,$responseType,$checkIcon,$uname,$udom));
     } elsif ($env{'form.lastSub'} =~ /^(last|all)$/) {
 	$request->print(&Apache::loncommon::get_previous_attempt($symb,$uname,$udom,
@@ -2204,10 +2213,10 @@ KEYWORDS
 								 $last,'.submission',
 								 'Apache::grades::keywords_highlight'));
     }
-
     $request->print('<input type="hidden" name="unamedom'.$counter.'" value="'.$uname.':'
 	.$udom.'" />'."\n");
     # return if view submission with no grading option
+# FIXME: the logic seems off here. Why show the grade button if you cannot grade?
     if (!&canmodify($usec)) {
 	my $toGrade.='<input type="button" value="Grade Student" '.
 	    'onclick="javascript:checksubmit(this.form,\'Grade Student\',\''
@@ -8385,6 +8394,9 @@ sub grading_menu {
     $fields{'command'}='all_for_one';
     my $url1d=&Apache::lonhtmlcommon::build_url('grades/',\%fields);
 
+    $fields{'command'}='downloadfilesselect';
+    my $url1e=&Apache::lonhtmlcommon::build_url('grades/',\%fields);
+
     $fields{'command'} = 'csvform';
     my $url2 = &Apache::lonhtmlcommon::build_url('grades/',\%fields);
     
@@ -8423,6 +8435,12 @@ sub grading_menu {
                                 permission => 'F',
                                 icon => 'edit-find-replace.png',
                                 linktitle => 'Grade all resources in current page/sequence/folder for one student.'
+                        },
+                        {       linktext => 'Download submissions',
+                                url => $url1e,
+                                permission => 'F',
+                                icon => 'edit-find-replace.png',
+                                linktitle => 'Download all students submissions.'
                         }]},
                          { categorytitle=>'Automated Grading',
                items =>[
@@ -8516,7 +8534,27 @@ sub submit_options_table {
     return $result;
 }
 
+sub submit_options_download {
+    my ($request,$symb) = @_;
+    if (!$symb) {return '';}
 
+    &commonJSfunctions($request);
+
+    my $result='<form action="/adm/grades" method="post" name="gradingMenu">'."\n".
+        '<input type="hidden" name="symb"        value="'.&Apache::lonenc::check_encrypt($symb).'" />'."\n";
+    $result.='
+<h2>
+  '.&mt('Select Students for Which to Download Submissions').'
+</h2>'.&selectfield(1).'
+                <input type="hidden" name="command" value="downloadfileslink" /> 
+              <input type="submit" value="'.&mt('Next').' &rarr;" />
+            </div>
+          </div>
+
+
+  </form>';
+    return $result;
+}
 
 #--- Displays the submissions first page -------
 sub submit_options {
@@ -9130,6 +9168,14 @@ sub startpage {
     }
 }
 
+sub select_problem {
+    my ($r)=@_;
+    $r->print('<h2>'.&mt('Select the problem or one of the problems you want to grade').'</h2><form action="/adm/grades">');
+    $r->print(&Apache::lonstathelpers::problem_selector('.',undef,1));
+    $r->print('<input type="hidden" name="command" value="gradingmenu" />');
+    $r->print('<input type="submit" value="'.&mt('Next').' &rarr;" /></form>');
+}
+
 sub handler {
     my $request=$_[0];
     &reset_caches();
@@ -9161,11 +9207,12 @@ sub handler {
     &Apache::lonenc::check_decrypt(\$symb);                             
 
     $ssi_error = 0;
-    if ($symb eq '' && $command eq '') {
+    if ($symb eq '' || $command eq '') {
 #
 # Not called from a resource
 #    
-
+        &startpage($request,undef,[],1,1);
+        &select_problem($request);
     } else {
 	&init_perm();
 	if ($command eq 'submission' && $perm{'vgr'}) {
@@ -9188,7 +9235,8 @@ sub handler {
                                        {href=>'',text=>'Store grades'}],1,1);
 	    &updateGradeByPage($request,$symb);
 	} elsif ($command eq 'processGroup' && $perm{'vgr'}) {
-            &startpage($request,$symb);
+            &startpage($request,$symb,[{href=>'',text=>'...'},
+                                       {href=>'',text=>'Modify grades'}]);
 	    &processGroup($request,$symb);
 	} elsif ($command eq 'gradingmenu' && $perm{'vgr'}) {
             &startpage($request,$symb);
@@ -9209,7 +9257,8 @@ sub handler {
             &startpage($request,$symb,[{href=>&href_symb_cmd($symb,"table"), text=>"Grading table"},{href=>'', text=>"Modify grades"}]);
 	    $request->print(&viewgrades($request,$symb));
 	} elsif ($command eq 'handgrade' && $perm{'mgr'}) {
-            &startpage($request,$symb);
+            &startpage($request,$symb,[{href=>'',text=>'...'},
+                                       {href=>'',text=>'Store grades'}]);
 	    $request->print(&processHandGrade($request,$symb));
 	} elsif ($command eq 'editgrades' && $perm{'mgr'}) {
             &startpage($request,$symb,[{href=>&href_symb_cmd($symb,"table"), text=>"Grading table"},
@@ -9289,9 +9338,17 @@ sub handler {
  	    $request->print(&scantron_download_scantron_data($request,$symb));
         } elsif ($command eq 'checksubmissions' && $perm{'vgr'}) {
             &startpage($request,$symb,[{href=>'', text=>'Grade/Manage/Review Bubblesheets'}],1,1);
-            $request->print(&checkscantron_results($request,$symb));     
+            $request->print(&checkscantron_results($request,$symb));
+        } elsif ($command eq 'downloadfilesselect' && $perm{'vgr'}) {
+            &startpage($request,$symb,[{href=>'', text=>'Select which submissions to download'}]);
+            $request->print(&submit_options_download($request,$symb));
+         } elsif ($command eq 'downloadfileslink' && $perm{'vgr'}) {
+            &startpage($request,$symb,
+   [{href=>&href_symb_cmd($symb,'downloadfilesselect'), text=>'Select which submissions to download'},
+    {href=>'', text=>'Download submissions'}]);
+            &submit_download_link($request,$symb);
 	} elsif ($command) {
-            &startpage($request,$symb);
+            &startpage($request,$symb,[{href=>'', text=>'Access denied'}]);
 	    $request->print('<p class="LC_error">'.&mt('Access Denied ([_1])',$command).'</p>');
 	}
     }