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

raeburn raeburn@source.lon-capa.org
Mon, 04 May 2009 22:41:37 -0000


raeburn		Mon May  4 22:41:37 2009 EDT

  Modified files:              
    /loncom/homework	grades.pm 
  Log:
  - Bug 3576
   - New routine: &validate_uploaded_scantron_file().
     - Used to compare studentIDs in scantron file with IDs in course roster.
     - Report outcome to uploader.
   - Remove escaped quotes from &mt().
   - Move HTML (<span></span) to [_1], [_2] etc. in &mt().
   - Some wording changes.
  
  
Index: loncom/homework/grades.pm
diff -u loncom/homework/grades.pm:1.566 loncom/homework/grades.pm:1.567
--- loncom/homework/grades.pm:1.566	Fri May  1 14:16:13 2009
+++ loncom/homework/grades.pm	Mon May  4 22:41:37 2009
@@ -1,7 +1,7 @@
 # The LearningOnline Network with CAPA
 # The LON-CAPA Grading handler
 #
-# $Id: grades.pm,v 1.566 2009/05/01 14:16:13 raeburn Exp $
+# $Id: grades.pm,v 1.567 2009/05/04 22:41:37 raeburn Exp $
 #
 # Copyright Michigan State University Board of Trustees
 #
@@ -7787,7 +7787,7 @@
 	return '';
     }
     my %coursedata=&Apache::lonnet::coursedescription($env{'form.domainid'}.'_'.$env{'form.courseid'});
-    $r->print(&mt("Doing upload to [_1]",$coursedata{'description'})." <br />");
+    $r->print('<h3>'.&mt("Uploading file to [_1]",$coursedata{'description'}).'</h3>');
     my $fname=$env{'form.upfile.filename'};
     #FIXME
     #copied from lonnet::userfileupload()
@@ -7809,14 +7809,15 @@
     } else {
 	my $result=&Apache::lonnet::finishuserfileupload($env{'form.courseid'},$env{'form.domainid'},'upfile',$fname);
 	if ($result =~ m|^/uploaded/|) {
-	    $r->print(&mt("<span class=\"LC_success\">Success:</span> Successfully uploaded [_1] bytes of data into location [_2]",
-			  (length($env{'form.upfile'})-1),
-			  '<span class="LC_filename">'.$result."</span>"));
+	    $r->print(&mt('[_1]Success:[_2] Successfully uploaded [_3] bytes of data into location: [_4]',
+                          '<span class="LC_success">','</span>',(length($env{'form.upfile'})-1),
+			  '<span class="LC_filename">'.$result.'</span>'));
+            $r->print(&validate_uploaded_scantron_file($env{'form.domainid'},
+                                                       $env{'form.courseid'},$fname));
 	} else {
-	    $r->print(&mt("<span class=\"LC_error\">Error:</span> An error ([_1]) occurred when attempting to upload the file, [_2]",
-			  $result,
+	    $r->print(&mt('[_1]Error:[_2] An error ([_3]) occurred when attempting to upload the file, [_4]',
+                          '<span class="LC_error">','</span>',$result,
 			  '<span class="LC_filename">'.&HTML::Entities::encode($env{'form.upfile.filename'},'<>&"')."</span>"));
-
 	}
     }
     if ($symb) {
@@ -7827,6 +7828,92 @@
     return '';
 }
 
+sub validate_uploaded_scantron_file {
+    my ($cdom,$cname,$fname) = @_;
+    my $scanlines=&Apache::lonnet::getfile('/uploaded/'.$cdom.'/'.$cname.'/'.$fname);
+    my @lines;
+    if ($scanlines ne '-1') {
+        @lines=split("\n",$scanlines,-1);
+    }
+    my $output;
+    if (@lines) {
+        my (%counts,$max_match_format);
+        my ($max_match_count,$max_match_pct) = (0,0);
+        my $classlist = &Apache::loncoursedata::get_classlist($cdom,$cname);
+        my %idmap = &username_to_idmap($classlist);
+        foreach my $key (keys(%idmap)) {
+            my $lckey = lc($key);
+            $idmap{$lckey} = $idmap{$key};
+        }
+        my %unique_formats;
+        my @formatlines = &get_scantronformat_file();
+        foreach my $line (@formatlines) {
+            chomp($line);
+            my @config = split(/:/,$line);
+            my $idstart = $config[5];
+            my $idlength = $config[6];
+            if (($idstart ne '') && ($idlength > 0)) {
+                if (ref($unique_formats{$idstart.':'.$idlength}) eq 'ARRAY') {
+                    push(@{$unique_formats{$idstart.':'.$idlength}},$config[0].':'.$config[1]); 
+                } else {
+                    $unique_formats{$idstart.':'.$idlength} = [$config[0].':'.$config[1]];
+                }
+            }
+        }
+        foreach my $key (keys(%unique_formats)) {
+            my ($idstart,$idlength) = split(':',$key);
+            %{$counts{$key}} = (
+                               'found'   => 0,
+                               'total'   => 0,
+                              );
+            foreach my $line (@lines) {
+                next if ($line =~ /^#/);
+                next if ($line =~ /^[\s\cz]*$/);
+                my $id = substr($line,$idstart-1,$idlength);
+                $id = lc($id);
+                if (exists($idmap{$id})) {
+                    $counts{$key}{'found'} ++;
+                }
+                $counts{$key}{'total'} ++;
+            }
+            if ($counts{$key}{'total'}) {
+                my $percent_match = (100*$counts{$key}{'found'})/($counts{$key}{'total'});
+                if (($max_match_format eq '') || ($percent_match > $max_match_pct)) {
+                    $max_match_pct = $percent_match;
+                    $max_match_format = $key;
+                    $max_match_count = $counts{$key}{'total'};
+                }
+            }
+        }
+        if (ref($unique_formats{$max_match_format}) eq 'ARRAY') {
+            my $format_descs;
+            my $numwithformat = @{$unique_formats{$max_match_format}};
+            for (my $i=0; $i<$numwithformat; $i++) {
+                my ($name,$desc) = split(':',$unique_formats{$max_match_format}[$i]);
+                if ($i<$numwithformat-2) {
+                    $format_descs .= '"<i>'.$desc.'</i>", ';
+                } elsif ($i==$numwithformat-2) {
+                    $format_descs .= '"<i>'.$desc.'</i>" '.&mt('and').' ';
+                } elsif ($i==$numwithformat-1) {
+                    $format_descs .= '"<i>'.$desc.'</i>"';
+                }
+            }
+            my $showpct = sprintf("%.0f",$max_match_pct).'%';
+            $output .= '<br />'.&mt('Comparison of student IDs in the uploaded file with the course roster found matches for [_1] of the [_2] entries in the file (for the format defined for [_3]).','<b>'.$showpct.'</b>','<b>'.$max_match_count.'</b>',$format_descs).
+                       '<br />'.&mt('A low percentage of matches results from one of the following:').'<ul>'.
+                       '<li>'.&mt('The file was uploaded to the wrong course').'</li>'.
+                       '<li>'.&mt('The data are not in the format expected for the domain: [_1]',
+                                  '<i>'.$cdom.'</i>').'</li>'.
+                       '<li>'.&mt('Students did not bubble their IDs, or mis-bubbled them').'</li>'.
+                       '<li>'.&mt('The course roster is not up to date').'</li>'.
+                       '</ul>';
+        }
+    } else {
+        $output = '<span class="LC_warning">'.&mt('Uploaded file contained no data').'</span>';
+    }
+    return $output;
+}
+
 sub valid_file {
     my ($requested_file)=@_;
     foreach my $filename (sort(&scantron_filenames())) {