File:  [LON-CAPA] / loncom / homework / grades.pm
Revision 1.596: download - view: text, annotated - select for diffs
Sun Feb 28 23:31:42 2010 UTC (14 years, 2 months ago) by raeburn
Branches: MAIN
CVS tags: version_2_10_X, HEAD
- Bug 6119 Anonymous Survey.
  - course personnel can not view both identity of submitter and submission details.

    1: # The LearningOnline Network with CAPA
    2: # The LON-CAPA Grading handler
    3: #
    4: # $Id: grades.pm,v 1.596 2010/02/28 23:31:42 raeburn Exp $
    5: #
    6: # Copyright Michigan State University Board of Trustees
    7: #
    8: # This file is part of the LearningOnline Network with CAPA (LON-CAPA).
    9: #
   10: # LON-CAPA is free software; you can redistribute it and/or modify
   11: # it under the terms of the GNU General Public License as published by
   12: # the Free Software Foundation; either version 2 of the License, or
   13: # (at your option) any later version.
   14: #
   15: # LON-CAPA is distributed in the hope that it will be useful,
   16: # but WITHOUT ANY WARRANTY; without even the implied warranty of
   17: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   18: # GNU General Public License for more details.
   19: #
   20: # You should have received a copy of the GNU General Public License
   21: # along with LON-CAPA; if not, write to the Free Software
   22: # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
   23: #
   24: # /home/httpd/html/adm/gpl.txt
   25: #
   26: # http://www.lon-capa.org/
   27: #
   28: 
   29: 
   30: 
   31: package Apache::grades;
   32: use strict;
   33: use Apache::style;
   34: use Apache::lonxml;
   35: use Apache::lonnet;
   36: use Apache::loncommon;
   37: use Apache::lonhtmlcommon;
   38: use Apache::lonnavmaps;
   39: use Apache::lonhomework;
   40: use Apache::lonpickcode;
   41: use Apache::loncoursedata;
   42: use Apache::lonmsg();
   43: use Apache::Constants qw(:common);
   44: use Apache::lonlocal;
   45: use Apache::lonenc;
   46: use String::Similarity;
   47: use LONCAPA;
   48: 
   49: use POSIX qw(floor);
   50: 
   51: 
   52: 
   53: my %perm=();
   54: 
   55: #  These variables are used to recover from ssi errors
   56: 
   57: my $ssi_retries = 5;
   58: my $ssi_error;
   59: my $ssi_error_resource;
   60: my $ssi_error_message;
   61: 
   62: 
   63: sub ssi_with_retries {
   64:     my ($resource, $retries, %form) = @_;
   65:     my ($content, $response) = &Apache::loncommon::ssi_with_retries($resource, $retries, %form);
   66:     if ($response->is_error) {
   67: 	$ssi_error          = 1;
   68: 	$ssi_error_resource = $resource;
   69: 	$ssi_error_message  = $response->code . " " . $response->message;
   70:     }
   71: 
   72:     return $content;
   73: 
   74: }
   75: #
   76: #  Prodcuces an ssi retry failure error message to the user:
   77: #
   78: 
   79: sub ssi_print_error {
   80:     my ($r) = @_;
   81:     my $helpurl = &Apache::loncommon::top_nav_help('Helpdesk');
   82:     $r->print('
   83: <br />
   84: <h2>'.&mt('An unrecoverable network error occurred:').'</h2>
   85: <p>
   86: '.&mt('Unable to retrieve a resource from a server:').'<br />
   87: '.&mt('Resource:').' '.$ssi_error_resource.'<br />
   88: '.&mt('Error:').' '.$ssi_error_message.'
   89: </p>
   90: <p>'.
   91: &mt('It is recommended that you try again later, as this error may mean the server was just temporarily unavailable, or is down for maintenance.').'<br />'.
   92: &mt('If the error persists, please contact the [_1] for assistance.',$helpurl).
   93: '</p>');
   94:     return;
   95: }
   96: 
   97: #
   98: # --- Retrieve the parts from the metadata file.---
   99: sub getpartlist {
  100:     my ($symb,$errorref) = @_;
  101: 
  102:     my $navmap   = Apache::lonnavmaps::navmap->new();
  103:     unless (ref($navmap)) {
  104:         if (ref($errorref)) { 
  105:             $$errorref = 'navmap';
  106:             return;
  107:         }
  108:     }
  109:     my $res      = $navmap->getBySymb($symb);
  110:     my $partlist = $res->parts();
  111:     my $url      = $res->src();
  112:     my @metakeys = split(/,/,&Apache::lonnet::metadata($url,'keys'));
  113: 
  114:     my @stores;
  115:     foreach my $part (@{ $partlist }) {
  116: 	foreach my $key (@metakeys) {
  117: 	    if ($key =~ m/^stores_\Q$part\E_/) { push(@stores,$key); }
  118: 	}
  119:     }
  120:     return @stores;
  121: }
  122: 
  123: # --- Get the symbolic name of a problem and the url
  124: sub get_symb {
  125:     my ($request,$silent) = @_;
  126:     (my $url=$env{'form.url'}) =~ s-^http://($ENV{'SERVER_NAME'}|$ENV{'HTTP_HOST'})--;
  127:     my $symb=($env{'form.symb'} ne '' ? $env{'form.symb'} : (&Apache::lonnet::symbread($url)));
  128:     if ($symb eq '') { 
  129: 	if (!$silent) {
  130: 	    $request->print("Unable to handle ambiguous references:$url:.");
  131: 	    return ();
  132: 	}
  133:     }
  134:     &Apache::lonenc::check_decrypt(\$symb);
  135:     return ($symb);
  136: }
  137: 
  138: #--- Format fullname, username:domain if different for display
  139: #--- Use anywhere where the student names are listed
  140: sub nameUserString {
  141:     my ($type,$fullname,$uname,$udom) = @_;
  142:     if ($type eq 'header') {
  143: 	return '<b>&nbsp;'.&mt('Fullname').'&nbsp;</b><span class="LC_internal_info">('.&mt('Username').')</span>';
  144:     } else {
  145: 	return '&nbsp;'.$fullname.'<span class="LC_internal_info">&nbsp;('.$uname.
  146: 	    ($env{'user.domain'} eq $udom ? '' : ' ('.$udom.')').')</span>';
  147:     }
  148: }
  149: 
  150: #--- Get the partlist and the response type for a given problem. ---
  151: #--- Indicate if a response type is coded handgraded or not. ---
  152: sub response_type {
  153:     my ($symb,$response_error) = @_;
  154: 
  155:     my $navmap = Apache::lonnavmaps::navmap->new();
  156:     unless (ref($navmap)) {
  157:         if (ref($response_error)) {
  158:             $$response_error = 1;
  159:         }
  160:         return;
  161:     }
  162:     my $res = $navmap->getBySymb($symb);
  163:     unless (ref($res)) {
  164:         $$response_error = 1;
  165:         return;
  166:     }
  167:     my $partlist = $res->parts();
  168:     my %vPart = 
  169: 	map { $_ => 1 } (&Apache::loncommon::get_env_multiple('form.vPart'));
  170:     my (%response_types,%handgrade);
  171:     foreach my $part (@{ $partlist }) {
  172: 	next if (%vPart && !exists($vPart{$part}));
  173: 
  174: 	my @types = $res->responseType($part);
  175: 	my @ids = $res->responseIds($part);
  176: 	for (my $i=0; $i < scalar(@ids); $i++) {
  177: 	    $response_types{$part}{$ids[$i]} = $types[$i];
  178: 	    $handgrade{$part.'_'.$ids[$i]} = 
  179: 		&Apache::lonnet::EXT('resource.'.$part.'_'.$ids[$i].
  180: 				     '.handgrade',$symb);
  181: 	}
  182:     }
  183:     return ($partlist,\%handgrade,\%response_types);
  184: }
  185: 
  186: sub flatten_responseType {
  187:     my ($responseType) = @_;
  188:     my @part_response_id =
  189: 	map { 
  190: 	    my $part = $_;
  191: 	    map {
  192: 		[$part,$_]
  193: 		} sort(keys(%{ $responseType->{$part} }));
  194: 	} sort(keys(%$responseType));
  195:     return @part_response_id;
  196: }
  197: 
  198: sub get_display_part {
  199:     my ($partID,$symb)=@_;
  200:     my $display=&Apache::lonnet::EXT('resource.'.$partID.'.display',$symb);
  201:     if (defined($display) and $display ne '') {
  202:         $display.= ' (<span class="LC_internal_info">'
  203:                   .&mt('Part ID: [_1]',$partID).'</span>)';
  204:     } else {
  205: 	$display=$partID;
  206:     }
  207:     return $display;
  208: }
  209: 
  210: #--- Show resource title
  211: #--- and parts and response type
  212: sub showResourceInfo {
  213:     my ($symb,$probTitle,$checkboxes,$res_error) = @_;
  214:     my $result = '<h3>'.&mt('Current Resource').': '.$probTitle.'</h3>'."\n";
  215:     my ($partlist,$handgrade,$responseType) = &response_type($symb,$res_error);
  216:     if (ref($res_error)) {
  217:         if ($$res_error) {
  218:             return;
  219:         }
  220:     }
  221:     $result.=&Apache::loncommon::start_data_table()
  222:             .&Apache::loncommon::start_data_table_header_row();
  223:     if ($checkboxes) {
  224:         $result.='<th>&nbsp;</th>';
  225:     }
  226:     $result.='<th>'.&mt('Problem Part').'</th>'
  227:             .'<th>'.&mt('Res. ID').'</th>'
  228:             .'<th>'.&mt('Type').'</th>'
  229:             .&Apache::loncommon::end_data_table_header_row();
  230:     my %resptype = ();
  231:     my $hdgrade='no';
  232:     my %partsseen;
  233:     foreach my $partID (sort(keys(%$responseType))) {
  234:         foreach my $resID (sort(keys(%{ $responseType->{$partID} }))) {
  235:             my $handgrade=$$handgrade{$partID.'_'.$resID};
  236:             my $responsetype = $responseType->{$partID}->{$resID};
  237:             $hdgrade = $handgrade if ($handgrade eq 'yes');
  238:             $result.=&Apache::loncommon::start_data_table_row();
  239:             if ($checkboxes) {
  240:                 if (exists($partsseen{$partID})) {
  241:                     $result.="<td>&nbsp;</td>";
  242:                 } else {
  243:                     $result.="<td><input type='checkbox' name='vPart' value='$partID' checked='checked' /></td>";
  244:                 }
  245:                 $partsseen{$partID}=1;
  246:             }
  247:             my $display_part=&get_display_part($partID,$symb);
  248:             $result.='<td>'.$display_part.'</td>'
  249:                     .'<td>'.'<span class="LC_internal_info">'.$resID.'</span></td>'
  250:                     .'<td>'.&mt($responsetype).'</td>'
  251: #                   .'<td>'.&mt('<b>Handgrade: </b>[_1]',$handgrade).'</td>'
  252:                     .&Apache::loncommon::end_data_table_row();
  253:         }
  254:     }
  255:     $result.=&Apache::loncommon::end_data_table();
  256:     return $result,$responseType,$hdgrade,$partlist,$handgrade;
  257: }
  258: 
  259: sub reset_caches {
  260:     &reset_analyze_cache();
  261:     &reset_perm();
  262: }
  263: 
  264: {
  265:     my %analyze_cache;
  266:     my %analyze_cache_formkeys;
  267: 
  268:     sub reset_analyze_cache {
  269: 	undef(%analyze_cache);
  270:         undef(%analyze_cache_formkeys);
  271:     }
  272: 
  273:     sub get_analyze {
  274: 	my ($symb,$uname,$udom,$no_increment,$add_to_hash)=@_;
  275: 	my $key = "$symb\0$uname\0$udom";
  276: 	if (exists($analyze_cache{$key})) {
  277:             my $getupdate = 0;
  278:             if (ref($add_to_hash) eq 'HASH') {
  279:                 foreach my $item (keys(%{$add_to_hash})) {
  280:                     if (ref($analyze_cache_formkeys{$key}) eq 'HASH') {
  281:                         if (!exists($analyze_cache_formkeys{$key}{$item})) {
  282:                             $getupdate = 1;
  283:                             last;
  284:                         }
  285:                     } else {
  286:                         $getupdate = 1;
  287:                     }
  288:                 }
  289:             }
  290:             if (!$getupdate) {
  291:                 return $analyze_cache{$key};
  292:             }
  293:         }
  294: 
  295: 	my (undef,undef,$url)=&Apache::lonnet::decode_symb($symb);
  296: 	$url=&Apache::lonnet::clutter($url);
  297:         my %form = ('grade_target'      => 'analyze',
  298:                     'grade_domain'      => $udom,
  299:                     'grade_symb'        => $symb,
  300:                     'grade_courseid'    =>  $env{'request.course.id'},
  301:                     'grade_username'    => $uname,
  302:                     'grade_noincrement' => $no_increment);
  303:         if (ref($add_to_hash)) {
  304:             %form = (%form,%{$add_to_hash});
  305:         } 
  306: 	my $subresult=&ssi_with_retries($url, $ssi_retries,%form);
  307: 	(undef,$subresult)=split(/_HASH_REF__/,$subresult,2);
  308: 	my %analyze=&Apache::lonnet::str2hash($subresult);
  309:         if (ref($add_to_hash) eq 'HASH') {
  310:             $analyze_cache_formkeys{$key} = $add_to_hash;
  311:         } else {
  312:             $analyze_cache_formkeys{$key} = {};
  313:         }
  314: 	return $analyze_cache{$key} = \%analyze;
  315:     }
  316: 
  317:     sub get_order {
  318: 	my ($partid,$respid,$symb,$uname,$udom,$no_increment)=@_;
  319: 	my $analyze = &get_analyze($symb,$uname,$udom,$no_increment);
  320: 	return $analyze->{"$partid.$respid.shown"};
  321:     }
  322: 
  323:     sub get_radiobutton_correct_foil {
  324: 	my ($partid,$respid,$symb,$uname,$udom)=@_;
  325: 	my $analyze = &get_analyze($symb,$uname,$udom);
  326:         my $foils = &get_order($partid,$respid,$symb,$uname,$udom);
  327:         if (ref($foils) eq 'ARRAY') {
  328: 	    foreach my $foil (@{$foils}) {
  329: 	        if ($analyze->{"$partid.$respid.foil.value.$foil"} eq 'true') {
  330: 		    return $foil;
  331: 	        }
  332: 	    }
  333: 	}
  334:     }
  335: 
  336:     sub scantron_partids_tograde {
  337:         my ($resource,$cid,$uname,$udom,$check_for_randomlist) = @_;
  338:         my (%analysis,@parts);
  339:         if (ref($resource)) {
  340:             my $symb = $resource->symb();
  341:             my $add_to_form;
  342:             if ($check_for_randomlist) {
  343:                 $add_to_form = { 'check_parts_withrandomlist' => 1,};
  344:             }
  345:             my $analyze = &get_analyze($symb,$uname,$udom,undef,$add_to_form);
  346:             if (ref($analyze) eq 'HASH') {
  347:                 %analysis = %{$analyze};
  348:             }
  349:             if (ref($analysis{'parts'}) eq 'ARRAY') {
  350:                 foreach my $part (@{$analysis{'parts'}}) {
  351:                     my ($id,$respid) = split(/\./,$part);
  352:                     if (!&Apache::loncommon::check_if_partid_hidden($id,$symb,$udom,$uname)) {
  353:                         push(@parts,$part);
  354:                     }
  355:                 }
  356:             }
  357:         }
  358:         return (\%analysis,\@parts);
  359:     }
  360: 
  361: }
  362: 
  363: #--- Clean response type for display
  364: #--- Currently filters option/rank/radiobutton/match/essay/Task
  365: #        response types only.
  366: sub cleanRecord {
  367:     my ($answer,$response,$symb,$partid,$respid,$record,$order,$version,
  368: 	$uname,$udom) = @_;
  369:     my $grayFont = '<span class="LC_internal_info">';
  370:     if ($response =~ /^(option|rank)$/) {
  371: 	my %answer=&Apache::lonnet::str2hash($answer);
  372: 	my %grading=&Apache::lonnet::str2hash($record->{$version."resource.$partid.$respid.submissiongrading"});
  373: 	my ($toprow,$bottomrow);
  374: 	foreach my $foil (@$order) {
  375: 	    if ($grading{$foil} == 1) {
  376: 		$toprow.='<td><b>'.$answer{$foil}.'&nbsp;</b></td>';
  377: 	    } else {
  378: 		$toprow.='<td><i>'.$answer{$foil}.'&nbsp;</i></td>';
  379: 	    }
  380: 	    $bottomrow.='<td>'.$grayFont.$foil.'</span>&nbsp;</td>';
  381: 	}
  382: 	return '<blockquote><table border="1">'.
  383: 	    '<tr valign="top"><td>'.&mt('Answer').'</td>'.$toprow.'</tr>'.
  384: 	    '<tr valign="top"><td>'.$grayFont.&mt('Option ID').'</span></td>'.
  385: 	    $grayFont.$bottomrow.'</tr>'.'</table></blockquote>';
  386:     } elsif ($response eq 'match') {
  387: 	my %answer=&Apache::lonnet::str2hash($answer);
  388: 	my %grading=&Apache::lonnet::str2hash($record->{$version."resource.$partid.$respid.submissiongrading"});
  389: 	my @items=&Apache::lonnet::str2array($record->{$version."resource.$partid.$respid.submissionitems"});
  390: 	my ($toprow,$middlerow,$bottomrow);
  391: 	foreach my $foil (@$order) {
  392: 	    my $item=shift(@items);
  393: 	    if ($grading{$foil} == 1) {
  394: 		$toprow.='<td><b>'.$item.'&nbsp;</b></td>';
  395: 		$middlerow.='<td><b>'.$grayFont.$answer{$foil}.'&nbsp;</span></b></td>';
  396: 	    } else {
  397: 		$toprow.='<td><i>'.$item.'&nbsp;</i></td>';
  398: 		$middlerow.='<td><i>'.$grayFont.$answer{$foil}.'&nbsp;</span></i></td>';
  399: 	    }
  400: 	    $bottomrow.='<td>'.$grayFont.$foil.'</span>&nbsp;</td>';
  401: 	}
  402: 	return '<blockquote><table border="1">'.
  403: 	    '<tr valign="top"><td>'.&mt('Answer').'</td>'.$toprow.'</tr>'.
  404: 	    '<tr valign="top"><td>'.$grayFont.&mt('Item ID').'</span></td>'.
  405: 	    $middlerow.'</tr>'.
  406: 	    '<tr valign="top"><td>'.$grayFont.&mt('Option ID').'</span></td>'.
  407: 	    $bottomrow.'</tr>'.'</table></blockquote>';
  408:     } elsif ($response eq 'radiobutton') {
  409: 	my %answer=&Apache::lonnet::str2hash($answer);
  410: 	my ($toprow,$bottomrow);
  411: 	my $correct = 
  412: 	    &get_radiobutton_correct_foil($partid,$respid,$symb,$uname,$udom);
  413: 	foreach my $foil (@$order) {
  414: 	    if (exists($answer{$foil})) {
  415: 		if ($foil eq $correct) {
  416: 		    $toprow.='<td><b>'.&mt('true').'</b></td>';
  417: 		} else {
  418: 		    $toprow.='<td><i>'.&mt('true').'</i></td>';
  419: 		}
  420: 	    } else {
  421: 		$toprow.='<td>'.&mt('false').'</td>';
  422: 	    }
  423: 	    $bottomrow.='<td>'.$grayFont.$foil.'</span>&nbsp;</td>';
  424: 	}
  425: 	return '<blockquote><table border="1">'.
  426: 	    '<tr valign="top"><td>'.&mt('Answer').'</td>'.$toprow.'</tr>'.
  427: 	    '<tr valign="top"><td>'.$grayFont.&mt('Option ID').'</span></td>'.
  428: 	    $grayFont.$bottomrow.'</tr>'.'</table></blockquote>';
  429:     } elsif ($response eq 'essay') {
  430: 	if (! exists ($env{'form.'.$symb})) {
  431: 	    my (%keyhash) = &Apache::lonnet::dump('nohist_handgrade',
  432: 						  $env{'course.'.$env{'request.course.id'}.'.domain'},
  433: 						  $env{'course.'.$env{'request.course.id'}.'.num'});
  434: 
  435: 	    my $loginuser = $env{'user.name'}.':'.$env{'user.domain'};
  436: 	    $env{'form.keywords'} = $keyhash{$symb.'_keywords'} ne '' ? $keyhash{$symb.'_keywords'} : '';
  437: 	    $env{'form.kwclr'}    = $keyhash{$loginuser.'_kwclr'} ne '' ? $keyhash{$loginuser.'_kwclr'} : 'red';
  438: 	    $env{'form.kwsize'}   = $keyhash{$loginuser.'_kwsize'} ne '' ? $keyhash{$loginuser.'_kwsize'} : '0';
  439: 	    $env{'form.kwstyle'}  = $keyhash{$loginuser.'_kwstyle'} ne '' ? $keyhash{$loginuser.'_kwstyle'} : '';
  440: 	    $env{'form.'.$symb} = 1; # so that we don't have to read it from disk for multiple sub of the same prob.
  441: 	}
  442: 	$answer =~ s-\n-<br />-g;
  443: 	return '<br /><br /><blockquote><tt>'.&keywords_highlight($answer).'</tt></blockquote>';
  444:     } elsif ( $response eq 'organic') {
  445: 	my $result='Smile representation: "<tt>'.$answer.'</tt>"';
  446: 	my $jme=$record->{$version."resource.$partid.$respid.molecule"};
  447: 	$result.=&Apache::chemresponse::jme_img($jme,$answer,400);
  448: 	return $result;
  449:     } elsif ( $response eq 'Task') {
  450: 	if ( $answer eq 'SUBMITTED') {
  451: 	    my $files = $record->{$version."resource.$respid.$partid.bridgetask.portfiles"};
  452: 	    my $result = &Apache::bridgetask::file_list($files,$uname,$udom);
  453: 	    return $result;
  454: 	} elsif ( grep(/^\Q$version\E.*?\.instance$/, keys(%{$record})) ) {
  455: 	    my @matches = grep(/^\Q$version\E.*?\.instance$/,
  456: 			       keys(%{$record}));
  457: 	    return join('<br />',($version,@matches));
  458: 			       
  459: 			       
  460: 	} else {
  461: 	    my $result =
  462: 		'<p>'
  463: 		.&mt('Overall result: [_1]',
  464: 		     $record->{$version."resource.$respid.$partid.status"})
  465: 		.'</p>';
  466: 	    
  467: 	    $result .= '<ul>';
  468: 	    my @grade = grep(/^\Q${version}resource.$respid.$partid.\E[^.]*[.]status$/,
  469: 			     keys(%{$record}));
  470: 	    foreach my $grade (sort(@grade)) {
  471: 		my ($dim) = ($grade =~/[.]([^.]+)[.]status$/);
  472: 		$result.= '<li>'.&mt("Dimension: [_1], status [_2] ",
  473: 				     $dim, $record->{$grade}).
  474: 			  '</li>';
  475: 	    }
  476: 	    $result.='</ul>';
  477: 	    return $result;
  478: 	}
  479:     } elsif ( $response =~ m/(?:numerical|formula)/) {
  480: 	$answer = 
  481: 	    &Apache::loncommon::format_previous_attempt_value('submission',
  482: 							      $answer);
  483:     }
  484:     return $answer;
  485: }
  486: 
  487: #-- A couple of common js functions
  488: sub commonJSfunctions {
  489:     my $request = shift;
  490:     $request->print(<<COMMONJSFUNCTIONS);
  491: <script type="text/javascript" language="javascript">
  492:     function radioSelection(radioButton) {
  493: 	var selection=null;
  494: 	if (radioButton.length > 1) {
  495: 	    for (var i=0; i<radioButton.length; i++) {
  496: 		if (radioButton[i].checked) {
  497: 		    return radioButton[i].value;
  498: 		}
  499: 	    }
  500: 	} else {
  501: 	    if (radioButton.checked) return radioButton.value;
  502: 	}
  503: 	return selection;
  504:     }
  505: 
  506:     function pullDownSelection(selectOne) {
  507: 	var selection="";
  508: 	if (selectOne.length > 1) {
  509: 	    for (var i=0; i<selectOne.length; i++) {
  510: 		if (selectOne[i].selected) {
  511: 		    return selectOne[i].value;
  512: 		}
  513: 	    }
  514: 	} else {
  515:             // only one value it must be the selected one
  516: 	    return selectOne.value;
  517: 	}
  518:     }
  519: </script>
  520: COMMONJSFUNCTIONS
  521: }
  522: 
  523: #--- Dumps the class list with usernames,list of sections,
  524: #--- section, ids and fullnames for each user.
  525: sub getclasslist {
  526:     my ($getsec,$filterlist,$getgroup) = @_;
  527:     my @getsec;
  528:     my @getgroup;
  529:     my $stu_status = join(':',&Apache::loncommon::get_env_multiple('form.Status'));
  530:     if (!ref($getsec)) {
  531: 	if ($getsec ne '' && $getsec ne 'all') {
  532: 	    @getsec=($getsec);
  533: 	}
  534:     } else {
  535: 	@getsec=@{$getsec};
  536:     }
  537:     if (grep(/^all$/,@getsec)) { undef(@getsec); }
  538:     if (!ref($getgroup)) {
  539: 	if ($getgroup ne '' && $getgroup ne 'all') {
  540: 	    @getgroup=($getgroup);
  541: 	}
  542:     } else {
  543: 	@getgroup=@{$getgroup};
  544:     }
  545:     if (grep(/^all$/,@getgroup)) { undef(@getgroup); }
  546: 
  547:     my ($classlist,$keylist)=&Apache::loncoursedata::get_classlist();
  548:     # Bail out if we were unable to get the classlist
  549:     return if (! defined($classlist));
  550:     &Apache::loncoursedata::get_group_memberships($classlist,$keylist);
  551:     #
  552:     my %sections;
  553:     my %fullnames;
  554:     foreach my $student (keys(%$classlist)) {
  555:         my $end      = 
  556:             $classlist->{$student}->[&Apache::loncoursedata::CL_END()];
  557:         my $start    = 
  558:             $classlist->{$student}->[&Apache::loncoursedata::CL_START()];
  559:         my $id       = 
  560:             $classlist->{$student}->[&Apache::loncoursedata::CL_ID()];
  561:         my $section  = 
  562:             $classlist->{$student}->[&Apache::loncoursedata::CL_SECTION()];
  563:         my $fullname = 
  564:             $classlist->{$student}->[&Apache::loncoursedata::CL_FULLNAME()];
  565:         my $status   = 
  566:             $classlist->{$student}->[&Apache::loncoursedata::CL_STATUS()];
  567:         my $group   = 
  568:             $classlist->{$student}->[&Apache::loncoursedata::CL_GROUP()];
  569: 	# filter students according to status selected
  570: 	if ($filterlist && (!($stu_status =~ /Any/))) {
  571: 	    if (!($stu_status =~ $status)) {
  572: 		delete($classlist->{$student});
  573: 		next;
  574: 	    }
  575: 	}
  576: 	# filter students according to groups selected
  577: 	my @stu_groups = split(/,/,$group);
  578: 	if (@getgroup) {
  579: 	    my $exclude = 1;
  580: 	    foreach my $grp (@getgroup) {
  581: 	        foreach my $stu_group (@stu_groups) {
  582: 	            if ($stu_group eq $grp) {
  583: 	                $exclude = 0;
  584:     	            } 
  585: 	        }
  586:     	        if (($grp eq 'none') && !$group) {
  587:         	        $exclude = 0;
  588:         	}
  589: 	    }
  590: 	    if ($exclude) {
  591: 	        delete($classlist->{$student});
  592: 	    }
  593: 	}
  594: 	$section = ($section ne '' ? $section : 'none');
  595: 	if (&canview($section)) {
  596: 	    if (!@getsec || grep(/^\Q$section\E$/,@getsec)) {
  597: 		$sections{$section}++;
  598: 		if ($classlist->{$student}) {
  599: 		    $fullnames{$student}=$fullname;
  600: 		}
  601: 	    } else {
  602: 		delete($classlist->{$student});
  603: 	    }
  604: 	} else {
  605: 	    delete($classlist->{$student});
  606: 	}
  607:     }
  608:     my %seen = ();
  609:     my @sections = sort(keys(%sections));
  610:     return ($classlist,\@sections,\%fullnames);
  611: }
  612: 
  613: sub canmodify {
  614:     my ($sec)=@_;
  615:     if ($perm{'mgr'}) {
  616: 	if (!defined($perm{'mgr_section'})) {
  617: 	    # can modify whole class
  618: 	    return 1;
  619: 	} else {
  620: 	    if ($sec eq $perm{'mgr_section'}) {
  621: 		#can modify the requested section
  622: 		return 1;
  623: 	    } else {
  624: 		# can't modify the request section
  625: 		return 0;
  626: 	    }
  627: 	}
  628:     }
  629:     #can't modify
  630:     return 0;
  631: }
  632: 
  633: sub canview {
  634:     my ($sec)=@_;
  635:     if ($perm{'vgr'}) {
  636: 	if (!defined($perm{'vgr_section'})) {
  637: 	    # can modify whole class
  638: 	    return 1;
  639: 	} else {
  640: 	    if ($sec eq $perm{'vgr_section'}) {
  641: 		#can modify the requested section
  642: 		return 1;
  643: 	    } else {
  644: 		# can't modify the request section
  645: 		return 0;
  646: 	    }
  647: 	}
  648:     }
  649:     #can't modify
  650:     return 0;
  651: }
  652: 
  653: #--- Retrieve the grade status of a student for all the parts
  654: sub student_gradeStatus {
  655:     my ($symb,$udom,$uname,$partlist) = @_;
  656:     my %record     = &Apache::lonnet::restore($symb,$env{'request.course.id'},$udom,$uname);
  657:     my %partstatus = ();
  658:     foreach (@$partlist) {
  659: 	my ($status,undef)   = split(/_/,$record{"resource.$_.solved"},2);
  660: 	$status              = 'nothing' if ($status eq '');
  661: 	$partstatus{$_}      = $status;
  662: 	my $subkey           = "resource.$_.submitted_by";
  663: 	$partstatus{$subkey} = $record{$subkey} if ($record{$subkey} ne '');
  664:     }
  665:     return %partstatus;
  666: }
  667: 
  668: # hidden form and javascript that calls the form
  669: # Use by verifyscript and viewgrades
  670: # Shows a student's view of problem and submission
  671: sub jscriptNform {
  672:     my ($symb) = @_;
  673:     my $stu_status = join(':',&Apache::loncommon::get_env_multiple('form.Status'));
  674:     my $jscript='<script type="text/javascript" language="javascript">'."\n".
  675: 	'    function viewOneStudent(user,domain) {'."\n".
  676: 	'	document.onestudent.student.value = user;'."\n".
  677: 	'	document.onestudent.userdom.value = domain;'."\n".
  678: 	'	document.onestudent.submit();'."\n".
  679: 	'    }'."\n".
  680: 	'</script>'."\n";
  681:     $jscript.= '<form action="/adm/grades" method="post" name="onestudent">'."\n".
  682: 	'<input type="hidden" name="symb"    value="'.&Apache::lonenc::check_encrypt($symb).'" />'."\n".
  683: 	'<input type="hidden" name="saveState" value="'.$env{'form.saveState'}.'" />'."\n".
  684: 	'<input type="hidden" name="probTitle" value="'.$env{'form.probTitle'}.'" />'."\n".
  685: 	'<input type="hidden" name="Status"  value="'.$stu_status.'" />'."\n".
  686: 	'<input type="hidden" name="command" value="submission" />'."\n".
  687: 	'<input type="hidden" name="student" value="" />'."\n".
  688: 	'<input type="hidden" name="userdom" value="" />'."\n".
  689: 	'</form>'."\n";
  690:     return $jscript;
  691: }
  692: 
  693: 
  694: 
  695: # Given the score (as a number [0-1] and the weight) what is the final
  696: # point value? This function will round to the nearest tenth, third,
  697: # or quarter if one of those is within the tolerance of .00001.
  698: sub compute_points {
  699:     my ($score, $weight) = @_;
  700:     
  701:     my $tolerance = .00001;
  702:     my $points = $score * $weight;
  703: 
  704:     # Check for nearness to 1/x.
  705:     my $check_for_nearness = sub {
  706:         my ($factor) = @_;
  707:         my $num = ($points * $factor) + $tolerance;
  708:         my $floored_num = floor($num);
  709:         if ($num - $floored_num < 2 * $tolerance * $factor) {
  710:             return $floored_num / $factor;
  711:         }
  712:         return $points;
  713:     };
  714: 
  715:     $points = $check_for_nearness->(10);
  716:     $points = $check_for_nearness->(3);
  717:     $points = $check_for_nearness->(4);
  718:     
  719:     return $points;
  720: }
  721: 
  722: #------------------ End of general use routines --------------------
  723: 
  724: #
  725: # Find most similar essay
  726: #
  727: 
  728: sub most_similar {
  729:     my ($uname,$udom,$uessay,$old_essays)=@_;
  730: 
  731: # ignore spaces and punctuation
  732: 
  733:     $uessay=~s/\W+/ /gs;
  734: 
  735: # ignore empty submissions (occuring when only files are sent)
  736: 
  737:     unless ($uessay=~/\w+/) { return ''; }
  738: 
  739: # these will be returned. Do not care if not at least 50 percent similar
  740:     my $limit=0.6;
  741:     my $sname='';
  742:     my $sdom='';
  743:     my $scrsid='';
  744:     my $sessay='';
  745: # go through all essays ...
  746:     foreach my $tkey (keys(%$old_essays)) {
  747: 	my ($tname,$tdom,$tcrsid)=map {&unescape($_)} (split(/\./,$tkey));
  748: # ... except the same student
  749:         next if (($tname eq $uname) && ($tdom eq $udom));
  750: 	my $tessay=$old_essays->{$tkey};
  751: 	$tessay=~s/\W+/ /gs;
  752: # String similarity gives up if not even limit
  753: 	my $tsimilar=&String::Similarity::similarity($uessay,$tessay,$limit);
  754: # Found one
  755: 	if ($tsimilar>$limit) {
  756: 	    $limit=$tsimilar;
  757: 	    $sname=$tname;
  758: 	    $sdom=$tdom;
  759: 	    $scrsid=$tcrsid;
  760: 	    $sessay=$old_essays->{$tkey};
  761: 	}
  762:     }
  763:     if ($limit>0.6) {
  764:        return ($sname,$sdom,$scrsid,$sessay,$limit);
  765:     } else {
  766:        return ('','','','',0);
  767:     }
  768: }
  769: 
  770: #-------------------------------------------------------------------
  771: 
  772: #------------------------------------ Receipt Verification Routines
  773: #
  774: #--- Check whether a receipt number is valid.---
  775: sub verifyreceipt {
  776:     my $request  = shift;
  777: 
  778:     my $courseid = $env{'request.course.id'};
  779:     my $receipt  = &Apache::lonnet::recprefix($courseid).'-'.
  780: 	$env{'form.receipt'};
  781:     $receipt     =~ s/[^\-\d]//g;
  782:     my ($symb)   = &get_symb($request);
  783: 
  784:     my $title.=
  785: 	'<h3><span class="LC_info">'.
  786: 	&mt('Verifying Receipt No. [_1]',$receipt).
  787: 	'</span></h3>'."\n".
  788: 	'<h4>'.&mt('<b>Resource: </b>[_1]',$env{'form.probTitle'}).
  789: 	'</h4>'."\n";
  790: 
  791:     my ($string,$contents,$matches) = ('','',0);
  792:     my (undef,undef,$fullname) = &getclasslist('all','0');
  793:     
  794:     my $receiptparts=0;
  795:     if ($env{"course.$courseid.receiptalg"} eq 'receipt2' ||
  796: 	$env{"course.$courseid.receiptalg"} eq 'receipt3') { $receiptparts=1; }
  797:     my $parts=['0'];
  798:     if ($receiptparts) {
  799:         my $res_error; 
  800:         ($parts)=&response_type($symb,\$res_error);
  801:         if ($res_error) {
  802:             return &navmap_errormsg();
  803:         } 
  804:     }
  805:     
  806:     my $header = 
  807: 	&Apache::loncommon::start_data_table().
  808: 	&Apache::loncommon::start_data_table_header_row().
  809: 	'<th>&nbsp;'.&mt('Fullname').'&nbsp;</th>'."\n".
  810: 	'<th>&nbsp;'.&mt('Username').'&nbsp;</th>'."\n".
  811: 	'<th>&nbsp;'.&mt('Domain').'&nbsp;</th>';
  812:     if ($receiptparts) {
  813: 	$header.='<th>&nbsp;'.&mt('Problem Part').'&nbsp;</th>';
  814:     }
  815:     $header.=
  816: 	&Apache::loncommon::end_data_table_header_row();
  817: 
  818:     foreach (sort 
  819: 	     {
  820: 		 if (lc($$fullname{$a}) ne lc($$fullname{$b})) {
  821: 		     return (lc($$fullname{$a}) cmp lc($$fullname{$b}));
  822: 		 }
  823: 		 return $a cmp $b;
  824: 	     } (keys(%$fullname))) {
  825: 	my ($uname,$udom)=split(/\:/);
  826: 	foreach my $part (@$parts) {
  827: 	    if ($receipt eq &Apache::lonnet::ireceipt($uname,$udom,$courseid,$symb,$part)) {
  828: 		$contents.=
  829: 		    &Apache::loncommon::start_data_table_row().
  830: 		    '<td>&nbsp;'."\n".
  831: 		    '<a href="javascript:viewOneStudent(\''.$uname.'\',\''.$udom.
  832: 		    '\');" target="_self">'.$$fullname{$_}.'</a>&nbsp;</td>'."\n".
  833: 		    '<td>&nbsp;'.$uname.'&nbsp;</td>'.
  834: 		    '<td>&nbsp;'.$udom.'&nbsp;</td>';
  835: 		if ($receiptparts) {
  836: 		    $contents.='<td>&nbsp;'.$part.'&nbsp;</td>';
  837: 		}
  838: 		$contents.= 
  839: 		    &Apache::loncommon::end_data_table_row()."\n";
  840: 		
  841: 		$matches++;
  842: 	    }
  843: 	}
  844:     }
  845:     if ($matches == 0) {
  846:         $string = $title
  847:                  .'<p class="LC_warning">'
  848:                  .&mt('No match found for the above receipt number.')
  849:                  .'</p>';
  850:     } else {
  851: 	$string = &jscriptNform($symb).$title.
  852: 	    '<p>'.
  853: 	    &mt('The above receipt number matches the following [quant,_1,student].',$matches).
  854: 	    '</p>'.
  855: 	    $header.
  856: 	    $contents.
  857: 	    &Apache::loncommon::end_data_table()."\n";
  858:     }
  859:     return $string.&show_grading_menu_form($symb);
  860: }
  861: 
  862: #--- This is called by a number of programs.
  863: #--- Called from the Grading Menu - View/Grade an individual student
  864: #--- Also called directly when one clicks on the subm button 
  865: #    on the problem page.
  866: sub listStudents {
  867:     my ($request) = shift;
  868: 
  869:     my ($symb) = &get_symb($request);
  870:     my $cdom      = $env{"course.$env{'request.course.id'}.domain"};
  871:     my $cnum      = $env{"course.$env{'request.course.id'}.num"};
  872:     my $getsec    = $env{'form.section'} eq '' ? 'all' : $env{'form.section'};
  873:     my $getgroup  = $env{'form.group'} eq '' ? 'all' : $env{'form.group'};
  874:     my $submitonly= $env{'form.submitonly'} eq '' ? 'all' : $env{'form.submitonly'};
  875:     my $viewgrade = $env{'form.showgrading'} eq 'yes' ? 'View/Grade/Regrade' : 'View';
  876:     $env{'form.probTitle'} = $env{'form.probTitle'} eq '' ? 
  877: 	&Apache::lonnet::gettitle($symb) : $env{'form.probTitle'};
  878: 
  879:     my $result='<h3><span class="LC_info">&nbsp;'
  880: 	.&mt("$viewgrade Submissions for a Student or a Group of Students")
  881: 	.'</span></h3>';
  882: 
  883:     my ($table,undef,$hdgrade,$partlist,$handgrade) = &showResourceInfo($symb,$env{'form.probTitle'},($env{'form.showgrading'} eq 'yes'));
  884: 
  885:     my %lt = &Apache::lonlocal::texthash (
  886: 		'multiple' => 'Please select a student or group of students before clicking on the Next button.',
  887: 		'single'   => 'Please select the student before clicking on the Next button.',
  888: 	     );
  889:     $request->print(<<LISTJAVASCRIPT);
  890: <script type="text/javascript" language="javascript">
  891:     function checkSelect(checkBox) {
  892: 	var ctr=0;
  893: 	var sense="";
  894: 	if (checkBox.length > 1) {
  895: 	    for (var i=0; i<checkBox.length; i++) {
  896: 		if (checkBox[i].checked) {
  897: 		    ctr++;
  898: 		}
  899: 	    }
  900: 	    sense = '$lt{'multiple'}';
  901: 	} else {
  902: 	    if (checkBox.checked) {
  903: 		ctr = 1;
  904: 	    }
  905: 	    sense = '$lt{'single'}';
  906: 	}
  907: 	if (ctr == 0) {
  908: 	    alert(sense);
  909: 	    return false;
  910: 	}
  911: 	document.gradesub.submit();
  912:     }
  913: 
  914:     function reLoadList(formname) {
  915: 	if (formname.saveStatusOld.value == pullDownSelection(formname.Status)) {return;}
  916: 	formname.command.value = 'submission';
  917: 	formname.submit();
  918:     }
  919: </script>
  920: LISTJAVASCRIPT
  921: 
  922:     &commonJSfunctions($request);
  923:     $request->print($result);
  924: 
  925:     my $checkhdgrade = ($env{'form.handgrade'} eq 'yes' && scalar(@$partlist) > 1 ) ? 'checked="checked"' : '';
  926:     my $checklastsub = $checkhdgrade eq '' ? 'checked="checked"' : '';
  927:     my $gradeTable='<form action="/adm/grades" method="post" name="gradesub">'.
  928: 	"\n".$table;
  929: 	
  930:     $gradeTable .= &Apache::lonhtmlcommon::start_pick_box();
  931:     $gradeTable .= &Apache::lonhtmlcommon::row_title(&mt('View Problem Text'))
  932:                   .'<label><input type="radio" name="vProb" value="no" checked="checked" /> '.&mt('no').' </label>'."\n"
  933:                   .'<label><input type="radio" name="vProb" value="yes" /> '.&mt('one student').' </label>'."\n"
  934:                   .'<label><input type="radio" name="vProb" value="all" /> '.&mt('all students').' </label><br />'."\n"
  935:                   .&Apache::lonhtmlcommon::row_closure();
  936:     $gradeTable .= &Apache::lonhtmlcommon::row_title(&mt('View Answer'))
  937:                   .'<label><input type="radio" name="vAns" value="no"  /> '.&mt('no').' </label>'."\n"
  938:                   .'<label><input type="radio" name="vAns" value="yes" /> '.&mt('one student').' </label>'."\n"
  939:                   .'<label><input type="radio" name="vAns" value="all" checked="checked" /> '.&mt('all students').' </label><br />'."\n"
  940:                   .&Apache::lonhtmlcommon::row_closure();
  941: 
  942:     my $submission_options;
  943:     if ($env{'form.handgrade'} eq 'yes' && scalar(@$partlist) > 1) {
  944: 	$submission_options.=
  945: 	    '<label><input type="radio" name="lastSub" value="hdgrade" '.$checkhdgrade.' /> '.&mt('essay part only').' </label>'."\n";
  946:     }
  947:     my $stu_status = join(':',&Apache::loncommon::get_env_multiple('form.Status'));
  948:     my $saveStatus = $stu_status eq '' ? 'Active' : $stu_status;
  949:     $env{'form.Status'} = $saveStatus;
  950:     $submission_options.=
  951:         '<span class="LC_nobreak">'.
  952:         '<label><input type="radio" name="lastSub" value="lastonly" '.$checklastsub.' /> '.
  953:         &mt('last submission only').' </label></span>'."\n".
  954:         '<span class="LC_nobreak">'.
  955:         '<label><input type="radio" name="lastSub" value="last" /> '.
  956:         &mt('last submission &amp; parts info').' </label></span>'."\n".
  957:         '<span class="LC_nobreak">'.
  958:         '<label><input type="radio" name="lastSub" value="datesub" /> '.
  959:         &mt('by dates and submissions').'</label></span>'."\n".
  960:         '<span class="LC_nobreak">'.
  961:         '<label><input type="radio" name="lastSub" value="all" /> '.
  962:         &mt('all details').'</label></span>';
  963:     $gradeTable .= &Apache::lonhtmlcommon::row_title(&mt('Submissions'))
  964:                   .$submission_options
  965:                   .&Apache::lonhtmlcommon::row_closure();
  966: 
  967:     $gradeTable .= &Apache::lonhtmlcommon::row_title(&mt('Grading Increments'))
  968:                   .'<select name="increment">'
  969:                   .'<option value="1">'.&mt('Whole Points').'</option>'
  970:                   .'<option value=".5">'.&mt('Half Points').'</option>'
  971:                   .'<option value=".25">'.&mt('Quarter Points').'</option>'
  972:                   .'<option value=".1">'.&mt('Tenths of a Point').'</option>'
  973:                   .'</select>'
  974:                   .&Apache::lonhtmlcommon::row_closure();
  975: 
  976:     $gradeTable .= 
  977:         &build_section_inputs().
  978: 	'<input type="hidden" name="submitonly"  value="'.$submitonly.'" />'."\n".
  979: 	'<input type="hidden" name="handgrade"   value="'.$env{'form.handgrade'}.'" /><br />'."\n".
  980: 	'<input type="hidden" name="showgrading" value="'.$env{'form.showgrading'}.'" /><br />'."\n".
  981: 	'<input type="hidden" name="saveState"   value="'.$env{'form.saveState'}.'" />'."\n".
  982: 	'<input type="hidden" name="probTitle"   value="'.$env{'form.probTitle'}.'" />'."\n".
  983: 	'<input type="hidden" name="symb" value="'.&Apache::lonenc::check_encrypt($symb).'" />'."\n".
  984: 	'<input type="hidden" name="saveStatusOld" value="'.$saveStatus.'" />'."\n";
  985: 
  986:     if (exists($env{'form.gradingMenu'}) && exists($env{'form.Status'})) {
  987: 	$gradeTable .= '<input type="hidden" name="Status" value="'.$stu_status.'" />'."\n";
  988:     } else {
  989:         $gradeTable .= &Apache::lonhtmlcommon::row_title(&mt('Student Status'))
  990:                       .&Apache::lonhtmlcommon::StatusOptions(
  991:                            $saveStatus,undef,1,'javascript:reLoadList(this.form);')
  992:                       .&Apache::lonhtmlcommon::row_closure();
  993:     }
  994: 
  995:     $gradeTable .= &Apache::lonhtmlcommon::row_title(&mt('Check For Plagiarism'))
  996:                   .'<input type="checkbox" name="checkPlag" checked="checked" />'
  997:                   .&Apache::lonhtmlcommon::row_closure(1)
  998:                   .&Apache::lonhtmlcommon::end_pick_box();
  999: 
 1000:     $gradeTable .= '<p>'
 1001:                   .&mt('To '.lc($viewgrade)." a submission or a group of submissions, click on the check box(es) next to the student's name(s). Then click on the Next button.")."\n"
 1002:                   .'<input type="hidden" name="command" value="processGroup" />'
 1003:                   .'</p>';
 1004: 
 1005: # checkall buttons
 1006:     $gradeTable.=&check_script('gradesub', 'stuinfo');
 1007:     $gradeTable.='<input type="button" '."\n".
 1008:         'onclick="javascript:checkSelect(this.form.stuinfo);" '."\n".
 1009:         'value="'.&mt('Next').' &rarr;" /> <br />'."\n";
 1010:     $gradeTable.=&check_buttons();
 1011:     my ($classlist, undef, $fullname) = &getclasslist($getsec,'1',$getgroup);
 1012:     $gradeTable.= &Apache::loncommon::start_data_table().
 1013: 	&Apache::loncommon::start_data_table_header_row();
 1014:     my $loop = 0;
 1015:     while ($loop < 2) {
 1016: 	$gradeTable.='<th>'.&mt('No.').'</th><th>'.&mt('Select').'</th>'.
 1017: 	    '<th>'.&nameUserString('header').'&nbsp;'.&mt('Section/Group').'</th>';
 1018: 	if ($env{'form.showgrading'} eq 'yes' 
 1019: 	    && $submitonly ne 'queued'
 1020: 	    && $submitonly ne 'all') {
 1021: 	    foreach my $part (sort(@$partlist)) {
 1022: 		my $display_part=
 1023: 		    &get_display_part((split(/_/,$part))[0],$symb);
 1024: 		$gradeTable.=
 1025: 		    '<th>'.&mt('Part: [_1] Status',$display_part).'</th>';
 1026: 	    }
 1027: 	} elsif ($submitonly eq 'queued') {
 1028: 	    $gradeTable.='<th>'.&mt('Queue Status').'&nbsp;</th>';
 1029: 	}
 1030: 	$loop++;
 1031: #	$gradeTable.='<td></td>' if ($loop%2 ==1);
 1032:     }
 1033:     $gradeTable.=&Apache::loncommon::end_data_table_header_row()."\n";
 1034: 
 1035:     my $ctr = 0;
 1036:     foreach my $student (sort 
 1037: 			 {
 1038: 			     if (lc($$fullname{$a}) ne lc($$fullname{$b})) {
 1039: 				 return (lc($$fullname{$a}) cmp lc($$fullname{$b}));
 1040: 			     }
 1041: 			     return $a cmp $b;
 1042: 			 }
 1043: 			 (keys(%$fullname))) {
 1044: 	my ($uname,$udom) = split(/:/,$student);
 1045: 
 1046: 	my %status = ();
 1047: 
 1048: 	if ($submitonly eq 'queued') {
 1049: 	    my %queue_status = 
 1050: 		&Apache::bridgetask::get_student_status($symb,$cdom,$cnum,
 1051: 							$udom,$uname);
 1052: 	    next if (!defined($queue_status{'gradingqueue'}));
 1053: 	    $status{'gradingqueue'} = $queue_status{'gradingqueue'};
 1054: 	}
 1055: 
 1056: 	if ($env{'form.showgrading'} eq 'yes' 
 1057: 	    && $submitonly ne 'queued'
 1058: 	    && $submitonly ne 'all') {
 1059: 	    (%status) =&student_gradeStatus($symb,$udom,$uname,$partlist);
 1060: 	    my $submitted = 0;
 1061: 	    my $graded = 0;
 1062: 	    my $incorrect = 0;
 1063: 	    foreach (keys(%status)) {
 1064: 		$submitted = 1 if ($status{$_} ne 'nothing');
 1065: 		$graded = 1 if ($status{$_} =~ /^ungraded/);
 1066: 		$incorrect = 1 if ($status{$_} =~ /^incorrect/);
 1067: 		
 1068: 		my ($foo,$partid,$foo1) = split(/\./,$_);
 1069: 		if ($status{'resource.'.$partid.'.submitted_by'} ne '') {
 1070: 		    $submitted = 0;
 1071: 		    my ($part)=split(/\./,$partid);
 1072: 		    $gradeTable.='<input type="hidden" name="'.
 1073: 			$student.':'.$part.':submitted_by" value="'.
 1074: 			$status{'resource.'.$partid.'.submitted_by'}.'" />';
 1075: 		}
 1076: 	    }
 1077: 	    
 1078: 	    next if (!$submitted && ($submitonly eq 'yes' ||
 1079: 				     $submitonly eq 'incorrect' ||
 1080: 				     $submitonly eq 'graded'));
 1081: 	    next if (!$graded && ($submitonly eq 'graded'));
 1082: 	    next if (!$incorrect && $submitonly eq 'incorrect');
 1083: 	}
 1084: 
 1085: 	$ctr++;
 1086: 	my $section = $classlist->{$student}->[&Apache::loncoursedata::CL_SECTION()];
 1087:         my $group = $classlist->{$student}->[&Apache::loncoursedata::CL_GROUP()];
 1088: 	if ( $perm{'vgr'} eq 'F' ) {
 1089: 	    if ($ctr%2 ==1) {
 1090: 		$gradeTable.= &Apache::loncommon::start_data_table_row();
 1091: 	    }
 1092: 	    $gradeTable.='<td align="right">'.$ctr.'&nbsp;</td>'.
 1093:                '<td align="center"><label><input type="checkbox" name="stuinfo" value="'.
 1094:                $student.':'.$$fullname{$student}.':::SECTION'.$section.
 1095: 	       ')&nbsp;" />&nbsp;&nbsp;</label></td>'."\n".'<td>'.
 1096: 	       &nameUserString(undef,$$fullname{$student},$uname,$udom).
 1097: 	       '&nbsp;'.$section.($group ne '' ?'/'.$group:'').'</td>'."\n";
 1098: 
 1099: 	    if ($env{'form.showgrading'} eq 'yes' && $submitonly ne 'all') {
 1100: 		foreach (sort(keys(%status))) {
 1101: 		    next if ($_ =~ /^resource.*?submitted_by$/);
 1102: 		    $gradeTable.='<td align="center">&nbsp;'.&mt($status{$_}).'&nbsp;</td>'."\n";
 1103: 		}
 1104: 	    }
 1105: #	    $gradeTable.='<td></td>' if ($ctr%2 ==1);
 1106: 	    if ($ctr%2 ==0) {
 1107: 		$gradeTable.=&Apache::loncommon::end_data_table_row()."\n";
 1108: 	    }
 1109: 	}
 1110:     }
 1111:     if ($ctr%2 ==1) {
 1112: 	$gradeTable.='<td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td>';
 1113: 	    if ($env{'form.showgrading'} eq 'yes' 
 1114: 		&& $submitonly ne 'queued'
 1115: 		&& $submitonly ne 'all') {
 1116: 		foreach (@$partlist) {
 1117: 		    $gradeTable.='<td>&nbsp;</td>';
 1118: 		}
 1119: 	    } elsif ($submitonly eq 'queued') {
 1120: 		$gradeTable.='<td>&nbsp;</td>';
 1121: 	    }
 1122: 	$gradeTable.=&Apache::loncommon::end_data_table_row();
 1123:     }
 1124: 
 1125:     $gradeTable.=&Apache::loncommon::end_data_table()."\n".
 1126:         '<input type="button" '.
 1127:         'onclick="javascript:checkSelect(this.form.stuinfo);" '.
 1128:         'value="'.&mt('Next').' &rarr;" /></form>'."\n";
 1129:     if ($ctr == 0) {
 1130: 	my $num_students=(scalar(keys(%$fullname)));
 1131: 	if ($num_students eq 0) {
 1132: 	    $gradeTable='<br />&nbsp;<span class="LC_warning">'.&mt('There are no students currently enrolled.').'</span>';
 1133: 	} else {
 1134: 	    my $submissions='submissions';
 1135: 	    if ($submitonly eq 'incorrect') { $submissions = 'incorrect submissions'; }
 1136: 	    if ($submitonly eq 'graded'   ) { $submissions = 'ungraded submissions'; }
 1137: 	    if ($submitonly eq 'queued'   ) { $submissions = 'queued submissions'; }
 1138: 	    $gradeTable='<br />&nbsp;<span class="LC_warning">'.
 1139: 		&mt('No '.$submissions.' found for this resource for any students. ([_1] students checked for '.$submissions.')',
 1140: 		    $num_students).
 1141: 		'</span><br />';
 1142: 	}
 1143:     } elsif ($ctr == 1) {
 1144: 	$gradeTable =~ s/type="checkbox"/type="checkbox" checked="checked"/;
 1145:     }
 1146:     $gradeTable.=&show_grading_menu_form($symb);
 1147:     $request->print($gradeTable);
 1148:     return '';
 1149: }
 1150: 
 1151: #---- Called from the listStudents routine
 1152: 
 1153: sub check_script {
 1154:     my ($form, $type)=@_;
 1155:     my $chkallscript='<script type="text/javascript">
 1156:     function checkall() {
 1157:         for (i=0; i<document.forms.'.$form.'.elements.length; i++) {
 1158:             ele = document.forms.'.$form.'.elements[i];
 1159:             if (ele.name == "'.$type.'") {
 1160:             document.forms.'.$form.'.elements[i].checked=true;
 1161:                                        }
 1162:         }
 1163:     }
 1164: 
 1165:     function checksec() {
 1166:         for (i=0; i<document.forms.'.$form.'.elements.length; i++) {
 1167:             ele = document.forms.'.$form.'.elements[i];
 1168:            string = document.forms.'.$form.'.chksec.value;
 1169:            if
 1170:           (ele.value.indexOf(":::SECTION"+string)>0) {
 1171:               document.forms.'.$form.'.elements[i].checked=true;
 1172:             }
 1173:         }
 1174:     }
 1175: 
 1176: 
 1177:     function uncheckall() {
 1178:         for (i=0; i<document.forms.'.$form.'.elements.length; i++) {
 1179:             ele = document.forms.'.$form.'.elements[i];
 1180:             if (ele.name == "'.$type.'") {
 1181:             document.forms.'.$form.'.elements[i].checked=false;
 1182:                                        }
 1183:         }
 1184:     }
 1185: 
 1186: </script>'."\n";
 1187:     return $chkallscript;
 1188: }
 1189: 
 1190: sub check_buttons {
 1191:     my $buttons.='<input type="button" onclick="checkall()" value="'.&mt('Check All').'" />';
 1192:     $buttons.='<input type="button" onclick="uncheckall()" value="'.&mt('Uncheck All').'" />&nbsp;';
 1193:     $buttons.='<input type="button" onclick="checksec()" value="'.&mt('Check Section/Group').'" />';
 1194:     $buttons.='<input type="text" size="5" name="chksec" />&nbsp;';
 1195:     return $buttons;
 1196: }
 1197: 
 1198: #     Displays the submissions for one student or a group of students
 1199: sub processGroup {
 1200:     my ($request)  = shift;
 1201:     my $ctr        = 0;
 1202:     my @stuchecked = &Apache::loncommon::get_env_multiple('form.stuinfo');
 1203:     my $total      = scalar(@stuchecked)-1;
 1204: 
 1205:     foreach my $student (@stuchecked) {
 1206: 	my ($uname,$udom,$fullname) = split(/:/,$student);
 1207: 	$env{'form.student'}        = $uname;
 1208: 	$env{'form.userdom'}        = $udom;
 1209: 	$env{'form.fullname'}       = $fullname;
 1210: 	&submission($request,$ctr,$total);
 1211: 	$ctr++;
 1212:     }
 1213:     return '';
 1214: }
 1215: 
 1216: #------------------------------------------------------------------------------------
 1217: #
 1218: #-------------------------- Next few routines handles grading by student, essentially
 1219: #                           handles essay response type problem/part
 1220: #
 1221: #--- Javascript to handle the submission page functionality ---
 1222: sub sub_page_js {
 1223:     my $request = shift;
 1224: 	    my $alertmsg = &mt('A number equal or greater than 0 is expected. Entered value = ');
 1225:     $request->print(<<SUBJAVASCRIPT);
 1226: <script type="text/javascript" language="javascript">
 1227:     function updateRadio(formname,id,weight) {
 1228: 	var gradeBox = formname["GD_BOX"+id];
 1229: 	var radioButton = formname["RADVAL"+id];
 1230: 	var oldpts = formname["oldpts"+id].value;
 1231: 	var pts = checkSolved(formname,id) == 'update' ? gradeBox.value : oldpts;
 1232: 	gradeBox.value = pts;
 1233: 	var resetbox = false;
 1234: 	if (isNaN(pts) || pts < 0) {
 1235: 	    alert("$alertmsg"+pts);
 1236: 	    for (var i=0; i<radioButton.length; i++) {
 1237: 		if (radioButton[i].checked) {
 1238: 		    gradeBox.value = i;
 1239: 		    resetbox = true;
 1240: 		}
 1241: 	    }
 1242: 	    if (!resetbox) {
 1243: 		formtextbox.value = "";
 1244: 	    }
 1245: 	    return;
 1246: 	}
 1247: 
 1248: 	if (pts > weight) {
 1249: 	    var resp = confirm("You entered a value ("+pts+
 1250: 			       ") greater than the weight for the part. Accept?");
 1251: 	    if (resp == false) {
 1252: 		gradeBox.value = oldpts;
 1253: 		return;
 1254: 	    }
 1255: 	}
 1256: 
 1257: 	for (var i=0; i<radioButton.length; i++) {
 1258: 	    radioButton[i].checked=false;
 1259: 	    if (pts == i && pts != "") {
 1260: 		radioButton[i].checked=true;
 1261: 	    }
 1262: 	}
 1263: 	updateSelect(formname,id);
 1264: 	formname["stores"+id].value = "0";
 1265:     }
 1266: 
 1267:     function writeBox(formname,id,pts) {
 1268: 	var gradeBox = formname["GD_BOX"+id];
 1269: 	if (checkSolved(formname,id) == 'update') {
 1270: 	    gradeBox.value = pts;
 1271: 	} else {
 1272: 	    var oldpts = formname["oldpts"+id].value;
 1273: 	    gradeBox.value = oldpts;
 1274: 	    var radioButton = formname["RADVAL"+id];
 1275: 	    for (var i=0; i<radioButton.length; i++) {
 1276: 		radioButton[i].checked=false;
 1277: 		if (i == oldpts) {
 1278: 		    radioButton[i].checked=true;
 1279: 		}
 1280: 	    }
 1281: 	}
 1282: 	formname["stores"+id].value = "0";
 1283: 	updateSelect(formname,id);
 1284: 	return;
 1285:     }
 1286: 
 1287:     function clearRadBox(formname,id) {
 1288: 	if (checkSolved(formname,id) == 'noupdate') {
 1289: 	    updateSelect(formname,id);
 1290: 	    return;
 1291: 	}
 1292: 	gradeSelect = formname["GD_SEL"+id];
 1293: 	for (var i=0; i<gradeSelect.length; i++) {
 1294: 	    if (gradeSelect[i].selected) {
 1295: 		var selectx=i;
 1296: 	    }
 1297: 	}
 1298: 	var stores = formname["stores"+id];
 1299: 	if (selectx == stores.value) { return };
 1300: 	var gradeBox = formname["GD_BOX"+id];
 1301: 	gradeBox.value = "";
 1302: 	var radioButton = formname["RADVAL"+id];
 1303: 	for (var i=0; i<radioButton.length; i++) {
 1304: 	    radioButton[i].checked=false;
 1305: 	}
 1306: 	stores.value = selectx;
 1307:     }
 1308: 
 1309:     function checkSolved(formname,id) {
 1310: 	if (formname["solved"+id].value == "correct_by_student" && formname.overRideScore.value == 'no') {
 1311: 	    var reply = confirm("This problem has been graded correct by the computer. Do you want to change the score?");
 1312: 	    if (!reply) {return "noupdate";}
 1313: 	    formname.overRideScore.value = 'yes';
 1314: 	}
 1315: 	return "update";
 1316:     }
 1317: 
 1318:     function updateSelect(formname,id) {
 1319: 	formname["GD_SEL"+id][0].selected = true;
 1320: 	return;
 1321:     }
 1322: 
 1323: //=========== Check that a point is assigned for all the parts  ============
 1324:     function checksubmit(formname,val,total,parttot) {
 1325: 	formname.gradeOpt.value = val;
 1326: 	if (val == "Save & Next") {
 1327: 	    for (i=0;i<=total;i++) {
 1328: 		for (j=0;j<parttot;j++) {
 1329: 		    var partid = formname["partid"+i+"_"+j].value;
 1330: 		    if (formname["GD_SEL"+i+"_"+partid][0].selected) {
 1331: 			var points = formname["GD_BOX"+i+"_"+partid].value;
 1332: 			if (points == "") {
 1333: 			    var name = formname["name"+i].value;
 1334: 			    var studentID = (name != '' ? name : formname["unamedom"+i].value);
 1335: 			    var resp = confirm("You did not assign a score for "+studentID+
 1336: 					       ", part "+partid+". Continue?");
 1337: 			    if (resp == false) {
 1338: 				formname["GD_BOX"+i+"_"+partid].focus();
 1339: 				return false;
 1340: 			    }
 1341: 			}
 1342: 		    }
 1343: 		    
 1344: 		}
 1345: 	    }
 1346: 	    
 1347: 	}
 1348: 	if (val == "Grade Student") {
 1349: 	    formname.showgrading.value = "yes";
 1350: 	    if (formname.Status.value == "") {
 1351: 		formname.Status.value = "Active";
 1352: 	    }
 1353: 	    formname.studentNo.value = total;
 1354: 	}
 1355: 	formname.submit();
 1356:     }
 1357: 
 1358: //======= Check that a score is assigned for all the problems (page/sequence grading only) =========
 1359:     function checkSubmitPage(formname,total) {
 1360: 	noscore = new Array(100);
 1361: 	var ptr = 0;
 1362: 	for (i=1;i<total;i++) {
 1363: 	    var partid = formname["q_"+i].value;
 1364: 	    if (formname["GD_SEL"+i+"_"+partid][0].selected) {
 1365: 		var points = formname["GD_BOX"+i+"_"+partid].value;
 1366: 		var status = formname["solved"+i+"_"+partid].value;
 1367: 		if (points == "" && status != "correct_by_student") {
 1368: 		    noscore[ptr] = i;
 1369: 		    ptr++;
 1370: 		}
 1371: 	    }
 1372: 	}
 1373: 	if (ptr != 0) {
 1374: 	    var sense = ptr == 1 ? ": " : "s: ";
 1375: 	    var prolist = "";
 1376: 	    if (ptr == 1) {
 1377: 		prolist = noscore[0];
 1378: 	    } else {
 1379: 		var i = 0;
 1380: 		while (i < ptr-1) {
 1381: 		    prolist += noscore[i]+", ";
 1382: 		    i++;
 1383: 		}
 1384: 		prolist += "and "+noscore[i];
 1385: 	    }
 1386: 	    var resp = confirm("You did not assign any score for the following problem"+sense+prolist+". Continue?");
 1387: 	    if (resp == false) {
 1388: 		return false;
 1389: 	    }
 1390: 	}
 1391: 
 1392: 	formname.submit();
 1393:     }
 1394: </script>
 1395: SUBJAVASCRIPT
 1396: }
 1397: 
 1398: #--- javascript for essay type problem --
 1399: sub sub_page_kw_js {
 1400:     my $request = shift;
 1401:     my $iconpath = $request->dir_config('lonIconsURL');
 1402:     &commonJSfunctions($request);
 1403: 
 1404:     my $inner_js_msg_central=<<INNERJS;
 1405:     <script text="text/javascript">
 1406:     function checkInput() {
 1407:       opener.document.SCORE.msgsub.value = opener.checkEntities(document.msgcenter.msgsub.value);
 1408:       var nmsg   = opener.document.SCORE.savemsgN.value;
 1409:       var usrctr = document.msgcenter.usrctr.value;
 1410:       var newval = opener.document.SCORE["newmsg"+usrctr];
 1411:       newval.value = opener.checkEntities(document.msgcenter.newmsg.value);
 1412: 
 1413:       var msgchk = "";
 1414:       if (document.msgcenter.subchk.checked) {
 1415:          msgchk = "msgsub,";
 1416:       }
 1417:       var includemsg = 0;
 1418:       for (var i=1; i<=nmsg; i++) {
 1419:           var opnmsg = opener.document.SCORE["savemsg"+i];
 1420:           var frmmsg = document.msgcenter["msg"+i];
 1421:           opnmsg.value = opener.checkEntities(frmmsg.value);
 1422:           var showflg = opener.document.SCORE["shownOnce"+i];
 1423:           showflg.value = "1";
 1424:           var chkbox = document.msgcenter["msgn"+i];
 1425:           if (chkbox.checked) {
 1426:              msgchk += "savemsg"+i+",";
 1427:              includemsg = 1;
 1428:           }
 1429:       }
 1430:       if (document.msgcenter.newmsgchk.checked) {
 1431:          msgchk += "newmsg"+usrctr;
 1432:          includemsg = 1;
 1433:       }
 1434:       imgformname = opener.document.SCORE["mailicon"+usrctr];
 1435:       imgformname.src = "$iconpath/"+((includemsg) ? "mailto.gif" : "mailbkgrd.gif");
 1436:       var includemsg = opener.document.SCORE["includemsg"+usrctr];
 1437:       includemsg.value = msgchk;
 1438: 
 1439:       self.close()
 1440: 
 1441:     }
 1442:     </script>
 1443: INNERJS
 1444: 
 1445:     my $inner_js_highlight_central=<<INNERJS;
 1446:  <script type="text/javascript">
 1447:     function updateChoice(flag) {
 1448:       opener.document.SCORE.kwclr.value = opener.radioSelection(document.hlCenter.kwdclr);
 1449:       opener.document.SCORE.kwsize.value = opener.radioSelection(document.hlCenter.kwdsize);
 1450:       opener.document.SCORE.kwstyle.value = opener.radioSelection(document.hlCenter.kwdstyle);
 1451:       opener.document.SCORE.refresh.value = "on";
 1452:       if (opener.document.SCORE.keywords.value!=""){
 1453:          opener.document.SCORE.submit();
 1454:       }
 1455:       self.close()
 1456:     }
 1457: </script>
 1458: INNERJS
 1459: 
 1460:     my $start_page_msg_central = 
 1461:         &Apache::loncommon::start_page('Message Central',$inner_js_msg_central,
 1462: 				       {'js_ready'  => 1,
 1463: 					'only_body' => 1,
 1464: 					'bgcolor'   =>'#FFFFFF',});
 1465:     my $end_page_msg_central = 
 1466: 	&Apache::loncommon::end_page({'js_ready' => 1});
 1467: 
 1468: 
 1469:     my $start_page_highlight_central = 
 1470:         &Apache::loncommon::start_page('Highlight Central',
 1471: 				       $inner_js_highlight_central,
 1472: 				       {'js_ready'  => 1,
 1473: 					'only_body' => 1,
 1474: 					'bgcolor'   =>'#FFFFFF',});
 1475:     my $end_page_highlight_central = 
 1476: 	&Apache::loncommon::end_page({'js_ready' => 1});
 1477: 
 1478:     my $docopen=&Apache::lonhtmlcommon::javascript_docopen();
 1479:     $docopen=~s/^document\.//;
 1480:     my $alertmsg = &mt('Please select a word or group of words from document and then click this link.');
 1481:     $request->print(<<SUBJAVASCRIPT);
 1482: <script type="text/javascript" language="javascript">
 1483: 
 1484: //===================== Show list of keywords ====================
 1485:   function keywords(formname) {
 1486:     var nret = prompt("Keywords list, separated by a space. Add/delete to list if desired.",formname.keywords.value);
 1487:     if (nret==null) return;
 1488:     formname.keywords.value = nret;
 1489: 
 1490:     if (formname.keywords.value != "") {
 1491: 	formname.refresh.value = "on";
 1492: 	formname.submit();
 1493:     }
 1494:     return;
 1495:   }
 1496: 
 1497: //===================== Script to view submitted by ==================
 1498:   function viewSubmitter(submitter) {
 1499:     document.SCORE.refresh.value = "on";
 1500:     document.SCORE.NCT.value = "1";
 1501:     document.SCORE.unamedom0.value = submitter;
 1502:     document.SCORE.submit();
 1503:     return;
 1504:   }
 1505: 
 1506: //===================== Script to add keyword(s) ==================
 1507:   function getSel() {
 1508:     if (document.getSelection) txt = document.getSelection();
 1509:     else if (document.selection) txt = document.selection.createRange().text;
 1510:     else return;
 1511:     var cleantxt = txt.replace(new RegExp('([\\f\\n\\r\\t\\v ])+', 'g')," ");
 1512:     if (cleantxt=="") {
 1513: 	alert("$alertmsg");
 1514: 	return;
 1515:     }
 1516:     var nret = prompt("Add selection to keyword list? Edit if desired.",cleantxt);
 1517:     if (nret==null) return;
 1518:     document.SCORE.keywords.value = document.SCORE.keywords.value+" "+nret;
 1519:     if (document.SCORE.keywords.value != "") {
 1520: 	document.SCORE.refresh.value = "on";
 1521: 	document.SCORE.submit();
 1522:     }
 1523:     return;
 1524:   }
 1525: 
 1526: //====================== Script for composing message ==============
 1527:    // preload images
 1528:    img1 = new Image();
 1529:    img1.src = "$iconpath/mailbkgrd.gif";
 1530:    img2 = new Image();
 1531:    img2.src = "$iconpath/mailto.gif";
 1532: 
 1533:   function msgCenter(msgform,usrctr,fullname) {
 1534:     var Nmsg  = msgform.savemsgN.value;
 1535:     savedMsgHeader(Nmsg,usrctr,fullname);
 1536:     var subject = msgform.msgsub.value;
 1537:     var msgchk = document.SCORE["includemsg"+usrctr].value;
 1538:     re = /msgsub/;
 1539:     var shwsel = "";
 1540:     if (re.test(msgchk)) { shwsel = "checked" }
 1541:     subject = (document.SCORE.shownSub.value == 0 ? checkEntities(subject) : subject);
 1542:     displaySubject(checkEntities(subject),shwsel);
 1543:     for (var i=1; i<=Nmsg; i++) {
 1544: 	var testmsg = "savemsg"+i+",";
 1545: 	re = new RegExp(testmsg,"g");
 1546: 	shwsel = "";
 1547: 	if (re.test(msgchk)) { shwsel = "checked" }
 1548: 	var message = document.SCORE["savemsg"+i].value;
 1549: 	message = (document.SCORE["shownOnce"+i].value == 0 ? checkEntities(message) : message);
 1550: 	displaySavedMsg(i,message,shwsel); //I do not get it. w/o checkEntities on saved messages,
 1551: 	                                   //any &lt; is already converted to <, etc. However, only once!!
 1552:     }
 1553:     newmsg = document.SCORE["newmsg"+usrctr].value;
 1554:     shwsel = "";
 1555:     re = /newmsg/;
 1556:     if (re.test(msgchk)) { shwsel = "checked" }
 1557:     newMsg(newmsg,shwsel);
 1558:     msgTail(); 
 1559:     return;
 1560:   }
 1561: 
 1562:   function checkEntities(strx) {
 1563:     if (strx.length == 0) return strx;
 1564:     var orgStr = ["&", "<", ">", '"']; 
 1565:     var newStr = ["&amp;", "&lt;", "&gt;", "&quot;"];
 1566:     var counter = 0;
 1567:     while (counter < 4) {
 1568: 	strx = strReplace(strx,orgStr[counter],newStr[counter]);
 1569: 	counter++;
 1570:     }
 1571:     return strx;
 1572:   }
 1573: 
 1574:   function strReplace(strx, orgStr, newStr) {
 1575:     return strx.split(orgStr).join(newStr);
 1576:   }
 1577: 
 1578:   function savedMsgHeader(Nmsg,usrctr,fullname) {
 1579:     var height = 70*Nmsg+250;
 1580:     var scrollbar = "no";
 1581:     if (height > 600) {
 1582: 	height = 600;
 1583: 	scrollbar = "yes";
 1584:     }
 1585:     var xpos = (screen.width-600)/2;
 1586:     xpos = (xpos < 0) ? '0' : xpos;
 1587:     var ypos = (screen.height-height)/2-30;
 1588:     ypos = (ypos < 0) ? '0' : ypos;
 1589: 
 1590:     pWin = window.open('', 'MessageCenter', 'resizable=yes,toolbar=no,location=no,scrollbars='+scrollbar+',screenx='+xpos+',screeny='+ypos+',width=600,height='+height);
 1591:     pWin.focus();
 1592:     pDoc = pWin.document;
 1593:     pDoc.$docopen;
 1594:     pDoc.write('$start_page_msg_central');
 1595: 
 1596:     pDoc.write("<form action=\\"inactive\\" name=\\"msgcenter\\">");
 1597:     pDoc.write("<input value=\\""+usrctr+"\\" name=\\"usrctr\\" type=\\"hidden\\">");
 1598:     pDoc.write("<h3><span class=\\"LC_info\\">&nbsp;Compose Message for \"+fullname+\"<\\/span><\\/h3><br /><br />");
 1599: 
 1600:     pDoc.write('<table border="0" width="100%"><tr><td bgcolor="#777777">');
 1601:     pDoc.write('<table border="0" width="100%"><tr bgcolor="#DDFFFF">');
 1602:     pDoc.write("<td><b>Type<\\/b><\\/td><td><b>Include<\\/b><\\/td><td><b>Message<\\/td><\\/tr>");
 1603: }
 1604:     function displaySubject(msg,shwsel) {
 1605:     pDoc = pWin.document;
 1606:     pDoc.write("<tr bgcolor=\\"#ffffdd\\">");
 1607:     pDoc.write("<td>Subject<\\/td>");
 1608:     pDoc.write("<td align=\\"center\\"><input name=\\"subchk\\" type=\\"checkbox\\"" +shwsel+"><\\/td>");
 1609:     pDoc.write("<td><input name=\\"msgsub\\" type=\\"text\\" value=\\""+msg+"\\"size=\\"60\\" maxlength=\\"80\\"><\\/td><\\/tr>");
 1610: }
 1611: 
 1612:   function displaySavedMsg(ctr,msg,shwsel) {
 1613:     pDoc = pWin.document;
 1614:     pDoc.write("<tr bgcolor=\\"#ffffdd\\">");
 1615:     pDoc.write("<td align=\\"center\\">"+ctr+"<\\/td>");
 1616:     pDoc.write("<td align=\\"center\\"><input name=\\"msgn"+ctr+"\\" type=\\"checkbox\\"" +shwsel+"><\\/td>");
 1617:     pDoc.write("<td><textarea name=\\"msg"+ctr+"\\" cols=\\"60\\" rows=\\"3\\">"+msg+"<\\/textarea><\\/td><\\/tr>");
 1618: }
 1619: 
 1620:   function newMsg(newmsg,shwsel) {
 1621:     pDoc = pWin.document;
 1622:     pDoc.write("<tr bgcolor=\\"#ffffdd\\">");
 1623:     pDoc.write("<td align=\\"center\\">New<\\/td>");
 1624:     pDoc.write("<td align=\\"center\\"><input name=\\"newmsgchk\\" type=\\"checkbox\\"" +shwsel+"><\\/td>");
 1625:     pDoc.write("<td><textarea name=\\"newmsg\\" cols=\\"60\\" rows=\\"3\\" onchange=\\"javascript:this.form.newmsgchk.checked=true\\" >"+newmsg+"<\\/textarea><\\/td><\\/tr>");
 1626: }
 1627: 
 1628:   function msgTail() {
 1629:     pDoc = pWin.document;
 1630:     pDoc.write("<\\/table>");
 1631:     pDoc.write("<\\/td><\\/tr><\\/table>&nbsp;");
 1632:     pDoc.write("<input type=\\"button\\" value=\\"Save\\" onclick=\\"javascript:checkInput()\\">&nbsp;&nbsp;");
 1633:     pDoc.write("<input type=\\"button\\" value=\\"Cancel\\" onclick=\\"self.close()\\"><br /><br />");
 1634:     pDoc.write("<\\/form>");
 1635:     pDoc.write('$end_page_msg_central');
 1636:     pDoc.close();
 1637: }
 1638: 
 1639: //====================== Script for keyword highlight options ==============
 1640:   function kwhighlight() {
 1641:     var kwclr    = document.SCORE.kwclr.value;
 1642:     var kwsize   = document.SCORE.kwsize.value;
 1643:     var kwstyle  = document.SCORE.kwstyle.value;
 1644:     var redsel = "";
 1645:     var grnsel = "";
 1646:     var blusel = "";
 1647:     if (kwclr=="red")   {var redsel="checked"};
 1648:     if (kwclr=="green") {var grnsel="checked"};
 1649:     if (kwclr=="blue")  {var blusel="checked"};
 1650:     var sznsel = "";
 1651:     var sz1sel = "";
 1652:     var sz2sel = "";
 1653:     if (kwsize=="0")  {var sznsel="checked"};
 1654:     if (kwsize=="+1") {var sz1sel="checked"};
 1655:     if (kwsize=="+2") {var sz2sel="checked"};
 1656:     var synsel = "";
 1657:     var syisel = "";
 1658:     var sybsel = "";
 1659:     if (kwstyle=="")    {var synsel="checked"};
 1660:     if (kwstyle=="<i>") {var syisel="checked"};
 1661:     if (kwstyle=="<b>") {var sybsel="checked"};
 1662:     highlightCentral();
 1663:     highlightbody('red','red',redsel,'0','normal',sznsel,'','normal',synsel);
 1664:     highlightbody('green','green',grnsel,'+1','+1',sz1sel,'<i>','italic',syisel);
 1665:     highlightbody('blue','blue',blusel,'+2','+2',sz2sel,'<b>','bold',sybsel);
 1666:     highlightend();
 1667:     return;
 1668:   }
 1669: 
 1670:   function highlightCentral() {
 1671: //    if (window.hwdWin) window.hwdWin.close();
 1672:     var xpos = (screen.width-400)/2;
 1673:     xpos = (xpos < 0) ? '0' : xpos;
 1674:     var ypos = (screen.height-330)/2-30;
 1675:     ypos = (ypos < 0) ? '0' : ypos;
 1676: 
 1677:     hwdWin = window.open('', 'KeywordHighlightCentral', 'resizeable=yes,toolbar=no,location=no,scrollbars=no,width=400,height=300,screenx='+xpos+',screeny='+ypos);
 1678:     hwdWin.focus();
 1679:     var hDoc = hwdWin.document;
 1680:     hDoc.$docopen;
 1681:     hDoc.write('$start_page_highlight_central');
 1682:     hDoc.write("<form action=\\"inactive\\" name=\\"hlCenter\\">");
 1683:     hDoc.write("<h3><span class=\\"LC_info\\">&nbsp;Keyword Highlight Options<\\/span><\\/h3><br /><br />");
 1684: 
 1685:     hDoc.write('<table border="0" width="100%"><tr><td bgcolor="#777777">');
 1686:     hDoc.write('<table border="0" width="100%"><tr bgcolor="#DDFFFF">');
 1687:     hDoc.write("<td><b>Text Color<\\/b><\\/td><td><b>Font Size<\\/b><\\/td><td><b>Font Style<\\/td><\\/tr>");
 1688:   }
 1689: 
 1690:   function highlightbody(clrval,clrtxt,clrsel,szval,sztxt,szsel,syval,sytxt,sysel) { 
 1691:     var hDoc = hwdWin.document;
 1692:     hDoc.write("<tr bgcolor=\\"#ffffdd\\">");
 1693:     hDoc.write("<td align=\\"left\\">");
 1694:     hDoc.write("<input name=\\"kwdclr\\" type=\\"radio\\" value=\\""+clrval+"\\" "+clrsel+">&nbsp;"+clrtxt+"<\\/td>");
 1695:     hDoc.write("<td align=\\"left\\">");
 1696:     hDoc.write("<input name=\\"kwdsize\\" type=\\"radio\\" value=\\""+szval+"\\" "+szsel+">&nbsp;"+sztxt+"<\\/td>");
 1697:     hDoc.write("<td align=\\"left\\">");
 1698:     hDoc.write("<input name=\\"kwdstyle\\" type=\\"radio\\" value=\\""+syval+"\\" "+sysel+">&nbsp;"+sytxt+"<\\/td>");
 1699:     hDoc.write("<\\/tr>");
 1700:   }
 1701: 
 1702:   function highlightend() { 
 1703:     var hDoc = hwdWin.document;
 1704:     hDoc.write("<\\/table>");
 1705:     hDoc.write("<\\/td><\\/tr><\\/table>&nbsp;");
 1706:     hDoc.write("<input type=\\"button\\" value=\\"Save\\" onclick=\\"javascript:updateChoice(1)\\">&nbsp;&nbsp;");
 1707:     hDoc.write("<input type=\\"button\\" value=\\"Cancel\\" onclick=\\"self.close()\\"><br /><br />");
 1708:     hDoc.write("<\\/form>");
 1709:     hDoc.write('$end_page_highlight_central');
 1710:     hDoc.close();
 1711:   }
 1712: 
 1713: </script>
 1714: SUBJAVASCRIPT
 1715: }
 1716: 
 1717: sub get_increment {
 1718:     my $increment = $env{'form.increment'};
 1719:     if ($increment != 1 && $increment != .5 && $increment != .25 &&
 1720:         $increment != .1) {
 1721:         $increment = 1;
 1722:     }
 1723:     return $increment;
 1724: }
 1725: 
 1726: sub gradeBox_start {
 1727:     return (
 1728:         &Apache::loncommon::start_data_table()
 1729:        .&Apache::loncommon::start_data_table_header_row()
 1730:        .'<th>'.&mt('Part').'</th>'
 1731:        .'<th>'.&mt('Points').'</th>'
 1732:        .'<th>&nbsp;</th>'
 1733:        .'<th>'.&mt('Assign Grade').'</th>'
 1734:        .'<th>'.&mt('Weight').'</th>'
 1735:        .'<th>'.&mt('Grade Status').'</th>'
 1736:        .&Apache::loncommon::end_data_table_header_row()
 1737:     );
 1738: }
 1739: 
 1740: sub gradeBox_end {
 1741:     return (
 1742:         &Apache::loncommon::end_data_table()
 1743:     );
 1744: }
 1745: #--- displays the grading box, used in essay type problem and grading by page/sequence
 1746: sub gradeBox {
 1747:     my ($request,$symb,$uname,$udom,$counter,$partid,$record) = @_;
 1748:     my $checkIcon = '<img alt="'.&mt('Check Mark').
 1749: 	'" src="'.&Apache::loncommon::lonhttpdurl($request->dir_config('lonIconsURL').'/check.gif').'" height="16" border="0" />';
 1750:     my $wgt    = &Apache::lonnet::EXT('resource.'.$partid.'.weight',$symb,$udom,$uname);
 1751:     my $wgtmsg = ($wgt > 0) ? &mt('(problem weight)') 
 1752:                            : '<span class="LC_info">'.&mt('problem weight assigned by computer').'</span>';
 1753:     $wgt       = ($wgt > 0 ? $wgt : '1');
 1754:     my $score  = ($$record{'resource.'.$partid.'.awarded'} eq '' ?
 1755: 		  '' : &compute_points($$record{'resource.'.$partid.'.awarded'},$wgt));
 1756:     my $result='<input type="hidden" name="WGT'.$counter.'_'.$partid.'" value="'.$wgt.'" />'."\n";
 1757:     my $display_part= &get_display_part($partid,$symb);
 1758:     my %last_resets = &get_last_resets($symb,$env{'request.course.id'},
 1759: 				       [$partid]);
 1760:     my $aggtries = $$record{'resource.'.$partid.'.tries'};
 1761:     if ($last_resets{$partid}) {
 1762:         $aggtries = &get_num_tries($record,$last_resets{$partid},$partid);
 1763:     }
 1764:     $result.=&Apache::loncommon::start_data_table_row();
 1765:     my $ctr = 0;
 1766:     my $thisweight = 0;
 1767:     my $increment = &get_increment();
 1768: 
 1769:     my $radio.='<table border="0"><tr>'."\n";  # display radio buttons in a nice table 10 across
 1770:     while ($thisweight<=$wgt) {
 1771: 	$radio.= '<td><span class="LC_nobreak"><label><input type="radio" name="RADVAL'.$counter.'_'.$partid.'" '.
 1772:         'onclick="javascript:writeBox(this.form,\''.$counter.'_'.$partid.'\','.
 1773: 	    $thisweight.')" value="'.$thisweight.'" '.
 1774: 	    ($score eq $thisweight ? 'checked="checked"':'').' /> '.$thisweight."</label></span></td>\n";
 1775: 	$radio.=(($ctr+1)%10 == 0 ? '</tr><tr>' : '');
 1776:         $thisweight += $increment;
 1777: 	$ctr++;
 1778:     }
 1779:     $radio.='</tr></table>';
 1780: 
 1781:     my $line.='<input type="text" name="GD_BOX'.$counter.'_'.$partid.'"'.
 1782: 	($score ne ''? ' value = "'.$score.'"':'').' size="4" '.
 1783: 	'onchange="javascript:updateRadio(this.form,\''.$counter.'_'.$partid.'\','.
 1784: 	$wgt.')" /></td>'."\n";
 1785:     $line.='<td>/'.$wgt.' '.$wgtmsg.
 1786: 	($$record{'resource.'.$partid.'.solved'} eq 'correct_by_student' ? '&nbsp;'.$checkIcon : '').
 1787: 	' </td>'."\n";
 1788:     $line.='<td><select name="GD_SEL'.$counter.'_'.$partid.'" '.
 1789: 	'onchange="javascript:clearRadBox(this.form,\''.$counter.'_'.$partid.'\')" >'."\n";
 1790:     if ($$record{'resource.'.$partid.'.solved'} eq 'excused') {
 1791: 	$line.='<option></option>'.
 1792: 	    '<option value="excused" selected="selected">'.&mt('excused').'</option>';
 1793:     } else {
 1794: 	$line.='<option selected="selected"></option>'.
 1795: 	    '<option value="excused" >'.&mt('excused').'</option>';
 1796:     }
 1797:     $line.='<option value="reset status">'.&mt('reset status').'</option></select>'."\n";
 1798: 
 1799: 
 1800: 	#&mt('<td><b>Part:</b></td><td>[_1]</td><td><b>Points:</b></td><td>[_2]</td><td>or</td><td>[_3]</td>',$display_part,$radio,$line);
 1801:     $result .= 
 1802: 	    '<td>'.$display_part.'</td><td>'.$radio.'</td><td>'.&mt('or').'</td><td>'.$line.'</td>';
 1803:     $result.=&Apache::loncommon::end_data_table_row();
 1804:     $result.='<input type="hidden" name="stores'.$counter.'_'.$partid.'" value="" />'."\n".
 1805: 	'<input type="hidden" name="oldpts'.$counter.'_'.$partid.'" value="'.$score.'" />'."\n".
 1806: 	'<input type="hidden" name="solved'.$counter.'_'.$partid.'" value="'.
 1807: 	$$record{'resource.'.$partid.'.solved'}.'" />'."\n".
 1808:         '<input type="hidden" name="totaltries'.$counter.'_'.$partid.'" value="'.
 1809:         $$record{'resource.'.$partid.'.tries'}.'" />'."\n".
 1810:         '<input type="hidden" name="aggtries'.$counter.'_'.$partid.'" value="'.
 1811:         $aggtries.'" />'."\n";
 1812:     my $res_error;
 1813:     $result.=&handback_box($symb,$uname,$udom,$counter,$partid,$record,\$res_error);
 1814:     if ($res_error) {
 1815:         return &navmap_errormsg();
 1816:     }
 1817:     return $result;
 1818: }
 1819: 
 1820: sub handback_box {
 1821:     my ($symb,$uname,$udom,$counter,$partid,$record,$res_error) = @_;
 1822:     my ($partlist,$handgrade,$responseType) = &response_type($symb,$res_error);
 1823:     my (@respids);
 1824:      my @part_response_id = &flatten_responseType($responseType);
 1825:     foreach my $part_response_id (@part_response_id) {
 1826:     	my ($part,$resp) = @{ $part_response_id };
 1827:         if ($part eq $partid) {
 1828:             push(@respids,$resp);
 1829:         }
 1830:     }
 1831:     my $result;
 1832:     foreach my $respid (@respids) {
 1833: 	my $prefix = $counter.'_'.$partid.'_'.$respid.'_';
 1834: 	my $files=&get_submitted_files($udom,$uname,$partid,$respid,$record);
 1835: 	next if (!@$files);
 1836: 	my $file_counter = 1;
 1837: 	foreach my $file (@$files) {
 1838: 	    if ($file =~ /\/portfolio\//) {
 1839:     	        my ($file_path, $file_disp) = ($file =~ m|(.+/)(.+)$|);
 1840:     	        my ($name,$version,$ext) = &file_name_version_ext($file_disp);
 1841:     	        $file_disp = "$name.$ext";
 1842:     	        $file = $file_path.$file_disp;
 1843:     	        $result.=&mt('Return commented version of [_1] to student.',
 1844:     			 '<span class="LC_filename">'.$file_disp.'</span>');
 1845:     	        $result.='<input type="file"   name="'.$prefix.'returndoc'.$file_counter.'" />'."\n";
 1846:     	        $result.='<input type="hidden" name="'.$prefix.'origdoc'.$file_counter.'" value="'.$file.'" /><br />';
 1847:     	        $result.='('.&mt('File will be uploaded when you click on Save &amp; Next below.').')<br />';
 1848:     	        $file_counter++;
 1849: 	    }
 1850: 	}
 1851:     }
 1852:     return $result;    
 1853: }
 1854: 
 1855: sub show_problem {
 1856:     my ($request,$symb,$uname,$udom,$removeform,$viewon,$mode,$form) = @_;
 1857:     my $rendered;
 1858:     my %form = ((ref($form) eq 'HASH')? %{$form} : ());
 1859:     &Apache::lonxml::remember_problem_counter();
 1860:     if ($mode eq 'both' or $mode eq 'text') {
 1861: 	$rendered=&Apache::loncommon::get_student_view($symb,$uname,$udom,
 1862: 						       $env{'request.course.id'},
 1863: 						       undef,\%form);
 1864:     }
 1865:     if ($removeform) {
 1866: 	$rendered=~s|<form(.*?)>||g;
 1867: 	$rendered=~s|</form>||g;
 1868: 	$rendered=~s|(<input[^>]*name\s*=\s*"?)(\w+)("?)|$1would_have_been_$2$3|g;
 1869:     }
 1870:     my $companswer;
 1871:     if ($mode eq 'both' or $mode eq 'answer') {
 1872: 	&Apache::lonxml::restore_problem_counter();
 1873: 	$companswer=
 1874: 	    &Apache::loncommon::get_student_answers($symb,$uname,$udom,
 1875: 						    $env{'request.course.id'},
 1876: 						    %form);
 1877:     }
 1878:     if ($removeform) {
 1879: 	$companswer=~s|<form(.*?)>||g;
 1880: 	$companswer=~s|</form>||g;
 1881: 	$companswer=~s|name="submit"|name="would_have_been_submit"|g;
 1882:     }
 1883:     $rendered=
 1884:         '<div class="LC_Box">'
 1885:        .'<h3 class="LC_hcell">'.&mt('View of the problem').'</h3>'
 1886:        .$rendered
 1887:        .'</div>';
 1888:     $companswer=
 1889:         '<div class="LC_Box">'
 1890:        .'<h3 class="LC_hcell">'.&mt('Correct answer').'</h3>'
 1891:        .$companswer
 1892:        .'</div>';
 1893:     my $result;
 1894:     if ($mode eq 'both') {
 1895:         $result=$rendered.$companswer;
 1896:     } elsif ($mode eq 'text') {
 1897:         $result=$rendered;
 1898:     } elsif ($mode eq 'answer') {
 1899:         $result=$companswer;
 1900:     }
 1901:     return $result;
 1902: }
 1903: 
 1904: sub files_exist {
 1905:     my ($r, $symb) = @_;
 1906:     my @students = &Apache::loncommon::get_env_multiple('form.stuinfo');
 1907: 
 1908:     foreach my $student (@students) {
 1909:         my ($uname,$udom,$fullname) = split(/:/,$student);
 1910:         my %record = &Apache::lonnet::restore($symb,$env{'request.course.id'},
 1911: 					      $udom,$uname);
 1912:         my ($string,$timestamp)= &get_last_submission(\%record);
 1913:         foreach my $submission (@$string) {
 1914:             my ($partid,$respid) =
 1915: 		($submission =~ /^resource\.([^\.]*)\.([^\.]*)\.submission/);
 1916:             my $files=&get_submitted_files($udom,$uname,$partid,$respid,
 1917: 					   \%record);
 1918:             return 1 if (@$files);
 1919:         }
 1920:     }
 1921:     return 0;
 1922: }
 1923: 
 1924: sub download_all_link {
 1925:     my ($r,$symb) = @_;
 1926:     my $all_students = 
 1927: 	join("\n", &Apache::loncommon::get_env_multiple('form.stuinfo'));
 1928: 
 1929:     my $parts =
 1930: 	join("\n",&Apache::loncommon::get_env_multiple('form.vPart'));
 1931: 
 1932:     my $identifier = &Apache::loncommon::get_cgi_id();
 1933:     &Apache::lonnet::appenv({'cgi.'.$identifier.'.students' => $all_students,
 1934:                              'cgi.'.$identifier.'.symb' => $symb,
 1935:                              'cgi.'.$identifier.'.parts' => $parts,});
 1936:     $r->print('<a href="/cgi-bin/multidownload.pl?'.$identifier.'">'.
 1937: 	      &mt('Download All Submitted Documents').'</a>');
 1938:     return
 1939: }
 1940: 
 1941: sub build_section_inputs {
 1942:     my $section_inputs;
 1943:     if ($env{'form.section'} eq '') {
 1944:         $section_inputs .= '<input type="hidden" name="section" value="all" />'."\n";
 1945:     } else {
 1946:         my @sections = &Apache::loncommon::get_env_multiple('form.section');
 1947:         foreach my $section (@sections) {
 1948:             $section_inputs .= '<input type="hidden" name="section" value="'.$section.'" />'."\n";
 1949:         }
 1950:     }
 1951:     return $section_inputs;
 1952: }
 1953: 
 1954: # --------------------------- show submissions of a student, option to grade 
 1955: sub submission {
 1956:     my ($request,$counter,$total) = @_;
 1957:     my ($uname,$udom)     = ($env{'form.student'},$env{'form.userdom'});
 1958:     $udom = ($udom eq '' ? $env{'user.domain'} : $udom); #has form.userdom changed for a student?
 1959:     my $usec = &Apache::lonnet::getsection($udom,$uname,$env{'request.course.id'});
 1960:     $env{'form.fullname'} = &Apache::loncommon::plainname($uname,$udom,'lastname') if $env{'form.fullname'} eq '';
 1961:     my $symb = &get_symb($request); 
 1962:     if ($symb eq '') { $request->print("Unable to handle ambiguous references:."); return ''; }
 1963: 
 1964:     if (!&canview($usec)) {
 1965: 	$request->print('<span class="LC_warning">Unable to view requested student.('.
 1966: 			$uname.':'.$udom.' in section '.$usec.' in course id '.
 1967: 			$env{'request.course.id'}.')</span>');
 1968: 	$request->print(&show_grading_menu_form($symb));
 1969: 	return;
 1970:     }
 1971: 
 1972:     if (!$env{'form.lastSub'}) { $env{'form.lastSub'} = 'datesub'; }
 1973:     if (!$env{'form.vProb'}) { $env{'form.vProb'} = 'yes'; }
 1974:     if (!$env{'form.vAns'}) { $env{'form.vAns'} = 'yes'; }
 1975:     my $last = ($env{'form.lastSub'} eq 'last' ? 'last' : '');
 1976:     my $checkIcon = '<img alt="'.&mt('Check Mark').
 1977: 	'" src="'.$request->dir_config('lonIconsURL').
 1978: 	'/check.gif" height="16" border="0" />';
 1979: 
 1980:     my %old_essays;
 1981:     # header info
 1982:     if ($counter == 0) {
 1983: 	&sub_page_js($request);
 1984: 	&sub_page_kw_js($request) if ($env{'form.handgrade'} eq 'yes');
 1985: 	$env{'form.probTitle'} = $env{'form.probTitle'} eq '' ? 
 1986: 	    &Apache::lonnet::gettitle($symb) : $env{'form.probTitle'};
 1987: 	if ($env{'form.handgrade'} eq 'yes' && &files_exist($request, $symb)) {
 1988: 	    &download_all_link($request, $symb);
 1989: 	}
 1990: 	$request->print('<h3>&nbsp;<span class="LC_info">'.&mt('Submission Record').'</span></h3>'."\n".
 1991: 			'<h4>&nbsp;'.&mt('<b>Resource: </b> [_1]',$env{'form.probTitle'}).'</h4>'."\n");
 1992: 
 1993: 	# option to display problem, only once else it cause problems 
 1994:         # with the form later since the problem has a form.
 1995: 	if ($env{'form.vProb'} eq 'yes' or $env{'form.vAns'} eq 'yes') {
 1996: 	    my $mode;
 1997: 	    if ($env{'form.vProb'} eq 'yes' && $env{'form.vAns'} eq 'yes') {
 1998: 		$mode='both';
 1999: 	    } elsif ($env{'form.vProb'} eq 'yes') {
 2000: 		$mode='text';
 2001: 	    } elsif ($env{'form.vAns'} eq 'yes') {
 2002: 		$mode='answer';
 2003: 	    }
 2004: 	    &Apache::lonxml::clear_problem_counter();
 2005: 	    $request->print(&show_problem($request,$symb,$uname,$udom,0,1,$mode));
 2006: 	}
 2007: 
 2008: 	# kwclr is the only variable that is guaranteed to be non blank 
 2009:         # if this subroutine has been called once.
 2010: 	my %keyhash = ();
 2011: 	if ($env{'form.kwclr'} eq '' && $env{'form.handgrade'} eq 'yes') {
 2012: 	    %keyhash = &Apache::lonnet::dump('nohist_handgrade',
 2013: 					     $env{'course.'.$env{'request.course.id'}.'.domain'},
 2014: 					     $env{'course.'.$env{'request.course.id'}.'.num'});
 2015: 
 2016: 	    my $loginuser = $env{'user.name'}.':'.$env{'user.domain'};
 2017: 	    $env{'form.keywords'} = $keyhash{$symb.'_keywords'} ne '' ? $keyhash{$symb.'_keywords'} : '';
 2018: 	    $env{'form.kwclr'}    = $keyhash{$loginuser.'_kwclr'} ne '' ? $keyhash{$loginuser.'_kwclr'} : 'red';
 2019: 	    $env{'form.kwsize'}   = $keyhash{$loginuser.'_kwsize'} ne '' ? $keyhash{$loginuser.'_kwsize'} : '0';
 2020: 	    $env{'form.kwstyle'}  = $keyhash{$loginuser.'_kwstyle'} ne '' ? $keyhash{$loginuser.'_kwstyle'} : '';
 2021: 	    $env{'form.msgsub'}   = $keyhash{$symb.'_subject'} ne '' ? 
 2022: 		$keyhash{$symb.'_subject'} : $env{'form.probTitle'};
 2023: 	    $env{'form.savemsgN'} = $keyhash{$symb.'_savemsgN'} ne '' ? $keyhash{$symb.'_savemsgN'} : '0';
 2024: 	}
 2025: 	my $overRideScore = $env{'form.overRideScore'} eq '' ? 'no' : $env{'form.overRideScore'};
 2026: 	my $stu_status = join(':',&Apache::loncommon::get_env_multiple('form.Status'));
 2027: 	$request->print('<form action="/adm/grades" method="post" name="SCORE" enctype="multipart/form-data">'."\n".
 2028: 			'<input type="hidden" name="command"    value="handgrade" />'."\n".
 2029: 			'<input type="hidden" name="saveState"  value="'.$env{'form.saveState'}.'" />'."\n".
 2030: 			'<input type="hidden" name="Status"     value="'.$stu_status.'" />'."\n".
 2031: 			'<input type="hidden" name="overRideScore" value="'.$overRideScore.'" />'."\n".
 2032: 			'<input type="hidden" name="probTitle"  value="'.$env{'form.probTitle'}.'" />'."\n".
 2033: 			'<input type="hidden" name="refresh"    value="off" />'."\n".
 2034: 			'<input type="hidden" name="studentNo"  value="" />'."\n".
 2035: 			'<input type="hidden" name="gradeOpt"   value="" />'."\n".
 2036: 			'<input type="hidden" name="symb"       value="'.&Apache::lonenc::check_encrypt($symb).'" />'."\n".
 2037: 			'<input type="hidden" name="showgrading" value="'.$env{'form.showgrading'}.'" />'."\n".
 2038: 			'<input type="hidden" name="vProb"      value="'.$env{'form.vProb'}.'" />'."\n".
 2039: 			'<input type="hidden" name="vAns"       value="'.$env{'form.vAns'}.'" />'."\n".
 2040: 			'<input type="hidden" name="lastSub"    value="'.$env{'form.lastSub'}.'" />'."\n".
 2041: 			&build_section_inputs().
 2042: 			'<input type="hidden" name="submitonly" value="'.$env{'form.submitonly'}.'" />'."\n".
 2043: 			'<input type="hidden" name="handgrade"  value="'.$env{'form.handgrade'}.'" />'."\n".
 2044: 			'<input type="hidden" name="NCT"'.
 2045: 			' value="'.($env{'form.NTSTU'} ne '' ? $env{'form.NTSTU'} : $total+1).'" />'."\n");
 2046: 	if ($env{'form.handgrade'} eq 'yes') {
 2047: 	    $request->print('<input type="hidden" name="keywords" value="'.$env{'form.keywords'}.'" />'."\n".
 2048: 			    '<input type="hidden" name="kwclr"    value="'.$env{'form.kwclr'}.'" />'."\n".
 2049: 			    '<input type="hidden" name="kwsize"   value="'.$env{'form.kwsize'}.'" />'."\n".
 2050: 			    '<input type="hidden" name="kwstyle"  value="'.$env{'form.kwstyle'}.'" />'."\n".
 2051: 			    '<input type="hidden" name="msgsub"   value="'.$env{'form.msgsub'}.'" />'."\n".
 2052: 			    '<input type="hidden" name="shownSub" value="0" />'."\n".
 2053: 			    '<input type="hidden" name="savemsgN" value="'.$env{'form.savemsgN'}.'" />'."\n");
 2054: 	    foreach my $partid (&Apache::loncommon::get_env_multiple('form.vPart')) {
 2055: 		$request->print('<input type="hidden" name="vPart" value="'.$partid.'" />'."\n");
 2056: 	    }
 2057: 	}
 2058: 	
 2059: 	my ($cts,$prnmsg) = (1,'');
 2060: 	while ($cts <= $env{'form.savemsgN'}) {
 2061: 	    $prnmsg.='<input type="hidden" name="savemsg'.$cts.'" value="'.
 2062: 		(!exists($keyhash{$symb.'_savemsg'.$cts}) ? 
 2063: 		 &Apache::lonfeedback::clear_out_html($env{'form.savemsg'.$cts}) :
 2064: 		 &Apache::lonfeedback::clear_out_html($keyhash{$symb.'_savemsg'.$cts})).
 2065: 		'" />'."\n".
 2066: 		'<input type="hidden" name="shownOnce'.$cts.'" value="0" />'."\n";
 2067: 	    $cts++;
 2068: 	}
 2069: 	$request->print($prnmsg);
 2070: 
 2071: 	if ($env{'form.handgrade'} eq 'yes' && $env{'form.showgrading'} eq 'yes') {
 2072: #
 2073: # Print out the keyword options line
 2074: #
 2075: 	    $request->print(<<KEYWORDS);
 2076: &nbsp;<b>Keyword Options:</b>&nbsp;
 2077: <a href="javascript:keywords(document.SCORE);" target="_self">List</a>&nbsp; &nbsp;
 2078: <a href="#" onmousedown="javascript:getSel(); return false"
 2079:  CLASS="page">Paste Selection to List</a>&nbsp; &nbsp;
 2080: <a href="javascript:kwhighlight();" target="_self">Highlight Attribute</a><br /><br />
 2081: KEYWORDS
 2082: #
 2083: # Load the other essays for similarity check
 2084: #
 2085:             my (undef,undef,$essayurl) = &Apache::lonnet::decode_symb($symb);
 2086: 	    my ($adom,$aname,$apath)=($essayurl=~/^($LONCAPA::domain_re)\/($LONCAPA::username_re)\/(.*)$/);
 2087: 	    $apath=&escape($apath);
 2088: 	    $apath=~s/\W/\_/gs;
 2089: 	    %old_essays=&Apache::lonnet::dump('nohist_essay_'.$apath,$adom,$aname);
 2090:         }
 2091:     }
 2092: 
 2093: # This is where output for one specific student would start
 2094:     my $add_class = ($counter%2) ? ' LC_grade_show_user_odd_row' : '';
 2095:     $request->print(
 2096:         "\n\n"
 2097:        .'<div class="LC_grade_show_user'.$add_class.'">'
 2098:        .'<h2>'.&nameUserString(undef,$env{'form.fullname'},$uname,$udom).'</h2>'
 2099:        ."\n"
 2100:     );
 2101: 
 2102:     # Show additional functions if allowed
 2103:     if ($perm{'vgr'}) {
 2104:         $request->print(
 2105:             &Apache::loncommon::track_student_link(
 2106:                 &mt('View recent activity'),
 2107:                 $uname,$udom,'check')
 2108:            .' '
 2109:         );
 2110:     }
 2111:     if ($perm{'opa'}) {
 2112:         $request->print(
 2113:             &Apache::loncommon::pprmlink(
 2114:                 &mt('Set/Change parameters'),
 2115:                 $uname,$udom,$symb,'check'));
 2116:     }
 2117: 
 2118:     # Show Problem
 2119:     if ($env{'form.vProb'} eq 'all' or $env{'form.vAns'} eq 'all') {
 2120: 	my $mode;
 2121: 	if ($env{'form.vProb'} eq 'all' && $env{'form.vAns'} eq 'all') {
 2122: 	    $mode='both';
 2123: 	} elsif ($env{'form.vProb'} eq 'all' ) {
 2124: 	    $mode='text';
 2125: 	} elsif ($env{'form.vAns'} eq 'all') {
 2126: 	    $mode='answer';
 2127: 	}
 2128: 	&Apache::lonxml::clear_problem_counter();
 2129: 	$request->print(&show_problem($request,$symb,$uname,$udom,1,1,$mode,{'request.prefix' => 'ctr'.$counter}));
 2130:     }
 2131: 
 2132:     my %record = &Apache::lonnet::restore($symb,$env{'request.course.id'},$udom,$uname);
 2133:     my $res_error;
 2134:     my ($partlist,$handgrade,$responseType) = &response_type($symb,\$res_error);
 2135:     if ($res_error) {
 2136:         $request->print(&navmap_errormsg());
 2137:         return;
 2138:     }
 2139: 
 2140:     # Display student info
 2141:     $request->print(($counter == 0 ? '' : '<br />'));
 2142: 
 2143:     my $result='<div class="LC_Box">'
 2144:               .'<h3 class="LC_hcell">'.&mt('Submissions').'</h3>';
 2145:     $result.='<input type="hidden" name="name'.$counter.
 2146:              '" value="'.$env{'form.fullname'}.'" />'."\n";
 2147:     if ($env{'form.handgrade'} eq 'no') {
 2148:         $result.='<p class="LC_info">'
 2149:                 .&mt('Part(s) graded correct by the computer is marked with a [_1] symbol.',$checkIcon)
 2150:                 ."</p>\n";
 2151:     }
 2152: 
 2153:     # If any part of the problem is an essay-response (handgraded), then check for collaborators
 2154:     my $fullname;
 2155:     my $col_fullnames = [];
 2156:     if ($env{'form.handgrade'} eq 'yes') {
 2157: 	(my $sub_result,$fullname,$col_fullnames)=
 2158: 	    &check_collaborators($symb,$uname,$udom,\%record,$handgrade,
 2159: 				 $counter);
 2160: 	$result.=$sub_result;
 2161:     }
 2162:     $request->print($result."\n");
 2163: 
 2164:     # print student answer/submission
 2165:     # Options are (1) Handgraded submission only
 2166:     #             (2) Last submission, includes submission that is not handgraded 
 2167:     #                  (for multi-response type part)
 2168:     #             (3) Last submission plus the parts info
 2169:     #             (4) The whole record for this student
 2170:     if ($env{'form.lastSub'} =~ /^(lastonly|hdgrade)$/) {
 2171: 	my ($string,$timestamp)= &get_last_submission(\%record);
 2172: 	
 2173: 	my $lastsubonly;
 2174: 
 2175:         if ($$timestamp eq '') {
 2176:             $lastsubonly.='<div class="LC_grade_submissions_body">'.$$string[0].'</div>'; 
 2177:         } else {
 2178:             $lastsubonly =
 2179:                 '<div class="LC_grade_submissions_body">'
 2180:                .'<b>'.&mt('Date Submitted:').'</b> '.$$timestamp."\n";
 2181: 
 2182: 	    my %seenparts;
 2183: 	    my @part_response_id = &flatten_responseType($responseType);
 2184: 	    foreach my $part (@part_response_id) {
 2185: 		next if ($env{'form.lastSub'} eq 'hdgrade' 
 2186: 			 && $$handgrade{$$part[0].'_'.$$part[1]} ne 'yes');
 2187: 
 2188: 		my ($partid,$respid) = @{ $part };
 2189: 		my $display_part=&get_display_part($partid,$symb);
 2190: 		if ($env{"form.$uname:$udom:$partid:submitted_by"}) {
 2191: 		    if (exists($seenparts{$partid})) { next; }
 2192: 		    $seenparts{$partid}=1;
 2193: 		    my $submitby='<b>Part:</b> '.$display_part.
 2194: 			' <b>Collaborative submission by:</b> '.
 2195: 			'<a href="javascript:viewSubmitter(\''.
 2196: 			$env{"form.$uname:$udom:$partid:submitted_by"}.
 2197: 			'\');" target="_self">'.
 2198: 			$$fullname{$env{"form.$uname:$udom:$partid:submitted_by"}}.'</a><br />';
 2199: 		    $request->print($submitby);
 2200: 		    next;
 2201: 		}
 2202: 		my $responsetype = $responseType->{$partid}->{$respid};
 2203: 		if (!exists($record{"resource.$partid.$respid.submission"})) {
 2204:                     $lastsubonly.="\n".'<div class="LC_grade_submission_part">'.
 2205:                         '<b>'.&mt('Part: [_1]',$display_part).'</b>'.
 2206:                         ' <span class="LC_internal_info">'.
 2207:                         '('.&mt('Part ID: [_1]',$respid).')</b>'.
 2208:                         '</span>&nbsp; &nbsp;'.
 2209: 			'<span class="LC_warning">'.&mt('Nothing submitted - no attempts.').'</span><br /><br /></div>';
 2210: 		    next;
 2211: 		}
 2212: 		foreach my $submission (@$string) {
 2213: 		    my ($partid,$respid) = ($submission =~ /^resource\.([^\.]*)\.([^\.]*)\.submission/);
 2214: 		    if (join('_',@{$part}) ne ($partid.'_'.$respid)) { next; }
 2215: 		    my ($ressub,$hide,$subval) = split(/:/,$submission,3);
 2216: 		    # Similarity check
 2217: 		    my $similar='';
 2218: 		    if($env{'form.checkPlag'}){
 2219: 			my ($oname,$odom,$ocrsid,$oessay,$osim)=
 2220: 			    &most_similar($uname,$udom,$subval,\%old_essays);
 2221: 			if ($osim) {
 2222: 			    $osim=int($osim*100.0);
 2223: 			    my %old_course_desc = 
 2224: 				&Apache::lonnet::coursedescription($ocrsid,
 2225: 								   {'one_time' => 1});
 2226: 
 2227:                             if ($hide) {
 2228:                                 $similar='<hr /><span class="LC_warning">'.&mt("Essay was found to be similar to another essay submitted for this assignment.").'<br />'.
 2229:                                          &mt('As the current submission is for an anonymous survey, no other details are available.').'</span><hr />';
 2230:                             } else {
 2231: 			        $similar="<hr /><h3><span class=\"LC_warning\">".
 2232: 				    &mt('Essay is [_1]% similar to an essay by [_2] in course [_3] (course id [_4]:[_5])',
 2233: 				        $osim,
 2234: 				        &Apache::loncommon::plainname($oname,$odom).' ('.$oname.':'.$odom.')',
 2235: 				        $old_course_desc{'description'},
 2236: 				        $old_course_desc{'num'},
 2237: 				        $old_course_desc{'domain'}).
 2238: 				    '</span></h3><blockquote><i>'.
 2239: 				    &keywords_highlight($oessay).
 2240: 				    '</i></blockquote><hr />';
 2241:                             }
 2242: 			}
 2243: 		    }
 2244: 		    my $order=&get_order($partid,$respid,$symb,$uname,$udom);
 2245: 		    if ($env{'form.lastSub'} eq 'lastonly' || 
 2246: 			($env{'form.lastSub'} eq 'hdgrade' && 
 2247: 			 $$handgrade{$$part[0].'_'.$$part[1]} eq 'yes')) {
 2248: 			my $display_part=&get_display_part($partid,$symb);
 2249:                         $lastsubonly.='<div class="LC_grade_submission_part">'.
 2250:                             '<b>'.&mt('Part: [_1]',$display_part).'</b>'.
 2251:                             ' <span class="LC_internal_info">'.
 2252:                             '('.&mt('Part ID: [_1]',$respid).')'.
 2253:                             '</b></span>&nbsp; &nbsp;';
 2254: 			my $files=&get_submitted_files($udom,$uname,$partid,$respid,\%record);
 2255: 			if (@$files) {
 2256:                             if ($hide) {
 2257:                                 $lastsubonly.='<br />'.&mt('[quant,_1,file] uploaded to this anonymous survey',scalar(@{$files}));
 2258:                             } else {
 2259:                                 $lastsubonly.='<br /><span class="LC_warning">'.&mt('Like all files provided by users, this file may contain viruses').'</span><br />';
 2260:                                 foreach my $file (@$files) {
 2261:                                     &Apache::lonnet::allowuploaded('/adm/grades',$file);
 2262:                                     $lastsubonly.='<br /><a href="'.$file.'?rawmode=1" target="lonGRDs"><img src="'.&Apache::loncommon::icon($file).'" border="0" /> '.$file.'</a>';
 2263:                                 }
 2264:                             }
 2265: 			    $lastsubonly.='<br />';
 2266: 			}
 2267:                         if ($hide) {
 2268:                             $lastsubonly.='<b>'.&mt('Anonymous Survey').'</b>'; 
 2269:                         } else {
 2270: 			    $lastsubonly.='<b>'.&mt('Submitted Answer:').' </b>'.
 2271: 			        &cleanRecord($subval,$responsetype,$symb,$partid,
 2272: 					     $respid,\%record,$order,undef,$uname,$udom);
 2273:                         }
 2274: 			if ($similar) {$lastsubonly.="<br /><br />$similar\n";}
 2275: 			$lastsubonly.='</div>';
 2276: 		    }
 2277: 		}
 2278: 	    }
 2279: 	    $lastsubonly.='</div>'."\n"; # End: LC_grade_submissions_body
 2280: 	}
 2281: 	$request->print($lastsubonly);
 2282:    } elsif ($env{'form.lastSub'} eq 'datesub') {
 2283: 	my (undef,$responseType,undef,$parts) = &showResourceInfo($symb);
 2284: 	$request->print(&displaySubByDates($symb,\%record,$parts,$responseType,$checkIcon,$uname,$udom));
 2285:     } elsif ($env{'form.lastSub'} =~ /^(last|all)$/) {
 2286: 	$request->print(&Apache::loncommon::get_previous_attempt($symb,$uname,$udom,
 2287: 								 $env{'request.course.id'},
 2288: 								 $last,'.submission',
 2289: 								 'Apache::grades::keywords_highlight'));
 2290:     }
 2291: 
 2292:     $request->print('<input type="hidden" name="unamedom'.$counter.'" value="'.$uname.':'
 2293: 	.$udom.'" />'."\n");
 2294:     # return if view submission with no grading option
 2295:     if ($env{'form.showgrading'} eq '' || (!&canmodify($usec))) {
 2296: 	my $toGrade.='<input type="button" value="Grade Student" '.
 2297: 	    'onclick="javascript:checksubmit(this.form,\'Grade Student\',\''
 2298: 	    .$counter.'\');" target="_self" /> &nbsp;'."\n" if (&canmodify($usec));
 2299: 	$toGrade.='</div>'."\n";
 2300: 	if (($env{'form.command'} eq 'submission') || 
 2301: 	    ($env{'form.command'} eq 'processGroup' && $counter == $total)) {
 2302: 	    $toGrade.='</form>'.&show_grading_menu_form($symb); 
 2303: 	}
 2304: 	$request->print($toGrade);
 2305: 	return;
 2306:     } else {
 2307: 	$request->print('</div>'."\n");
 2308:     }
 2309: 
 2310:     # essay grading message center
 2311:     if ($env{'form.handgrade'} eq 'yes') {
 2312: 	my $result='<div class="LC_grade_message_center">';
 2313:     
 2314: 	$result.='<div class="LC_grade_message_center_header">'.
 2315: 	    &mt('Send Message').'</div><div class="LC_grade_message_center_body">';
 2316: 	my ($lastname,$givenn) = split(/,/,$env{'form.fullname'});
 2317: 	my $msgfor = $givenn.' '.$lastname;
 2318: 	if (scalar(@$col_fullnames) > 0) {
 2319: 	    my $lastone = pop(@$col_fullnames);
 2320: 	    $msgfor .= ', '.(join ', ',@$col_fullnames).' and '.$lastone.'.';
 2321: 	}
 2322: 	$msgfor =~ s/\'/\\'/g; #' stupid emacs - no! javascript
 2323: 	$result.='<input type="hidden" name="includemsg'.$counter.'" value="" />'."\n".
 2324: 	    '<input type="hidden" name="newmsg'.$counter.'" value="" />'."\n";
 2325: 	$result.='&nbsp;<a href="javascript:msgCenter(document.SCORE,'.$counter.
 2326: 	    ',\''.$msgfor.'\');" target="_self">'.
 2327: 	    &mt('Compose message to student').(scalar(@$col_fullnames) >= 1 ? 's' : '').'</a><label> ('.
 2328: 	    &mt('incl. grades').' <input type="checkbox" name="withgrades'.$counter.'" /></label>)'.
 2329: 	    '<img src="'.$request->dir_config('lonIconsURL').
 2330: 	    '/mailbkgrd.gif" width="14" height="10" name="mailicon'.$counter.'" />'."\n".
 2331: 	    '<br />&nbsp;('.
 2332: 	    &mt('Message will be sent when you click on Save &amp; Next below.').")\n";
 2333: 	$result.='</div></div>';
 2334: 	$request->print($result);
 2335:     }
 2336: 
 2337:     my %seen = ();
 2338:     my @partlist;
 2339:     my @gradePartRespid;
 2340:     my @part_response_id = &flatten_responseType($responseType);
 2341:     $request->print(
 2342:         '<div class="LC_Box">'
 2343:        .'<h3 class="LC_hcell">'.&mt('Assign Grades').'</h3>'
 2344:     );
 2345:     $request->print(&gradeBox_start());
 2346:     foreach my $part_response_id (@part_response_id) {
 2347:     	my ($partid,$respid) = @{ $part_response_id };
 2348: 	my $part_resp = join('_',@{ $part_response_id });
 2349: 	next if ($seen{$partid} > 0);
 2350: 	$seen{$partid}++;
 2351: 	next if ($$handgrade{$part_resp} ne 'yes' 
 2352: 		 && $env{'form.lastSub'} eq 'hdgrade');
 2353: 	push(@partlist,$partid);
 2354: 	push(@gradePartRespid,$partid.'.'.$respid);
 2355: 	$request->print(&gradeBox($request,$symb,$uname,$udom,$counter,$partid,\%record));
 2356:     }
 2357:     $request->print(&gradeBox_end()); # </div>
 2358:     $request->print('</div>');
 2359: 
 2360:     $request->print('<div class="LC_grade_info_links">');
 2361:     $request->print('</div>');
 2362: 
 2363:     $result='<input type="hidden" name="partlist'.$counter.
 2364: 	'" value="'.(join ":",@partlist).'" />'."\n";
 2365:     $result.='<input type="hidden" name="gradePartRespid'.
 2366: 	'" value="'.(join ":",@gradePartRespid).'" />'."\n" if ($counter == 0);
 2367:     my $ctr = 0;
 2368:     while ($ctr < scalar(@partlist)) {
 2369: 	$result.='<input type="hidden" name="partid'.$counter.'_'.$ctr.'" value="'.
 2370: 	    $partlist[$ctr].'" />'."\n";
 2371: 	$ctr++;
 2372:     }
 2373:     $request->print($result.''."\n");
 2374: 
 2375: # Done with printing info for one student
 2376: 
 2377:     $request->print('</div>');#LC_grade_show_user
 2378: 
 2379: 
 2380:     # print end of form
 2381:     if ($counter == $total) {
 2382:         my $endform='<br /><hr /><table border="0"><tr><td>'."\n";
 2383: 	$endform.='<input type="button" value="'.&mt('Save &amp; Next').'" '.
 2384: 	    'onclick="javascript:checksubmit(this.form,\'Save & Next\','.
 2385: 	    $total.','.scalar(@partlist).');" target="_self" /> &nbsp;'."\n";
 2386: 	my $ntstu ='<select name="NTSTU">'.
 2387: 	    '<option>1</option><option>2</option>'.
 2388: 	    '<option>3</option><option>5</option>'.
 2389: 	    '<option>7</option><option>10</option></select>'."\n";
 2390: 	my $nsel = ($env{'form.NTSTU'} ne '' ? $env{'form.NTSTU'} : '1');
 2391: 	$ntstu =~ s/<option>$nsel</<option selected="selected">$nsel</;
 2392:         $endform.=&mt('[_1]student(s)',$ntstu);
 2393: 	$endform.='&nbsp;&nbsp;<input type="button" value="'.&mt('Previous').'" '.
 2394: 	    'onclick="javascript:checksubmit(this.form,\'Previous\');" target="_self" /> &nbsp;'."\n".
 2395: 	    '<input type="button" value="'.&mt('Next').'" '.
 2396: 	    'onclick="javascript:checksubmit(this.form,\'Next\');" target="_self" /> &nbsp;';
 2397:         $endform.='<span class="LC_warning">'.
 2398:                   &mt('(Next and Previous (student) do not save the scores.)').
 2399:                   '</span>'."\n" ;
 2400:         $endform.="<input type='hidden' value='".&get_increment().
 2401:             "' name='increment' />";
 2402: 	$endform.='</td></tr></table></form>';
 2403: 	$endform.=&show_grading_menu_form($symb);
 2404: 	$request->print($endform);
 2405:     }
 2406:     return '';
 2407: }
 2408: 
 2409: sub check_collaborators {
 2410:     my ($symb,$uname,$udom,$record,$handgrade,$counter) = @_;
 2411:     my ($result,@col_fullnames);
 2412:     my ($classlist,undef,$fullname) = &getclasslist('all','0');
 2413:     foreach my $part (keys(%$handgrade)) {
 2414: 	my $ncol = &Apache::lonnet::EXT('resource.'.$part.
 2415: 					'.maxcollaborators',
 2416: 					$symb,$udom,$uname);
 2417: 	next if ($ncol <= 0);
 2418: 	$part =~ s/\_/\./g;
 2419: 	next if ($record->{'resource.'.$part.'.collaborators'} eq '');
 2420: 	my (@good_collaborators, @bad_collaborators);
 2421: 	foreach my $possible_collaborator
 2422: 	    (split(/,?\s+/,$record->{'resource.'.$part.'.collaborators'})) { 
 2423: 	    $possible_collaborator =~ s/[\$\^\(\)]//g;
 2424: 	    next if ($possible_collaborator eq '');
 2425: 	    my ($co_name,$co_dom) = split(/\@|:/,$possible_collaborator);
 2426: 	    $co_dom = $udom if (! defined($co_dom) || $co_dom =~ /^domain$/i);
 2427: 	    next if ($co_name eq $uname && $co_dom eq $udom);
 2428: 	    # Doing this grep allows 'fuzzy' specification
 2429: 	    my @matches = grep(/^\Q$co_name\E:\Q$co_dom\E$/i, 
 2430: 			       keys(%$classlist));
 2431: 	    if (! scalar(@matches)) {
 2432: 		push(@bad_collaborators, $possible_collaborator);
 2433: 	    } else {
 2434: 		push(@good_collaborators, @matches);
 2435: 	    }
 2436: 	}
 2437: 	if (scalar(@good_collaborators) != 0) {
 2438: 	    $result.='<br />'.&mt('Collaborators: ');
 2439: 	    foreach my $name (@good_collaborators) {
 2440: 		my ($lastname,$givenn) = split(/,/,$$fullname{$name});
 2441: 		push(@col_fullnames, $givenn.' '.$lastname);
 2442: 		$result.=$fullname->{$name}.'&nbsp; &nbsp; &nbsp;';
 2443: 	    }
 2444: 	    $result.='<br />'."\n";
 2445: 	    my ($part)=split(/\./,$part);
 2446: 	    $result.='<input type="hidden" name="collaborator'.$counter.
 2447: 		'" value="'.$part.':'.(join ':',@good_collaborators).'" />'.
 2448: 		"\n";
 2449: 	}
 2450: 	if (scalar(@bad_collaborators) > 0) {
 2451: 	    $result.='<div class="LC_warning">';
 2452: 	    $result.=&mt('This student has submitted [quant,_1,invalid collaborator]: [_2]',scalar(@bad_collaborators),join(', ',@bad_collaborators));
 2453: 	    $result .= '</div>';
 2454: 	}         
 2455: 	if (scalar(@bad_collaborators > $ncol)) {
 2456: 	    $result .= '<div class="LC_warning">';
 2457: 	    $result .= &mt('This student has submitted too many '.
 2458: 		'collaborators.  Maximum is [_1].',$ncol);
 2459: 	    $result .= '</div>';
 2460: 	}
 2461:     }
 2462:     return ($result,$fullname,\@col_fullnames);
 2463: }
 2464: 
 2465: #--- Retrieve the last submission for all the parts
 2466: sub get_last_submission {
 2467:     my ($returnhash)=@_;
 2468:     my (@string,$timestamp,%lasthidden);
 2469:     if ($$returnhash{'version'}) {
 2470: 	my %lasthash=();
 2471: 	my ($version);
 2472: 	for ($version=1;$version<=$$returnhash{'version'};$version++) {
 2473: 	    foreach my $key (sort(split(/\:/,
 2474: 					$$returnhash{$version.':keys'}))) {
 2475: 		$lasthash{$key}=$$returnhash{$version.':'.$key};
 2476: 		$timestamp = 
 2477: 		    &Apache::lonlocal::locallocaltime($$returnhash{$version.':timestamp'});
 2478: 	    }
 2479: 	}
 2480:         my %typeparts;
 2481:         my $showsurv = 
 2482:             &Apache::lonnet::allowed('vas',$env{'request.course.id'});
 2483:         foreach my $key (sort(keys(%lasthash))) {
 2484:             if ($key =~ /\.type$/) {
 2485:                 if (($lasthash{$key} eq 'anonsurvey') || 
 2486:                     ($lasthash{$key} eq 'anonsurveycred')) {
 2487:                     my ($ign,@parts) = split(/\./,$key);
 2488:                     pop(@parts);
 2489:                     unless ($showsurv) {
 2490:                         my $id = join(',',@parts);
 2491:                         $typeparts{$ign.'.'.$id} = $lasthash{$key};
 2492:                     }
 2493:                     delete($lasthash{$key});
 2494:                 }
 2495:             }
 2496:         }
 2497:         my @hidden = keys(%typeparts);
 2498: 	foreach my $key (keys(%lasthash)) {
 2499: 	    next if ($key !~ /\.submission$/);
 2500:             my $hide;
 2501:             if (@hidden) {
 2502:                 foreach my $id (@hidden) {
 2503:                     if ($key =~ /^\Q$id\E/) {
 2504:                         $hide = 1;
 2505:                         last;
 2506:                     }
 2507:                 }
 2508:             }
 2509: 	    my ($partid,$foo) = split(/submission$/,$key);
 2510: 	    my $draft  = $lasthash{$partid.'awarddetail'} eq 'DRAFT' ?
 2511: 		'<span class="LC_warning">Draft Copy</span> ' : '';
 2512: 	    push(@string, join(':', $key, $hide, $draft.$lasthash{$key}));
 2513: 	}
 2514:     }
 2515:     if (!@string) {
 2516: 	$string[0] =
 2517: 	    '<span class="LC_warning">'.&mt('Nothing submitted - no attempts.').'</span>';
 2518:     }
 2519:     return (\@string,\$timestamp);
 2520: }
 2521: 
 2522: #--- High light keywords, with style choosen by user.
 2523: sub keywords_highlight {
 2524:     my $string    = shift;
 2525:     my $size      = $env{'form.kwsize'} eq '0' ? '' : 'size='.$env{'form.kwsize'};
 2526:     my $styleon   = $env{'form.kwstyle'} eq ''  ? '' : $env{'form.kwstyle'};
 2527:     (my $styleoff = $styleon) =~ s/\</\<\//;
 2528:     my @keylist   = split(/[,\s+]/,$env{'form.keywords'});
 2529:     foreach my $keyword (@keylist) {
 2530: 	$string =~ s/\b\Q$keyword\E(\b|\.)/<font color\=$env{'form.kwclr'} $size\>$styleon$keyword$styleoff<\/font>/gi;
 2531:     }
 2532:     return $string;
 2533: }
 2534: 
 2535: #--- Called from submission routine
 2536: sub processHandGrade {
 2537:     my ($request) = shift;
 2538:     my $symb   = &get_symb($request);
 2539:     my (undef,undef,$url) = &Apache::lonnet::decode_symb($symb);
 2540:     my $button = $env{'form.gradeOpt'};
 2541:     my $ngrade = $env{'form.NCT'};
 2542:     my $ntstu  = $env{'form.NTSTU'};
 2543:     my $cdom   = $env{'course.'.$env{'request.course.id'}.'.domain'};
 2544:     my $cnum   = $env{'course.'.$env{'request.course.id'}.'.num'};
 2545: 
 2546:     if ($button eq 'Save & Next') {
 2547: 	my $ctr = 0;
 2548: 	while ($ctr < $ngrade) {
 2549: 	    my ($uname,$udom) = split(/:/,$env{'form.unamedom'.$ctr});
 2550: 	    my ($errorflag,$pts,$wgt) = &saveHandGrade($request,$symb,$uname,$udom,$ctr);
 2551: 	    if ($errorflag eq 'no_score') {
 2552: 		$ctr++;
 2553: 		next;
 2554: 	    }
 2555: 	    if ($errorflag eq 'not_allowed') {
 2556: 		$request->print("<span class=\"LC_warning\">Not allowed to modify grades for $uname:$udom</span>");
 2557: 		$ctr++;
 2558: 		next;
 2559: 	    }
 2560: 	    my $includemsg = $env{'form.includemsg'.$ctr};
 2561: 	    my ($subject,$message,$msgstatus) = ('','','');
 2562: 	    my $restitle = &Apache::lonnet::gettitle($symb);
 2563:             my ($feedurl,$showsymb) =
 2564: 		&get_feedurl_and_symb($symb,$uname,$udom);
 2565: 	    my $messagetail;
 2566: 	    if ($includemsg =~ /savemsg|newmsg\Q$ctr\E/) {
 2567: 		$subject = $env{'form.msgsub'} if ($includemsg =~ /msgsub/);
 2568: 		unless ($subject=~/\w/) { $subject=&mt('Grading Feedback'); }
 2569: 		$subject.=' ['.$restitle.']';
 2570: 		my (@msgnum) = split(/,/,$includemsg);
 2571: 		foreach (@msgnum) {
 2572: 		    $message.=$env{'form.'.$_} if ($_ =~ /savemsg|newmsg/ && $_ ne '');
 2573: 		}
 2574: 		$message =&Apache::lonfeedback::clear_out_html($message);
 2575: 		if ($env{'form.withgrades'.$ctr}) {
 2576: 		    $message.="\n\nPoint".($pts > 1 ? 's':'').' awarded = '.$pts.' out of '.$wgt;
 2577: 		    $messagetail = " for <a href=\"".
 2578: 		                   $feedurl."?symb=$showsymb\">$env{'form.probTitle'}</a>";
 2579: 		}
 2580: 		$msgstatus = 
 2581:                     &Apache::lonmsg::user_normal_msg($uname,$udom,$subject,
 2582: 						     $message.$messagetail,
 2583:                                                      undef,$feedurl,undef,
 2584:                                                      undef,undef,$showsymb,
 2585:                                                      $restitle);
 2586: 		$request->print('<br />'.&mt('Sending message to [_1]',$uname.':'.$udom).': '.
 2587: 				$msgstatus);
 2588: 	    }
 2589: 	    if ($env{'form.collaborator'.$ctr}) {
 2590: 		my @collabstrs=&Apache::loncommon::get_env_multiple("form.collaborator$ctr");
 2591: 		foreach my $collabstr (@collabstrs) {
 2592: 		    my ($part,@collaborators) = split(/:/,$collabstr);
 2593: 		    foreach my $collaborator (@collaborators) {
 2594: 			my ($errorflag,$pts,$wgt) = 
 2595: 			    &saveHandGrade($request,$symb,$collaborator,$udom,$ctr,
 2596: 					   $env{'form.unamedom'.$ctr},$part);
 2597: 			if ($errorflag eq 'not_allowed') {
 2598: 			    $request->print("<span class=\"LC_error\">".&mt('Not allowed to modify grades for [_1]',"$collaborator:$udom")."</span>");
 2599: 			    next;
 2600: 			} elsif ($message ne '') {
 2601: 			    my ($baseurl,$showsymb) = 
 2602: 				&get_feedurl_and_symb($symb,$collaborator,
 2603: 						      $udom);
 2604: 			    if ($env{'form.withgrades'.$ctr}) {
 2605: 				$messagetail = " for <a href=\"".
 2606:                                     $baseurl."?symb=$showsymb\">$env{'form.probTitle'}</a>";
 2607: 			    }
 2608: 			    $msgstatus = 
 2609: 				&Apache::lonmsg::user_normal_msg($collaborator,$udom,$subject,$message.$messagetail,undef,$baseurl,undef,undef,undef,$showsymb,$restitle);
 2610: 			}
 2611: 		    }
 2612: 		}
 2613: 	    }
 2614: 	    $ctr++;
 2615: 	}
 2616:     }
 2617: 
 2618:     if ($env{'form.handgrade'} eq 'yes') {
 2619: 	# Keywords sorted in alphabatical order
 2620: 	my $loginuser = $env{'user.name'}.':'.$env{'user.domain'};
 2621: 	my %keyhash = ();
 2622: 	$env{'form.keywords'}           =~ s/,\s{0,}|\s+/ /g;
 2623: 	$env{'form.keywords'}           =~ s/^\s+|\s+$//;
 2624: 	my (@keywords) = sort(split(/\s+/,$env{'form.keywords'}));
 2625: 	$env{'form.keywords'} = join(' ',@keywords);
 2626: 	$keyhash{$symb.'_keywords'}     = $env{'form.keywords'};
 2627: 	$keyhash{$symb.'_subject'}      = $env{'form.msgsub'};
 2628: 	$keyhash{$loginuser.'_kwclr'}   = $env{'form.kwclr'};
 2629: 	$keyhash{$loginuser.'_kwsize'}  = $env{'form.kwsize'};
 2630: 	$keyhash{$loginuser.'_kwstyle'} = $env{'form.kwstyle'};
 2631: 
 2632: 	# message center - Order of message gets changed. Blank line is eliminated.
 2633: 	# New messages are saved in env for the next student.
 2634: 	# All messages are saved in nohist_handgrade.db
 2635: 	my ($ctr,$idx) = (1,1);
 2636: 	while ($ctr <= $env{'form.savemsgN'}) {
 2637: 	    if ($env{'form.savemsg'.$ctr} ne '') {
 2638: 		$keyhash{$symb.'_savemsg'.$idx} = $env{'form.savemsg'.$ctr};
 2639: 		$idx++;
 2640: 	    }
 2641: 	    $ctr++;
 2642: 	}
 2643: 	$ctr = 0;
 2644: 	while ($ctr < $ngrade) {
 2645: 	    if ($env{'form.newmsg'.$ctr} ne '') {
 2646: 		$keyhash{$symb.'_savemsg'.$idx} = $env{'form.newmsg'.$ctr};
 2647: 		$env{'form.savemsg'.$idx} = $env{'form.newmsg'.$ctr};
 2648: 		$idx++;
 2649: 	    }
 2650: 	    $ctr++;
 2651: 	}
 2652: 	$env{'form.savemsgN'} = --$idx;
 2653: 	$keyhash{$symb.'_savemsgN'} = $env{'form.savemsgN'};
 2654: 	my $putresult = &Apache::lonnet::put
 2655: 	    ('nohist_handgrade',\%keyhash,$cdom,$cnum);
 2656:     }
 2657:     # Called by Save & Refresh from Highlight Attribute Window
 2658:     my (undef,undef,$fullname) = &getclasslist($env{'form.section'},'1');
 2659:     if ($env{'form.refresh'} eq 'on') {
 2660: 	my ($ctr,$total) = (0,0);
 2661: 	while ($ctr < $ngrade) {
 2662: 	    $total++ if  $env{'form.unamedom'.$ctr} ne '';
 2663: 	    $ctr++;
 2664: 	}
 2665: 	$env{'form.NTSTU'}=$ngrade;
 2666: 	$ctr = 0;
 2667: 	while ($ctr < $total) {
 2668: 	    my $processUser = $env{'form.unamedom'.$ctr};
 2669: 	    ($env{'form.student'},$env{'form.userdom'}) = split(/:/,$processUser);
 2670: 	    $env{'form.fullname'} = $$fullname{$processUser};
 2671: 	    &submission($request,$ctr,$total-1);
 2672: 	    $ctr++;
 2673: 	}
 2674: 	return '';
 2675:     }
 2676: 
 2677: # Go directly to grade student - from submission or link from chart page
 2678:     if ($button eq 'Grade Student') {
 2679: 	(undef,undef,$env{'form.handgrade'},undef,undef) = &showResourceInfo($symb);
 2680: 	my $processUser = $env{'form.unamedom'.$env{'form.studentNo'}};
 2681: 	($env{'form.student'},$env{'form.userdom'}) = split(/:/,$processUser);
 2682: 	$env{'form.fullname'} = $$fullname{$processUser};
 2683: 	&submission($request,0,0);
 2684: 	return '';
 2685:     }
 2686: 
 2687:     # Get the next/previous one or group of students
 2688:     my $firststu = $env{'form.unamedom0'};
 2689:     my $laststu = $env{'form.unamedom'.($ngrade-1)};
 2690:     my $ctr = 2;
 2691:     while ($laststu eq '') {
 2692: 	$laststu  = $env{'form.unamedom'.($ngrade-$ctr)};
 2693: 	$ctr++;
 2694: 	$laststu = $firststu if ($ctr > $ngrade);
 2695:     }
 2696: 
 2697:     my (@parsedlist,@nextlist);
 2698:     my ($nextflg) = 0;
 2699:     foreach my $item (sort 
 2700: 	     {
 2701: 		 if (lc($$fullname{$a}) ne lc($$fullname{$b})) {
 2702: 		     return (lc($$fullname{$a}) cmp lc($$fullname{$b}));
 2703: 		 }
 2704: 		 return $a cmp $b;
 2705: 	     } (keys(%$fullname))) {
 2706: 	if ($nextflg == 1 && $button =~ /Next$/) {
 2707: 	    push(@parsedlist,$item);
 2708: 	}
 2709: 	$nextflg = 1 if ($item eq $laststu);
 2710: 	if ($button eq 'Previous') {
 2711: 	    last if ($item eq $firststu);
 2712: 	    push(@parsedlist,$item);
 2713: 	}
 2714:     }
 2715:     $ctr = 0;
 2716:     @parsedlist = reverse @parsedlist if ($button eq 'Previous');
 2717:     my $res_error;
 2718:     my ($partlist) = &response_type($symb,\$res_error);
 2719:     if ($res_error) {
 2720:         $request->print(&navmap_errormsg());
 2721:         return;
 2722:     }
 2723:     foreach my $student (@parsedlist) {
 2724: 	my $submitonly=$env{'form.submitonly'};
 2725: 	my ($uname,$udom) = split(/:/,$student);
 2726: 	
 2727: 	if ($submitonly eq 'queued') {
 2728: 	    my %queue_status = 
 2729: 		&Apache::bridgetask::get_student_status($symb,$cdom,$cnum,
 2730: 							$udom,$uname);
 2731: 	    next if (!defined($queue_status{'gradingqueue'}));
 2732: 	}
 2733: 
 2734: 	if ($submitonly =~ /^(yes|graded|incorrect)$/) {
 2735: #	    my %record = &Apache::lonnet::restore($symb,$env{'request.course.id'},$udom,$uname);
 2736: 	    my %status=&student_gradeStatus($symb,$udom,$uname,$partlist);
 2737: 	    my $submitted = 0;
 2738: 	    my $ungraded = 0;
 2739: 	    my $incorrect = 0;
 2740: 	    foreach my $item (keys(%status)) {
 2741: 		$submitted = 1 if ($status{$item} ne 'nothing');
 2742: 		$ungraded = 1 if ($status{$item} =~ /^ungraded/);
 2743: 		$incorrect = 1 if ($status{$item} =~ /^incorrect/);
 2744: 		my ($foo,$partid,$foo1) = split(/\./,$item);
 2745: 		if ($status{'resource.'.$partid.'.submitted_by'} ne '') {
 2746: 		    $submitted = 0;
 2747: 		}
 2748: 	    }
 2749: 	    next if (!$submitted && ($submitonly eq 'yes' ||
 2750: 				     $submitonly eq 'incorrect' ||
 2751: 				     $submitonly eq 'graded'));
 2752: 	    next if (!$ungraded && ($submitonly eq 'graded'));
 2753: 	    next if (!$incorrect && $submitonly eq 'incorrect');
 2754: 	}
 2755: 	push(@nextlist,$student) if ($ctr < $ntstu);
 2756: 	last if ($ctr == $ntstu);
 2757: 	$ctr++;
 2758:     }
 2759: 
 2760:     $ctr = 0;
 2761:     my $total = scalar(@nextlist)-1;
 2762: 
 2763:     foreach (sort(@nextlist)) {
 2764: 	my ($uname,$udom,$submitter) = split(/:/);
 2765: 	$env{'form.student'}  = $uname;
 2766: 	$env{'form.userdom'}  = $udom;
 2767: 	$env{'form.fullname'} = $$fullname{$_};
 2768: 	&submission($request,$ctr,$total);
 2769: 	$ctr++;
 2770:     }
 2771:     if ($total < 0) {
 2772: 	my $the_end = '<h3><span class="LC_info">'.&mt('LON-CAPA User Message').'</span></h3><br />'."\n";
 2773: 	$the_end.=&mt('<b>Message: </b> No more students for this section or class.').'<br /><br />'."\n";
 2774: 	$the_end.=&mt('Click on the button below to return to the grading menu.').'<br /><br />'."\n";
 2775: 	$the_end.=&show_grading_menu_form($symb);
 2776: 	$request->print($the_end);
 2777:     }
 2778:     return '';
 2779: }
 2780: 
 2781: #---- Save the score and award for each student, if changed
 2782: sub saveHandGrade {
 2783:     my ($request,$symb,$stuname,$domain,$newflg,$submitter,$part) = @_;
 2784:     my @version_parts;
 2785:     my $usec = &Apache::lonnet::getsection($domain,$stuname,
 2786: 					   $env{'request.course.id'});
 2787:     if (!&canmodify($usec)) { return('not_allowed'); }
 2788:     my %record = &Apache::lonnet::restore($symb,$env{'request.course.id'},$domain,$stuname);
 2789:     my @parts_graded;
 2790:     my %newrecord  = ();
 2791:     my ($pts,$wgt) = ('','');
 2792:     my %aggregate = ();
 2793:     my $aggregateflag = 0;
 2794:     my @parts = split(/:/,$env{'form.partlist'.$newflg});
 2795:     foreach my $new_part (@parts) {
 2796: 	#collaborator ($submi may vary for different parts
 2797: 	if ($submitter && $new_part ne $part) { next; }
 2798: 	my $dropMenu = $env{'form.GD_SEL'.$newflg.'_'.$new_part};
 2799: 	if ($dropMenu eq 'excused') {
 2800: 	    if ($record{'resource.'.$new_part.'.solved'} ne 'excused') {
 2801: 		$newrecord{'resource.'.$new_part.'.solved'} = 'excused';
 2802: 		if (exists($record{'resource.'.$new_part.'.awarded'})) {
 2803: 		    $newrecord{'resource.'.$new_part.'.awarded'} = '';
 2804: 		}
 2805: 	        $newrecord{'resource.'.$new_part.'.regrader'}="$env{'user.name'}:$env{'user.domain'}";
 2806: 	    }
 2807: 	} elsif ($dropMenu eq 'reset status'
 2808: 		 && exists($record{'resource.'.$new_part.'.solved'})) { #don't bother if no old records -> no attempts
 2809: 	    foreach my $key (keys(%record)) {
 2810: 		if ($key=~/^resource\.\Q$new_part\E\./) { $newrecord{$key} = ''; }
 2811: 	    }
 2812: 	    $newrecord{'resource.'.$new_part.'.regrader'}=
 2813: 		"$env{'user.name'}:$env{'user.domain'}";
 2814:             my $totaltries = $record{'resource.'.$part.'.tries'};
 2815: 
 2816:             my %last_resets = &get_last_resets($symb,$env{'request.course.id'},
 2817: 					       [$new_part]);
 2818:             my $aggtries =$totaltries;
 2819:             if ($last_resets{$new_part}) {
 2820:                 $aggtries = &get_num_tries(\%record,$last_resets{$new_part},
 2821: 					   $new_part);
 2822:             }
 2823: 
 2824:             my $solvedstatus = $record{'resource.'.$new_part.'.solved'};
 2825:             if ($aggtries > 0) {
 2826:                 &decrement_aggs($symb,$new_part,\%aggregate,$aggtries,$totaltries,$solvedstatus);
 2827:                 $aggregateflag = 1;
 2828:             }
 2829: 	} elsif ($dropMenu eq '') {
 2830: 	    $pts = ($env{'form.GD_BOX'.$newflg.'_'.$new_part} ne '' ? 
 2831: 		    $env{'form.GD_BOX'.$newflg.'_'.$new_part} : 
 2832: 		    $env{'form.RADVAL'.$newflg.'_'.$new_part});
 2833: 	    if ($pts eq '' && $env{'form.GD_SEL'.$newflg.'_'.$new_part} eq '') {
 2834: 		next;
 2835: 	    }
 2836: 	    $wgt = $env{'form.WGT'.$newflg.'_'.$new_part} eq '' ? 1 : 
 2837: 		$env{'form.WGT'.$newflg.'_'.$new_part};
 2838: 	    my $partial= $pts/$wgt;
 2839: 	    if ($partial eq $record{'resource.'.$new_part.'.awarded'}) {
 2840: 		#do not update score for part if not changed.
 2841:                 &handback_files($request,$symb,$stuname,$domain,$newflg,$new_part,\%newrecord);
 2842: 		next;
 2843: 	    } else {
 2844: 	        push(@parts_graded,$new_part);
 2845: 	    }
 2846: 	    if ($record{'resource.'.$new_part.'.awarded'} ne $partial) {
 2847: 		$newrecord{'resource.'.$new_part.'.awarded'}  = $partial;
 2848: 	    }
 2849: 	    my $reckey = 'resource.'.$new_part.'.solved';
 2850: 	    if ($partial == 0) {
 2851: 		if ($record{$reckey} ne 'incorrect_by_override') {
 2852: 		    $newrecord{$reckey} = 'incorrect_by_override';
 2853: 		}
 2854: 	    } else {
 2855: 		if ($record{$reckey} ne 'correct_by_override') {
 2856: 		    $newrecord{$reckey} = 'correct_by_override';
 2857: 		}
 2858: 	    }	    
 2859: 	    if ($submitter && 
 2860: 		($record{'resource.'.$new_part.'.submitted_by'} ne $submitter)) {
 2861: 		$newrecord{'resource.'.$new_part.'.submitted_by'} = $submitter;
 2862: 	    }
 2863: 	    $newrecord{'resource.'.$new_part.'.regrader'}=
 2864: 		"$env{'user.name'}:$env{'user.domain'}";
 2865: 	}
 2866: 	# unless problem has been graded, set flag to version the submitted files
 2867: 	unless ($record{'resource.'.$new_part.'.solved'} =~ /^correct_/  || 
 2868: 	        $record{'resource.'.$new_part.'.solved'} eq 'incorrect_by_override' ||
 2869: 	        $dropMenu eq 'reset status')
 2870: 	   {
 2871: 	    push(@version_parts,$new_part);
 2872: 	}
 2873:     }
 2874:     my $cdom = $env{'course.'.$env{'request.course.id'}.'.domain'};
 2875:     my $cnum = $env{'course.'.$env{'request.course.id'}.'.num'};
 2876: 
 2877:     if (%newrecord) {
 2878:         if (@version_parts) {
 2879:             my @changed_keys = &version_portfiles(\%record, \@parts_graded, 
 2880:                                 $env{'request.course.id'}, $symb, $domain, $stuname, \@version_parts);
 2881: 	    @newrecord{@changed_keys} = @record{@changed_keys};
 2882: 	    foreach my $new_part (@version_parts) {
 2883: 		&handback_files($request,$symb,$stuname,$domain,$newflg,
 2884: 				$new_part,\%newrecord);
 2885: 	    }
 2886:         }
 2887: 	&Apache::lonnet::cstore(\%newrecord,$symb,
 2888: 				$env{'request.course.id'},$domain,$stuname);
 2889: 	&check_and_remove_from_queue(\@parts,\%record,\%newrecord,$symb,
 2890: 				     $cdom,$cnum,$domain,$stuname);
 2891:     }
 2892:     if ($aggregateflag) {
 2893:         &Apache::lonnet::cinc('nohist_resourcetracker',\%aggregate,
 2894: 			      $cdom,$cnum);
 2895:     }
 2896:     return ('',$pts,$wgt);
 2897: }
 2898: 
 2899: sub check_and_remove_from_queue {
 2900:     my ($parts,$record,$newrecord,$symb,$cdom,$cnum,$domain,$stuname) = @_;
 2901:     my @ungraded_parts;
 2902:     foreach my $part (@{$parts}) {
 2903: 	if (    $record->{   'resource.'.$part.'.awarded'} eq ''
 2904: 	     && $record->{   'resource.'.$part.'.solved' } ne 'excused'
 2905: 	     && $newrecord->{'resource.'.$part.'.awarded'} eq ''
 2906: 	     && $newrecord->{'resource.'.$part.'.solved' } ne 'excused'
 2907: 		) {
 2908: 	    push(@ungraded_parts, $part);
 2909: 	}
 2910:     }
 2911:     if ( !@ungraded_parts ) {
 2912: 	&Apache::bridgetask::remove_from_queue('gradingqueue',$symb,$cdom,
 2913: 					       $cnum,$domain,$stuname);
 2914:     }
 2915: }
 2916: 
 2917: sub handback_files {
 2918:     my ($request,$symb,$stuname,$domain,$newflg,$new_part,$newrecord) = @_;
 2919:     my $portfolio_root = '/userfiles/portfolio';
 2920:     my $res_error;
 2921:     my ($partlist,$handgrade,$responseType) = &response_type($symb,\$res_error);
 2922:     if ($res_error) {
 2923:         $request->print('<br />'.&navmap_errormsg().'<br />');
 2924:         return;
 2925:     }
 2926:     my @part_response_id = &flatten_responseType($responseType);
 2927:     foreach my $part_response_id (@part_response_id) {
 2928:     	my ($part_id,$resp_id) = @{ $part_response_id };
 2929: 	my $part_resp = join('_',@{ $part_response_id });
 2930:             if (($env{'form.'.$newflg.'_'.$part_resp.'_returndoc1'}) && ($new_part == $part_id)) {
 2931:                 # if multiple files are uploaded names will be 'returndoc2','returndoc3'
 2932:                 my $file_counter = 1;
 2933: 		my $file_msg;
 2934:                 while ($env{'form.'.$newflg.'_'.$part_resp.'_returndoc'.$file_counter}) {
 2935:                     my $fname=$env{'form.'.$newflg.'_'.$part_resp.'_returndoc'.$file_counter.'.filename'};
 2936:                     my ($directory,$answer_file) = 
 2937:                         ($env{'form.'.$newflg.'_'.$part_resp.'_origdoc'.$file_counter} =~ /^(.*?)([^\/]*)$/);
 2938:                     my ($answer_name,$answer_ver,$answer_ext) =
 2939: 		        &file_name_version_ext($answer_file);
 2940: 		    my ($portfolio_path) = ($directory =~ /^.+$stuname\/portfolio(.*)/);
 2941:                     my $getpropath = 1;
 2942: 		    my @dir_list = &Apache::lonnet::dirlist($portfolio_root.$portfolio_path,$domain,$stuname,$getpropath);
 2943: 		    my $version = &get_next_version($answer_name, $answer_ext, \@dir_list);
 2944:                     # fix file name
 2945:                     my ($save_file_name) = (($directory.$answer_name.".$version.".$answer_ext) =~ /^.+\/${stuname}\/(.*)/);
 2946:                     my $result=&Apache::lonnet::finishuserfileupload($stuname,$domain,
 2947:             	                                $newflg.'_'.$part_resp.'_returndoc'.$file_counter,
 2948:             	                                $save_file_name);
 2949:                     if ($result !~ m|^/uploaded/|) {
 2950:                         $request->print('<br /><span class="LC_error">'.
 2951:                             &mt('An error occurred ([_1]) while trying to upload [_2].',
 2952:                                 $result,$newflg.'_'.$part_resp.'_returndoc'.$file_counter).
 2953:                                         '</span>');
 2954:                     } else {
 2955:                         # mark the file as read only
 2956:                         my @files = ($save_file_name);
 2957:                         my @what = ($symb,$env{'request.course.id'},'handback');
 2958:                         &Apache::lonnet::mark_as_readonly($domain,$stuname,\@files,\@what);
 2959: 			if (exists($$newrecord{"resource.$new_part.$resp_id.handback"})) {
 2960: 			    $$newrecord{"resource.$new_part.$resp_id.handback"}.=',';
 2961: 			}
 2962:                         $$newrecord{"resource.$new_part.$resp_id.handback"} .= $save_file_name;
 2963: 			$file_msg.= "\n".'<br /><span class="LC_filename"><a href="/uploaded/'."$domain/$stuname/".$save_file_name.'">'.$save_file_name."</a></span><br />";
 2964: 
 2965:                     }
 2966:                     $request->print("<br />".$fname." will be the uploaded file name");
 2967:                     $request->print(" ".$env{'form.'.$newflg.'_'.$part_resp.'_origdoc'.$file_counter});
 2968:                     $file_counter++;
 2969:                 }
 2970: 		my $subject = "File Handed Back by Instructor ";
 2971: 		my $message = "A file has been returned that was originally submitted in reponse to: <br />";
 2972: 		$message .= "<strong>".&Apache::lonnet::gettitle($symb)."</strong><br />";
 2973: 		$message .= ' The returned file(s) are named: '. $file_msg;
 2974: 		$message .= " and can be found in your portfolio space.";
 2975: 		my ($feedurl,$showsymb) = 
 2976: 		    &get_feedurl_and_symb($symb,$domain,$stuname);
 2977:                 my $restitle = &Apache::lonnet::gettitle($symb);
 2978: 		my $msgstatus = 
 2979:                    &Apache::lonmsg::user_normal_msg($stuname,$domain,$subject.
 2980: 			 ' (File Returned) ['.$restitle.']',$message,undef,
 2981:                          $feedurl,undef,undef,undef,$showsymb,$restitle);
 2982:             }
 2983:         }
 2984:     return;
 2985: }
 2986: 
 2987: sub get_feedurl_and_symb {
 2988:     my ($symb,$uname,$udom) = @_;
 2989:     my (undef,undef,$url) = &Apache::lonnet::decode_symb($symb);
 2990:     $url = &Apache::lonnet::clutter($url);
 2991:     my $encrypturl=&Apache::lonnet::EXT('resource.0.encrypturl',
 2992: 					$symb,$udom,$uname);
 2993:     if ($encrypturl =~ /^yes$/i) {
 2994: 	&Apache::lonenc::encrypted(\$url,1);
 2995: 	&Apache::lonenc::encrypted(\$symb,1);
 2996:     }
 2997:     return ($url,$symb);
 2998: }
 2999: 
 3000: sub get_submitted_files {
 3001:     my ($udom,$uname,$partid,$respid,$record) = @_;
 3002:     my @files;
 3003:     if ($$record{"resource.$partid.$respid.portfiles"}) {
 3004:         my $file_url = '/uploaded/'.$udom.'/'.$uname.'/portfolio';
 3005:         foreach my $file (split(',',$$record{"resource.$partid.$respid.portfiles"})) {
 3006:     	    push(@files,$file_url.$file);
 3007:         }
 3008:     }
 3009:     if ($$record{"resource.$partid.$respid.uploadedurl"}) {
 3010:         push(@files,$$record{"resource.$partid.$respid.uploadedurl"});
 3011:     }
 3012:     return (\@files);
 3013: }
 3014: 
 3015: # ----------- Provides number of tries since last reset.
 3016: sub get_num_tries {
 3017:     my ($record,$last_reset,$part) = @_;
 3018:     my $timestamp = '';
 3019:     my $num_tries = 0;
 3020:     if ($$record{'version'}) {
 3021:         for (my $version=$$record{'version'};$version>=1;$version--) {
 3022:             if (exists($$record{$version.':resource.'.$part.'.solved'})) {
 3023:                 $timestamp = $$record{$version.':timestamp'};
 3024:                 if ($timestamp > $last_reset) {
 3025:                     $num_tries ++;
 3026:                 } else {
 3027:                     last;
 3028:                 }
 3029:             }
 3030:         }
 3031:     }
 3032:     return $num_tries;
 3033: }
 3034: 
 3035: # ----------- Determine decrements required in aggregate totals 
 3036: sub decrement_aggs {
 3037:     my ($symb,$part,$aggregate,$aggtries,$totaltries,$solvedstatus) = @_;
 3038:     my %decrement = (
 3039:                         attempts => 0,
 3040:                         users => 0,
 3041:                         correct => 0
 3042:                     );
 3043:     $decrement{'attempts'} = $aggtries;
 3044:     if ($solvedstatus =~ /^correct/) {
 3045:         $decrement{'correct'} = 1;
 3046:     }
 3047:     if ($aggtries == $totaltries) {
 3048:         $decrement{'users'} = 1;
 3049:     }
 3050:     foreach my $type (keys(%decrement)) {
 3051:         $$aggregate{$symb."\0".$part."\0".$type} = -$decrement{$type};
 3052:     }
 3053:     return;
 3054: }
 3055: 
 3056: # ----------- Determine timestamps for last reset of aggregate totals for parts  
 3057: sub get_last_resets {
 3058:     my ($symb,$courseid,$partids) =@_;
 3059:     my %last_resets;
 3060:     my $cdom = $env{'course.'.$courseid.'.domain'};
 3061:     my $cname = $env{'course.'.$courseid.'.num'};
 3062:     my @keys;
 3063:     foreach my $part (@{$partids}) {
 3064: 	push(@keys,"$symb\0$part\0resettime");
 3065:     }
 3066:     my %results=&Apache::lonnet::get('nohist_resourcetracker',\@keys,
 3067: 				     $cdom,$cname);
 3068:     foreach my $part (@{$partids}) {
 3069: 	$last_resets{$part}=$results{"$symb\0$part\0resettime"};
 3070:     }
 3071:     return %last_resets;
 3072: }
 3073: 
 3074: # ----------- Handles creating versions for portfolio files as answers
 3075: sub version_portfiles {
 3076:     my ($record, $parts_graded, $courseid, $symb, $domain, $stu_name, $v_flag) = @_;
 3077:     my $version_parts = join('|',@$v_flag);
 3078:     my @returned_keys;
 3079:     my $parts = join('|', @$parts_graded);
 3080:     my $portfolio_root = '/userfiles/portfolio';
 3081:     foreach my $key (keys(%$record)) {
 3082:         my $new_portfiles;
 3083:         if ($key =~ /^resource\.($version_parts)\./ && $key =~ /\.portfiles$/ ) {
 3084:             my @versioned_portfiles;
 3085:             my @portfiles = split(/\s*,\s*/,$$record{$key});
 3086:             foreach my $file (@portfiles) {
 3087:                 &Apache::lonnet::unmark_as_readonly($domain,$stu_name,[$symb,$env{'request.course.id'}],$file);
 3088:                 my ($directory,$answer_file) =($file =~ /^(.*?)([^\/]*)$/);
 3089: 		my ($answer_name,$answer_ver,$answer_ext) =
 3090: 		    &file_name_version_ext($answer_file);
 3091:                 my $getpropath = 1;    
 3092:                 my @dir_list = &Apache::lonnet::dirlist($portfolio_root.$directory,$domain,$stu_name,$getpropath);
 3093:                 my $version = &get_next_version($answer_name, $answer_ext, \@dir_list);
 3094:                 my $new_answer = &version_selected_portfile($domain, $stu_name, $directory, $answer_file, $version);
 3095:                 if ($new_answer ne 'problem getting file') {
 3096:                     push(@versioned_portfiles, $directory.$new_answer);
 3097:                     &Apache::lonnet::mark_as_readonly($domain,$stu_name,
 3098:                         [$directory.$new_answer],
 3099:                         [$symb,$env{'request.course.id'},'graded']);
 3100:                 }
 3101:             }
 3102:             $$record{$key} = join(',',@versioned_portfiles);
 3103:             push(@returned_keys,$key);
 3104:         }
 3105:     } 
 3106:     return (@returned_keys);   
 3107: }
 3108: 
 3109: sub get_next_version {
 3110:     my ($answer_name, $answer_ext, $dir_list) = @_;
 3111:     my $version;
 3112:     foreach my $row (@$dir_list) {
 3113:         my ($file) = split(/\&/,$row,2);
 3114:         my ($file_name,$file_version,$file_ext) =
 3115: 	    &file_name_version_ext($file);
 3116:         if (($file_name eq $answer_name) && 
 3117: 	    ($file_ext eq $answer_ext)) {
 3118:                 # gets here if filename and extension match, regardless of version
 3119:                 if ($file_version ne '') {
 3120:                 # a versioned file is found  so save it for later
 3121:                 if ($file_version > $version) {
 3122: 		    $version = $file_version;
 3123: 	        }
 3124:             }
 3125:         }
 3126:     } 
 3127:     $version ++;
 3128:     return($version);
 3129: }
 3130: 
 3131: sub version_selected_portfile {
 3132:     my ($domain,$stu_name,$directory,$file_name,$version) = @_;
 3133:     my ($answer_name,$answer_ver,$answer_ext) =
 3134:         &file_name_version_ext($file_name);
 3135:     my $new_answer;
 3136:     $env{'form.copy'} = &Apache::lonnet::getfile("/uploaded/$domain/$stu_name/portfolio$directory$file_name");
 3137:     if($env{'form.copy'} eq '-1') {
 3138:         $new_answer = 'problem getting file';
 3139:     } else {
 3140:         $new_answer = $answer_name.'.'.$version.'.'.$answer_ext;
 3141:         my $copy_result = &Apache::lonnet::finishuserfileupload(
 3142:                             $stu_name,$domain,'copy',
 3143: 		        '/portfolio'.$directory.$new_answer);
 3144:     }    
 3145:     return ($new_answer);
 3146: }
 3147: 
 3148: sub file_name_version_ext {
 3149:     my ($file)=@_;
 3150:     my @file_parts = split(/\./, $file);
 3151:     my ($name,$version,$ext);
 3152:     if (@file_parts > 1) {
 3153: 	$ext=pop(@file_parts);
 3154: 	if (@file_parts > 1 && $file_parts[-1] =~ /^\d+$/) {
 3155: 	    $version=pop(@file_parts);
 3156: 	}
 3157: 	$name=join('.',@file_parts);
 3158:     } else {
 3159: 	$name=join('.',@file_parts);
 3160:     }
 3161:     return($name,$version,$ext);
 3162: }
 3163: 
 3164: #--------------------------------------------------------------------------------------
 3165: #
 3166: #-------------------------- Next few routines handles grading by section or whole class
 3167: #
 3168: #--- Javascript to handle grading by section or whole class
 3169: sub viewgrades_js {
 3170:     my ($request) = shift;
 3171: 
 3172:     my $alertmsg = &mt('A number equal or greater than 0 is expected. Entered value = ');
 3173:     $request->print(<<VIEWJAVASCRIPT);
 3174: <script type="text/javascript" language="javascript">
 3175:    function writePoint(partid,weight,point) {
 3176: 	var radioButton = document.classgrade["RADVAL_"+partid];
 3177: 	var textbox = document.classgrade["TEXTVAL_"+partid];
 3178: 	if (point == "textval") {
 3179: 	    point = document.classgrade["TEXTVAL_"+partid].value;
 3180: 	    if (isNaN(point) || parseFloat(point) < 0) {
 3181: 		alert("$alertmsg"+parseFloat(point));
 3182: 		var resetbox = false;
 3183: 		for (var i=0; i<radioButton.length; i++) {
 3184: 		    if (radioButton[i].checked) {
 3185: 			textbox.value = i;
 3186: 			resetbox = true;
 3187: 		    }
 3188: 		}
 3189: 		if (!resetbox) {
 3190: 		    textbox.value = "";
 3191: 		}
 3192: 		return;
 3193: 	    }
 3194: 	    if (parseFloat(point) > parseFloat(weight)) {
 3195: 		var resp = confirm("You entered a value ("+parseFloat(point)+
 3196: 				   ") greater than the weight for the part. Accept?");
 3197: 		if (resp == false) {
 3198: 		    textbox.value = "";
 3199: 		    return;
 3200: 		}
 3201: 	    }
 3202: 	    for (var i=0; i<radioButton.length; i++) {
 3203: 		radioButton[i].checked=false;
 3204: 		if (parseFloat(point) == i) {
 3205: 		    radioButton[i].checked=true;
 3206: 		}
 3207: 	    }
 3208: 
 3209: 	} else {
 3210: 	    textbox.value = parseFloat(point);
 3211: 	}
 3212: 	for (i=0;i<document.classgrade.total.value;i++) {
 3213: 	    var user = document.classgrade["ctr"+i].value;
 3214: 	    user = user.replace(new RegExp(':', 'g'),"_");
 3215: 	    var scorename = document.classgrade["GD_"+user+"_"+partid+"_awarded"];
 3216: 	    var saveval   = document.classgrade["GD_"+user+"_"+partid+"_solved_s"].value;
 3217: 	    var selname   = document.classgrade["GD_"+user+"_"+partid+"_solved"];
 3218: 	    if (saveval != "correct") {
 3219: 		scorename.value = point;
 3220: 		if (selname[0].selected != true) {
 3221: 		    selname[0].selected = true;
 3222: 		}
 3223: 	    }
 3224: 	}
 3225: 	document.classgrade["SELVAL_"+partid][0].selected = true;
 3226:     }
 3227: 
 3228:     function writeRadText(partid,weight) {
 3229: 	var selval   = document.classgrade["SELVAL_"+partid];
 3230: 	var radioButton = document.classgrade["RADVAL_"+partid];
 3231:         var override = document.classgrade["FORCE_"+partid].checked;
 3232: 	var textbox = document.classgrade["TEXTVAL_"+partid];
 3233: 	if (selval[1].selected || selval[2].selected) {
 3234: 	    for (var i=0; i<radioButton.length; i++) {
 3235: 		radioButton[i].checked=false;
 3236: 
 3237: 	    }
 3238: 	    textbox.value = "";
 3239: 
 3240: 	    for (i=0;i<document.classgrade.total.value;i++) {
 3241: 		var user = document.classgrade["ctr"+i].value;
 3242: 		user = user.replace(new RegExp(':', 'g'),"_");
 3243: 		var scorename = document.classgrade["GD_"+user+"_"+partid+"_awarded"];
 3244: 		var saveval   = document.classgrade["GD_"+user+"_"+partid+"_solved_s"].value;
 3245: 		var selname   = document.classgrade["GD_"+user+"_"+partid+"_solved"];
 3246: 		if ((saveval != "correct") || override) {
 3247: 		    scorename.value = "";
 3248: 		    if (selval[1].selected) {
 3249: 			selname[1].selected = true;
 3250: 		    } else {
 3251: 			selname[2].selected = true;
 3252: 			if (Number(document.classgrade["GD_"+user+"_"+partid+"_tries"].value)) 
 3253: 			{document.classgrade["GD_"+user+"_"+partid+"_tries"].value = '0';}
 3254: 		    }
 3255: 		}
 3256: 	    }
 3257: 	} else {
 3258: 	    for (i=0;i<document.classgrade.total.value;i++) {
 3259: 		var user = document.classgrade["ctr"+i].value;
 3260: 		user = user.replace(new RegExp(':', 'g'),"_");
 3261: 		var scorename = document.classgrade["GD_"+user+"_"+partid+"_awarded"];
 3262: 		var saveval   = document.classgrade["GD_"+user+"_"+partid+"_solved_s"].value;
 3263: 		var selname   = document.classgrade["GD_"+user+"_"+partid+"_solved"];
 3264: 		if ((saveval != "correct") || override) {
 3265: 		    scorename.value = document.classgrade["GD_"+user+"_"+partid+"_awarded_s"].value;
 3266: 		    selname[0].selected = true;
 3267: 		}
 3268: 	    }
 3269: 	}	    
 3270:     }
 3271: 
 3272:     function changeSelect(partid,user) {
 3273: 	var selval = document.classgrade["GD_"+user+'_'+partid+"_solved"];
 3274: 	var textbox = document.classgrade["GD_"+user+'_'+partid+"_awarded"];
 3275: 	var point  = textbox.value;
 3276: 	var weight = document.classgrade["weight_"+partid].value;
 3277: 
 3278: 	if (isNaN(point) || parseFloat(point) < 0) {
 3279: 	    alert("$alertmsg"+parseFloat(point));
 3280: 	    textbox.value = "";
 3281: 	    return;
 3282: 	}
 3283: 	if (parseFloat(point) > parseFloat(weight)) {
 3284: 	    var resp = confirm("You entered a value ("+parseFloat(point)+
 3285: 			       ") greater than the weight of the part. Accept?");
 3286: 	    if (resp == false) {
 3287: 		textbox.value = "";
 3288: 		return;
 3289: 	    }
 3290: 	}
 3291: 	selval[0].selected = true;
 3292:     }
 3293: 
 3294:     function changeOneScore(partid,user) {
 3295: 	var selval = document.classgrade["GD_"+user+'_'+partid+"_solved"];
 3296: 	if (selval[1].selected || selval[2].selected) {
 3297: 	    document.classgrade["GD_"+user+'_'+partid+"_awarded"].value = "";
 3298: 	    if (selval[2].selected) {
 3299: 		document.classgrade["GD_"+user+'_'+partid+"_tries"].value = "0";
 3300: 	    }
 3301:         }
 3302:     }
 3303: 
 3304:     function resetEntry(numpart) {
 3305: 	for (ctpart=0;ctpart<numpart;ctpart++) {
 3306: 	    var partid = document.classgrade["partid_"+ctpart].value;
 3307: 	    var radioButton = document.classgrade["RADVAL_"+partid];
 3308: 	    var textbox = document.classgrade["TEXTVAL_"+partid];
 3309: 	    var selval  = document.classgrade["SELVAL_"+partid];
 3310: 	    for (var i=0; i<radioButton.length; i++) {
 3311: 		radioButton[i].checked=false;
 3312: 
 3313: 	    }
 3314: 	    textbox.value = "";
 3315: 	    selval[0].selected = true;
 3316: 
 3317: 	    for (i=0;i<document.classgrade.total.value;i++) {
 3318: 		var user = document.classgrade["ctr"+i].value;
 3319: 		user = user.replace(new RegExp(':', 'g'),"_");
 3320: 		var resetscore = document.classgrade["GD_"+user+"_"+partid+"_awarded"];
 3321: 		resetscore.value = document.classgrade["GD_"+user+"_"+partid+"_awarded_s"].value;
 3322: 		var resettries = document.classgrade["GD_"+user+"_"+partid+"_tries"];
 3323: 		resettries.value = document.classgrade["GD_"+user+"_"+partid+"_tries_s"].value;
 3324: 		var saveselval   = document.classgrade["GD_"+user+"_"+partid+"_solved_s"].value;
 3325: 		var selname   = document.classgrade["GD_"+user+"_"+partid+"_solved"];
 3326: 		if (saveselval == "excused") {
 3327: 		    if (selname[1].selected == false) { selname[1].selected = true;}
 3328: 		} else {
 3329: 		    if (selname[0].selected == false) {selname[0].selected = true};
 3330: 		}
 3331: 	    }
 3332: 	}
 3333:     }
 3334: 
 3335: </script>
 3336: VIEWJAVASCRIPT
 3337: }
 3338: 
 3339: #--- show scores for a section or whole class w/ option to change/update a score
 3340: sub viewgrades {
 3341:     my ($request) = shift;
 3342:     &viewgrades_js($request);
 3343: 
 3344:     my ($symb) = &get_symb($request);
 3345:     #need to make sure we have the correct data for later EXT calls, 
 3346:     #thus invalidate the cache
 3347:     &Apache::lonnet::devalidatecourseresdata(
 3348:                  $env{'course.'.$env{'request.course.id'}.'.num'},
 3349:                  $env{'course.'.$env{'request.course.id'}.'.domain'});
 3350:     &Apache::lonnet::clear_EXT_cache_status();
 3351: 
 3352:     my $result='<h3><span class="LC_info">'.&mt('Manual Grading').'</span></h3>';
 3353:     $result.='<h4>'.&mt('<b>Current Resource: </b>[_1]',$env{'form.probTitle'}).'</h4>'."\n";
 3354: 
 3355:     #view individual student submission form - called using Javascript viewOneStudent
 3356:     $result.=&jscriptNform($symb);
 3357: 
 3358:     #beginning of class grading form
 3359:     my $stu_status = join(':',&Apache::loncommon::get_env_multiple('form.Status'));
 3360:     $result.= '<form action="/adm/grades" method="post" name="classgrade">'."\n".
 3361: 	'<input type="hidden" name="symb"    value="'.&Apache::lonenc::check_encrypt($symb).'" />'."\n".
 3362: 	'<input type="hidden" name="command" value="editgrades" />'."\n".
 3363: 	&build_section_inputs().
 3364: 	'<input type="hidden" name="saveState" value="'.$env{'form.saveState'}.'" />'."\n".
 3365: 	'<input type="hidden" name="Status" value="'.$env{'stu_status'}.'" />'."\n".
 3366: 	'<input type="hidden" name="probTitle" value="'.$env{'form.probTitle'}.'" />'."\n";
 3367: 
 3368:     my ($common_header,$specific_header);
 3369:     if ($env{'form.section'} eq 'all') {
 3370: 	$common_header = &mt('Assign Common Grade to Class');
 3371:         $specific_header = &mt('Assign Grade to Specific Students in Class');
 3372:     } elsif ($env{'form.section'} eq 'none') {
 3373:         $common_header = &mt('Assign Common Grade to Students in no Section');
 3374: 	$specific_header = &mt('Assign Grade to Specific Students in no Section');
 3375:     } else {
 3376:         my $section_display = join (", ",&Apache::loncommon::get_env_multiple('form.section'));
 3377:         $common_header = &mt('Assign Common Grade to Students in Section(s) [_1]',$section_display);
 3378: 	$specific_header = &mt('Assign Grade to Specific Students in Section(s) [_1]',$section_display);
 3379:     }
 3380:     $result.= '<h3>'.$common_header.'</h3>'.&Apache::loncommon::start_data_table();
 3381:     #radio buttons/text box for assigning points for a section or class.
 3382:     #handles different parts of a problem
 3383:     my $res_error;
 3384:     my ($partlist,$handgrade,$responseType) = &response_type($symb,\$res_error);
 3385:     if ($res_error) {
 3386:         return &navmap_errormsg();
 3387:     }
 3388:     my %weight = ();
 3389:     my $ctsparts = 0;
 3390:     my %seen = ();
 3391:     my @part_response_id = &flatten_responseType($responseType);
 3392:     foreach my $part_response_id (@part_response_id) {
 3393:     	my ($partid,$respid) = @{ $part_response_id };
 3394: 	my $part_resp = join('_',@{ $part_response_id });
 3395: 	next if $seen{$partid};
 3396: 	$seen{$partid}++;
 3397: 	my $handgrade=$$handgrade{$part_resp};
 3398: 	my $wgt = &Apache::lonnet::EXT('resource.'.$partid.'.weight',$symb);
 3399: 	$weight{$partid} = $wgt eq '' ? '1' : $wgt;
 3400: 
 3401: 	my $display_part=&get_display_part($partid,$symb);
 3402: 	my $radio.='<table border="0"><tr>';  
 3403: 	my $ctr = 0;
 3404: 	while ($ctr<=$weight{$partid}) { # display radio buttons in a nice table 10 across
 3405: 	    $radio.= '<td><label><input type="radio" name="RADVAL_'.$partid.'" '.
 3406: 		'onclick="javascript:writePoint(\''.$partid.'\','.$weight{$partid}.
 3407: 		','.$ctr.')" />'.$ctr."</label></td>\n";
 3408: 	    $result.=(($ctr+1)%10 == 0 ? '</tr><tr>' : '');
 3409: 	    $ctr++;
 3410: 	}
 3411: 	$radio.='</tr></table>';
 3412: 	my $line = '<input type="text" name="TEXTVAL_'.
 3413: 	    $partid.'" size="4" '.'onchange="javascript:writePoint(\''.
 3414: 		$partid.'\','.$weight{$partid}.',\'textval\')" /> /'.
 3415: 	    $weight{$partid}.' '.&mt('(problem weight)').'</td>'."\n";
 3416: 	$line.= '<td><b>'.&mt('Grade Status').':</b><select name="SELVAL_'.$partid.'"'.
 3417: 	    'onchange="javascript:writeRadText(\''.$partid.'\','.
 3418: 		$weight{$partid}.')"> '.
 3419: 	    '<option selected="selected"> </option>'.
 3420: 	    '<option value="excused">'.&mt('excused').'</option>'.
 3421: 	    '<option value="reset status">'.&mt('reset status').'</option>'.
 3422: 	    '</select></td>'.
 3423:             '<td><label><input type="checkbox" name="FORCE_'.$partid.'" />'.&mt('Override "Correct"').'</label>';
 3424: 	$line.='<input type="hidden" name="partid_'.
 3425: 	    $ctsparts.'" value="'.$partid.'" />'."\n";
 3426: 	$line.='<input type="hidden" name="weight_'.
 3427: 	    $partid.'" value="'.$weight{$partid}.'" />'."\n";
 3428: 
 3429: 	$result.=
 3430: 	    &Apache::loncommon::start_data_table_row()."\n".
 3431: 	    '<td><b>'.&mt('Part:').'</b></td><td>'.$display_part.'</td><td><b>'.&mt('Points:').'</b></td><td>'.$radio.'</td><td>'.&mt('or').'</td><td>'.$line.'</td>'.
 3432: 	    &Apache::loncommon::end_data_table_row()."\n";
 3433: 	$ctsparts++;
 3434:     }
 3435:     $result.=&Apache::loncommon::end_data_table()."\n".
 3436: 	'<input type="hidden" name="totalparts" value="'.$ctsparts.'" />';
 3437:     $result.='<input type="button" value="'.&mt('Revert to Default').'" '.
 3438: 	'onclick="javascript:resetEntry('.$ctsparts.');" />';
 3439: 
 3440:     #table listing all the students in a section/class
 3441:     #header of table
 3442:     $result.= '<h3>'.$specific_header.'</h3>'.
 3443:               &Apache::loncommon::start_data_table().
 3444: 	      &Apache::loncommon::start_data_table_header_row().
 3445: 	      '<th>'.&mt('No.').'</th>'.
 3446: 	      '<th>'.&nameUserString('header')."</th>\n";
 3447:     my $partserror;
 3448:     my (@parts) = sort(&getpartlist($symb,\$partserror));
 3449:     if ($partserror) {
 3450:         return &navmap_errormsg();
 3451:     }
 3452:     my (undef,undef,$url)=&Apache::lonnet::decode_symb($symb);
 3453:     my @partids = ();
 3454:     foreach my $part (@parts) {
 3455: 	my $display=&Apache::lonnet::metadata($url,$part.'.display');
 3456:         my $narrowtext = &mt('Tries');
 3457: 	$display =~ s|^Number of Attempts|$narrowtext <br />|; # makes the column narrower
 3458: 	if  (!$display) { $display = &Apache::lonnet::metadata($url,$part.'.name'); }
 3459: 	my ($partid) = &split_part_type($part);
 3460:         push(@partids,$partid);
 3461: 	my $display_part=&get_display_part($partid,$symb);
 3462: 	if ($display =~ /^Partial Credit Factor/) {
 3463: 	    $result.='<th>'.
 3464: 		&mt('Score Part: [_1]<br /> (weight = [_2])',
 3465: 		    $display_part,$weight{$partid}).'</th>'."\n";
 3466: 	    next;
 3467: 	    
 3468: 	} else {
 3469: 	    if ($display =~ /Problem Status/) {
 3470: 		my $grade_status_mt = &mt('Grade Status');
 3471: 		$display =~ s{Problem Status}{$grade_status_mt<br />};
 3472: 	    }
 3473: 	    my $part_mt = &mt('Part:');
 3474: 	    $display =~s{\[Part: \Q$partid\E\]}{$part_mt $display_part};
 3475: 	}
 3476: 
 3477: 	$result.='<th>'.$display.'</th>'."\n";
 3478:     }
 3479:     $result.=&Apache::loncommon::end_data_table_header_row();
 3480: 
 3481:     my %last_resets = 
 3482: 	&get_last_resets($symb,$env{'request.course.id'},\@partids);
 3483: 
 3484:     #get info for each student
 3485:     #list all the students - with points and grade status
 3486:     my (undef,undef,$fullname) = &getclasslist($env{'form.section'},'1');
 3487:     my $ctr = 0;
 3488:     foreach (sort 
 3489: 	     {
 3490: 		 if (lc($$fullname{$a}) ne lc($$fullname{$b})) {
 3491: 		     return (lc($$fullname{$a}) cmp lc($$fullname{$b}));
 3492: 		 }
 3493: 		 return $a cmp $b;
 3494: 	     } (keys(%$fullname))) {
 3495: 	$ctr++;
 3496: 	$result.=&viewstudentgrade($symb,$env{'request.course.id'},
 3497: 				   $_,$$fullname{$_},\@parts,\%weight,$ctr,\%last_resets);
 3498:     }
 3499:     $result.=&Apache::loncommon::end_data_table();
 3500:     $result.='<input type="hidden" name="total" value="'.$ctr.'" />'."\n";
 3501:     $result.='<input type="button" value="'.&mt('Save').'" '.
 3502: 	'onclick="javascript:submit();" target="_self" /></form>'."\n";
 3503:     if (scalar(%$fullname) eq 0) {
 3504: 	my $colspan=3+scalar(@parts);
 3505: 	my $section_display = join (", ",&Apache::loncommon::get_env_multiple('form.section'));
 3506:         my $stu_status = join(' or ',&Apache::loncommon::get_env_multiple('form.Status'));
 3507: 	$result='<span class="LC_warning">'.
 3508: 	    &mt('There are no students in section(s) [_1] with enrollment status [_2] to modify or grade.',
 3509: 	        $section_display, $stu_status).
 3510: 	    '</span>';
 3511:     }
 3512:     $result.=&show_grading_menu_form($symb);
 3513:     return $result;
 3514: }
 3515: 
 3516: #--- call by previous routine to display each student
 3517: sub viewstudentgrade {
 3518:     my ($symb,$courseid,$student,$fullname,$parts,$weight,$ctr,$last_resets) = @_;
 3519:     my ($uname,$udom) = split(/:/,$student);
 3520:     my %record=&Apache::lonnet::restore($symb,$courseid,$udom,$uname);
 3521:     my %aggregates = (); 
 3522:     my $result=&Apache::loncommon::start_data_table_row().'<td align="right">'.
 3523: 	'<input type="hidden" name="ctr'.($ctr-1).'" value="'.$student.'" />'.
 3524: 	"\n".$ctr.'&nbsp;</td><td>&nbsp;'.
 3525: 	'<a href="javascript:viewOneStudent(\''.$uname.'\',\''.$udom.
 3526: 	'\');" target="_self">'.$fullname.'</a> '.
 3527: 	'<span class="LC_internal_info">('.$uname.($env{'user.domain'} eq $udom ? '' : ':'.$udom).')</span></td>'."\n";
 3528:     $student=~s/:/_/; # colon doen't work in javascript for names
 3529:     foreach my $apart (@$parts) {
 3530: 	my ($part,$type) = &split_part_type($apart);
 3531: 	my $score=$record{"resource.$part.$type"};
 3532:         $result.='<td align="center">';
 3533:         my ($aggtries,$totaltries);
 3534:         unless (exists($aggregates{$part})) {
 3535: 	    $totaltries = $record{'resource.'.$part.'.tries'};
 3536: 
 3537: 	    $aggtries = $totaltries;
 3538:             if ($$last_resets{$part}) {  
 3539:                 $aggtries = &get_num_tries(\%record,$$last_resets{$part},
 3540: 					   $part);
 3541:             }
 3542:             $result.='<input type="hidden" name="'.
 3543:                 'GD_'.$student.'_'.$part.'_aggtries" value="'.$aggtries.'" />'."\n";
 3544:             $result.='<input type="hidden" name="'.
 3545:                 'GD_'.$student.'_'.$part.'_totaltries" value="'.$totaltries.'" />'."\n";
 3546:             $aggregates{$part} = 1;
 3547:         }
 3548: 	if ($type eq 'awarded') {
 3549: 	    my $pts = $score eq '' ? '' : &compute_points($score,$$weight{$part});
 3550: 	    $result.='<input type="hidden" name="'.
 3551: 		'GD_'.$student.'_'.$part.'_awarded_s" value="'.$pts.'" />'."\n";
 3552: 	    $result.='<input type="text" name="'.
 3553: 		'GD_'.$student.'_'.$part.'_awarded" '.
 3554:                 'onchange="javascript:changeSelect(\''.$part.'\',\''.$student.
 3555: 		'\')" value="'.$pts.'" size="4" /></td>'."\n";
 3556: 	} elsif ($type eq 'solved') {
 3557: 	    my ($status,$foo)=split(/_/,$score,2);
 3558: 	    $status = 'nothing' if ($status eq '');
 3559: 	    $result.='<input type="hidden" name="'.'GD_'.$student.'_'.
 3560: 		$part.'_solved_s" value="'.$status.'" />'."\n";
 3561: 	    $result.='&nbsp;<select name="'.
 3562: 		'GD_'.$student.'_'.$part.'_solved" '.
 3563:                 'onchange="javascript:changeOneScore(\''.$part.'\',\''.$student.'\')" >'."\n";
 3564: 	    $result.= (($status eq 'excused') ? '<option> </option><option selected="selected" value="excused">'.&mt('excused').'</option>' 
 3565: 		: '<option selected="selected"> </option><option value="excused">'.&mt('excused').'</option>')."\n";
 3566: 	    $result.='<option value="reset status">'.&mt('reset status').'</option>';
 3567: 	    $result.="</select>&nbsp;</td>\n";
 3568: 	} else {
 3569: 	    $result.='<input type="hidden" name="'.
 3570: 		'GD_'.$student.'_'.$part.'_'.$type.'_s" value="'.$score.'" />'.
 3571: 		    "\n";
 3572: 	    $result.='<input type="text" name="'.
 3573: 		'GD_'.$student.'_'.$part.'_'.$type.'" '.
 3574: 		'value="'.$score.'" size="4" /></td>'."\n";
 3575: 	}
 3576:     }
 3577:     $result.=&Apache::loncommon::end_data_table_row();
 3578:     return $result;
 3579: }
 3580: 
 3581: #--- change scores for all the students in a section/class
 3582: #    record does not get update if unchanged
 3583: sub editgrades {
 3584:     my ($request) = @_;
 3585: 
 3586:     my $symb=&get_symb($request);
 3587:     my $section_display = join (", ",&Apache::loncommon::get_env_multiple('form.section'));
 3588:     my $title='<h2>'.&mt('Current Grade Status').'</h2>';
 3589:     $title.='<h4>'.&mt('<b>Current Resource: </b>[_1]',$env{'form.probTitle'}).'</h4>'."\n";
 3590:     $title.='<h4>'.&mt('<b>Section: </b>[_1]',$section_display).'</h4>'."\n";
 3591: 
 3592:     my $result= &Apache::loncommon::start_data_table().
 3593: 	&Apache::loncommon::start_data_table_header_row().
 3594: 	'<th rowspan="2" valign="middle">'.&mt('No.').'</th>'.
 3595: 	'<th rowspan="2" valign="middle">'.&nameUserString('header')."</th>\n";
 3596:     my %scoreptr = (
 3597: 		    'correct'  =>'correct_by_override',
 3598: 		    'incorrect'=>'incorrect_by_override',
 3599: 		    'excused'  =>'excused',
 3600: 		    'ungraded' =>'ungraded_attempted',
 3601:                     'credited' =>'credit_attempted',
 3602: 		    'nothing'  => '',
 3603: 		    );
 3604:     my ($classlist,undef,$fullname) = &getclasslist($env{'form.section'},'0');
 3605: 
 3606:     my (@partid);
 3607:     my %weight = ();
 3608:     my %columns = ();
 3609:     my ($i,$ctr,$count,$rec_update) = (0,0,0,0);
 3610: 
 3611:     my $partserror;
 3612:     my (@parts) = sort(&getpartlist($symb,\$partserror));
 3613:     if ($partserror) {
 3614:         return &navmap_errormsg();
 3615:     }
 3616:     my $header;
 3617:     while ($ctr < $env{'form.totalparts'}) {
 3618: 	my $partid = $env{'form.partid_'.$ctr};
 3619: 	push(@partid,$partid);
 3620: 	$weight{$partid} = $env{'form.weight_'.$partid};
 3621: 	$ctr++;
 3622:     }
 3623:     my (undef,undef,$url) = &Apache::lonnet::decode_symb($symb);
 3624:     foreach my $partid (@partid) {
 3625: 	$header .= '<th align="center">'.&mt('Old Score').'</th>'.
 3626: 	    '<th align="center">'.&mt('New Score').'</th>';
 3627: 	$columns{$partid}=2;
 3628: 	foreach my $stores (@parts) {
 3629: 	    my ($part,$type) = &split_part_type($stores);
 3630: 	    if ($part !~ m/^\Q$partid\E/) { next;}
 3631: 	    if ($type eq 'awarded' || $type eq 'solved') { next; }
 3632: 	    my $display=&Apache::lonnet::metadata($url,$stores.'.display');
 3633: 	    $display =~ s/\[Part: \Q$part\E\]//;
 3634:             my $narrowtext = &mt('Tries');
 3635: 	    $display =~ s/Number of Attempts/$narrowtext/;
 3636: 	    $header .= '<th align="center">'.&mt('Old').' '.$display.'</th>'.
 3637: 		'<th align="center">'.&mt('New').' '.$display.'</th>';
 3638: 	    $columns{$partid}+=2;
 3639: 	}
 3640:     }
 3641:     foreach my $partid (@partid) {
 3642: 	my $display_part=&get_display_part($partid,$symb);
 3643: 	$result .= '<th colspan="'.$columns{$partid}.'" align="center">'.
 3644: 	    &mt('Part: [_1] (Weight = [_2])',$display_part,$weight{$partid}).
 3645: 	    '</th>';
 3646: 
 3647:     }
 3648:     $result .= &Apache::loncommon::end_data_table_header_row().
 3649: 	&Apache::loncommon::start_data_table_header_row().
 3650: 	$header.
 3651: 	&Apache::loncommon::end_data_table_header_row();
 3652:     my @noupdate;
 3653:     my ($updateCtr,$noupdateCtr) = (1,1);
 3654:     for ($i=0; $i<$env{'form.total'}; $i++) {
 3655: 	my $line;
 3656: 	my $user = $env{'form.ctr'.$i};
 3657: 	my ($uname,$udom)=split(/:/,$user);
 3658: 	my %newrecord;
 3659: 	my $updateflag = 0;
 3660: 	$line .= '<td>'.&nameUserString(undef,$$fullname{$user},$uname,$udom).'</td>';
 3661: 	my $usec=$classlist->{"$uname:$udom"}[5];
 3662: 	if (!&canmodify($usec)) {
 3663: 	    my $numcols=scalar(@partid)*4+2;
 3664: 	    push(@noupdate,
 3665: 		 $line."<td colspan=\"$numcols\"><span class=\"LC_warning\">".
 3666: 		 &mt('Not allowed to modify student')."</span></td></tr>");
 3667: 	    next;
 3668: 	}
 3669:         my %aggregate = ();
 3670:         my $aggregateflag = 0;
 3671: 	$user=~s/:/_/; # colon doen't work in javascript for names
 3672: 	foreach (@partid) {
 3673: 	    my $old_aw    = $env{'form.GD_'.$user.'_'.$_.'_awarded_s'};
 3674: 	    my $old_part_pcr = $old_aw/($weight{$_} ne '0' ? $weight{$_}:1);
 3675: 	    my $old_part  = $old_aw eq '' ? '' : $old_part_pcr;
 3676: 	    my $old_score = $scoreptr{$env{'form.GD_'.$user.'_'.$_.'_solved_s'}};
 3677: 	    my $awarded   = $env{'form.GD_'.$user.'_'.$_.'_awarded'};
 3678: 	    my $pcr       = $awarded/($weight{$_} ne '0' ? $weight{$_} : 1);
 3679: 	    my $partial   = $awarded eq '' ? '' : $pcr;
 3680: 	    my $score;
 3681: 	    if ($partial eq '') {
 3682: 		$score = $scoreptr{$env{'form.GD_'.$user.'_'.$_.'_solved_s'}};
 3683: 	    } elsif ($partial > 0) {
 3684: 		$score = 'correct_by_override';
 3685: 	    } elsif ($partial == 0) {
 3686: 		$score = 'incorrect_by_override';
 3687: 	    }
 3688: 	    my $dropMenu = $env{'form.GD_'.$user.'_'.$_.'_solved'};
 3689: 	    $score = 'excused' if (($dropMenu eq 'excused') && ($score ne 'excused'));
 3690: 
 3691: 	    $newrecord{'resource.'.$_.'.regrader'}=
 3692: 		"$env{'user.name'}:$env{'user.domain'}";
 3693: 	    if ($dropMenu eq 'reset status' &&
 3694: 		$old_score ne '') { # ignore if no previous attempts => nothing to reset
 3695: 		$newrecord{'resource.'.$_.'.tries'} = '';
 3696: 		$newrecord{'resource.'.$_.'.solved'} = '';
 3697: 		$newrecord{'resource.'.$_.'.award'} = '';
 3698: 		$newrecord{'resource.'.$_.'.awarded'} = '';
 3699: 		$updateflag = 1;
 3700:                 if ($env{'form.GD_'.$user.'_'.$_.'_aggtries'} > 0) {
 3701:                     my $aggtries = $env{'form.GD_'.$user.'_'.$_.'_aggtries'};
 3702:                     my $totaltries = $env{'form.GD_'.$user.'_'.$_.'_totaltries'};
 3703:                     my $solvedstatus = $env{'form.GD_'.$user.'_'.$_.'_solved_s'};
 3704:                     &decrement_aggs($symb,$_,\%aggregate,$aggtries,$totaltries,$solvedstatus);
 3705:                     $aggregateflag = 1;
 3706:                 }
 3707: 	    } elsif (!($old_part eq $partial && $old_score eq $score)) {
 3708: 		$updateflag = 1;
 3709: 		$newrecord{'resource.'.$_.'.awarded'}  = $partial if $partial ne '';
 3710: 		$newrecord{'resource.'.$_.'.solved'}   = $score;
 3711: 		$rec_update++;
 3712: 	    }
 3713: 
 3714: 	    $line .= '<td align="center">'.$old_aw.'&nbsp;</td>'.
 3715: 		'<td align="center">'.$awarded.
 3716: 		($score eq 'excused' ? $score : '').'&nbsp;</td>';
 3717: 
 3718: 
 3719: 	    my $partid=$_;
 3720: 	    foreach my $stores (@parts) {
 3721: 		my ($part,$type) = &split_part_type($stores);
 3722: 		if ($part !~ m/^\Q$partid\E/) { next;}
 3723: 		if ($type eq 'awarded' || $type eq 'solved') { next; }
 3724: 		my $old_aw    = $env{'form.GD_'.$user.'_'.$part.'_'.$type.'_s'};
 3725: 		my $awarded   = $env{'form.GD_'.$user.'_'.$part.'_'.$type};
 3726: 		if ($awarded ne '' && $awarded ne $old_aw) {
 3727: 		    $newrecord{'resource.'.$part.'.'.$type}= $awarded;
 3728: 		    $newrecord{'resource.'.$part.'.regrader'}="$env{'user.name'}:$env{'user.domain'}";
 3729: 		    $updateflag=1;
 3730: 		}
 3731: 		$line .= '<td align="center">'.$old_aw.'&nbsp;</td>'.
 3732: 		    '<td align="center">'.$awarded.'&nbsp;</td>';
 3733: 	    }
 3734: 	}
 3735: 	$line.="\n";
 3736: 
 3737: 	my $cdom = $env{'course.'.$env{'request.course.id'}.'.domain'};
 3738: 	my $cnum = $env{'course.'.$env{'request.course.id'}.'.num'};
 3739: 
 3740: 	if ($updateflag) {
 3741: 	    $count++;
 3742: 	    &Apache::lonnet::cstore(\%newrecord,$symb,$env{'request.course.id'},
 3743: 				    $udom,$uname);
 3744: 
 3745: 	    if (&Apache::bridgetask::in_queue('gradingqueue',$symb,$cdom,
 3746: 					      $cnum,$udom,$uname)) {
 3747: 		# need to figure out if should be in queue.
 3748: 		my %record =  
 3749: 		    &Apache::lonnet::restore($symb,$env{'request.course.id'},
 3750: 					     $udom,$uname);
 3751: 		my $all_graded = 1;
 3752: 		my $none_graded = 1;
 3753: 		foreach my $part (@parts) {
 3754: 		    if ( $record{'resource.'.$part.'.awarded'} eq '' ) {
 3755: 			$all_graded = 0;
 3756: 		    } else {
 3757: 			$none_graded = 0;
 3758: 		    }
 3759: 		}
 3760: 
 3761: 		if ($all_graded || $none_graded) {
 3762: 		    &Apache::bridgetask::remove_from_queue('gradingqueue',
 3763: 							   $symb,$cdom,$cnum,
 3764: 							   $udom,$uname);
 3765: 		}
 3766: 	    }
 3767: 
 3768: 	    $result.=&Apache::loncommon::start_data_table_row().
 3769: 		'<td align="right">&nbsp;'.$updateCtr.'&nbsp;</td>'.$line.
 3770: 		&Apache::loncommon::end_data_table_row();
 3771: 	    $updateCtr++;
 3772: 	} else {
 3773: 	    push(@noupdate,
 3774: 		 '<td align="right">&nbsp;'.$noupdateCtr.'&nbsp;</td>'.$line);
 3775: 	    $noupdateCtr++;
 3776: 	}
 3777:         if ($aggregateflag) {
 3778:             &Apache::lonnet::cinc('nohist_resourcetracker',\%aggregate,
 3779: 				  $cdom,$cnum);
 3780:         }
 3781:     }
 3782:     if (@noupdate) {
 3783: #	my $numcols=(scalar(@partid)*(scalar(@parts)-1)*2)+3;
 3784: 	my $numcols=scalar(@partid)*4+2;
 3785: 	$result .= &Apache::loncommon::start_data_table_row('LC_empty_row').
 3786: 	    '<td align="center" colspan="'.$numcols.'">'.
 3787: 	    &mt('No Changes Occurred For the Students Below').
 3788: 	    '</td>'.
 3789: 	    &Apache::loncommon::end_data_table_row();
 3790: 	foreach my $line (@noupdate) {
 3791: 	    $result.=
 3792: 		&Apache::loncommon::start_data_table_row().
 3793: 		$line.
 3794: 		&Apache::loncommon::end_data_table_row();
 3795: 	}
 3796:     }
 3797:     $result .= &Apache::loncommon::end_data_table().
 3798: 	&show_grading_menu_form($symb);
 3799:     my $msg = '<p><b>'.
 3800: 	&mt('Number of records updated = [_1] for [quant,_2,student].',
 3801: 	    $rec_update,$count).'</b><br />'.
 3802: 	'<b>'.&mt('Total number of students = [_1]',$env{'form.total'}).
 3803: 	'</b></p>';
 3804:     return $title.$msg.$result;
 3805: }
 3806: 
 3807: sub split_part_type {
 3808:     my ($partstr) = @_;
 3809:     my ($temp,@allparts)=split(/_/,$partstr);
 3810:     my $type=pop(@allparts);
 3811:     my $part=join('_',@allparts);
 3812:     return ($part,$type);
 3813: }
 3814: 
 3815: #------------- end of section for handling grading by section/class ---------
 3816: #
 3817: #----------------------------------------------------------------------------
 3818: 
 3819: 
 3820: #----------------------------------------------------------------------------
 3821: #
 3822: #-------------------------- Next few routines handles grading by csv upload
 3823: #
 3824: #--- Javascript to handle csv upload
 3825: sub csvupload_javascript_reverse_associate {
 3826:     my $error1=&mt('You need to specify the username or the student/employee ID');
 3827:     my $error2=&mt('You need to specify at least one grading field');
 3828:   return(<<ENDPICK);
 3829:   function verify(vf) {
 3830:     var foundsomething=0;
 3831:     var founduname=0;
 3832:     var foundID=0;
 3833:     for (i=0;i<=vf.nfields.value;i++) {
 3834:       tw=eval('vf.f'+i+'.selectedIndex');
 3835:       if (i==0 && tw!=0) { foundID=1; }
 3836:       if (i==1 && tw!=0) { founduname=1; }
 3837:       if (i!=0 && i!=1 && i!=2 && tw!=0) { foundsomething=1; }
 3838:     }
 3839:     if (founduname==0 && foundID==0) {
 3840: 	alert('$error1');
 3841: 	return;
 3842:     }
 3843:     if (foundsomething==0) {
 3844: 	alert('$error2');
 3845: 	return;
 3846:     }
 3847:     vf.submit();
 3848:   }
 3849:   function flip(vf,tf) {
 3850:     var nw=eval('vf.f'+tf+'.selectedIndex');
 3851:     var i;
 3852:     for (i=0;i<=vf.nfields.value;i++) {
 3853:       //can not pick the same destination field for both name and domain
 3854:       if (((i ==0)||(i ==1)) && 
 3855:           ((tf==0)||(tf==1)) && 
 3856:           (i!=tf) &&
 3857:           (eval('vf.f'+i+'.selectedIndex')==nw)) {
 3858:         eval('vf.f'+i+'.selectedIndex=0;')
 3859:       }
 3860:     }
 3861:   }
 3862: ENDPICK
 3863: }
 3864: 
 3865: sub csvupload_javascript_forward_associate {
 3866:     my $error1=&mt('You need to specify the username or the student/employee ID');
 3867:     my $error2=&mt('You need to specify at least one grading field');
 3868:   return(<<ENDPICK);
 3869:   function verify(vf) {
 3870:     var foundsomething=0;
 3871:     var founduname=0;
 3872:     var foundID=0;
 3873:     for (i=0;i<=vf.nfields.value;i++) {
 3874:       tw=eval('vf.f'+i+'.selectedIndex');
 3875:       if (tw==1) { foundID=1; }
 3876:       if (tw==2) { founduname=1; }
 3877:       if (tw>3) { foundsomething=1; }
 3878:     }
 3879:     if (founduname==0 && foundID==0) {
 3880: 	alert('$error1');
 3881: 	return;
 3882:     }
 3883:     if (foundsomething==0) {
 3884: 	alert('$error2');
 3885: 	return;
 3886:     }
 3887:     vf.submit();
 3888:   }
 3889:   function flip(vf,tf) {
 3890:     var nw=eval('vf.f'+tf+'.selectedIndex');
 3891:     var i;
 3892:     //can not pick the same destination field twice
 3893:     for (i=0;i<=vf.nfields.value;i++) {
 3894:       if ((i!=tf) && (eval('vf.f'+i+'.selectedIndex')==nw)) {
 3895:         eval('vf.f'+i+'.selectedIndex=0;')
 3896:       }
 3897:     }
 3898:   }
 3899: ENDPICK
 3900: }
 3901: 
 3902: sub csvuploadmap_header {
 3903:     my ($request,$symb,$datatoken,$distotal)= @_;
 3904:     my $javascript;
 3905:     if ($env{'form.upfile_associate'} eq 'reverse') {
 3906: 	$javascript=&csvupload_javascript_reverse_associate();
 3907:     } else {
 3908: 	$javascript=&csvupload_javascript_forward_associate();
 3909:     }
 3910: 
 3911:     my ($result) = &showResourceInfo($symb,$env{'form.probTitle'});
 3912:     my $checked=(($env{'form.noFirstLine'})?' checked="checked"':'');
 3913:     my $ignore=&mt('Ignore First Line');
 3914:     $symb = &Apache::lonenc::check_encrypt($symb);
 3915:     $request->print(<<ENDPICK);
 3916: <form method="post" enctype="multipart/form-data" action="/adm/grades" name="gradesupload">
 3917: <h3><span class="LC_info">Uploading Class Grades</span></h3>
 3918: $result
 3919: <hr />
 3920: <h3>Identify fields</h3>
 3921: Total number of records found in file: $distotal <hr />
 3922: Enter as many fields as you can. The system will inform you and bring you back
 3923: to this page if the data selected is insufficient to run your class.<hr />
 3924: <input type="button" value="Reverse Association" onclick="javascript:this.form.associate.value='Reverse Association';submit(this.form);" />
 3925: <label><input type="checkbox" name="noFirstLine" $checked />$ignore</label>
 3926: <input type="hidden" name="associate"  value="" />
 3927: <input type="hidden" name="phase"      value="three" />
 3928: <input type="hidden" name="datatoken"  value="$datatoken" />
 3929: <input type="hidden" name="fileupload" value="$env{'form.fileupload'}" />
 3930: <input type="hidden" name="upfiletype" value="$env{'form.upfiletype'}" />
 3931: <input type="hidden" name="upfile_associate" 
 3932:                                        value="$env{'form.upfile_associate'}" />
 3933: <input type="hidden" name="symb"       value="$symb" />
 3934: <input type="hidden" name="saveState"  value="$env{'form.saveState'}" />
 3935: <input type="hidden" name="probTitle"  value="$env{'form.probTitle'}" />
 3936: <input type="hidden" name="command"    value="csvuploadoptions" />
 3937: <hr />
 3938: <script type="text/javascript" language="Javascript">
 3939: $javascript
 3940: </script>
 3941: ENDPICK
 3942:     return '';
 3943: 
 3944: }
 3945: 
 3946: sub csvupload_fields {
 3947:     my ($symb,$errorref) = @_;
 3948:     my (@parts) = &getpartlist($symb,$errorref);
 3949:     if (ref($errorref)) {
 3950:         if ($$errorref) {
 3951:             return;
 3952:         }
 3953:     }
 3954: 
 3955:     my @fields=(['ID','Student/Employee ID'],
 3956: 		['username','Student Username'],
 3957: 		['domain','Student Domain']);
 3958:     my (undef,undef,$url) = &Apache::lonnet::decode_symb($symb);
 3959:     foreach my $part (sort(@parts)) {
 3960: 	my @datum;
 3961: 	my $display=&Apache::lonnet::metadata($url,$part.'.display');
 3962: 	my $name=$part;
 3963: 	if  (!$display) { $display = $name; }
 3964: 	@datum=($name,$display);
 3965: 	if ($name=~/^stores_(.*)_awarded/) {
 3966: 	    push(@fields,['stores_'.$1.'_points',"Points [Part: $1]"]);
 3967: 	}
 3968: 	push(@fields,\@datum);
 3969:     }
 3970:     return (@fields);
 3971: }
 3972: 
 3973: sub csvuploadmap_footer {
 3974:     my ($request,$i,$keyfields) =@_;
 3975:     $request->print(<<ENDPICK);
 3976: </table>
 3977: <input type="hidden" name="nfields" value="$i" />
 3978: <input type="hidden" name="keyfields" value="$keyfields" />
 3979: <input type="button" onclick="javascript:verify(this.form)" value="Assign Grades" /><br />
 3980: </form>
 3981: ENDPICK
 3982: }
 3983: 
 3984: sub checkforfile_js {
 3985:     my $alertmsg = &mt('Please use the browse button to select a file from your local directory.');
 3986:     my $result =<<CSVFORMJS;
 3987: <script type="text/javascript" language="javascript">
 3988:     function checkUpload(formname) {
 3989: 	if (formname.upfile.value == "") {
 3990: 	    alert("$alertmsg");
 3991: 	    return false;
 3992: 	}
 3993: 	formname.submit();
 3994:     }
 3995:     </script>
 3996: CSVFORMJS
 3997:     return $result;
 3998: }
 3999: 
 4000: sub upcsvScores_form {
 4001:     my ($request) = shift;
 4002:     my ($symb)=&get_symb($request);
 4003:     if (!$symb) {return '';}
 4004:     my $result=&checkforfile_js();
 4005:     $env{'form.probTitle'} = &Apache::lonnet::gettitle($symb);
 4006:     my ($table) = &showResourceInfo($symb,$env{'form.probTitle'});
 4007:     $result.=$table;
 4008:     $result.='<br /><table width="100%" border="0"><tr><td bgcolor="#777777">'."\n";
 4009:     $result.='<table width="100%" border="0"><tr bgcolor="#e6ffff"><td>'."\n";
 4010:     $result.='&nbsp;<b>'.&mt('Specify a file containing the class scores for current resource.').
 4011: 	'</b></td></tr>'."\n";
 4012:     $result.='<tr bgcolor=#ffffe6><td>'."\n";
 4013:     my $upload=&mt("Upload Scores");
 4014:     my $upfile_select=&Apache::loncommon::upfile_select_html();
 4015:     my $ignore=&mt('Ignore First Line');
 4016:     $symb = &Apache::lonenc::check_encrypt($symb);
 4017:     $result.=<<ENDUPFORM;
 4018: <form method="post" enctype="multipart/form-data" action="/adm/grades" name="gradesupload">
 4019: <input type="hidden" name="symb" value="$symb" />
 4020: <input type="hidden" name="command" value="csvuploadmap" />
 4021: <input type="hidden" name="probTitle" value="$env{'form.probTitle'}" />
 4022: <input type="hidden" name="saveState"  value="$env{'form.saveState'}" />
 4023: $upfile_select
 4024: <br /><input type="button" onclick="javascript:checkUpload(this.form);" value="$upload" />
 4025: <label><input type="checkbox" name="noFirstLine" />$ignore</label>
 4026: </form>
 4027: ENDUPFORM
 4028:     $result.=&Apache::loncommon::help_open_topic("Course_Convert_To_CSV",
 4029:                            &mt("How do I create a CSV file from a spreadsheet"))
 4030:     .'</td></tr></table>'."\n";
 4031:     $result.='</td></tr></table><br /><br />'."\n";
 4032:     $result.=&show_grading_menu_form($symb);
 4033:     return $result;
 4034: }
 4035: 
 4036: 
 4037: sub csvuploadmap {
 4038:     my ($request)= @_;
 4039:     my ($symb)=&get_symb($request);
 4040:     if (!$symb) {return '';}
 4041: 
 4042:     my $datatoken;
 4043:     if (!$env{'form.datatoken'}) {
 4044: 	$datatoken=&Apache::loncommon::upfile_store($request);
 4045:     } else {
 4046: 	$datatoken=$env{'form.datatoken'};
 4047: 	&Apache::loncommon::load_tmp_file($request);
 4048:     }
 4049:     my @records=&Apache::loncommon::upfile_record_sep();
 4050:     if ($env{'form.noFirstLine'}) { shift(@records); }
 4051:     &csvuploadmap_header($request,$symb,$datatoken,$#records+1);
 4052:     my ($i,$keyfields);
 4053:     if (@records) {
 4054:         my $fieldserror;
 4055: 	my @fields=&csvupload_fields($symb,\$fieldserror);
 4056:         if ($fieldserror) {
 4057:             $request->print(&navmap_errormsg());
 4058:             return;
 4059:         }
 4060: 	if ($env{'form.upfile_associate'} eq 'reverse') {	
 4061: 	    &Apache::loncommon::csv_print_samples($request,\@records);
 4062: 	    $i=&Apache::loncommon::csv_print_select_table($request,\@records,
 4063: 							  \@fields);
 4064: 	    foreach (@fields) { $keyfields.=$_->[0].','; }
 4065: 	    chop($keyfields);
 4066: 	} else {
 4067: 	    unshift(@fields,['none','']);
 4068: 	    $i=&Apache::loncommon::csv_samples_select_table($request,\@records,
 4069: 							    \@fields);
 4070:             foreach my $rec (@records) {
 4071:                 my %temp = &Apache::loncommon::record_sep($rec);
 4072:                 if (%temp) {
 4073:                     $keyfields=join(',',sort(keys(%temp)));
 4074:                     last;
 4075:                 }
 4076:             }
 4077: 	}
 4078:     }
 4079:     &csvuploadmap_footer($request,$i,$keyfields);
 4080:     $request->print(&show_grading_menu_form($symb));
 4081: 
 4082:     return '';
 4083: }
 4084: 
 4085: sub csvuploadoptions {
 4086:     my ($request)= @_;
 4087:     my ($symb)=&get_symb($request);
 4088:     my $checked=(($env{'form.noFirstLine'})?'1':'0');
 4089:     my $ignore=&mt('Ignore First Line');
 4090:     $request->print(<<ENDPICK);
 4091: <form method="post" enctype="multipart/form-data" action="/adm/grades" name="gradesupload">
 4092: <h3><span class="LC_info">Uploading Class Grade Options</span></h3>
 4093: <input type="hidden" name="command"    value="csvuploadassign" />
 4094: <!--
 4095: <p>
 4096: <label>
 4097:    <input type="checkbox" name="show_full_results" />
 4098:    Show a table of all changes
 4099: </label>
 4100: </p>
 4101: -->
 4102: <p>
 4103: <label>
 4104:    <input type="checkbox" name="overwite_scores" checked="checked" />
 4105:    Overwrite any existing score
 4106: </label>
 4107: </p>
 4108: ENDPICK
 4109:     my %fields=&get_fields();
 4110:     if (!defined($fields{'domain'})) {
 4111: 	my $domform = &Apache::loncommon::select_dom_form($env{'request.role.domain'},'default_domain');
 4112: 	$request->print("\n<p> Users are in domain: ".$domform."</p>\n");
 4113:     }
 4114:     foreach my $key (sort(keys(%env))) {
 4115: 	if ($key !~ /^form\.(.*)$/) { next; }
 4116: 	my $cleankey=$1;
 4117: 	if ($cleankey eq 'command') { next; }
 4118: 	$request->print('<input type="hidden" name="'.$cleankey.
 4119: 			'"  value="'.$env{$key}.'" />'."\n");
 4120:     }
 4121:     # FIXME do a check for any duplicated user ids...
 4122:     # FIXME do a check for any invalid user ids?...
 4123:     $request->print('<input type="submit" value="Assign Grades" /><br />
 4124: <hr /></form>'."\n");
 4125:     $request->print(&show_grading_menu_form($symb));
 4126:     return '';
 4127: }
 4128: 
 4129: sub get_fields {
 4130:     my %fields;
 4131:     my @keyfields = split(/\,/,$env{'form.keyfields'});
 4132:     for (my $i=0; $i<=$env{'form.nfields'}; $i++) {
 4133: 	if ($env{'form.upfile_associate'} eq 'reverse') {
 4134: 	    if ($env{'form.f'.$i} ne 'none') {
 4135: 		$fields{$keyfields[$i]}=$env{'form.f'.$i};
 4136: 	    }
 4137: 	} else {
 4138: 	    if ($env{'form.f'.$i} ne 'none') {
 4139: 		$fields{$env{'form.f'.$i}}=$keyfields[$i];
 4140: 	    }
 4141: 	}
 4142:     }
 4143:     return %fields;
 4144: }
 4145: 
 4146: sub csvuploadassign {
 4147:     my ($request)= @_;
 4148:     my ($symb)=&get_symb($request);
 4149:     if (!$symb) {return '';}
 4150:     my $error_msg = '';
 4151:     &Apache::loncommon::load_tmp_file($request);
 4152:     my @gradedata = &Apache::loncommon::upfile_record_sep();
 4153:     if ($env{'form.noFirstLine'}) { shift(@gradedata); }
 4154:     my %fields=&get_fields();
 4155:     $request->print('<h3>Assigning Grades</h3>');
 4156:     my $courseid=$env{'request.course.id'};
 4157:     my ($classlist) = &getclasslist('all',0);
 4158:     my @notallowed;
 4159:     my @skipped;
 4160:     my $countdone=0;
 4161:     foreach my $grade (@gradedata) {
 4162: 	my %entries=&Apache::loncommon::record_sep($grade);
 4163: 	my $domain;
 4164: 	if ($entries{$fields{'domain'}}) {
 4165: 	    $domain=$entries{$fields{'domain'}};
 4166: 	} else {
 4167: 	    $domain=$env{'form.default_domain'};
 4168: 	}
 4169: 	$domain=~s/\s//g;
 4170: 	my $username=$entries{$fields{'username'}};
 4171: 	$username=~s/\s//g;
 4172: 	if (!$username) {
 4173: 	    my $id=$entries{$fields{'ID'}};
 4174: 	    $id=~s/\s//g;
 4175: 	    my %ids=&Apache::lonnet::idget($domain,$id);
 4176: 	    $username=$ids{$id};
 4177: 	}
 4178: 	if (!exists($$classlist{"$username:$domain"})) {
 4179: 	    my $id=$entries{$fields{'ID'}};
 4180: 	    $id=~s/\s//g;
 4181: 	    if ($id) {
 4182: 		push(@skipped,"$id:$domain");
 4183: 	    } else {
 4184: 		push(@skipped,"$username:$domain");
 4185: 	    }
 4186: 	    next;
 4187: 	}
 4188: 	my $usec=$classlist->{"$username:$domain"}[5];
 4189: 	if (!&canmodify($usec)) {
 4190: 	    push(@notallowed,"$username:$domain");
 4191: 	    next;
 4192: 	}
 4193: 	my %points;
 4194: 	my %grades;
 4195: 	foreach my $dest (keys(%fields)) {
 4196: 	    if ($dest eq 'ID' || $dest eq 'username' ||
 4197: 		$dest eq 'domain') { next; }
 4198: 	    if ($entries{$fields{$dest}} =~ /^\s*$/) { next; }
 4199: 	    if ($dest=~/stores_(.*)_points/) {
 4200: 		my $part=$1;
 4201: 		my $wgt =&Apache::lonnet::EXT('resource.'.$part.'.weight',
 4202: 					      $symb,$domain,$username);
 4203:                 if ($wgt) {
 4204:                     $entries{$fields{$dest}}=~s/\s//g;
 4205:                     my $pcr=$entries{$fields{$dest}} / $wgt;
 4206:                     my $award=($pcr == 0) ? 'incorrect_by_override'
 4207:                                           : 'correct_by_override';
 4208:                     $grades{"resource.$part.awarded"}=$pcr;
 4209:                     $grades{"resource.$part.solved"}=$award;
 4210:                     $points{$part}=1;
 4211:                 } else {
 4212:                     $error_msg = "<br />" .
 4213:                         &mt("Some point values were assigned"
 4214:                             ." for problems with a weight "
 4215:                             ."of zero. These values were "
 4216:                             ."ignored.");
 4217:                 }
 4218: 	    } else {
 4219: 		if ($dest=~/stores_(.*)_awarded/) { if ($points{$1}) {next;} }
 4220: 		if ($dest=~/stores_(.*)_solved/)  { if ($points{$1}) {next;} }
 4221: 		my $store_key=$dest;
 4222: 		$store_key=~s/^stores/resource/;
 4223: 		$store_key=~s/_/\./g;
 4224: 		$grades{$store_key}=$entries{$fields{$dest}};
 4225: 	    }
 4226: 	}
 4227: 	if (! %grades) { 
 4228:            push(@skipped,&mt("[_1]: no data to save","$username:$domain")); 
 4229:         } else {
 4230: 	   $grades{"resource.regrader"}="$env{'user.name'}:$env{'user.domain'}";
 4231: 	   my $result=&Apache::lonnet::cstore(\%grades,$symb,
 4232: 					   $env{'request.course.id'},
 4233: 					   $domain,$username);
 4234: 	   if ($result eq 'ok') {
 4235: 	      $request->print('.');
 4236: 	   } else {
 4237: 	      $request->print("<p><span class=\"LC_error\">".
 4238:                               &mt("Failed to save data for student [_1]. Message when trying to save was: [_2]",
 4239:                                   "$username:$domain",$result)."</span></p>");
 4240: 	   }
 4241: 	   $request->rflush();
 4242: 	   $countdone++;
 4243:         }
 4244:     }
 4245:     $request->print('<br />'.&Apache::lonhtmlcommon::confirm_success(&mt("Saved scores for [quant,_1,student]",$countdone),$countdone==0));
 4246:     if (@skipped) {
 4247: 	$request->print('<br />'.&Apache::lonhtmlcommon::confirm_success(&mt('No scores stored for the following username(s):'),1).'<br />');
 4248:         $request->print(join(', ',@skipped));
 4249:     }
 4250:     if (@notallowed) {
 4251: 	$request->print('<br />'.&Apache::lonhtmlcommon::confirm_success(&mt('Modification of scores not allowed for the following username(s):'),1).'<br />');
 4252: 	$request->print(join(', ',@notallowed));
 4253:     }
 4254:     $request->print("<br />\n");
 4255:     $request->print(&show_grading_menu_form($symb));
 4256:     return $error_msg;
 4257: }
 4258: #------------- end of section for handling csv file upload ---------
 4259: #
 4260: #-------------------------------------------------------------------
 4261: #
 4262: #-------------- Next few routines handle grading by page/sequence
 4263: #
 4264: #--- Select a page/sequence and a student to grade
 4265: sub pickStudentPage {
 4266:     my ($request) = shift;
 4267: 
 4268:     my $alertmsg = &mt('Please select the student you wish to grade.');
 4269:     $request->print(<<LISTJAVASCRIPT);
 4270: <script type="text/javascript" language="javascript">
 4271: 
 4272: function checkPickOne(formname) {
 4273:     if (radioSelection(formname.student) == null) {
 4274: 	alert("$alertmsg");
 4275: 	return;
 4276:     }
 4277:     ptr = pullDownSelection(formname.selectpage);
 4278:     formname.page.value = formname["page"+ptr].value;
 4279:     formname.title.value = formname["title"+ptr].value;
 4280:     formname.submit();
 4281: }
 4282: 
 4283: </script>
 4284: LISTJAVASCRIPT
 4285:     &commonJSfunctions($request);
 4286:     my ($symb) = &get_symb($request);
 4287:     my $cdom      = $env{"course.$env{'request.course.id'}.domain"};
 4288:     my $cnum      = $env{"course.$env{'request.course.id'}.num"};
 4289:     my $getsec    = $env{'form.section'} eq '' ? 'all' : $env{'form.section'};
 4290: 
 4291:     my $result='<h3><span class="LC_info">&nbsp;'.
 4292: 	&mt('Manual Grading by Page or Sequence').'</span></h3>';
 4293: 
 4294:     $result.='<form action="/adm/grades" method="post" name="displayPage">'."\n";
 4295:     my $map_error;
 4296:     my ($titles,$symbx) = &getSymbMap($map_error);
 4297:     if ($map_error) {
 4298:         $request->print(&navmap_errormsg());
 4299:         return; 
 4300:     }
 4301:     my ($curpage) =&Apache::lonnet::decode_symb($symb); 
 4302: #    my ($curpage,$mapId) =&Apache::lonnet::decode_symb($symb); 
 4303: #    my $type=($curpage =~ /\.(page|sequence)/);
 4304:     my $select = '<select name="selectpage">'."\n";
 4305:     my $ctr=0;
 4306:     foreach (@$titles) {
 4307: 	my ($minder,$showtitle) = ($_ =~ /(\d+)\.(.*)/);
 4308: 	$select.='<option value="'.$ctr.'" '.
 4309: 	    ($$symbx{$_} =~ /$curpage$/ ? 'selected="selected"' : '').
 4310: 	    '>'.$showtitle.'</option>'."\n";
 4311: 	$ctr++;
 4312:     }
 4313:     $select.= '</select>';
 4314:     $result.='&nbsp;<b>'.&mt('Problems from').':</b> '.$select."<br />\n";
 4315: 
 4316:     $ctr=0;
 4317:     foreach (@$titles) {
 4318: 	my ($minder,$showtitle) = ($_ =~ /(\d+)\.(.*)/);
 4319: 	$result.='<input type="hidden" name="page'.$ctr.'" value="'.$$symbx{$_}.'" />'."\n";
 4320: 	$result.='<input type="hidden" name="title'.$ctr.'" value="'.$showtitle.'" />'."\n";
 4321: 	$ctr++;
 4322:     }
 4323:     $result.='<input type="hidden" name="page" />'."\n".
 4324: 	'<input type="hidden" name="title" />'."\n";
 4325: 
 4326:     my $options =
 4327: 	'<label><input type="radio" name="vProb" value="no" checked="checked" /> '.&mt('no').' </label>'."\n".
 4328: 	'<label><input type="radio" name="vProb" value="yes" /> '.&mt('yes').' </label>'."<br />\n";
 4329:     $result.='&nbsp;<b>'.&mt('View Problem Text').': </b>'.$options;
 4330: 
 4331:     $options =
 4332: 	'<label><input type="radio" name="lastSub" value="none" /> '.&mt('none').' </label>'."\n".
 4333: 	'<label><input type="radio" name="lastSub" value="datesub" checked="checked" /> '.&mt('by dates and submissions').'</label>'."\n".
 4334: 	'<label><input type="radio" name="lastSub" value="all" /> '.&mt('all details').' </label>'."\n";
 4335:     $result.='&nbsp;<b>'.&mt('Submissions').': </b>'.$options;
 4336:     
 4337:     $result.=&build_section_inputs();
 4338:     my $stu_status = join(':',&Apache::loncommon::get_env_multiple('form.Status'));
 4339:     $result.='<input type="hidden" name="Status"  value="'.$stu_status.'" />'."\n".
 4340: 	'<input type="hidden" name="command" value="displayPage" />'."\n".
 4341: 	'<input type="hidden" name="symb"    value="'.&Apache::lonenc::check_encrypt($symb).'" />'."\n".
 4342: 	'<input type="hidden" name="saveState" value="'.$env{'form.saveState'}.'" />'."<br />\n";
 4343: 
 4344:     $result.='&nbsp;<b>'.&mt('Use CODE').': </b> <input type="text" name="CODE" value="" /> <br />'."\n";
 4345: 
 4346:     $result.='&nbsp;<input type="button" '.
 4347:              'onclick="javascript:checkPickOne(this.form);" value="'.&mt('Next').' &rarr;" /><br />'."\n";
 4348: 
 4349:     $request->print($result);
 4350: 
 4351:     my $studentTable.='&nbsp;<b>'.&mt('Select a student you wish to grade and then click on the Next button.').'</b><br />'.
 4352: 	&Apache::loncommon::start_data_table().
 4353: 	&Apache::loncommon::start_data_table_header_row().
 4354: 	'<th align="right">&nbsp;'.&mt('No.').'</th>'.
 4355: 	'<th>'.&nameUserString('header').'</th>'.
 4356: 	'<th align="right">&nbsp;'.&mt('No.').'</th>'.
 4357: 	'<th>'.&nameUserString('header').'</th>'.
 4358: 	&Apache::loncommon::end_data_table_header_row();
 4359:  
 4360:     my (undef,undef,$fullname) = &getclasslist($getsec,'1');
 4361:     my $ptr = 1;
 4362:     foreach my $student (sort 
 4363: 			 {
 4364: 			     if (lc($$fullname{$a}) ne lc($$fullname{$b})) {
 4365: 				 return (lc($$fullname{$a}) cmp lc($$fullname{$b}));
 4366: 			     }
 4367: 			     return $a cmp $b;
 4368: 			 } (keys(%$fullname))) {
 4369: 	my ($uname,$udom) = split(/:/,$student);
 4370: 	$studentTable.=($ptr%2==1 ? &Apache::loncommon::start_data_table_row()
 4371:                                   : '</td>');
 4372: 	$studentTable.='<td align="right">'.$ptr.'&nbsp;</td>';
 4373: 	$studentTable.='<td>&nbsp;<label><input type="radio" name="student" value="'.$student.'" /> '
 4374: 	    .&nameUserString(undef,$$fullname{$student},$uname,$udom)."</label>\n";
 4375: 	$studentTable.=
 4376: 	    ($ptr%2 == 0 ? '</td>'.&Apache::loncommon::end_data_table_row() 
 4377:                          : '');
 4378: 	$ptr++;
 4379:     }
 4380:     if ($ptr%2 == 0) {
 4381: 	$studentTable.='</td><td>&nbsp;</td><td>&nbsp;</td>'.
 4382: 	    &Apache::loncommon::end_data_table_row();
 4383:     }
 4384:     $studentTable.=&Apache::loncommon::end_data_table()."\n";
 4385:     $studentTable.='<input type="button" '.
 4386:                    'onclick="javascript:checkPickOne(this.form);" value="'.&mt('Next').' &rarr;" /></form>'."\n";
 4387: 
 4388:     $studentTable.=&show_grading_menu_form($symb);
 4389:     $request->print($studentTable);
 4390: 
 4391:     return '';
 4392: }
 4393: 
 4394: sub getSymbMap {
 4395:     my ($map_error) = @_;
 4396:     my $navmap = Apache::lonnavmaps::navmap->new();
 4397:     unless (ref($navmap)) {
 4398:         if (ref($map_error)) {
 4399:             $$map_error = 'navmap';
 4400:         }
 4401:         return;
 4402:     }
 4403:     my %symbx = ();
 4404:     my @titles = ();
 4405:     my $minder = 0;
 4406: 
 4407:     # Gather every sequence that has problems.
 4408:     my @sequences = $navmap->retrieveResources(undef, sub { shift->is_map(); },
 4409: 					       1,0,1);
 4410:     for my $sequence ($navmap->getById('0.0'), @sequences) {
 4411: 	if ($navmap->hasResource($sequence, sub { shift->is_problem(); }, 0) ) {
 4412: 	    my $title = $minder.'.'.
 4413: 		&HTML::Entities::encode($sequence->compTitle(),'"\'&');
 4414: 	    push(@titles, $title); # minder in case two titles are identical
 4415: 	    $symbx{$title} = &HTML::Entities::encode($sequence->symb(),'"\'&');
 4416: 	    $minder++;
 4417: 	}
 4418:     }
 4419:     return \@titles,\%symbx;
 4420: }
 4421: 
 4422: #
 4423: #--- Displays a page/sequence w/wo problems, w/wo submissions
 4424: sub displayPage {
 4425:     my ($request) = shift;
 4426: 
 4427:     my ($symb) = &get_symb($request);
 4428:     my $cdom      = $env{"course.$env{'request.course.id'}.domain"};
 4429:     my $cnum      = $env{"course.$env{'request.course.id'}.num"};
 4430:     my $getsec    = $env{'form.section'} eq '' ? 'all' : $env{'form.section'};
 4431:     my $pageTitle = $env{'form.page'};
 4432:     my ($classlist,undef,$fullname) = &getclasslist($getsec,'1');
 4433:     my ($uname,$udom) = split(/:/,$env{'form.student'});
 4434:     my $usec=$classlist->{$env{'form.student'}}[5];
 4435: 
 4436:     #need to make sure we have the correct data for later EXT calls, 
 4437:     #thus invalidate the cache
 4438:     &Apache::lonnet::devalidatecourseresdata(
 4439:                  $env{'course.'.$env{'request.course.id'}.'.num'},
 4440:                  $env{'course.'.$env{'request.course.id'}.'.domain'});
 4441:     &Apache::lonnet::clear_EXT_cache_status();
 4442: 
 4443:     if (!&canview($usec)) {
 4444: 	$request->print('<span class="LC_warning">'.&mt('Unable to view requested student. ([_1])',$env{'form.student'}).'</span>');
 4445: 	$request->print(&show_grading_menu_form($symb));
 4446: 	return;
 4447:     }
 4448:     my $result='<h3><span class="LC_info">&nbsp;'.$env{'form.title'}.'</span></h3>';
 4449:     $result.='<h3>&nbsp;'.&mt('Student: [_1]',&nameUserString(undef,$$fullname{$env{'form.student'}},$uname,$udom)).
 4450: 	'</h3>'."\n";
 4451:     $env{'form.CODE'} = uc($env{'form.CODE'});
 4452:     if (&Apache::lonnet::validCODE(uc($env{'form.CODE'}))) {
 4453: 	$result.='<h3>&nbsp;'.&mt('CODE: [_1]',$env{'form.CODE'}).'</h3>'."\n";
 4454:     } else {
 4455: 	delete($env{'form.CODE'});
 4456:     }
 4457:     &sub_page_js($request);
 4458:     $request->print($result);
 4459: 
 4460:     my $navmap = Apache::lonnavmaps::navmap->new();
 4461:     unless (ref($navmap)) {
 4462:         $request->print(&navmap_errormsg());
 4463:         $request->print(&show_grading_menu_form($symb));
 4464:         return;
 4465:     }
 4466:     my ($mapUrl, $id, $resUrl)=&Apache::lonnet::decode_symb($env{'form.page'});
 4467:     my $map = $navmap->getResourceByUrl($resUrl); # add to navmaps
 4468:     if (!$map) {
 4469: 	$request->print('<span class="LC_warning">'.&mt('Unable to view requested sequence. ([_1])',$resUrl).'</span>');
 4470: 	$request->print(&show_grading_menu_form($symb));
 4471: 	return; 
 4472:     }
 4473:     my $iterator = $navmap->getIterator($map->map_start(),
 4474: 					$map->map_finish());
 4475: 
 4476:     my $studentTable='<form action="/adm/grades" method="post" name="gradePage">'."\n".
 4477: 	'<input type="hidden" name="command" value="gradeByPage" />'."\n".
 4478: 	'<input type="hidden" name="fullname" value="'.$$fullname{$env{'form.student'}}.'" />'."\n".
 4479: 	'<input type="hidden" name="student" value="'.$env{'form.student'}.'" />'."\n".
 4480: 	'<input type="hidden" name="page"    value="'.$pageTitle.'" />'."\n".
 4481: 	'<input type="hidden" name="title"   value="'.$env{'form.title'}.'" />'."\n".
 4482: 	'<input type="hidden" name="symb"    value="'.&Apache::lonenc::check_encrypt($symb).'" />'."\n".
 4483: 	'<input type="hidden" name="overRideScore" value="no" />'."\n".
 4484: 	'<input type="hidden" name="saveState" value="'.$env{'form.saveState'}.'" />'."\n";
 4485: 
 4486:     if (defined($env{'form.CODE'})) {
 4487: 	$studentTable.=
 4488: 	    '<input type="hidden" name="CODE" value="'.$env{'form.CODE'}.'" />'."\n";
 4489:     }
 4490:     my $checkIcon = '<img alt="'.&mt('Check Mark').
 4491: 	'" src="'.&Apache::loncommon::lonhttpdurl($request->dir_config('lonIconsURL').'/check.gif').'" height="16" border="0" />';
 4492: 
 4493:     $studentTable.='&nbsp;<span class="LC_info">'.
 4494:         &mt('Problems graded correct by the computer are marked with a [_1] symbol.',$checkIcon).
 4495:         '</span>'."\n".
 4496: 	&Apache::loncommon::start_data_table().
 4497: 	&Apache::loncommon::start_data_table_header_row().
 4498: 	'<th align="center">&nbsp;Prob.&nbsp;</th>'.
 4499: 	'<th>&nbsp;'.($env{'form.vProb'} eq 'no' ? &mt('Title') : &mt('Problem Text')).'/'.&mt('Grade').'</th>'.
 4500: 	&Apache::loncommon::end_data_table_header_row();
 4501: 
 4502:     &Apache::lonxml::clear_problem_counter();
 4503:     my ($depth,$question,$prob) = (1,1,1);
 4504:     $iterator->next(); # skip the first BEGIN_MAP
 4505:     my $curRes = $iterator->next(); # for "current resource"
 4506:     while ($depth > 0) {
 4507:         if($curRes == $iterator->BEGIN_MAP) { $depth++; }
 4508:         if($curRes == $iterator->END_MAP) { $depth--; }
 4509: 
 4510:         if (ref($curRes) && $curRes->is_problem()) {
 4511: 	    my $parts = $curRes->parts();
 4512:             my $title = $curRes->compTitle();
 4513: 	    my $symbx = $curRes->symb();
 4514: 	    $studentTable.=
 4515: 		&Apache::loncommon::start_data_table_row().
 4516: 		'<td align="center" valign="top" >'.$prob.
 4517: 		(scalar(@{$parts}) == 1 ? '' 
 4518: 		                        : '<br />('.&mt('[_1]&nbsp;parts)',
 4519: 							scalar(@{$parts}))
 4520: 		 ).
 4521: 		 '</td>';
 4522: 	    $studentTable.='<td valign="top">';
 4523: 	    my %form = ('CODE' => $env{'form.CODE'},);
 4524: 	    if ($env{'form.vProb'} eq 'yes' ) {
 4525: 		$studentTable.=&show_problem($request,$symbx,$uname,$udom,1,
 4526: 					     undef,'both',\%form);
 4527: 	    } else {
 4528: 		my $companswer = &Apache::loncommon::get_student_answers($symbx,$uname,$udom,$env{'request.course.id'},%form);
 4529: 		$companswer =~ s|<form(.*?)>||g;
 4530: 		$companswer =~ s|</form>||g;
 4531: #		while ($companswer =~ /(<a href\=\"javascript:newWindow.*?Script Vars<\/a>)/s) { #<a href="javascript:newWindow</a>
 4532: #		    $companswer =~ s/$1/ /ms;
 4533: #		    $request->print('match='.$1."<br />\n");
 4534: #		}
 4535: #		$companswer =~ s|<table border=\"1\">|<table border=\"0\">|g;
 4536: 		$studentTable.='&nbsp;<b>'.$title.'</b>&nbsp;<br />&nbsp;<b>'.&mt('Correct answer').':</b><br />'.$companswer;
 4537: 	    }
 4538: 
 4539: 	    my %record = &Apache::lonnet::restore($symbx,$env{'request.course.id'},$udom,$uname);
 4540: 
 4541: 	    if ($env{'form.lastSub'} eq 'datesub') {
 4542: 		if ($record{'version'} eq '') {
 4543: 		    $studentTable.='<br />&nbsp;<span class="LC_warning">'.&mt('No recorded submission for this problem.').'</span><br />';
 4544: 		} else {
 4545: 		    my %responseType = ();
 4546: 		    foreach my $partid (@{$parts}) {
 4547: 			my @responseIds =$curRes->responseIds($partid);
 4548: 			my @responseType =$curRes->responseType($partid);
 4549: 			my %responseIds;
 4550: 			for (my $i=0;$i<=$#responseIds;$i++) {
 4551: 			    $responseIds{$responseIds[$i]}=$responseType[$i];
 4552: 			}
 4553: 			$responseType{$partid} = \%responseIds;
 4554: 		    }
 4555: 		    $studentTable.= &displaySubByDates($symbx,\%record,$parts,\%responseType,$checkIcon,$uname,$udom);
 4556: 
 4557: 		}
 4558: 	    } elsif ($env{'form.lastSub'} eq 'all') {
 4559: 		my $last = ($env{'form.lastSub'} eq 'last' ? 'last' : '');
 4560: 		$studentTable.=&Apache::loncommon::get_previous_attempt($symbx,$uname,$udom,
 4561: 									$env{'request.course.id'},
 4562: 									'','.submission');
 4563:  
 4564: 	    }
 4565: 	    if (&canmodify($usec)) {
 4566:             $studentTable.=&gradeBox_start();
 4567: 		foreach my $partid (@{$parts}) {
 4568: 		    $studentTable.=&gradeBox($request,$symbx,$uname,$udom,$question,$partid,\%record);
 4569: 		    $studentTable.='<input type="hidden" name="q_'.$question.'" value="'.$partid.'" />'."\n";
 4570: 		    $question++;
 4571: 		}
 4572:             $studentTable.=&gradeBox_end();
 4573: 		$prob++;
 4574: 	    }
 4575: 	    $studentTable.='</td></tr>';
 4576: 
 4577: 	}
 4578:         $curRes = $iterator->next();
 4579:     }
 4580: 
 4581:     $studentTable.=
 4582:         '</table>'."\n".
 4583:         '<input type="button" value="'.&mt('Save').'" '.
 4584:         'onclick="javascript:checkSubmitPage(this.form,'.$question.');" />'.
 4585:         '</form>'."\n";
 4586:     $studentTable.=&show_grading_menu_form($symb);
 4587:     $request->print($studentTable);
 4588: 
 4589:     return '';
 4590: }
 4591: 
 4592: sub displaySubByDates {
 4593:     my ($symb,$record,$parts,$responseType,$checkIcon,$uname,$udom) = @_;
 4594:     my $isCODE=0;
 4595:     my $isTask = ($symb =~/\.task$/);
 4596:     if (exists($record->{'resource.CODE'})) { $isCODE=1; }
 4597:     my $studentTable=&Apache::loncommon::start_data_table().
 4598: 	&Apache::loncommon::start_data_table_header_row().
 4599: 	'<th>'.&mt('Date/Time').'</th>'.
 4600: 	($isCODE?'<th>'.&mt('CODE').'</th>':'').
 4601: 	'<th>'.&mt('Submission').'</th>'.
 4602: 	'<th>'.&mt('Status').'</th>'.
 4603: 	&Apache::loncommon::end_data_table_header_row();
 4604:     my ($version);
 4605:     my %mark;
 4606:     my %orders;
 4607:     $mark{'correct_by_student'} = $checkIcon;
 4608:     if (!exists($$record{'1:timestamp'})) {
 4609: 	return '<br />&nbsp;<span class="LC_warning">'.&mt('Nothing submitted - no attempts.').'</span><br />';
 4610:     }
 4611: 
 4612:     my $interaction;
 4613:     my $no_increment = 1;
 4614:     for ($version=1;$version<=$$record{'version'};$version++) {
 4615: 	my $timestamp = 
 4616: 	    &Apache::lonlocal::locallocaltime($$record{$version.':timestamp'});
 4617: 	if (exists($$record{$version.':resource.0.version'})) {
 4618: 	    $interaction = $$record{$version.':resource.0.version'};
 4619: 	}
 4620: 
 4621: 	my $where = ($isTask ? "$version:resource.$interaction"
 4622: 		             : "$version:resource");
 4623: 	$studentTable.=&Apache::loncommon::start_data_table_row().
 4624: 	    '<td>'.$timestamp.'</td>';
 4625: 	if ($isCODE) {
 4626: 	    $studentTable.='<td>'.$record->{$version.':resource.CODE'}.'</td>';
 4627: 	}
 4628: 	my @versionKeys = split(/\:/,$$record{$version.':keys'});
 4629: 	my @displaySub = ();
 4630: 	foreach my $partid (@{$parts}) {
 4631:             my $hidden;
 4632:             if (($$record{$version.':resource.'.$partid.'.type'} eq 'anonsurvey') ||
 4633:                 ($$record{$version.':resource.'.$partid.'.type'} eq 'anonsurveycred')) {
 4634:                 $hidden = 1;
 4635:             }
 4636: 	    my @matchKey = ($isTask ? sort(grep /^resource\.\d+\.\Q$partid\E\.award$/,@versionKeys)
 4637: 			            : sort(grep /^resource\.\Q$partid\E\..*?\.submission$/,@versionKeys));
 4638: 	    
 4639: #	    next if ($$record{"$version:resource.$partid.solved"} eq '');
 4640: 	    my $display_part=&get_display_part($partid,$symb);
 4641: 	    foreach my $matchKey (@matchKey) {
 4642: 		if (exists($$record{$version.':'.$matchKey}) &&
 4643: 		    $$record{$version.':'.$matchKey} ne '') {
 4644:                     
 4645: 		    my ($responseId)= ($isTask ? ($matchKey=~ /^resource\.(.*?)\.\Q$partid\E\.award$/)
 4646: 				               : ($matchKey=~ /^resource\.\Q$partid\E\.(.*?)\.submission$/));
 4647:                     $displaySub[0].='<span class="LC_nobreak"';
 4648:                     $displaySub[0].='<b>'.&mt('Part: [_1]',$display_part).'</b>'
 4649:                                    .' <span class="LC_internal_info">'
 4650:                                    .'('.&mt('Part ID: [_1]',$responseId).')'
 4651:                                    .'</span>'
 4652:                                    .' <b>';
 4653:                     if ($hidden) {
 4654:                         $displaySub[0].= &mt('Anonymous Survey').'</b>';
 4655:                     } else {
 4656: 		        if ($$record{"$where.$partid.tries"} eq '') {
 4657: 			    $displaySub[0].=&mt('Trial not counted');
 4658: 		        } else {
 4659: 			    $displaySub[0].=&mt('Trial: [_1]',
 4660: 					    $$record{"$where.$partid.tries"});
 4661: 		        }
 4662: 		        my $responseType=($isTask ? 'Task'
 4663:                                               : $responseType->{$partid}->{$responseId});
 4664: 		        if (!exists($orders{$partid})) { $orders{$partid}={}; }
 4665: 		        if (!exists($orders{$partid}->{$responseId})) {
 4666: 			    $orders{$partid}->{$responseId}=
 4667: 			        &get_order($partid,$responseId,$symb,$uname,$udom,
 4668:                                            $no_increment);
 4669: 		        }
 4670: 		        $displaySub[0].='</b></span>'; # /nobreak
 4671: 		        $displaySub[0].='&nbsp; '.
 4672: 			    &cleanRecord($$record{$version.':'.$matchKey},$responseType,$symb,$partid,$responseId,$record,$orders{$partid}->{$responseId},"$version:",$uname,$udom).'<br />';
 4673:                     }
 4674: 		}
 4675: 	    }
 4676: 	    if (exists($$record{"$where.$partid.checkedin"})) {
 4677: 		$displaySub[1].=&mt('Checked in by [_1] into slot [_2]',
 4678: 				    $$record{"$where.$partid.checkedin"},
 4679: 				    $$record{"$where.$partid.checkedin.slot"}).
 4680: 					'<br />';
 4681: 	    }
 4682: 	    if (exists $$record{"$where.$partid.award"}) {
 4683: 		$displaySub[1].='<b>'.&mt('Part:').'</b>&nbsp;'.$display_part.' &nbsp;'.
 4684: 		    lc($$record{"$where.$partid.award"}).' '.
 4685: 		    $mark{$$record{"$where.$partid.solved"}}.
 4686: 		    '<br />';
 4687: 	    }
 4688: 	    if (exists $$record{"$where.$partid.regrader"}) {
 4689: 		$displaySub[2].=$$record{"$where.$partid.regrader"}.
 4690: 		    ' (<b>'.&mt('Part').':</b> '.$display_part.')';
 4691: 	    } elsif ($$record{"$version:resource.$partid.regrader"} =~ /\S/) {
 4692: 		$displaySub[2].=
 4693: 		    $$record{"$version:resource.$partid.regrader"}.
 4694: 		    ' (<b>'.&mt('Part').':</b> '.$display_part.')';
 4695: 	    }
 4696: 	}
 4697: 	# needed because old essay regrader has not parts info
 4698: 	if (exists $$record{"$version:resource.regrader"}) {
 4699: 	    $displaySub[2].=$$record{"$version:resource.regrader"};
 4700: 	}
 4701: 	$studentTable.='<td>'.$displaySub[0].'&nbsp;</td><td>'.$displaySub[1];
 4702: 	if ($displaySub[2]) {
 4703: 	    $studentTable.=&mt('Manually graded by [_1]',$displaySub[2]);
 4704: 	}
 4705: 	$studentTable.='&nbsp;</td>'.
 4706: 	    &Apache::loncommon::end_data_table_row();
 4707:     }
 4708:     $studentTable.=&Apache::loncommon::end_data_table();
 4709:     return $studentTable;
 4710: }
 4711: 
 4712: sub updateGradeByPage {
 4713:     my ($request) = shift;
 4714: 
 4715:     my $cdom      = $env{"course.$env{'request.course.id'}.domain"};
 4716:     my $cnum      = $env{"course.$env{'request.course.id'}.num"};
 4717:     my $getsec    = $env{'form.section'} eq '' ? 'all' : $env{'form.section'};
 4718:     my $pageTitle = $env{'form.page'};
 4719:     my ($classlist,undef,$fullname) = &getclasslist($getsec,'1');
 4720:     my ($uname,$udom) = split(/:/,$env{'form.student'});
 4721:     my $usec=$classlist->{$env{'form.student'}}[5];
 4722:     if (!&canmodify($usec)) {
 4723: 	$request->print('<span class="LC_warning">'.&mt('Unable to modify requested student ([_1])',$env{'form.student'}).'</span>');
 4724: 	$request->print(&show_grading_menu_form($env{'form.symb'}));
 4725: 	return;
 4726:     }
 4727:     my $result='<h3><span class="LC_info">&nbsp;'.$env{'form.title'}.'</span></h3>';
 4728:     $result.='<h3>&nbsp;'.&mt('Student: ').&nameUserString(undef,$env{'form.fullname'},$uname,$udom).
 4729: 	'</h3>'."\n";
 4730: 
 4731:     $request->print($result);
 4732: 
 4733: 
 4734:     my $navmap = Apache::lonnavmaps::navmap->new();
 4735:     unless (ref($navmap)) {
 4736:         $request->print(&navmap_errormsg());
 4737:         return;
 4738:     }
 4739:     my ($mapUrl, $id, $resUrl) = &Apache::lonnet::decode_symb( $env{'form.page'});
 4740:     my $map = $navmap->getResourceByUrl($resUrl); # add to navmaps
 4741:     if (!$map) {
 4742: 	$request->print('<span class="LC_warning">'.&mt('Unable to grade requested sequence ([_1]).',$resUrl).'</span>');
 4743: 	my ($symb)=&get_symb($request);
 4744: 	$request->print(&show_grading_menu_form($symb));
 4745: 	return; 
 4746:     }
 4747:     my $iterator = $navmap->getIterator($map->map_start(),
 4748: 					$map->map_finish());
 4749: 
 4750:     my $studentTable=
 4751: 	&Apache::loncommon::start_data_table().
 4752: 	&Apache::loncommon::start_data_table_header_row().
 4753: 	'<th align="center">&nbsp;'.&mt('Prob.').'&nbsp;</th>'.
 4754: 	'<th>&nbsp;'.&mt('Title').'&nbsp;</th>'.
 4755: 	'<th>&nbsp;'.&mt('Previous Score').'&nbsp;</th>'.
 4756: 	'<th>&nbsp;'.&mt('New Score').'&nbsp;</th>'.
 4757: 	&Apache::loncommon::end_data_table_header_row();
 4758: 
 4759:     $iterator->next(); # skip the first BEGIN_MAP
 4760:     my $curRes = $iterator->next(); # for "current resource"
 4761:     my ($depth,$question,$prob,$changeflag)= (1,1,1,0);
 4762:     while ($depth > 0) {
 4763:         if($curRes == $iterator->BEGIN_MAP) { $depth++; }
 4764:         if($curRes == $iterator->END_MAP) { $depth--; }
 4765: 
 4766:         if (ref($curRes) && $curRes->is_problem()) {
 4767: 	    my $parts = $curRes->parts();
 4768:             my $title = $curRes->compTitle();
 4769: 	    my $symbx = $curRes->symb();
 4770: 	    $studentTable.=
 4771: 		&Apache::loncommon::start_data_table_row().
 4772: 		'<td align="center" valign="top" >'.$prob.
 4773: 		(scalar(@{$parts}) == 1 ? '' 
 4774:                                         : '<br />('.&mt('[quant,_1,&nbsp;part]',scalar(@{$parts}))
 4775: 		.')').'</td>';
 4776: 	    $studentTable.='<td valign="top">&nbsp;<b>'.$title.'</b>&nbsp;</td>';
 4777: 
 4778: 	    my %newrecord=();
 4779: 	    my @displayPts=();
 4780:             my %aggregate = ();
 4781:             my $aggregateflag = 0;
 4782: 	    foreach my $partid (@{$parts}) {
 4783: 		my $newpts = $env{'form.GD_BOX'.$question.'_'.$partid};
 4784: 		my $oldpts = $env{'form.oldpts'.$question.'_'.$partid};
 4785: 
 4786: 		my $wgt = $env{'form.WGT'.$question.'_'.$partid} != 0 ? 
 4787: 		    $env{'form.WGT'.$question.'_'.$partid} : 1;
 4788: 		my $partial = $newpts/$wgt;
 4789: 		my $score;
 4790: 		if ($partial > 0) {
 4791: 		    $score = 'correct_by_override';
 4792: 		} elsif ($newpts ne '') { #empty is taken as 0
 4793: 		    $score = 'incorrect_by_override';
 4794: 		}
 4795: 		my $dropMenu = $env{'form.GD_SEL'.$question.'_'.$partid};
 4796: 		if ($dropMenu eq 'excused') {
 4797: 		    $partial = '';
 4798: 		    $score = 'excused';
 4799: 		} elsif ($dropMenu eq 'reset status'
 4800: 			 && $env{'form.solved'.$question.'_'.$partid} ne '') { #update only if previous record exists
 4801: 		    $newrecord{'resource.'.$partid.'.tries'} = 0;
 4802: 		    $newrecord{'resource.'.$partid.'.solved'} = '';
 4803: 		    $newrecord{'resource.'.$partid.'.award'} = '';
 4804: 		    $newrecord{'resource.'.$partid.'.awarded'} = 0;
 4805: 		    $newrecord{'resource.'.$partid.'.regrader'} = "$env{'user.name'}:$env{'user.domain'}";
 4806: 		    $changeflag++;
 4807: 		    $newpts = '';
 4808:                     
 4809:                     my $aggtries =  $env{'form.aggtries'.$question.'_'.$partid};
 4810:                     my $totaltries = $env{'form.totaltries'.$question.'_'.$partid};
 4811:                     my $solvedstatus = $env{'form.solved'.$question.'_'.$partid};
 4812:                     if ($aggtries > 0) {
 4813:                         &decrement_aggs($symbx,$partid,\%aggregate,$aggtries,$totaltries,$solvedstatus);
 4814:                         $aggregateflag = 1;
 4815:                     }
 4816: 		}
 4817: 		my $display_part=&get_display_part($partid,$curRes->symb());
 4818: 		my $oldstatus = $env{'form.solved'.$question.'_'.$partid};
 4819: 		$displayPts[0].='&nbsp;<b>'.&mt('Part').':</b> '.$display_part.' = '.
 4820: 		    (($oldstatus eq 'excused') ? 'excused' : $oldpts).
 4821: 		    '&nbsp;<br />';
 4822: 		$displayPts[1].='&nbsp;<b>'.&mt('Part').':</b> '.$display_part.' = '.
 4823: 		     (($score eq 'excused') ? 'excused' : $newpts).
 4824: 		    '&nbsp;<br />';
 4825: 		$question++;
 4826: 		next if ($dropMenu eq 'reset status' || ($newpts eq $oldpts && $score ne 'excused'));
 4827: 
 4828: 		$newrecord{'resource.'.$partid.'.awarded'}  = $partial if $partial ne '';
 4829: 		$newrecord{'resource.'.$partid.'.solved'}   = $score if $score ne '';
 4830: 		$newrecord{'resource.'.$partid.'.regrader'} = "$env{'user.name'}:$env{'user.domain'}"
 4831: 		    if (scalar(keys(%newrecord)) > 0);
 4832: 
 4833: 		$changeflag++;
 4834: 	    }
 4835: 	    if (scalar(keys(%newrecord)) > 0) {
 4836: 		my %record = 
 4837: 		    &Apache::lonnet::restore($symbx,$env{'request.course.id'},
 4838: 					     $udom,$uname);
 4839: 
 4840: 		if (&Apache::lonnet::validCODE($env{'form.CODE'})) {
 4841: 		    $newrecord{'resource.CODE'} = $env{'form.CODE'};
 4842: 		} elsif (&Apache::lonnet::validCODE($record{'resource.CODE'})) {
 4843: 		    $newrecord{'resource.CODE'} = '';
 4844: 		}
 4845: 		&Apache::lonnet::cstore(\%newrecord,$symbx,$env{'request.course.id'},
 4846: 					$udom,$uname);
 4847: 		%record = &Apache::lonnet::restore($symbx,
 4848: 						   $env{'request.course.id'},
 4849: 						   $udom,$uname);
 4850: 		&check_and_remove_from_queue($parts,\%record,undef,$symbx,
 4851: 					     $cdom,$cnum,$udom,$uname);
 4852: 	    }
 4853: 	    
 4854:             if ($aggregateflag) {
 4855:                 &Apache::lonnet::cinc('nohist_resourcetracker',\%aggregate,
 4856:                       $env{'course.'.$env{'request.course.id'}.'.domain'},
 4857:                       $env{'course.'.$env{'request.course.id'}.'.num'});
 4858:             }
 4859: 
 4860: 	    $studentTable.='<td valign="top">'.$displayPts[0].'</td>'.
 4861: 		'<td valign="top">'.$displayPts[1].'</td>'.
 4862: 		&Apache::loncommon::end_data_table_row();
 4863: 
 4864: 	    $prob++;
 4865: 	}
 4866:         $curRes = $iterator->next();
 4867:     }
 4868: 
 4869:     $studentTable.=&Apache::loncommon::end_data_table();
 4870:     $studentTable.=&show_grading_menu_form($env{'form.symb'});
 4871:     my $grademsg=($changeflag == 0 ? &mt('No score was changed or updated.') :
 4872: 		  &mt('The scores were changed for [quant,_1,problem].',
 4873: 		  $changeflag));
 4874:     $request->print($grademsg.$studentTable);
 4875: 
 4876:     return '';
 4877: }
 4878: 
 4879: #-------- end of section for handling grading by page/sequence ---------
 4880: #
 4881: #-------------------------------------------------------------------
 4882: 
 4883: #-------------------- Bubblesheet (Scantron) Grading -------------------
 4884: #
 4885: #------ start of section for handling grading by page/sequence ---------
 4886: 
 4887: =pod
 4888: 
 4889: =head1 Bubble sheet grading routines
 4890: 
 4891:   For this documentation:
 4892: 
 4893:    'scanline' refers to the full line of characters
 4894:    from the file that we are parsing that represents one entire sheet
 4895: 
 4896:    'bubble line' refers to the data
 4897:    representing the line of bubbles that are on the physical bubble sheet
 4898: 
 4899: 
 4900: The overall process is that a scanned in bubble sheet data is uploaded
 4901: into a course. When a user wants to grade, they select a
 4902: sequence/folder of resources, a file of bubble sheet info, and pick
 4903: one of the predefined configurations for what each scanline looks
 4904: like.
 4905: 
 4906: Next each scanline is checked for any errors of either 'missing
 4907: bubbles' (it's an error because it may have been mis-scanned
 4908: because too light bubbling), 'double bubble' (each bubble line should
 4909: have no more that one letter picked), invalid or duplicated CODE,
 4910: invalid student/employee ID
 4911: 
 4912: If the CODE option is used that determines the randomization of the
 4913: homework problems, either way the student/employee ID is looked up into a
 4914: username:domain.
 4915: 
 4916: During the validation phase the instructor can choose to skip scanlines. 
 4917: 
 4918: After the validation phase, there are now 3 bubble sheet files
 4919: 
 4920:   scantron_original_filename (unmodified original file)
 4921:   scantron_corrected_filename (file where the corrected information has replaced the original information)
 4922:   scantron_skipped_filename (contains the exact text of scanlines that where skipped)
 4923: 
 4924: Also there is a separate hash nohist_scantrondata that contains extra
 4925: correction information that isn't representable in the bubble sheet
 4926: file (see &scantron_getfile() for more information)
 4927: 
 4928: After all scanlines are either valid, marked as valid or skipped, then
 4929: foreach line foreach problem in the picked sequence, an ssi request is
 4930: made that simulates a user submitting their selected letter(s) against
 4931: the homework problem.
 4932: 
 4933: =over 4
 4934: 
 4935: 
 4936: 
 4937: =item defaultFormData
 4938: 
 4939:   Returns html hidden inputs used to hold context/default values.
 4940: 
 4941:  Arguments:
 4942:   $symb - $symb of the current resource 
 4943: 
 4944: =cut
 4945: 
 4946: sub defaultFormData {
 4947:     my ($symb)=@_;
 4948:     return '<input type="hidden" name="symb"    value="'.&Apache::lonenc::check_encrypt($symb).'" />'."\n".
 4949:      '<input type="hidden" name="saveState" value="'.$env{'form.saveState'}.'" />'."\n".
 4950:      '<input type="hidden" name="probTitle" value="'.$env{'form.probTitle'}.'" />'."\n";
 4951: }
 4952: 
 4953: 
 4954: =pod 
 4955: 
 4956: =item getSequenceDropDown
 4957: 
 4958:    Return html dropdown of possible sequences to grade
 4959:  
 4960:  Arguments:
 4961:    $symb - $symb of the current resource
 4962:    $map_error - ref to scalar which will container error if
 4963:                 $navmap object is unavailable in &getSymbMap().
 4964: 
 4965: =cut
 4966: 
 4967: sub getSequenceDropDown {
 4968:     my ($symb,$map_error)=@_;
 4969:     my $result='<select name="selectpage">'."\n";
 4970:     my ($titles,$symbx) = &getSymbMap($map_error);
 4971:     if (ref($map_error)) {
 4972:         return if ($$map_error);
 4973:     }
 4974:     my ($curpage)=&Apache::lonnet::decode_symb($symb); 
 4975:     my $ctr=0;
 4976:     foreach (@$titles) {
 4977: 	my ($minder,$showtitle) = ($_ =~ /(\d+)\.(.*)/);
 4978: 	$result.='<option value="'.$$symbx{$_}.'" '.
 4979: 	    ($$symbx{$_} =~ /$curpage$/ ? 'selected="selected"' : '').
 4980: 	    '>'.$showtitle.'</option>'."\n";
 4981: 	$ctr++;
 4982:     }
 4983:     $result.= '</select>';
 4984:     return $result;
 4985: }
 4986: 
 4987: my %bubble_lines_per_response;     # no. bubble lines for each response.
 4988:                                    # key is zero-based index - 0, 1, 2 ...
 4989: 
 4990: my %first_bubble_line;             # First bubble line no. for each bubble.
 4991: 
 4992: my %subdivided_bubble_lines;       # no. bubble lines for optionresponse, 
 4993:                                    # matchresponse or rankresponse, where 
 4994:                                    # an individual response can have multiple 
 4995:                                    # lines
 4996: 
 4997: my %responsetype_per_response;     # responsetype for each response
 4998: 
 4999: # Save and restore the bubble lines array to the form env.
 5000: 
 5001: 
 5002: sub save_bubble_lines {
 5003:     foreach my $line (keys(%bubble_lines_per_response)) {
 5004: 	$env{"form.scantron.bubblelines.$line"}  = $bubble_lines_per_response{$line};
 5005: 	$env{"form.scantron.first_bubble_line.$line"} =
 5006: 	    $first_bubble_line{$line};
 5007:         $env{"form.scantron.sub_bubblelines.$line"} = 
 5008:             $subdivided_bubble_lines{$line};
 5009:         $env{"form.scantron.responsetype.$line"} =
 5010:             $responsetype_per_response{$line};
 5011:     }
 5012: }
 5013: 
 5014: 
 5015: sub restore_bubble_lines {
 5016:     my $line = 0;
 5017:     %bubble_lines_per_response = ();
 5018:     while ($env{"form.scantron.bubblelines.$line"}) {
 5019: 	my $value = $env{"form.scantron.bubblelines.$line"};
 5020: 	$bubble_lines_per_response{$line} = $value;
 5021: 	$first_bubble_line{$line}  =
 5022: 	    $env{"form.scantron.first_bubble_line.$line"};
 5023:         $subdivided_bubble_lines{$line} =
 5024:             $env{"form.scantron.sub_bubblelines.$line"};
 5025:         $responsetype_per_response{$line} =
 5026:             $env{"form.scantron.responsetype.$line"};
 5027: 	$line++;
 5028:     }
 5029: }
 5030: 
 5031: #  Given the parsed scanline, get the response for 
 5032: #  'answer' number n:
 5033: 
 5034: sub get_response_bubbles {
 5035:     my ($parsed_line, $response)  = @_;
 5036: 
 5037:     my $bubble_line = $first_bubble_line{$response-1} +1;
 5038:     my $bubble_lines= $bubble_lines_per_response{$response-1};
 5039:     
 5040:     my $selected = "";
 5041: 
 5042:     for (my $bline = 0; $bline < $bubble_lines; $bline++) {
 5043: 	$selected .= $$parsed_line{"scantron.$bubble_line.answer"}.":";
 5044: 	$bubble_line++;
 5045:     }
 5046:     return $selected;
 5047: }
 5048: 
 5049: =pod 
 5050: 
 5051: =item scantron_filenames
 5052: 
 5053:    Returns a list of the scantron files in the current course 
 5054: 
 5055: =cut
 5056: 
 5057: sub scantron_filenames {
 5058:     my $cdom=$env{'course.'.$env{'request.course.id'}.'.domain'};
 5059:     my $cname=$env{'course.'.$env{'request.course.id'}.'.num'};
 5060:     my $getpropath = 1;
 5061:     my @files=&Apache::lonnet::dirlist('userfiles',$cdom,$cname,
 5062:                                        $getpropath);
 5063:     my @possiblenames;
 5064:     foreach my $filename (sort(@files)) {
 5065: 	($filename)=split(/&/,$filename);
 5066: 	if ($filename!~/^scantron_orig_/) { next ; }
 5067: 	$filename=~s/^scantron_orig_//;
 5068: 	push(@possiblenames,$filename);
 5069:     }
 5070:     return @possiblenames;
 5071: }
 5072: 
 5073: =pod 
 5074: 
 5075: =item scantron_uploads
 5076: 
 5077:    Returns  html drop-down list of scantron files in current course.
 5078: 
 5079:  Arguments:
 5080:    $file2grade - filename to set as selected in the dropdown
 5081: 
 5082: =cut
 5083: 
 5084: sub scantron_uploads {
 5085:     my ($file2grade) = @_;
 5086:     my $result=	'<select name="scantron_selectfile">';
 5087:     $result.="<option></option>";
 5088:     foreach my $filename (sort(&scantron_filenames())) {
 5089: 	$result.="<option".($filename eq $file2grade ? ' selected="selected"':'').">$filename</option>\n";
 5090:     }
 5091:     $result.="</select>";
 5092:     return $result;
 5093: }
 5094: 
 5095: =pod 
 5096: 
 5097: =item scantron_scantab
 5098: 
 5099:   Returns html drop down of the scantron formats in the scantronformat.tab
 5100:   file.
 5101: 
 5102: =cut
 5103: 
 5104: sub scantron_scantab {
 5105:     my $result='<select name="scantron_format">'."\n";
 5106:     $result.='<option></option>'."\n";
 5107:     my @lines = &get_scantronformat_file();
 5108:     if (@lines > 0) {
 5109:         foreach my $line (@lines) {
 5110:             next if (($line =~ /^\#/) || ($line eq ''));
 5111: 	    my ($name,$descrip)=split(/:/,$line);
 5112: 	    $result.='<option value="'.$name.'">'.$descrip.'</option>'."\n";
 5113:         }
 5114:     }
 5115:     $result.='</select>'."\n";
 5116:     return $result;
 5117: }
 5118: 
 5119: =pod
 5120: 
 5121: =item get_scantronformat_file
 5122: 
 5123:   Returns an array containing lines from the scantron format file for
 5124:   the domain of the course.
 5125: 
 5126:   If a url for a custom.tab file is listed in domain's configuration.db, 
 5127:   lines are from this file.
 5128: 
 5129:   Otherwise, if a default.tab has been published in RES space by the 
 5130:   domainconfig user, lines are from this file.
 5131: 
 5132:   Otherwise, fall back to getting lines from the legacy file on the
 5133:   local server:  /home/httpd/lonTabs/default_scantronformat.tab    
 5134: 
 5135: =cut
 5136: 
 5137: sub get_scantronformat_file {
 5138:     my $cdom= $env{'course.'.$env{'request.course.id'}.'.domain'};
 5139:     my %domconfig = &Apache::lonnet::get_dom('configuration',['scantron'],$cdom);
 5140:     my $gottab = 0;
 5141:     my @lines;
 5142:     if (ref($domconfig{'scantron'}) eq 'HASH') {
 5143:         if ($domconfig{'scantron'}{'scantronformat'} ne '') {
 5144:             my $formatfile = &Apache::lonnet::getfile($Apache::lonnet::perlvar{'lonDocRoot'}.$domconfig{'scantron'}{'scantronformat'});
 5145:             if ($formatfile ne '-1') {
 5146:                 @lines = split("\n",$formatfile,-1);
 5147:                 $gottab = 1;
 5148:             }
 5149:         }
 5150:     }
 5151:     if (!$gottab) {
 5152:         my $confname = $cdom.'-domainconfig';
 5153:         my $default = $Apache::lonnet::perlvar{'lonDocRoot'}.'/res/'.$cdom.'/'.$confname.'/default.tab';
 5154:         my $formatfile =  &Apache::lonnet::getfile($default);
 5155:         if ($formatfile ne '-1') {
 5156:             @lines = split("\n",$formatfile,-1);
 5157:             $gottab = 1;
 5158:         }
 5159:     }
 5160:     if (!$gottab) {
 5161:         my @domains = &Apache::lonnet::current_machine_domains();
 5162:         if (grep(/^\Q$cdom\E$/,@domains)) {
 5163:             my $fh=Apache::File->new($Apache::lonnet::perlvar{'lonTabDir'}.'/scantronformat.tab');
 5164:             @lines = <$fh>;
 5165:             close($fh);
 5166:         } else {
 5167:             my $fh=Apache::File->new($Apache::lonnet::perlvar{'lonTabDir'}.'/default_scantronformat.tab');
 5168:             @lines = <$fh>;
 5169:             close($fh);
 5170:         }
 5171:     }
 5172:     return @lines;
 5173: }
 5174: 
 5175: =pod 
 5176: 
 5177: =item scantron_CODElist
 5178: 
 5179:   Returns html drop down of the saved CODE lists from current course,
 5180:   generated from earlier printings.
 5181: 
 5182: =cut
 5183: 
 5184: sub scantron_CODElist {
 5185:     my $cdom = $env{'course.'.$env{'request.course.id'}.'.domain'};
 5186:     my $cnum = $env{'course.'.$env{'request.course.id'}.'.num'};
 5187:     my @names=&Apache::lonnet::getkeys('CODEs',$cdom,$cnum);
 5188:     my $namechoice='<option></option>';
 5189:     foreach my $name (sort {uc($a) cmp uc($b)} @names) {
 5190: 	if ($name =~ /^error: 2 /) { next; }
 5191: 	if ($name =~ /^type\0/) { next; }
 5192: 	$namechoice.='<option value="'.$name.'">'.$name.'</option>';
 5193:     }
 5194:     $namechoice='<select name="scantron_CODElist">'.$namechoice.'</select>';
 5195:     return $namechoice;
 5196: }
 5197: 
 5198: =pod 
 5199: 
 5200: =item scantron_CODEunique
 5201: 
 5202:   Returns the html for "Each CODE to be used once" radio.
 5203: 
 5204: =cut
 5205: 
 5206: sub scantron_CODEunique {
 5207:     my $result='<span class="LC_nobreak">
 5208:                  <label><input type="radio" name="scantron_CODEunique"
 5209:                         value="yes" checked="checked" />'.&mt('Yes').' </label>
 5210:                 </span>
 5211:                 <span class="LC_nobreak">
 5212:                  <label><input type="radio" name="scantron_CODEunique"
 5213:                         value="no" />'.&mt('No').' </label>
 5214:                 </span>';
 5215:     return $result;
 5216: }
 5217: 
 5218: =pod 
 5219: 
 5220: =item scantron_selectphase
 5221: 
 5222:   Generates the initial screen to start the bubble sheet process.
 5223:   Allows for - starting a grading run.
 5224:              - downloading existing scan data (original, corrected
 5225:                                                 or skipped info)
 5226: 
 5227:              - uploading new scan data
 5228: 
 5229:  Arguments:
 5230:   $r          - The Apache request object
 5231:   $file2grade - name of the file that contain the scanned data to score
 5232: 
 5233: =cut
 5234: 
 5235: sub scantron_selectphase {
 5236:     my ($r,$file2grade) = @_;
 5237:     my ($symb)=&get_symb($r);
 5238:     if (!$symb) {return '';}
 5239:     my $map_error;
 5240:     my $sequence_selector=&getSequenceDropDown($symb,\$map_error);
 5241:     if ($map_error) {
 5242:         $r->print('<br />'.&navmap_errormsg().'<br />');
 5243:         return;
 5244:     }
 5245:     my $default_form_data=&defaultFormData($symb);
 5246:     my $grading_menu_button=&show_grading_menu_form($symb);
 5247:     my $file_selector=&scantron_uploads($file2grade);
 5248:     my $format_selector=&scantron_scantab();
 5249:     my $CODE_selector=&scantron_CODElist();
 5250:     my $CODE_unique=&scantron_CODEunique();
 5251:     my $result;
 5252: 
 5253:     $ssi_error = 0;
 5254: 
 5255:     # Chunk of form to prompt for a file to grade and how:
 5256: 
 5257:     $result.= '
 5258:     <br />
 5259:     <form method="post" enctype="multipart/form-data" action="/adm/grades" name="scantron_process">
 5260:     <input type="hidden" name="command" value="scantron_warning" />
 5261:     '.$default_form_data.'
 5262:     '.&Apache::loncommon::start_data_table('LC_scantron_action').'
 5263:        '.&Apache::loncommon::start_data_table_header_row().'
 5264:             <th colspan="2">
 5265:               &nbsp;'.&mt('Specify file and which Folder/Sequence to grade').'
 5266:             </th>
 5267:        '.&Apache::loncommon::end_data_table_header_row().'
 5268:        '.&Apache::loncommon::start_data_table_row().'
 5269:             <td> '.&mt('Sequence to grade:').' </td><td> '.$sequence_selector.' </td>
 5270:        '.&Apache::loncommon::end_data_table_row().'
 5271:        '.&Apache::loncommon::start_data_table_row().'
 5272:             <td> '.&mt('Filename of bubblesheet data file:').' </td><td> '.$file_selector.' </td>
 5273:        '.&Apache::loncommon::end_data_table_row().'
 5274:        '.&Apache::loncommon::start_data_table_row().'
 5275:             <td> '.&mt('Format of bubblesheet data file:').' </td><td> '.$format_selector.' </td>
 5276:        '.&Apache::loncommon::end_data_table_row().'
 5277:        '.&Apache::loncommon::start_data_table_row().'
 5278:             <td> '.&mt('Saved CODEs to validate against:').' </td><td> '.$CODE_selector.' </td>
 5279:        '.&Apache::loncommon::end_data_table_row().'
 5280:        '.&Apache::loncommon::start_data_table_row().'
 5281:             <td> '.&mt('Each CODE is only to be used once:').'</td><td> '.$CODE_unique.' </td>
 5282:        '.&Apache::loncommon::end_data_table_row().'
 5283:        '.&Apache::loncommon::start_data_table_row().'
 5284: 	    <td> '.&mt('Options:').' </td>
 5285:             <td>
 5286: 	       <label><input type="checkbox" name="scantron_options_redo" value="redo_skipped"/> '.&mt('Do only previously skipped records').'</label> <br />
 5287:                <label><input type="checkbox" name="scantron_options_ignore" value="ignore_corrections"/> '.&mt('Remove all existing corrections').'</label> <br />
 5288:                <label><input type="checkbox" name="scantron_options_hidden" value="ignore_hidden"/> '.&mt('Skip hidden resources when grading').'</label>
 5289: 	    </td>
 5290:        '.&Apache::loncommon::end_data_table_row().'
 5291:        '.&Apache::loncommon::start_data_table_row().'
 5292:             <td colspan="2">
 5293:               <input type="submit" value="'.&mt('Grading: Validate Bubblesheet Records').'" />
 5294:             </td>
 5295:        '.&Apache::loncommon::end_data_table_row().'
 5296:     '.&Apache::loncommon::end_data_table().'
 5297:     </form>
 5298: ';
 5299:    
 5300:     $r->print($result);
 5301: 
 5302:     if (&Apache::lonnet::allowed('usc',$env{'request.role.domain'}) ||
 5303:         &Apache::lonnet::allowed('usc',$env{'request.course.id'})) {
 5304: 
 5305: 	# Chunk of form to prompt for a scantron file upload.
 5306: 
 5307:         $r->print('
 5308:     <br />
 5309:     '.&Apache::loncommon::start_data_table('LC_scantron_action').'
 5310:        '.&Apache::loncommon::start_data_table_header_row().'
 5311:             <th>
 5312:               &nbsp;'.&mt('Specify a bubblesheet data file to upload.').'
 5313:             </th>
 5314:        '.&Apache::loncommon::end_data_table_header_row().'
 5315:        '.&Apache::loncommon::start_data_table_row().'
 5316:             <td>
 5317: ');
 5318:     my $default_form_data=&defaultFormData(&get_symb($r,1));
 5319:     my $cdom= $env{'course.'.$env{'request.course.id'}.'.domain'};
 5320:     my $cnum= $env{'course.'.$env{'request.course.id'}.'.num'};
 5321:     $r->print('
 5322:               <script type="text/javascript" language="javascript">
 5323:     function checkUpload(formname) {
 5324: 	if (formname.upfile.value == "") {
 5325: 	    alert("'.&mt('Please use the browse button to select a file from your local directory.').'");
 5326: 	    return false;
 5327: 	}
 5328: 	formname.submit();
 5329:     }
 5330:               </script>
 5331: 
 5332:               <form enctype="multipart/form-data" action="/adm/grades" name="rules" method="post">
 5333:                 '.$default_form_data.'
 5334:                 <input name="courseid" type="hidden" value="'.$cnum.'" />
 5335:                 <input name="domainid" type="hidden" value="'.$cdom.'" />
 5336:                 <input name="command" value="scantronupload_save" type="hidden" />
 5337:                 '.&mt('File to upload: [_1]','<input type="file" name="upfile" size="50" />').'
 5338:                 <br />
 5339:                 <input type="button" onclick="javascript:checkUpload(this.form);" value="'.&mt('Upload Bubblesheet Data').'" />
 5340:               </form>
 5341: ');
 5342: 
 5343:         $r->print('
 5344:             </td>
 5345:        '.&Apache::loncommon::end_data_table_row().'
 5346:        '.&Apache::loncommon::end_data_table().'
 5347: ');
 5348:     }
 5349: 
 5350:     # Chunk of the form that prompts to view a scoring office file,
 5351:     # corrected file, skipped records in a file.
 5352: 
 5353:     $r->print('
 5354:    <br />
 5355:    <form action="/adm/grades" name="scantron_download">
 5356:      '.$default_form_data.'
 5357:      <input type="hidden" name="command" value="scantron_download" />
 5358:      '.&Apache::loncommon::start_data_table('LC_scantron_action').'
 5359:        '.&Apache::loncommon::start_data_table_header_row().'
 5360:               <th>
 5361:                 &nbsp;'.&mt('Download a scoring office file').'
 5362:               </th>
 5363:        '.&Apache::loncommon::end_data_table_header_row().'
 5364:        '.&Apache::loncommon::start_data_table_row().'
 5365:               <td> '.&mt('Filename of scoring office file: [_1]',$file_selector).' 
 5366:                 <br />
 5367:                 <input type="submit" value="'.&mt('Download: Show List of Associated Files').'" />
 5368:        '.&Apache::loncommon::end_data_table_row().'
 5369:      '.&Apache::loncommon::end_data_table().'
 5370:    </form>
 5371:    <br />
 5372: ');
 5373: 
 5374:     &Apache::lonpickcode::code_list($r,2);
 5375: 
 5376:     $r->print('<br /><form method="post" name="checkscantron">'.
 5377:              $default_form_data."\n".
 5378:              &Apache::loncommon::start_data_table('LC_scantron_action')."\n".
 5379:              &Apache::loncommon::start_data_table_header_row()."\n".
 5380:              '<th colspan="2">
 5381:               &nbsp;'.&mt('Review bubblesheet data and submissions for a previously graded folder/sequence')."\n".
 5382:              '</th>'."\n".
 5383:               &Apache::loncommon::end_data_table_header_row()."\n".
 5384:               &Apache::loncommon::start_data_table_row()."\n".
 5385:               '<td> '.&mt('Graded folder/sequence:').' </td>'."\n".
 5386:               '<td> '.$sequence_selector.' </td>'.
 5387:               &Apache::loncommon::end_data_table_row()."\n".
 5388:               &Apache::loncommon::start_data_table_row()."\n".
 5389:               '<td> '.&mt('Filename of scoring office file:').' </td>'."\n".
 5390:               '<td> '.$file_selector.' </td>'."\n".
 5391:               &Apache::loncommon::end_data_table_row()."\n".
 5392:               &Apache::loncommon::start_data_table_row()."\n".
 5393:               '<td> '.&mt('Format of data file:').' </td>'."\n".
 5394:               '<td> '.$format_selector.' </td>'."\n".
 5395:               &Apache::loncommon::end_data_table_row()."\n".
 5396:               &Apache::loncommon::start_data_table_row()."\n".
 5397:               '<td> '.&mt('Options').' </td>'."\n".
 5398:               '<td> <label><input type="checkbox" name="scantron_options_hidden" value="ignore_hidden"/> '.&mt('Skip hidden resources').'</label></td>'.
 5399:               &Apache::loncommon::end_data_table_row()."\n".
 5400:               &Apache::loncommon::start_data_table_row()."\n".
 5401:               '<td colspan="2">'."\n".
 5402:               '<input type="hidden" name="command" value="checksubmissions" />'."\n".
 5403:               '<input type="submit" value="'.&mt('Review Bubblesheet Data and Submission Records').'" />'."\n".
 5404:               '</td>'."\n".
 5405:               &Apache::loncommon::end_data_table_row()."\n".
 5406:               &Apache::loncommon::end_data_table()."\n".
 5407:               '</form><br />');
 5408:     $r->print($grading_menu_button);
 5409:     return;
 5410: }
 5411: 
 5412: =pod
 5413: 
 5414: =item get_scantron_config
 5415: 
 5416:    Parse and return the scantron configuration line selected as a
 5417:    hash of configuration file fields.
 5418: 
 5419:  Arguments:
 5420:     which - the name of the configuration to parse from the file.
 5421: 
 5422: 
 5423:  Returns:
 5424:             If the named configuration is not in the file, an empty
 5425:             hash is returned.
 5426:     a hash with the fields
 5427:       name         - internal name for the this configuration setup
 5428:       description  - text to display to operator that describes this config
 5429:       CODElocation - if 0 or the string 'none'
 5430:                           - no CODE exists for this config
 5431:                      if -1 || the string 'letter'
 5432:                           - a CODE exists for this config and is
 5433:                             a string of letters
 5434:                      Unsupported value (but planned for future support)
 5435:                           if a positive integer
 5436:                                - The CODE exists as the first n items from
 5437:                                  the question section of the form
 5438:                           if the string 'number'
 5439:                                - The CODE exists for this config and is
 5440:                                  a string of numbers
 5441:       CODEstart   - (only matter if a CODE exists) column in the line where
 5442:                      the CODE starts
 5443:       CODElength  - length of the CODE
 5444:       IDstart     - column where the student/employee ID starts
 5445:       IDlength    - length of the student/employee ID info
 5446:       Qstart      - column where the information from the bubbled
 5447:                     'questions' start
 5448:       Qlength     - number of columns comprising a single bubble line from
 5449:                     the sheet. (usually either 1 or 10)
 5450:       Qon         - either a single character representing the character used
 5451:                     to signal a bubble was chosen in the positional setup, or
 5452:                     the string 'letter' if the letter of the chosen bubble is
 5453:                     in the final, or 'number' if a number representing the
 5454:                     chosen bubble is in the file (1->A 0->J)
 5455:       Qoff        - the character used to represent that a bubble was
 5456:                     left blank
 5457:       PaperID     - if the scanning process generates a unique number for each
 5458:                     sheet scanned the column that this ID number starts in
 5459:       PaperIDlength - number of columns that comprise the unique ID number
 5460:                       for the sheet of paper
 5461:       FirstName   - column that the first name starts in
 5462:       FirstNameLength - number of columns that the first name spans
 5463:  
 5464:       LastName    - column that the last name starts in
 5465:       LastNameLength - number of columns that the last name spans
 5466: 
 5467: =cut
 5468: 
 5469: sub get_scantron_config {
 5470:     my ($which) = @_;
 5471:     my @lines = &get_scantronformat_file();
 5472:     my %config;
 5473:     #FIXME probably should move to XML it has already gotten a bit much now
 5474:     foreach my $line (@lines) {
 5475: 	my ($name,$descrip)=split(/:/,$line);
 5476: 	if ($name ne $which ) { next; }
 5477: 	chomp($line);
 5478: 	my @config=split(/:/,$line);
 5479: 	$config{'name'}=$config[0];
 5480: 	$config{'description'}=$config[1];
 5481: 	$config{'CODElocation'}=$config[2];
 5482: 	$config{'CODEstart'}=$config[3];
 5483: 	$config{'CODElength'}=$config[4];
 5484: 	$config{'IDstart'}=$config[5];
 5485: 	$config{'IDlength'}=$config[6];
 5486: 	$config{'Qstart'}=$config[7];
 5487:  	$config{'Qlength'}=$config[8];
 5488: 	$config{'Qoff'}=$config[9];
 5489: 	$config{'Qon'}=$config[10];
 5490: 	$config{'PaperID'}=$config[11];
 5491: 	$config{'PaperIDlength'}=$config[12];
 5492: 	$config{'FirstName'}=$config[13];
 5493: 	$config{'FirstNamelength'}=$config[14];
 5494: 	$config{'LastName'}=$config[15];
 5495: 	$config{'LastNamelength'}=$config[16];
 5496: 	last;
 5497:     }
 5498:     return %config;
 5499: }
 5500: 
 5501: =pod 
 5502: 
 5503: =item username_to_idmap
 5504: 
 5505:     creates a hash keyed by student/employee ID with values of the corresponding
 5506:     student username:domain.
 5507: 
 5508:   Arguments:
 5509: 
 5510:     $classlist - reference to the class list hash. This is a hash
 5511:                  keyed by student name:domain  whose elements are references
 5512:                  to arrays containing various chunks of information
 5513:                  about the student. (See loncoursedata for more info).
 5514: 
 5515:   Returns
 5516:     %idmap - the constructed hash
 5517: 
 5518: =cut
 5519: 
 5520: sub username_to_idmap {
 5521:     my ($classlist)= @_;
 5522:     my %idmap;
 5523:     foreach my $student (keys(%$classlist)) {
 5524: 	$idmap{$classlist->{$student}->[&Apache::loncoursedata::CL_ID]}=
 5525: 	    $student;
 5526:     }
 5527:     return %idmap;
 5528: }
 5529: 
 5530: =pod
 5531: 
 5532: =item scantron_fixup_scanline
 5533: 
 5534:    Process a requested correction to a scanline.
 5535: 
 5536:   Arguments:
 5537:     $scantron_config   - hash from &get_scantron_config()
 5538:     $scan_data         - hash of correction information 
 5539:                           (see &scantron_getfile())
 5540:     $line              - existing scanline
 5541:     $whichline         - line number of the passed in scanline
 5542:     $field             - type of change to process 
 5543:                          (either 
 5544:                           'ID'     -> correct the student/employee ID
 5545:                           'CODE'   -> correct the CODE
 5546:                           'answer' -> fixup the submitted answers)
 5547:     
 5548:    $args               - hash of additional info,
 5549:                           - 'ID' 
 5550:                                'newid' -> studentID to use in replacement
 5551:                                           of existing one
 5552:                           - 'CODE' 
 5553:                                'CODE_ignore_dup' - set to true if duplicates
 5554:                                                    should be ignored.
 5555: 	                       'CODE' - is new code or 'use_unfound'
 5556:                                         if the existing unfound code should
 5557:                                         be used as is
 5558:                           - 'answer'
 5559:                                'response' - new answer or 'none' if blank
 5560:                                'question' - the bubble line to change
 5561:                                'questionnum' - the question identifier,
 5562:                                                may include subquestion. 
 5563: 
 5564:   Returns:
 5565:     $line - the modified scanline
 5566: 
 5567:   Side effects: 
 5568:     $scan_data - may be updated
 5569: 
 5570: =cut
 5571: 
 5572: 
 5573: sub scantron_fixup_scanline {
 5574:     my ($scantron_config,$scan_data,$line,$whichline,$field,$args)=@_;
 5575:     if ($field eq 'ID') {
 5576: 	if (length($args->{'newid'}) > $$scantron_config{'IDlength'}) {
 5577: 	    return ($line,1,'New value too large');
 5578: 	}
 5579: 	if (length($args->{'newid'}) < $$scantron_config{'IDlength'}) {
 5580: 	    $args->{'newid'}=sprintf('%-'.$$scantron_config{'IDlength'}.'s',
 5581: 				     $args->{'newid'});
 5582: 	}
 5583: 	substr($line,$$scantron_config{'IDstart'}-1,
 5584: 	       $$scantron_config{'IDlength'})=$args->{'newid'};
 5585: 	if ($args->{'newid'}=~/^\s*$/) {
 5586: 	    &scan_data($scan_data,"$whichline.user",
 5587: 		       $args->{'username'}.':'.$args->{'domain'});
 5588: 	}
 5589:     } elsif ($field eq 'CODE') {
 5590: 	if ($args->{'CODE_ignore_dup'}) {
 5591: 	    &scan_data($scan_data,"$whichline.CODE_ignore_dup",'1');
 5592: 	}
 5593: 	&scan_data($scan_data,"$whichline.useCODE",'1');
 5594: 	if ($args->{'CODE'} ne 'use_unfound') {
 5595: 	    if (length($args->{'CODE'}) > $$scantron_config{'CODElength'}) {
 5596: 		return ($line,1,'New CODE value too large');
 5597: 	    }
 5598: 	    if (length($args->{'CODE'}) < $$scantron_config{'CODElength'}) {
 5599: 		$args->{'CODE'}=sprintf('%-'.$$scantron_config{'CODElength'}.'s',$args->{'CODE'});
 5600: 	    }
 5601: 	    substr($line,$$scantron_config{'CODEstart'}-1,
 5602: 		   $$scantron_config{'CODElength'})=$args->{'CODE'};
 5603: 	}
 5604:     } elsif ($field eq 'answer') {
 5605: 	my $length=$scantron_config->{'Qlength'};
 5606: 	my $off=$scantron_config->{'Qoff'};
 5607: 	my $on=$scantron_config->{'Qon'};
 5608: 	my $answer=${off}x$length;
 5609: 	if ($args->{'response'} eq 'none') {
 5610: 	    &scan_data($scan_data,
 5611: 		       "$whichline.no_bubble.".$args->{'questionnum'},'1');
 5612: 	} else {
 5613: 	    if ($on eq 'letter') {
 5614: 		my @alphabet=('A'..'Z');
 5615: 		$answer=$alphabet[$args->{'response'}];
 5616: 	    } elsif ($on eq 'number') {
 5617: 		$answer=$args->{'response'}+1;
 5618: 		if ($answer == 10) { $answer = '0'; }
 5619: 	    } else {
 5620: 		substr($answer,$args->{'response'},1)=$on;
 5621: 	    }
 5622: 	    &scan_data($scan_data,
 5623: 		       "$whichline.no_bubble.".$args->{'questionnum'},undef,'1');
 5624: 	}
 5625: 	my $where=$length*($args->{'question'}-1)+$scantron_config->{'Qstart'};
 5626: 	substr($line,$where-1,$length)=$answer;
 5627:     }
 5628:     return $line;
 5629: }
 5630: 
 5631: =pod
 5632: 
 5633: =item scan_data
 5634: 
 5635:     Edit or look up  an item in the scan_data hash.
 5636: 
 5637:   Arguments:
 5638:     $scan_data  - The hash (see scantron_getfile)
 5639:     $key        - shorthand of the key to edit (actual key is
 5640:                   scantronfilename_key).
 5641:     $data        - New value of the hash entry.
 5642:     $delete      - If true, the entry is removed from the hash.
 5643: 
 5644:   Returns:
 5645:     The new value of the hash table field (undefined if deleted).
 5646: 
 5647: =cut
 5648: 
 5649: 
 5650: sub scan_data {
 5651:     my ($scan_data,$key,$value,$delete)=@_;
 5652:     my $filename=$env{'form.scantron_selectfile'};
 5653:     if (defined($value)) {
 5654: 	$scan_data->{$filename.'_'.$key} = $value;
 5655:     }
 5656:     if ($delete) { delete($scan_data->{$filename.'_'.$key}); }
 5657:     return $scan_data->{$filename.'_'.$key};
 5658: }
 5659: 
 5660: # ----- These first few routines are general use routines.----
 5661: 
 5662: # Return the number of occurences of a pattern in a string.
 5663: 
 5664: sub occurence_count {
 5665:     my ($string, $pattern) = @_;
 5666: 
 5667:     my @matches = ($string =~ /$pattern/g);
 5668: 
 5669:     return scalar(@matches);
 5670: }
 5671: 
 5672: 
 5673: # Take a string known to have digits and convert all the
 5674: # digits into letters in the range J,A..I.
 5675: 
 5676: sub digits_to_letters {
 5677:     my ($input) = @_;
 5678: 
 5679:     my @alphabet = ('J', 'A'..'I');
 5680: 
 5681:     my @input    = split(//, $input);
 5682:     my $output ='';
 5683:     for (my $i = 0; $i < scalar(@input); $i++) {
 5684: 	if ($input[$i] =~ /\d/) {
 5685: 	    $output .= $alphabet[$input[$i]];
 5686: 	} else {
 5687: 	    $output .= $input[$i];
 5688: 	}
 5689:     }
 5690:     return $output;
 5691: }
 5692: 
 5693: =pod 
 5694: 
 5695: =item scantron_parse_scanline
 5696: 
 5697:   Decodes a scanline from the selected scantron file
 5698: 
 5699:  Arguments:
 5700:     line             - The text of the scantron file line to process
 5701:     whichline        - Line number
 5702:     scantron_config  - Hash describing the format of the scantron lines.
 5703:     scan_data        - Hash of extra information about the scanline
 5704:                        (see scantron_getfile for more information)
 5705:     just_header      - True if should not process question answers but only
 5706:                        the stuff to the left of the answers.
 5707:  Returns:
 5708:    Hash containing the result of parsing the scanline
 5709: 
 5710:    Keys are all proceeded by the string 'scantron.'
 5711: 
 5712:        CODE    - the CODE in use for this scanline
 5713:        useCODE - 1 if the CODE is invalid but it usage has been forced
 5714:                  by the operator
 5715:        CODE_ignore_dup - 1 if the CODE is a duplicated use when unique
 5716:                             CODEs were selected, but the usage has been
 5717:                             forced by the operator
 5718:        ID  - student/employee ID
 5719:        PaperID - if used, the ID number printed on the sheet when the 
 5720:                  paper was scanned
 5721:        FirstName - first name from the sheet
 5722:        LastName  - last name from the sheet
 5723: 
 5724:      if just_header was not true these key may also exist
 5725: 
 5726:        missingerror - a list of bubble ranges that are considered to be answers
 5727:                       to a single question that don't have any bubbles filled in.
 5728:                       Of the form questionnumber:firstbubblenumber:count.
 5729:        doubleerror  - a list of bubble ranges that are considered to be answers
 5730:                       to a single question that have more than one bubble filled in.
 5731:                       Of the form questionnumber::firstbubblenumber:count
 5732:    
 5733:                 In the above, count is the number of bubble responses in the
 5734:                 input line needed to represent the possible answers to the question.
 5735:                 e.g. a radioresponse with 15 choices in an answer sheet with 10 choices
 5736:                 per line would have count = 2.
 5737: 
 5738:        maxquest     - the number of the last bubble line that was parsed
 5739: 
 5740:        (<number> starts at 1)
 5741:        <number>.answer - zero or more letters representing the selected
 5742:                          letters from the scanline for the bubble line 
 5743:                          <number>.
 5744:                          if blank there was either no bubble or there where
 5745:                          multiple bubbles, (consult the keys missingerror and
 5746:                          doubleerror if this is an error condition)
 5747: 
 5748: =cut
 5749: 
 5750: sub scantron_parse_scanline {
 5751:     my ($line,$whichline,$scantron_config,$scan_data,$just_header)=@_;
 5752: 
 5753:     my %record;
 5754:     my $lastpos = $env{'form.scantron_maxbubble'}*$$scantron_config{'Qlength'};
 5755:     my $questions=substr($line,$$scantron_config{'Qstart'}-1,$lastpos);  # Answers
 5756:     my $data=substr($line,0,$$scantron_config{'Qstart'}-1);     # earlier stuff
 5757:     if (!($$scantron_config{'CODElocation'} eq 0 ||
 5758: 	  $$scantron_config{'CODElocation'} eq 'none')) {
 5759: 	if ($$scantron_config{'CODElocation'} < 0 ||
 5760: 	    $$scantron_config{'CODElocation'} eq 'letter' ||
 5761: 	    $$scantron_config{'CODElocation'} eq 'number') {
 5762: 	    $record{'scantron.CODE'}=substr($data,
 5763: 					    $$scantron_config{'CODEstart'}-1,
 5764: 					    $$scantron_config{'CODElength'});
 5765: 	    if (&scan_data($scan_data,"$whichline.useCODE")) {
 5766: 		$record{'scantron.useCODE'}=1;
 5767: 	    }
 5768: 	    if (&scan_data($scan_data,"$whichline.CODE_ignore_dup")) {
 5769: 		$record{'scantron.CODE_ignore_dup'}=1;
 5770: 	    }
 5771: 	} else {
 5772: 	    #FIXME interpret first N questions
 5773: 	}
 5774:     }
 5775:     $record{'scantron.ID'}=substr($data,$$scantron_config{'IDstart'}-1,
 5776: 				  $$scantron_config{'IDlength'});
 5777:     $record{'scantron.PaperID'}=
 5778: 	substr($data,$$scantron_config{'PaperID'}-1,
 5779: 	       $$scantron_config{'PaperIDlength'});
 5780:     $record{'scantron.FirstName'}=
 5781: 	substr($data,$$scantron_config{'FirstName'}-1,
 5782: 	       $$scantron_config{'FirstNamelength'});
 5783:     $record{'scantron.LastName'}=
 5784: 	substr($data,$$scantron_config{'LastName'}-1,
 5785: 	       $$scantron_config{'LastNamelength'});
 5786:     if ($just_header) { return \%record; }
 5787: 
 5788:     my @alphabet=('A'..'Z');
 5789:     my $questnum=0;
 5790:     my $ansnum  =1;		# Multiple 'answer lines'/question.
 5791: 
 5792:     chomp($questions);		# Get rid of any trailing \n.
 5793:     $questions =~ s/\r$//;      # Get rid of trailing \r too (MAC or Win uploads).
 5794:     while (length($questions)) {
 5795: 	my $answers_needed = $bubble_lines_per_response{$questnum};
 5796:         my $answer_length  = ($$scantron_config{'Qlength'} * $answers_needed)
 5797:                              || 1;
 5798:         $questnum++;
 5799:         my $quest_id = $questnum;
 5800:         my $currentquest = substr($questions,0,$answer_length);
 5801:         $questions       = substr($questions,$answer_length);
 5802:         if (length($currentquest) < $answer_length) { next; }
 5803: 
 5804:         if ($subdivided_bubble_lines{$questnum-1} =~ /,/) {
 5805:             my $subquestnum = 1;
 5806:             my $subquestions = $currentquest;
 5807:             my @subanswers_needed = 
 5808:                 split(/,/,$subdivided_bubble_lines{$questnum-1});  
 5809:             foreach my $subans (@subanswers_needed) {
 5810:                 my $subans_length =
 5811:                     ($$scantron_config{'Qlength'} * $subans)  || 1;
 5812:                 my $currsubquest = substr($subquestions,0,$subans_length);
 5813:                 $subquestions   = substr($subquestions,$subans_length);
 5814:                 $quest_id = "$questnum.$subquestnum";
 5815:                 if (($$scantron_config{'Qon'} eq 'letter') ||
 5816:                     ($$scantron_config{'Qon'} eq 'number')) {
 5817:                     $ansnum = &scantron_validator_lettnum($ansnum, 
 5818:                         $questnum,$quest_id,$subans,$currsubquest,$whichline,
 5819:                         \@alphabet,\%record,$scantron_config,$scan_data);
 5820:                 } else {
 5821:                     $ansnum = &scantron_validator_positional($ansnum,
 5822:                         $questnum,$quest_id,$subans,$currsubquest,$whichline,                        \@alphabet,\%record,$scantron_config,$scan_data);
 5823:                 }
 5824:                 $subquestnum ++;
 5825:             }
 5826:         } else {
 5827:             if (($$scantron_config{'Qon'} eq 'letter') ||
 5828:                 ($$scantron_config{'Qon'} eq 'number')) {
 5829:                 $ansnum = &scantron_validator_lettnum($ansnum,$questnum,
 5830:                     $quest_id,$answers_needed,$currentquest,$whichline,
 5831:                     \@alphabet,\%record,$scantron_config,$scan_data);
 5832:             } else {
 5833:                 $ansnum = &scantron_validator_positional($ansnum,$questnum,
 5834:                     $quest_id,$answers_needed,$currentquest,$whichline,
 5835:                     \@alphabet,\%record,$scantron_config,$scan_data);
 5836:             }
 5837:         }
 5838:     }
 5839:     $record{'scantron.maxquest'}=$questnum;
 5840:     return \%record;
 5841: }
 5842: 
 5843: sub scantron_validator_lettnum {
 5844:     my ($ansnum,$questnum,$quest_id,$answers_needed,$currquest,$whichline,
 5845:         $alphabet,$record,$scantron_config,$scan_data) = @_;
 5846: 
 5847:     # Qon 'letter' implies for each slot in currquest we have:
 5848:     #    ? or * for doubles, a letter in A-Z for a bubble, and
 5849:     #    about anything else (esp. a value of Qoff) for missing
 5850:     #    bubbles.
 5851:     #
 5852:     # Qon 'number' implies each slot gives a digit that indexes the
 5853:     #    bubbles filled, or Qoff, or a non-number for unbubbled lines,
 5854:     #    and * or ? for double bubbles on a single line.
 5855:     #
 5856: 
 5857:     my $matchon;
 5858:     if ($$scantron_config{'Qon'} eq 'letter') {
 5859:         $matchon = '[A-Z]';
 5860:     } elsif ($$scantron_config{'Qon'} eq 'number') {
 5861:         $matchon = '\d';
 5862:     }
 5863:     my $occurrences = 0;
 5864:     if (($responsetype_per_response{$questnum-1} eq 'essayresponse') ||
 5865:         ($responsetype_per_response{$questnum-1} eq 'formularesponse') ||
 5866:         ($responsetype_per_response{$questnum-1} eq 'stringresponse') ||
 5867:         ($responsetype_per_response{$questnum-1} eq 'imageresponse') ||
 5868:         ($responsetype_per_response{$questnum-1} eq 'reactionresponse') ||
 5869:         ($responsetype_per_response{$questnum-1} eq 'organicresponse')) {
 5870:         my @singlelines = split('',$currquest);
 5871:         foreach my $entry (@singlelines) {
 5872:             $occurrences = &occurence_count($entry,$matchon);
 5873:             if ($occurrences > 1) {
 5874:                 last;
 5875:             }
 5876:         } 
 5877:     } else {
 5878:         $occurrences = &occurence_count($currquest,$matchon); 
 5879:     }
 5880:     if (($currquest =~ /\?/ || $currquest =~ /\*/) || ($occurrences > 1)) {
 5881:         push(@{$record->{'scantron.doubleerror'}},$quest_id);
 5882:         for (my $ans=0; $ans<$answers_needed; $ans++) {
 5883:             my $bubble = substr($currquest,$ans,1);
 5884:             if ($bubble =~ /$matchon/ ) {
 5885:                 if ($$scantron_config{'Qon'} eq 'number') {
 5886:                     if ($bubble == 0) {
 5887:                         $bubble = 10; 
 5888:                     }
 5889:                     $record->{"scantron.$ansnum.answer"} = 
 5890:                         $alphabet->[$bubble-1];
 5891:                 } else {
 5892:                     $record->{"scantron.$ansnum.answer"} = $bubble;
 5893:                 }
 5894:             } else {
 5895:                 $record->{"scantron.$ansnum.answer"}='';
 5896:             }
 5897:             $ansnum++;
 5898:         }
 5899:     } elsif (!defined($currquest)
 5900:             || (&occurence_count($currquest, $$scantron_config{'Qoff'}) == length($currquest))
 5901:             || (&occurence_count($currquest,$matchon) == 0)) {
 5902:         for (my $ans=0; $ans<$answers_needed; $ans++ ) {
 5903:             $record->{"scantron.$ansnum.answer"}='';
 5904:             $ansnum++;
 5905:         }
 5906:         if (!&scan_data($scan_data,"$whichline.no_bubble.$quest_id")) {
 5907:             push(@{$record->{'scantron.missingerror'}},$quest_id);
 5908:         }
 5909:     } else {
 5910:         if ($$scantron_config{'Qon'} eq 'number') {
 5911:             $currquest = &digits_to_letters($currquest);            
 5912:         }
 5913:         for (my $ans=0; $ans<$answers_needed; $ans++) {
 5914:             my $bubble = substr($currquest,$ans,1);
 5915:             $record->{"scantron.$ansnum.answer"} = $bubble;
 5916:             $ansnum++;
 5917:         }
 5918:     }
 5919:     return $ansnum;
 5920: }
 5921: 
 5922: sub scantron_validator_positional {
 5923:     my ($ansnum,$questnum,$quest_id,$answers_needed,$currquest,
 5924:         $whichline,$alphabet,$record,$scantron_config,$scan_data) = @_;
 5925: 
 5926:     # Otherwise there's a positional notation;
 5927:     # each bubble line requires Qlength items, and there are filled in
 5928:     # bubbles for each case where there 'Qon' characters.
 5929:     #
 5930: 
 5931:     my @array=split($$scantron_config{'Qon'},$currquest,-1);
 5932: 
 5933:     # If the split only gives us one element.. the full length of the
 5934:     # answer string, no bubbles are filled in:
 5935: 
 5936:     if ($answers_needed eq '') {
 5937:         return;
 5938:     }
 5939: 
 5940:     if (length($array[0]) eq $$scantron_config{'Qlength'}*$answers_needed) {
 5941:         for (my $ans=0; $ans<$answers_needed; $ans++ ) {
 5942:             $record->{"scantron.$ansnum.answer"}='';
 5943:             $ansnum++;
 5944:         }
 5945:         if (!&scan_data($scan_data,"$whichline.no_bubble.$quest_id")) {
 5946:             push(@{$record->{"scantron.missingerror"}},$quest_id);
 5947:         }
 5948:     } elsif (scalar(@array) == 2) {
 5949:         my $location = length($array[0]);
 5950:         my $line_num = int($location / $$scantron_config{'Qlength'});
 5951:         my $bubble   = $alphabet->[$location % $$scantron_config{'Qlength'}];
 5952:         for (my $ans=0; $ans<$answers_needed; $ans++) {
 5953:             if ($ans eq $line_num) {
 5954:                 $record->{"scantron.$ansnum.answer"} = $bubble;
 5955:             } else {
 5956:                 $record->{"scantron.$ansnum.answer"} = ' ';
 5957:             }
 5958:             $ansnum++;
 5959:          }
 5960:     } else {
 5961:         #  If there's more than one instance of a bubble character
 5962:         #  That's a double bubble; with positional notation we can
 5963:         #  record all the bubbles filled in as well as the
 5964:         #  fact this response consists of multiple bubbles.
 5965:         #
 5966:         if (($responsetype_per_response{$questnum-1} eq 'essayresponse') ||
 5967:             ($responsetype_per_response{$questnum-1} eq 'formularesponse') ||
 5968:             ($responsetype_per_response{$questnum-1} eq 'stringresponse') ||
 5969:             ($responsetype_per_response{$questnum-1} eq 'imageresponse') ||
 5970:             ($responsetype_per_response{$questnum-1} eq 'reactionresponse') ||
 5971:             ($responsetype_per_response{$questnum-1} eq 'organicresponse')) {
 5972:             my $doubleerror = 0;
 5973:             while (($currquest >= $$scantron_config{'Qlength'}) && 
 5974:                    (!$doubleerror)) {
 5975:                my $currline = substr($currquest,0,$$scantron_config{'Qlength'});
 5976:                $currquest = substr($currquest,$$scantron_config{'Qlength'});
 5977:                my @currarray = split($$scantron_config{'Qon'},$currline,-1);
 5978:                if (length(@currarray) > 2) {
 5979:                    $doubleerror = 1;
 5980:                } 
 5981:             }
 5982:             if ($doubleerror) {
 5983:                 push(@{$record->{'scantron.doubleerror'}},$quest_id);
 5984:             }
 5985:         } else {
 5986:             push(@{$record->{'scantron.doubleerror'}},$quest_id);
 5987:         }
 5988:         my $item = $ansnum;
 5989:         for (my $ans=0; $ans<$answers_needed; $ans++) {
 5990:             $record->{"scantron.$item.answer"} = '';
 5991:             $item ++;
 5992:         }
 5993: 
 5994:         my @ans=@array;
 5995:         my $i=0;
 5996:         my $increment = 0;
 5997:         while ($#ans) {
 5998:             $i+=length($ans[0]) + $increment;
 5999:             my $line   = int($i/$$scantron_config{'Qlength'} + $ansnum);
 6000:             my $bubble = $i%$$scantron_config{'Qlength'};
 6001:             $record->{"scantron.$line.answer"}.=$alphabet->[$bubble];
 6002:             shift(@ans);
 6003:             $increment = 1;
 6004:         }
 6005:         $ansnum += $answers_needed;
 6006:     }
 6007:     return $ansnum;
 6008: }
 6009: 
 6010: =pod
 6011: 
 6012: =item scantron_add_delay
 6013: 
 6014:    Adds an error message that occurred during the grading phase to a
 6015:    queue of messages to be shown after grading pass is complete
 6016: 
 6017:  Arguments:
 6018:    $delayqueue  - arrary ref of hash ref of error messages
 6019:    $scanline    - the scanline that caused the error
 6020:    $errormesage - the error message
 6021:    $errorcode   - a numeric code for the error
 6022: 
 6023:  Side Effects:
 6024:    updates the $delayqueue to have a new hash ref of the error
 6025: 
 6026: =cut
 6027: 
 6028: sub scantron_add_delay {
 6029:     my ($delayqueue,$scanline,$errormessage,$errorcode)=@_;
 6030:     push(@$delayqueue,
 6031: 	 {'line' => $scanline, 'emsg' => $errormessage,
 6032: 	  'ecode' => $errorcode }
 6033: 	 );
 6034: }
 6035: 
 6036: =pod
 6037: 
 6038: =item scantron_find_student
 6039: 
 6040:    Finds the username for the current scanline
 6041: 
 6042:   Arguments:
 6043:    $scantron_record - hash result from scantron_parse_scanline
 6044:    $scan_data       - hash of correction information 
 6045:                       (see &scantron_getfile() form more information)
 6046:    $idmap           - hash from &username_to_idmap()
 6047:    $line            - number of current scanline
 6048:  
 6049:   Returns:
 6050:    Either 'username:domain' or undef if unknown
 6051: 
 6052: =cut
 6053: 
 6054: sub scantron_find_student {
 6055:     my ($scantron_record,$scan_data,$idmap,$line)=@_;
 6056:     my $scanID=$$scantron_record{'scantron.ID'};
 6057:     if ($scanID =~ /^\s*$/) {
 6058:  	return &scan_data($scan_data,"$line.user");
 6059:     }
 6060:     foreach my $id (keys(%$idmap)) {
 6061:  	if (lc($id) eq lc($scanID)) {
 6062:  	    return $$idmap{$id};
 6063:  	}
 6064:     }
 6065:     return undef;
 6066: }
 6067: 
 6068: =pod
 6069: 
 6070: =item scantron_filter
 6071: 
 6072:    Filter sub for lonnavmaps, filters out hidden resources if ignore
 6073:    hidden resources was selected
 6074: 
 6075: =cut
 6076: 
 6077: sub scantron_filter {
 6078:     my ($curres)=@_;
 6079: 
 6080:     if (ref($curres) && $curres->is_problem()) {
 6081: 	# if the user has asked to not have either hidden
 6082: 	# or 'randomout' controlled resources to be graded
 6083: 	# don't include them
 6084: 	if ($env{'form.scantron_options_hidden'} eq 'ignore_hidden'
 6085: 	    && $curres->randomout) {
 6086: 	    return 0;
 6087: 	}
 6088: 	return 1;
 6089:     }
 6090:     return 0;
 6091: }
 6092: 
 6093: =pod
 6094: 
 6095: =item scantron_process_corrections
 6096: 
 6097:    Gets correction information out of submitted form data and corrects
 6098:    the scanline
 6099: 
 6100: =cut
 6101: 
 6102: sub scantron_process_corrections {
 6103:     my ($r) = @_;
 6104:     my %scantron_config=&get_scantron_config($env{'form.scantron_format'});
 6105:     my ($scanlines,$scan_data)=&scantron_getfile();
 6106:     my $classlist=&Apache::loncoursedata::get_classlist();
 6107:     my $which=$env{'form.scantron_line'};
 6108:     my $line=&scantron_get_line($scanlines,$scan_data,$which);
 6109:     my ($skip,$err,$errmsg);
 6110:     if ($env{'form.scantron_skip_record'}) {
 6111: 	$skip=1;
 6112:     } elsif ($env{'form.scantron_corrections'} =~ /^(duplicate|incorrect)ID$/) {
 6113: 	my $newstudent=$env{'form.scantron_username'}.':'.
 6114: 	    $env{'form.scantron_domain'};
 6115: 	my $newid=$classlist->{$newstudent}->[&Apache::loncoursedata::CL_ID];
 6116: 	($line,$err,$errmsg)=
 6117: 	    &scantron_fixup_scanline(\%scantron_config,$scan_data,$line,$which,
 6118: 				     'ID',{'newid'=>$newid,
 6119: 				    'username'=>$env{'form.scantron_username'},
 6120: 				    'domain'=>$env{'form.scantron_domain'}});
 6121:     } elsif ($env{'form.scantron_corrections'} =~ /^(duplicate|incorrect)CODE$/) {
 6122: 	my $resolution=$env{'form.scantron_CODE_resolution'};
 6123: 	my $newCODE;
 6124: 	my %args;
 6125: 	if      ($resolution eq 'use_unfound') {
 6126: 	    $newCODE='use_unfound';
 6127: 	} elsif ($resolution eq 'use_found') {
 6128: 	    $newCODE=$env{'form.scantron_CODE_selectedvalue'};
 6129: 	} elsif ($resolution eq 'use_typed') {
 6130: 	    $newCODE=$env{'form.scantron_CODE_newvalue'};
 6131: 	} elsif ($resolution =~ /^use_closest_(\d+)/) {
 6132: 	    $newCODE=$env{"form.scantron_CODE_closest_$1"};
 6133: 	}
 6134: 	if ($env{'form.scantron_corrections'} eq 'duplicateCODE') {
 6135: 	    $args{'CODE_ignore_dup'}=1;
 6136: 	}
 6137: 	$args{'CODE'}=$newCODE;
 6138: 	($line,$err,$errmsg)=
 6139: 	    &scantron_fixup_scanline(\%scantron_config,$scan_data,$line,$which,
 6140: 				     'CODE',\%args);
 6141:     } elsif ($env{'form.scantron_corrections'} =~ /^(missing|double)bubble$/) {
 6142: 	foreach my $question (split(',',$env{'form.scantron_questions'})) {
 6143: 	    ($line,$err,$errmsg)=
 6144: 		&scantron_fixup_scanline(\%scantron_config,$scan_data,$line,
 6145: 					 $which,'answer',
 6146: 					 { 'question'=>$question,
 6147: 		      		   'response'=>$env{"form.scantron_correct_Q_$question"},
 6148:                                    'questionnum'=>$env{"form.scantron_questionnum_Q_$question"}});
 6149: 	    if ($err) { last; }
 6150: 	}
 6151:     }
 6152:     if ($err) {
 6153: 	$r->print("<span class=\"LC_warning\">Unable to accept last correction, an error occurred :$errmsg:</span>");
 6154:     } else {
 6155: 	&scantron_put_line($scanlines,$scan_data,$which,$line,$skip);
 6156: 	&scantron_putfile($scanlines,$scan_data);
 6157:     }
 6158: }
 6159: 
 6160: =pod
 6161: 
 6162: =item reset_skipping_status
 6163: 
 6164:    Forgets the current set of remember skipped scanlines (and thus
 6165:    reverts back to considering all lines in the
 6166:    scantron_skipped_<filename> file)
 6167: 
 6168: =cut
 6169: 
 6170: sub reset_skipping_status {
 6171:     my ($scanlines,$scan_data)=&scantron_getfile();
 6172:     &scan_data($scan_data,'remember_skipping',undef,1);
 6173:     &scantron_putfile(undef,$scan_data);
 6174: }
 6175: 
 6176: =pod
 6177: 
 6178: =item start_skipping
 6179: 
 6180:    Marks a scanline to be skipped. 
 6181: 
 6182: =cut
 6183: 
 6184: sub start_skipping {
 6185:     my ($scan_data,$i)=@_;
 6186:     my %remembered=split(':',&scan_data($scan_data,'remember_skipping'));
 6187:     if ($env{'form.scantron_options_redo'} =~ /^redo_/) {
 6188: 	$remembered{$i}=2;
 6189:     } else {
 6190: 	$remembered{$i}=1;
 6191:     }
 6192:     &scan_data($scan_data,'remember_skipping',join(':',%remembered));
 6193: }
 6194: 
 6195: =pod
 6196: 
 6197: =item should_be_skipped
 6198: 
 6199:    Checks whether a scanline should be skipped.
 6200: 
 6201: =cut
 6202: 
 6203: sub should_be_skipped {
 6204:     my ($scanlines,$scan_data,$i)=@_;
 6205:     if ($env{'form.scantron_options_redo'} !~ /^redo_/) {
 6206: 	# not redoing old skips
 6207: 	if ($scanlines->{'skipped'}[$i]) { return 1; }
 6208: 	return 0;
 6209:     }
 6210:     my %remembered=split(':',&scan_data($scan_data,'remember_skipping'));
 6211: 
 6212:     if (exists($remembered{$i}) && $remembered{$i} != 2 ) {
 6213: 	return 0;
 6214:     }
 6215:     return 1;
 6216: }
 6217: 
 6218: =pod
 6219: 
 6220: =item remember_current_skipped
 6221: 
 6222:    Discovers what scanlines are in the scantron_skipped_<filename>
 6223:    file and remembers them into scan_data for later use.
 6224: 
 6225: =cut
 6226: 
 6227: sub remember_current_skipped {
 6228:     my ($scanlines,$scan_data)=&scantron_getfile();
 6229:     my %to_remember;
 6230:     for (my $i=0;$i<=$scanlines->{'count'};$i++) {
 6231: 	if ($scanlines->{'skipped'}[$i]) {
 6232: 	    $to_remember{$i}=1;
 6233: 	}
 6234:     }
 6235: 
 6236:     &scan_data($scan_data,'remember_skipping',join(':',%to_remember));
 6237:     &scantron_putfile(undef,$scan_data);
 6238: }
 6239: 
 6240: =pod
 6241: 
 6242: =item check_for_error
 6243: 
 6244:     Checks if there was an error when attempting to remove a specific
 6245:     scantron_.. bubble sheet data file. Prints out an error if
 6246:     something went wrong.
 6247: 
 6248: =cut
 6249: 
 6250: sub check_for_error {
 6251:     my ($r,$result)=@_;
 6252:     if ($result ne 'ok' && $result ne 'not_found' ) {
 6253: 	$r->print(&mt("An error occurred ([_1]) when trying to remove the existing corrections.",$result));
 6254:     }
 6255: }
 6256: 
 6257: =pod
 6258: 
 6259: =item scantron_warning_screen
 6260: 
 6261:    Interstitial screen to make sure the operator has selected the
 6262:    correct options before we start the validation phase.
 6263: 
 6264: =cut
 6265: 
 6266: sub scantron_warning_screen {
 6267:     my ($button_text)=@_;
 6268:     my $title=&Apache::lonnet::gettitle($env{'form.selectpage'});
 6269:     my %scantron_config=&get_scantron_config($env{'form.scantron_format'});
 6270:     my $CODElist;
 6271:     if ($scantron_config{'CODElocation'} &&
 6272: 	$scantron_config{'CODEstart'} &&
 6273: 	$scantron_config{'CODElength'}) {
 6274: 	$CODElist=$env{'form.scantron_CODElist'};
 6275: 	if ($env{'form.scantron_CODElist'} eq '') { $CODElist='<span class="LC_warning">None</span>'; }
 6276: 	$CODElist=
 6277: 	    '<tr><td><b>'.&mt('List of CODES to validate against:').'</b></td><td><tt>'.
 6278: 	    $env{'form.scantron_CODElist'}.'</tt></td></tr>';
 6279:     }
 6280:     return ('
 6281: <p>
 6282: <span class="LC_warning">
 6283: '.&mt('Please double check the information below before clicking on \'[_1]\'',&mt($button_text)).'</span>
 6284: </p>
 6285: <table>
 6286: <tr><td><b>'.&mt('Sequence to be Graded:').'</b></td><td>'.$title.'</td></tr>
 6287: <tr><td><b>'.&mt('Data File that will be used:').'</b></td><td><tt>'.$env{'form.scantron_selectfile'}.'</tt></td></tr>
 6288: '.$CODElist.'
 6289: </table>
 6290: <br />
 6291: <p> '.&mt('If this information is correct, please click on \'[_1]\'.',&mt($button_text)).'</p>
 6292: <p> '.&mt('If something is incorrect, please click the \'Grading Menu\' button to start over.').'</p>
 6293: 
 6294: <br />
 6295: ');
 6296: }
 6297: 
 6298: =pod
 6299: 
 6300: =item scantron_do_warning
 6301: 
 6302:    Check if the operator has picked something for all required
 6303:    fields. Error out if something is missing.
 6304: 
 6305: =cut
 6306: 
 6307: sub scantron_do_warning {
 6308:     my ($r)=@_;
 6309:     my ($symb)=&get_symb($r);
 6310:     if (!$symb) {return '';}
 6311:     my $default_form_data=&defaultFormData($symb);
 6312:     $r->print(&scantron_form_start().$default_form_data);
 6313:     if ( $env{'form.selectpage'} eq '' ||
 6314: 	 $env{'form.scantron_selectfile'} eq '' ||
 6315: 	 $env{'form.scantron_format'} eq '' ) {
 6316: 	$r->print("<p>".&mt('You have forgetten to specify some information. Please go Back and try again.')."</p>");
 6317: 	if ( $env{'form.selectpage'} eq '') {
 6318: 	    $r->print('<p><span class="LC_error">'.&mt('You have not selected a Sequence to grade').'</span></p>');
 6319: 	} 
 6320: 	if ( $env{'form.scantron_selectfile'} eq '') {
 6321: 	    $r->print('<p><span class="LC_error">'.&mt('You have not selected a file that contains the student\'s response data.').'</span></p>');
 6322: 	} 
 6323: 	if ( $env{'form.scantron_format'} eq '') {
 6324: 	    $r->print('<p><span class="LC_error">'.&mt('You have not selected a the format of the student\'s response data.').'</span></p>');
 6325: 	} 
 6326:     } else {
 6327: 	my $warning=&scantron_warning_screen('Grading: Validate Records');
 6328: 	$r->print('
 6329: '.$warning.'
 6330: <input type="submit" name="submit" value="'.&mt('Grading: Validate Records').'" />
 6331: <input type="hidden" name="command" value="scantron_validate" />
 6332: ');
 6333:     }
 6334:     $r->print("</form><br />".&show_grading_menu_form($symb));
 6335:     return '';
 6336: }
 6337: 
 6338: =pod
 6339: 
 6340: =item scantron_form_start
 6341: 
 6342:     html hidden input for remembering all selected grading options
 6343: 
 6344: =cut
 6345: 
 6346: sub scantron_form_start {
 6347:     my ($max_bubble)=@_;
 6348:     my $result= <<SCANTRONFORM;
 6349: <form method="post" enctype="multipart/form-data" action="/adm/grades" name="scantronupload">
 6350:   <input type="hidden" name="selectpage" value="$env{'form.selectpage'}" />
 6351:   <input type="hidden" name="scantron_format" value="$env{'form.scantron_format'}" />
 6352:   <input type="hidden" name="scantron_selectfile" value="$env{'form.scantron_selectfile'}" />
 6353:   <input type="hidden" name="scantron_maxbubble" value="$max_bubble" />
 6354:   <input type="hidden" name="scantron_CODElist" value="$env{'form.scantron_CODElist'}" />
 6355:   <input type="hidden" name="scantron_CODEunique" value="$env{'form.scantron_CODEunique'}" />
 6356:   <input type="hidden" name="scantron_options_redo" value="$env{'form.scantron_options_redo'}" />
 6357:   <input type="hidden" name="scantron_options_ignore" value="$env{'form.scantron_options_ignore'}" />
 6358:   <input type="hidden" name="scantron_options_hidden" value="$env{'form.scantron_options_hidden'}" />
 6359: SCANTRONFORM
 6360: 
 6361:   my $line = 0;
 6362:     while (defined($env{"form.scantron.bubblelines.$line"})) {
 6363:        my $chunk =
 6364: 	   '<input type="hidden" name="scantron.bubblelines.'.$line.'" value="'.$env{"form.scantron.bubblelines.$line"}.'" />'."\n";
 6365:        $chunk .=
 6366: 	   '<input type="hidden" name="scantron.first_bubble_line.'.$line.'" value="'.$env{"form.scantron.first_bubble_line.$line"}.'" />'."\n";
 6367:        $chunk .= 
 6368:            '<input type="hidden" name="scantron.sub_bubblelines.'.$line.'" value="'.$env{"form.scantron.sub_bubblelines.$line"}.'" />'."\n";
 6369:        $chunk .=
 6370:            '<input type="hidden" name="scantron.responsetype.'.$line.'" value="'.$env{"form.scantron.responsetype.$line"}.'" />'."\n";
 6371:        $result .= $chunk;
 6372:        $line++;
 6373:    }
 6374:     return $result;
 6375: }
 6376: 
 6377: =pod
 6378: 
 6379: =item scantron_validate_file
 6380: 
 6381:     Dispatch routine for doing validation of a bubble sheet data file.
 6382: 
 6383:     Also processes any necessary information resets that need to
 6384:     occur before validation begins (ignore previous corrections,
 6385:     restarting the skipped records processing)
 6386: 
 6387: =cut
 6388: 
 6389: sub scantron_validate_file {
 6390:     my ($r) = @_;
 6391:     my ($symb)=&get_symb($r);
 6392:     if (!$symb) {return '';}
 6393:     my $default_form_data=&defaultFormData($symb);
 6394:     
 6395:     # do the detection of only doing skipped records first befroe we delete
 6396:     # them when doing the corrections reset
 6397:     if ($env{'form.scantron_options_redo'} ne 'redo_skipped_ready') {
 6398: 	&reset_skipping_status();
 6399:     }
 6400:     if ($env{'form.scantron_options_redo'} eq 'redo_skipped') {
 6401: 	&remember_current_skipped();
 6402: 	$env{'form.scantron_options_redo'}='redo_skipped_ready';
 6403:     }
 6404: 
 6405:     if ($env{'form.scantron_options_ignore'} eq 'ignore_corrections') {
 6406: 	&check_for_error($r,&scantron_remove_file('corrected'));
 6407: 	&check_for_error($r,&scantron_remove_file('skipped'));
 6408: 	&check_for_error($r,&scantron_remove_scan_data());
 6409: 	$env{'form.scantron_options_ignore'}='done';
 6410:     }
 6411: 
 6412:     if ($env{'form.scantron_corrections'}) {
 6413: 	&scantron_process_corrections($r);
 6414:     }
 6415:     $r->print('<p>'.&mt('Gathering necessary information.').'</p>');$r->rflush();
 6416:     #get the student pick code ready
 6417:     $r->print(&Apache::loncommon::studentbrowser_javascript());
 6418:     my $nav_error;
 6419:     my $max_bubble=&scantron_get_maxbubble(\$nav_error);
 6420:     if ($nav_error) {
 6421:         $r->print(&navmap_errormsg());
 6422:         return '';
 6423:     }
 6424:     my $result=&scantron_form_start($max_bubble).$default_form_data;
 6425:     $r->print($result);
 6426:     
 6427:     my @validate_phases=( 'sequence',
 6428: 			  'ID',
 6429: 			  'CODE',
 6430: 			  'doublebubble',
 6431: 			  'missingbubbles');
 6432:     if (!$env{'form.validatepass'}) {
 6433: 	$env{'form.validatepass'} = 0;
 6434:     }
 6435:     my $currentphase=$env{'form.validatepass'};
 6436: 
 6437: 
 6438:     my $stop=0;
 6439:     while (!$stop && $currentphase < scalar(@validate_phases)) {
 6440: 	$r->print(&mt('Validating '.$validate_phases[$currentphase]).'<br />');
 6441: 	$r->rflush();
 6442: 	my $which="scantron_validate_".$validate_phases[$currentphase];
 6443: 	{
 6444: 	    no strict 'refs';
 6445: 	    ($stop,$currentphase)=&$which($r,$currentphase);
 6446: 	}
 6447:     }
 6448:     if (!$stop) {
 6449: 	my $warning=&scantron_warning_screen('Start Grading');
 6450: 	$r->print(&mt('Validation process complete.').'<br />'.
 6451:                   $warning.
 6452:                   &mt('Perform verification for each student after storage of submissions?').
 6453:                   '&nbsp;<span class="LC_nobreak"><label>'.
 6454:                   '<input type="radio" name="verifyrecord" value="1" />'.&mt('Yes').'</label>'.
 6455:                   ('&nbsp;'x3).'<label>'.
 6456:                   '<input type="radio" name="verifyrecord" value="0" checked="checked" />'.&mt('No').
 6457:                   '</label></span><br />'.
 6458:                   &mt('Grading will take longer if you use verification.').'<br />'.
 6459:                   &mt("Alternatively, the 'Review bubblesheet data' utility (see grading menu) can be used for all students after grading is complete.").'<br /><br />'.
 6460:                   '<input type="submit" name="submit" value="'.&mt('Start Grading').'" />'.
 6461:                   '<input type="hidden" name="command" value="scantron_process" />'."\n");
 6462:     } else {
 6463: 	$r->print('<input type="hidden" name="command" value="scantron_validate" />');
 6464: 	$r->print("<input type='hidden' name='validatepass' value='".$currentphase."' />");
 6465:     }
 6466:     if ($stop) {
 6467: 	if ($validate_phases[$currentphase] eq 'sequence') {
 6468: 	    $r->print('<input type="submit" name="submit" value="'.&mt('Ignore').' &rarr; " />');
 6469: 	    $r->print(' '.&mt('this error').' <br />');
 6470: 
 6471: 	    $r->print(" <p>".&mt("Or click the 'Grading Menu' button to start over.")."</p>");
 6472: 	} else {
 6473:             if ($validate_phases[$currentphase] eq 'doublebubble' || $validate_phases[$currentphase] eq 'missingbubbles') {
 6474: 	        $r->print('<input type="button" name="submitbutton" value="'.&mt('Continue').' &rarr;" onclick="javascript:verify_bubble_radio(this.form)" />');
 6475:             } else {
 6476:                 $r->print('<input type="submit" name="submit" value="'.&mt('Continue').' &rarr;" />');
 6477:             }
 6478: 	    $r->print(' '.&mt('using corrected info').' <br />');
 6479: 	    $r->print("<input type='submit' value='".&mt("Skip")."' name='scantron_skip_record' />");
 6480: 	    $r->print(" ".&mt("this scanline saving it for later."));
 6481: 	}
 6482:     }
 6483:     $r->print(" </form><br />".&show_grading_menu_form($symb));
 6484:     return '';
 6485: }
 6486: 
 6487: 
 6488: =pod
 6489: 
 6490: =item scantron_remove_file
 6491: 
 6492:    Removes the requested bubble sheet data file, makes sure that
 6493:    scantron_original_<filename> is never removed
 6494: 
 6495: 
 6496: =cut
 6497: 
 6498: sub scantron_remove_file {
 6499:     my ($which)=@_;
 6500:     my $cname=$env{'course.'.$env{'request.course.id'}.'.num'};
 6501:     my $cdom=$env{'course.'.$env{'request.course.id'}.'.domain'};
 6502:     my $file='scantron_';
 6503:     if ($which eq 'corrected' || $which eq 'skipped') {
 6504: 	$file.=$which.'_';
 6505:     } else {
 6506: 	return 'refused';
 6507:     }
 6508:     $file.=$env{'form.scantron_selectfile'};
 6509:     return &Apache::lonnet::removeuserfile($cname,$cdom,$file);
 6510: }
 6511: 
 6512: 
 6513: =pod
 6514: 
 6515: =item scantron_remove_scan_data
 6516: 
 6517:    Removes all scan_data correction for the requested bubble sheet
 6518:    data file.  (In the case that both the are doing skipped records we need
 6519:    to remember the old skipped lines for the time being so that element
 6520:    persists for a while.)
 6521: 
 6522: =cut
 6523: 
 6524: sub scantron_remove_scan_data {
 6525:     my $cname=$env{'course.'.$env{'request.course.id'}.'.num'};
 6526:     my $cdom=$env{'course.'.$env{'request.course.id'}.'.domain'};
 6527:     my @keys=&Apache::lonnet::getkeys('nohist_scantrondata',$cdom,$cname);
 6528:     my @todelete;
 6529:     my $filename=$env{'form.scantron_selectfile'};
 6530:     foreach my $key (@keys) {
 6531: 	if ($key=~/^\Q$filename\E_/) {
 6532: 	    if ($env{'form.scantron_options_redo'} eq 'redo_skipped_ready' &&
 6533: 		$key=~/remember_skipping/) {
 6534: 		next;
 6535: 	    }
 6536: 	    push(@todelete,$key);
 6537: 	}
 6538:     }
 6539:     my $result;
 6540:     if (@todelete) {
 6541: 	$result = &Apache::lonnet::del('nohist_scantrondata',
 6542: 				       \@todelete,$cdom,$cname);
 6543:     } else {
 6544: 	$result = 'ok';
 6545:     }
 6546:     return $result;
 6547: }
 6548: 
 6549: 
 6550: =pod
 6551: 
 6552: =item scantron_getfile
 6553: 
 6554:     Fetches the requested bubble sheet data file (all 3 versions), and
 6555:     the scan_data hash
 6556:   
 6557:   Arguments:
 6558:     None
 6559: 
 6560:   Returns:
 6561:     2 hash references
 6562: 
 6563:      - first one has 
 6564:          orig      -
 6565:          corrected -
 6566:          skipped   -  each of which points to an array ref of the specified
 6567:                       file broken up into individual lines
 6568:          count     - number of scanlines
 6569:  
 6570:      - second is the scan_data hash possible keys are
 6571:        ($number refers to scanline numbered $number and thus the key affects
 6572:         only that scanline
 6573:         $bubline refers to the specific bubble line element and the aspects
 6574:         refers to that specific bubble line element)
 6575: 
 6576:        $number.user - username:domain to use
 6577:        $number.CODE_ignore_dup 
 6578:                     - ignore the duplicate CODE error 
 6579:        $number.useCODE
 6580:                     - use the CODE in the scanline as is
 6581:        $number.no_bubble.$bubline
 6582:                     - it is valid that there is no bubbled in bubble
 6583:                       at $number $bubline
 6584:        remember_skipping
 6585:                     - a frozen hash containing keys of $number and values
 6586:                       of either 
 6587:                         1 - we are on a 'do skipped records pass' and plan
 6588:                             on processing this line
 6589:                         2 - we are on a 'do skipped records pass' and this
 6590:                             scanline has been marked to skip yet again
 6591: 
 6592: =cut
 6593: 
 6594: sub scantron_getfile {
 6595:     #FIXME really would prefer a scantron directory
 6596:     my $cname=$env{'course.'.$env{'request.course.id'}.'.num'};
 6597:     my $cdom=$env{'course.'.$env{'request.course.id'}.'.domain'};
 6598:     my $lines;
 6599:     $lines=&Apache::lonnet::getfile('/uploaded/'.$cdom.'/'.$cname.'/'.
 6600: 		       'scantron_orig_'.$env{'form.scantron_selectfile'});
 6601:     my %scanlines;
 6602:     $scanlines{'orig'}=[(split("\n",$lines,-1))];
 6603:     my $temp=$scanlines{'orig'};
 6604:     $scanlines{'count'}=$#$temp;
 6605: 
 6606:     $lines=&Apache::lonnet::getfile('/uploaded/'.$cdom.'/'.$cname.'/'.
 6607: 		       'scantron_corrected_'.$env{'form.scantron_selectfile'});
 6608:     if ($lines eq '-1') {
 6609: 	$scanlines{'corrected'}=[];
 6610:     } else {
 6611: 	$scanlines{'corrected'}=[(split("\n",$lines,-1))];
 6612:     }
 6613:     $lines=&Apache::lonnet::getfile('/uploaded/'.$cdom.'/'.$cname.'/'.
 6614: 		       'scantron_skipped_'.$env{'form.scantron_selectfile'});
 6615:     if ($lines eq '-1') {
 6616: 	$scanlines{'skipped'}=[];
 6617:     } else {
 6618: 	$scanlines{'skipped'}=[(split("\n",$lines,-1))];
 6619:     }
 6620:     my @tmp=&Apache::lonnet::dump('nohist_scantrondata',$cdom,$cname);
 6621:     if ($tmp[0] =~ /^(error:|no_such_host)/) { @tmp=(); }
 6622:     my %scan_data = @tmp;
 6623:     return (\%scanlines,\%scan_data);
 6624: }
 6625: 
 6626: =pod
 6627: 
 6628: =item lonnet_putfile
 6629: 
 6630:    Wrapper routine to call &Apache::lonnet::finishuserfileupload
 6631: 
 6632:  Arguments:
 6633:    $contents - data to store
 6634:    $filename - filename to store $contents into
 6635: 
 6636:  Returns:
 6637:    result value from &Apache::lonnet::finishuserfileupload
 6638: 
 6639: =cut
 6640: 
 6641: sub lonnet_putfile {
 6642:     my ($contents,$filename)=@_;
 6643:     my $docuname=$env{'course.'.$env{'request.course.id'}.'.num'};
 6644:     my $docudom=$env{'course.'.$env{'request.course.id'}.'.domain'};
 6645:     $env{'form.sillywaytopassafilearound'}=$contents;
 6646:     &Apache::lonnet::finishuserfileupload($docuname,$docudom,'sillywaytopassafilearound',$filename);
 6647: 
 6648: }
 6649: 
 6650: =pod
 6651: 
 6652: =item scantron_putfile
 6653: 
 6654:     Stores the current version of the bubble sheet data files, and the
 6655:     scan_data hash. (Does not modify the original version only the
 6656:     corrected and skipped versions.
 6657: 
 6658:  Arguments:
 6659:     $scanlines - hash ref that looks like the first return value from
 6660:                  &scantron_getfile()
 6661:     $scan_data - hash ref that looks like the second return value from
 6662:                  &scantron_getfile()
 6663: 
 6664: =cut
 6665: 
 6666: sub scantron_putfile {
 6667:     my ($scanlines,$scan_data) = @_;
 6668:     #FIXME really would prefer a scantron directory
 6669:     my $cname=$env{'course.'.$env{'request.course.id'}.'.num'};
 6670:     my $cdom=$env{'course.'.$env{'request.course.id'}.'.domain'};
 6671:     if ($scanlines) {
 6672: 	my $prefix='scantron_';
 6673: # no need to update orig, shouldn't change
 6674: #   &lonnet_putfile(join("\n",@{$scanlines->{'orig'}}),$prefix.'orig_'.
 6675: #		    $env{'form.scantron_selectfile'});
 6676: 	&lonnet_putfile(join("\n",@{$scanlines->{'corrected'}}),
 6677: 			$prefix.'corrected_'.
 6678: 			$env{'form.scantron_selectfile'});
 6679: 	&lonnet_putfile(join("\n",@{$scanlines->{'skipped'}}),
 6680: 			$prefix.'skipped_'.
 6681: 			$env{'form.scantron_selectfile'});
 6682:     }
 6683:     &Apache::lonnet::put('nohist_scantrondata',$scan_data,$cdom,$cname);
 6684: }
 6685: 
 6686: =pod
 6687: 
 6688: =item scantron_get_line
 6689: 
 6690:    Returns the correct version of the scanline
 6691: 
 6692:  Arguments:
 6693:     $scanlines - hash ref that looks like the first return value from
 6694:                  &scantron_getfile()
 6695:     $scan_data - hash ref that looks like the second return value from
 6696:                  &scantron_getfile()
 6697:     $i         - number of the requested line (starts at 0)
 6698: 
 6699:  Returns:
 6700:    A scanline, (either the original or the corrected one if it
 6701:    exists), or undef if the requested scanline should be
 6702:    skipped. (Either because it's an skipped scanline, or it's an
 6703:    unskipped scanline and we are not doing a 'do skipped scanlines'
 6704:    pass.
 6705: 
 6706: =cut
 6707: 
 6708: sub scantron_get_line {
 6709:     my ($scanlines,$scan_data,$i)=@_;
 6710:     if (&should_be_skipped($scanlines,$scan_data,$i)) { return undef; }
 6711:     #if ($scanlines->{'skipped'}[$i]) { return undef; }
 6712:     if ($scanlines->{'corrected'}[$i]) {return $scanlines->{'corrected'}[$i];}
 6713:     return $scanlines->{'orig'}[$i]; 
 6714: }
 6715: 
 6716: =pod
 6717: 
 6718: =item scantron_todo_count
 6719: 
 6720:     Counts the number of scanlines that need processing.
 6721: 
 6722:  Arguments:
 6723:     $scanlines - hash ref that looks like the first return value from
 6724:                  &scantron_getfile()
 6725:     $scan_data - hash ref that looks like the second return value from
 6726:                  &scantron_getfile()
 6727: 
 6728:  Returns:
 6729:     $count - number of scanlines to process
 6730: 
 6731: =cut
 6732: 
 6733: sub get_todo_count {
 6734:     my ($scanlines,$scan_data)=@_;
 6735:     my $count=0;
 6736:     for (my $i=0;$i<=$scanlines->{'count'};$i++) {
 6737: 	my $line=&scantron_get_line($scanlines,$scan_data,$i);
 6738: 	if ($line=~/^[\s\cz]*$/) { next; }
 6739: 	$count++;
 6740:     }
 6741:     return $count;
 6742: }
 6743: 
 6744: =pod
 6745: 
 6746: =item scantron_put_line
 6747: 
 6748:     Updates the 'corrected' or 'skipped' versions of the bubble sheet
 6749:     data file.
 6750: 
 6751:  Arguments:
 6752:     $scanlines - hash ref that looks like the first return value from
 6753:                  &scantron_getfile()
 6754:     $scan_data - hash ref that looks like the second return value from
 6755:                  &scantron_getfile()
 6756:     $i         - line number to update
 6757:     $newline   - contents of the updated scanline
 6758:     $skip      - if true make the line for skipping and update the
 6759:                  'skipped' file
 6760: 
 6761: =cut
 6762: 
 6763: sub scantron_put_line {
 6764:     my ($scanlines,$scan_data,$i,$newline,$skip)=@_;
 6765:     if ($skip) {
 6766: 	$scanlines->{'skipped'}[$i]=$newline;
 6767: 	&start_skipping($scan_data,$i);
 6768: 	return;
 6769:     }
 6770:     $scanlines->{'corrected'}[$i]=$newline;
 6771: }
 6772: 
 6773: =pod
 6774: 
 6775: =item scantron_clear_skip
 6776: 
 6777:    Remove a line from the 'skipped' file
 6778: 
 6779:  Arguments:
 6780:     $scanlines - hash ref that looks like the first return value from
 6781:                  &scantron_getfile()
 6782:     $scan_data - hash ref that looks like the second return value from
 6783:                  &scantron_getfile()
 6784:     $i         - line number to update
 6785: 
 6786: =cut
 6787: 
 6788: sub scantron_clear_skip {
 6789:     my ($scanlines,$scan_data,$i)=@_;
 6790:     if (exists($scanlines->{'skipped'}[$i])) {
 6791: 	undef($scanlines->{'skipped'}[$i]);
 6792: 	return 1;
 6793:     }
 6794:     return 0;
 6795: }
 6796: 
 6797: =pod
 6798: 
 6799: =item scantron_filter_not_exam
 6800: 
 6801:    Filter routine used by &Apache::lonnavmaps::retrieveResources(), to
 6802:    filter out resources that are not marked as 'exam' mode
 6803: 
 6804: =cut
 6805: 
 6806: sub scantron_filter_not_exam {
 6807:     my ($curres)=@_;
 6808:     
 6809:     if (ref($curres) && $curres->is_problem() && !$curres->is_exam()) {
 6810: 	# if the user has asked to not have either hidden
 6811: 	# or 'randomout' controlled resources to be graded
 6812: 	# don't include them
 6813: 	if ($env{'form.scantron_options_hidden'} eq 'ignore_hidden'
 6814: 	    && $curres->randomout) {
 6815: 	    return 0;
 6816: 	}
 6817: 	return 1;
 6818:     }
 6819:     return 0;
 6820: }
 6821: 
 6822: =pod
 6823: 
 6824: =item scantron_validate_sequence
 6825: 
 6826:     Validates the selected sequence, checking for resource that are
 6827:     not set to exam mode.
 6828: 
 6829: =cut
 6830: 
 6831: sub scantron_validate_sequence {
 6832:     my ($r,$currentphase) = @_;
 6833: 
 6834:     my $navmap=Apache::lonnavmaps::navmap->new();
 6835:     unless (ref($navmap)) {
 6836:         $r->print(&navmap_errormsg());
 6837:         return (1,$currentphase);
 6838:     }
 6839:     my (undef,undef,$sequence)=
 6840: 	&Apache::lonnet::decode_symb($env{'form.selectpage'});
 6841: 
 6842:     my $map=$navmap->getResourceByUrl($sequence);
 6843: 
 6844:     $r->print('<input type="hidden" name="validate_sequence_exam"
 6845:                                     value="ignore" />');
 6846:     if ($env{'form.validate_sequence_exam'} ne 'ignore') {
 6847: 	my @resources=
 6848: 	    $navmap->retrieveResources($map,\&scantron_filter_not_exam,1,0);
 6849: 	if (@resources) {
 6850: 	    $r->print("<p>".&mt('Some resources in the sequence currently are not set to exam mode. Grading these resources currently may not work correctly.')."</p>");
 6851: 	    return (1,$currentphase);
 6852: 	}
 6853:     }
 6854: 
 6855:     return (0,$currentphase+1);
 6856: }
 6857: 
 6858: 
 6859: 
 6860: sub scantron_validate_ID {
 6861:     my ($r,$currentphase) = @_;
 6862:     
 6863:     #get student info
 6864:     my $classlist=&Apache::loncoursedata::get_classlist();
 6865:     my %idmap=&username_to_idmap($classlist);
 6866: 
 6867:     #get scantron line setup
 6868:     my %scantron_config=&get_scantron_config($env{'form.scantron_format'});
 6869:     my ($scanlines,$scan_data)=&scantron_getfile();
 6870: 
 6871:     my $nav_error;
 6872:     &scantron_get_maxbubble(\$nav_error); # parse needs the bubble_lines.. array.
 6873:     if ($nav_error) {
 6874:         $r->print(&navmap_errormsg());
 6875:         return(1,$currentphase);
 6876:     }
 6877: 
 6878:     my %found=('ids'=>{},'usernames'=>{});
 6879:     for (my $i=0;$i<=$scanlines->{'count'};$i++) {
 6880: 	my $line=&scantron_get_line($scanlines,$scan_data,$i);
 6881: 	if ($line=~/^[\s\cz]*$/) { next; }
 6882: 	my $scan_record=&scantron_parse_scanline($line,$i,\%scantron_config,
 6883: 						 $scan_data);
 6884: 	my $id=$$scan_record{'scantron.ID'};
 6885: 	my $found;
 6886: 	foreach my $checkid (keys(%idmap)) {
 6887: 	    if (lc($checkid) eq lc($id)) { $found=$checkid;last; }
 6888: 	}
 6889: 	if ($found) {
 6890: 	    my $username=$idmap{$found};
 6891: 	    if ($found{'ids'}{$found}) {
 6892: 		&scantron_get_correction($r,$i,$scan_record,\%scantron_config,
 6893: 					 $line,'duplicateID',$found);
 6894: 		return(1,$currentphase);
 6895: 	    } elsif ($found{'usernames'}{$username}) {
 6896: 		&scantron_get_correction($r,$i,$scan_record,\%scantron_config,
 6897: 					 $line,'duplicateID',$username);
 6898: 		return(1,$currentphase);
 6899: 	    }
 6900: 	    #FIXME store away line we previously saw the ID on to use above
 6901: 	    $found{'ids'}{$found}++;
 6902: 	    $found{'usernames'}{$username}++;
 6903: 	} else {
 6904: 	    if ($id =~ /^\s*$/) {
 6905: 		my $username=&scan_data($scan_data,"$i.user");
 6906: 		if (defined($username) && $found{'usernames'}{$username}) {
 6907: 		    &scantron_get_correction($r,$i,$scan_record,
 6908: 					     \%scantron_config,
 6909: 					     $line,'duplicateID',$username);
 6910: 		    return(1,$currentphase);
 6911: 		} elsif (!defined($username)) {
 6912: 		    &scantron_get_correction($r,$i,$scan_record,
 6913: 					     \%scantron_config,
 6914: 					     $line,'incorrectID');
 6915: 		    return(1,$currentphase);
 6916: 		}
 6917: 		$found{'usernames'}{$username}++;
 6918: 	    } else {
 6919: 		&scantron_get_correction($r,$i,$scan_record,\%scantron_config,
 6920: 					 $line,'incorrectID');
 6921: 		return(1,$currentphase);
 6922: 	    }
 6923: 	}
 6924:     }
 6925: 
 6926:     return (0,$currentphase+1);
 6927: }
 6928: 
 6929: 
 6930: sub scantron_get_correction {
 6931:     my ($r,$i,$scan_record,$scan_config,$line,$error,$arg)=@_;
 6932: #FIXME in the case of a duplicated ID the previous line, probably need
 6933: #to show both the current line and the previous one and allow skipping
 6934: #the previous one or the current one
 6935: 
 6936:     if ( $$scan_record{'scantron.PaperID'} =~ /\S/) {
 6937: 	$r->print("<p>".&mt("<b>An error was detected ($error)</b>".
 6938: 			    " for PaperID <tt>[_1]</tt>",
 6939: 			    $$scan_record{'scantron.PaperID'})."</p> \n");
 6940:     } else {
 6941: 	$r->print("<p>".&mt("<b>An error was detected ($error)</b>".
 6942: 			    " in scanline [_1] <pre>[_2]</pre>",
 6943: 			    $i,$line)."</p> \n");
 6944:     }
 6945:     my $message="<p>".&mt("The ID on the form is  <tt>[_1]</tt><br />".
 6946: 			  "The name on the paper is [_2],[_3]",
 6947: 			  $$scan_record{'scantron.ID'},
 6948: 			  $$scan_record{'scantron.LastName'},
 6949: 			  $$scan_record{'scantron.FirstName'})."</p>";
 6950: 
 6951:     $r->print('<input type="hidden" name="scantron_corrections" value="'.$error.'" />'."\n");
 6952:     $r->print('<input type="hidden" name="scantron_line" value="'.$i.'" />'."\n");
 6953:                            # Array populated for doublebubble or
 6954:     my @lines_to_correct;  # missingbubble errors to build javascript
 6955:                            # to validate radio button checking   
 6956: 
 6957:     if ($error =~ /ID$/) {
 6958: 	if ($error eq 'incorrectID') {
 6959: 	    $r->print("<p>".&mt("The encoded ID is not in the classlist").
 6960: 		      "</p>\n");
 6961: 	} elsif ($error eq 'duplicateID') {
 6962: 	    $r->print("<p>".&mt("The encoded ID has also been used by a previous paper [_1]",$arg)."</p>\n");
 6963: 	}
 6964: 	$r->print($message);
 6965: 	$r->print("<p>".&mt("How should I handle this?")." <br /> \n");
 6966: 	$r->print("\n<ul><li> ");
 6967: 	#FIXME it would be nice if this sent back the user ID and
 6968: 	#could do partial userID matches
 6969: 	$r->print(&Apache::loncommon::selectstudent_link('scantronupload',
 6970: 				       'scantron_username','scantron_domain'));
 6971: 	$r->print(": <input type='text' name='scantron_username' value='' />");
 6972: 	$r->print("\n@".
 6973: 		 &Apache::loncommon::select_dom_form($env{'request.role.domain'},'scantron_domain'));
 6974: 
 6975: 	$r->print('</li>');
 6976:     } elsif ($error =~ /CODE$/) {
 6977: 	if ($error eq 'incorrectCODE') {
 6978: 	    $r->print("<p>".&mt("The encoded CODE is not in the list of possible CODEs.")."</p>\n");
 6979: 	} elsif ($error eq 'duplicateCODE') {
 6980: 	    $r->print("<p>".&mt("The encoded CODE has also been used by a previous paper [_1], and CODEs are supposed to be unique.",join(', ',@{$arg}))."</p>\n");
 6981: 	}
 6982: 	$r->print("<p>".&mt("The CODE on the form is  <tt>'[_1]'</tt>",
 6983: 			    $$scan_record{'scantron.CODE'})."<br />\n");
 6984: 	$r->print($message);
 6985: 	$r->print("<p>".&mt("How should I handle this?")." <br /> \n");
 6986: 	$r->print("\n<br /> ");
 6987: 	my $i=0;
 6988: 	if ($error eq 'incorrectCODE' 
 6989: 	    && $$scan_record{'scantron.CODE'}=~/\S/ ) {
 6990: 	    my ($max,$closest)=&scantron_get_closely_matching_CODEs($arg,$$scan_record{'scantron.CODE'});
 6991: 	    if ($closest > 0) {
 6992: 		foreach my $testcode (@{$closest}) {
 6993: 		    my $checked='';
 6994: 		    if (!$i) { $checked=' checked="checked"'; }
 6995: 		    $r->print("
 6996:    <label>
 6997:        <input type='radio' name='scantron_CODE_resolution' value='use_closest_$i'$checked />
 6998:        ".&mt("Use the similar CODE [_1] instead.",
 6999: 	    "<b><tt>".$testcode."</tt></b>")."
 7000:     </label>
 7001:     <input type='hidden' name='scantron_CODE_closest_$i' value='$testcode' />");
 7002: 		    $r->print("\n<br />");
 7003: 		    $i++;
 7004: 		}
 7005: 	    }
 7006: 	}
 7007: 	if ($$scan_record{'scantron.CODE'}=~/\S/ ) {
 7008: 	    my $checked; if (!$i) { $checked=' checked="checked"'; }
 7009: 	    $r->print("
 7010:     <label>
 7011:         <input type='radio' name='scantron_CODE_resolution' value='use_unfound'$checked />
 7012:        ".&mt("Use the CODE [_1] that is was on the paper, ignoring the error.",
 7013: 	     "<b><tt>".$$scan_record{'scantron.CODE'}."</tt></b>")."
 7014:     </label>");
 7015: 	    $r->print("\n<br />");
 7016: 	}
 7017: 
 7018: 	$r->print(<<ENDSCRIPT);
 7019: <script type="text/javascript">
 7020: function change_radio(field) {
 7021:     var slct=document.scantronupload.scantron_CODE_resolution;
 7022:     var i;
 7023:     for (i=0;i<slct.length;i++) {
 7024:         if (slct[i].value==field) { slct[i].checked=true; }
 7025:     }
 7026: }
 7027: </script>
 7028: ENDSCRIPT
 7029: 	my $href="/adm/pickcode?".
 7030: 	   "form=".&escape("scantronupload").
 7031: 	   "&scantron_format=".&escape($env{'form.scantron_format'}).
 7032: 	   "&scantron_CODElist=".&escape($env{'form.scantron_CODElist'}).
 7033: 	   "&curCODE=".&escape($$scan_record{'scantron.CODE'}).
 7034: 	   "&scantron_selectfile=".&escape($env{'form.scantron_selectfile'});
 7035: 	if ($env{'form.scantron_CODElist'} =~ /\S/) { 
 7036: 	    $r->print("
 7037:     <label>
 7038:        <input type='radio' name='scantron_CODE_resolution' value='use_found' />
 7039:        ".&mt("[_1]Select[_2] a CODE from the list of all CODEs and use it.",
 7040: 	     "<a target='_blank' href='$href'>","</a>")."
 7041:     </label> 
 7042:     ".&mt("Selected CODE is [_1]",'<input readonly="readonly" type="text" size="8" name="scantron_CODE_selectedvalue" onfocus="javascript:change_radio(\'use_found\')" onchange="javascript:change_radio(\'use_found\')" />'));
 7043: 	    $r->print("\n<br />");
 7044: 	}
 7045: 	$r->print("
 7046:     <label>
 7047:        <input type='radio' name='scantron_CODE_resolution' value='use_typed' />
 7048:        ".&mt("Use [_1] as the CODE.",
 7049: 	     "</label><input type='text' size='8' name='scantron_CODE_newvalue' onfocus=\"javascript:change_radio('use_typed')\" onkeypress=\"javascript:change_radio('use_typed')\" />"));
 7050: 	$r->print("\n<br /><br />");
 7051:     } elsif ($error eq 'doublebubble') {
 7052: 	$r->print("<p>".&mt("There have been multiple bubbles scanned for some question(s)")."</p>\n");
 7053: 
 7054: 	# The form field scantron_questions is acutally a list of line numbers.
 7055: 	# represented by this form so:
 7056: 
 7057: 	my $line_list = &questions_to_line_list($arg);
 7058: 
 7059: 	$r->print('<input type="hidden" name="scantron_questions" value="'.
 7060: 		  $line_list.'" />');
 7061: 	$r->print($message);
 7062: 	$r->print("<p>".&mt("Please indicate which bubble should be used for grading")."</p>");
 7063: 	foreach my $question (@{$arg}) {
 7064: 	    my @linenums = &prompt_for_corrections($r,$question,$scan_config,
 7065:                                                    $scan_record, $error);
 7066:             push(@lines_to_correct,@linenums);
 7067: 	}
 7068:         $r->print(&verify_bubbles_checked(@lines_to_correct));
 7069:     } elsif ($error eq 'missingbubble') {
 7070: 	$r->print("<p>".&mt("There have been <b>no</b> bubbles scanned for some question(s)")."</p>\n");
 7071: 	$r->print($message);
 7072: 	$r->print("<p>".&mt("Please indicate which bubble should be used for grading.")."</p>");
 7073: 	$r->print(&mt("Some questions have no scanned bubbles.")."\n");
 7074: 
 7075: 	# The form field scantron_questions is actually a list of line numbers not
 7076: 	# a list of question numbers. Therefore:
 7077: 	#
 7078: 	
 7079: 	my $line_list = &questions_to_line_list($arg);
 7080: 
 7081: 	$r->print('<input type="hidden" name="scantron_questions" value="'.
 7082: 		  $line_list.'" />');
 7083: 	foreach my $question (@{$arg}) {
 7084: 	    my @linenums = &prompt_for_corrections($r,$question,$scan_config,
 7085:                                                    $scan_record, $error);
 7086:             push(@lines_to_correct,@linenums);
 7087: 	}
 7088:         $r->print(&verify_bubbles_checked(@lines_to_correct));
 7089:     } else {
 7090: 	$r->print("\n<ul>");
 7091:     }
 7092:     $r->print("\n</li></ul>");
 7093: }
 7094: 
 7095: sub verify_bubbles_checked {
 7096:     my (@ansnums) = @_;
 7097:     my $ansnumstr = join('","',@ansnums);
 7098:     my $warning = &mt("A bubble or 'No bubble' selection has not been made for one or more lines.");
 7099:     my $output = (<<ENDSCRIPT);
 7100: <script type="text/javascript">
 7101: function verify_bubble_radio(form) {
 7102:     var ansnumArray = new Array ("$ansnumstr");
 7103:     var need_bubble_count = 0;
 7104:     for (var i=0; i<ansnumArray.length; i++) {
 7105:         if (form.elements["scantron_correct_Q_"+ansnumArray[i]].length > 1) {
 7106:             var bubble_picked = 0; 
 7107:             for (var j=0; j<form.elements["scantron_correct_Q_"+ansnumArray[i]].length; j++) {
 7108:                 if (form.elements["scantron_correct_Q_"+ansnumArray[i]][j].checked == true) {
 7109:                     bubble_picked = 1;
 7110:                 }
 7111:             }
 7112:             if (bubble_picked == 0) {
 7113:                 need_bubble_count ++;
 7114:             }
 7115:         }
 7116:     }
 7117:     if (need_bubble_count) {
 7118:         alert("$warning");
 7119:         return;
 7120:     }
 7121:     form.submit(); 
 7122: }
 7123: </script>
 7124: ENDSCRIPT
 7125:     return $output;
 7126: }
 7127: 
 7128: =pod
 7129: 
 7130: =item  questions_to_line_list
 7131: 
 7132: Converts a list of questions into a string of comma separated
 7133: line numbers in the answer sheet used by the questions.  This is
 7134: used to fill in the scantron_questions form field.
 7135: 
 7136:   Arguments:
 7137:      questions    - Reference to an array of questions.
 7138: 
 7139: =cut
 7140: 
 7141: 
 7142: sub questions_to_line_list {
 7143:     my ($questions) = @_;
 7144:     my @lines;
 7145: 
 7146:     foreach my $item (@{$questions}) {
 7147:         my $question = $item;
 7148:         my ($first,$count,$last);
 7149:         if ($item =~ /^(\d+)\.(\d+)$/) {
 7150:             $question = $1;
 7151:             my $subquestion = $2;
 7152:             $first = $first_bubble_line{$question-1} + 1;
 7153:             my @subans = split(/,/,$subdivided_bubble_lines{$question-1});
 7154:             my $subcount = 1;
 7155:             while ($subcount<$subquestion) {
 7156:                 $first += $subans[$subcount-1];
 7157:                 $subcount ++;
 7158:             }
 7159:             $count = $subans[$subquestion-1];
 7160:         } else {
 7161: 	    $first   = $first_bubble_line{$question-1} + 1;
 7162: 	    $count   = $bubble_lines_per_response{$question-1};
 7163:         }
 7164:         $last = $first+$count-1;
 7165:         push(@lines, ($first..$last));
 7166:     }
 7167:     return join(',', @lines);
 7168: }
 7169: 
 7170: =pod 
 7171: 
 7172: =item prompt_for_corrections
 7173: 
 7174: Prompts for a potentially multiline correction to the
 7175: user's bubbling (factors out common code from scantron_get_correction
 7176: for multi and missing bubble cases).
 7177: 
 7178:  Arguments:
 7179:    $r           - Apache request object.
 7180:    $question    - The question number to prompt for.
 7181:    $scan_config - The scantron file configuration hash.
 7182:    $scan_record - Reference to the hash that has the the parsed scanlines.
 7183:    $error       - Type of error
 7184: 
 7185:  Implicit inputs:
 7186:    %bubble_lines_per_response   - Starting line numbers for each question.
 7187:                                   Numbered from 0 (but question numbers are from
 7188:                                   1.
 7189:    %first_bubble_line           - Starting bubble line for each question.
 7190:    %subdivided_bubble_lines     - optionresponse, matchresponse and rankresponse 
 7191:                                   type problems render as separate sub-questions, 
 7192:                                   in exam mode. This hash contains a 
 7193:                                   comma-separated list of the lines per 
 7194:                                   sub-question.
 7195:    %responsetype_per_response   - essayresponse, formularesponse,
 7196:                                   stringresponse, imageresponse, reactionresponse,
 7197:                                   and organicresponse type problem parts can have
 7198:                                   multiple lines per response if the weight
 7199:                                   assigned exceeds 10.  In this case, only
 7200:                                   one bubble per line is permitted, but more 
 7201:                                   than one line might contain bubbles, e.g.
 7202:                                   bubbling of: line 1 - J, line 2 - J, 
 7203:                                   line 3 - B would assign 22 points.  
 7204: 
 7205: =cut
 7206: 
 7207: sub prompt_for_corrections {
 7208:     my ($r, $question, $scan_config, $scan_record, $error) = @_;
 7209:     my ($current_line,$lines);
 7210:     my @linenums;
 7211:     my $questionnum = $question;
 7212:     if ($question =~ /^(\d+)\.(\d+)$/) {
 7213:         $question = $1;
 7214:         $current_line = $first_bubble_line{$question-1} + 1 ;
 7215:         my $subquestion = $2;
 7216:         my @subans = split(/,/,$subdivided_bubble_lines{$question-1});
 7217:         my $subcount = 1;
 7218:         while ($subcount<$subquestion) {
 7219:             $current_line += $subans[$subcount-1];
 7220:             $subcount ++;
 7221:         }
 7222:         $lines = $subans[$subquestion-1];
 7223:     } else {
 7224:         $current_line = $first_bubble_line{$question-1} + 1 ;
 7225:         $lines        = $bubble_lines_per_response{$question-1};
 7226:     }
 7227:     if ($lines > 1) {
 7228:         $r->print(&mt('The group of bubble lines below responds to a single question.').'<br />');
 7229:         if (($responsetype_per_response{$question-1} eq 'essayresponse') ||
 7230:             ($responsetype_per_response{$question-1} eq 'formularesponse') ||
 7231:             ($responsetype_per_response{$question-1} eq 'stringresponse') ||
 7232:             ($responsetype_per_response{$question-1} eq 'imageresponse') ||
 7233:             ($responsetype_per_response{$question-1} eq 'reactionresponse') ||
 7234:             ($responsetype_per_response{$question-1} eq 'organicresponse')) {
 7235:             $r->print(&mt("Although this particular question type requires handgrading, the instructions for this question in the exam directed students to leave [quant,_1,line] blank on their bubblesheets.",$lines).'<br /><br />'.&mt('A non-zero score can be assigned to the student during bubblesheet grading by selecting a bubble in at least one line.').'<br />'.&mt('The score for this question will be a sum of the numeric values for the selected bubbles from each line, where A=1 point, B=2 points etc.').'<br />'.&mt("To assign a score of zero for this question, mark all lines as 'No bubble'.").'<br /><br />');
 7236:         } else {
 7237:             $r->print(&mt("Select at most one bubble in a single line and select 'No Bubble' in all the other lines. ")."<br />");
 7238:         }
 7239:     }
 7240:     for (my $i =0; $i < $lines; $i++) {
 7241:         my $selected = $$scan_record{"scantron.$current_line.answer"};
 7242: 	&scantron_bubble_selector($r,$scan_config,$current_line, 
 7243: 	        		  $questionnum,$error,split('', $selected));
 7244:         push(@linenums,$current_line);
 7245: 	$current_line++;
 7246:     }
 7247:     if ($lines > 1) {
 7248: 	$r->print("<hr /><br />");
 7249:     }
 7250:     return @linenums;
 7251: }
 7252: 
 7253: =pod
 7254: 
 7255: =item scantron_bubble_selector
 7256:   
 7257:    Generates the html radiobuttons to correct a single bubble line
 7258:    possibly showing the existing the selected bubbles if known
 7259: 
 7260:  Arguments:
 7261:     $r           - Apache request object
 7262:     $scan_config - hash from &get_scantron_config()
 7263:     $line        - Number of the line being displayed.
 7264:     $questionnum - Question number (may include subquestion)
 7265:     $error       - Type of error.
 7266:     @selected    - Array of bubbles picked on this line.
 7267: 
 7268: =cut
 7269: 
 7270: sub scantron_bubble_selector {
 7271:     my ($r,$scan_config,$line,$questionnum,$error,@selected)=@_;
 7272:     my $max=$$scan_config{'Qlength'};
 7273: 
 7274:     my $scmode=$$scan_config{'Qon'};
 7275:     if ($scmode eq 'number' || $scmode eq 'letter') { $max=10; }	     
 7276: 
 7277:     my @alphabet=('A'..'Z');
 7278:     $r->print(&Apache::loncommon::start_data_table().
 7279:               &Apache::loncommon::start_data_table_row());
 7280:     $r->print('<td rowspan="2" class="LC_leftcol_header">'.$line.'</td>');
 7281:     for (my $i=0;$i<$max+1;$i++) {
 7282: 	$r->print("\n".'<td align="center">');
 7283: 	if ($selected[0] eq $alphabet[$i]) { $r->print('X'); shift(@selected) }
 7284: 	else { $r->print('&nbsp;'); }
 7285: 	$r->print('</td>');
 7286:     }
 7287:     $r->print(&Apache::loncommon::end_data_table_row().
 7288:               &Apache::loncommon::start_data_table_row());
 7289:     for (my $i=0;$i<$max;$i++) {
 7290: 	$r->print("\n".
 7291: 		  '<td><label><input type="radio" name="scantron_correct_Q_'.
 7292: 		  $line.'" value="'.$i.'" />'.$alphabet[$i]."</label></td>");
 7293:     }
 7294:     my $nobub_checked = ' ';
 7295:     if ($error eq 'missingbubble') {
 7296:         $nobub_checked = ' checked = "checked" ';
 7297:     }
 7298:     $r->print("\n".'<td><label><input type="radio" name="scantron_correct_Q_'.
 7299: 	      $line.'" value="none"'.$nobub_checked.'/>'.&mt('No bubble').
 7300:               '</label>'."\n".'<input type="hidden" name="scantron_questionnum_Q_'.
 7301:               $line.'" value="'.$questionnum.'" /></td>');
 7302:     $r->print(&Apache::loncommon::end_data_table_row().
 7303:               &Apache::loncommon::end_data_table());
 7304: }
 7305: 
 7306: =pod
 7307: 
 7308: =item num_matches
 7309: 
 7310:    Counts the number of characters that are the same between the two arguments.
 7311: 
 7312:  Arguments:
 7313:    $orig - CODE from the scanline
 7314:    $code - CODE to match against
 7315: 
 7316:  Returns:
 7317:    $count - integer count of the number of same characters between the
 7318:             two arguments
 7319: 
 7320: =cut
 7321: 
 7322: sub num_matches {
 7323:     my ($orig,$code) = @_;
 7324:     my @code=split(//,$code);
 7325:     my @orig=split(//,$orig);
 7326:     my $same=0;
 7327:     for (my $i=0;$i<scalar(@code);$i++) {
 7328: 	if ($code[$i] eq $orig[$i]) { $same++; }
 7329:     }
 7330:     return $same;
 7331: }
 7332: 
 7333: =pod
 7334: 
 7335: =item scantron_get_closely_matching_CODEs
 7336: 
 7337:    Cycles through all CODEs and finds the set that has the greatest
 7338:    number of same characters as the provided CODE
 7339: 
 7340:  Arguments:
 7341:    $allcodes - hash ref returned by &get_codes()
 7342:    $CODE     - CODE from the current scanline
 7343: 
 7344:  Returns:
 7345:    2 element list
 7346:     - first elements is number of how closely matching the best fit is 
 7347:       (5 means best set has 5 matching characters)
 7348:     - second element is an arrary ref containing the set of valid CODEs
 7349:       that best fit the passed in CODE
 7350: 
 7351: =cut
 7352: 
 7353: sub scantron_get_closely_matching_CODEs {
 7354:     my ($allcodes,$CODE)=@_;
 7355:     my @CODEs;
 7356:     foreach my $testcode (sort(keys(%{$allcodes}))) {
 7357: 	push(@{$CODEs[&num_matches($CODE,$testcode)]},$testcode);
 7358:     }
 7359: 
 7360:     return ($#CODEs,$CODEs[-1]);
 7361: }
 7362: 
 7363: =pod
 7364: 
 7365: =item get_codes
 7366: 
 7367:    Builds a hash which has keys of all of the valid CODEs from the selected
 7368:    set of remembered CODEs.
 7369: 
 7370:  Arguments:
 7371:   $old_name - name of the set of remembered CODEs
 7372:   $cdom     - domain of the course
 7373:   $cnum     - internal course name
 7374: 
 7375:  Returns:
 7376:   %allcodes - keys are the valid CODEs, values are all 1
 7377: 
 7378: =cut
 7379: 
 7380: sub get_codes {
 7381:     my ($old_name, $cdom, $cnum) = @_;
 7382:     if (!$old_name) {
 7383: 	$old_name=$env{'form.scantron_CODElist'};
 7384:     }
 7385:     if (!$cdom) {
 7386: 	$cdom =$env{'course.'.$env{'request.course.id'}.'.domain'};
 7387:     }
 7388:     if (!$cnum) {
 7389: 	$cnum =$env{'course.'.$env{'request.course.id'}.'.num'};
 7390:     }
 7391:     my %result=&Apache::lonnet::get('CODEs',[$old_name,"type\0$old_name"],
 7392: 				    $cdom,$cnum);
 7393:     my %allcodes;
 7394:     if ($result{"type\0$old_name"} eq 'number') {
 7395: 	%allcodes=map {($_,1)} split(',',$result{$old_name});
 7396:     } else {
 7397: 	%allcodes=map {(&Apache::lonprintout::num_to_letters($_),1)} split(',',$result{$old_name});
 7398:     }
 7399:     return %allcodes;
 7400: }
 7401: 
 7402: =pod
 7403: 
 7404: =item scantron_validate_CODE
 7405: 
 7406:    Validates all scanlines in the selected file to not have any
 7407:    invalid or underspecified CODEs and that none of the codes are
 7408:    duplicated if this was requested.
 7409: 
 7410: =cut
 7411: 
 7412: sub scantron_validate_CODE {
 7413:     my ($r,$currentphase) = @_;
 7414:     my %scantron_config=&get_scantron_config($env{'form.scantron_format'});
 7415:     if ($scantron_config{'CODElocation'} &&
 7416: 	$scantron_config{'CODEstart'} &&
 7417: 	$scantron_config{'CODElength'}) {
 7418: 	if (!defined($env{'form.scantron_CODElist'})) {
 7419: 	    &FIXME_blow_up()
 7420: 	}
 7421:     } else {
 7422: 	return (0,$currentphase+1);
 7423:     }
 7424:     
 7425:     my %usedCODEs;
 7426: 
 7427:     my %allcodes=&get_codes();
 7428: 
 7429:     my $nav_error;
 7430:     &scantron_get_maxbubble(\$nav_error); # parse needs the lines per response array.
 7431:     if ($nav_error) {
 7432:         $r->print(&navmap_errormsg());
 7433:         return(1,$currentphase);
 7434:     }
 7435: 
 7436:     my ($scanlines,$scan_data)=&scantron_getfile();
 7437:     for (my $i=0;$i<=$scanlines->{'count'};$i++) {
 7438: 	my $line=&scantron_get_line($scanlines,$scan_data,$i);
 7439: 	if ($line=~/^[\s\cz]*$/) { next; }
 7440: 	my $scan_record=&scantron_parse_scanline($line,$i,\%scantron_config,
 7441: 						 $scan_data);
 7442: 	my $CODE=$$scan_record{'scantron.CODE'};
 7443: 	my $error=0;
 7444: 	if (!&Apache::lonnet::validCODE($CODE)) {
 7445: 	    &scantron_get_correction($r,$i,$scan_record,
 7446: 				     \%scantron_config,
 7447: 				     $line,'incorrectCODE',\%allcodes);
 7448: 	    return(1,$currentphase);
 7449: 	}
 7450: 	if (%allcodes && !exists($allcodes{$CODE}) 
 7451: 	    && !$$scan_record{'scantron.useCODE'}) {
 7452: 	    &scantron_get_correction($r,$i,$scan_record,
 7453: 				     \%scantron_config,
 7454: 				     $line,'incorrectCODE',\%allcodes);
 7455: 	    return(1,$currentphase);
 7456: 	}
 7457: 	if (exists($usedCODEs{$CODE}) 
 7458: 	    && $env{'form.scantron_CODEunique'} eq 'yes'
 7459: 	    && !$$scan_record{'scantron.CODE_ignore_dup'}) {
 7460: 	    &scantron_get_correction($r,$i,$scan_record,
 7461: 				     \%scantron_config,
 7462: 				     $line,'duplicateCODE',$usedCODEs{$CODE});
 7463: 	    return(1,$currentphase);
 7464: 	}
 7465: 	push(@{$usedCODEs{$CODE}},$$scan_record{'scantron.PaperID'});
 7466:     }
 7467:     return (0,$currentphase+1);
 7468: }
 7469: 
 7470: =pod
 7471: 
 7472: =item scantron_validate_doublebubble
 7473: 
 7474:    Validates all scanlines in the selected file to not have any
 7475:    bubble lines with multiple bubbles marked.
 7476: 
 7477: =cut
 7478: 
 7479: sub scantron_validate_doublebubble {
 7480:     my ($r,$currentphase) = @_;
 7481:     #get student info
 7482:     my $classlist=&Apache::loncoursedata::get_classlist();
 7483:     my %idmap=&username_to_idmap($classlist);
 7484: 
 7485:     #get scantron line setup
 7486:     my %scantron_config=&get_scantron_config($env{'form.scantron_format'});
 7487:     my ($scanlines,$scan_data)=&scantron_getfile();
 7488:     my $nav_error;
 7489:     &scantron_get_maxbubble(\$nav_error); # parse needs the bubble line array.
 7490:     if ($nav_error) {
 7491:         $r->print(&navmap_errormsg());
 7492:         return(1,$currentphase);
 7493:     }
 7494: 
 7495:     for (my $i=0;$i<=$scanlines->{'count'};$i++) {
 7496: 	my $line=&scantron_get_line($scanlines,$scan_data,$i);
 7497: 	if ($line=~/^[\s\cz]*$/) { next; }
 7498: 	my $scan_record=&scantron_parse_scanline($line,$i,\%scantron_config,
 7499: 						 $scan_data);
 7500: 	if (!defined($$scan_record{'scantron.doubleerror'})) { next; }
 7501: 	&scantron_get_correction($r,$i,$scan_record,\%scantron_config,$line,
 7502: 				 'doublebubble',
 7503: 				 $$scan_record{'scantron.doubleerror'});
 7504:     	return (1,$currentphase);
 7505:     }
 7506:     return (0,$currentphase+1);
 7507: }
 7508: 
 7509: 
 7510: sub scantron_get_maxbubble {
 7511:     my ($nav_error) = @_;
 7512:     if (defined($env{'form.scantron_maxbubble'}) &&
 7513: 	$env{'form.scantron_maxbubble'}) {
 7514: 	&restore_bubble_lines();
 7515: 	return $env{'form.scantron_maxbubble'};
 7516:     }
 7517: 
 7518:     my (undef, undef, $sequence) =
 7519: 	&Apache::lonnet::decode_symb($env{'form.selectpage'});
 7520: 
 7521:     my $navmap=Apache::lonnavmaps::navmap->new();
 7522:     unless (ref($navmap)) {
 7523:         if (ref($nav_error)) {
 7524:             $$nav_error = 1;
 7525:         }
 7526:         return;
 7527:     }
 7528:     my $map=$navmap->getResourceByUrl($sequence);
 7529:     my @resources=$navmap->retrieveResources($map,\&scantron_filter,1,0);
 7530: 
 7531:     &Apache::lonxml::clear_problem_counter();
 7532: 
 7533:     my $uname       = $env{'user.name'};
 7534:     my $udom        = $env{'user.domain'};
 7535:     my $cid         = $env{'request.course.id'};
 7536:     my $total_lines = 0;
 7537:     %bubble_lines_per_response = ();
 7538:     %first_bubble_line         = ();
 7539:     %subdivided_bubble_lines   = ();
 7540:     %responsetype_per_response = ();
 7541: 
 7542:     my $response_number = 0;
 7543:     my $bubble_line     = 0;
 7544:     foreach my $resource (@resources) {
 7545:         my ($analysis,$parts) = &scantron_partids_tograde($resource,$cid,$uname,$udom);
 7546:         if ((ref($analysis) eq 'HASH') && (ref($parts) eq 'ARRAY')) {
 7547: 	    foreach my $part_id (@{$parts}) {
 7548:                 my $lines;
 7549: 
 7550: 	        # TODO - make this a persistent hash not an array.
 7551: 
 7552:                 # optionresponse, matchresponse and rankresponse type items 
 7553:                 # render as separate sub-questions in exam mode.
 7554:                 if (($analysis->{$part_id.'.type'} eq 'optionresponse') ||
 7555:                     ($analysis->{$part_id.'.type'} eq 'matchresponse') ||
 7556:                     ($analysis->{$part_id.'.type'} eq 'rankresponse')) {
 7557:                     my ($numbub,$numshown);
 7558:                     if ($analysis->{$part_id.'.type'} eq 'optionresponse') {
 7559:                         if (ref($analysis->{$part_id.'.options'}) eq 'ARRAY') {
 7560:                             $numbub = scalar(@{$analysis->{$part_id.'.options'}});
 7561:                         }
 7562:                     } elsif ($analysis->{$part_id.'.type'} eq 'matchresponse') {
 7563:                         if (ref($analysis->{$part_id.'.items'}) eq 'ARRAY') {
 7564:                             $numbub = scalar(@{$analysis->{$part_id.'.items'}});
 7565:                         }
 7566:                     } elsif ($analysis->{$part_id.'.type'} eq 'rankresponse') {
 7567:                         if (ref($analysis->{$part_id.'.foils'}) eq 'ARRAY') {
 7568:                             $numbub = scalar(@{$analysis->{$part_id.'.foils'}});
 7569:                         }
 7570:                     }
 7571:                     if (ref($analysis->{$part_id.'.shown'}) eq 'ARRAY') {
 7572:                         $numshown = scalar(@{$analysis->{$part_id.'.shown'}});
 7573:                     }
 7574:                     my $bubbles_per_line = 10;
 7575:                     my $inner_bubble_lines = int($numbub/$bubbles_per_line);
 7576:                     if (($numbub % $bubbles_per_line) != 0) {
 7577:                         $inner_bubble_lines++;
 7578:                     }
 7579:                     for (my $i=0; $i<$numshown; $i++) {
 7580:                         $subdivided_bubble_lines{$response_number} .= 
 7581:                             $inner_bubble_lines.',';
 7582:                     }
 7583:                     $subdivided_bubble_lines{$response_number} =~ s/,$//;
 7584:                     $lines = $numshown * $inner_bubble_lines;
 7585:                 } else {
 7586:                     $lines = $analysis->{"$part_id.bubble_lines"};
 7587:                 } 
 7588: 
 7589:                 $first_bubble_line{$response_number} = $bubble_line;
 7590: 	        $bubble_lines_per_response{$response_number} = $lines;
 7591:                 $responsetype_per_response{$response_number} = 
 7592:                     $analysis->{$part_id.'.type'};
 7593: 	        $response_number++;
 7594: 
 7595: 	        $bubble_line +=  $lines;
 7596: 	        $total_lines +=  $lines;
 7597: 	    }
 7598:         }
 7599:     }
 7600:     &Apache::lonnet::delenv('scantron.');
 7601: 
 7602:     &save_bubble_lines();
 7603:     $env{'form.scantron_maxbubble'} =
 7604: 	$total_lines;
 7605:     return $env{'form.scantron_maxbubble'};
 7606: }
 7607: 
 7608: sub scantron_validate_missingbubbles {
 7609:     my ($r,$currentphase) = @_;
 7610:     #get student info
 7611:     my $classlist=&Apache::loncoursedata::get_classlist();
 7612:     my %idmap=&username_to_idmap($classlist);
 7613: 
 7614:     #get scantron line setup
 7615:     my %scantron_config=&get_scantron_config($env{'form.scantron_format'});
 7616:     my ($scanlines,$scan_data)=&scantron_getfile();
 7617:     my $nav_error;
 7618:     my $max_bubble=&scantron_get_maxbubble(\$nav_error);
 7619:     if ($nav_error) {
 7620:         return(1,$currentphase);
 7621:     }
 7622:     if (!$max_bubble) { $max_bubble=2**31; }
 7623:     for (my $i=0;$i<=$scanlines->{'count'};$i++) {
 7624: 	my $line=&scantron_get_line($scanlines,$scan_data,$i);
 7625: 	if ($line=~/^[\s\cz]*$/) { next; }
 7626: 	my $scan_record=&scantron_parse_scanline($line,$i,\%scantron_config,
 7627: 						 $scan_data);
 7628: 	if (!defined($$scan_record{'scantron.missingerror'})) { next; }
 7629: 	my @to_correct;
 7630: 	
 7631: 	# Probably here's where the error is...
 7632: 
 7633: 	foreach my $missing (@{$$scan_record{'scantron.missingerror'}}) {
 7634:             my $lastbubble;
 7635:             if ($missing =~ /^(\d+)\.(\d+)$/) {
 7636:                my $question = $1;
 7637:                my $subquestion = $2;
 7638:                if (!defined($first_bubble_line{$question -1})) { next; }
 7639:                my $first = $first_bubble_line{$question-1};
 7640:                my @subans = split(/,/,$subdivided_bubble_lines{$question-1});
 7641:                my $subcount = 1;
 7642:                while ($subcount<$subquestion) {
 7643:                    $first += $subans[$subcount-1];
 7644:                    $subcount ++;
 7645:                }
 7646:                my $count = $subans[$subquestion-1];
 7647:                $lastbubble = $first + $count;
 7648:             } else {
 7649:                 if (!defined($first_bubble_line{$missing - 1})) { next; }
 7650:                 $lastbubble = $first_bubble_line{$missing - 1} + $bubble_lines_per_response{$missing - 1};
 7651:             }
 7652:             if ($lastbubble > $max_bubble) { next; }
 7653: 	    push(@to_correct,$missing);
 7654: 	}
 7655: 	if (@to_correct) {
 7656: 	    &scantron_get_correction($r,$i,$scan_record,\%scantron_config,
 7657: 				     $line,'missingbubble',\@to_correct);
 7658: 	    return (1,$currentphase);
 7659: 	}
 7660: 
 7661:     }
 7662:     return (0,$currentphase+1);
 7663: }
 7664: 
 7665: 
 7666: sub scantron_process_students {
 7667:     my ($r) = @_;
 7668: 
 7669:     my (undef,undef,$sequence)=&Apache::lonnet::decode_symb($env{'form.selectpage'});
 7670:     my ($symb)=&get_symb($r);
 7671:     if (!$symb) {
 7672: 	return '';
 7673:     }
 7674:     my $default_form_data=&defaultFormData($symb);
 7675: 
 7676:     my %scantron_config=&get_scantron_config($env{'form.scantron_format'});
 7677:     my ($scanlines,$scan_data)=&scantron_getfile();
 7678:     my $classlist=&Apache::loncoursedata::get_classlist();
 7679:     my %idmap=&username_to_idmap($classlist);
 7680:     my $navmap=Apache::lonnavmaps::navmap->new();
 7681:     unless (ref($navmap)) {
 7682:         $r->print(&navmap_errormsg());
 7683:         return '';
 7684:     }  
 7685:     my $map=$navmap->getResourceByUrl($sequence);
 7686:     my @resources=$navmap->retrieveResources($map,\&scantron_filter,1,0);
 7687:     my (%grader_partids_by_symb,%grader_randomlists_by_symb);
 7688:     &graders_resources_pass(\@resources,\%grader_partids_by_symb,
 7689:                             \%grader_randomlists_by_symb);
 7690:     my $resource_error;
 7691:     foreach my $resource (@resources) {
 7692:         my $ressymb;
 7693:         if (ref($resource)) {
 7694:             $ressymb = $resource->symb();
 7695:         } else {
 7696:             $resource_error = 1;
 7697:             last;
 7698:         }
 7699:         my ($analysis,$parts) =
 7700:             &scantron_partids_tograde($resource,$env{'request.course.id'},
 7701:                                       $env{'user.name'},$env{'user.domain'},1);
 7702:         $grader_partids_by_symb{$ressymb} = $parts;
 7703:         if (ref($analysis) eq 'HASH') {
 7704:             if (ref($analysis->{'parts_withrandomlist'}) eq 'ARRAY') {
 7705:                 $grader_randomlists_by_symb{$ressymb} = 
 7706:                     $analysis->{'parts_withrandomlist'};
 7707:             }
 7708:         }
 7709:     }
 7710:     if ($resource_error) {
 7711:         $r->print(&navmap_errormsg());
 7712:         return '';
 7713:     }
 7714: 
 7715:     my ($uname,$udom);
 7716:     my $result= <<SCANTRONFORM;
 7717: <form method="post" enctype="multipart/form-data" action="/adm/grades" name="scantronupload">
 7718:   <input type="hidden" name="command" value="scantron_configphase" />
 7719:   $default_form_data
 7720: SCANTRONFORM
 7721:     $r->print($result);
 7722: 
 7723:     my @delayqueue;
 7724:     my (%completedstudents,%scandata);
 7725:     
 7726:     my $lock=&Apache::lonnet::set_lock(&mt('Grading bubblesheet exam'));
 7727:     my $count=&get_todo_count($scanlines,$scan_data);
 7728:     my %prog_state=&Apache::lonhtmlcommon::Create_PrgWin($r,'Bubblesheet Status',
 7729:  				    'Bubblesheet Progress',$count,
 7730: 				    'inline',undef,'scantronupload');
 7731:     &Apache::lonhtmlcommon::Update_PrgWin($r,\%prog_state,
 7732: 					  'Processing first student');
 7733:     $r->print('<br />');
 7734:     my $start=&Time::HiRes::time();
 7735:     my $i=-1;
 7736:     my $started;
 7737: 
 7738:     my $nav_error;
 7739:     &scantron_get_maxbubble(\$nav_error); # Need the bubble lines array to parse.
 7740:     if ($nav_error) {
 7741:         $r->print(&navmap_errormsg());
 7742:         return '';
 7743:     }
 7744: 
 7745:     # If an ssi failed in scantron_get_maxbubble, put an error message out to
 7746:     # the user and return.
 7747: 
 7748:     if ($ssi_error) {
 7749: 	$r->print("</form>");
 7750: 	&ssi_print_error($r);
 7751: 	$r->print(&show_grading_menu_form($symb));
 7752:         &Apache::lonnet::remove_lock($lock);
 7753: 	return '';		# Dunno why the other returns return '' rather than just returning.
 7754:     }
 7755: 
 7756:     my %lettdig = &letter_to_digits();
 7757:     my $numletts = scalar(keys(%lettdig));
 7758: 
 7759:     while ($i<$scanlines->{'count'}) {
 7760:  	($uname,$udom)=('','');
 7761:  	$i++;
 7762:  	my $line=&scantron_get_line($scanlines,$scan_data,$i);
 7763:  	if ($line=~/^[\s\cz]*$/) { next; }
 7764: 	if ($started) {
 7765: 	    &Apache::lonhtmlcommon::Increment_PrgWin($r,\%prog_state,
 7766: 						     'last student');
 7767: 	}
 7768: 	$started=1;
 7769:  	my $scan_record=&scantron_parse_scanline($line,$i,\%scantron_config,
 7770:  						 $scan_data);
 7771:  	unless ($uname=&scantron_find_student($scan_record,$scan_data,
 7772:  					      \%idmap,$i)) {
 7773:   	    &scantron_add_delay(\@delayqueue,$line,
 7774:  				'Unable to find a student that matches',1);
 7775:  	    next;
 7776:   	}
 7777:  	if (exists $completedstudents{$uname}) {
 7778:  	    &scantron_add_delay(\@delayqueue,$line,
 7779:  				'Student '.$uname.' has multiple sheets',2);
 7780:  	    next;
 7781:  	}
 7782:   	($uname,$udom)=split(/:/,$uname);
 7783: 
 7784:         my (%partids_by_symb,$res_error);
 7785:         foreach my $resource (@resources) {
 7786:             my $ressymb;
 7787:             if (ref($resource)) {
 7788:                 $ressymb = $resource->symb();
 7789:             } else {
 7790:                 $res_error = 1;
 7791:                 last;
 7792:             }
 7793:             if ((exists($grader_randomlists_by_symb{$ressymb})) ||
 7794:                 (ref($grader_partids_by_symb{$ressymb}) ne 'ARRAY')) {
 7795:                 my ($analysis,$parts) =
 7796:                     &scantron_partids_tograde($resource,$env{'request.course.id'},$uname,$udom);
 7797:                 $partids_by_symb{$ressymb} = $parts;
 7798:             } else {
 7799:                 $partids_by_symb{$ressymb} = $grader_partids_by_symb{$ressymb};
 7800:             }
 7801:         }
 7802: 
 7803:         if ($res_error) {
 7804:             &scantron_add_delay(\@delayqueue,$line,
 7805:                                 'An error occurred while grading student '.$uname,2);
 7806:             next;
 7807:         }
 7808: 
 7809: 	&Apache::lonxml::clear_problem_counter();
 7810:   	&Apache::lonnet::appenv($scan_record);
 7811: 
 7812: 	if (&scantron_clear_skip($scanlines,$scan_data,$i)) {
 7813: 	    &scantron_putfile($scanlines,$scan_data);
 7814: 	}
 7815: 	
 7816:         my $scancode;
 7817:         if ((exists($scan_record->{'scantron.CODE'})) &&
 7818:             (&Apache::lonnet::validCODE($scan_record->{'scantron.CODE'}))) {
 7819:             $scancode = $scan_record->{'scantron.CODE'};
 7820:         } else {
 7821:             $scancode = '';
 7822:         }
 7823: 
 7824:         if (&grade_student_bubbles($r,$uname,$udom,$scan_record,$scancode,
 7825:                                    \@resources,\%partids_by_symb) eq 'ssi_error') {
 7826:             $ssi_error = 0; # So end of handler error message does not trigger.
 7827:             $r->print("</form>");
 7828:             &ssi_print_error($r);
 7829:             $r->print(&show_grading_menu_form($symb));
 7830:             &Apache::lonnet::remove_lock($lock);
 7831:             return '';      # Why return ''?  Beats me.
 7832:         }
 7833: 
 7834: 	$completedstudents{$uname}={'line'=>$line};
 7835:         if ($env{'form.verifyrecord'}) {
 7836:             my $lastpos = $env{'form.scantron_maxbubble'}*$scantron_config{'Qlength'};
 7837:             my $studentdata = substr($line,$scantron_config{'Qstart'}-1,$lastpos);
 7838:             chomp($studentdata);
 7839:             $studentdata =~ s/\r$//;
 7840:             my $studentrecord = '';
 7841:             my $counter = -1;
 7842:             foreach my $resource (@resources) {
 7843:                 my $ressymb = $resource->symb();
 7844:                 ($counter,my $recording) =
 7845:                     &verify_scantron_grading($resource,$udom,$uname,$env{'request.course.id'},
 7846:                                              $counter,$studentdata,$partids_by_symb{$ressymb},
 7847:                                              \%scantron_config,\%lettdig,$numletts);
 7848:                 $studentrecord .= $recording;
 7849:             }
 7850:             if ($studentrecord ne $studentdata) {
 7851:                 &Apache::lonxml::clear_problem_counter();
 7852:                 if (&grade_student_bubbles($r,$uname,$udom,$scan_record,$scancode,
 7853:                                            \@resources,\%partids_by_symb) eq 'ssi_error') {
 7854:                     $ssi_error = 0; # So end of handler error message does not trigger.
 7855:                     $r->print("</form>");
 7856:                     &ssi_print_error($r);
 7857:                     $r->print(&show_grading_menu_form($symb));
 7858:                     &Apache::lonnet::remove_lock($lock);
 7859:                     delete($completedstudents{$uname});
 7860:                     return '';
 7861:                 }
 7862:                 $counter = -1;
 7863:                 $studentrecord = '';
 7864:                 foreach my $resource (@resources) {
 7865:                     my $ressymb = $resource->symb();
 7866:                     ($counter,my $recording) =
 7867:                         &verify_scantron_grading($resource,$udom,$uname,$env{'request.course.id'},
 7868:                                                  $counter,$studentdata,$partids_by_symb{$ressymb},
 7869:                                                  \%scantron_config,\%lettdig,$numletts);
 7870:                     $studentrecord .= $recording;
 7871:                 }
 7872:                 if ($studentrecord ne $studentdata) {
 7873:                     $r->print('<p><span class="LC_error">');
 7874:                     if ($scancode eq '') {
 7875:                         $r->print(&mt('Mismatch grading bubble sheet for user: [_1] with ID: [_2].',
 7876:                                   $uname.':'.$udom,$scan_record->{'scantron.ID'}));
 7877:                     } else {
 7878:                         $r->print(&mt('Mismatch grading bubble sheet for user: [_1] with ID: [_2] and CODE: [_3].',
 7879:                                   $uname.':'.$udom,$scan_record->{'scantron.ID'},$scancode));
 7880:                     }
 7881:                     $r->print('</span><br />'.&Apache::loncommon::start_data_table()."\n".
 7882:                               &Apache::loncommon::start_data_table_header_row()."\n".
 7883:                               '<th>'.&mt('Source').'</th><th>'.&mt('Bubbled responses').'</th>'.
 7884:                               &Apache::loncommon::end_data_table_header_row()."\n".
 7885:                               &Apache::loncommon::start_data_table_row().
 7886:                               '<td>'.&mt('Bubble Sheet').'</td>'.
 7887:                               '<td><span class="LC_nobreak">'.$studentdata.'</span></td>'.
 7888:                               &Apache::loncommon::end_data_table_row().
 7889:                               &Apache::loncommon::start_data_table_row().
 7890:                               '<td>Stored submissions</td>'.
 7891:                               '<td><span class="LC_nobreak">'.$studentrecord.'</span></td>'."\n".
 7892:                               &Apache::loncommon::end_data_table_row().
 7893:                               &Apache::loncommon::end_data_table().'</p>');
 7894:                 } else {
 7895:                     $r->print('<br /><span class="LC_warning">'.
 7896:                              &mt('A second grading pass was needed for user: [_1] with ID: [_2], because a mismatch was seen on the first pass.',$uname.':'.$udom,$scan_record->{'scantron.ID'}).'<br />'.
 7897:                              &mt("As a consequence, this user's submission history records two tries.").
 7898:                                  '</span><br />');
 7899:                 }
 7900:             }
 7901:         }
 7902:         if (&Apache::loncommon::connection_aborted($r)) { last; }
 7903:     } continue {
 7904: 	&Apache::lonxml::clear_problem_counter();
 7905: 	&Apache::lonnet::delenv('scantron.');
 7906:     }
 7907:     &Apache::lonhtmlcommon::Close_PrgWin($r,\%prog_state);
 7908:     &Apache::lonnet::remove_lock($lock);
 7909: #    my $lasttime = &Time::HiRes::time()-$start;
 7910: #    $r->print("<p>took $lasttime</p>");
 7911: 
 7912:     $r->print("</form>");
 7913:     $r->print(&show_grading_menu_form($symb));
 7914:     return '';
 7915: }
 7916: 
 7917: sub graders_resources_pass {
 7918:     my ($resources,$grader_partids_by_symb,$grader_randomlists_by_symb) = @_;
 7919:     if ((ref($resources) eq 'ARRAY') && (ref($grader_partids_by_symb)) && 
 7920:         (ref($grader_randomlists_by_symb) eq 'HASH')) {
 7921:         foreach my $resource (@{$resources}) {
 7922:             my $ressymb = $resource->symb();
 7923:             my ($analysis,$parts) =
 7924:                 &scantron_partids_tograde($resource,$env{'request.course.id'},
 7925:                                           $env{'user.name'},$env{'user.domain'},1);
 7926:             $grader_partids_by_symb->{$ressymb} = $parts;
 7927:             if (ref($analysis) eq 'HASH') {
 7928:                 if (ref($analysis->{'parts_withrandomlist'}) eq 'ARRAY') {
 7929:                     $grader_randomlists_by_symb->{$ressymb} =
 7930:                         $analysis->{'parts_withrandomlist'};
 7931:                 }
 7932:             }
 7933:         }
 7934:     }
 7935:     return;
 7936: }
 7937: 
 7938: sub grade_student_bubbles {
 7939:     my ($r,$uname,$udom,$scan_record,$scancode,$resources,$parts) = @_;
 7940:     if (ref($resources) eq 'ARRAY') {
 7941:         my $count = 0;
 7942:         foreach my $resource (@{$resources}) {
 7943:             my $ressymb = $resource->symb();
 7944:             my %form = ('submitted'      => 'scantron',
 7945:                         'grade_target'   => 'grade',
 7946:                         'grade_username' => $uname,
 7947:                         'grade_domain'   => $udom,
 7948:                         'grade_courseid' => $env{'request.course.id'},
 7949:                         'grade_symb'     => $ressymb,
 7950:                         'CODE'           => $scancode
 7951:                        );
 7952:             if (ref($parts) eq 'HASH') {
 7953:                 if (ref($parts->{$ressymb}) eq 'ARRAY') {
 7954:                     foreach my $part (@{$parts->{$ressymb}}) {
 7955:                         $form{'scantron_questnum_start.'.$part} =
 7956:                             1+$env{'form.scantron.first_bubble_line.'.$count};
 7957:                         $count++;
 7958:                     }
 7959:                 }
 7960:             }
 7961:             my $result=&ssi_with_retries($resource->src(),$ssi_retries,%form);
 7962:             return 'ssi_error' if ($ssi_error);
 7963:             last if (&Apache::loncommon::connection_aborted($r));
 7964:         }
 7965:     }
 7966:     return;
 7967: }
 7968: 
 7969: sub scantron_upload_scantron_data {
 7970:     my ($r)=@_;
 7971:     my $dom = $env{'request.role.domain'};
 7972:     my $domdesc = &Apache::lonnet::domain($dom,'description');
 7973:     $r->print(&Apache::loncommon::coursebrowser_javascript($dom));
 7974:     my $select_link=&Apache::loncommon::selectcourse_link('rules','courseid',
 7975: 							  'domainid',
 7976: 							  'coursename',$dom);
 7977:     my $syllabuslink = '<a href="javascript:ToSyllabus();">'.&mt('Syllabus').'</a>'.
 7978:                        ('&nbsp'x2).&mt('(shows course personnel)'); 
 7979:     my $default_form_data=&defaultFormData(&get_symb($r,1));
 7980:     my $nofile_alert = &mt('Please use the browse button to select a file from your local directory.');
 7981:     my $nocourseid_alert = &mt("Please use the 'Select Course' link to open a separate window where you can search for a course to which a file can be uploaded.");
 7982:     $r->print('
 7983: <script type="text/javascript" language="javascript">
 7984:     function checkUpload(formname) {
 7985: 	if (formname.upfile.value == "") {
 7986: 	    alert("'.$nofile_alert.'");
 7987: 	    return false;
 7988: 	}
 7989:         if (formname.courseid.value == "") {
 7990:             alert("'.$nocourseid_alert.'");
 7991:             return false;
 7992:         }
 7993: 	formname.submit();
 7994:     }
 7995: 
 7996:     function ToSyllabus() {
 7997:         var cdom = '."'$dom'".';
 7998:         var cnum = document.rules.courseid.value;
 7999:         if (cdom == "" || cdom == null) {
 8000:             return;
 8001:         }
 8002:         if (cnum == "" || cnum == null) {
 8003:            return;
 8004:         }
 8005:         syllwin=window.open("/public/"+cdom+"/"+cnum+"/syllabus","LONCAPASyllabus",
 8006:                             "height=350,width=350,scrollbars=yes,menubar=no");
 8007:         return;
 8008:     }
 8009: 
 8010: </script>
 8011: 
 8012: <h3>'.&mt('Send scanned bubblesheet data to a course').'</h3>
 8013: 
 8014: <form enctype="multipart/form-data" action="/adm/grades" name="rules" method="post">
 8015: '.$default_form_data.
 8016:   &Apache::lonhtmlcommon::start_pick_box().
 8017:   &Apache::lonhtmlcommon::row_title(&mt('Course ID')).
 8018:   '<input name="courseid" type="text" size="30" />'.$select_link.
 8019:   &Apache::lonhtmlcommon::row_closure().
 8020:   &Apache::lonhtmlcommon::row_title(&mt('Course Name')).
 8021:   '<input name="coursename" type="text" size="30" />'.$syllabuslink.
 8022:   &Apache::lonhtmlcommon::row_closure().
 8023:   &Apache::lonhtmlcommon::row_title(&mt('Domain')).
 8024:   '<input name="domainid" type="hidden" />'.$domdesc.
 8025:   &Apache::lonhtmlcommon::row_closure().
 8026:   &Apache::lonhtmlcommon::row_title(&mt('File to upload')).
 8027:   '<input type="file" name="upfile" size="50" />'.
 8028:   &Apache::lonhtmlcommon::row_closure(1).
 8029:   &Apache::lonhtmlcommon::end_pick_box().'<br />
 8030: 
 8031: <input name="command" value="scantronupload_save" type="hidden" />
 8032: <input type="button" onclick="javascript:checkUpload(this.form);" value="'.&mt('Upload Bubblesheet Data').'" />
 8033: </form>
 8034: ');
 8035:     return '';
 8036: }
 8037: 
 8038: 
 8039: sub scantron_upload_scantron_data_save {
 8040:     my($r)=@_;
 8041:     my ($symb)=&get_symb($r,1);
 8042:     my $doanotherupload=
 8043: 	'<br /><form action="/adm/grades" method="post">'."\n".
 8044: 	'<input type="hidden" name="command" value="scantronupload" />'."\n".
 8045: 	'<input type="submit" name="submit" value="'.&mt('Do Another Upload').'" />'."\n".
 8046: 	'</form>'."\n";
 8047:     if (!&Apache::lonnet::allowed('usc',$env{'form.domainid'}) &&
 8048: 	!&Apache::lonnet::allowed('usc',
 8049: 			    $env{'form.domainid'}.'_'.$env{'form.courseid'})) {
 8050: 	$r->print(&mt("You are not allowed to upload bubblesheet data to the requested course.")."<br />");
 8051: 	if ($symb) {
 8052: 	    $r->print(&show_grading_menu_form($symb));
 8053: 	} else {
 8054: 	    $r->print($doanotherupload);
 8055: 	}
 8056: 	return '';
 8057:     }
 8058:     my %coursedata=&Apache::lonnet::coursedescription($env{'form.domainid'}.'_'.$env{'form.courseid'});
 8059:     my $uploadedfile;
 8060:     $r->print('<h3>'.&mt("Uploading file to [_1]",$coursedata{'description'}).'</h3>');
 8061:     if (length($env{'form.upfile'}) < 2) {
 8062:         $r->print(&mt('[_1]Error:[_2] The file you attempted to upload, [_3] contained no information. Please check that you entered the correct filename.','<span class="LC_error">','</span>','<span class="LC_filename">'.&HTML::Entities::encode($env{'form.upfile.filename'},'<>&"').'</span>'));
 8063:     } else {
 8064:         my $result = 
 8065:             &Apache::lonnet::userfileupload('upfile','','scantron','','','',
 8066:                                             $env{'form.courseid'},$env{'form.domainid'});
 8067: 	if ($result =~ m{^/uploaded/}) {
 8068: 	    $r->print(&mt('[_1]Success:[_2] Successfully uploaded [_3] bytes of data into location: [_4]',
 8069:                           '<span class="LC_success">','</span>',(length($env{'form.upfile'})-1),
 8070: 			  '<span class="LC_filename">'.$result.'</span>'));
 8071:             ($uploadedfile) = ($result =~ m{/([^/]+)$});
 8072:             $r->print(&validate_uploaded_scantron_file($env{'form.domainid'},
 8073:                                                        $env{'form.courseid'},$uploadedfile));
 8074: 	} else {
 8075: 	    $r->print(&mt('[_1]Error:[_2] An error ([_3]) occurred when attempting to upload the file, [_4]',
 8076:                           '<span class="LC_error">','</span>',$result,
 8077: 			  '<span class="LC_filename">'.&HTML::Entities::encode($env{'form.upfile.filename'},'<>&"').'</span>'));
 8078: 	}
 8079:     }
 8080:     if ($symb) {
 8081: 	$r->print(&scantron_selectphase($r,$uploadedfile));
 8082:     } else {
 8083: 	$r->print($doanotherupload);
 8084:     }
 8085:     return '';
 8086: }
 8087: 
 8088: sub validate_uploaded_scantron_file {
 8089:     my ($cdom,$cname,$fname) = @_;
 8090:     my $scanlines=&Apache::lonnet::getfile('/uploaded/'.$cdom.'/'.$cname.'/'.$fname);
 8091:     my @lines;
 8092:     if ($scanlines ne '-1') {
 8093:         @lines=split("\n",$scanlines,-1);
 8094:     }
 8095:     my $output;
 8096:     if (@lines) {
 8097:         my (%counts,$max_match_format);
 8098:         my ($max_match_count,$max_match_pct) = (0,0);
 8099:         my $classlist = &Apache::loncoursedata::get_classlist($cdom,$cname);
 8100:         my %idmap = &username_to_idmap($classlist);
 8101:         foreach my $key (keys(%idmap)) {
 8102:             my $lckey = lc($key);
 8103:             $idmap{$lckey} = $idmap{$key};
 8104:         }
 8105:         my %unique_formats;
 8106:         my @formatlines = &get_scantronformat_file();
 8107:         foreach my $line (@formatlines) {
 8108:             chomp($line);
 8109:             my @config = split(/:/,$line);
 8110:             my $idstart = $config[5];
 8111:             my $idlength = $config[6];
 8112:             if (($idstart ne '') && ($idlength > 0)) {
 8113:                 if (ref($unique_formats{$idstart.':'.$idlength}) eq 'ARRAY') {
 8114:                     push(@{$unique_formats{$idstart.':'.$idlength}},$config[0].':'.$config[1]); 
 8115:                 } else {
 8116:                     $unique_formats{$idstart.':'.$idlength} = [$config[0].':'.$config[1]];
 8117:                 }
 8118:             }
 8119:         }
 8120:         foreach my $key (keys(%unique_formats)) {
 8121:             my ($idstart,$idlength) = split(':',$key);
 8122:             %{$counts{$key}} = (
 8123:                                'found'   => 0,
 8124:                                'total'   => 0,
 8125:                               );
 8126:             foreach my $line (@lines) {
 8127:                 next if ($line =~ /^#/);
 8128:                 next if ($line =~ /^[\s\cz]*$/);
 8129:                 my $id = substr($line,$idstart-1,$idlength);
 8130:                 $id = lc($id);
 8131:                 if (exists($idmap{$id})) {
 8132:                     $counts{$key}{'found'} ++;
 8133:                 }
 8134:                 $counts{$key}{'total'} ++;
 8135:             }
 8136:             if ($counts{$key}{'total'}) {
 8137:                 my $percent_match = (100*$counts{$key}{'found'})/($counts{$key}{'total'});
 8138:                 if (($max_match_format eq '') || ($percent_match > $max_match_pct)) {
 8139:                     $max_match_pct = $percent_match;
 8140:                     $max_match_format = $key;
 8141:                     $max_match_count = $counts{$key}{'total'};
 8142:                 }
 8143:             }
 8144:         }
 8145:         if (ref($unique_formats{$max_match_format}) eq 'ARRAY') {
 8146:             my $format_descs;
 8147:             my $numwithformat = @{$unique_formats{$max_match_format}};
 8148:             for (my $i=0; $i<$numwithformat; $i++) {
 8149:                 my ($name,$desc) = split(':',$unique_formats{$max_match_format}[$i]);
 8150:                 if ($i<$numwithformat-2) {
 8151:                     $format_descs .= '"<i>'.$desc.'</i>", ';
 8152:                 } elsif ($i==$numwithformat-2) {
 8153:                     $format_descs .= '"<i>'.$desc.'</i>" '.&mt('and').' ';
 8154:                 } elsif ($i==$numwithformat-1) {
 8155:                     $format_descs .= '"<i>'.$desc.'</i>"';
 8156:                 }
 8157:             }
 8158:             my $showpct = sprintf("%.0f",$max_match_pct).'%';
 8159:             $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).
 8160:                        '<br />'.&mt('A low percentage of matches results from one of the following:').'<ul>'.
 8161:                        '<li>'.&mt('The file was uploaded to the wrong course').'</li>'.
 8162:                        '<li>'.&mt('The data are not in the format expected for the domain: [_1]',
 8163:                                   '<i>'.$cdom.'</i>').'</li>'.
 8164:                        '<li>'.&mt('Students did not bubble their IDs, or mis-bubbled them').'</li>'.
 8165:                        '<li>'.&mt('The course roster is not up to date').'</li>'.
 8166:                        '</ul>';
 8167:         }
 8168:     } else {
 8169:         $output = '<span class="LC_warning">'.&mt('Uploaded file contained no data').'</span>';
 8170:     }
 8171:     return $output;
 8172: }
 8173: 
 8174: sub valid_file {
 8175:     my ($requested_file)=@_;
 8176:     foreach my $filename (sort(&scantron_filenames())) {
 8177: 	if ($requested_file eq $filename) { return 1; }
 8178:     }
 8179:     return 0;
 8180: }
 8181: 
 8182: sub scantron_download_scantron_data {
 8183:     my ($r)=@_;
 8184:     my $default_form_data=&defaultFormData(&get_symb($r,1));
 8185:     my $cname=$env{'course.'.$env{'request.course.id'}.'.num'};
 8186:     my $cdom=$env{'course.'.$env{'request.course.id'}.'.domain'};
 8187:     my $file=$env{'form.scantron_selectfile'};
 8188:     if (! &valid_file($file)) {
 8189: 	$r->print('
 8190: 	<p>
 8191: 	    '.&mt('The requested file name was invalid.').'
 8192:         </p>
 8193: ');
 8194: 	$r->print(&show_grading_menu_form(&get_symb($r,1)));
 8195: 	return;
 8196:     }
 8197:     my $orig='/uploaded/'.$cdom.'/'.$cname.'/scantron_orig_'.$file;
 8198:     my $corrected='/uploaded/'.$cdom.'/'.$cname.'/scantron_corrected_'.$file;
 8199:     my $skipped='/uploaded/'.$cdom.'/'.$cname.'/scantron_skipped_'.$file;
 8200:     &Apache::lonnet::allowuploaded('/adm/grades',$orig);
 8201:     &Apache::lonnet::allowuploaded('/adm/grades',$corrected);
 8202:     &Apache::lonnet::allowuploaded('/adm/grades',$skipped);
 8203:     $r->print('
 8204:     <p>
 8205: 	'.&mt('[_1]Original[_2] file as uploaded by the scantron office.',
 8206: 	      '<a href="'.$orig.'">','</a>').'
 8207:     </p>
 8208:     <p>
 8209: 	'.&mt('[_1]Corrections[_2], a file of corrected records that were used in grading.',
 8210: 	      '<a href="'.$corrected.'">','</a>').'
 8211:     </p>
 8212:     <p>
 8213: 	'.&mt('[_1]Skipped[_2], a file of records that were skipped.',
 8214: 	      '<a href="'.$skipped.'">','</a>').'
 8215:     </p>
 8216: ');
 8217:     $r->print(&show_grading_menu_form(&get_symb($r,1)));
 8218:     return '';
 8219: }
 8220: 
 8221: sub checkscantron_results {
 8222:     my ($r) = @_;
 8223:     my ($symb)=&get_symb($r);
 8224:     if (!$symb) {return '';}
 8225:     my $grading_menu_button=&show_grading_menu_form($symb);
 8226:     my $cid = $env{'request.course.id'};
 8227:     my %lettdig = &letter_to_digits();
 8228:     my $numletts = scalar(keys(%lettdig));
 8229:     my $cnum = $env{'course.'.$cid.'.num'};
 8230:     my $cdom = $env{'course.'.$cid.'.domain'};
 8231:     my (undef, undef, $sequence) = &Apache::lonnet::decode_symb($env{'form.selectpage'});
 8232:     my %record;
 8233:     my %scantron_config =
 8234:         &Apache::grades::get_scantron_config($env{'form.scantron_format'});
 8235:     my ($scanlines,$scan_data)=&Apache::grades::scantron_getfile();
 8236:     my $classlist=&Apache::loncoursedata::get_classlist();
 8237:     my %idmap=&Apache::grades::username_to_idmap($classlist);
 8238:     my $navmap=Apache::lonnavmaps::navmap->new();
 8239:     unless (ref($navmap)) {
 8240:         $r->print(&navmap_errormsg());
 8241:         return '';
 8242:     }
 8243:     my $map=$navmap->getResourceByUrl($sequence);
 8244:     my @resources=$navmap->retrieveResources($map,\&scantron_filter,1,0);
 8245:     my (%grader_partids_by_symb,%grader_randomlists_by_symb);
 8246:     &graders_resources_pass(\@resources,\%grader_partids_by_symb,                             \%grader_randomlists_by_symb);
 8247: 
 8248:     my ($uname,$udom);
 8249:     my (%scandata,%lastname,%bylast);
 8250:     $r->print('
 8251: <form method="post" enctype="multipart/form-data" action="/adm/grades" name="checkscantron">'."\n");
 8252: 
 8253:     my @delayqueue;
 8254:     my %completedstudents;
 8255: 
 8256:     my $count=&Apache::grades::get_todo_count($scanlines,$scan_data);
 8257:     my %prog_state=&Apache::lonhtmlcommon::Create_PrgWin($r,'Bubblesheet/Submissions Comparison Status',
 8258:                                     'Progress of Bubblesheet Data/Submission Records Comparison',$count,
 8259:                                     'inline',undef,'checkscantron');
 8260:     my ($username,$domain,$started);
 8261:     my $nav_error;
 8262:     &scantron_get_maxbubble(\$nav_error); # Need the bubble lines array to parse.
 8263:     if ($nav_error) {
 8264:         $r->print(&navmap_errormsg());
 8265:         return '';
 8266:     }
 8267: 
 8268:     &Apache::lonhtmlcommon::Update_PrgWin($r,\%prog_state,
 8269:                                           'Processing first student');
 8270:     my $start=&Time::HiRes::time();
 8271:     my $i=-1;
 8272: 
 8273:     while ($i<$scanlines->{'count'}) {
 8274:         ($username,$domain,$uname)=('','','');
 8275:         $i++;
 8276:         my $line=&Apache::grades::scantron_get_line($scanlines,$scan_data,$i);
 8277:         if ($line=~/^[\s\cz]*$/) { next; }
 8278:         if ($started) {
 8279:             &Apache::lonhtmlcommon::Increment_PrgWin($r,\%prog_state,
 8280:                                                      'last student');
 8281:         }
 8282:         $started=1;
 8283:         my $scan_record=
 8284:             &Apache::grades::scantron_parse_scanline($line,$i,\%scantron_config,
 8285:                                                      $scan_data);
 8286:         unless ($uname=&Apache::grades::scantron_find_student($scan_record,$scan_data,
 8287:                                                               \%idmap,$i)) {
 8288:             &Apache::grades::scantron_add_delay(\@delayqueue,$line,
 8289:                                 'Unable to find a student that matches',1);
 8290:             next;
 8291:         }
 8292:         if (exists $completedstudents{$uname}) {
 8293:             &Apache::grades::scantron_add_delay(\@delayqueue,$line,
 8294:                                 'Student '.$uname.' has multiple sheets',2);
 8295:             next;
 8296:         }
 8297:         my $pid = $scan_record->{'scantron.ID'};
 8298:         $lastname{$pid} = $scan_record->{'scantron.LastName'};
 8299:         push(@{$bylast{$lastname{$pid}}},$pid);
 8300:         my $lastpos = $env{'form.scantron_maxbubble'}*$scantron_config{'Qlength'};
 8301:         $scandata{$pid} = substr($line,$scantron_config{'Qstart'}-1,$lastpos);
 8302:         chomp($scandata{$pid});
 8303:         $scandata{$pid} =~ s/\r$//;
 8304:         ($username,$domain)=split(/:/,$uname);
 8305:         my $counter = -1;
 8306:         foreach my $resource (@resources) {
 8307:             my $parts;
 8308:             my $ressymb = $resource->symb();
 8309:             if ((exists($grader_randomlists_by_symb{$ressymb})) ||
 8310:                 (ref($grader_partids_by_symb{$ressymb}) ne 'ARRAY')) {
 8311:                 (my $analysis,$parts) =
 8312:                     &scantron_partids_tograde($resource,$env{'request.course.id'},$username,$domain);
 8313:             } else {
 8314:                 $parts = $grader_partids_by_symb{$ressymb};
 8315:             }
 8316:             ($counter,my $recording) =
 8317:                 &verify_scantron_grading($resource,$domain,$username,$cid,$counter,
 8318:                                          $scandata{$pid},$parts,
 8319:                                          \%scantron_config,\%lettdig,$numletts);
 8320:             $record{$pid} .= $recording;
 8321:         }
 8322:     }
 8323:     &Apache::lonhtmlcommon::Close_PrgWin($r,\%prog_state);
 8324:     $r->print('<br />');
 8325:     my ($okstudents,$badstudents,$numstudents,$passed,$failed);
 8326:     $passed = 0;
 8327:     $failed = 0;
 8328:     $numstudents = 0;
 8329:     foreach my $last (sort(keys(%bylast))) {
 8330:         if (ref($bylast{$last}) eq 'ARRAY') {
 8331:             foreach my $pid (sort(@{$bylast{$last}})) {
 8332:                 my $showscandata = $scandata{$pid};
 8333:                 my $showrecord = $record{$pid};
 8334:                 $showscandata =~ s/\s/&nbsp;/g;
 8335:                 $showrecord =~ s/\s/&nbsp;/g;
 8336:                 if ($scandata{$pid} eq $record{$pid}) {
 8337:                     my $css_class = ($passed % 2)?'LC_odd_row':'LC_even_row';
 8338:                     $okstudents .= '<tr class="'.$css_class.'">'.
 8339: '<td>'.&mt('Bubblesheet').'</td><td>'.$showscandata.'</td><td rowspan="2">'.$last.'</td><td rowspan="2">'.$pid.'</td>'."\n".
 8340: '</tr>'."\n".
 8341: '<tr class="'.$css_class.'">'."\n".
 8342: '<td>Submissions</td><td>'.$showrecord.'</td></tr>'."\n";
 8343:                     $passed ++;
 8344:                 } else {
 8345:                     my $css_class = ($failed % 2)?'LC_odd_row':'LC_even_row';
 8346:                     $badstudents .= '<tr class="'.$css_class.'"><td>'.&mt('Bubblesheet').'</td><td><span class="LC_nobreak">'.$scandata{$pid}.'</span></td><td rowspan="2">'.$last.'</td><td rowspan="2">'.$pid.'</td>'."\n".
 8347: '</tr>'."\n".
 8348: '<tr class="'.$css_class.'">'."\n".
 8349: '<td>Submissions</td><td><span class="LC_nobreak">'.$record{$pid}.'</span></td>'."\n".
 8350: '</tr>'."\n";
 8351:                     $failed ++;
 8352:                 }
 8353:                 $numstudents ++;
 8354:             }
 8355:         }
 8356:     }
 8357:     $r->print('<p>'.&mt('Comparison of bubblesheet data (including corrections) with corresponding submission records (most recent submission) for <b>[quant,_1,student]</b>  ([_2] scantron lines/student).',$numstudents,$env{'form.scantron_maxbubble'}).'</p>');
 8358:     $r->print('<p>'.&mt('Exact matches for <b>[quant,_1,student]</b>.',$passed).'<br />'.&mt('Discrepancies detected for <b>[quant,_1,student]</b>.',$failed).'</p>');
 8359:     if ($passed) {
 8360:         $r->print(&mt('Students with exact correspondence between bubblesheet data and submissions are as follows:').'<br /><br />');
 8361:         $r->print(&Apache::loncommon::start_data_table()."\n".
 8362:                  &Apache::loncommon::start_data_table_header_row()."\n".
 8363:                  '<th>'.&mt('Source').'</th><th>'.&mt('Bubble records').'</th><th>'.&mt('Name').'</th><th>'.&mt('ID').'</th>'.
 8364:                  &Apache::loncommon::end_data_table_header_row()."\n".
 8365:                  $okstudents."\n".
 8366:                  &Apache::loncommon::end_data_table().'<br />');
 8367:     }
 8368:     if ($failed) {
 8369:         $r->print(&mt('Students with differences between bubblesheet data and submissions are as follows:').'<br /><br />');
 8370:         $r->print(&Apache::loncommon::start_data_table()."\n".
 8371:                  &Apache::loncommon::start_data_table_header_row()."\n".
 8372:                  '<th>'.&mt('Source').'</th><th>'.&mt('Bubble records').'</th><th>'.&mt('Name').'</th><th>'.&mt('ID').'</th>'.
 8373:                  &Apache::loncommon::end_data_table_header_row()."\n".
 8374:                  $badstudents."\n".
 8375:                  &Apache::loncommon::end_data_table()).'<br />'.
 8376:                  &mt('Differences can occur if submissions were modified using manual grading after a bubblesheet grading pass.').'<br />'.&mt('If unexpected discrepancies were detected, it is recommended that you inspect the original bubblesheets.');  
 8377:     }
 8378:     $r->print('</form><br />'.$grading_menu_button);
 8379:     return;
 8380: }
 8381: 
 8382: sub verify_scantron_grading {
 8383:     my ($resource,$domain,$username,$cid,$counter,$scandata,$partids,
 8384:         $scantron_config,$lettdig,$numletts) = @_;
 8385:     my ($record,%expected,%startpos);
 8386:     return ($counter,$record) if (!ref($resource));
 8387:     return ($counter,$record) if (!$resource->is_problem());
 8388:     my $symb = $resource->symb();
 8389:     return ($counter,$record) if (ref($partids) ne 'ARRAY');
 8390:     foreach my $part_id (@{$partids}) {
 8391:         $counter ++;
 8392:         $expected{$part_id} = 0;
 8393:         if ($env{"form.scantron.sub_bubblelines.$counter"}) {
 8394:             my @sub_lines = split(/,/,$env{"form.scantron.sub_bubblelines.$counter"});
 8395:             foreach my $item (@sub_lines) {
 8396:                 $expected{$part_id} += $item;
 8397:             }
 8398:         } else {
 8399:             $expected{$part_id} = $env{"form.scantron.bubblelines.$counter"};
 8400:         }
 8401:         $startpos{$part_id} = $env{"form.scantron.first_bubble_line.$counter"};
 8402:     }
 8403:     if ($symb) {
 8404:         my %recorded;
 8405:         my (%returnhash) = &Apache::lonnet::restore($symb,$cid,$domain,$username);
 8406:         if ($returnhash{'version'}) {
 8407:             my %lasthash=();
 8408:             my $version;
 8409:             for ($version=1;$version<=$returnhash{'version'};$version++) {
 8410:                 foreach my $key (sort(split(/\:/,$returnhash{$version.':keys'}))) {
 8411:                     $lasthash{$key}=$returnhash{$version.':'.$key};
 8412:                 }
 8413:             }
 8414:             foreach my $key (keys(%lasthash)) {
 8415:                 if ($key =~ /\.scantron$/) {
 8416:                     my $value = &unescape($lasthash{$key});
 8417:                     my ($part_id) = ($key =~ /^resource\.(.+)\.scantron$/);
 8418:                     if ($value eq '') {
 8419:                         for (my $i=0; $i<$expected{$part_id}; $i++) {
 8420:                             for (my $j=0; $j<$scantron_config->{'length'}; $j++) {
 8421:                                 $recorded{$part_id} .= $scantron_config->{'Qoff'};
 8422:                             }
 8423:                         }
 8424:                     } else {
 8425:                         my @tocheck;
 8426:                         my @items = split(//,$value);
 8427:                         if (($scantron_config->{'Qon'} eq 'letter') ||
 8428:                             ($scantron_config->{'Qon'} eq 'number')) {
 8429:                             if (@items < $expected{$part_id}) {
 8430:                                 my $fragment = substr($scandata,$startpos{$part_id},$expected{$part_id});
 8431:                                 my @singles = split(//,$fragment);
 8432:                                 foreach my $pos (@singles) {
 8433:                                     if ($pos eq ' ') {
 8434:                                         push(@tocheck,$pos);
 8435:                                     } else {
 8436:                                         my $next = shift(@items);
 8437:                                         push(@tocheck,$next);
 8438:                                     }
 8439:                                 }
 8440:                             } else {
 8441:                                 @tocheck = @items;
 8442:                             }
 8443:                             foreach my $letter (@tocheck) {
 8444:                                 if ($scantron_config->{'Qon'} eq 'letter') {
 8445:                                     if ($letter !~ /^[A-J]$/) {
 8446:                                         $letter = $scantron_config->{'Qoff'};
 8447:                                     }
 8448:                                     $recorded{$part_id} .= $letter;
 8449:                                 } elsif ($scantron_config->{'Qon'} eq 'number') {
 8450:                                     my $digit;
 8451:                                     if ($letter !~ /^[A-J]$/) {
 8452:                                         $digit = $scantron_config->{'Qoff'};
 8453:                                     } else {
 8454:                                         $digit = $lettdig->{$letter};
 8455:                                     }
 8456:                                     $recorded{$part_id} .= $digit;
 8457:                                 }
 8458:                             }
 8459:                         } else {
 8460:                             @tocheck = @items;
 8461:                             for (my $i=0; $i<$expected{$part_id}; $i++) {
 8462:                                 my $curr_sub = shift(@tocheck);
 8463:                                 my $digit;
 8464:                                 if ($curr_sub =~ /^[A-J]$/) {
 8465:                                     $digit = $lettdig->{$curr_sub}-1;
 8466:                                 }
 8467:                                 if ($curr_sub eq 'J') {
 8468:                                     $digit += scalar($numletts);
 8469:                                 }
 8470:                                 for (my $j=0; $j<$scantron_config->{'Qlength'}; $j++) {
 8471:                                     if ($j == $digit) {
 8472:                                         $recorded{$part_id} .= $scantron_config->{'Qon'};
 8473:                                     } else {
 8474:                                         $recorded{$part_id} .= $scantron_config->{'Qoff'};
 8475:                                     }
 8476:                                 }
 8477:                             }
 8478:                         }
 8479:                     }
 8480:                 }
 8481:             }
 8482:         }
 8483:         foreach my $part_id (@{$partids}) {
 8484:             if ($recorded{$part_id} eq '') {
 8485:                 for (my $i=0; $i<$expected{$part_id}; $i++) {
 8486:                     for (my $j=0; $j<$scantron_config->{'Qlength'}; $j++) {
 8487:                         $recorded{$part_id} .= $scantron_config->{'Qoff'};
 8488:                     }
 8489:                 }
 8490:             }
 8491:             $record .= $recorded{$part_id};
 8492:         }
 8493:     }
 8494:     return ($counter,$record);
 8495: }
 8496: 
 8497: sub letter_to_digits { 
 8498:     my %lettdig = (
 8499:                     A => 1,
 8500:                     B => 2,
 8501:                     C => 3,
 8502:                     D => 4,
 8503:                     E => 5,
 8504:                     F => 6,
 8505:                     G => 7,
 8506:                     H => 8,
 8507:                     I => 9,
 8508:                     J => 0,
 8509:                   );
 8510:     return %lettdig;
 8511: }
 8512: 
 8513: 
 8514: #-------- end of section for handling grading scantron forms -------
 8515: #
 8516: #-------------------------------------------------------------------
 8517: 
 8518: #-------------------------- Menu interface -------------------------
 8519: #
 8520: #--- Show a Grading Menu button - Calls the next routine ---
 8521: sub show_grading_menu_form {
 8522:     my ($symb)=@_;
 8523:     my $result.='<br /><form action="/adm/grades" method="post">'."\n".
 8524: 	'<input type="hidden" name="symb" value="'.&Apache::lonenc::check_encrypt($symb).'" />'."\n".
 8525: 	'<input type="hidden" name="saveState"  value="'.$env{'form.saveState'}.'" />'."\n".
 8526: 	'<input type="hidden" name="command" value="gradingmenu" />'."\n".
 8527: 	'<input type="submit" name="submit" value="'.&mt('Grading Menu').'" />'."\n".
 8528: 	'</form>'."\n";
 8529:     return $result;
 8530: }
 8531: 
 8532: # -- Retrieve choices for grading form
 8533: sub savedState {
 8534:     my %savedState = ();
 8535:     if ($env{'form.saveState'}) {
 8536: 	foreach (split(/:/,$env{'form.saveState'})) {
 8537: 	    my ($key,$value) = split(/=/,$_,2);
 8538: 	    $savedState{$key} = $value;
 8539: 	}
 8540:     }
 8541:     return \%savedState;
 8542: }
 8543: 
 8544: sub grading_menu {
 8545:     my ($request) = @_;
 8546:     my ($symb)=&get_symb($request);
 8547:     if (!$symb) {return '';}
 8548:     my $probTitle = &Apache::lonnet::gettitle($symb);
 8549:     my ($table,undef,$hdgrade) = &showResourceInfo($symb,$probTitle);
 8550: 
 8551:     $request->print($table);
 8552:     my %fields = ('symb'=>&Apache::lonenc::check_encrypt($symb),
 8553:                   'handgrade'=>$hdgrade,
 8554:                   'probTitle'=>$probTitle,
 8555:                   'command'=>'submit_options',
 8556:                   'saveState'=>"",
 8557:                   'gradingMenu'=>1,
 8558:                   'showgrading'=>"yes");
 8559:     
 8560:     my $url1 = &Apache::lonhtmlcommon::build_url('grades/',\%fields);
 8561:     
 8562:     $fields{'command'} = 'csvform';
 8563:     my $url2 = &Apache::lonhtmlcommon::build_url('grades/',\%fields);
 8564:     
 8565:     $fields{'command'} = 'processclicker';
 8566:     my $url3 = &Apache::lonhtmlcommon::build_url('grades/',\%fields);
 8567:     
 8568:     $fields{'command'} = 'scantron_selectphase';
 8569:     my $url4 = &Apache::lonhtmlcommon::build_url('grades/',\%fields);
 8570:     
 8571:     my @menu = ({	categorytitle=>'Course Grading',
 8572:             items =>[
 8573:                         {	linktext => 'Manual Grading/View Submissions',
 8574:                     		url => $url1,
 8575:                     		permission => 'F',
 8576:                     		icon => 'edit-find-replace.png',
 8577:                     		linktitle => 'Start the process of hand grading submissions.'
 8578:                         },
 8579:                 	    {	linktext => 'Upload Scores',
 8580:                     		url => $url2,
 8581:                     		permission => 'F',
 8582:                     		icon => 'uploadscores.png',
 8583:                     		linktitle => 'Specify a file containing the class scores for current resource.'
 8584:                 	    },
 8585:                 	    {	linktext => 'Process Clicker',
 8586:                     		url => $url3,
 8587:                     		permission => 'F',
 8588:                     		icon => 'addClickerInfoFile.png',
 8589:                     		linktitle => 'Specify a file containing the clicker information for this resource.'
 8590:                 	    },
 8591:                 	    {	linktext => 'Grade/Manage/Review Bubblesheets',
 8592:                     		url => $url4,
 8593:                     		permission => 'F',
 8594:                     		icon => 'stat.png',
 8595:                     		linktitle => 'Grade scantron exams, upload/download scantron data files, and review previously graded scantron exams.'
 8596:                 	    }
 8597:                     ]
 8598:             });
 8599: 
 8600:     #$fields{'command'} = 'verify';
 8601:     #$url = &Apache::lonhtmlcommon::build_url('grades/',\%fields);
 8602:     #
 8603:     # Create the menu
 8604:     my $Str;
 8605:     # $Str .= '<h2>'.&mt('Please select a grading task').'</h2>';
 8606:     $Str .= '<form method="post" action="" name="gradingMenu">';
 8607:     $Str .= '<input type="hidden" name="command" value="" />'.
 8608:     	'<input type="hidden" name="symb"        value="'.&Apache::lonenc::check_encrypt($symb).'" />'."\n".
 8609: 	'<input type="hidden" name="handgrade"   value="'.$hdgrade.'" />'."\n".
 8610: 	'<input type="hidden" name="probTitle"   value="'.$probTitle.'" />'."\n".
 8611: 	'<input type="hidden" name="saveState"   value="" />'."\n".
 8612: 	'<input type="hidden" name="gradingMenu" value="1" />'."\n".
 8613: 	'<input type="hidden" name="showgrading" value="yes" />'."\n";
 8614: 
 8615:     $Str .= Apache::lonhtmlcommon::generate_menu(@menu);
 8616:     #$menudata->{'jscript'}
 8617:     $Str .='<hr /><input type="button" value="'.&mt('Verify Receipt No.').'" '.
 8618:         ' onclick="javascript:checkChoice(document.forms.gradingMenu,\'5\',\'verify\')" '.
 8619:         ' /> '.
 8620:         &Apache::lonnet::recprefix($env{'request.course.id'}).
 8621:         '-<input type="text" name="receipt" size="4" onchange="javascript:checkReceiptNo(this.form,\'OK\')" />';
 8622: 
 8623:     $Str .="</form>\n";
 8624:     my $receiptalert = &mt("Please enter a receipt number given by a student in the receipt box.");
 8625:     $request->print(<<GRADINGMENUJS);
 8626: <script type="text/javascript" language="javascript">
 8627:     function checkChoice(formname,val,cmdx) {
 8628: 	if (val <= 2) {
 8629: 	    var cmd = radioSelection(formname.radioChoice);
 8630: 	    var cmdsave = cmd;
 8631: 	} else {
 8632: 	    cmd = cmdx;
 8633: 	    cmdsave = 'submission';
 8634: 	}
 8635: 	formname.command.value = cmd;
 8636: 	if (val < 5) formname.submit();
 8637: 	if (val == 5) {
 8638: 	    if (!checkReceiptNo(formname,'notOK')) { 
 8639: 	        return false;
 8640: 	    } else {
 8641: 	        formname.submit();
 8642: 	    }
 8643: 	}
 8644:     }
 8645: 
 8646:     function checkReceiptNo(formname,nospace) {
 8647: 	var receiptNo = formname.receipt.value;
 8648: 	var checkOpt = false;
 8649: 	if (nospace == "OK" && isNaN(receiptNo)) {checkOpt = true;}
 8650: 	if (nospace == "notOK" && (isNaN(receiptNo) || receiptNo == "")) {checkOpt = true;}
 8651: 	if (checkOpt) {
 8652: 	    alert("$receiptalert");
 8653: 	    formname.receipt.value = "";
 8654: 	    formname.receipt.focus();
 8655: 	    return false;
 8656: 	}
 8657: 	return true;
 8658:     }
 8659: </script>
 8660: GRADINGMENUJS
 8661:     &commonJSfunctions($request);
 8662:     return $Str;    
 8663: }
 8664: 
 8665: 
 8666: #--- Displays the submissions first page -------
 8667: sub submit_options {
 8668:     my ($request) = @_;
 8669:     my ($symb)=&get_symb($request);
 8670:     if (!$symb) {return '';}
 8671:     my $probTitle = &Apache::lonnet::gettitle($symb);
 8672: 
 8673:     my $receiptalert = &mt("Please enter a receipt number given by a student in the receipt box."); 
 8674:     $request->print(<<GRADINGMENUJS);
 8675: <script type="text/javascript" language="javascript">
 8676:     function checkChoice(formname,val,cmdx) {
 8677: 	if (val <= 2) {
 8678: 	    var cmd = radioSelection(formname.radioChoice);
 8679: 	    var cmdsave = cmd;
 8680: 	} else {
 8681: 	    cmd = cmdx;
 8682: 	    cmdsave = 'submission';
 8683: 	}
 8684: 	formname.command.value = cmd;
 8685: 	formname.saveState.value = "saveCmd="+cmdsave+":saveSec="+pullDownSelection(formname.section)+
 8686: 	    ":saveSub="+pullDownSelection(formname.submitonly)+":saveStatus="+pullDownSelection(formname.Status);
 8687: 	if (val < 5) formname.submit();
 8688: 	if (val == 5) {
 8689: 	    if (!checkReceiptNo(formname,'notOK')) { return false;}
 8690: 	    formname.submit();
 8691: 	}
 8692: 	if (val < 7) formname.submit();
 8693:     }
 8694: 
 8695:     function checkReceiptNo(formname,nospace) {
 8696: 	var receiptNo = formname.receipt.value;
 8697: 	var checkOpt = false;
 8698: 	if (nospace == "OK" && isNaN(receiptNo)) {checkOpt = true;}
 8699: 	if (nospace == "notOK" && (isNaN(receiptNo) || receiptNo == "")) {checkOpt = true;}
 8700: 	if (checkOpt) {
 8701: 	    alert("$receiptalert");
 8702: 	    formname.receipt.value = "";
 8703: 	    formname.receipt.focus();
 8704: 	    return false;
 8705: 	}
 8706: 	return true;
 8707:     }
 8708: </script>
 8709: GRADINGMENUJS
 8710:     &commonJSfunctions($request);
 8711:     my ($table,undef,$hdgrade) = &showResourceInfo($symb,$probTitle);
 8712:     my $result;
 8713:     my (undef,$sections) = &getclasslist('all','0');
 8714:     my $savedState = &savedState();
 8715:     my $saveCmd = ($$savedState{'saveCmd'} eq '' ? 'submission' : $$savedState{'saveCmd'});
 8716:     my $saveSec = ($$savedState{'saveSec'} eq '' ? 'all' : $$savedState{'saveSec'});
 8717:     my $saveSub = ($$savedState{'saveSub'} eq '' ? 'all' : $$savedState{'saveSub'});
 8718:     my $saveStatus = ($$savedState{'saveStatus'} eq '' ? 'Active' : $$savedState{'saveStatus'});
 8719: 
 8720:     # Preselect sections
 8721:     my $selsec="";
 8722:     if (ref($sections)) {
 8723:         foreach my $section (sort(@$sections)) {
 8724:             $selsec.='<option value="'.$section.'" '.
 8725:                 ($saveSec eq $section ? 'selected="selected"':'').'>'.$section.'</option>'."\n";
 8726:         }
 8727:     }
 8728: 
 8729:     $result.='<form action="/adm/grades" method="post" name="gradingMenu">'."\n".
 8730: 	'<input type="hidden" name="symb"        value="'.&Apache::lonenc::check_encrypt($symb).'" />'."\n".
 8731: 	'<input type="hidden" name="handgrade"   value="'.$hdgrade.'" />'."\n".
 8732: 	'<input type="hidden" name="probTitle"   value="'.$probTitle.'" />'."\n".
 8733: 	'<input type="hidden" name="command"     value="" />'."\n".
 8734: 	'<input type="hidden" name="saveState"   value="" />'."\n".
 8735: 	'<input type="hidden" name="gradingMenu" value="1" />'."\n".
 8736: 	'<input type="hidden" name="showgrading" value="yes" />'."\n";
 8737: 
 8738:     $result.='
 8739: <h2>
 8740:   '.&mt('Grade Current Resource').'
 8741: </h2>
 8742: <div>
 8743:   '.$table.'
 8744: </div>
 8745: 
 8746: <div class="LC_columnSection">
 8747:   
 8748:     <fieldset>
 8749:       <legend>
 8750:        '.&mt('Sections').'
 8751:       </legend>
 8752:       <select name="section" multiple="multiple" size="5">'."\n";
 8753:     $result.= $selsec;
 8754:     $result.= '<option value="all" '.($saveSec eq 'all' ? 'selected="selected"' : ''). '>all</option></select> &nbsp; ';
 8755:     $result.='
 8756:     </fieldset>
 8757:   
 8758:     <fieldset>
 8759:       <legend>
 8760:         '.&mt('Groups').'
 8761:       </legend>
 8762:       '.&Apache::lonstatistics::GroupSelect('group','multiple',5).'
 8763:     </fieldset>
 8764:   
 8765:     <fieldset>
 8766:       <legend>
 8767:         '.&mt('Access Status').'
 8768:       </legend>
 8769:       '.&Apache::lonhtmlcommon::StatusOptions($saveStatus,undef,5,undef,'mult').'
 8770:     </fieldset>
 8771:   
 8772:     <fieldset>
 8773:       <legend>
 8774:         '.&mt('Submission Status').'
 8775:       </legend>
 8776:       <select name="submitonly" size="5">
 8777: 	         <option value="yes" '.      ($saveSub eq 'yes'       ? 'selected="selected"' : '').'>'.&mt('with submissions').'</option>
 8778: 	         <option value="queued" '.   ($saveSub eq 'queued'    ? 'selected="selected"' : '').'>'.&mt('in grading queue').'</option>
 8779: 	         <option value="graded" '.   ($saveSub eq 'graded'    ? 'selected="selected"' : '').'>'.&mt('with ungraded submissions').'</option>
 8780: 	         <option value="incorrect" '.($saveSub eq 'incorrect' ? 'selected="selected"' : '').'>'.&mt('with incorrect submissions').'</option>
 8781:                  <option value="all" '.      ($saveSub eq 'all'       ? 'selected="selected"' : '').'>'.&mt('with any status').'</option>
 8782:       </select>
 8783:     </fieldset>
 8784:   
 8785: </div>
 8786: 
 8787: <br />
 8788:           <div>
 8789:             <div>
 8790:               <label>
 8791:                 <input type="radio" name="radioChoice" value="submission" '.
 8792:                   ($saveCmd eq 'submission' ? 'checked="checked"' : '').' /> '.
 8793:              &mt('Select individual students to grade and view submissions.').'
 8794: 	      </label> 
 8795:             </div>
 8796:             <div>
 8797: 	      <label>
 8798:                 <input type="radio" name="radioChoice" value="viewgrades" '.
 8799:                   ($saveCmd eq 'viewgrades' ? 'checked="checked"' : '').' /> '.
 8800:                     &mt('Grade all selected students in a grading table.').'
 8801:               </label>
 8802:             </div>
 8803:             <div>
 8804: 	      <input type="button" onclick="javascript:checkChoice(this.form,\'2\');" value="'.&mt('Next').' &rarr;" />
 8805:             </div>
 8806:           </div>
 8807: 
 8808: 
 8809:         <h2>
 8810:          '.&mt('Grade Complete Folder for One Student').'
 8811:         </h2>
 8812:         <div>
 8813:             <div>
 8814:               <label>
 8815:                 <input type="radio" name="radioChoice" value="pickStudentPage" '.
 8816: 	  ($saveCmd eq 'pickStudentPage' ? 'checked="checked"' : '').' /> '.
 8817:   &mt('The <b>complete</b> page/sequence/folder: For one student').'
 8818:               </label>
 8819:             </div>
 8820:             <div>
 8821: 	      <input type="button" onclick="javascript:checkChoice(this.form,\'2\');" value="'.&mt('Next').' &rarr;" />
 8822:             </div>
 8823:         </div>
 8824:   </form>';
 8825:     $result .= &show_grading_menu_form($symb);
 8826:     return $result;
 8827: }
 8828: 
 8829: sub reset_perm {
 8830:     undef(%perm);
 8831: }
 8832: 
 8833: sub init_perm {
 8834:     &reset_perm();
 8835:     foreach my $test_perm ('vgr','mgr','opa') {
 8836: 
 8837: 	my $scope = $env{'request.course.id'};
 8838: 	if (!($perm{$test_perm}=&Apache::lonnet::allowed($test_perm,$scope))) {
 8839: 
 8840: 	    $scope .= '/'.$env{'request.course.sec'};
 8841: 	    if ( $perm{$test_perm}=
 8842: 		 &Apache::lonnet::allowed($test_perm,$scope)) {
 8843: 		$perm{$test_perm.'_section'}=$env{'request.course.sec'};
 8844: 	    } else {
 8845: 		delete($perm{$test_perm});
 8846: 	    }
 8847: 	}
 8848:     }
 8849: }
 8850: 
 8851: sub gather_clicker_ids {
 8852:     my %clicker_ids;
 8853: 
 8854:     my $classlist = &Apache::loncoursedata::get_classlist();
 8855: 
 8856:     # Set up a couple variables.
 8857:     my $username_idx = &Apache::loncoursedata::CL_SNAME();
 8858:     my $domain_idx   = &Apache::loncoursedata::CL_SDOM();
 8859:     my $status_idx   = &Apache::loncoursedata::CL_STATUS();
 8860: 
 8861:     foreach my $student (keys(%$classlist)) {
 8862:         if ($classlist->{$student}->[$status_idx] ne 'Active') { next; }
 8863:         my $username = $classlist->{$student}->[$username_idx];
 8864:         my $domain   = $classlist->{$student}->[$domain_idx];
 8865:         my $clickers =
 8866: 	    (&Apache::lonnet::userenvironment($domain,$username,'clickers'))[1];
 8867:         foreach my $id (split(/\,/,$clickers)) {
 8868:             $id=~s/^[\#0]+//;
 8869:             $id=~s/[\-\:]//g;
 8870:             if (exists($clicker_ids{$id})) {
 8871: 		$clicker_ids{$id}.=','.$username.':'.$domain;
 8872:             } else {
 8873: 		$clicker_ids{$id}=$username.':'.$domain;
 8874:             }
 8875:         }
 8876:     }
 8877:     return %clicker_ids;
 8878: }
 8879: 
 8880: sub gather_adv_clicker_ids {
 8881:     my %clicker_ids;
 8882:     my $cnum=$env{'course.'.$env{'request.course.id'}.'.num'};
 8883:     my $cdom=$env{'course.'.$env{'request.course.id'}.'.domain'};
 8884:     my %coursepersonnel=&Apache::lonnet::get_course_adv_roles($cdom.'/'.$cnum);
 8885:     foreach my $element (sort(keys(%coursepersonnel))) {
 8886:         foreach my $person (split(/\,/,$coursepersonnel{$element})) {
 8887:             my ($puname,$pudom)=split(/\:/,$person);
 8888:             my $clickers =
 8889: 		(&Apache::lonnet::userenvironment($pudom,$puname,'clickers'))[1];
 8890:             foreach my $id (split(/\,/,$clickers)) {
 8891: 		$id=~s/^[\#0]+//;
 8892:                 $id=~s/[\-\:]//g;
 8893: 		if (exists($clicker_ids{$id})) {
 8894: 		    $clicker_ids{$id}.=','.$puname.':'.$pudom;
 8895: 		} else {
 8896: 		    $clicker_ids{$id}=$puname.':'.$pudom;
 8897: 		}
 8898:             }
 8899:         }
 8900:     }
 8901:     return %clicker_ids;
 8902: }
 8903: 
 8904: sub clicker_grading_parameters {
 8905:     return ('gradingmechanism' => 'scalar',
 8906:             'upfiletype' => 'scalar',
 8907:             'specificid' => 'scalar',
 8908:             'pcorrect' => 'scalar',
 8909:             'pincorrect' => 'scalar');
 8910: }
 8911: 
 8912: sub process_clicker {
 8913:     my ($r)=@_;
 8914:     my ($symb)=&get_symb($r);
 8915:     if (!$symb) {return '';}
 8916:     my $result=&checkforfile_js();
 8917:     $env{'form.probTitle'} = &Apache::lonnet::gettitle($symb);
 8918:     my ($table) = &showResourceInfo($symb,$env{'form.probTitle'});
 8919:     $result.=$table;
 8920:     $result.='<br /><table width="100%" border="0"><tr><td bgcolor="#777777">'."\n";
 8921:     $result.='<table width="100%" border="0"><tr bgcolor="#e6ffff"><td>'."\n";
 8922:     $result.='&nbsp;<b>'.&mt('Specify a file containing the clicker information for this resource.').
 8923:         '</b></td></tr>'."\n";
 8924:     $result.='<tr bgcolor=#ffffe6><td>'."\n";
 8925: # Attempt to restore parameters from last session, set defaults if not present
 8926:     my %Saveable_Parameters=&clicker_grading_parameters();
 8927:     &Apache::loncommon::restore_course_settings('grades_clicker',
 8928:                                                  \%Saveable_Parameters);
 8929:     if (!$env{'form.pcorrect'}) { $env{'form.pcorrect'}=100; }
 8930:     if (!$env{'form.pincorrect'}) { $env{'form.pincorrect'}=100; }
 8931:     if (!$env{'form.gradingmechanism'}) { $env{'form.gradingmechanism'}='attendance'; }
 8932:     if (!$env{'form.upfiletype'}) { $env{'form.upfiletype'}='iclicker'; }
 8933: 
 8934:     my %checked;
 8935:     foreach my $gradingmechanism ('attendance','personnel','specific','given') {
 8936:        if ($env{'form.gradingmechanism'} eq $gradingmechanism) {
 8937:           $checked{$gradingmechanism}=' checked="checked"';
 8938:        }
 8939:     }
 8940: 
 8941:     my $upload=&mt("Upload File");
 8942:     my $type=&mt("Type");
 8943:     my $attendance=&mt("Award points just for participation");
 8944:     my $personnel=&mt("Correctness determined from response by course personnel");
 8945:     my $specific=&mt("Correctness determined from response with clicker ID(s)"); 
 8946:     my $given=&mt("Correctness determined from given list of answers").' '.
 8947:               '<font size="-2"><tt>('.&mt("Provide comma-separated list. Use '*' for any answer correct, '-' for skip").')</tt></font>';
 8948:     my $pcorrect=&mt("Percentage points for correct solution");
 8949:     my $pincorrect=&mt("Percentage points for incorrect solution");
 8950:     my $selectform=&Apache::loncommon::select_form($env{'form.upfiletype'},'upfiletype',
 8951: 						   ('iclicker' => 'i>clicker',
 8952:                                                     'interwrite' => 'interwrite PRS'));
 8953:     $symb = &Apache::lonenc::check_encrypt($symb);
 8954:     $result.=<<ENDUPFORM;
 8955: <script type="text/javascript">
 8956: function sanitycheck() {
 8957: // Accept only integer percentages
 8958:    document.forms.gradesupload.pcorrect.value=Math.round(document.forms.gradesupload.pcorrect.value);
 8959:    document.forms.gradesupload.pincorrect.value=Math.round(document.forms.gradesupload.pincorrect.value);
 8960: // Find out grading choice
 8961:    for (i=0; i<document.forms.gradesupload.gradingmechanism.length; i++) {
 8962:       if (document.forms.gradesupload.gradingmechanism[i].checked) {
 8963:          gradingchoice=document.forms.gradesupload.gradingmechanism[i].value;
 8964:       }
 8965:    }
 8966: // By default, new choice equals user selection
 8967:    newgradingchoice=gradingchoice;
 8968: // Not good to give more points for false answers than correct ones
 8969:    if (Math.round(document.forms.gradesupload.pcorrect.value)<Math.round(document.forms.gradesupload.pincorrect.value)) {
 8970:       document.forms.gradesupload.pcorrect.value=document.forms.gradesupload.pincorrect.value;
 8971:    }
 8972: // If new choice is attendance only, and old choice was correctness-based, restore defaults
 8973:    if ((gradingchoice=='attendance') && (document.forms.gradesupload.waschecked.value!='attendance')) {
 8974:       document.forms.gradesupload.pcorrect.value=100;
 8975:       document.forms.gradesupload.pincorrect.value=100;
 8976:    }
 8977: // If the values are different, cannot be attendance only
 8978:    if ((Math.round(document.forms.gradesupload.pcorrect.value)!=Math.round(document.forms.gradesupload.pincorrect.value)) &&
 8979:        (gradingchoice=='attendance')) {
 8980:        newgradingchoice='personnel';
 8981:    }
 8982: // Change grading choice to new one
 8983:    for (i=0; i<document.forms.gradesupload.gradingmechanism.length; i++) {
 8984:       if (document.forms.gradesupload.gradingmechanism[i].value==newgradingchoice) {
 8985:          document.forms.gradesupload.gradingmechanism[i].checked=true;
 8986:       } else {
 8987:          document.forms.gradesupload.gradingmechanism[i].checked=false;
 8988:       }
 8989:    }
 8990: // Remember the old state
 8991:    document.forms.gradesupload.waschecked.value=newgradingchoice;
 8992: }
 8993: </script>
 8994: <form method="post" enctype="multipart/form-data" action="/adm/grades" name="gradesupload">
 8995: <input type="hidden" name="symb" value="$symb" />
 8996: <input type="hidden" name="command" value="processclickerfile" />
 8997: <input type="hidden" name="probTitle" value="$env{'form.probTitle'}" />
 8998: <input type="hidden" name="saveState"  value="$env{'form.saveState'}" />
 8999: <input type="file" name="upfile" size="50" />
 9000: <br /><label>$type: $selectform</label>
 9001: <br /><label><input type="radio" name="gradingmechanism" value="attendance"$checked{'attendance'} onclick="sanitycheck()" />$attendance </label>
 9002: <br /><label><input type="radio" name="gradingmechanism" value="personnel"$checked{'personnel'} onclick="sanitycheck()" />$personnel</label>
 9003: <br /><label><input type="radio" name="gradingmechanism" value="specific"$checked{'specific'} onclick="sanitycheck()" />$specific </label>
 9004: <input type="text" name="specificid" value="$env{'form.specificid'}" size="20" />
 9005: <br /><label><input type="radio" name="gradingmechanism" value="given"$checked{'given'} onclick="sanitycheck()" />$given </label>
 9006: <br />&nbsp;&nbsp;&nbsp;
 9007: <input type="text" name="givenanswer" size="50" />
 9008: <input type="hidden" name="waschecked" value="$env{'form.gradingmechanism'}" />
 9009: <br /><label>$pcorrect: <input type="text" name="pcorrect" size="4" value="$env{'form.pcorrect'}" onchange="sanitycheck()" /></label>
 9010: <br /><label>$pincorrect: <input type="text" name="pincorrect" size="4" value="$env{'form.pincorrect'}" onchange="sanitycheck()" /></label>
 9011: <br /><input type="button" onclick="javascript:checkUpload(this.form);" value="$upload" />
 9012: </form>
 9013: ENDUPFORM
 9014:     $result.='</td></tr></table>'."\n".
 9015:              '</td></tr></table><br /><br />'."\n";
 9016:     $result.=&show_grading_menu_form($symb);
 9017:     return $result;
 9018: }
 9019: 
 9020: sub process_clicker_file {
 9021:     my ($r)=@_;
 9022:     my ($symb)=&get_symb($r);
 9023:     if (!$symb) {return '';}
 9024: 
 9025:     my %Saveable_Parameters=&clicker_grading_parameters();
 9026:     &Apache::loncommon::store_course_settings('grades_clicker',
 9027:                                               \%Saveable_Parameters);
 9028: 
 9029:     my ($result) = &showResourceInfo($symb,$env{'form.probTitle'});
 9030:     if (($env{'form.gradingmechanism'} eq 'specific') && ($env{'form.specificid'}!~/\w/)) {
 9031: 	$result.='<span class="LC_error">'.&mt('You need to specify a clicker ID for the correct answer').'</span>';
 9032: 	return $result.&show_grading_menu_form($symb);
 9033:     }
 9034:     if (($env{'form.gradingmechanism'} eq 'given') && ($env{'form.givenanswer'}!~/\S/)) {
 9035:         $result.='<span class="LC_error">'.&mt('You need to specify the correct answer').'</span>';
 9036:         return $result.&show_grading_menu_form($symb);
 9037:     }
 9038:     my $foundgiven=0;
 9039:     if ($env{'form.gradingmechanism'} eq 'given') {
 9040:         $env{'form.givenanswer'}=~s/^\s*//gs;
 9041:         $env{'form.givenanswer'}=~s/\s*$//gs;
 9042:         $env{'form.givenanswer'}=~s/[^a-zA-Z0-9\.\*\-]+/\,/g;
 9043:         $env{'form.givenanswer'}=uc($env{'form.givenanswer'});
 9044:         my @answers=split(/\,/,$env{'form.givenanswer'});
 9045:         $foundgiven=$#answers+1;
 9046:     }
 9047:     my %clicker_ids=&gather_clicker_ids();
 9048:     my %correct_ids;
 9049:     if ($env{'form.gradingmechanism'} eq 'personnel') {
 9050: 	%correct_ids=&gather_adv_clicker_ids();
 9051:     }
 9052:     if ($env{'form.gradingmechanism'} eq 'specific') {
 9053: 	foreach my $correct_id (split(/[\s\,]/,$env{'form.specificid'})) {;
 9054: 	   $correct_id=~tr/a-z/A-Z/;
 9055: 	   $correct_id=~s/\s//gs;
 9056: 	   $correct_id=~s/^[\#0]+//;
 9057:            $correct_id=~s/[\-\:]//g;
 9058:            if ($correct_id) {
 9059: 	      $correct_ids{$correct_id}='specified';
 9060:            }
 9061:         }
 9062:     }
 9063:     if ($env{'form.gradingmechanism'} eq 'attendance') {
 9064: 	$result.=&mt('Score based on attendance only');
 9065:     } elsif ($env{'form.gradingmechanism'} eq 'given') {
 9066:         $result.=&mt('Score based on [_1] ([_2] answers)','<tt>'.$env{'form.givenanswer'}.'</tt>',$foundgiven);
 9067:     } else {
 9068: 	my $number=0;
 9069: 	$result.='<p><b>'.&mt('Correctness determined by the following IDs').'</b>';
 9070: 	foreach my $id (sort(keys(%correct_ids))) {
 9071: 	    $result.='<br /><tt>'.$id.'</tt> - ';
 9072: 	    if ($correct_ids{$id} eq 'specified') {
 9073: 		$result.=&mt('specified');
 9074: 	    } else {
 9075: 		my ($uname,$udom)=split(/\:/,$correct_ids{$id});
 9076: 		$result.=&Apache::loncommon::plainname($uname,$udom);
 9077: 	    }
 9078: 	    $number++;
 9079: 	}
 9080:         $result.="</p>\n";
 9081: 	if ($number==0) {
 9082: 	    $result.='<span class="LC_error">'.&mt('No IDs found to determine correct answer').'</span>';
 9083: 	    return $result.&show_grading_menu_form($symb);
 9084: 	}
 9085:     }
 9086:     if (length($env{'form.upfile'}) < 2) {
 9087:         $result.=&mt('[_1] Error: [_2] The file you attempted to upload, [_3] contained no information. Please check that you entered the correct filename.',
 9088: 		     '<span class="LC_error">',
 9089: 		     '</span>',
 9090: 		     '<span class="LC_filename">'.&HTML::Entities::encode($env{'form.upfile.filename'},'<>&"').'</span>');
 9091:         return $result.&show_grading_menu_form($symb);
 9092:     }
 9093: 
 9094: # Were able to get all the info needed, now analyze the file
 9095: 
 9096:     $result.=&Apache::loncommon::studentbrowser_javascript();
 9097:     $symb = &Apache::lonenc::check_encrypt($symb);
 9098:     my $heading=&mt('Scanning clicker file');
 9099:     $result.=(<<ENDHEADER);
 9100: <br /><table width="100%" border="0"><tr><td bgcolor="#777777">
 9101: <table width="100%" border="0"><tr bgcolor="#e6ffff"><td>
 9102: <b>$heading</b></td></tr><tr bgcolor=#ffffe6><td>
 9103: <form method="post" action="/adm/grades" name="clickeranalysis">
 9104: <input type="hidden" name="symb" value="$symb" />
 9105: <input type="hidden" name="command" value="assignclickergrades" />
 9106: <input type="hidden" name="probTitle" value="$env{'form.probTitle'}" />
 9107: <input type="hidden" name="saveState"  value="$env{'form.saveState'}" />
 9108: <input type="hidden" name="gradingmechanism" value="$env{'form.gradingmechanism'}" />
 9109: <input type="hidden" name="pcorrect" value="$env{'form.pcorrect'}" />
 9110: <input type="hidden" name="pincorrect" value="$env{'form.pincorrect'}" />
 9111: ENDHEADER
 9112:     if ($env{'form.gradingmechanism'} eq 'given') {
 9113:        $result.='<input type="hidden" name="correct:given" value="'.$env{'form.givenanswer'}.'" />';
 9114:     } 
 9115:     my %responses;
 9116:     my @questiontitles;
 9117:     my $errormsg='';
 9118:     my $number=0;
 9119:     if ($env{'form.upfiletype'} eq 'iclicker') {
 9120: 	($errormsg,$number)=&iclicker_eval(\@questiontitles,\%responses);
 9121:     }
 9122:     if ($env{'form.upfiletype'} eq 'interwrite') {
 9123:         ($errormsg,$number)=&interwrite_eval(\@questiontitles,\%responses);
 9124:     }
 9125:     $result.='<br />'.&mt('Found [_1] question(s)',$number).'<br />'.
 9126:              '<input type="hidden" name="number" value="'.$number.'" />'.
 9127:              &mt('Awarding [_1] percent for correct and [_2] percent for incorrect responses',
 9128:                  $env{'form.pcorrect'},$env{'form.pincorrect'}).
 9129:              '<br />';
 9130:     if (($env{'form.gradingmechanism'} eq 'given') && ($number!=$foundgiven)) {
 9131:        $result.='<span class="LC_error">'.&mt('Number of given answers does not agree with number of questions in file.').'</span>';
 9132:        return $result.&show_grading_menu_form($symb);
 9133:     } 
 9134: # Remember Question Titles
 9135: # FIXME: Possibly need delimiter other than ":"
 9136:     for (my $i=0;$i<$number;$i++) {
 9137:         $result.='<input type="hidden" name="question:'.$i.'" value="'.
 9138:                  &HTML::Entities::encode($questiontitles[$i],'"&<>').'" />';
 9139:     }
 9140:     my $correct_count=0;
 9141:     my $student_count=0;
 9142:     my $unknown_count=0;
 9143: # Match answers with usernames
 9144: # FIXME: Possibly need delimiter other than ":"
 9145:     foreach my $id (keys(%responses)) {
 9146:        if ($correct_ids{$id}) {
 9147:           $result.="\n".'<input type="hidden" name="correct:'.$correct_count.':'.$correct_ids{$id}.'" value="'.$responses{$id}.'" />';
 9148:           $correct_count++;
 9149:        } elsif ($clicker_ids{$id}) {
 9150:           if ($clicker_ids{$id}=~/\,/) {
 9151: # More than one user with the same clicker!
 9152:              $result.="\n<hr />".&mt('Clicker registered more than once').": <tt>".$id."</tt><br />";
 9153:              $result.="\n".'<input type="hidden" name="unknown:'.$id.'" value="'.$responses{$id}.'" />'.
 9154:                            "<select name='multi".$id."'>";
 9155:              foreach my $reguser (sort(split(/\,/,$clicker_ids{$id}))) {
 9156:                  $result.="<option value='".$reguser."'>".&Apache::loncommon::plainname(split(/\:/,$reguser)).' ('.$reguser.')</option>';
 9157:              }
 9158:              $result.='</select>';
 9159:              $unknown_count++;
 9160:           } else {
 9161: # Good: found one and only one user with the right clicker
 9162:              $result.="\n".'<input type="hidden" name="student:'.$clicker_ids{$id}.'" value="'.$responses{$id}.'" />';
 9163:              $student_count++;
 9164:           }
 9165:        } else {
 9166:           $result.="\n<hr />".&mt('Unregistered Clicker')." <tt>".$id."</tt><br />";
 9167:           $result.="\n".'<input type="hidden" name="unknown:'.$id.'" value="'.$responses{$id}.'" />'.
 9168:                    "\n".&mt("Username").": <input type='text' name='uname".$id."' />&nbsp;".
 9169:                    "\n".&mt("Domain").": ".
 9170:                    &Apache::loncommon::select_dom_form($env{'course.'.$env{'request.course.id'}.'.domain'},'udom'.$id).'&nbsp;'.
 9171:                    &Apache::loncommon::selectstudent_link('clickeranalysis','uname'.$id,'udom'.$id);
 9172:           $unknown_count++;
 9173:        }
 9174:     }
 9175:     $result.='<hr />'.
 9176:              &mt('Found [_1] registered and [_2] unregistered clickers.',$student_count,$unknown_count);
 9177:     if (($env{'form.gradingmechanism'} ne 'attendance') && ($env{'form.gradingmechanism'} ne 'given')) {
 9178:        if ($correct_count==0) {
 9179:           $errormsg.="Found no correct answers answers for grading!";
 9180:        } elsif ($correct_count>1) {
 9181:           $result.='<br /><span class="LC_warning">'.&mt("Found [_1] entries for grading!",$correct_count).'</span>';
 9182:        }
 9183:     }
 9184:     if ($number<1) {
 9185:        $errormsg.="Found no questions.";
 9186:     }
 9187:     if ($errormsg) {
 9188:        $result.='<br /><span class="LC_error">'.&mt($errormsg).'</span>';
 9189:     } else {
 9190:        $result.='<br /><input type="submit" name="finalize" value="'.&mt('Finalize Grading').'" />';
 9191:     }
 9192:     $result.='</form></td></tr></table>'."\n".
 9193:              '</td></tr></table><br /><br />'."\n";
 9194:     return $result.&show_grading_menu_form($symb);
 9195: }
 9196: 
 9197: sub iclicker_eval {
 9198:     my ($questiontitles,$responses)=@_;
 9199:     my $number=0;
 9200:     my $errormsg='';
 9201:     foreach my $line (split(/[\n\r]/,$env{'form.upfile'})) {
 9202:         my %components=&Apache::loncommon::record_sep($line);
 9203:         my @entries=map {$components{$_}} (sort(keys(%components)));
 9204: 	if ($entries[0] eq 'Question') {
 9205: 	    for (my $i=3;$i<$#entries;$i+=6) {
 9206: 		$$questiontitles[$number]=$entries[$i];
 9207: 		$number++;
 9208: 	    }
 9209: 	}
 9210: 	if ($entries[0]=~/^\#/) {
 9211: 	    my $id=$entries[0];
 9212: 	    my @idresponses;
 9213: 	    $id=~s/^[\#0]+//;
 9214: 	    for (my $i=0;$i<$number;$i++) {
 9215: 		my $idx=3+$i*6;
 9216: 		push(@idresponses,$entries[$idx]);
 9217: 	    }
 9218: 	    $$responses{$id}=join(',',@idresponses);
 9219: 	}
 9220:     }
 9221:     return ($errormsg,$number);
 9222: }
 9223: 
 9224: sub interwrite_eval {
 9225:     my ($questiontitles,$responses)=@_;
 9226:     my $number=0;
 9227:     my $errormsg='';
 9228:     my $skipline=1;
 9229:     my $questionnumber=0;
 9230:     my %idresponses=();
 9231:     foreach my $line (split(/[\n\r]/,$env{'form.upfile'})) {
 9232:         my %components=&Apache::loncommon::record_sep($line);
 9233:         my @entries=map {$components{$_}} (sort(keys(%components)));
 9234:         if ($entries[1] eq 'Time') { $skipline=0; next; }
 9235:         if ($entries[1] eq 'Response') { $skipline=1; }
 9236:         next if $skipline;
 9237:         if ($entries[0]!=$questionnumber) {
 9238:            $questionnumber=$entries[0];
 9239:            $$questiontitles[$number]=&mt('Question [_1]',$questionnumber);
 9240:            $number++;
 9241:         }
 9242:         my $id=$entries[4];
 9243:         $id=~s/^[\#0]+//;
 9244:         $id=~s/^v\d*\://i;
 9245:         $id=~s/[\-\:]//g;
 9246:         $idresponses{$id}[$number]=$entries[6];
 9247:     }
 9248:     foreach my $id (keys(%idresponses)) {
 9249:        $$responses{$id}=join(',',@{$idresponses{$id}});
 9250:        $$responses{$id}=~s/^\s*\,//;
 9251:     }
 9252:     return ($errormsg,$number);
 9253: }
 9254: 
 9255: sub assign_clicker_grades {
 9256:     my ($r)=@_;
 9257:     my ($symb)=&get_symb($r);
 9258:     if (!$symb) {return '';}
 9259: # See which part we are saving to
 9260:     my $res_error;
 9261:     my ($partlist,$handgrade,$responseType) = &response_type($symb,\$res_error);
 9262:     if ($res_error) {
 9263:         return &navmap_errormsg();
 9264:     }
 9265: # FIXME: This should probably look for the first handgradeable part
 9266:     my $part=$$partlist[0];
 9267: # Start screen output
 9268:     my ($result) = &showResourceInfo($symb,$env{'form.probTitle'});
 9269: 
 9270:     my $heading=&mt('Assigning grades based on clicker file');
 9271:     $result.=(<<ENDHEADER);
 9272: <br /><table width="100%" border="0"><tr><td bgcolor="#777777">
 9273: <table width="100%" border="0"><tr bgcolor="#e6ffff"><td>
 9274: <b>$heading</b></td></tr><tr bgcolor=#ffffe6><td>
 9275: ENDHEADER
 9276: # Get correct result
 9277: # FIXME: Possibly need delimiter other than ":"
 9278:     my @correct=();
 9279:     my $gradingmechanism=$env{'form.gradingmechanism'};
 9280:     my $number=$env{'form.number'};
 9281:     if ($gradingmechanism ne 'attendance') {
 9282:        foreach my $key (keys(%env)) {
 9283:           if ($key=~/^form\.correct\:/) {
 9284:              my @input=split(/\,/,$env{$key});
 9285:              for (my $i=0;$i<=$#input;$i++) {
 9286:                  if (($correct[$i]) && ($input[$i]) &&
 9287:                      ($correct[$i] ne $input[$i])) {
 9288:                     $result.='<br /><span class="LC_warning">'.
 9289:                              &mt('More than one correct result given for question "[_1]": [_2] versus [_3].',
 9290:                                  $env{'form.question:'.$i},$correct[$i],$input[$i]).'</span>';
 9291:                  } elsif ($input[$i]) {
 9292:                     $correct[$i]=$input[$i];
 9293:                  }
 9294:              }
 9295:           }
 9296:        }
 9297:        for (my $i=0;$i<$number;$i++) {
 9298:           if (!$correct[$i]) {
 9299:              $result.='<br /><span class="LC_error">'.
 9300:                       &mt('No correct result given for question "[_1]"!',
 9301:                           $env{'form.question:'.$i}).'</span>';
 9302:           }
 9303:        }
 9304:        $result.='<br />'.&mt("Correct answer: [_1]",join(', ',map { ($_?$_:'-') } @correct));
 9305:     }
 9306: # Start grading
 9307:     my $pcorrect=$env{'form.pcorrect'};
 9308:     my $pincorrect=$env{'form.pincorrect'};
 9309:     my $storecount=0;
 9310:     foreach my $key (keys(%env)) {
 9311:        my $user='';
 9312:        if ($key=~/^form\.student\:(.*)$/) {
 9313:           $user=$1;
 9314:        }
 9315:        if ($key=~/^form\.unknown\:(.*)$/) {
 9316:           my $id=$1;
 9317:           if (($env{'form.uname'.$id}) && ($env{'form.udom'.$id})) {
 9318:              $user=$env{'form.uname'.$id}.':'.$env{'form.udom'.$id};
 9319:           } elsif ($env{'form.multi'.$id}) {
 9320:              $user=$env{'form.multi'.$id};
 9321:           }
 9322:        }
 9323:        if ($user) { 
 9324:           my @answer=split(/\,/,$env{$key});
 9325:           my $sum=0;
 9326:           my $realnumber=$number;
 9327:           for (my $i=0;$i<$number;$i++) {
 9328:              if  ($correct[$i] eq '-') {
 9329:                 $realnumber--;
 9330:              } elsif ($answer[$i]) {
 9331:                 if ($gradingmechanism eq 'attendance') {
 9332:                    $sum+=$pcorrect;
 9333:                 } elsif ($correct[$i] eq '*') {
 9334:                    $sum+=$pcorrect;
 9335:                 } else {
 9336:                    if ($answer[$i] eq $correct[$i]) {
 9337:                       $sum+=$pcorrect;
 9338:                    } else {
 9339:                       $sum+=$pincorrect;
 9340:                    }
 9341:                 }
 9342:              }
 9343:           }
 9344:           my $ave=$sum/(100*$realnumber);
 9345: # Store
 9346:           my ($username,$domain)=split(/\:/,$user);
 9347:           my %grades=();
 9348:           $grades{"resource.$part.solved"}='correct_by_override';
 9349:           $grades{"resource.$part.awarded"}=$ave;
 9350:           $grades{"resource.regrader"}="$env{'user.name'}:$env{'user.domain'}";
 9351:           my $returncode=&Apache::lonnet::cstore(\%grades,$symb,
 9352:                                                  $env{'request.course.id'},
 9353:                                                  $domain,$username);
 9354:           if ($returncode ne 'ok') {
 9355:              $result.="<br /><span class=\"LC_error\">Failed to save student $username:$domain. Message when trying to save was ($returncode)</span>";
 9356:           } else {
 9357:              $storecount++;
 9358:           }
 9359:        }
 9360:     }
 9361: # We are done
 9362:     $result.='<br />'.&mt('Successfully stored grades for [quant,_1,student].',$storecount).
 9363:              '</td></tr></table>'."\n".
 9364:              '</td></tr></table><br /><br />'."\n";
 9365:     return $result.&show_grading_menu_form($symb);
 9366: }
 9367: 
 9368: sub navmap_errormsg {
 9369:     return '<div class="LC_error">'.
 9370:            &mt('An error occurred retrieving information about resources in the course.').'<br />'.
 9371:            &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>').
 9372:            '</div>';
 9373: }
 9374: 
 9375: sub handler {
 9376:     my $request=$_[0];
 9377:     &reset_caches();
 9378:     if ($env{'browser.mathml'}) {
 9379: 	&Apache::loncommon::content_type($request,'text/xml');
 9380:     } else {
 9381: 	&Apache::loncommon::content_type($request,'text/html');
 9382:     }
 9383:     $request->send_http_header;
 9384:     return '' if $request->header_only;
 9385:     &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'});
 9386:     my $symb=&get_symb($request,1);
 9387:     my @commands=&Apache::loncommon::get_env_multiple('form.command');
 9388:     my $command=$commands[0];
 9389: 
 9390:     if ($#commands > 0) {
 9391: 	&Apache::lonnet::logthis("grades got multiple commands ".join(':',@commands));
 9392:     }
 9393: 
 9394:     $ssi_error = 0;
 9395:     my $brcrum = [{href=>"/adm/grades",text=>"Grading"}];
 9396:     $request->print(&Apache::loncommon::start_page('Grading',undef,
 9397:                                           {'bread_crumbs' => $brcrum}));
 9398:     if ($symb eq '' && $command eq '') {
 9399: 	if ($env{'user.adv'}) {
 9400: 	    if (($env{'form.codeone'}) && ($env{'form.codetwo'}) &&
 9401: 		($env{'form.codethree'})) {
 9402: 		my $token=$env{'form.codeone'}.'*'.$env{'form.codetwo'}.'*'.
 9403: 		    $env{'form.codethree'};
 9404: 		my ($tsymb,$tuname,$tudom,$tcrsid)=
 9405: 		    &Apache::lonnet::checkin($token);
 9406: 		if ($tsymb) {
 9407: 		    my ($map,$id,$url)=&Apache::lonnet::decode_symb($tsymb);
 9408: 		    if (&Apache::lonnet::allowed('mgr',$tcrsid)) {
 9409: 			$request->print(&ssi_with_retries('/res/'.$url, $ssi_retries,
 9410: 					  ('grade_username' => $tuname,
 9411: 					   'grade_domain' => $tudom,
 9412: 					   'grade_courseid' => $tcrsid,
 9413: 					   'grade_symb' => $tsymb)));
 9414: 		    } else {
 9415: 			$request->print('<h3>Not authorized: '.$token.'</h3>');
 9416: 		    }
 9417: 		} else {
 9418: 		    $request->print('<h3>Not a valid DocID: '.$token.'</h3>');
 9419: 		}
 9420: 	    } else {
 9421: 		$request->print(&Apache::lonxml::tokeninputfield());
 9422: 	    }
 9423: 	}
 9424:     } else {
 9425: 	&init_perm();
 9426: 	if ($command eq 'submission' && $perm{'vgr'}) {
 9427: 	    ($env{'form.student'} eq '' ? &listStudents($request) : &submission($request,0,0));
 9428: 	} elsif ($command eq 'pickStudentPage' && $perm{'vgr'}) {
 9429: 	    &pickStudentPage($request);
 9430: 	} elsif ($command eq 'displayPage' && $perm{'vgr'}) {
 9431: 	    &displayPage($request);
 9432: 	} elsif ($command eq 'gradeByPage' && $perm{'mgr'}) {
 9433: 	    &updateGradeByPage($request);
 9434: 	} elsif ($command eq 'processGroup' && $perm{'vgr'}) {
 9435: 	    &processGroup($request);
 9436: 	} elsif ($command eq 'gradingmenu' && $perm{'vgr'}) {
 9437: 	    $request->print(&grading_menu($request));
 9438: 	} elsif ($command eq 'submit_options' && $perm{'vgr'}) {
 9439: 	    $request->print(&submit_options($request));
 9440: 	} elsif ($command eq 'viewgrades' && $perm{'vgr'}) {
 9441: 	    $request->print(&viewgrades($request));
 9442: 	} elsif ($command eq 'handgrade' && $perm{'mgr'}) {
 9443: 	    $request->print(&processHandGrade($request));
 9444: 	} elsif ($command eq 'editgrades' && $perm{'mgr'}) {
 9445: 	    $request->print(&editgrades($request));
 9446: 	} elsif ($command eq 'verify' && $perm{'vgr'}) {
 9447: 	    $request->print(&verifyreceipt($request));
 9448:         } elsif ($command eq 'processclicker' && $perm{'mgr'}) {
 9449:             $request->print(&process_clicker($request));
 9450:         } elsif ($command eq 'processclickerfile' && $perm{'mgr'}) {
 9451:             $request->print(&process_clicker_file($request));
 9452:         } elsif ($command eq 'assignclickergrades' && $perm{'mgr'}) {
 9453:             $request->print(&assign_clicker_grades($request));
 9454: 	} elsif ($command eq 'csvform' && $perm{'mgr'}) {
 9455: 	    $request->print(&upcsvScores_form($request));
 9456: 	} elsif ($command eq 'csvupload' && $perm{'mgr'}) {
 9457: 	    $request->print(&csvupload($request));
 9458: 	} elsif ($command eq 'csvuploadmap' && $perm{'mgr'} ) {
 9459: 	    $request->print(&csvuploadmap($request));
 9460: 	} elsif ($command eq 'csvuploadoptions' && $perm{'mgr'}) {
 9461: 	    if ($env{'form.associate'} ne 'Reverse Association') {
 9462: 		$request->print(&csvuploadoptions($request));
 9463: 	    } else {
 9464: 		if ( $env{'form.upfile_associate'} ne 'reverse' ) {
 9465: 		    $env{'form.upfile_associate'} = 'reverse';
 9466: 		} else {
 9467: 		    $env{'form.upfile_associate'} = 'forward';
 9468: 		}
 9469: 		$request->print(&csvuploadmap($request));
 9470: 	    }
 9471: 	} elsif ($command eq 'csvuploadassign' && $perm{'mgr'} ) {
 9472: 	    $request->print(&csvuploadassign($request));
 9473: 	} elsif ($command eq 'scantron_selectphase' && $perm{'mgr'}) {
 9474: 	    $request->print(&scantron_selectphase($request));
 9475:  	} elsif ($command eq 'scantron_warning' && $perm{'mgr'}) {
 9476:  	    $request->print(&scantron_do_warning($request));
 9477: 	} elsif ($command eq 'scantron_validate' && $perm{'mgr'}) {
 9478: 	    $request->print(&scantron_validate_file($request));
 9479: 	} elsif ($command eq 'scantron_process' && $perm{'mgr'}) {
 9480: 	    $request->print(&scantron_process_students($request));
 9481:  	} elsif ($command eq 'scantronupload' && 
 9482:  		 (&Apache::lonnet::allowed('usc',$env{'request.role.domain'})||
 9483: 		  &Apache::lonnet::allowed('usc',$env{'request.course.id'}))) {
 9484:  	    $request->print(&scantron_upload_scantron_data($request)); 
 9485:  	} elsif ($command eq 'scantronupload_save' &&
 9486:  		 (&Apache::lonnet::allowed('usc',$env{'request.role.domain'})||
 9487: 		  &Apache::lonnet::allowed('usc',$env{'request.course.id'}))) {
 9488:  	    $request->print(&scantron_upload_scantron_data_save($request));
 9489:  	} elsif ($command eq 'scantron_download' &&
 9490: 		 &Apache::lonnet::allowed('usc',$env{'request.course.id'})) {
 9491:  	    $request->print(&scantron_download_scantron_data($request));
 9492:         } elsif ($command eq 'checksubmissions' && $perm{'vgr'}) {
 9493:             $request->print(&checkscantron_results($request));     
 9494: 	} elsif ($command) {
 9495: 	    $request->print('<p class="LC_error">'.&mt('Access Denied ([_1])',$command).'</p>');
 9496: 	}
 9497:     }
 9498:     if ($ssi_error) {
 9499: 	&ssi_print_error($request);
 9500:     }
 9501:     $request->print(&Apache::loncommon::end_page());
 9502:     &reset_caches();
 9503:     return '';
 9504: }
 9505: 
 9506: 1;
 9507: 
 9508: __END__;
 9509: 
 9510: 
 9511: =head1 NAME
 9512: 
 9513: Apache::grades
 9514: 
 9515: =head1 SYNOPSIS
 9516: 
 9517: Handles the viewing of grades.
 9518: 
 9519: This is part of the LearningOnline Network with CAPA project
 9520: described at http://www.lon-capa.org.
 9521: 
 9522: =head1 OVERVIEW
 9523: 
 9524: Do an ssi with retries:
 9525: While I'd love to factor out this with the vesrion in lonprintout,
 9526: 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
 9527: I'm not quite ready to invent (e.g. an ssi_with_retry object).
 9528: 
 9529: At least the logic that drives this has been pulled out into loncommon.
 9530: 
 9531: 
 9532: 
 9533: ssi_with_retries - Does the server side include of a resource.
 9534:                      if the ssi call returns an error we'll retry it up to
 9535:                      the number of times requested by the caller.
 9536:                      If we still have a proble, no text is appended to the
 9537:                      output and we set some global variables.
 9538:                      to indicate to the caller an SSI error occurred.  
 9539:                      All of this is supposed to deal with the issues described
 9540:                      in LonCAPA BZ 5631 see:
 9541:                      http://bugs.lon-capa.org/show_bug.cgi?id=5631
 9542:                      by informing the user that this happened.
 9543: 
 9544: Parameters:
 9545:   resource   - The resource to include.  This is passed directly, without
 9546:                interpretation to lonnet::ssi.
 9547:   form       - The form hash parameters that guide the interpretation of the resource
 9548:                
 9549:   retries    - Number of retries allowed before giving up completely.
 9550: Returns:
 9551:   On success, returns the rendered resource identified by the resource parameter.
 9552: Side Effects:
 9553:   The following global variables can be set:
 9554:    ssi_error                - If an unrecoverable error occurred this becomes true.
 9555:                               It is up to the caller to initialize this to false
 9556:                               if desired.
 9557:    ssi_error_resource  - If an unrecoverable error occurred, this is the value
 9558:                               of the resource that could not be rendered by the ssi
 9559:                               call.
 9560:    ssi_error_message   - The error string fetched from the ssi response
 9561:                               in the event of an error.
 9562: 
 9563: 
 9564: =head1 HANDLER SUBROUTINE
 9565: 
 9566: ssi_with_retries()
 9567: 
 9568: =head1 SUBROUTINES
 9569: 
 9570: =over
 9571: 
 9572: =item scantron_get_correction() : 
 9573: 
 9574:    Builds the interface screen to interact with the operator to fix a
 9575:    specific error condition in a specific scanline
 9576: 
 9577:  Arguments:
 9578:     $r           - Apache request object
 9579:     $i           - number of the current scanline
 9580:     $scan_record - hash ref as returned from &scantron_parse_scanline()
 9581:     $scan_config - hash ref as returned from &get_scantron_config()
 9582:     $line        - full contents of the current scanline
 9583:     $error       - error condition, valid values are
 9584:                    'incorrectCODE', 'duplicateCODE',
 9585:                    'doublebubble', 'missingbubble',
 9586:                    'duplicateID', 'incorrectID'
 9587:     $arg         - extra information needed
 9588:        For errors:
 9589:          - duplicateID   - paper number that this studentID was seen before on
 9590:          - duplicateCODE - array ref of the paper numbers this CODE was
 9591:                            seen on before
 9592:          - incorrectCODE - current incorrect CODE 
 9593:          - doublebubble  - array ref of the bubble lines that have double
 9594:                            bubble errors
 9595:          - missingbubble - array ref of the bubble lines that have missing
 9596:                            bubble errors
 9597: 
 9598: =item  scantron_get_maxbubble() : 
 9599: 
 9600:    Arguments:
 9601:        $nav_error  - Reference to scalar which is a flag to indicate a
 9602:                       failure to retrieve a navmap object.
 9603:        if $nav_error is set to 1 by scantron_get_maxbubble(), the 
 9604:        calling routine should trap the error condition and display the warning
 9605:        found in &navmap_errormsg().
 9606: 
 9607:    Returns the maximum number of bubble lines that are expected to
 9608:    occur. Does this by walking the selected sequence rendering the
 9609:    resource and then checking &Apache::lonxml::get_problem_counter()
 9610:    for what the current value of the problem counter is.
 9611: 
 9612:    Caches the results to $env{'form.scantron_maxbubble'},
 9613:    $env{'form.scantron.bubble_lines.n'}, 
 9614:    $env{'form.scantron.first_bubble_line.n'} and
 9615:    $env{"form.scantron.sub_bubblelines.n"}
 9616:    which are the total number of bubble, lines, the number of bubble
 9617:    lines for response n and number of the first bubble line for response n,
 9618:    and a comma separated list of numbers of bubble lines for sub-questions
 9619:    (for optionresponse, matchresponse, and rankresponse items), for response n.  
 9620: 
 9621: 
 9622: =item  scantron_validate_missingbubbles() : 
 9623: 
 9624:    Validates all scanlines in the selected file to not have any
 9625:     answers that don't have bubbles that have not been verified
 9626:     to be bubble free.
 9627: 
 9628: =item  scantron_process_students() : 
 9629: 
 9630:    Routine that does the actual grading of the bubble sheet information.
 9631: 
 9632:    The parsed scanline hash is added to %env 
 9633: 
 9634:    Then foreach unskipped scanline it does an &Apache::lonnet::ssi()
 9635:    foreach resource , with the form data of
 9636: 
 9637: 	'submitted'     =>'scantron' 
 9638: 	'grade_target'  =>'grade',
 9639: 	'grade_username'=> username of student
 9640: 	'grade_domain'  => domain of student
 9641: 	'grade_courseid'=> of course
 9642: 	'grade_symb'    => symb of resource to grade
 9643: 
 9644:     This triggers a grading pass. The problem grading code takes care
 9645:     of converting the bubbled letter information (now in %env) into a
 9646:     valid submission.
 9647: 
 9648: =item  scantron_upload_scantron_data() :
 9649: 
 9650:     Creates the screen for adding a new bubble sheet data file to a course.
 9651: 
 9652: =item  scantron_upload_scantron_data_save() : 
 9653: 
 9654:    Adds a provided bubble information data file to the course if user
 9655:    has the correct privileges to do so. 
 9656: 
 9657: =item  valid_file() :
 9658: 
 9659:    Validates that the requested bubble data file exists in the course.
 9660: 
 9661: =item  scantron_download_scantron_data() : 
 9662: 
 9663:    Shows a list of the three internal files (original, corrected,
 9664:    skipped) for a specific bubble sheet data file that exists in the
 9665:    course.
 9666: 
 9667: =item  scantron_validate_ID() : 
 9668: 
 9669:    Validates all scanlines in the selected file to not have any
 9670:    invalid or underspecified student/employee IDs
 9671: 
 9672: =item navmap_errormsg() :
 9673: 
 9674:    Returns HTML mark-up inside a <div></div> with a link to re-initialize the course.
 9675:    Should be called whenever the request to instantiate a navmap object fails.  
 9676: 
 9677: =back
 9678: 
 9679: =cut

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>