Annotation of loncom/homework/grades.pm, revision 1.579

1.17      albertel    1: # The LearningOnline Network with CAPA
1.13      albertel    2: # The LON-CAPA Grading handler
1.17      albertel    3: #
1.579   ! raeburn     4: # $Id: grades.pm,v 1.578 2009/06/06 19:23:30 raeburn Exp $
1.17      albertel    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: #
1.1       albertel   28: 
1.529     jms        29: 
                     30: 
1.1       albertel   31: package Apache::grades;
                     32: use strict;
                     33: use Apache::style;
                     34: use Apache::lonxml;
                     35: use Apache::lonnet;
1.3       albertel   36: use Apache::loncommon;
1.112     ng         37: use Apache::lonhtmlcommon;
1.68      ng         38: use Apache::lonnavmaps;
1.1       albertel   39: use Apache::lonhomework;
1.456     banghart   40: use Apache::lonpickcode;
1.55      matthew    41: use Apache::loncoursedata;
1.362     albertel   42: use Apache::lonmsg();
1.1       albertel   43: use Apache::Constants qw(:common);
1.167     sakharuk   44: use Apache::lonlocal;
1.386     raeburn    45: use Apache::lonenc;
1.170     albertel   46: use String::Similarity;
1.359     www        47: use LONCAPA;
                     48: 
1.315     bowersj2   49: use POSIX qw(floor);
1.87      www        50: 
1.435     foxr       51: 
1.513     foxr       52: 
1.435     foxr       53: my %perm=();
1.447     foxr       54: 
1.513     foxr       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) = @_;
1.516     raeburn    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;
1.513     foxr       95: }
                     96: 
1.44      ng         97: #
1.146     albertel   98: # --- Retrieve the parts from the metadata file.---
1.44      ng         99: sub getpartlist {
1.324     albertel  100:     my ($symb) = @_;
1.439     albertel  101: 
                    102:     my $navmap   = Apache::lonnavmaps::navmap->new();
                    103:     my $res      = $navmap->getBySymb($symb);
                    104:     my $partlist = $res->parts();
                    105:     my $url      = $res->src();
                    106:     my @metakeys = split(/,/,&Apache::lonnet::metadata($url,'keys'));
                    107: 
1.146     albertel  108:     my @stores;
1.439     albertel  109:     foreach my $part (@{ $partlist }) {
1.146     albertel  110: 	foreach my $key (@metakeys) {
                    111: 	    if ($key =~ m/^stores_\Q$part\E_/) { push(@stores,$key); }
                    112: 	}
                    113:     }
                    114:     return @stores;
1.2       albertel  115: }
                    116: 
1.44      ng        117: # --- Get the symbolic name of a problem and the url
1.324     albertel  118: sub get_symb {
1.173     albertel  119:     my ($request,$silent) = @_;
1.257     albertel  120:     (my $url=$env{'form.url'}) =~ s-^http://($ENV{'SERVER_NAME'}|$ENV{'HTTP_HOST'})--;
                    121:     my $symb=($env{'form.symb'} ne '' ? $env{'form.symb'} : (&Apache::lonnet::symbread($url)));
1.173     albertel  122:     if ($symb eq '') { 
                    123: 	if (!$silent) {
                    124: 	    $request->print("Unable to handle ambiguous references:$url:.");
                    125: 	    return ();
                    126: 	}
                    127:     }
1.418     albertel  128:     &Apache::lonenc::check_decrypt(\$symb);
1.324     albertel  129:     return ($symb);
1.32      ng        130: }
                    131: 
1.129     ng        132: #--- Format fullname, username:domain if different for display
                    133: #--- Use anywhere where the student names are listed
                    134: sub nameUserString {
                    135:     my ($type,$fullname,$uname,$udom) = @_;
                    136:     if ($type eq 'header') {
1.485     albertel  137: 	return '<b>&nbsp;'.&mt('Fullname').'&nbsp;</b><span class="LC_internal_info">('.&mt('Username').')</span>';
1.129     ng        138:     } else {
1.398     albertel  139: 	return '&nbsp;'.$fullname.'<span class="LC_internal_info">&nbsp;('.$uname.
                    140: 	    ($env{'user.domain'} eq $udom ? '' : ' ('.$udom.')').')</span>';
1.129     ng        141:     }
                    142: }
                    143: 
1.44      ng        144: #--- Get the partlist and the response type for a given problem. ---
                    145: #--- Indicate if a response type is coded handgraded or not. ---
1.39      ng        146: sub response_type {
1.324     albertel  147:     my ($symb) = shift;
1.377     albertel  148: 
                    149:     my $navmap = Apache::lonnavmaps::navmap->new();
                    150:     my $res = $navmap->getBySymb($symb);
                    151:     my $partlist = $res->parts();
1.392     albertel  152:     my %vPart = 
                    153: 	map { $_ => 1 } (&Apache::loncommon::get_env_multiple('form.vPart'));
1.377     albertel  154:     my (%response_types,%handgrade);
                    155:     foreach my $part (@{ $partlist }) {
1.392     albertel  156: 	next if (%vPart && !exists($vPart{$part}));
                    157: 
1.377     albertel  158: 	my @types = $res->responseType($part);
                    159: 	my @ids = $res->responseIds($part);
                    160: 	for (my $i=0; $i < scalar(@ids); $i++) {
                    161: 	    $response_types{$part}{$ids[$i]} = $types[$i];
                    162: 	    $handgrade{$part.'_'.$ids[$i]} = 
                    163: 		&Apache::lonnet::EXT('resource.'.$part.'_'.$ids[$i].
                    164: 				     '.handgrade',$symb);
1.41      ng        165: 	}
                    166:     }
1.377     albertel  167:     return ($partlist,\%handgrade,\%response_types);
1.39      ng        168: }
                    169: 
1.375     albertel  170: sub flatten_responseType {
                    171:     my ($responseType) = @_;
                    172:     my @part_response_id =
                    173: 	map { 
                    174: 	    my $part = $_;
                    175: 	    map {
                    176: 		[$part,$_]
                    177: 		} sort(keys(%{ $responseType->{$part} }));
                    178: 	} sort(keys(%$responseType));
                    179:     return @part_response_id;
                    180: }
                    181: 
1.207     albertel  182: sub get_display_part {
1.324     albertel  183:     my ($partID,$symb)=@_;
1.207     albertel  184:     my $display=&Apache::lonnet::EXT('resource.'.$partID.'.display',$symb);
                    185:     if (defined($display) and $display ne '') {
1.577     bisitz    186:         $display.= ' (<span class="LC_internal_info">'
                    187:                   .&mt('Part ID: [_1]',$partID).'</span>)';
1.207     albertel  188:     } else {
                    189: 	$display=$partID;
                    190:     }
                    191:     return $display;
                    192: }
1.269     raeburn   193: 
1.118     ng        194: #--- Show resource title
                    195: #--- and parts and response type
                    196: sub showResourceInfo {
1.324     albertel  197:     my ($symb,$probTitle,$checkboxes) = @_;
1.154     albertel  198:     my $col=3;
                    199:     if ($checkboxes) { $col=4; }
1.398     albertel  200:     my $result = '<h3>'.&mt('Current Resource').': '.$probTitle.'</h3>'."\n";
                    201:     $result .='<table border="0">';
1.324     albertel  202:     my ($partlist,$handgrade,$responseType) = &response_type($symb);
1.126     ng        203:     my %resptype = ();
1.122     ng        204:     my $hdgrade='no';
1.154     albertel  205:     my %partsseen;
1.524     raeburn   206:     foreach my $partID (sort(keys(%$responseType))) {
                    207: 	foreach my $resID (sort(keys(%{ $responseType->{$partID} }))) {
1.375     albertel  208: 	    my $handgrade=$$handgrade{$partID.'_'.$resID};
                    209: 	    my $responsetype = $responseType->{$partID}->{$resID};
                    210: 	    $hdgrade = $handgrade if ($handgrade eq 'yes');
                    211: 	    $result.='<tr>';
                    212: 	    if ($checkboxes) {
                    213: 		if (exists($partsseen{$partID})) {
                    214: 		    $result.="<td>&nbsp;</td>";
                    215: 		} else {
1.401     albertel  216: 		    $result.="<td><input type='checkbox' name='vPart' value='$partID' checked='checked' /></td>";
1.375     albertel  217: 		}
                    218: 		$partsseen{$partID}=1;
1.154     albertel  219: 	    }
1.375     albertel  220: 	    my $display_part=&get_display_part($partID,$symb);
1.577     bisitz    221:             $result.='<td><b>'.&mt('Part: [_1]',$display_part).'</b>'.
1.539     riegler   222:                 ' <span class="LC_internal_info">'.$resID.'</span></td>'.
1.577     bisitz    223:                 '<td><b>'.&mt('Type: [_1]',$responsetype).'</b></td></tr>';
1.485     albertel  224: #	    '<td>'.&mt('<b>Handgrade: </b>[_1]',$handgrade).'</td></tr>';
1.154     albertel  225: 	}
1.118     ng        226:     }
                    227:     $result.='</table>'."\n";
1.147     albertel  228:     return $result,$responseType,$hdgrade,$partlist,$handgrade;
1.118     ng        229: }
                    230: 
1.434     albertel  231: sub reset_caches {
                    232:     &reset_analyze_cache();
                    233:     &reset_perm();
                    234: }
                    235: 
                    236: {
                    237:     my %analyze_cache;
1.557     raeburn   238:     my %analyze_cache_formkeys;
1.148     albertel  239: 
1.434     albertel  240:     sub reset_analyze_cache {
                    241: 	undef(%analyze_cache);
1.557     raeburn   242:         undef(%analyze_cache_formkeys);
1.434     albertel  243:     }
                    244: 
                    245:     sub get_analyze {
1.557     raeburn   246: 	my ($symb,$uname,$udom,$no_increment,$add_to_hash)=@_;
1.434     albertel  247: 	my $key = "$symb\0$uname\0$udom";
1.557     raeburn   248: 	if (exists($analyze_cache{$key})) {
                    249:             my $getupdate = 0;
                    250:             if (ref($add_to_hash) eq 'HASH') {
                    251:                 foreach my $item (keys(%{$add_to_hash})) {
                    252:                     if (ref($analyze_cache_formkeys{$key}) eq 'HASH') {
                    253:                         if (!exists($analyze_cache_formkeys{$key}{$item})) {
                    254:                             $getupdate = 1;
                    255:                             last;
                    256:                         }
                    257:                     } else {
                    258:                         $getupdate = 1;
                    259:                     }
                    260:                 }
                    261:             }
                    262:             if (!$getupdate) {
                    263:                 return $analyze_cache{$key};
                    264:             }
                    265:         }
1.434     albertel  266: 
                    267: 	my (undef,undef,$url)=&Apache::lonnet::decode_symb($symb);
                    268: 	$url=&Apache::lonnet::clutter($url);
1.557     raeburn   269:         my %form = ('grade_target'      => 'analyze',
                    270:                     'grade_domain'      => $udom,
                    271:                     'grade_symb'        => $symb,
                    272:                     'grade_courseid'    =>  $env{'request.course.id'},
                    273:                     'grade_username'    => $uname,
                    274:                     'grade_noincrement' => $no_increment);
                    275:         if (ref($add_to_hash)) {
                    276:             %form = (%form,%{$add_to_hash});
                    277:         } 
                    278: 	my $subresult=&ssi_with_retries($url, $ssi_retries,%form);
1.434     albertel  279: 	(undef,$subresult)=split(/_HASH_REF__/,$subresult,2);
                    280: 	my %analyze=&Apache::lonnet::str2hash($subresult);
1.557     raeburn   281:         if (ref($add_to_hash) eq 'HASH') {
                    282:             $analyze_cache_formkeys{$key} = $add_to_hash;
                    283:         } else {
                    284:             $analyze_cache_formkeys{$key} = {};
                    285:         }
1.434     albertel  286: 	return $analyze_cache{$key} = \%analyze;
                    287:     }
                    288: 
                    289:     sub get_order {
1.525     raeburn   290: 	my ($partid,$respid,$symb,$uname,$udom,$no_increment)=@_;
                    291: 	my $analyze = &get_analyze($symb,$uname,$udom,$no_increment);
1.434     albertel  292: 	return $analyze->{"$partid.$respid.shown"};
                    293:     }
                    294: 
                    295:     sub get_radiobutton_correct_foil {
                    296: 	my ($partid,$respid,$symb,$uname,$udom)=@_;
                    297: 	my $analyze = &get_analyze($symb,$uname,$udom);
1.555     raeburn   298:         my $foils = &get_order($partid,$respid,$symb,$uname,$udom);
                    299:         if (ref($foils) eq 'ARRAY') {
                    300: 	    foreach my $foil (@{$foils}) {
                    301: 	        if ($analyze->{"$partid.$respid.foil.value.$foil"} eq 'true') {
                    302: 		    return $foil;
                    303: 	        }
1.434     albertel  304: 	    }
                    305: 	}
                    306:     }
1.554     raeburn   307: 
                    308:     sub scantron_partids_tograde {
1.557     raeburn   309:         my ($resource,$cid,$uname,$udom,$check_for_randomlist) = @_;
1.554     raeburn   310:         my (%analysis,@parts);
                    311:         if (ref($resource)) {
                    312:             my $symb = $resource->symb();
1.557     raeburn   313:             my $add_to_form;
                    314:             if ($check_for_randomlist) {
                    315:                 $add_to_form = { 'check_parts_withrandomlist' => 1,};
                    316:             }
                    317:             my $analyze = &get_analyze($symb,$uname,$udom,undef,$add_to_form);
1.554     raeburn   318:             if (ref($analyze) eq 'HASH') {
                    319:                 %analysis = %{$analyze};
                    320:             }
                    321:             if (ref($analysis{'parts'}) eq 'ARRAY') {
                    322:                 foreach my $part (@{$analysis{'parts'}}) {
                    323:                     my ($id,$respid) = split(/\./,$part);
                    324:                     if (!&Apache::loncommon::check_if_partid_hidden($id,$symb,$udom,$uname)) {
                    325:                         push(@parts,$part);
                    326:                     }
                    327:                 }
                    328:             }
                    329:         }
                    330:         return (\%analysis,\@parts);
                    331:     }
                    332: 
1.148     albertel  333: }
1.434     albertel  334: 
1.118     ng        335: #--- Clean response type for display
1.335     albertel  336: #--- Currently filters option/rank/radiobutton/match/essay/Task
                    337: #        response types only.
1.118     ng        338: sub cleanRecord {
1.336     albertel  339:     my ($answer,$response,$symb,$partid,$respid,$record,$order,$version,
                    340: 	$uname,$udom) = @_;
1.398     albertel  341:     my $grayFont = '<span class="LC_internal_info">';
1.148     albertel  342:     if ($response =~ /^(option|rank)$/) {
                    343: 	my %answer=&Apache::lonnet::str2hash($answer);
                    344: 	my %grading=&Apache::lonnet::str2hash($record->{$version."resource.$partid.$respid.submissiongrading"});
                    345: 	my ($toprow,$bottomrow);
                    346: 	foreach my $foil (@$order) {
                    347: 	    if ($grading{$foil} == 1) {
                    348: 		$toprow.='<td><b>'.$answer{$foil}.'&nbsp;</b></td>';
                    349: 	    } else {
                    350: 		$toprow.='<td><i>'.$answer{$foil}.'&nbsp;</i></td>';
                    351: 	    }
1.398     albertel  352: 	    $bottomrow.='<td>'.$grayFont.$foil.'</span>&nbsp;</td>';
1.148     albertel  353: 	}
                    354: 	return '<blockquote><table border="1">'.
1.466     albertel  355: 	    '<tr valign="top"><td>'.&mt('Answer').'</td>'.$toprow.'</tr>'.
                    356: 	    '<tr valign="top"><td>'.$grayFont.&mt('Option ID').'</span></td>'.
1.148     albertel  357: 	    $grayFont.$bottomrow.'</tr>'.'</table></blockquote>';
                    358:     } elsif ($response eq 'match') {
                    359: 	my %answer=&Apache::lonnet::str2hash($answer);
                    360: 	my %grading=&Apache::lonnet::str2hash($record->{$version."resource.$partid.$respid.submissiongrading"});
                    361: 	my @items=&Apache::lonnet::str2array($record->{$version."resource.$partid.$respid.submissionitems"});
                    362: 	my ($toprow,$middlerow,$bottomrow);
                    363: 	foreach my $foil (@$order) {
                    364: 	    my $item=shift(@items);
                    365: 	    if ($grading{$foil} == 1) {
                    366: 		$toprow.='<td><b>'.$item.'&nbsp;</b></td>';
1.398     albertel  367: 		$middlerow.='<td><b>'.$grayFont.$answer{$foil}.'&nbsp;</span></b></td>';
1.148     albertel  368: 	    } else {
                    369: 		$toprow.='<td><i>'.$item.'&nbsp;</i></td>';
1.398     albertel  370: 		$middlerow.='<td><i>'.$grayFont.$answer{$foil}.'&nbsp;</span></i></td>';
1.148     albertel  371: 	    }
1.398     albertel  372: 	    $bottomrow.='<td>'.$grayFont.$foil.'</span>&nbsp;</td>';
1.118     ng        373: 	}
1.126     ng        374: 	return '<blockquote><table border="1">'.
1.466     albertel  375: 	    '<tr valign="top"><td>'.&mt('Answer').'</td>'.$toprow.'</tr>'.
                    376: 	    '<tr valign="top"><td>'.$grayFont.&mt('Item ID').'</span></td>'.
1.148     albertel  377: 	    $middlerow.'</tr>'.
1.466     albertel  378: 	    '<tr valign="top"><td>'.$grayFont.&mt('Option ID').'</span></td>'.
1.148     albertel  379: 	    $bottomrow.'</tr>'.'</table></blockquote>';
                    380:     } elsif ($response eq 'radiobutton') {
                    381: 	my %answer=&Apache::lonnet::str2hash($answer);
                    382: 	my ($toprow,$bottomrow);
1.434     albertel  383: 	my $correct = 
                    384: 	    &get_radiobutton_correct_foil($partid,$respid,$symb,$uname,$udom);
                    385: 	foreach my $foil (@$order) {
1.148     albertel  386: 	    if (exists($answer{$foil})) {
1.434     albertel  387: 		if ($foil eq $correct) {
1.466     albertel  388: 		    $toprow.='<td><b>'.&mt('true').'</b></td>';
1.148     albertel  389: 		} else {
1.466     albertel  390: 		    $toprow.='<td><i>'.&mt('true').'</i></td>';
1.148     albertel  391: 		}
                    392: 	    } else {
1.466     albertel  393: 		$toprow.='<td>'.&mt('false').'</td>';
1.148     albertel  394: 	    }
1.398     albertel  395: 	    $bottomrow.='<td>'.$grayFont.$foil.'</span>&nbsp;</td>';
1.148     albertel  396: 	}
                    397: 	return '<blockquote><table border="1">'.
1.466     albertel  398: 	    '<tr valign="top"><td>'.&mt('Answer').'</td>'.$toprow.'</tr>'.
                    399: 	    '<tr valign="top"><td>'.$grayFont.&mt('Option ID').'</span></td>'.
1.148     albertel  400: 	    $grayFont.$bottomrow.'</tr>'.'</table></blockquote>';
                    401:     } elsif ($response eq 'essay') {
1.257     albertel  402: 	if (! exists ($env{'form.'.$symb})) {
1.122     ng        403: 	    my (%keyhash) = &Apache::lonnet::dump('nohist_handgrade',
1.257     albertel  404: 						  $env{'course.'.$env{'request.course.id'}.'.domain'},
                    405: 						  $env{'course.'.$env{'request.course.id'}.'.num'});
1.122     ng        406: 
1.257     albertel  407: 	    my $loginuser = $env{'user.name'}.':'.$env{'user.domain'};
                    408: 	    $env{'form.keywords'} = $keyhash{$symb.'_keywords'} ne '' ? $keyhash{$symb.'_keywords'} : '';
                    409: 	    $env{'form.kwclr'}    = $keyhash{$loginuser.'_kwclr'} ne '' ? $keyhash{$loginuser.'_kwclr'} : 'red';
                    410: 	    $env{'form.kwsize'}   = $keyhash{$loginuser.'_kwsize'} ne '' ? $keyhash{$loginuser.'_kwsize'} : '0';
                    411: 	    $env{'form.kwstyle'}  = $keyhash{$loginuser.'_kwstyle'} ne '' ? $keyhash{$loginuser.'_kwstyle'} : '';
                    412: 	    $env{'form.'.$symb} = 1; # so that we don't have to read it from disk for multiple sub of the same prob.
1.122     ng        413: 	}
1.166     albertel  414: 	$answer =~ s-\n-<br />-g;
                    415: 	return '<br /><br /><blockquote><tt>'.&keywords_highlight($answer).'</tt></blockquote>';
1.268     albertel  416:     } elsif ( $response eq 'organic') {
                    417: 	my $result='Smile representation: "<tt>'.$answer.'</tt>"';
                    418: 	my $jme=$record->{$version."resource.$partid.$respid.molecule"};
                    419: 	$result.=&Apache::chemresponse::jme_img($jme,$answer,400);
                    420: 	return $result;
1.335     albertel  421:     } elsif ( $response eq 'Task') {
                    422: 	if ( $answer eq 'SUBMITTED') {
                    423: 	    my $files = $record->{$version."resource.$respid.$partid.bridgetask.portfiles"};
1.336     albertel  424: 	    my $result = &Apache::bridgetask::file_list($files,$uname,$udom);
1.335     albertel  425: 	    return $result;
                    426: 	} elsif ( grep(/^\Q$version\E.*?\.instance$/, keys(%{$record})) ) {
                    427: 	    my @matches = grep(/^\Q$version\E.*?\.instance$/,
                    428: 			       keys(%{$record}));
                    429: 	    return join('<br />',($version,@matches));
                    430: 			       
                    431: 			       
                    432: 	} else {
                    433: 	    my $result =
                    434: 		'<p>'
                    435: 		.&mt('Overall result: [_1]',
                    436: 		     $record->{$version."resource.$respid.$partid.status"})
                    437: 		.'</p>';
                    438: 	    
                    439: 	    $result .= '<ul>';
                    440: 	    my @grade = grep(/^\Q${version}resource.$respid.$partid.\E[^.]*[.]status$/,
                    441: 			     keys(%{$record}));
                    442: 	    foreach my $grade (sort(@grade)) {
                    443: 		my ($dim) = ($grade =~/[.]([^.]+)[.]status$/);
                    444: 		$result.= '<li>'.&mt("Dimension: [_1], status [_2] ",
                    445: 				     $dim, $record->{$grade}).
                    446: 			  '</li>';
                    447: 	    }
                    448: 	    $result.='</ul>';
                    449: 	    return $result;
                    450: 	}
1.440     albertel  451:     } elsif ( $response =~ m/(?:numerical|formula)/) {
                    452: 	$answer = 
                    453: 	    &Apache::loncommon::format_previous_attempt_value('submission',
                    454: 							      $answer);
1.122     ng        455:     }
1.118     ng        456:     return $answer;
                    457: }
                    458: 
                    459: #-- A couple of common js functions
                    460: sub commonJSfunctions {
                    461:     my $request = shift;
                    462:     $request->print(<<COMMONJSFUNCTIONS);
                    463: <script type="text/javascript" language="javascript">
                    464:     function radioSelection(radioButton) {
                    465: 	var selection=null;
                    466: 	if (radioButton.length > 1) {
                    467: 	    for (var i=0; i<radioButton.length; i++) {
                    468: 		if (radioButton[i].checked) {
                    469: 		    return radioButton[i].value;
                    470: 		}
                    471: 	    }
                    472: 	} else {
                    473: 	    if (radioButton.checked) return radioButton.value;
                    474: 	}
                    475: 	return selection;
                    476:     }
                    477: 
                    478:     function pullDownSelection(selectOne) {
                    479: 	var selection="";
                    480: 	if (selectOne.length > 1) {
                    481: 	    for (var i=0; i<selectOne.length; i++) {
                    482: 		if (selectOne[i].selected) {
                    483: 		    return selectOne[i].value;
                    484: 		}
                    485: 	    }
                    486: 	} else {
1.138     albertel  487:             // only one value it must be the selected one
                    488: 	    return selectOne.value;
1.118     ng        489: 	}
                    490:     }
                    491: </script>
                    492: COMMONJSFUNCTIONS
                    493: }
                    494: 
1.44      ng        495: #--- Dumps the class list with usernames,list of sections,
                    496: #--- section, ids and fullnames for each user.
                    497: sub getclasslist {
1.449     banghart  498:     my ($getsec,$filterlist,$getgroup) = @_;
1.291     albertel  499:     my @getsec;
1.450     banghart  500:     my @getgroup;
1.442     banghart  501:     my $stu_status = join(':',&Apache::loncommon::get_env_multiple('form.Status'));
1.291     albertel  502:     if (!ref($getsec)) {
                    503: 	if ($getsec ne '' && $getsec ne 'all') {
                    504: 	    @getsec=($getsec);
                    505: 	}
                    506:     } else {
                    507: 	@getsec=@{$getsec};
                    508:     }
                    509:     if (grep(/^all$/,@getsec)) { undef(@getsec); }
1.450     banghart  510:     if (!ref($getgroup)) {
                    511: 	if ($getgroup ne '' && $getgroup ne 'all') {
                    512: 	    @getgroup=($getgroup);
                    513: 	}
                    514:     } else {
                    515: 	@getgroup=@{$getgroup};
                    516:     }
                    517:     if (grep(/^all$/,@getgroup)) { undef(@getgroup); }
1.291     albertel  518: 
1.449     banghart  519:     my ($classlist,$keylist)=&Apache::loncoursedata::get_classlist();
1.49      albertel  520:     # Bail out if we were unable to get the classlist
1.56      matthew   521:     return if (! defined($classlist));
1.449     banghart  522:     &Apache::loncoursedata::get_group_memberships($classlist,$keylist);
1.56      matthew   523:     #
                    524:     my %sections;
                    525:     my %fullnames;
1.205     matthew   526:     foreach my $student (keys(%$classlist)) {
                    527:         my $end      = 
                    528:             $classlist->{$student}->[&Apache::loncoursedata::CL_END()];
                    529:         my $start    = 
                    530:             $classlist->{$student}->[&Apache::loncoursedata::CL_START()];
                    531:         my $id       = 
                    532:             $classlist->{$student}->[&Apache::loncoursedata::CL_ID()];
                    533:         my $section  = 
                    534:             $classlist->{$student}->[&Apache::loncoursedata::CL_SECTION()];
                    535:         my $fullname = 
                    536:             $classlist->{$student}->[&Apache::loncoursedata::CL_FULLNAME()];
                    537:         my $status   = 
                    538:             $classlist->{$student}->[&Apache::loncoursedata::CL_STATUS()];
1.449     banghart  539:         my $group   = 
                    540:             $classlist->{$student}->[&Apache::loncoursedata::CL_GROUP()];
1.76      ng        541: 	# filter students according to status selected
1.442     banghart  542: 	if ($filterlist && (!($stu_status =~ /Any/))) {
                    543: 	    if (!($stu_status =~ $status)) {
1.450     banghart  544: 		delete($classlist->{$student});
1.76      ng        545: 		next;
                    546: 	    }
                    547: 	}
1.450     banghart  548: 	# filter students according to groups selected
1.453     banghart  549: 	my @stu_groups = split(/,/,$group);
1.450     banghart  550: 	if (@getgroup) {
                    551: 	    my $exclude = 1;
1.454     banghart  552: 	    foreach my $grp (@getgroup) {
                    553: 	        foreach my $stu_group (@stu_groups) {
1.453     banghart  554: 	            if ($stu_group eq $grp) {
                    555: 	                $exclude = 0;
                    556:     	            } 
1.450     banghart  557: 	        }
1.453     banghart  558:     	        if (($grp eq 'none') && !$group) {
                    559:         	        $exclude = 0;
                    560:         	}
1.450     banghart  561: 	    }
                    562: 	    if ($exclude) {
                    563: 	        delete($classlist->{$student});
                    564: 	    }
                    565: 	}
1.205     matthew   566: 	$section = ($section ne '' ? $section : 'none');
1.106     albertel  567: 	if (&canview($section)) {
1.291     albertel  568: 	    if (!@getsec || grep(/^\Q$section\E$/,@getsec)) {
1.103     albertel  569: 		$sections{$section}++;
1.450     banghart  570: 		if ($classlist->{$student}) {
                    571: 		    $fullnames{$student}=$fullname;
                    572: 		}
1.103     albertel  573: 	    } else {
1.205     matthew   574: 		delete($classlist->{$student});
1.103     albertel  575: 	    }
                    576: 	} else {
1.205     matthew   577: 	    delete($classlist->{$student});
1.103     albertel  578: 	}
1.44      ng        579:     }
                    580:     my %seen = ();
1.56      matthew   581:     my @sections = sort(keys(%sections));
                    582:     return ($classlist,\@sections,\%fullnames);
1.44      ng        583: }
                    584: 
1.103     albertel  585: sub canmodify {
                    586:     my ($sec)=@_;
                    587:     if ($perm{'mgr'}) {
                    588: 	if (!defined($perm{'mgr_section'})) {
                    589: 	    # can modify whole class
                    590: 	    return 1;
                    591: 	} else {
                    592: 	    if ($sec eq $perm{'mgr_section'}) {
                    593: 		#can modify the requested section
                    594: 		return 1;
                    595: 	    } else {
                    596: 		# can't modify the request section
                    597: 		return 0;
                    598: 	    }
                    599: 	}
                    600:     }
                    601:     #can't modify
                    602:     return 0;
                    603: }
                    604: 
                    605: sub canview {
                    606:     my ($sec)=@_;
                    607:     if ($perm{'vgr'}) {
                    608: 	if (!defined($perm{'vgr_section'})) {
                    609: 	    # can modify whole class
                    610: 	    return 1;
                    611: 	} else {
                    612: 	    if ($sec eq $perm{'vgr_section'}) {
                    613: 		#can modify the requested section
                    614: 		return 1;
                    615: 	    } else {
                    616: 		# can't modify the request section
                    617: 		return 0;
                    618: 	    }
                    619: 	}
                    620:     }
                    621:     #can't modify
                    622:     return 0;
                    623: }
                    624: 
1.44      ng        625: #--- Retrieve the grade status of a student for all the parts
                    626: sub student_gradeStatus {
1.324     albertel  627:     my ($symb,$udom,$uname,$partlist) = @_;
1.257     albertel  628:     my %record     = &Apache::lonnet::restore($symb,$env{'request.course.id'},$udom,$uname);
1.44      ng        629:     my %partstatus = ();
                    630:     foreach (@$partlist) {
1.128     ng        631: 	my ($status,undef)   = split(/_/,$record{"resource.$_.solved"},2);
1.44      ng        632: 	$status              = 'nothing' if ($status eq '');
                    633: 	$partstatus{$_}      = $status;
                    634: 	my $subkey           = "resource.$_.submitted_by";
                    635: 	$partstatus{$subkey} = $record{$subkey} if ($record{$subkey} ne '');
                    636:     }
                    637:     return %partstatus;
                    638: }
                    639: 
1.45      ng        640: # hidden form and javascript that calls the form
                    641: # Use by verifyscript and viewgrades
                    642: # Shows a student's view of problem and submission
                    643: sub jscriptNform {
1.324     albertel  644:     my ($symb) = @_;
1.442     banghart  645:     my $stu_status = join(':',&Apache::loncommon::get_env_multiple('form.Status'));
1.45      ng        646:     my $jscript='<script type="text/javascript" language="javascript">'."\n".
                    647: 	'    function viewOneStudent(user,domain) {'."\n".
                    648: 	'	document.onestudent.student.value = user;'."\n".
                    649: 	'	document.onestudent.userdom.value = domain;'."\n".
                    650: 	'	document.onestudent.submit();'."\n".
                    651: 	'    }'."\n".
                    652: 	'</script>'."\n";
                    653:     $jscript.= '<form action="/adm/grades" method="post" name="onestudent">'."\n".
1.418     albertel  654: 	'<input type="hidden" name="symb"    value="'.&Apache::lonenc::check_encrypt($symb).'" />'."\n".
1.257     albertel  655: 	'<input type="hidden" name="saveState" value="'.$env{'form.saveState'}.'" />'."\n".
                    656: 	'<input type="hidden" name="probTitle" value="'.$env{'form.probTitle'}.'" />'."\n".
1.442     banghart  657: 	'<input type="hidden" name="Status"  value="'.$stu_status.'" />'."\n".
1.45      ng        658: 	'<input type="hidden" name="command" value="submission" />'."\n".
                    659: 	'<input type="hidden" name="student" value="" />'."\n".
                    660: 	'<input type="hidden" name="userdom" value="" />'."\n".
                    661: 	'</form>'."\n";
                    662:     return $jscript;
                    663: }
1.39      ng        664: 
1.447     foxr      665: 
                    666: 
1.315     bowersj2  667: # Given the score (as a number [0-1] and the weight) what is the final
                    668: # point value? This function will round to the nearest tenth, third,
                    669: # or quarter if one of those is within the tolerance of .00001.
1.316     albertel  670: sub compute_points {
1.315     bowersj2  671:     my ($score, $weight) = @_;
                    672:     
                    673:     my $tolerance = .00001;
                    674:     my $points = $score * $weight;
                    675: 
                    676:     # Check for nearness to 1/x.
                    677:     my $check_for_nearness = sub {
                    678:         my ($factor) = @_;
                    679:         my $num = ($points * $factor) + $tolerance;
                    680:         my $floored_num = floor($num);
1.316     albertel  681:         if ($num - $floored_num < 2 * $tolerance * $factor) {
1.315     bowersj2  682:             return $floored_num / $factor;
                    683:         }
                    684:         return $points;
                    685:     };
                    686: 
                    687:     $points = $check_for_nearness->(10);
                    688:     $points = $check_for_nearness->(3);
                    689:     $points = $check_for_nearness->(4);
                    690:     
                    691:     return $points;
                    692: }
                    693: 
1.44      ng        694: #------------------ End of general use routines --------------------
1.87      www       695: 
                    696: #
                    697: # Find most similar essay
                    698: #
                    699: 
                    700: sub most_similar {
1.426     albertel  701:     my ($uname,$udom,$uessay,$old_essays)=@_;
1.87      www       702: 
                    703: # ignore spaces and punctuation
                    704: 
                    705:     $uessay=~s/\W+/ /gs;
                    706: 
1.282     www       707: # ignore empty submissions (occuring when only files are sent)
                    708: 
                    709:     unless ($uessay=~/\w+/) { return ''; }
                    710: 
1.87      www       711: # these will be returned. Do not care if not at least 50 percent similar
1.88      www       712:     my $limit=0.6;
1.87      www       713:     my $sname='';
                    714:     my $sdom='';
                    715:     my $scrsid='';
                    716:     my $sessay='';
                    717: # go through all essays ...
1.426     albertel  718:     foreach my $tkey (keys(%$old_essays)) {
                    719: 	my ($tname,$tdom,$tcrsid)=map {&unescape($_)} (split(/\./,$tkey));
1.87      www       720: # ... except the same student
1.426     albertel  721:         next if (($tname eq $uname) && ($tdom eq $udom));
                    722: 	my $tessay=$old_essays->{$tkey};
                    723: 	$tessay=~s/\W+/ /gs;
1.87      www       724: # String similarity gives up if not even limit
1.426     albertel  725: 	my $tsimilar=&String::Similarity::similarity($uessay,$tessay,$limit);
1.87      www       726: # Found one
1.426     albertel  727: 	if ($tsimilar>$limit) {
                    728: 	    $limit=$tsimilar;
                    729: 	    $sname=$tname;
                    730: 	    $sdom=$tdom;
                    731: 	    $scrsid=$tcrsid;
                    732: 	    $sessay=$old_essays->{$tkey};
                    733: 	}
1.87      www       734:     }
1.88      www       735:     if ($limit>0.6) {
1.87      www       736:        return ($sname,$sdom,$scrsid,$sessay,$limit);
                    737:     } else {
                    738:        return ('','','','',0);
                    739:     }
                    740: }
                    741: 
1.44      ng        742: #-------------------------------------------------------------------
                    743: 
                    744: #------------------------------------ Receipt Verification Routines
1.45      ng        745: #
1.44      ng        746: #--- Check whether a receipt number is valid.---
                    747: sub verifyreceipt {
                    748:     my $request  = shift;
                    749: 
1.257     albertel  750:     my $courseid = $env{'request.course.id'};
1.184     www       751:     my $receipt  = &Apache::lonnet::recprefix($courseid).'-'.
1.257     albertel  752: 	$env{'form.receipt'};
1.44      ng        753:     $receipt     =~ s/[^\-\d]//g;
1.378     albertel  754:     my ($symb)   = &get_symb($request);
1.44      ng        755: 
1.487     albertel  756:     my $title.=
                    757: 	'<h3><span class="LC_info">'.
1.553     biermanm  758: 	&mt('Verifying  Receipt No. [_1]',$receipt).
1.487     albertel  759: 	'</span></h3>'."\n".
                    760: 	'<h4>'.&mt('<b>Resource: </b>[_1]',$env{'form.probTitle'}).
                    761: 	'</h4>'."\n";
1.44      ng        762: 
                    763:     my ($string,$contents,$matches) = ('','',0);
1.56      matthew   764:     my (undef,undef,$fullname) = &getclasslist('all','0');
1.177     albertel  765:     
                    766:     my $receiptparts=0;
1.390     albertel  767:     if ($env{"course.$courseid.receiptalg"} eq 'receipt2' ||
                    768: 	$env{"course.$courseid.receiptalg"} eq 'receipt3') { $receiptparts=1; }
1.177     albertel  769:     my $parts=['0'];
1.324     albertel  770:     if ($receiptparts) { ($parts)=&response_type($symb); }
1.486     albertel  771:     
                    772:     my $header = 
                    773: 	&Apache::loncommon::start_data_table().
                    774: 	&Apache::loncommon::start_data_table_header_row().
1.487     albertel  775: 	'<th>&nbsp;'.&mt('Fullname').'&nbsp;</th>'."\n".
                    776: 	'<th>&nbsp;'.&mt('Username').'&nbsp;</th>'."\n".
                    777: 	'<th>&nbsp;'.&mt('Domain').'&nbsp;</th>';
1.486     albertel  778:     if ($receiptparts) {
1.487     albertel  779: 	$header.='<th>&nbsp;'.&mt('Problem Part').'&nbsp;</th>';
1.486     albertel  780:     }
                    781:     $header.=
                    782: 	&Apache::loncommon::end_data_table_header_row();
                    783: 
1.294     albertel  784:     foreach (sort 
                    785: 	     {
                    786: 		 if (lc($$fullname{$a}) ne lc($$fullname{$b})) {
                    787: 		     return (lc($$fullname{$a}) cmp lc($$fullname{$b}));
                    788: 		 }
                    789: 		 return $a cmp $b;
                    790: 	     } (keys(%$fullname))) {
1.44      ng        791: 	my ($uname,$udom)=split(/\:/);
1.177     albertel  792: 	foreach my $part (@$parts) {
                    793: 	    if ($receipt eq &Apache::lonnet::ireceipt($uname,$udom,$courseid,$symb,$part)) {
1.486     albertel  794: 		$contents.=
                    795: 		    &Apache::loncommon::start_data_table_row().
                    796: 		    '<td>&nbsp;'."\n".
1.177     albertel  797: 		    '<a href="javascript:viewOneStudent(\''.$uname.'\',\''.$udom.
1.417     albertel  798: 		    '\');" target="_self">'.$$fullname{$_}.'</a>&nbsp;</td>'."\n".
1.177     albertel  799: 		    '<td>&nbsp;'.$uname.'&nbsp;</td>'.
                    800: 		    '<td>&nbsp;'.$udom.'&nbsp;</td>';
                    801: 		if ($receiptparts) {
                    802: 		    $contents.='<td>&nbsp;'.$part.'&nbsp;</td>';
                    803: 		}
1.486     albertel  804: 		$contents.= 
                    805: 		    &Apache::loncommon::end_data_table_row()."\n";
1.177     albertel  806: 		
                    807: 		$matches++;
                    808: 	    }
1.44      ng        809: 	}
                    810:     }
                    811:     if ($matches == 0) {
1.487     albertel  812: 	$string = $title.&mt('No match found for the above receipt.');
1.44      ng        813:     } else {
1.324     albertel  814: 	$string = &jscriptNform($symb).$title.
1.487     albertel  815: 	    '<p>'.
                    816: 	    &mt('The above receipt matches the following [numerate,_1,student].',$matches).
                    817: 	    '</p>'.
1.486     albertel  818: 	    $header.
                    819: 	    $contents.
                    820: 	    &Apache::loncommon::end_data_table()."\n";
1.44      ng        821:     }
1.324     albertel  822:     return $string.&show_grading_menu_form($symb);
1.44      ng        823: }
                    824: 
                    825: #--- This is called by a number of programs.
                    826: #--- Called from the Grading Menu - View/Grade an individual student
                    827: #--- Also called directly when one clicks on the subm button 
                    828: #    on the problem page.
1.30      ng        829: sub listStudents {
1.41      ng        830:     my ($request) = shift;
1.49      albertel  831: 
1.324     albertel  832:     my ($symb) = &get_symb($request);
1.257     albertel  833:     my $cdom      = $env{"course.$env{'request.course.id'}.domain"};
                    834:     my $cnum      = $env{"course.$env{'request.course.id'}.num"};
                    835:     my $getsec    = $env{'form.section'} eq '' ? 'all' : $env{'form.section'};
1.449     banghart  836:     my $getgroup  = $env{'form.group'} eq '' ? 'all' : $env{'form.group'};
1.257     albertel  837:     my $submitonly= $env{'form.submitonly'} eq '' ? 'all' : $env{'form.submitonly'};
1.548     bisitz    838:     my $viewgrade = $env{'form.showgrading'} eq 'yes' ? 'View/Grade/Regrade' : 'View';
1.257     albertel  839:     $env{'form.probTitle'} = $env{'form.probTitle'} eq '' ? 
                    840: 	&Apache::lonnet::gettitle($symb) : $env{'form.probTitle'};
1.49      albertel  841: 
1.548     bisitz    842:     my $result='<h3><span class="LC_info">&nbsp;'
                    843: 	.&mt("$viewgrade Submissions for a Student or a Group of Students")
1.485     albertel  844: 	.'</span></h3>';
1.118     ng        845: 
1.324     albertel  846:     my ($table,undef,$hdgrade,$partlist,$handgrade) = &showResourceInfo($symb,$env{'form.probTitle'},($env{'form.showgrading'} eq 'yes'));
1.49      albertel  847: 
1.559     raeburn   848:     my %lt = &Apache::lonlocal::texthash (
                    849: 		'multiple' => 'Please select a student or group of students before clicking on the Next button.',
                    850: 		'single'   => 'Please select the student before clicking on the Next button.',
                    851: 	     );
1.45      ng        852:     $request->print(<<LISTJAVASCRIPT);
                    853: <script type="text/javascript" language="javascript">
1.110     ng        854:     function checkSelect(checkBox) {
                    855: 	var ctr=0;
                    856: 	var sense="";
                    857: 	if (checkBox.length > 1) {
                    858: 	    for (var i=0; i<checkBox.length; i++) {
                    859: 		if (checkBox[i].checked) {
                    860: 		    ctr++;
                    861: 		}
                    862: 	    }
1.485     albertel  863: 	    sense = '$lt{'multiple'}';
1.110     ng        864: 	} else {
                    865: 	    if (checkBox.checked) {
                    866: 		ctr = 1;
                    867: 	    }
1.485     albertel  868: 	    sense = '$lt{'single'}';
1.110     ng        869: 	}
                    870: 	if (ctr == 0) {
1.485     albertel  871: 	    alert(sense);
1.110     ng        872: 	    return false;
                    873: 	}
                    874: 	document.gradesub.submit();
                    875:     }
                    876: 
                    877:     function reLoadList(formname) {
1.112     ng        878: 	if (formname.saveStatusOld.value == pullDownSelection(formname.Status)) {return;}
1.110     ng        879: 	formname.command.value = 'submission';
                    880: 	formname.submit();
                    881:     }
1.45      ng        882: </script>
                    883: LISTJAVASCRIPT
                    884: 
1.118     ng        885:     &commonJSfunctions($request);
1.41      ng        886:     $request->print($result);
1.39      ng        887: 
1.401     albertel  888:     my $checkhdgrade = ($env{'form.handgrade'} eq 'yes' && scalar(@$partlist) > 1 ) ? 'checked="checked"' : '';
                    889:     my $checklastsub = $checkhdgrade eq '' ? 'checked="checked"' : '';
1.154     albertel  890:     my $gradeTable='<form action="/adm/grades" method="post" name="gradesub">'.
1.485     albertel  891: 	"\n".$table;
                    892: 	
1.561     bisitz    893:     $gradeTable .= &Apache::lonhtmlcommon::start_pick_box();
                    894:     $gradeTable .= &Apache::lonhtmlcommon::row_title(&mt('View Problem Text'))
                    895:                   .'<label><input type="radio" name="vProb" value="no" checked="checked" /> '.&mt('no').' </label>'."\n"
                    896:                   .'<label><input type="radio" name="vProb" value="yes" /> '.&mt('one student').' </label>'."\n"
                    897:                   .'<label><input type="radio" name="vProb" value="all" /> '.&mt('all students').' </label><br />'."\n"
                    898:                   .&Apache::lonhtmlcommon::row_closure();
                    899:     $gradeTable .= &Apache::lonhtmlcommon::row_title(&mt('View Answer'))
                    900:                   .'<label><input type="radio" name="vAns" value="no"  /> '.&mt('no').' </label>'."\n"
                    901:                   .'<label><input type="radio" name="vAns" value="yes" /> '.&mt('one student').' </label>'."\n"
                    902:                   .'<label><input type="radio" name="vAns" value="all" checked="checked" /> '.&mt('all students').' </label><br />'."\n"
                    903:                   .&Apache::lonhtmlcommon::row_closure();
1.485     albertel  904: 
                    905:     my $submission_options;
1.257     albertel  906:     if ($env{'form.handgrade'} eq 'yes' && scalar(@$partlist) > 1) {
1.485     albertel  907: 	$submission_options.=
                    908: 	    '<label><input type="radio" name="lastSub" value="hdgrade" '.$checkhdgrade.' /> '.&mt('essay part only').' </label>'."\n";
1.49      albertel  909:     }
1.442     banghart  910:     my $stu_status = join(':',&Apache::loncommon::get_env_multiple('form.Status'));
                    911:     my $saveStatus = $stu_status eq '' ? 'Active' : $stu_status;
1.257     albertel  912:     $env{'form.Status'} = $saveStatus;
1.485     albertel  913:     $submission_options.=
                    914: 	'<label><input type="radio" name="lastSub" value="lastonly" '.$checklastsub.' /> '.&mt('last submission only').' </label>'."\n".
                    915: 	'<label><input type="radio" name="lastSub" value="last" /> '.&mt('last submission &amp; parts info').' </label>'."\n".
                    916: 	'<label><input type="radio" name="lastSub" value="datesub" /> '.&mt('by dates and submissions').' </label>'."\n".
                    917: 	'<label><input type="radio" name="lastSub" value="all" /> '.&mt('all details').'</label>';
1.561     bisitz    918:     $gradeTable .= &Apache::lonhtmlcommon::row_title(&mt('Submissions'))
                    919:                   .$submission_options
                    920:                   .&Apache::lonhtmlcommon::row_closure();
                    921: 
                    922:     $gradeTable .= &Apache::lonhtmlcommon::row_title(&mt('Grading Increments'))
                    923:                   .'<select name="increment">'
                    924:                   .'<option value="1">'.&mt('Whole Points').'</option>'
                    925:                   .'<option value=".5">'.&mt('Half Points').'</option>'
                    926:                   .'<option value=".25">'.&mt('Quarter Points').'</option>'
                    927:                   .'<option value=".1">'.&mt('Tenths of a Point').'</option>'
                    928:                   .'</select>'
                    929:                   .&Apache::lonhtmlcommon::row_closure();
1.485     albertel  930: 
                    931:     $gradeTable .= 
1.432     banghart  932:         &build_section_inputs().
1.45      ng        933: 	'<input type="hidden" name="submitonly"  value="'.$submitonly.'" />'."\n".
1.257     albertel  934: 	'<input type="hidden" name="handgrade"   value="'.$env{'form.handgrade'}.'" /><br />'."\n".
                    935: 	'<input type="hidden" name="showgrading" value="'.$env{'form.showgrading'}.'" /><br />'."\n".
                    936: 	'<input type="hidden" name="saveState"   value="'.$env{'form.saveState'}.'" />'."\n".
                    937: 	'<input type="hidden" name="probTitle"   value="'.$env{'form.probTitle'}.'" />'."\n".
1.418     albertel  938: 	'<input type="hidden" name="symb" value="'.&Apache::lonenc::check_encrypt($symb).'" />'."\n".
1.110     ng        939: 	'<input type="hidden" name="saveStatusOld" value="'.$saveStatus.'" />'."\n";
                    940: 
1.257     albertel  941:     if (exists($env{'form.gradingMenu'}) && exists($env{'form.Status'})) {
1.561     bisitz    942: 	$gradeTable .= '<input type="hidden" name="Status" value="'.$stu_status.'" />'."\n";
1.124     ng        943:     } else {
1.561     bisitz    944:         $gradeTable .= &Apache::lonhtmlcommon::row_title(&mt('Student Status'))
                    945:                       .&Apache::lonhtmlcommon::StatusOptions(
                    946:                            $saveStatus,undef,1,'javascript:reLoadList(this.form);')
                    947:                       .&Apache::lonhtmlcommon::row_closure();
1.124     ng        948:     }
1.112     ng        949: 
1.561     bisitz    950:     $gradeTable .= &Apache::lonhtmlcommon::row_title(&mt('Check For Plagiarism'))
                    951:                   .'<input type="checkbox" name="checkPlag" checked="checked" />'
                    952:                   .&Apache::lonhtmlcommon::row_closure(1)
                    953:                   .&Apache::lonhtmlcommon::end_pick_box();
                    954: 
                    955:     $gradeTable .= '<p>'
                    956:                   .&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"
                    957:                   .'<input type="hidden" name="command" value="processGroup" />'
                    958:                   .'</p>';
1.249     albertel  959: 
                    960: # checkall buttons
                    961:     $gradeTable.=&check_script('gradesub', 'stuinfo');
1.110     ng        962:     $gradeTable.='<input type="button" '."\n".
1.45      ng        963: 	'onClick="javascript:checkSelect(this.form.stuinfo);" '."\n".
1.539     riegler   964: 	'value="'.&mt('Next').' &rarr;" /> <br />'."\n";
1.249     albertel  965:     $gradeTable.=&check_buttons();
1.450     banghart  966:     my ($classlist, undef, $fullname) = &getclasslist($getsec,'1',$getgroup);
1.474     albertel  967:     $gradeTable.= &Apache::loncommon::start_data_table().
                    968: 	&Apache::loncommon::start_data_table_header_row();
1.110     ng        969:     my $loop = 0;
                    970:     while ($loop < 2) {
1.485     albertel  971: 	$gradeTable.='<th>'.&mt('No.').'</th><th>'.&mt('Select').'</th>'.
                    972: 	    '<th>'.&nameUserString('header').'&nbsp;'.&mt('Section/Group').'</th>';
1.301     albertel  973: 	if ($env{'form.showgrading'} eq 'yes' 
                    974: 	    && $submitonly ne 'queued'
                    975: 	    && $submitonly ne 'all') {
1.485     albertel  976: 	    foreach my $part (sort(@$partlist)) {
                    977: 		my $display_part=
                    978: 		    &get_display_part((split(/_/,$part))[0],$symb);
                    979: 		$gradeTable.=
                    980: 		    '<th>'.&mt('Part: [_1] Status',$display_part).'</th>';
1.110     ng        981: 	    }
1.301     albertel  982: 	} elsif ($submitonly eq 'queued') {
1.474     albertel  983: 	    $gradeTable.='<th>'.&mt('Queue Status').'&nbsp;</th>';
1.110     ng        984: 	}
                    985: 	$loop++;
1.126     ng        986: #	$gradeTable.='<td></td>' if ($loop%2 ==1);
1.41      ng        987:     }
1.474     albertel  988:     $gradeTable.=&Apache::loncommon::end_data_table_header_row()."\n";
1.41      ng        989: 
1.45      ng        990:     my $ctr = 0;
1.294     albertel  991:     foreach my $student (sort 
                    992: 			 {
                    993: 			     if (lc($$fullname{$a}) ne lc($$fullname{$b})) {
                    994: 				 return (lc($$fullname{$a}) cmp lc($$fullname{$b}));
                    995: 			     }
                    996: 			     return $a cmp $b;
                    997: 			 }
                    998: 			 (keys(%$fullname))) {
1.41      ng        999: 	my ($uname,$udom) = split(/:/,$student);
1.301     albertel 1000: 
1.110     ng       1001: 	my %status = ();
1.301     albertel 1002: 
                   1003: 	if ($submitonly eq 'queued') {
                   1004: 	    my %queue_status = 
                   1005: 		&Apache::bridgetask::get_student_status($symb,$cdom,$cnum,
                   1006: 							$udom,$uname);
                   1007: 	    next if (!defined($queue_status{'gradingqueue'}));
                   1008: 	    $status{'gradingqueue'} = $queue_status{'gradingqueue'};
                   1009: 	}
                   1010: 
                   1011: 	if ($env{'form.showgrading'} eq 'yes' 
                   1012: 	    && $submitonly ne 'queued'
                   1013: 	    && $submitonly ne 'all') {
1.324     albertel 1014: 	    (%status) =&student_gradeStatus($symb,$udom,$uname,$partlist);
1.145     albertel 1015: 	    my $submitted = 0;
1.164     albertel 1016: 	    my $graded = 0;
1.248     albertel 1017: 	    my $incorrect = 0;
1.110     ng       1018: 	    foreach (keys(%status)) {
1.145     albertel 1019: 		$submitted = 1 if ($status{$_} ne 'nothing');
1.248     albertel 1020: 		$graded = 1 if ($status{$_} =~ /^ungraded/);
                   1021: 		$incorrect = 1 if ($status{$_} =~ /^incorrect/);
                   1022: 		
1.110     ng       1023: 		my ($foo,$partid,$foo1) = split(/\./,$_);
                   1024: 		if ($status{'resource.'.$partid.'.submitted_by'} ne '') {
1.145     albertel 1025: 		    $submitted = 0;
1.150     albertel 1026: 		    my ($part)=split(/\./,$partid);
1.110     ng       1027: 		    $gradeTable.='<input type="hidden" name="'.
1.150     albertel 1028: 			$student.':'.$part.':submitted_by" value="'.
1.110     ng       1029: 			$status{'resource.'.$partid.'.submitted_by'}.'" />';
                   1030: 		}
1.41      ng       1031: 	    }
1.248     albertel 1032: 	    
1.156     albertel 1033: 	    next if (!$submitted && ($submitonly eq 'yes' ||
                   1034: 				     $submitonly eq 'incorrect' ||
                   1035: 				     $submitonly eq 'graded'));
1.248     albertel 1036: 	    next if (!$graded && ($submitonly eq 'graded'));
                   1037: 	    next if (!$incorrect && $submitonly eq 'incorrect');
1.41      ng       1038: 	}
1.34      ng       1039: 
1.45      ng       1040: 	$ctr++;
1.249     albertel 1041: 	my $section = $classlist->{$student}->[&Apache::loncoursedata::CL_SECTION()];
1.452     banghart 1042:         my $group = $classlist->{$student}->[&Apache::loncoursedata::CL_GROUP()];
1.104     albertel 1043: 	if ( $perm{'vgr'} eq 'F' ) {
1.474     albertel 1044: 	    if ($ctr%2 ==1) {
                   1045: 		$gradeTable.= &Apache::loncommon::start_data_table_row();
                   1046: 	    }
1.126     ng       1047: 	    $gradeTable.='<td align="right">'.$ctr.'&nbsp;</td>'.
1.563     bisitz   1048:                '<td align="center"><label><input type="checkbox" name="stuinfo" value="'.
1.249     albertel 1049:                $student.':'.$$fullname{$student}.':::SECTION'.$section.
                   1050: 	       ')&nbsp;" />&nbsp;&nbsp;</label></td>'."\n".'<td>'.
                   1051: 	       &nameUserString(undef,$$fullname{$student},$uname,$udom).
1.474     albertel 1052: 	       '&nbsp;'.$section.($group ne '' ?'/'.$group:'').'</td>'."\n";
1.110     ng       1053: 
1.257     albertel 1054: 	    if ($env{'form.showgrading'} eq 'yes' && $submitonly ne 'all') {
1.524     raeburn  1055: 		foreach (sort(keys(%status))) {
1.485     albertel 1056: 		    next if ($_ =~ /^resource.*?submitted_by$/);
                   1057: 		    $gradeTable.='<td align="center">&nbsp;'.&mt($status{$_}).'&nbsp;</td>'."\n";
1.110     ng       1058: 		}
1.41      ng       1059: 	    }
1.126     ng       1060: #	    $gradeTable.='<td></td>' if ($ctr%2 ==1);
1.474     albertel 1061: 	    if ($ctr%2 ==0) {
                   1062: 		$gradeTable.=&Apache::loncommon::end_data_table_row()."\n";
                   1063: 	    }
1.41      ng       1064: 	}
                   1065:     }
1.110     ng       1066:     if ($ctr%2 ==1) {
1.126     ng       1067: 	$gradeTable.='<td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td>';
1.301     albertel 1068: 	    if ($env{'form.showgrading'} eq 'yes' 
                   1069: 		&& $submitonly ne 'queued'
                   1070: 		&& $submitonly ne 'all') {
1.110     ng       1071: 		foreach (@$partlist) {
                   1072: 		    $gradeTable.='<td>&nbsp;</td>';
                   1073: 		}
1.301     albertel 1074: 	    } elsif ($submitonly eq 'queued') {
                   1075: 		$gradeTable.='<td>&nbsp;</td>';
1.110     ng       1076: 	    }
1.474     albertel 1077: 	$gradeTable.=&Apache::loncommon::end_data_table_row();
1.110     ng       1078:     }
                   1079: 
1.474     albertel 1080:     $gradeTable.=&Apache::loncommon::end_data_table()."\n".
1.45      ng       1081: 	'<input type="button" '.
                   1082: 	'onClick="javascript:checkSelect(this.form.stuinfo);" '.
1.539     riegler  1083: 	'value="'.&mt('Next').' &rarr;" /></form>'."\n";
1.45      ng       1084:     if ($ctr == 0) {
1.96      albertel 1085: 	my $num_students=(scalar(keys(%$fullname)));
                   1086: 	if ($num_students eq 0) {
1.485     albertel 1087: 	    $gradeTable='<br />&nbsp;<span class="LC_warning">'.&mt('There are no students currently enrolled.').'</span>';
1.96      albertel 1088: 	} else {
1.171     albertel 1089: 	    my $submissions='submissions';
                   1090: 	    if ($submitonly eq 'incorrect') { $submissions = 'incorrect submissions'; }
                   1091: 	    if ($submitonly eq 'graded'   ) { $submissions = 'ungraded submissions'; }
1.301     albertel 1092: 	    if ($submitonly eq 'queued'   ) { $submissions = 'queued submissions'; }
1.398     albertel 1093: 	    $gradeTable='<br />&nbsp;<span class="LC_warning">'.
1.485     albertel 1094: 		&mt('No '.$submissions.' found for this resource for any students. ([_1] students checked for '.$submissions.')',
                   1095: 		    $num_students).
                   1096: 		'</span><br />';
1.96      albertel 1097: 	}
1.46      ng       1098:     } elsif ($ctr == 1) {
1.474     albertel 1099: 	$gradeTable =~ s/type="checkbox"/type="checkbox" checked="checked"/;
1.45      ng       1100:     }
1.324     albertel 1101:     $gradeTable.=&show_grading_menu_form($symb);
1.45      ng       1102:     $request->print($gradeTable);
1.44      ng       1103:     return '';
1.10      ng       1104: }
                   1105: 
1.44      ng       1106: #---- Called from the listStudents routine
1.249     albertel 1107: 
                   1108: sub check_script {
                   1109:     my ($form, $type)=@_;
                   1110:     my $chkallscript='<script type="text/javascript">
                   1111:     function checkall() {
                   1112:         for (i=0; i<document.forms.'.$form.'.elements.length; i++) {
                   1113:             ele = document.forms.'.$form.'.elements[i];
                   1114:             if (ele.name == "'.$type.'") {
                   1115:             document.forms.'.$form.'.elements[i].checked=true;
                   1116:                                        }
                   1117:         }
                   1118:     }
                   1119: 
                   1120:     function checksec() {
                   1121:         for (i=0; i<document.forms.'.$form.'.elements.length; i++) {
                   1122:             ele = document.forms.'.$form.'.elements[i];
                   1123:            string = document.forms.'.$form.'.chksec.value;
                   1124:            if
                   1125:           (ele.value.indexOf(":::SECTION"+string)>0) {
                   1126:               document.forms.'.$form.'.elements[i].checked=true;
                   1127:             }
                   1128:         }
                   1129:     }
                   1130: 
                   1131: 
                   1132:     function uncheckall() {
                   1133:         for (i=0; i<document.forms.'.$form.'.elements.length; i++) {
                   1134:             ele = document.forms.'.$form.'.elements[i];
                   1135:             if (ele.name == "'.$type.'") {
                   1136:             document.forms.'.$form.'.elements[i].checked=false;
                   1137:                                        }
                   1138:         }
                   1139:     }
                   1140: 
                   1141: </script>'."\n";
                   1142:     return $chkallscript;
                   1143: }
                   1144: 
                   1145: sub check_buttons {
1.485     albertel 1146:     my $buttons.='<input type="button" onclick="checkall()" value="'.&mt('Check All').'" />';
                   1147:     $buttons.='<input type="button" onclick="uncheckall()" value="'.&mt('Uncheck All').'" />&nbsp;';
                   1148:     $buttons.='<input type="button" onclick="checksec()" value="'.&mt('Check Section/Group').'" />';
1.249     albertel 1149:     $buttons.='<input type="text" size="5" name="chksec" />&nbsp;';
                   1150:     return $buttons;
                   1151: }
                   1152: 
1.44      ng       1153: #     Displays the submissions for one student or a group of students
1.34      ng       1154: sub processGroup {
1.41      ng       1155:     my ($request)  = shift;
                   1156:     my $ctr        = 0;
1.155     albertel 1157:     my @stuchecked = &Apache::loncommon::get_env_multiple('form.stuinfo');
1.41      ng       1158:     my $total      = scalar(@stuchecked)-1;
1.45      ng       1159: 
1.396     banghart 1160:     foreach my $student (@stuchecked) {
                   1161: 	my ($uname,$udom,$fullname) = split(/:/,$student);
1.257     albertel 1162: 	$env{'form.student'}        = $uname;
                   1163: 	$env{'form.userdom'}        = $udom;
                   1164: 	$env{'form.fullname'}       = $fullname;
1.41      ng       1165: 	&submission($request,$ctr,$total);
                   1166: 	$ctr++;
                   1167:     }
                   1168:     return '';
1.35      ng       1169: }
1.34      ng       1170: 
1.44      ng       1171: #------------------------------------------------------------------------------------
                   1172: #
                   1173: #-------------------------- Next few routines handles grading by student, essentially
                   1174: #                           handles essay response type problem/part
                   1175: #
                   1176: #--- Javascript to handle the submission page functionality ---
                   1177: sub sub_page_js {
                   1178:     my $request = shift;
1.539     riegler  1179: 	    my $alertmsg = &mt('A number equal or greater than 0 is expected. Entered value = ');
1.44      ng       1180:     $request->print(<<SUBJAVASCRIPT);
                   1181: <script type="text/javascript" language="javascript">
1.71      ng       1182:     function updateRadio(formname,id,weight) {
1.125     ng       1183: 	var gradeBox = formname["GD_BOX"+id];
                   1184: 	var radioButton = formname["RADVAL"+id];
                   1185: 	var oldpts = formname["oldpts"+id].value;
1.72      ng       1186: 	var pts = checkSolved(formname,id) == 'update' ? gradeBox.value : oldpts;
1.71      ng       1187: 	gradeBox.value = pts;
                   1188: 	var resetbox = false;
                   1189: 	if (isNaN(pts) || pts < 0) {
1.539     riegler  1190: 	    alert("$alertmsg"+pts);
1.71      ng       1191: 	    for (var i=0; i<radioButton.length; i++) {
                   1192: 		if (radioButton[i].checked) {
                   1193: 		    gradeBox.value = i;
                   1194: 		    resetbox = true;
                   1195: 		}
                   1196: 	    }
                   1197: 	    if (!resetbox) {
                   1198: 		formtextbox.value = "";
                   1199: 	    }
                   1200: 	    return;
1.44      ng       1201: 	}
1.71      ng       1202: 
                   1203: 	if (pts > weight) {
                   1204: 	    var resp = confirm("You entered a value ("+pts+
                   1205: 			       ") greater than the weight for the part. Accept?");
                   1206: 	    if (resp == false) {
1.125     ng       1207: 		gradeBox.value = oldpts;
1.71      ng       1208: 		return;
                   1209: 	    }
1.44      ng       1210: 	}
1.13      albertel 1211: 
1.71      ng       1212: 	for (var i=0; i<radioButton.length; i++) {
                   1213: 	    radioButton[i].checked=false;
                   1214: 	    if (pts == i && pts != "") {
                   1215: 		radioButton[i].checked=true;
                   1216: 	    }
                   1217: 	}
                   1218: 	updateSelect(formname,id);
1.125     ng       1219: 	formname["stores"+id].value = "0";
1.41      ng       1220:     }
1.5       albertel 1221: 
1.72      ng       1222:     function writeBox(formname,id,pts) {
1.125     ng       1223: 	var gradeBox = formname["GD_BOX"+id];
1.71      ng       1224: 	if (checkSolved(formname,id) == 'update') {
                   1225: 	    gradeBox.value = pts;
                   1226: 	} else {
1.125     ng       1227: 	    var oldpts = formname["oldpts"+id].value;
1.72      ng       1228: 	    gradeBox.value = oldpts;
1.125     ng       1229: 	    var radioButton = formname["RADVAL"+id];
1.71      ng       1230: 	    for (var i=0; i<radioButton.length; i++) {
                   1231: 		radioButton[i].checked=false;
1.72      ng       1232: 		if (i == oldpts) {
1.71      ng       1233: 		    radioButton[i].checked=true;
                   1234: 		}
                   1235: 	    }
1.41      ng       1236: 	}
1.125     ng       1237: 	formname["stores"+id].value = "0";
1.71      ng       1238: 	updateSelect(formname,id);
                   1239: 	return;
1.41      ng       1240:     }
1.44      ng       1241: 
1.71      ng       1242:     function clearRadBox(formname,id) {
                   1243: 	if (checkSolved(formname,id) == 'noupdate') {
                   1244: 	    updateSelect(formname,id);
                   1245: 	    return;
                   1246: 	}
1.125     ng       1247: 	gradeSelect = formname["GD_SEL"+id];
1.71      ng       1248: 	for (var i=0; i<gradeSelect.length; i++) {
                   1249: 	    if (gradeSelect[i].selected) {
                   1250: 		var selectx=i;
                   1251: 	    }
                   1252: 	}
1.125     ng       1253: 	var stores = formname["stores"+id];
1.71      ng       1254: 	if (selectx == stores.value) { return };
1.125     ng       1255: 	var gradeBox = formname["GD_BOX"+id];
1.71      ng       1256: 	gradeBox.value = "";
1.125     ng       1257: 	var radioButton = formname["RADVAL"+id];
1.71      ng       1258: 	for (var i=0; i<radioButton.length; i++) {
                   1259: 	    radioButton[i].checked=false;
                   1260: 	}
                   1261: 	stores.value = selectx;
                   1262:     }
1.5       albertel 1263: 
1.71      ng       1264:     function checkSolved(formname,id) {
1.125     ng       1265: 	if (formname["solved"+id].value == "correct_by_student" && formname.overRideScore.value == 'no') {
1.118     ng       1266: 	    var reply = confirm("This problem has been graded correct by the computer. Do you want to change the score?");
                   1267: 	    if (!reply) {return "noupdate";}
1.120     ng       1268: 	    formname.overRideScore.value = 'yes';
1.41      ng       1269: 	}
1.71      ng       1270: 	return "update";
1.13      albertel 1271:     }
1.71      ng       1272: 
                   1273:     function updateSelect(formname,id) {
1.125     ng       1274: 	formname["GD_SEL"+id][0].selected = true;
1.71      ng       1275: 	return;
1.41      ng       1276:     }
1.33      ng       1277: 
1.121     ng       1278: //=========== Check that a point is assigned for all the parts  ============
1.71      ng       1279:     function checksubmit(formname,val,total,parttot) {
1.121     ng       1280: 	formname.gradeOpt.value = val;
1.71      ng       1281: 	if (val == "Save & Next") {
                   1282: 	    for (i=0;i<=total;i++) {
                   1283: 		for (j=0;j<parttot;j++) {
1.125     ng       1284: 		    var partid = formname["partid"+i+"_"+j].value;
1.127     ng       1285: 		    if (formname["GD_SEL"+i+"_"+partid][0].selected) {
1.125     ng       1286: 			var points = formname["GD_BOX"+i+"_"+partid].value;
1.71      ng       1287: 			if (points == "") {
1.125     ng       1288: 			    var name = formname["name"+i].value;
1.129     ng       1289: 			    var studentID = (name != '' ? name : formname["unamedom"+i].value);
                   1290: 			    var resp = confirm("You did not assign a score for "+studentID+
                   1291: 					       ", part "+partid+". Continue?");
1.71      ng       1292: 			    if (resp == false) {
1.125     ng       1293: 				formname["GD_BOX"+i+"_"+partid].focus();
1.71      ng       1294: 				return false;
                   1295: 			    }
                   1296: 			}
                   1297: 		    }
                   1298: 		    
                   1299: 		}
                   1300: 	    }
                   1301: 	    
                   1302: 	}
1.121     ng       1303: 	if (val == "Grade Student") {
                   1304: 	    formname.showgrading.value = "yes";
                   1305: 	    if (formname.Status.value == "") {
                   1306: 		formname.Status.value = "Active";
                   1307: 	    }
                   1308: 	    formname.studentNo.value = total;
                   1309: 	}
1.120     ng       1310: 	formname.submit();
                   1311:     }
                   1312: 
1.71      ng       1313: //======= Check that a score is assigned for all the problems (page/sequence grading only) =========
                   1314:     function checkSubmitPage(formname,total) {
                   1315: 	noscore = new Array(100);
                   1316: 	var ptr = 0;
                   1317: 	for (i=1;i<total;i++) {
1.125     ng       1318: 	    var partid = formname["q_"+i].value;
1.127     ng       1319: 	    if (formname["GD_SEL"+i+"_"+partid][0].selected) {
1.125     ng       1320: 		var points = formname["GD_BOX"+i+"_"+partid].value;
                   1321: 		var status = formname["solved"+i+"_"+partid].value;
1.71      ng       1322: 		if (points == "" && status != "correct_by_student") {
                   1323: 		    noscore[ptr] = i;
                   1324: 		    ptr++;
                   1325: 		}
                   1326: 	    }
                   1327: 	}
                   1328: 	if (ptr != 0) {
                   1329: 	    var sense = ptr == 1 ? ": " : "s: ";
                   1330: 	    var prolist = "";
                   1331: 	    if (ptr == 1) {
                   1332: 		prolist = noscore[0];
                   1333: 	    } else {
                   1334: 		var i = 0;
                   1335: 		while (i < ptr-1) {
                   1336: 		    prolist += noscore[i]+", ";
                   1337: 		    i++;
                   1338: 		}
                   1339: 		prolist += "and "+noscore[i];
                   1340: 	    }
                   1341: 	    var resp = confirm("You did not assign any score for the following problem"+sense+prolist+". Continue?");
                   1342: 	    if (resp == false) {
                   1343: 		return false;
                   1344: 	    }
                   1345: 	}
1.45      ng       1346: 
1.71      ng       1347: 	formname.submit();
                   1348:     }
                   1349: </script>
                   1350: SUBJAVASCRIPT
                   1351: }
1.45      ng       1352: 
1.71      ng       1353: #--- javascript for essay type problem --
                   1354: sub sub_page_kw_js {
                   1355:     my $request = shift;
1.80      ng       1356:     my $iconpath = $request->dir_config('lonIconsURL');
1.118     ng       1357:     &commonJSfunctions($request);
1.350     albertel 1358: 
1.351     albertel 1359:     my $inner_js_msg_central=<<INNERJS;
1.350     albertel 1360:     <script text="text/javascript">
                   1361:     function checkInput() {
                   1362:       opener.document.SCORE.msgsub.value = opener.checkEntities(document.msgcenter.msgsub.value);
                   1363:       var nmsg   = opener.document.SCORE.savemsgN.value;
                   1364:       var usrctr = document.msgcenter.usrctr.value;
                   1365:       var newval = opener.document.SCORE["newmsg"+usrctr];
                   1366:       newval.value = opener.checkEntities(document.msgcenter.newmsg.value);
                   1367: 
                   1368:       var msgchk = "";
                   1369:       if (document.msgcenter.subchk.checked) {
                   1370:          msgchk = "msgsub,";
                   1371:       }
                   1372:       var includemsg = 0;
                   1373:       for (var i=1; i<=nmsg; i++) {
                   1374:           var opnmsg = opener.document.SCORE["savemsg"+i];
                   1375:           var frmmsg = document.msgcenter["msg"+i];
                   1376:           opnmsg.value = opener.checkEntities(frmmsg.value);
                   1377:           var showflg = opener.document.SCORE["shownOnce"+i];
                   1378:           showflg.value = "1";
                   1379:           var chkbox = document.msgcenter["msgn"+i];
                   1380:           if (chkbox.checked) {
                   1381:              msgchk += "savemsg"+i+",";
                   1382:              includemsg = 1;
                   1383:           }
                   1384:       }
                   1385:       if (document.msgcenter.newmsgchk.checked) {
                   1386:          msgchk += "newmsg"+usrctr;
                   1387:          includemsg = 1;
                   1388:       }
                   1389:       imgformname = opener.document.SCORE["mailicon"+usrctr];
                   1390:       imgformname.src = "$iconpath/"+((includemsg) ? "mailto.gif" : "mailbkgrd.gif");
                   1391:       var includemsg = opener.document.SCORE["includemsg"+usrctr];
                   1392:       includemsg.value = msgchk;
                   1393: 
                   1394:       self.close()
                   1395: 
                   1396:     }
                   1397:     </script>
                   1398: INNERJS
                   1399: 
1.351     albertel 1400:     my $inner_js_highlight_central=<<INNERJS;
                   1401:  <script type="text/javascript">
                   1402:     function updateChoice(flag) {
                   1403:       opener.document.SCORE.kwclr.value = opener.radioSelection(document.hlCenter.kwdclr);
                   1404:       opener.document.SCORE.kwsize.value = opener.radioSelection(document.hlCenter.kwdsize);
                   1405:       opener.document.SCORE.kwstyle.value = opener.radioSelection(document.hlCenter.kwdstyle);
                   1406:       opener.document.SCORE.refresh.value = "on";
                   1407:       if (opener.document.SCORE.keywords.value!=""){
                   1408:          opener.document.SCORE.submit();
                   1409:       }
                   1410:       self.close()
                   1411:     }
                   1412: </script>
                   1413: INNERJS
                   1414: 
                   1415:     my $start_page_msg_central = 
                   1416:         &Apache::loncommon::start_page('Message Central',$inner_js_msg_central,
                   1417: 				       {'js_ready'  => 1,
                   1418: 					'only_body' => 1,
                   1419: 					'bgcolor'   =>'#FFFFFF',});
                   1420:     my $end_page_msg_central = 
                   1421: 	&Apache::loncommon::end_page({'js_ready' => 1});
                   1422: 
                   1423: 
                   1424:     my $start_page_highlight_central = 
                   1425:         &Apache::loncommon::start_page('Highlight Central',
                   1426: 				       $inner_js_highlight_central,
1.350     albertel 1427: 				       {'js_ready'  => 1,
                   1428: 					'only_body' => 1,
                   1429: 					'bgcolor'   =>'#FFFFFF',});
1.351     albertel 1430:     my $end_page_highlight_central = 
1.350     albertel 1431: 	&Apache::loncommon::end_page({'js_ready' => 1});
                   1432: 
1.219     www      1433:     my $docopen=&Apache::lonhtmlcommon::javascript_docopen();
1.236     albertel 1434:     $docopen=~s/^document\.//;
1.539     riegler  1435:     my $alertmsg = &mt('Please select a word or group of words from document and then click this link.');
1.71      ng       1436:     $request->print(<<SUBJAVASCRIPT);
                   1437: <script type="text/javascript" language="javascript">
1.45      ng       1438: 
1.44      ng       1439: //===================== Show list of keywords ====================
1.122     ng       1440:   function keywords(formname) {
                   1441:     var nret = prompt("Keywords list, separated by a space. Add/delete to list if desired.",formname.keywords.value);
1.44      ng       1442:     if (nret==null) return;
1.122     ng       1443:     formname.keywords.value = nret;
1.44      ng       1444: 
1.122     ng       1445:     if (formname.keywords.value != "") {
1.128     ng       1446: 	formname.refresh.value = "on";
1.122     ng       1447: 	formname.submit();
1.44      ng       1448:     }
                   1449:     return;
                   1450:   }
                   1451: 
                   1452: //===================== Script to view submitted by ==================
                   1453:   function viewSubmitter(submitter) {
                   1454:     document.SCORE.refresh.value = "on";
                   1455:     document.SCORE.NCT.value = "1";
                   1456:     document.SCORE.unamedom0.value = submitter;
                   1457:     document.SCORE.submit();
                   1458:     return;
                   1459:   }
                   1460: 
                   1461: //===================== Script to add keyword(s) ==================
                   1462:   function getSel() {
                   1463:     if (document.getSelection) txt = document.getSelection();
                   1464:     else if (document.selection) txt = document.selection.createRange().text;
                   1465:     else return;
                   1466:     var cleantxt = txt.replace(new RegExp('([\\f\\n\\r\\t\\v ])+', 'g')," ");
                   1467:     if (cleantxt=="") {
1.539     riegler  1468: 	alert("$alertmsg");
1.44      ng       1469: 	return;
                   1470:     }
                   1471:     var nret = prompt("Add selection to keyword list? Edit if desired.",cleantxt);
                   1472:     if (nret==null) return;
1.127     ng       1473:     document.SCORE.keywords.value = document.SCORE.keywords.value+" "+nret;
1.44      ng       1474:     if (document.SCORE.keywords.value != "") {
1.127     ng       1475: 	document.SCORE.refresh.value = "on";
1.44      ng       1476: 	document.SCORE.submit();
                   1477:     }
                   1478:     return;
                   1479:   }
                   1480: 
                   1481: //====================== Script for composing message ==============
1.80      ng       1482:    // preload images
                   1483:    img1 = new Image();
                   1484:    img1.src = "$iconpath/mailbkgrd.gif";
                   1485:    img2 = new Image();
                   1486:    img2.src = "$iconpath/mailto.gif";
                   1487: 
1.44      ng       1488:   function msgCenter(msgform,usrctr,fullname) {
                   1489:     var Nmsg  = msgform.savemsgN.value;
                   1490:     savedMsgHeader(Nmsg,usrctr,fullname);
                   1491:     var subject = msgform.msgsub.value;
1.127     ng       1492:     var msgchk = document.SCORE["includemsg"+usrctr].value;
1.44      ng       1493:     re = /msgsub/;
                   1494:     var shwsel = "";
                   1495:     if (re.test(msgchk)) { shwsel = "checked" }
1.123     ng       1496:     subject = (document.SCORE.shownSub.value == 0 ? checkEntities(subject) : subject);
                   1497:     displaySubject(checkEntities(subject),shwsel);
1.44      ng       1498:     for (var i=1; i<=Nmsg; i++) {
1.123     ng       1499: 	var testmsg = "savemsg"+i+",";
                   1500: 	re = new RegExp(testmsg,"g");
1.44      ng       1501: 	shwsel = "";
                   1502: 	if (re.test(msgchk)) { shwsel = "checked" }
1.125     ng       1503: 	var message = document.SCORE["savemsg"+i].value;
1.126     ng       1504: 	message = (document.SCORE["shownOnce"+i].value == 0 ? checkEntities(message) : message);
1.123     ng       1505: 	displaySavedMsg(i,message,shwsel); //I do not get it. w/o checkEntities on saved messages,
                   1506: 	                                   //any &lt; is already converted to <, etc. However, only once!!
1.44      ng       1507:     }
1.125     ng       1508:     newmsg = document.SCORE["newmsg"+usrctr].value;
1.44      ng       1509:     shwsel = "";
                   1510:     re = /newmsg/;
                   1511:     if (re.test(msgchk)) { shwsel = "checked" }
                   1512:     newMsg(newmsg,shwsel);
                   1513:     msgTail(); 
                   1514:     return;
                   1515:   }
                   1516: 
1.123     ng       1517:   function checkEntities(strx) {
                   1518:     if (strx.length == 0) return strx;
                   1519:     var orgStr = ["&", "<", ">", '"']; 
                   1520:     var newStr = ["&amp;", "&lt;", "&gt;", "&quot;"];
                   1521:     var counter = 0;
                   1522:     while (counter < 4) {
                   1523: 	strx = strReplace(strx,orgStr[counter],newStr[counter]);
                   1524: 	counter++;
                   1525:     }
                   1526:     return strx;
                   1527:   }
                   1528: 
                   1529:   function strReplace(strx, orgStr, newStr) {
                   1530:     return strx.split(orgStr).join(newStr);
                   1531:   }
                   1532: 
1.44      ng       1533:   function savedMsgHeader(Nmsg,usrctr,fullname) {
1.76      ng       1534:     var height = 70*Nmsg+250;
1.44      ng       1535:     var scrollbar = "no";
                   1536:     if (height > 600) {
                   1537: 	height = 600;
                   1538: 	scrollbar = "yes";
                   1539:     }
1.118     ng       1540:     var xpos = (screen.width-600)/2;
                   1541:     xpos = (xpos < 0) ? '0' : xpos;
                   1542:     var ypos = (screen.height-height)/2-30;
                   1543:     ypos = (ypos < 0) ? '0' : ypos;
                   1544: 
1.206     albertel 1545:     pWin = window.open('', 'MessageCenter', 'resizable=yes,toolbar=no,location=no,scrollbars='+scrollbar+',screenx='+xpos+',screeny='+ypos+',width=600,height='+height);
1.76      ng       1546:     pWin.focus();
                   1547:     pDoc = pWin.document;
1.219     www      1548:     pDoc.$docopen;
1.351     albertel 1549:     pDoc.write('$start_page_msg_central');
1.76      ng       1550: 
                   1551:     pDoc.write("<form action=\\"inactive\\" name=\\"msgcenter\\">");
                   1552:     pDoc.write("<input value=\\""+usrctr+"\\" name=\\"usrctr\\" type=\\"hidden\\">");
1.465     albertel 1553:     pDoc.write("<h3><span class=\\"LC_info\\">&nbsp;Compose Message for \"+fullname+\"<\\/span><\\/h3><br /><br />");
1.76      ng       1554: 
1.564     bisitz   1555:     pDoc.write('<table border="0" width="100%"><tr><td bgcolor="#777777">');
                   1556:     pDoc.write('<table border="0" width="100%"><tr bgcolor="#DDFFFF">');
1.465     albertel 1557:     pDoc.write("<td><b>Type<\\/b><\\/td><td><b>Include<\\/b><\\/td><td><b>Message<\\/td><\\/tr>");
1.44      ng       1558: }
                   1559:     function displaySubject(msg,shwsel) {
1.76      ng       1560:     pDoc = pWin.document;
                   1561:     pDoc.write("<tr bgcolor=\\"#ffffdd\\">");
1.465     albertel 1562:     pDoc.write("<td>Subject<\\/td>");
                   1563:     pDoc.write("<td align=\\"center\\"><input name=\\"subchk\\" type=\\"checkbox\\"" +shwsel+"><\\/td>");
                   1564:     pDoc.write("<td><input name=\\"msgsub\\" type=\\"text\\" value=\\""+msg+"\\"size=\\"60\\" maxlength=\\"80\\"><\\/td><\\/tr>");
1.44      ng       1565: }
                   1566: 
1.72      ng       1567:   function displaySavedMsg(ctr,msg,shwsel) {
1.76      ng       1568:     pDoc = pWin.document;
                   1569:     pDoc.write("<tr bgcolor=\\"#ffffdd\\">");
1.465     albertel 1570:     pDoc.write("<td align=\\"center\\">"+ctr+"<\\/td>");
                   1571:     pDoc.write("<td align=\\"center\\"><input name=\\"msgn"+ctr+"\\" type=\\"checkbox\\"" +shwsel+"><\\/td>");
                   1572:     pDoc.write("<td><textarea name=\\"msg"+ctr+"\\" cols=\\"60\\" rows=\\"3\\">"+msg+"<\\/textarea><\\/td><\\/tr>");
1.44      ng       1573: }
                   1574: 
                   1575:   function newMsg(newmsg,shwsel) {
1.76      ng       1576:     pDoc = pWin.document;
                   1577:     pDoc.write("<tr bgcolor=\\"#ffffdd\\">");
1.465     albertel 1578:     pDoc.write("<td align=\\"center\\">New<\\/td>");
                   1579:     pDoc.write("<td align=\\"center\\"><input name=\\"newmsgchk\\" type=\\"checkbox\\"" +shwsel+"><\\/td>");
                   1580:     pDoc.write("<td><textarea name=\\"newmsg\\" cols=\\"60\\" rows=\\"3\\" onchange=\\"javascript:this.form.newmsgchk.checked=true\\" >"+newmsg+"<\\/textarea><\\/td><\\/tr>");
1.44      ng       1581: }
                   1582: 
                   1583:   function msgTail() {
1.76      ng       1584:     pDoc = pWin.document;
1.465     albertel 1585:     pDoc.write("<\\/table>");
                   1586:     pDoc.write("<\\/td><\\/tr><\\/table>&nbsp;");
1.76      ng       1587:     pDoc.write("<input type=\\"button\\" value=\\"Save\\" onClick=\\"javascript:checkInput()\\">&nbsp;&nbsp;");
1.326     albertel 1588:     pDoc.write("<input type=\\"button\\" value=\\"Cancel\\" onClick=\\"self.close()\\"><br /><br />");
1.465     albertel 1589:     pDoc.write("<\\/form>");
1.351     albertel 1590:     pDoc.write('$end_page_msg_central');
1.128     ng       1591:     pDoc.close();
1.44      ng       1592: }
                   1593: 
                   1594: //====================== Script for keyword highlight options ==============
                   1595:   function kwhighlight() {
                   1596:     var kwclr    = document.SCORE.kwclr.value;
                   1597:     var kwsize   = document.SCORE.kwsize.value;
                   1598:     var kwstyle  = document.SCORE.kwstyle.value;
                   1599:     var redsel = "";
                   1600:     var grnsel = "";
                   1601:     var blusel = "";
                   1602:     if (kwclr=="red")   {var redsel="checked"};
                   1603:     if (kwclr=="green") {var grnsel="checked"};
                   1604:     if (kwclr=="blue")  {var blusel="checked"};
                   1605:     var sznsel = "";
                   1606:     var sz1sel = "";
                   1607:     var sz2sel = "";
                   1608:     if (kwsize=="0")  {var sznsel="checked"};
                   1609:     if (kwsize=="+1") {var sz1sel="checked"};
                   1610:     if (kwsize=="+2") {var sz2sel="checked"};
                   1611:     var synsel = "";
                   1612:     var syisel = "";
                   1613:     var sybsel = "";
                   1614:     if (kwstyle=="")    {var synsel="checked"};
                   1615:     if (kwstyle=="<i>") {var syisel="checked"};
                   1616:     if (kwstyle=="<b>") {var sybsel="checked"};
                   1617:     highlightCentral();
                   1618:     highlightbody('red','red',redsel,'0','normal',sznsel,'','normal',synsel);
                   1619:     highlightbody('green','green',grnsel,'+1','+1',sz1sel,'<i>','italic',syisel);
                   1620:     highlightbody('blue','blue',blusel,'+2','+2',sz2sel,'<b>','bold',sybsel);
                   1621:     highlightend();
                   1622:     return;
                   1623:   }
                   1624: 
                   1625:   function highlightCentral() {
1.76      ng       1626: //    if (window.hwdWin) window.hwdWin.close();
1.118     ng       1627:     var xpos = (screen.width-400)/2;
                   1628:     xpos = (xpos < 0) ? '0' : xpos;
                   1629:     var ypos = (screen.height-330)/2-30;
                   1630:     ypos = (ypos < 0) ? '0' : ypos;
                   1631: 
1.206     albertel 1632:     hwdWin = window.open('', 'KeywordHighlightCentral', 'resizeable=yes,toolbar=no,location=no,scrollbars=no,width=400,height=300,screenx='+xpos+',screeny='+ypos);
1.76      ng       1633:     hwdWin.focus();
                   1634:     var hDoc = hwdWin.document;
1.219     www      1635:     hDoc.$docopen;
1.351     albertel 1636:     hDoc.write('$start_page_highlight_central');
1.76      ng       1637:     hDoc.write("<form action=\\"inactive\\" name=\\"hlCenter\\">");
1.465     albertel 1638:     hDoc.write("<h3><span class=\\"LC_info\\">&nbsp;Keyword Highlight Options<\\/span><\\/h3><br /><br />");
1.76      ng       1639: 
1.564     bisitz   1640:     hDoc.write('<table border="0" width="100%"><tr><td bgcolor="#777777">');
                   1641:     hDoc.write('<table border="0" width="100%"><tr bgcolor="#DDFFFF">');
1.465     albertel 1642:     hDoc.write("<td><b>Text Color<\\/b><\\/td><td><b>Font Size<\\/b><\\/td><td><b>Font Style<\\/td><\\/tr>");
1.44      ng       1643:   }
                   1644: 
                   1645:   function highlightbody(clrval,clrtxt,clrsel,szval,sztxt,szsel,syval,sytxt,sysel) { 
1.76      ng       1646:     var hDoc = hwdWin.document;
                   1647:     hDoc.write("<tr bgcolor=\\"#ffffdd\\">");
                   1648:     hDoc.write("<td align=\\"left\\">");
1.465     albertel 1649:     hDoc.write("<input name=\\"kwdclr\\" type=\\"radio\\" value=\\""+clrval+"\\" "+clrsel+">&nbsp;"+clrtxt+"<\\/td>");
1.76      ng       1650:     hDoc.write("<td align=\\"left\\">");
1.465     albertel 1651:     hDoc.write("<input name=\\"kwdsize\\" type=\\"radio\\" value=\\""+szval+"\\" "+szsel+">&nbsp;"+sztxt+"<\\/td>");
1.76      ng       1652:     hDoc.write("<td align=\\"left\\">");
1.465     albertel 1653:     hDoc.write("<input name=\\"kwdstyle\\" type=\\"radio\\" value=\\""+syval+"\\" "+sysel+">&nbsp;"+sytxt+"<\\/td>");
                   1654:     hDoc.write("<\\/tr>");
1.44      ng       1655:   }
                   1656: 
                   1657:   function highlightend() { 
1.76      ng       1658:     var hDoc = hwdWin.document;
1.465     albertel 1659:     hDoc.write("<\\/table>");
                   1660:     hDoc.write("<\\/td><\\/tr><\\/table>&nbsp;");
1.76      ng       1661:     hDoc.write("<input type=\\"button\\" value=\\"Save\\" onClick=\\"javascript:updateChoice(1)\\">&nbsp;&nbsp;");
1.326     albertel 1662:     hDoc.write("<input type=\\"button\\" value=\\"Cancel\\" onClick=\\"self.close()\\"><br /><br />");
1.465     albertel 1663:     hDoc.write("<\\/form>");
1.351     albertel 1664:     hDoc.write('$end_page_highlight_central');
1.128     ng       1665:     hDoc.close();
1.44      ng       1666:   }
                   1667: 
                   1668: </script>
                   1669: SUBJAVASCRIPT
                   1670: }
                   1671: 
1.349     albertel 1672: sub get_increment {
1.348     bowersj2 1673:     my $increment = $env{'form.increment'};
                   1674:     if ($increment != 1 && $increment != .5 && $increment != .25 &&
                   1675:         $increment != .1) {
                   1676:         $increment = 1;
                   1677:     }
                   1678:     return $increment;
                   1679: }
                   1680: 
1.71      ng       1681: #--- displays the grading box, used in essay type problem and grading by page/sequence
                   1682: sub gradeBox {
1.322     albertel 1683:     my ($request,$symb,$uname,$udom,$counter,$partid,$record) = @_;
1.381     albertel 1684:     my $checkIcon = '<img alt="'.&mt('Check Mark').
1.485     albertel 1685: 	'" src="'.&Apache::loncommon::lonhttpdurl($request->dir_config('lonIconsURL').'/check.gif').'" height="16" border="0" />';
1.71      ng       1686:     my $wgt    = &Apache::lonnet::EXT('resource.'.$partid.'.weight',$symb,$udom,$uname);
1.466     albertel 1687:     my $wgtmsg = ($wgt > 0) ? &mt('(problem weight)') 
                   1688:                            : '<span class="LC_info">'.&mt('problem weight assigned by computer').'</span>';
1.71      ng       1689:     $wgt       = ($wgt > 0 ? $wgt : '1');
                   1690:     my $score  = ($$record{'resource.'.$partid.'.awarded'} eq '' ?
1.320     albertel 1691: 		  '' : &compute_points($$record{'resource.'.$partid.'.awarded'},$wgt));
1.71      ng       1692:     my $result='<input type="hidden" name="WGT'.$counter.'_'.$partid.'" value="'.$wgt.'" />'."\n";
1.466     albertel 1693:     my $display_part= &get_display_part($partid,$symb);
1.270     albertel 1694:     my %last_resets = &get_last_resets($symb,$env{'request.course.id'},
                   1695: 				       [$partid]);
                   1696:     my $aggtries = $$record{'resource.'.$partid.'.tries'};
1.269     raeburn  1697:     if ($last_resets{$partid}) {
                   1698:         $aggtries = &get_num_tries($record,$last_resets{$partid},$partid);
                   1699:     }
1.485     albertel 1700:     $result.='<table border="0"><tr>';
1.71      ng       1701:     my $ctr = 0;
1.348     bowersj2 1702:     my $thisweight = 0;
1.349     albertel 1703:     my $increment = &get_increment();
1.485     albertel 1704: 
                   1705:     my $radio.='<table border="0"><tr>'."\n";  # display radio buttons in a nice table 10 across
1.348     bowersj2 1706:     while ($thisweight<=$wgt) {
1.532     bisitz   1707: 	$radio.= '<td><span class="LC_nobreak"><label><input type="radio" name="RADVAL'.$counter.'_'.$partid.'" '.
1.71      ng       1708: 	    'onclick="javascript:writeBox(this.form,\''.$counter.'_'.$partid.'\','.
1.348     bowersj2 1709: 	    $thisweight.')" value="'.$thisweight.'" '.
1.401     albertel 1710: 	    ($score eq $thisweight ? 'checked="checked"':'').' /> '.$thisweight."</label></span></td>\n";
1.485     albertel 1711: 	$radio.=(($ctr+1)%10 == 0 ? '</tr><tr>' : '');
1.348     bowersj2 1712:         $thisweight += $increment;
1.71      ng       1713: 	$ctr++;
                   1714:     }
1.485     albertel 1715:     $radio.='</tr></table>';
                   1716: 
                   1717:     my $line.='<input type="text" name="GD_BOX'.$counter.'_'.$partid.'"'.
1.71      ng       1718: 	($score ne ''? ' value = "'.$score.'"':'').' size="4" '.
                   1719: 	'onChange="javascript:updateRadio(this.form,\''.$counter.'_'.$partid.'\','.
                   1720: 	$wgt.')" /></td>'."\n";
1.485     albertel 1721:     $line.='<td>/'.$wgt.' '.$wgtmsg.
1.71      ng       1722: 	($$record{'resource.'.$partid.'.solved'} eq 'correct_by_student' ? '&nbsp;'.$checkIcon : '').
1.540     riegler  1723: 	' </td><td><b>'.&mt('Grade Status').':</b>'."\n";
1.485     albertel 1724:     $line.='<select name="GD_SEL'.$counter.'_'.$partid.'" '.
1.71      ng       1725: 	'onChange="javascript:clearRadBox(this.form,\''.$counter.'_'.$partid.'\')" >'."\n";
                   1726:     if ($$record{'resource.'.$partid.'.solved'} eq 'excused') {
1.485     albertel 1727: 	$line.='<option></option>'.
                   1728: 	    '<option value="excused" selected="selected">'.&mt('excused').'</option>';
1.71      ng       1729:     } else {
1.485     albertel 1730: 	$line.='<option selected="selected"></option>'.
                   1731: 	    '<option value="excused" >'.&mt('excused').'</option>';
1.71      ng       1732:     }
1.485     albertel 1733:     $line.='<option value="reset status">'.&mt('reset status').'</option></select>'."\n";
                   1734: 
                   1735: 
1.540     riegler  1736: 	#&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);
1.485     albertel 1737:     $result .= 
1.577     bisitz   1738: 	    '<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>'.
1.485     albertel 1739:     
                   1740:     $result.='</tr></table>'."\n";
1.71      ng       1741:     $result.='<input type="hidden" name="stores'.$counter.'_'.$partid.'" value="" />'."\n".
                   1742: 	'<input type="hidden" name="oldpts'.$counter.'_'.$partid.'" value="'.$score.'" />'."\n".
                   1743: 	'<input type="hidden" name="solved'.$counter.'_'.$partid.'" value="'.
1.269     raeburn  1744: 	$$record{'resource.'.$partid.'.solved'}.'" />'."\n".
                   1745:         '<input type="hidden" name="totaltries'.$counter.'_'.$partid.'" value="'.
                   1746:         $$record{'resource.'.$partid.'.tries'}.'" />'."\n".
                   1747:         '<input type="hidden" name="aggtries'.$counter.'_'.$partid.'" value="'.
                   1748:         $aggtries.'" />'."\n";
1.323     banghart 1749:     $result.=&handback_box($symb,$uname,$udom,$counter,$partid,$record);
1.318     banghart 1750:     return $result;
                   1751: }
1.322     albertel 1752: 
                   1753: sub handback_box {
1.323     banghart 1754:     my ($symb,$uname,$udom,$counter,$partid,$record) = @_;
1.324     albertel 1755:     my ($partlist,$handgrade,$responseType) = &response_type($symb);
1.323     banghart 1756:     my (@respids);
1.375     albertel 1757:      my @part_response_id = &flatten_responseType($responseType);
                   1758:     foreach my $part_response_id (@part_response_id) {
                   1759:     	my ($part,$resp) = @{ $part_response_id };
1.323     banghart 1760:         if ($part eq $partid) {
1.375     albertel 1761:             push(@respids,$resp);
1.323     banghart 1762:         }
                   1763:     }
1.318     banghart 1764:     my $result;
1.323     banghart 1765:     foreach my $respid (@respids) {
1.322     albertel 1766: 	my $prefix = $counter.'_'.$partid.'_'.$respid.'_';
                   1767: 	my $files=&get_submitted_files($udom,$uname,$partid,$respid,$record);
                   1768: 	next if (!@$files);
                   1769: 	my $file_counter = 1;
1.313     banghart 1770: 	foreach my $file (@$files) {
1.368     banghart 1771: 	    if ($file =~ /\/portfolio\//) {
                   1772:     	        my ($file_path, $file_disp) = ($file =~ m|(.+/)(.+)$|);
                   1773:     	        my ($name,$version,$ext) = &file_name_version_ext($file_disp);
                   1774:     	        $file_disp = "$name.$ext";
                   1775:     	        $file = $file_path.$file_disp;
                   1776:     	        $result.=&mt('Return commented version of [_1] to student.',
                   1777:     			 '<span class="LC_filename">'.$file_disp.'</span>');
                   1778:     	        $result.='<input type="file"   name="'.$prefix.'returndoc'.$file_counter.'" />'."\n";
                   1779:     	        $result.='<input type="hidden" name="'.$prefix.'origdoc'.$file_counter.'" value="'.$file.'" /><br />';
1.485     albertel 1780:     	        $result.='('.&mt('File will be uploaded when you click on Save &amp; Next below.').')<br />';
1.368     banghart 1781:     	        $file_counter++;
                   1782: 	    }
1.322     albertel 1783: 	}
1.313     banghart 1784:     }
1.318     banghart 1785:     return $result;    
1.71      ng       1786: }
1.44      ng       1787: 
1.58      albertel 1788: sub show_problem {
1.382     albertel 1789:     my ($request,$symb,$uname,$udom,$removeform,$viewon,$mode,$form) = @_;
1.144     albertel 1790:     my $rendered;
1.382     albertel 1791:     my %form = ((ref($form) eq 'HASH')? %{$form} : ());
1.329     albertel 1792:     &Apache::lonxml::remember_problem_counter();
1.144     albertel 1793:     if ($mode eq 'both' or $mode eq 'text') {
                   1794: 	$rendered=&Apache::loncommon::get_student_view($symb,$uname,$udom,
1.382     albertel 1795: 						       $env{'request.course.id'},
                   1796: 						       undef,\%form);
1.144     albertel 1797:     }
1.58      albertel 1798:     if ($removeform) {
                   1799: 	$rendered=~s|<form(.*?)>||g;
                   1800: 	$rendered=~s|</form>||g;
1.374     albertel 1801: 	$rendered=~s|(<input[^>]*name\s*=\s*"?)(\w+)("?)|$1would_have_been_$2$3|g;
1.58      albertel 1802:     }
1.144     albertel 1803:     my $companswer;
                   1804:     if ($mode eq 'both' or $mode eq 'answer') {
1.329     albertel 1805: 	&Apache::lonxml::restore_problem_counter();
1.382     albertel 1806: 	$companswer=
                   1807: 	    &Apache::loncommon::get_student_answers($symb,$uname,$udom,
                   1808: 						    $env{'request.course.id'},
                   1809: 						    %form);
1.144     albertel 1810:     }
1.58      albertel 1811:     if ($removeform) {
                   1812: 	$companswer=~s|<form(.*?)>||g;
                   1813: 	$companswer=~s|</form>||g;
1.144     albertel 1814: 	$companswer=~s|name="submit"|name="would_have_been_submit"|g;
1.58      albertel 1815:     }
1.468     albertel 1816:     $rendered=
                   1817: 	'<div class="LC_grade_show_problem_header">'.
                   1818: 	&mt('View of the problem').
                   1819: 	'</div><div class="LC_grade_show_problem_problem">'.
                   1820: 	$rendered.
                   1821: 	'</div>';
                   1822:     $companswer=
                   1823: 	'<div class="LC_grade_show_problem_header">'.
                   1824: 	&mt('Correct answer').
                   1825: 	'</div><div class="LC_grade_show_problem_problem">'.
                   1826: 	$companswer.
                   1827: 	'</div>';
                   1828:     my $result;
1.144     albertel 1829:     if ($mode eq 'both') {
1.468     albertel 1830: 	$result=$rendered.$companswer;
1.144     albertel 1831:     } elsif ($mode eq 'text') {
1.468     albertel 1832: 	$result=$rendered;
1.144     albertel 1833:     } elsif ($mode eq 'answer') {
1.468     albertel 1834: 	$result=$companswer;
1.144     albertel 1835:     }
1.468     albertel 1836:     $result='<div class="LC_grade_show_problem">'.$result.'</div>';
1.71      ng       1837:     return $result;
1.58      albertel 1838: }
1.397     albertel 1839: 
1.396     banghart 1840: sub files_exist {
                   1841:     my ($r, $symb) = @_;
                   1842:     my @students = &Apache::loncommon::get_env_multiple('form.stuinfo');
1.397     albertel 1843: 
1.396     banghart 1844:     foreach my $student (@students) {
                   1845:         my ($uname,$udom,$fullname) = split(/:/,$student);
1.397     albertel 1846:         my %record = &Apache::lonnet::restore($symb,$env{'request.course.id'},
                   1847: 					      $udom,$uname);
1.396     banghart 1848:         my ($string,$timestamp)= &get_last_submission(\%record);
1.397     albertel 1849:         foreach my $submission (@$string) {
                   1850:             my ($partid,$respid) =
                   1851: 		($submission =~ /^resource\.([^\.]*)\.([^\.]*)\.submission/);
                   1852:             my $files=&get_submitted_files($udom,$uname,$partid,$respid,
                   1853: 					   \%record);
                   1854:             return 1 if (@$files);
1.396     banghart 1855:         }
                   1856:     }
1.397     albertel 1857:     return 0;
1.396     banghart 1858: }
1.397     albertel 1859: 
1.394     banghart 1860: sub download_all_link {
                   1861:     my ($r,$symb) = @_;
1.395     albertel 1862:     my $all_students = 
                   1863: 	join("\n", &Apache::loncommon::get_env_multiple('form.stuinfo'));
                   1864: 
                   1865:     my $parts =
                   1866: 	join("\n",&Apache::loncommon::get_env_multiple('form.vPart'));
                   1867: 
1.394     banghart 1868:     my $identifier = &Apache::loncommon::get_cgi_id();
1.514     raeburn  1869:     &Apache::lonnet::appenv({'cgi.'.$identifier.'.students' => $all_students,
                   1870:                              'cgi.'.$identifier.'.symb' => $symb,
                   1871:                              'cgi.'.$identifier.'.parts' => $parts,});
1.395     albertel 1872:     $r->print('<a href="/cgi-bin/multidownload.pl?'.$identifier.'">'.
                   1873: 	      &mt('Download All Submitted Documents').'</a>');
1.394     banghart 1874:     return
                   1875: }
1.395     albertel 1876: 
1.432     banghart 1877: sub build_section_inputs {
                   1878:     my $section_inputs;
                   1879:     if ($env{'form.section'} eq '') {
                   1880:         $section_inputs .= '<input type="hidden" name="section" value="all" />'."\n";
                   1881:     } else {
                   1882:         my @sections = &Apache::loncommon::get_env_multiple('form.section');
1.434     albertel 1883:         foreach my $section (@sections) {
1.432     banghart 1884:             $section_inputs .= '<input type="hidden" name="section" value="'.$section.'" />'."\n";
                   1885:         }
                   1886:     }
                   1887:     return $section_inputs;
                   1888: }
                   1889: 
1.44      ng       1890: # --------------------------- show submissions of a student, option to grade 
                   1891: sub submission {
                   1892:     my ($request,$counter,$total) = @_;
1.257     albertel 1893:     my ($uname,$udom)     = ($env{'form.student'},$env{'form.userdom'});
                   1894:     $udom = ($udom eq '' ? $env{'user.domain'} : $udom); #has form.userdom changed for a student?
                   1895:     my $usec = &Apache::lonnet::getsection($udom,$uname,$env{'request.course.id'});
                   1896:     $env{'form.fullname'} = &Apache::loncommon::plainname($uname,$udom,'lastname') if $env{'form.fullname'} eq '';
1.324     albertel 1897:     my $symb = &get_symb($request); 
                   1898:     if ($symb eq '') { $request->print("Unable to handle ambiguous references:."); return ''; }
1.104     albertel 1899: 
                   1900:     if (!&canview($usec)) {
1.398     albertel 1901: 	$request->print('<span class="LC_warning">Unable to view requested student.('.
                   1902: 			$uname.':'.$udom.' in section '.$usec.' in course id '.
                   1903: 			$env{'request.course.id'}.')</span>');
1.324     albertel 1904: 	$request->print(&show_grading_menu_form($symb));
1.104     albertel 1905: 	return;
                   1906:     }
                   1907: 
1.257     albertel 1908:     if (!$env{'form.lastSub'}) { $env{'form.lastSub'} = 'datesub'; }
                   1909:     if (!$env{'form.vProb'}) { $env{'form.vProb'} = 'yes'; }
                   1910:     if (!$env{'form.vAns'}) { $env{'form.vAns'} = 'yes'; }
                   1911:     my $last = ($env{'form.lastSub'} eq 'last' ? 'last' : '');
1.381     albertel 1912:     my $checkIcon = '<img alt="'.&mt('Check Mark').
                   1913: 	'" src="'.$request->dir_config('lonIconsURL').
1.122     ng       1914: 	'/check.gif" height="16" border="0" />';
1.41      ng       1915: 
1.426     albertel 1916:     my %old_essays;
1.41      ng       1917:     # header info
                   1918:     if ($counter == 0) {
                   1919: 	&sub_page_js($request);
1.257     albertel 1920: 	&sub_page_kw_js($request) if ($env{'form.handgrade'} eq 'yes');
                   1921: 	$env{'form.probTitle'} = $env{'form.probTitle'} eq '' ? 
                   1922: 	    &Apache::lonnet::gettitle($symb) : $env{'form.probTitle'};
1.397     albertel 1923: 	if ($env{'form.handgrade'} eq 'yes' && &files_exist($request, $symb)) {
1.396     banghart 1924: 	    &download_all_link($request, $symb);
                   1925: 	}
1.485     albertel 1926: 	$request->print('<h3>&nbsp;<span class="LC_info">'.&mt('Submission Record').'</span></h3>'."\n".
                   1927: 			'<h4>&nbsp;'.&mt('<b>Resource: </b> [_1]',$env{'form.probTitle'}).'</h4>'."\n");
1.118     ng       1928: 
1.44      ng       1929: 	# option to display problem, only once else it cause problems 
                   1930:         # with the form later since the problem has a form.
1.257     albertel 1931: 	if ($env{'form.vProb'} eq 'yes' or $env{'form.vAns'} eq 'yes') {
1.144     albertel 1932: 	    my $mode;
1.257     albertel 1933: 	    if ($env{'form.vProb'} eq 'yes' && $env{'form.vAns'} eq 'yes') {
1.144     albertel 1934: 		$mode='both';
1.257     albertel 1935: 	    } elsif ($env{'form.vProb'} eq 'yes') {
1.144     albertel 1936: 		$mode='text';
1.257     albertel 1937: 	    } elsif ($env{'form.vAns'} eq 'yes') {
1.144     albertel 1938: 		$mode='answer';
                   1939: 	    }
1.329     albertel 1940: 	    &Apache::lonxml::clear_problem_counter();
1.144     albertel 1941: 	    $request->print(&show_problem($request,$symb,$uname,$udom,0,1,$mode));
1.41      ng       1942: 	}
1.441     www      1943: 
1.44      ng       1944: 	# kwclr is the only variable that is guaranteed to be non blank 
                   1945:         # if this subroutine has been called once.
1.41      ng       1946: 	my %keyhash = ();
1.257     albertel 1947: 	if ($env{'form.kwclr'} eq '' && $env{'form.handgrade'} eq 'yes') {
1.41      ng       1948: 	    %keyhash = &Apache::lonnet::dump('nohist_handgrade',
1.257     albertel 1949: 					     $env{'course.'.$env{'request.course.id'}.'.domain'},
                   1950: 					     $env{'course.'.$env{'request.course.id'}.'.num'});
1.41      ng       1951: 
1.257     albertel 1952: 	    my $loginuser = $env{'user.name'}.':'.$env{'user.domain'};
                   1953: 	    $env{'form.keywords'} = $keyhash{$symb.'_keywords'} ne '' ? $keyhash{$symb.'_keywords'} : '';
                   1954: 	    $env{'form.kwclr'}    = $keyhash{$loginuser.'_kwclr'} ne '' ? $keyhash{$loginuser.'_kwclr'} : 'red';
                   1955: 	    $env{'form.kwsize'}   = $keyhash{$loginuser.'_kwsize'} ne '' ? $keyhash{$loginuser.'_kwsize'} : '0';
                   1956: 	    $env{'form.kwstyle'}  = $keyhash{$loginuser.'_kwstyle'} ne '' ? $keyhash{$loginuser.'_kwstyle'} : '';
                   1957: 	    $env{'form.msgsub'}   = $keyhash{$symb.'_subject'} ne '' ? 
                   1958: 		$keyhash{$symb.'_subject'} : $env{'form.probTitle'};
                   1959: 	    $env{'form.savemsgN'} = $keyhash{$symb.'_savemsgN'} ne '' ? $keyhash{$symb.'_savemsgN'} : '0';
1.41      ng       1960: 	}
1.257     albertel 1961: 	my $overRideScore = $env{'form.overRideScore'} eq '' ? 'no' : $env{'form.overRideScore'};
1.442     banghart 1962: 	my $stu_status = join(':',&Apache::loncommon::get_env_multiple('form.Status'));
1.303     banghart 1963: 	$request->print('<form action="/adm/grades" method="post" name="SCORE" enctype="multipart/form-data">'."\n".
1.41      ng       1964: 			'<input type="hidden" name="command"    value="handgrade" />'."\n".
1.257     albertel 1965: 			'<input type="hidden" name="saveState"  value="'.$env{'form.saveState'}.'" />'."\n".
1.442     banghart 1966: 			'<input type="hidden" name="Status"     value="'.$stu_status.'" />'."\n".
1.120     ng       1967: 			'<input type="hidden" name="overRideScore" value="'.$overRideScore.'" />'."\n".
1.257     albertel 1968: 			'<input type="hidden" name="probTitle"  value="'.$env{'form.probTitle'}.'" />'."\n".
1.41      ng       1969: 			'<input type="hidden" name="refresh"    value="off" />'."\n".
1.120     ng       1970: 			'<input type="hidden" name="studentNo"  value="" />'."\n".
                   1971: 			'<input type="hidden" name="gradeOpt"   value="" />'."\n".
1.418     albertel 1972: 			'<input type="hidden" name="symb"       value="'.&Apache::lonenc::check_encrypt($symb).'" />'."\n".
1.257     albertel 1973: 			'<input type="hidden" name="showgrading" value="'.$env{'form.showgrading'}.'" />'."\n".
                   1974: 			'<input type="hidden" name="vProb"      value="'.$env{'form.vProb'}.'" />'."\n".
                   1975: 			'<input type="hidden" name="vAns"       value="'.$env{'form.vAns'}.'" />'."\n".
                   1976: 			'<input type="hidden" name="lastSub"    value="'.$env{'form.lastSub'}.'" />'."\n".
1.432     banghart 1977: 			&build_section_inputs().
1.326     albertel 1978: 			'<input type="hidden" name="submitonly" value="'.$env{'form.submitonly'}.'" />'."\n".
                   1979: 			'<input type="hidden" name="handgrade"  value="'.$env{'form.handgrade'}.'" />'."\n".
1.41      ng       1980: 			'<input type="hidden" name="NCT"'.
1.257     albertel 1981: 			' value="'.($env{'form.NTSTU'} ne '' ? $env{'form.NTSTU'} : $total+1).'" />'."\n");
                   1982: 	if ($env{'form.handgrade'} eq 'yes') {
                   1983: 	    $request->print('<input type="hidden" name="keywords" value="'.$env{'form.keywords'}.'" />'."\n".
                   1984: 			    '<input type="hidden" name="kwclr"    value="'.$env{'form.kwclr'}.'" />'."\n".
                   1985: 			    '<input type="hidden" name="kwsize"   value="'.$env{'form.kwsize'}.'" />'."\n".
                   1986: 			    '<input type="hidden" name="kwstyle"  value="'.$env{'form.kwstyle'}.'" />'."\n".
                   1987: 			    '<input type="hidden" name="msgsub"   value="'.$env{'form.msgsub'}.'" />'."\n".
1.123     ng       1988: 			    '<input type="hidden" name="shownSub" value="0" />'."\n".
1.257     albertel 1989: 			    '<input type="hidden" name="savemsgN" value="'.$env{'form.savemsgN'}.'" />'."\n");
1.154     albertel 1990: 	    foreach my $partid (&Apache::loncommon::get_env_multiple('form.vPart')) {
                   1991: 		$request->print('<input type="hidden" name="vPart" value="'.$partid.'" />'."\n");
                   1992: 	    }
1.123     ng       1993: 	}
1.41      ng       1994: 	
                   1995: 	my ($cts,$prnmsg) = (1,'');
1.257     albertel 1996: 	while ($cts <= $env{'form.savemsgN'}) {
1.41      ng       1997: 	    $prnmsg.='<input type="hidden" name="savemsg'.$cts.'" value="'.
1.123     ng       1998: 		(!exists($keyhash{$symb.'_savemsg'.$cts}) ? 
1.257     albertel 1999: 		 &Apache::lonfeedback::clear_out_html($env{'form.savemsg'.$cts}) :
1.80      ng       2000: 		 &Apache::lonfeedback::clear_out_html($keyhash{$symb.'_savemsg'.$cts})).
1.123     ng       2001: 		'" />'."\n".
                   2002: 		'<input type="hidden" name="shownOnce'.$cts.'" value="0" />'."\n";
1.41      ng       2003: 	    $cts++;
                   2004: 	}
                   2005: 	$request->print($prnmsg);
1.32      ng       2006: 
1.257     albertel 2007: 	if ($env{'form.handgrade'} eq 'yes' && $env{'form.showgrading'} eq 'yes') {
1.88      www      2008: #
                   2009: # Print out the keyword options line
                   2010: #
1.41      ng       2011: 	    $request->print(<<KEYWORDS);
1.38      ng       2012: &nbsp;<b>Keyword Options:</b>&nbsp;
1.417     albertel 2013: <a href="javascript:keywords(document.SCORE);" target="_self">List</a>&nbsp; &nbsp;
1.38      ng       2014: <a href="#" onMouseDown="javascript:getSel(); return false"
                   2015:  CLASS="page">Paste Selection to List</a>&nbsp; &nbsp;
1.417     albertel 2016: <a href="javascript:kwhighlight();" target="_self">Highlight Attribute</a><br /><br />
1.38      ng       2017: KEYWORDS
1.88      www      2018: #
                   2019: # Load the other essays for similarity check
                   2020: #
1.324     albertel 2021:             my (undef,undef,$essayurl) = &Apache::lonnet::decode_symb($symb);
1.384     albertel 2022: 	    my ($adom,$aname,$apath)=($essayurl=~/^($LONCAPA::domain_re)\/($LONCAPA::username_re)\/(.*)$/);
1.359     www      2023: 	    $apath=&escape($apath);
1.88      www      2024: 	    $apath=~s/\W/\_/gs;
1.426     albertel 2025: 	    %old_essays=&Apache::lonnet::dump('nohist_essay_'.$apath,$adom,$aname);
1.41      ng       2026:         }
                   2027:     }
1.44      ng       2028: 
1.441     www      2029: # This is where output for one specific student would start
1.468     albertel 2030:     my $add_class = ($counter%2) ? 'LC_grade_show_user_odd_row' : '';
1.441     www      2031:     $request->print("\n\n".
1.468     albertel 2032:                     '<div class="LC_grade_show_user '.$add_class.'">'.
                   2033: 		    '<div class="LC_grade_user_name">'.&nameUserString(undef,$env{'form.fullname'},$uname,$udom).'</div>'.
                   2034: 		    '<div class="LC_grade_show_user_body">'."\n");
1.441     www      2035: 
1.257     albertel 2036:     if ($env{'form.vProb'} eq 'all' or $env{'form.vAns'} eq 'all') {
1.144     albertel 2037: 	my $mode;
1.257     albertel 2038: 	if ($env{'form.vProb'} eq 'all' && $env{'form.vAns'} eq 'all') {
1.144     albertel 2039: 	    $mode='both';
1.257     albertel 2040: 	} elsif ($env{'form.vProb'} eq 'all' ) {
1.144     albertel 2041: 	    $mode='text';
1.257     albertel 2042: 	} elsif ($env{'form.vAns'} eq 'all') {
1.144     albertel 2043: 	    $mode='answer';
                   2044: 	}
1.329     albertel 2045: 	&Apache::lonxml::clear_problem_counter();
1.475     albertel 2046: 	$request->print(&show_problem($request,$symb,$uname,$udom,1,1,$mode,{'request.prefix' => 'ctr'.$counter}));
1.58      albertel 2047:     }
1.144     albertel 2048: 
1.257     albertel 2049:     my %record = &Apache::lonnet::restore($symb,$env{'request.course.id'},$udom,$uname);
1.324     albertel 2050:     my ($partlist,$handgrade,$responseType) = &response_type($symb);
1.41      ng       2051: 
1.44      ng       2052:     # Display student info
1.41      ng       2053:     $request->print(($counter == 0 ? '' : '<br />'));
1.468     albertel 2054:     my $result='<div class="LC_grade_submissions">';
                   2055:     
                   2056:     $result.='<div class="LC_grade_submissions_header">';
                   2057:     $result.= &mt('Submissions');
1.45      ng       2058:     $result.='<input type="hidden" name="name'.$counter.
1.257     albertel 2059: 	'" value="'.$env{'form.fullname'}.'" />'."\n";
1.469     albertel 2060:     if ($env{'form.handgrade'} eq 'no') {
                   2061: 	$result.='<span class="LC_grade_check_note">'.
                   2062: 	    &mt('Part(s) graded correct by the computer is marked with a [_1] symbol.',$checkIcon)."</span>\n";
                   2063: 
                   2064:     }
                   2065: 
                   2066: 
1.41      ng       2067: 
1.118     ng       2068:     # If any part of the problem is an essay-response (handgraded), then check for collaborators
1.464     albertel 2069:     my $fullname;
                   2070:     my $col_fullnames = [];
1.257     albertel 2071:     if ($env{'form.handgrade'} eq 'yes') {
1.464     albertel 2072: 	(my $sub_result,$fullname,$col_fullnames)=
                   2073: 	    &check_collaborators($symb,$uname,$udom,\%record,$handgrade,
                   2074: 				 $counter);
                   2075: 	$result.=$sub_result;
1.41      ng       2076:     }
1.44      ng       2077:     $request->print($result."\n");
1.468     albertel 2078:     $request->print('</div>'."\n");
1.44      ng       2079:     # print student answer/submission
                   2080:     # Options are (1) Handgaded submission only
                   2081:     #             (2) Last submission, includes submission that is not handgraded 
                   2082:     #                  (for multi-response type part)
                   2083:     #             (3) Last submission plus the parts info
                   2084:     #             (4) The whole record for this student
1.257     albertel 2085:     if ($env{'form.lastSub'} =~ /^(lastonly|hdgrade)$/) {
1.151     albertel 2086: 	my ($string,$timestamp)= &get_last_submission(\%record);
1.468     albertel 2087: 	
                   2088: 	my $lastsubonly;
                   2089: 
1.151     albertel 2090: 	if ($$timestamp eq '') {
1.468     albertel 2091: 	    $lastsubonly.='<div class="LC_grade_submissions_body">'.$$string[0].'</div>'; 
1.151     albertel 2092: 	} else {
1.468     albertel 2093: 	    $lastsubonly = '<div class="LC_grade_submissions_body"> <b>Date Submitted:</b> '.$$timestamp."\n";
                   2094: 
1.151     albertel 2095: 	    my %seenparts;
1.375     albertel 2096: 	    my @part_response_id = &flatten_responseType($responseType);
                   2097: 	    foreach my $part (@part_response_id) {
1.393     albertel 2098: 		next if ($env{'form.lastSub'} eq 'hdgrade' 
                   2099: 			 && $$handgrade{$$part[0].'_'.$$part[1]} ne 'yes');
                   2100: 
1.375     albertel 2101: 		my ($partid,$respid) = @{ $part };
1.324     albertel 2102: 		my $display_part=&get_display_part($partid,$symb);
1.257     albertel 2103: 		if ($env{"form.$uname:$udom:$partid:submitted_by"}) {
1.151     albertel 2104: 		    if (exists($seenparts{$partid})) { next; }
                   2105: 		    $seenparts{$partid}=1;
1.207     albertel 2106: 		    my $submitby='<b>Part:</b> '.$display_part.
                   2107: 			' <b>Collaborative submission by:</b> '.
1.151     albertel 2108: 			'<a href="javascript:viewSubmitter(\''.
1.257     albertel 2109: 			$env{"form.$uname:$udom:$partid:submitted_by"}.
1.417     albertel 2110: 			'\');" target="_self">'.
1.257     albertel 2111: 			$$fullname{$env{"form.$uname:$udom:$partid:submitted_by"}}.'</a><br />';
1.151     albertel 2112: 		    $request->print($submitby);
                   2113: 		    next;
                   2114: 		}
                   2115: 		my $responsetype = $responseType->{$partid}->{$respid};
                   2116: 		if (!exists($record{"resource.$partid.$respid.submission"})) {
1.577     bisitz   2117:                     $lastsubonly.="\n".'<div class="LC_grade_submission_part">'.
                   2118:                         '<b>'.&mt('Part: [_1]',$display_part).'</b>'.
                   2119:                         ' <span class="LC_internal_info">'.
                   2120:                         '('.&mt('Part ID: [_1]',$respid).')</b>'.
                   2121:                         '</span>&nbsp; &nbsp;'.
1.539     riegler  2122: 			'<span class="LC_warning">'.&mt('Nothing submitted - no attempts.').'</span><br /><br /></div>';
1.151     albertel 2123: 		    next;
                   2124: 		}
1.468     albertel 2125: 		foreach my $submission (@$string) {
                   2126: 		    my ($partid,$respid) = ($submission =~ /^resource\.([^\.]*)\.([^\.]*)\.submission/);
1.375     albertel 2127: 		    if (join('_',@{$part}) ne ($partid.'_'.$respid)) { next; }
1.468     albertel 2128: 		    my ($ressub,$subval) = split(/:/,$submission,2);
1.151     albertel 2129: 		    # Similarity check
                   2130: 		    my $similar='';
1.257     albertel 2131: 		    if($env{'form.checkPlag'}){
1.151     albertel 2132: 			my ($oname,$odom,$ocrsid,$oessay,$osim)=
1.426     albertel 2133: 			    &most_similar($uname,$udom,$subval,\%old_essays);
1.151     albertel 2134: 			if ($osim) {
                   2135: 			    $osim=int($osim*100.0);
1.426     albertel 2136: 			    my %old_course_desc = 
                   2137: 				&Apache::lonnet::coursedescription($ocrsid,
                   2138: 								   {'one_time' => 1});
                   2139: 
                   2140: 			    $similar="<hr /><h3><span class=\"LC_warning\">".
1.574     bisitz   2141: 				&mt('Essay is [_1]% similar to an essay by [_2] in course [_3] (course id [_4]:[_5])',
1.426     albertel 2142: 				    $osim,
1.574     bisitz   2143: 				    &Apache::loncommon::plainname($oname,$odom).' ('.$oname.':'.$odom.')',
1.426     albertel 2144: 				    $old_course_desc{'description'},
1.427     albertel 2145: 				    $old_course_desc{'num'},
1.426     albertel 2146: 				    $old_course_desc{'domain'}).
1.398     albertel 2147: 				'</span></h3><blockquote><i>'.
1.151     albertel 2148: 				&keywords_highlight($oessay).
                   2149: 				'</i></blockquote><hr />';
                   2150: 			}
1.150     albertel 2151: 		    }
1.151     albertel 2152: 		    my $order=&get_order($partid,$respid,$symb,$uname,$udom);
1.257     albertel 2153: 		    if ($env{'form.lastSub'} eq 'lastonly' || 
                   2154: 			($env{'form.lastSub'} eq 'hdgrade' && 
1.377     albertel 2155: 			 $$handgrade{$$part[0].'_'.$$part[1]} eq 'yes')) {
1.324     albertel 2156: 			my $display_part=&get_display_part($partid,$symb);
1.577     bisitz   2157:                         $lastsubonly.='<div class="LC_grade_submission_part">'.
                   2158:                             '<b>'.&mt('Part: [_1]',$display_part).'</b>'.
                   2159:                             ' <span class="LC_internal_info">'.
                   2160:                             '('.&mt('Part ID: [_1]',$respid).')'.
                   2161:                             '</b></span>&nbsp; &nbsp;';
1.313     banghart 2162: 			my $files=&get_submitted_files($udom,$uname,$partid,$respid,\%record);
                   2163: 			if (@$files) {
1.544     raeburn  2164: 			    $lastsubonly.='<br /><span class="LC_warning">'.&mt('Like all files provided by users, this file may contain viruses').'</span><br />';
1.303     banghart 2165: 			    my $file_counter = 0;
1.313     banghart 2166: 			    foreach my $file (@$files) {
1.468     albertel 2167: 			        $file_counter++;
1.232     albertel 2168: 				&Apache::lonnet::allowuploaded('/adm/grades',$file);
1.564     bisitz   2169: 				$lastsubonly.='<br /><a href="'.$file.'?rawmode=1" target="lonGRDs"><img src="'.&Apache::loncommon::icon($file).'" border="0" /> '.$file.'</a>';
1.232     albertel 2170: 			    }
1.236     albertel 2171: 			    $lastsubonly.='<br />';
1.41      ng       2172: 			}
1.468     albertel 2173: 			$lastsubonly.='<b>'.&mt('Submitted Answer:').' </b>'.
1.151     albertel 2174: 			    &cleanRecord($subval,$responsetype,$symb,$partid,
1.555     raeburn  2175: 					 $respid,\%record,$order,undef,$uname,$udom);
1.151     albertel 2176: 			if ($similar) {$lastsubonly.="<br /><br />$similar\n";}
1.468     albertel 2177: 			$lastsubonly.='</div>';
1.41      ng       2178: 		    }
                   2179: 		}
                   2180: 	    }
1.468     albertel 2181: 	    $lastsubonly.='</div>'."\n";
1.151     albertel 2182: 	}
                   2183: 	$request->print($lastsubonly);
1.468     albertel 2184:    } elsif ($env{'form.lastSub'} eq 'datesub') {
1.324     albertel 2185: 	my (undef,$responseType,undef,$parts) = &showResourceInfo($symb);
1.148     albertel 2186: 	$request->print(&displaySubByDates($symb,\%record,$parts,$responseType,$checkIcon,$uname,$udom));
1.257     albertel 2187:     } elsif ($env{'form.lastSub'} =~ /^(last|all)$/) {
1.41      ng       2188: 	$request->print(&Apache::loncommon::get_previous_attempt($symb,$uname,$udom,
1.257     albertel 2189: 								 $env{'request.course.id'},
1.44      ng       2190: 								 $last,'.submission',
                   2191: 								 'Apache::grades::keywords_highlight'));
1.41      ng       2192:     }
1.120     ng       2193: 
1.121     ng       2194:     $request->print('<input type="hidden" name="unamedom'.$counter.'" value="'.$uname.':'
                   2195: 	.$udom.'" />'."\n");
1.44      ng       2196:     # return if view submission with no grading option
1.257     albertel 2197:     if ($env{'form.showgrading'} eq '' || (!&canmodify($usec))) {
1.120     ng       2198: 	my $toGrade.='<input type="button" value="Grade Student" '.
1.121     ng       2199: 	    'onClick="javascript:checksubmit(this.form,\'Grade Student\',\''
1.417     albertel 2200: 	    .$counter.'\');" target="_self" /> &nbsp;'."\n" if (&canmodify($usec));
1.468     albertel 2201: 	$toGrade.='</div>'."\n";
1.257     albertel 2202: 	if (($env{'form.command'} eq 'submission') || 
                   2203: 	    ($env{'form.command'} eq 'processGroup' && $counter == $total)) {
1.324     albertel 2204: 	    $toGrade.='</form>'.&show_grading_menu_form($symb); 
1.169     albertel 2205: 	}
1.180     albertel 2206: 	$request->print($toGrade);
1.41      ng       2207: 	return;
1.180     albertel 2208:     } else {
1.468     albertel 2209: 	$request->print('</div>'."\n");
1.41      ng       2210:     }
1.33      ng       2211: 
1.121     ng       2212:     # essay grading message center
1.257     albertel 2213:     if ($env{'form.handgrade'} eq 'yes') {
1.468     albertel 2214: 	my $result='<div class="LC_grade_message_center">';
                   2215:     
                   2216: 	$result.='<div class="LC_grade_message_center_header">'.
                   2217: 	    &mt('Send Message').'</div><div class="LC_grade_message_center_body">';
1.257     albertel 2218: 	my ($lastname,$givenn) = split(/,/,$env{'form.fullname'});
1.118     ng       2219: 	my $msgfor = $givenn.' '.$lastname;
1.464     albertel 2220: 	if (scalar(@$col_fullnames) > 0) {
                   2221: 	    my $lastone = pop(@$col_fullnames);
                   2222: 	    $msgfor .= ', '.(join ', ',@$col_fullnames).' and '.$lastone.'.';
1.118     ng       2223: 	}
                   2224: 	$msgfor =~ s/\'/\\'/g; #' stupid emacs - no! javascript
1.468     albertel 2225: 	$result.='<input type="hidden" name="includemsg'.$counter.'" value="" />'."\n".
1.121     ng       2226: 	    '<input type="hidden" name="newmsg'.$counter.'" value="" />'."\n";
                   2227: 	$result.='&nbsp;<a href="javascript:msgCenter(document.SCORE,'.$counter.
1.417     albertel 2228: 	    ',\''.$msgfor.'\');" target="_self">'.
1.464     albertel 2229: 	    &mt('Compose message to student').(scalar(@$col_fullnames) >= 1 ? 's' : '').'</a><label> ('.
1.350     albertel 2230: 	    &mt('incl. grades').' <input type="checkbox" name="withgrades'.$counter.'" /></label>)'.
1.118     ng       2231: 	    '<img src="'.$request->dir_config('lonIconsURL').
                   2232: 	    '/mailbkgrd.gif" width="14" height="10" name="mailicon'.$counter.'" />'."\n".
1.298     www      2233: 	    '<br />&nbsp;('.
1.468     albertel 2234: 	    &mt('Message will be sent when you click on Save &amp; Next below.').")\n";
                   2235: 	$result.='</div></div>';
1.121     ng       2236: 	$request->print($result);
1.118     ng       2237:     }
1.41      ng       2238: 
                   2239:     my %seen = ();
                   2240:     my @partlist;
1.129     ng       2241:     my @gradePartRespid;
1.375     albertel 2242:     my @part_response_id = &flatten_responseType($responseType);
1.468     albertel 2243:     $request->print('<div class="LC_grade_assign">'.
                   2244: 		    
                   2245: 		    '<div class="LC_grade_assign_header">'.
                   2246: 		    &mt('Assign Grades').'</div>'.
                   2247: 		    '<div class="LC_grade_assign_body">');
1.375     albertel 2248:     foreach my $part_response_id (@part_response_id) {
                   2249:     	my ($partid,$respid) = @{ $part_response_id };
                   2250: 	my $part_resp = join('_',@{ $part_response_id });
1.322     albertel 2251: 	next if ($seen{$partid} > 0);
1.41      ng       2252: 	$seen{$partid}++;
1.393     albertel 2253: 	next if ($$handgrade{$part_resp} ne 'yes' 
                   2254: 		 && $env{'form.lastSub'} eq 'hdgrade');
1.524     raeburn  2255: 	push(@partlist,$partid);
                   2256: 	push(@gradePartRespid,$partid.'.'.$respid);
1.322     albertel 2257: 	$request->print(&gradeBox($request,$symb,$uname,$udom,$counter,$partid,\%record));
1.41      ng       2258:     }
1.468     albertel 2259:     $request->print('</div></div>');
                   2260: 
                   2261:     $request->print('<div class="LC_grade_info_links">');
                   2262:     if ($perm{'vgr'}) {
                   2263: 	$request->print(
                   2264: 	    &Apache::loncommon::track_student_link(&mt('View recent activity'),
                   2265: 						   $uname,$udom,'check'));
                   2266:     }
                   2267:     if ($perm{'opa'}) {
                   2268: 	$request->print(
                   2269: 	    &Apache::loncommon::pprmlink(&mt('Set/Change parameters'),
                   2270: 					 $uname,$udom,$symb,'check'));
                   2271:     }
                   2272:     $request->print('</div>');
                   2273: 
1.45      ng       2274:     $result='<input type="hidden" name="partlist'.$counter.
                   2275: 	'" value="'.(join ":",@partlist).'" />'."\n";
1.129     ng       2276:     $result.='<input type="hidden" name="gradePartRespid'.
                   2277: 	'" value="'.(join ":",@gradePartRespid).'" />'."\n" if ($counter == 0);
1.45      ng       2278:     my $ctr = 0;
                   2279:     while ($ctr < scalar(@partlist)) {
                   2280: 	$result.='<input type="hidden" name="partid'.$counter.'_'.$ctr.'" value="'.
                   2281: 	    $partlist[$ctr].'" />'."\n";
                   2282: 	$ctr++;
                   2283:     }
1.468     albertel 2284:     $request->print($result.''."\n");
1.41      ng       2285: 
1.441     www      2286: # Done with printing info for one student
                   2287: 
1.468     albertel 2288:     $request->print('</div>');#LC_grade_show_user_body
                   2289:     $request->print('</div>');#LC_grade_show_user
1.441     www      2290: 
                   2291: 
1.41      ng       2292:     # print end of form
                   2293:     if ($counter == $total) {
1.297     www      2294: 	my $endform='<table border="0"><tr><td>'."\n";
1.485     albertel 2295: 	$endform.='<input type="button" value="'.&mt('Save &amp; Next').'" '.
1.119     ng       2296: 	    'onClick="javascript:checksubmit(this.form,\'Save & Next\','.
1.417     albertel 2297: 	    $total.','.scalar(@partlist).');" target="_self" /> &nbsp;'."\n";
1.119     ng       2298: 	my $ntstu ='<select name="NTSTU">'.
                   2299: 	    '<option>1</option><option>2</option>'.
                   2300: 	    '<option>3</option><option>5</option>'.
                   2301: 	    '<option>7</option><option>10</option></select>'."\n";
1.257     albertel 2302: 	my $nsel = ($env{'form.NTSTU'} ne '' ? $env{'form.NTSTU'} : '1');
1.401     albertel 2303: 	$ntstu =~ s/<option>$nsel</<option selected="selected">$nsel</;
1.578     raeburn  2304:         $endform.=&mt('[_1]student(s)',$ntstu);
1.485     albertel 2305: 	$endform.='&nbsp;&nbsp;<input type="button" value="'.&mt('Previous').'" '.
1.417     albertel 2306: 	    'onClick="javascript:checksubmit(this.form,\'Previous\');" target="_self" /> &nbsp;'."\n".
1.485     albertel 2307: 	    '<input type="button" value="'.&mt('Next').'" '.
1.417     albertel 2308: 	    'onClick="javascript:checksubmit(this.form,\'Next\');" target="_self" /> &nbsp;';
1.485     albertel 2309: 	$endform.=&mt('(Next and Previous (student) do not save the scores.)')."\n" ;
1.349     albertel 2310:         $endform.="<input type='hidden' value='".&get_increment().
1.348     bowersj2 2311:             "' name='increment' />";
1.485     albertel 2312: 	$endform.='</td></tr></table></form>';
1.324     albertel 2313: 	$endform.=&show_grading_menu_form($symb);
1.41      ng       2314: 	$request->print($endform);
                   2315:     }
                   2316:     return '';
1.38      ng       2317: }
                   2318: 
1.464     albertel 2319: sub check_collaborators {
                   2320:     my ($symb,$uname,$udom,$record,$handgrade,$counter) = @_;
                   2321:     my ($result,@col_fullnames);
                   2322:     my ($classlist,undef,$fullname) = &getclasslist('all','0');
                   2323:     foreach my $part (keys(%$handgrade)) {
                   2324: 	my $ncol = &Apache::lonnet::EXT('resource.'.$part.
                   2325: 					'.maxcollaborators',
                   2326: 					$symb,$udom,$uname);
                   2327: 	next if ($ncol <= 0);
                   2328: 	$part =~ s/\_/\./g;
                   2329: 	next if ($record->{'resource.'.$part.'.collaborators'} eq '');
                   2330: 	my (@good_collaborators, @bad_collaborators);
                   2331: 	foreach my $possible_collaborator
                   2332: 	    (split(/,?\s+/,$record->{'resource.'.$part.'.collaborators'})) { 
                   2333: 	    $possible_collaborator =~ s/[\$\^\(\)]//g;
                   2334: 	    next if ($possible_collaborator eq '');
                   2335: 	    my ($co_name,$co_dom) = split(/\@|:/,$possible_collaborator);
                   2336: 	    $co_dom = $udom if (! defined($co_dom) || $co_dom =~ /^domain$/i);
                   2337: 	    next if ($co_name eq $uname && $co_dom eq $udom);
                   2338: 	    # Doing this grep allows 'fuzzy' specification
                   2339: 	    my @matches = grep(/^\Q$co_name\E:\Q$co_dom\E$/i, 
                   2340: 			       keys(%$classlist));
                   2341: 	    if (! scalar(@matches)) {
                   2342: 		push(@bad_collaborators, $possible_collaborator);
                   2343: 	    } else {
                   2344: 		push(@good_collaborators, @matches);
                   2345: 	    }
                   2346: 	}
                   2347: 	if (scalar(@good_collaborators) != 0) {
1.466     albertel 2348: 	    $result.='<br />'.&mt('Collaborators: ');
1.464     albertel 2349: 	    foreach my $name (@good_collaborators) {
                   2350: 		my ($lastname,$givenn) = split(/,/,$$fullname{$name});
                   2351: 		push(@col_fullnames, $givenn.' '.$lastname);
                   2352: 		$result.=$fullname->{$name}.'&nbsp; &nbsp; &nbsp;';
                   2353: 	    }
                   2354: 	    $result.='<br />'."\n";
1.466     albertel 2355: 	    my ($part)=split(/\./,$part);
1.464     albertel 2356: 	    $result.='<input type="hidden" name="collaborator'.$counter.
                   2357: 		'" value="'.$part.':'.(join ':',@good_collaborators).'" />'.
                   2358: 		"\n";
                   2359: 	}
                   2360: 	if (scalar(@bad_collaborators) > 0) {
1.466     albertel 2361: 	    $result.='<div class="LC_warning">';
1.464     albertel 2362: 	    $result.=&mt('This student has submitted [quant,_1,invalid collaborator]: [_2]',scalar(@bad_collaborators),join(', ',@bad_collaborators));
                   2363: 	    $result .= '</div>';
                   2364: 	}         
                   2365: 	if (scalar(@bad_collaborators > $ncol)) {
1.466     albertel 2366: 	    $result .= '<div class="LC_warning">';
1.464     albertel 2367: 	    $result .= &mt('This student has submitted too many '.
                   2368: 		'collaborators.  Maximum is [_1].',$ncol);
                   2369: 	    $result .= '</div>';
                   2370: 	}
                   2371:     }
                   2372:     return ($result,$fullname,\@col_fullnames);
                   2373: }
                   2374: 
1.44      ng       2375: #--- Retrieve the last submission for all the parts
1.38      ng       2376: sub get_last_submission {
1.119     ng       2377:     my ($returnhash)=@_;
1.46      ng       2378:     my (@string,$timestamp);
1.119     ng       2379:     if ($$returnhash{'version'}) {
1.46      ng       2380: 	my %lasthash=();
                   2381: 	my ($version);
1.119     ng       2382: 	for ($version=1;$version<=$$returnhash{'version'};$version++) {
1.397     albertel 2383: 	    foreach my $key (sort(split(/\:/,
                   2384: 					$$returnhash{$version.':keys'}))) {
                   2385: 		$lasthash{$key}=$$returnhash{$version.':'.$key};
                   2386: 		$timestamp = 
1.545     raeburn  2387: 		    &Apache::lonlocal::locallocaltime($$returnhash{$version.':timestamp'});
1.46      ng       2388: 	    }
                   2389: 	}
1.397     albertel 2390: 	foreach my $key (keys(%lasthash)) {
                   2391: 	    next if ($key !~ /\.submission$/);
                   2392: 
                   2393: 	    my ($partid,$foo) = split(/submission$/,$key);
                   2394: 	    my $draft  = $lasthash{$partid.'awarddetail'} eq 'DRAFT' ?
1.398     albertel 2395: 		'<span class="LC_warning">Draft Copy</span> ' : '';
1.397     albertel 2396: 	    push(@string, join(':', $key, $draft.$lasthash{$key}));
1.41      ng       2397: 	}
                   2398:     }
1.397     albertel 2399:     if (!@string) {
                   2400: 	$string[0] =
1.539     riegler  2401: 	    '<span class="LC_warning">'.&mt('Nothing submitted - no attempts.').'</span>';
1.397     albertel 2402:     }
                   2403:     return (\@string,\$timestamp);
1.38      ng       2404: }
1.35      ng       2405: 
1.44      ng       2406: #--- High light keywords, with style choosen by user.
1.38      ng       2407: sub keywords_highlight {
1.44      ng       2408:     my $string    = shift;
1.257     albertel 2409:     my $size      = $env{'form.kwsize'} eq '0' ? '' : 'size='.$env{'form.kwsize'};
                   2410:     my $styleon   = $env{'form.kwstyle'} eq ''  ? '' : $env{'form.kwstyle'};
1.41      ng       2411:     (my $styleoff = $styleon) =~ s/\</\<\//;
1.257     albertel 2412:     my @keylist   = split(/[,\s+]/,$env{'form.keywords'});
1.398     albertel 2413:     foreach my $keyword (@keylist) {
                   2414: 	$string =~ s/\b\Q$keyword\E(\b|\.)/<font color\=$env{'form.kwclr'} $size\>$styleon$keyword$styleoff<\/font>/gi;
1.41      ng       2415:     }
                   2416:     return $string;
1.38      ng       2417: }
1.36      ng       2418: 
1.44      ng       2419: #--- Called from submission routine
1.38      ng       2420: sub processHandGrade {
1.41      ng       2421:     my ($request) = shift;
1.324     albertel 2422:     my $symb   = &get_symb($request);
                   2423:     my (undef,undef,$url) = &Apache::lonnet::decode_symb($symb);
1.257     albertel 2424:     my $button = $env{'form.gradeOpt'};
                   2425:     my $ngrade = $env{'form.NCT'};
                   2426:     my $ntstu  = $env{'form.NTSTU'};
1.301     albertel 2427:     my $cdom   = $env{'course.'.$env{'request.course.id'}.'.domain'};
                   2428:     my $cnum   = $env{'course.'.$env{'request.course.id'}.'.num'};
                   2429: 
1.44      ng       2430:     if ($button eq 'Save & Next') {
                   2431: 	my $ctr = 0;
                   2432: 	while ($ctr < $ngrade) {
1.257     albertel 2433: 	    my ($uname,$udom) = split(/:/,$env{'form.unamedom'.$ctr});
1.324     albertel 2434: 	    my ($errorflag,$pts,$wgt) = &saveHandGrade($request,$symb,$uname,$udom,$ctr);
1.71      ng       2435: 	    if ($errorflag eq 'no_score') {
                   2436: 		$ctr++;
                   2437: 		next;
                   2438: 	    }
1.104     albertel 2439: 	    if ($errorflag eq 'not_allowed') {
1.398     albertel 2440: 		$request->print("<span class=\"LC_warning\">Not allowed to modify grades for $uname:$udom</span>");
1.104     albertel 2441: 		$ctr++;
                   2442: 		next;
                   2443: 	    }
1.257     albertel 2444: 	    my $includemsg = $env{'form.includemsg'.$ctr};
1.44      ng       2445: 	    my ($subject,$message,$msgstatus) = ('','','');
1.418     albertel 2446: 	    my $restitle = &Apache::lonnet::gettitle($symb);
                   2447:             my ($feedurl,$showsymb) =
                   2448: 		&get_feedurl_and_symb($symb,$uname,$udom);
                   2449: 	    my $messagetail;
1.62      albertel 2450: 	    if ($includemsg =~ /savemsg|newmsg\Q$ctr\E/) {
1.298     www      2451: 		$subject = $env{'form.msgsub'} if ($includemsg =~ /msgsub/);
1.295     www      2452: 		unless ($subject=~/\w/) { $subject=&mt('Grading Feedback'); }
1.386     raeburn  2453: 		$subject.=' ['.$restitle.']';
1.44      ng       2454: 		my (@msgnum) = split(/,/,$includemsg);
                   2455: 		foreach (@msgnum) {
1.257     albertel 2456: 		    $message.=$env{'form.'.$_} if ($_ =~ /savemsg|newmsg/ && $_ ne '');
1.44      ng       2457: 		}
1.80      ng       2458: 		$message =&Apache::lonfeedback::clear_out_html($message);
1.298     www      2459: 		if ($env{'form.withgrades'.$ctr}) {
                   2460: 		    $message.="\n\nPoint".($pts > 1 ? 's':'').' awarded = '.$pts.' out of '.$wgt;
1.386     raeburn  2461: 		    $messagetail = " for <a href=\"".
1.418     albertel 2462: 		                   $feedurl."?symb=$showsymb\">$env{'form.probTitle'}</a>";
1.386     raeburn  2463: 		}
                   2464: 		$msgstatus = 
                   2465:                     &Apache::lonmsg::user_normal_msg($uname,$udom,$subject,
                   2466: 						     $message.$messagetail,
1.418     albertel 2467:                                                      undef,$feedurl,undef,
1.386     raeburn  2468:                                                      undef,undef,$showsymb,
                   2469:                                                      $restitle);
1.574     bisitz   2470: 		$request->print('<br />'.&mt('Sending message to [_1]',$uname.':'.$udom).': '.
1.296     www      2471: 				$msgstatus);
1.44      ng       2472: 	    }
1.257     albertel 2473: 	    if ($env{'form.collaborator'.$ctr}) {
1.155     albertel 2474: 		my @collabstrs=&Apache::loncommon::get_env_multiple("form.collaborator$ctr");
1.150     albertel 2475: 		foreach my $collabstr (@collabstrs) {
                   2476: 		    my ($part,@collaborators) = split(/:/,$collabstr);
1.310     banghart 2477: 		    foreach my $collaborator (@collaborators) {
1.150     albertel 2478: 			my ($errorflag,$pts,$wgt) = 
1.324     albertel 2479: 			    &saveHandGrade($request,$symb,$collaborator,$udom,$ctr,
1.257     albertel 2480: 					   $env{'form.unamedom'.$ctr},$part);
1.150     albertel 2481: 			if ($errorflag eq 'not_allowed') {
1.362     albertel 2482: 			    $request->print("<span class=\"LC_error\">".&mt('Not allowed to modify grades for [_1]',"$collaborator:$udom")."</span>");
1.150     albertel 2483: 			    next;
1.418     albertel 2484: 			} elsif ($message ne '') {
                   2485: 			    my ($baseurl,$showsymb) = 
                   2486: 				&get_feedurl_and_symb($symb,$collaborator,
                   2487: 						      $udom);
                   2488: 			    if ($env{'form.withgrades'.$ctr}) {
                   2489: 				$messagetail = " for <a href=\"".
1.386     raeburn  2490:                                     $baseurl."?symb=$showsymb\">$env{'form.probTitle'}</a>";
1.150     albertel 2491: 			    }
1.418     albertel 2492: 			    $msgstatus = 
                   2493: 				&Apache::lonmsg::user_normal_msg($collaborator,$udom,$subject,$message.$messagetail,undef,$baseurl,undef,undef,undef,$showsymb,$restitle);
1.104     albertel 2494: 			}
1.44      ng       2495: 		    }
                   2496: 		}
                   2497: 	    }
                   2498: 	    $ctr++;
                   2499: 	}
                   2500:     }
                   2501: 
1.257     albertel 2502:     if ($env{'form.handgrade'} eq 'yes') {
1.119     ng       2503: 	# Keywords sorted in alphabatical order
1.257     albertel 2504: 	my $loginuser = $env{'user.name'}.':'.$env{'user.domain'};
1.119     ng       2505: 	my %keyhash = ();
1.257     albertel 2506: 	$env{'form.keywords'}           =~ s/,\s{0,}|\s+/ /g;
                   2507: 	$env{'form.keywords'}           =~ s/^\s+|\s+$//;
                   2508: 	my (@keywords) = sort(split(/\s+/,$env{'form.keywords'}));
                   2509: 	$env{'form.keywords'} = join(' ',@keywords);
                   2510: 	$keyhash{$symb.'_keywords'}     = $env{'form.keywords'};
                   2511: 	$keyhash{$symb.'_subject'}      = $env{'form.msgsub'};
                   2512: 	$keyhash{$loginuser.'_kwclr'}   = $env{'form.kwclr'};
                   2513: 	$keyhash{$loginuser.'_kwsize'}  = $env{'form.kwsize'};
                   2514: 	$keyhash{$loginuser.'_kwstyle'} = $env{'form.kwstyle'};
1.119     ng       2515: 
                   2516: 	# message center - Order of message gets changed. Blank line is eliminated.
1.257     albertel 2517: 	# New messages are saved in env for the next student.
1.119     ng       2518: 	# All messages are saved in nohist_handgrade.db
                   2519: 	my ($ctr,$idx) = (1,1);
1.257     albertel 2520: 	while ($ctr <= $env{'form.savemsgN'}) {
                   2521: 	    if ($env{'form.savemsg'.$ctr} ne '') {
                   2522: 		$keyhash{$symb.'_savemsg'.$idx} = $env{'form.savemsg'.$ctr};
1.119     ng       2523: 		$idx++;
                   2524: 	    }
                   2525: 	    $ctr++;
1.41      ng       2526: 	}
1.119     ng       2527: 	$ctr = 0;
                   2528: 	while ($ctr < $ngrade) {
1.257     albertel 2529: 	    if ($env{'form.newmsg'.$ctr} ne '') {
                   2530: 		$keyhash{$symb.'_savemsg'.$idx} = $env{'form.newmsg'.$ctr};
                   2531: 		$env{'form.savemsg'.$idx} = $env{'form.newmsg'.$ctr};
1.119     ng       2532: 		$idx++;
                   2533: 	    }
                   2534: 	    $ctr++;
1.41      ng       2535: 	}
1.257     albertel 2536: 	$env{'form.savemsgN'} = --$idx;
                   2537: 	$keyhash{$symb.'_savemsgN'} = $env{'form.savemsgN'};
1.119     ng       2538: 	my $putresult = &Apache::lonnet::put
1.301     albertel 2539: 	    ('nohist_handgrade',\%keyhash,$cdom,$cnum);
1.41      ng       2540:     }
1.44      ng       2541:     # Called by Save & Refresh from Highlight Attribute Window
1.257     albertel 2542:     my (undef,undef,$fullname) = &getclasslist($env{'form.section'},'1');
                   2543:     if ($env{'form.refresh'} eq 'on') {
1.86      ng       2544: 	my ($ctr,$total) = (0,0);
                   2545: 	while ($ctr < $ngrade) {
1.257     albertel 2546: 	    $total++ if  $env{'form.unamedom'.$ctr} ne '';
1.86      ng       2547: 	    $ctr++;
                   2548: 	}
1.257     albertel 2549: 	$env{'form.NTSTU'}=$ngrade;
1.86      ng       2550: 	$ctr = 0;
                   2551: 	while ($ctr < $total) {
1.257     albertel 2552: 	    my $processUser = $env{'form.unamedom'.$ctr};
                   2553: 	    ($env{'form.student'},$env{'form.userdom'}) = split(/:/,$processUser);
                   2554: 	    $env{'form.fullname'} = $$fullname{$processUser};
1.86      ng       2555: 	    &submission($request,$ctr,$total-1);
1.41      ng       2556: 	    $ctr++;
                   2557: 	}
                   2558: 	return '';
                   2559:     }
1.36      ng       2560: 
1.121     ng       2561: # Go directly to grade student - from submission or link from chart page
1.120     ng       2562:     if ($button eq 'Grade Student') {
1.324     albertel 2563: 	(undef,undef,$env{'form.handgrade'},undef,undef) = &showResourceInfo($symb);
1.257     albertel 2564: 	my $processUser = $env{'form.unamedom'.$env{'form.studentNo'}};
                   2565: 	($env{'form.student'},$env{'form.userdom'}) = split(/:/,$processUser);
                   2566: 	$env{'form.fullname'} = $$fullname{$processUser};
1.120     ng       2567: 	&submission($request,0,0);
                   2568: 	return '';
                   2569:     }
                   2570: 
1.44      ng       2571:     # Get the next/previous one or group of students
1.257     albertel 2572:     my $firststu = $env{'form.unamedom0'};
                   2573:     my $laststu = $env{'form.unamedom'.($ngrade-1)};
1.119     ng       2574:     my $ctr = 2;
1.41      ng       2575:     while ($laststu eq '') {
1.257     albertel 2576: 	$laststu  = $env{'form.unamedom'.($ngrade-$ctr)};
1.41      ng       2577: 	$ctr++;
                   2578: 	$laststu = $firststu if ($ctr > $ngrade);
                   2579:     }
1.44      ng       2580: 
1.41      ng       2581:     my (@parsedlist,@nextlist);
                   2582:     my ($nextflg) = 0;
1.524     raeburn  2583:     foreach my $item (sort 
1.294     albertel 2584: 	     {
                   2585: 		 if (lc($$fullname{$a}) ne lc($$fullname{$b})) {
                   2586: 		     return (lc($$fullname{$a}) cmp lc($$fullname{$b}));
                   2587: 		 }
                   2588: 		 return $a cmp $b;
                   2589: 	     } (keys(%$fullname))) {
1.41      ng       2590: 	if ($nextflg == 1 && $button =~ /Next$/) {
1.524     raeburn  2591: 	    push(@parsedlist,$item);
1.41      ng       2592: 	}
1.524     raeburn  2593: 	$nextflg = 1 if ($item eq $laststu);
1.41      ng       2594: 	if ($button eq 'Previous') {
1.524     raeburn  2595: 	    last if ($item eq $firststu);
                   2596: 	    push(@parsedlist,$item);
1.41      ng       2597: 	}
                   2598:     }
                   2599:     $ctr = 0;
                   2600:     @parsedlist = reverse @parsedlist if ($button eq 'Previous');
1.324     albertel 2601:     my ($partlist) = &response_type($symb);
1.41      ng       2602:     foreach my $student (@parsedlist) {
1.257     albertel 2603: 	my $submitonly=$env{'form.submitonly'};
1.41      ng       2604: 	my ($uname,$udom) = split(/:/,$student);
1.301     albertel 2605: 	
                   2606: 	if ($submitonly eq 'queued') {
                   2607: 	    my %queue_status = 
                   2608: 		&Apache::bridgetask::get_student_status($symb,$cdom,$cnum,
                   2609: 							$udom,$uname);
                   2610: 	    next if (!defined($queue_status{'gradingqueue'}));
                   2611: 	}
                   2612: 
1.156     albertel 2613: 	if ($submitonly =~ /^(yes|graded|incorrect)$/) {
1.257     albertel 2614: #	    my %record = &Apache::lonnet::restore($symb,$env{'request.course.id'},$udom,$uname);
1.324     albertel 2615: 	    my %status=&student_gradeStatus($symb,$udom,$uname,$partlist);
1.145     albertel 2616: 	    my $submitted = 0;
1.248     albertel 2617: 	    my $ungraded = 0;
                   2618: 	    my $incorrect = 0;
1.524     raeburn  2619: 	    foreach my $item (keys(%status)) {
                   2620: 		$submitted = 1 if ($status{$item} ne 'nothing');
                   2621: 		$ungraded = 1 if ($status{$item} =~ /^ungraded/);
                   2622: 		$incorrect = 1 if ($status{$item} =~ /^incorrect/);
                   2623: 		my ($foo,$partid,$foo1) = split(/\./,$item);
1.145     albertel 2624: 		if ($status{'resource.'.$partid.'.submitted_by'} ne '') {
                   2625: 		    $submitted = 0;
                   2626: 		}
1.41      ng       2627: 	    }
1.156     albertel 2628: 	    next if (!$submitted && ($submitonly eq 'yes' ||
                   2629: 				     $submitonly eq 'incorrect' ||
                   2630: 				     $submitonly eq 'graded'));
1.248     albertel 2631: 	    next if (!$ungraded && ($submitonly eq 'graded'));
                   2632: 	    next if (!$incorrect && $submitonly eq 'incorrect');
1.41      ng       2633: 	}
1.524     raeburn  2634: 	push(@nextlist,$student) if ($ctr < $ntstu);
1.129     ng       2635: 	last if ($ctr == $ntstu);
1.41      ng       2636: 	$ctr++;
                   2637:     }
1.36      ng       2638: 
1.41      ng       2639:     $ctr = 0;
                   2640:     my $total = scalar(@nextlist)-1;
1.39      ng       2641: 
1.524     raeburn  2642:     foreach (sort(@nextlist)) {
1.41      ng       2643: 	my ($uname,$udom,$submitter) = split(/:/);
1.257     albertel 2644: 	$env{'form.student'}  = $uname;
                   2645: 	$env{'form.userdom'}  = $udom;
                   2646: 	$env{'form.fullname'} = $$fullname{$_};
1.41      ng       2647: 	&submission($request,$ctr,$total);
                   2648: 	$ctr++;
                   2649:     }
                   2650:     if ($total < 0) {
1.485     albertel 2651: 	my $the_end = '<h3><span class="LC_info">'.&mt('LON-CAPA User Message').'</span></h3><br />'."\n";
                   2652: 	$the_end.=&mt('<b>Message: </b> No more students for this section or class.').'<br /><br />'."\n";
                   2653: 	$the_end.=&mt('Click on the button below to return to the grading menu.').'<br /><br />'."\n";
1.324     albertel 2654: 	$the_end.=&show_grading_menu_form($symb);
1.41      ng       2655: 	$request->print($the_end);
                   2656:     }
                   2657:     return '';
1.38      ng       2658: }
1.36      ng       2659: 
1.44      ng       2660: #---- Save the score and award for each student, if changed
1.38      ng       2661: sub saveHandGrade {
1.324     albertel 2662:     my ($request,$symb,$stuname,$domain,$newflg,$submitter,$part) = @_;
1.342     banghart 2663:     my @version_parts;
1.104     albertel 2664:     my $usec = &Apache::lonnet::getsection($domain,$stuname,
1.257     albertel 2665: 					   $env{'request.course.id'});
1.104     albertel 2666:     if (!&canmodify($usec)) { return('not_allowed'); }
1.337     banghart 2667:     my %record = &Apache::lonnet::restore($symb,$env{'request.course.id'},$domain,$stuname);
1.251     banghart 2668:     my @parts_graded;
1.77      ng       2669:     my %newrecord  = ();
                   2670:     my ($pts,$wgt) = ('','');
1.269     raeburn  2671:     my %aggregate = ();
                   2672:     my $aggregateflag = 0;
1.301     albertel 2673:     my @parts = split(/:/,$env{'form.partlist'.$newflg});
                   2674:     foreach my $new_part (@parts) {
1.337     banghart 2675: 	#collaborator ($submi may vary for different parts
1.259     banghart 2676: 	if ($submitter && $new_part ne $part) { next; }
                   2677: 	my $dropMenu = $env{'form.GD_SEL'.$newflg.'_'.$new_part};
1.125     ng       2678: 	if ($dropMenu eq 'excused') {
1.259     banghart 2679: 	    if ($record{'resource.'.$new_part.'.solved'} ne 'excused') {
                   2680: 		$newrecord{'resource.'.$new_part.'.solved'} = 'excused';
                   2681: 		if (exists($record{'resource.'.$new_part.'.awarded'})) {
                   2682: 		    $newrecord{'resource.'.$new_part.'.awarded'} = '';
1.58      albertel 2683: 		}
1.364     banghart 2684: 	        $newrecord{'resource.'.$new_part.'.regrader'}="$env{'user.name'}:$env{'user.domain'}";
1.58      albertel 2685: 	    }
1.125     ng       2686: 	} elsif ($dropMenu eq 'reset status'
1.259     banghart 2687: 		 && exists($record{'resource.'.$new_part.'.solved'})) { #don't bother if no old records -> no attempts
1.524     raeburn  2688: 	    foreach my $key (keys(%record)) {
1.259     banghart 2689: 		if ($key=~/^resource\.\Q$new_part\E\./) { $newrecord{$key} = ''; }
1.197     albertel 2690: 	    }
1.259     banghart 2691: 	    $newrecord{'resource.'.$new_part.'.regrader'}=
1.257     albertel 2692: 		"$env{'user.name'}:$env{'user.domain'}";
1.270     albertel 2693:             my $totaltries = $record{'resource.'.$part.'.tries'};
                   2694: 
                   2695:             my %last_resets = &get_last_resets($symb,$env{'request.course.id'},
                   2696: 					       [$new_part]);
                   2697:             my $aggtries =$totaltries;
1.269     raeburn  2698:             if ($last_resets{$new_part}) {
1.270     albertel 2699:                 $aggtries = &get_num_tries(\%record,$last_resets{$new_part},
                   2700: 					   $new_part);
1.269     raeburn  2701:             }
1.270     albertel 2702: 
                   2703:             my $solvedstatus = $record{'resource.'.$new_part.'.solved'};
1.269     raeburn  2704:             if ($aggtries > 0) {
1.327     albertel 2705:                 &decrement_aggs($symb,$new_part,\%aggregate,$aggtries,$totaltries,$solvedstatus);
1.269     raeburn  2706:                 $aggregateflag = 1;
                   2707:             }
1.125     ng       2708: 	} elsif ($dropMenu eq '') {
1.259     banghart 2709: 	    $pts = ($env{'form.GD_BOX'.$newflg.'_'.$new_part} ne '' ? 
                   2710: 		    $env{'form.GD_BOX'.$newflg.'_'.$new_part} : 
                   2711: 		    $env{'form.RADVAL'.$newflg.'_'.$new_part});
                   2712: 	    if ($pts eq '' && $env{'form.GD_SEL'.$newflg.'_'.$new_part} eq '') {
1.153     albertel 2713: 		next;
                   2714: 	    }
1.259     banghart 2715: 	    $wgt = $env{'form.WGT'.$newflg.'_'.$new_part} eq '' ? 1 : 
                   2716: 		$env{'form.WGT'.$newflg.'_'.$new_part};
1.41      ng       2717: 	    my $partial= $pts/$wgt;
1.259     banghart 2718: 	    if ($partial eq $record{'resource.'.$new_part.'.awarded'}) {
1.153     albertel 2719: 		#do not update score for part if not changed.
1.346     banghart 2720:                 &handback_files($request,$symb,$stuname,$domain,$newflg,$new_part,\%newrecord);
1.153     albertel 2721: 		next;
1.251     banghart 2722: 	    } else {
1.524     raeburn  2723: 	        push(@parts_graded,$new_part);
1.153     albertel 2724: 	    }
1.259     banghart 2725: 	    if ($record{'resource.'.$new_part.'.awarded'} ne $partial) {
                   2726: 		$newrecord{'resource.'.$new_part.'.awarded'}  = $partial;
1.153     albertel 2727: 	    }
1.259     banghart 2728: 	    my $reckey = 'resource.'.$new_part.'.solved';
1.41      ng       2729: 	    if ($partial == 0) {
1.153     albertel 2730: 		if ($record{$reckey} ne 'incorrect_by_override') {
                   2731: 		    $newrecord{$reckey} = 'incorrect_by_override';
                   2732: 		}
1.41      ng       2733: 	    } else {
1.153     albertel 2734: 		if ($record{$reckey} ne 'correct_by_override') {
                   2735: 		    $newrecord{$reckey} = 'correct_by_override';
                   2736: 		}
                   2737: 	    }	    
                   2738: 	    if ($submitter && 
1.259     banghart 2739: 		($record{'resource.'.$new_part.'.submitted_by'} ne $submitter)) {
                   2740: 		$newrecord{'resource.'.$new_part.'.submitted_by'} = $submitter;
1.41      ng       2741: 	    }
1.259     banghart 2742: 	    $newrecord{'resource.'.$new_part.'.regrader'}=
1.257     albertel 2743: 		"$env{'user.name'}:$env{'user.domain'}";
1.41      ng       2744: 	}
1.259     banghart 2745: 	# unless problem has been graded, set flag to version the submitted files
1.305     banghart 2746: 	unless ($record{'resource.'.$new_part.'.solved'} =~ /^correct_/  || 
                   2747: 	        $record{'resource.'.$new_part.'.solved'} eq 'incorrect_by_override' ||
                   2748: 	        $dropMenu eq 'reset status')
                   2749: 	   {
1.524     raeburn  2750: 	    push(@version_parts,$new_part);
1.259     banghart 2751: 	}
1.41      ng       2752:     }
1.301     albertel 2753:     my $cdom = $env{'course.'.$env{'request.course.id'}.'.domain'};
                   2754:     my $cnum = $env{'course.'.$env{'request.course.id'}.'.num'};
                   2755: 
1.344     albertel 2756:     if (%newrecord) {
                   2757:         if (@version_parts) {
1.364     banghart 2758:             my @changed_keys = &version_portfiles(\%record, \@parts_graded, 
                   2759:                                 $env{'request.course.id'}, $symb, $domain, $stuname, \@version_parts);
1.344     albertel 2760: 	    @newrecord{@changed_keys} = @record{@changed_keys};
1.367     albertel 2761: 	    foreach my $new_part (@version_parts) {
                   2762: 		&handback_files($request,$symb,$stuname,$domain,$newflg,
                   2763: 				$new_part,\%newrecord);
                   2764: 	    }
1.259     banghart 2765:         }
1.44      ng       2766: 	&Apache::lonnet::cstore(\%newrecord,$symb,
1.257     albertel 2767: 				$env{'request.course.id'},$domain,$stuname);
1.380     albertel 2768: 	&check_and_remove_from_queue(\@parts,\%record,\%newrecord,$symb,
                   2769: 				     $cdom,$cnum,$domain,$stuname);
1.41      ng       2770:     }
1.269     raeburn  2771:     if ($aggregateflag) {
                   2772:         &Apache::lonnet::cinc('nohist_resourcetracker',\%aggregate,
1.301     albertel 2773: 			      $cdom,$cnum);
1.269     raeburn  2774:     }
1.301     albertel 2775:     return ('',$pts,$wgt);
1.36      ng       2776: }
1.322     albertel 2777: 
1.380     albertel 2778: sub check_and_remove_from_queue {
                   2779:     my ($parts,$record,$newrecord,$symb,$cdom,$cnum,$domain,$stuname) = @_;
                   2780:     my @ungraded_parts;
                   2781:     foreach my $part (@{$parts}) {
                   2782: 	if (    $record->{   'resource.'.$part.'.awarded'} eq ''
                   2783: 	     && $record->{   'resource.'.$part.'.solved' } ne 'excused'
                   2784: 	     && $newrecord->{'resource.'.$part.'.awarded'} eq ''
                   2785: 	     && $newrecord->{'resource.'.$part.'.solved' } ne 'excused'
                   2786: 		) {
                   2787: 	    push(@ungraded_parts, $part);
                   2788: 	}
                   2789:     }
                   2790:     if ( !@ungraded_parts ) {
                   2791: 	&Apache::bridgetask::remove_from_queue('gradingqueue',$symb,$cdom,
                   2792: 					       $cnum,$domain,$stuname);
                   2793:     }
                   2794: }
                   2795: 
1.337     banghart 2796: sub handback_files {
                   2797:     my ($request,$symb,$stuname,$domain,$newflg,$new_part,$newrecord) = @_;
1.517     raeburn  2798:     my $portfolio_root = '/userfiles/portfolio';
1.359     www      2799:     my ($partlist,$handgrade,$responseType) = &response_type($symb);
1.375     albertel 2800: 
                   2801:     my @part_response_id = &flatten_responseType($responseType);
                   2802:     foreach my $part_response_id (@part_response_id) {
                   2803:     	my ($part_id,$resp_id) = @{ $part_response_id };
                   2804: 	my $part_resp = join('_',@{ $part_response_id });
1.337     banghart 2805:             if (($env{'form.'.$newflg.'_'.$part_resp.'_returndoc1'}) && ($new_part == $part_id)) {
                   2806:                 # if multiple files are uploaded names will be 'returndoc2','returndoc3'
                   2807:                 my $file_counter = 1;
1.367     albertel 2808: 		my $file_msg;
1.337     banghart 2809:                 while ($env{'form.'.$newflg.'_'.$part_resp.'_returndoc'.$file_counter}) {
                   2810:                     my $fname=$env{'form.'.$newflg.'_'.$part_resp.'_returndoc'.$file_counter.'.filename'};
1.338     banghart 2811:                     my ($directory,$answer_file) = 
                   2812:                         ($env{'form.'.$newflg.'_'.$part_resp.'_origdoc'.$file_counter} =~ /^(.*?)([^\/]*)$/);
                   2813:                     my ($answer_name,$answer_ver,$answer_ext) =
                   2814: 		        &file_name_version_ext($answer_file);
1.355     banghart 2815: 		    my ($portfolio_path) = ($directory =~ /^.+$stuname\/portfolio(.*)/);
1.517     raeburn  2816:                     my $getpropath = 1;
                   2817: 		    my @dir_list = &Apache::lonnet::dirlist($portfolio_root.$portfolio_path,$domain,$stuname,$getpropath);
1.338     banghart 2818: 		    my $version = &get_next_version($answer_name, $answer_ext, \@dir_list);
1.355     banghart 2819:                     # fix file name
                   2820:                     my ($save_file_name) = (($directory.$answer_name.".$version.".$answer_ext) =~ /^.+\/${stuname}\/(.*)/);
                   2821:                     my $result=&Apache::lonnet::finishuserfileupload($stuname,$domain,
                   2822:             	                                $newflg.'_'.$part_resp.'_returndoc'.$file_counter,
                   2823:             	                                $save_file_name);
1.337     banghart 2824:                     if ($result !~ m|^/uploaded/|) {
1.536     raeburn  2825:                         $request->print('<br /><span class="LC_error">'.
                   2826:                             &mt('An error occurred ([_1]) while trying to upload [_2].',
                   2827:                                 $result,$newflg.'_'.$part_resp.'_returndoc'.$file_counter).
                   2828:                                         '</span>');
1.356     banghart 2829:                     } else {
1.360     banghart 2830:                         # mark the file as read only
                   2831:                         my @files = ($save_file_name);
1.372     albertel 2832:                         my @what = ($symb,$env{'request.course.id'},'handback');
1.360     banghart 2833:                         &Apache::lonnet::mark_as_readonly($domain,$stuname,\@files,\@what);
1.367     albertel 2834: 			if (exists($$newrecord{"resource.$new_part.$resp_id.handback"})) {
                   2835: 			    $$newrecord{"resource.$new_part.$resp_id.handback"}.=',';
                   2836: 			}
                   2837:                         $$newrecord{"resource.$new_part.$resp_id.handback"} .= $save_file_name;
                   2838: 			$file_msg.= "\n".'<br /><span class="LC_filename"><a href="/uploaded/'."$domain/$stuname/".$save_file_name.'">'.$save_file_name."</a></span><br />";
                   2839: 
1.337     banghart 2840:                     }
                   2841:                     $request->print("<br />".$fname." will be the uploaded file name");
1.354     albertel 2842:                     $request->print(" ".$env{'form.'.$newflg.'_'.$part_resp.'_origdoc'.$file_counter});
1.337     banghart 2843:                     $file_counter++;
                   2844:                 }
1.367     albertel 2845: 		my $subject = "File Handed Back by Instructor ";
                   2846: 		my $message = "A file has been returned that was originally submitted in reponse to: <br />";
                   2847: 		$message .= "<strong>".&Apache::lonnet::gettitle($symb)."</strong><br />";
                   2848: 		$message .= ' The returned file(s) are named: '. $file_msg;
                   2849: 		$message .= " and can be found in your portfolio space.";
1.418     albertel 2850: 		my ($feedurl,$showsymb) = 
                   2851: 		    &get_feedurl_and_symb($symb,$domain,$stuname);
1.386     raeburn  2852:                 my $restitle = &Apache::lonnet::gettitle($symb);
                   2853: 		my $msgstatus = 
                   2854:                    &Apache::lonmsg::user_normal_msg($stuname,$domain,$subject.
                   2855: 			 ' (File Returned) ['.$restitle.']',$message,undef,
1.418     albertel 2856:                          $feedurl,undef,undef,undef,$showsymb,$restitle);
1.337     banghart 2857:             }
                   2858:         }
1.338     banghart 2859:     return;
1.337     banghart 2860: }
                   2861: 
1.418     albertel 2862: sub get_feedurl_and_symb {
                   2863:     my ($symb,$uname,$udom) = @_;
                   2864:     my (undef,undef,$url) = &Apache::lonnet::decode_symb($symb);
                   2865:     $url = &Apache::lonnet::clutter($url);
                   2866:     my $encrypturl=&Apache::lonnet::EXT('resource.0.encrypturl',
                   2867: 					$symb,$udom,$uname);
                   2868:     if ($encrypturl =~ /^yes$/i) {
                   2869: 	&Apache::lonenc::encrypted(\$url,1);
                   2870: 	&Apache::lonenc::encrypted(\$symb,1);
                   2871:     }
                   2872:     return ($url,$symb);
                   2873: }
                   2874: 
1.313     banghart 2875: sub get_submitted_files {
                   2876:     my ($udom,$uname,$partid,$respid,$record) = @_;
                   2877:     my @files;
                   2878:     if ($$record{"resource.$partid.$respid.portfiles"}) {
                   2879:         my $file_url = '/uploaded/'.$udom.'/'.$uname.'/portfolio';
                   2880:         foreach my $file (split(',',$$record{"resource.$partid.$respid.portfiles"})) {
                   2881:     	    push(@files,$file_url.$file);
                   2882:         }
                   2883:     }
                   2884:     if ($$record{"resource.$partid.$respid.uploadedurl"}) {
                   2885:         push(@files,$$record{"resource.$partid.$respid.uploadedurl"});
                   2886:     }
                   2887:     return (\@files);
                   2888: }
1.322     albertel 2889: 
1.269     raeburn  2890: # ----------- Provides number of tries since last reset.
                   2891: sub get_num_tries {
                   2892:     my ($record,$last_reset,$part) = @_;
                   2893:     my $timestamp = '';
                   2894:     my $num_tries = 0;
                   2895:     if ($$record{'version'}) {
                   2896:         for (my $version=$$record{'version'};$version>=1;$version--) {
                   2897:             if (exists($$record{$version.':resource.'.$part.'.solved'})) {
                   2898:                 $timestamp = $$record{$version.':timestamp'};
                   2899:                 if ($timestamp > $last_reset) {
                   2900:                     $num_tries ++;
                   2901:                 } else {
                   2902:                     last;
                   2903:                 }
                   2904:             }
                   2905:         }
                   2906:     }
                   2907:     return $num_tries;
                   2908: }
                   2909: 
                   2910: # ----------- Determine decrements required in aggregate totals 
                   2911: sub decrement_aggs {
                   2912:     my ($symb,$part,$aggregate,$aggtries,$totaltries,$solvedstatus) = @_;
                   2913:     my %decrement = (
                   2914:                         attempts => 0,
                   2915:                         users => 0,
                   2916:                         correct => 0
                   2917:                     );
                   2918:     $decrement{'attempts'} = $aggtries;
                   2919:     if ($solvedstatus =~ /^correct/) {
                   2920:         $decrement{'correct'} = 1;
                   2921:     }
                   2922:     if ($aggtries == $totaltries) {
                   2923:         $decrement{'users'} = 1;
                   2924:     }
1.524     raeburn  2925:     foreach my $type (keys(%decrement)) {
1.269     raeburn  2926:         $$aggregate{$symb."\0".$part."\0".$type} = -$decrement{$type};
                   2927:     }
                   2928:     return;
                   2929: }
                   2930: 
                   2931: # ----------- Determine timestamps for last reset of aggregate totals for parts  
                   2932: sub get_last_resets {
1.270     albertel 2933:     my ($symb,$courseid,$partids) =@_;
                   2934:     my %last_resets;
1.269     raeburn  2935:     my $cdom = $env{'course.'.$courseid.'.domain'};
                   2936:     my $cname = $env{'course.'.$courseid.'.num'};
1.271     albertel 2937:     my @keys;
                   2938:     foreach my $part (@{$partids}) {
                   2939: 	push(@keys,"$symb\0$part\0resettime");
                   2940:     }
                   2941:     my %results=&Apache::lonnet::get('nohist_resourcetracker',\@keys,
                   2942: 				     $cdom,$cname);
                   2943:     foreach my $part (@{$partids}) {
                   2944: 	$last_resets{$part}=$results{"$symb\0$part\0resettime"};
1.269     raeburn  2945:     }
1.270     albertel 2946:     return %last_resets;
1.269     raeburn  2947: }
                   2948: 
1.251     banghart 2949: # ----------- Handles creating versions for portfolio files as answers
                   2950: sub version_portfiles {
1.343     banghart 2951:     my ($record, $parts_graded, $courseid, $symb, $domain, $stu_name, $v_flag) = @_;
1.263     banghart 2952:     my $version_parts = join('|',@$v_flag);
1.343     banghart 2953:     my @returned_keys;
1.255     banghart 2954:     my $parts = join('|', @$parts_graded);
1.517     raeburn  2955:     my $portfolio_root = '/userfiles/portfolio';
1.277     albertel 2956:     foreach my $key (keys(%$record)) {
1.259     banghart 2957:         my $new_portfiles;
1.263     banghart 2958:         if ($key =~ /^resource\.($version_parts)\./ && $key =~ /\.portfiles$/ ) {
1.342     banghart 2959:             my @versioned_portfiles;
1.367     albertel 2960:             my @portfiles = split(/\s*,\s*/,$$record{$key});
1.252     banghart 2961:             foreach my $file (@portfiles) {
1.306     banghart 2962:                 &Apache::lonnet::unmark_as_readonly($domain,$stu_name,[$symb,$env{'request.course.id'}],$file);
1.304     albertel 2963:                 my ($directory,$answer_file) =($file =~ /^(.*?)([^\/]*)$/);
                   2964: 		my ($answer_name,$answer_ver,$answer_ext) =
                   2965: 		    &file_name_version_ext($answer_file);
1.517     raeburn  2966:                 my $getpropath = 1;    
                   2967:                 my @dir_list = &Apache::lonnet::dirlist($portfolio_root.$directory,$domain,$stu_name,$getpropath);
1.342     banghart 2968:                 my $version = &get_next_version($answer_name, $answer_ext, \@dir_list);
1.306     banghart 2969:                 my $new_answer = &version_selected_portfile($domain, $stu_name, $directory, $answer_file, $version);
                   2970:                 if ($new_answer ne 'problem getting file') {
1.342     banghart 2971:                     push(@versioned_portfiles, $directory.$new_answer);
1.306     banghart 2972:                     &Apache::lonnet::mark_as_readonly($domain,$stu_name,
1.367     albertel 2973:                         [$directory.$new_answer],
1.306     banghart 2974:                         [$symb,$env{'request.course.id'},'graded']);
1.259     banghart 2975:                 }
1.252     banghart 2976:             }
1.343     banghart 2977:             $$record{$key} = join(',',@versioned_portfiles);
                   2978:             push(@returned_keys,$key);
1.251     banghart 2979:         }
                   2980:     } 
1.343     banghart 2981:     return (@returned_keys);   
1.305     banghart 2982: }
                   2983: 
1.307     banghart 2984: sub get_next_version {
1.341     banghart 2985:     my ($answer_name, $answer_ext, $dir_list) = @_;
1.307     banghart 2986:     my $version;
                   2987:     foreach my $row (@$dir_list) {
                   2988:         my ($file) = split(/\&/,$row,2);
                   2989:         my ($file_name,$file_version,$file_ext) =
                   2990: 	    &file_name_version_ext($file);
                   2991:         if (($file_name eq $answer_name) && 
                   2992: 	    ($file_ext eq $answer_ext)) {
                   2993:                 # gets here if filename and extension match, regardless of version
                   2994:                 if ($file_version ne '') {
                   2995:                 # a versioned file is found  so save it for later
                   2996:                 if ($file_version > $version) {
                   2997: 		    $version = $file_version;
                   2998: 	        }
                   2999:             }
                   3000:         }
                   3001:     } 
                   3002:     $version ++;
                   3003:     return($version);
                   3004: }
                   3005: 
1.305     banghart 3006: sub version_selected_portfile {
1.306     banghart 3007:     my ($domain,$stu_name,$directory,$file_name,$version) = @_;
                   3008:     my ($answer_name,$answer_ver,$answer_ext) =
                   3009:         &file_name_version_ext($file_name);
                   3010:     my $new_answer;
                   3011:     $env{'form.copy'} = &Apache::lonnet::getfile("/uploaded/$domain/$stu_name/portfolio$directory$file_name");
                   3012:     if($env{'form.copy'} eq '-1') {
                   3013:         $new_answer = 'problem getting file';
                   3014:     } else {
                   3015:         $new_answer = $answer_name.'.'.$version.'.'.$answer_ext;
                   3016:         my $copy_result = &Apache::lonnet::finishuserfileupload(
                   3017:                             $stu_name,$domain,'copy',
                   3018: 		        '/portfolio'.$directory.$new_answer);
                   3019:     }    
                   3020:     return ($new_answer);
1.251     banghart 3021: }
                   3022: 
1.304     albertel 3023: sub file_name_version_ext {
                   3024:     my ($file)=@_;
                   3025:     my @file_parts = split(/\./, $file);
                   3026:     my ($name,$version,$ext);
                   3027:     if (@file_parts > 1) {
                   3028: 	$ext=pop(@file_parts);
                   3029: 	if (@file_parts > 1 && $file_parts[-1] =~ /^\d+$/) {
                   3030: 	    $version=pop(@file_parts);
                   3031: 	}
                   3032: 	$name=join('.',@file_parts);
                   3033:     } else {
                   3034: 	$name=join('.',@file_parts);
                   3035:     }
                   3036:     return($name,$version,$ext);
                   3037: }
                   3038: 
1.44      ng       3039: #--------------------------------------------------------------------------------------
                   3040: #
                   3041: #-------------------------- Next few routines handles grading by section or whole class
                   3042: #
                   3043: #--- Javascript to handle grading by section or whole class
1.42      ng       3044: sub viewgrades_js {
                   3045:     my ($request) = shift;
                   3046: 
1.539     riegler  3047:     my $alertmsg = &mt('A number equal or greater than 0 is expected. Entered value = ');
1.41      ng       3048:     $request->print(<<VIEWJAVASCRIPT);
                   3049: <script type="text/javascript" language="javascript">
1.45      ng       3050:    function writePoint(partid,weight,point) {
1.125     ng       3051: 	var radioButton = document.classgrade["RADVAL_"+partid];
                   3052: 	var textbox = document.classgrade["TEXTVAL_"+partid];
1.42      ng       3053: 	if (point == "textval") {
1.125     ng       3054: 	    point = document.classgrade["TEXTVAL_"+partid].value;
1.109     matthew  3055: 	    if (isNaN(point) || parseFloat(point) < 0) {
1.539     riegler  3056: 		alert("$alertmsg"+parseFloat(point));
1.42      ng       3057: 		var resetbox = false;
                   3058: 		for (var i=0; i<radioButton.length; i++) {
                   3059: 		    if (radioButton[i].checked) {
                   3060: 			textbox.value = i;
                   3061: 			resetbox = true;
                   3062: 		    }
                   3063: 		}
                   3064: 		if (!resetbox) {
                   3065: 		    textbox.value = "";
                   3066: 		}
                   3067: 		return;
                   3068: 	    }
1.109     matthew  3069: 	    if (parseFloat(point) > parseFloat(weight)) {
                   3070: 		var resp = confirm("You entered a value ("+parseFloat(point)+
1.44      ng       3071: 				   ") greater than the weight for the part. Accept?");
                   3072: 		if (resp == false) {
                   3073: 		    textbox.value = "";
                   3074: 		    return;
                   3075: 		}
                   3076: 	    }
1.42      ng       3077: 	    for (var i=0; i<radioButton.length; i++) {
                   3078: 		radioButton[i].checked=false;
1.109     matthew  3079: 		if (parseFloat(point) == i) {
1.42      ng       3080: 		    radioButton[i].checked=true;
                   3081: 		}
                   3082: 	    }
1.41      ng       3083: 
1.42      ng       3084: 	} else {
1.125     ng       3085: 	    textbox.value = parseFloat(point);
1.42      ng       3086: 	}
1.41      ng       3087: 	for (i=0;i<document.classgrade.total.value;i++) {
1.125     ng       3088: 	    var user = document.classgrade["ctr"+i].value;
1.289     albertel 3089: 	    user = user.replace(new RegExp(':', 'g'),"_");
1.125     ng       3090: 	    var scorename = document.classgrade["GD_"+user+"_"+partid+"_awarded"];
                   3091: 	    var saveval   = document.classgrade["GD_"+user+"_"+partid+"_solved_s"].value;
                   3092: 	    var selname   = document.classgrade["GD_"+user+"_"+partid+"_solved"];
1.42      ng       3093: 	    if (saveval != "correct") {
                   3094: 		scorename.value = point;
1.43      ng       3095: 		if (selname[0].selected != true) {
                   3096: 		    selname[0].selected = true;
                   3097: 		}
1.42      ng       3098: 	    }
                   3099: 	}
1.125     ng       3100: 	document.classgrade["SELVAL_"+partid][0].selected = true;
1.42      ng       3101:     }
                   3102: 
                   3103:     function writeRadText(partid,weight) {
1.125     ng       3104: 	var selval   = document.classgrade["SELVAL_"+partid];
                   3105: 	var radioButton = document.classgrade["RADVAL_"+partid];
1.265     www      3106:         var override = document.classgrade["FORCE_"+partid].checked;
1.125     ng       3107: 	var textbox = document.classgrade["TEXTVAL_"+partid];
                   3108: 	if (selval[1].selected || selval[2].selected) {
1.42      ng       3109: 	    for (var i=0; i<radioButton.length; i++) {
                   3110: 		radioButton[i].checked=false;
                   3111: 
                   3112: 	    }
                   3113: 	    textbox.value = "";
                   3114: 
                   3115: 	    for (i=0;i<document.classgrade.total.value;i++) {
1.125     ng       3116: 		var user = document.classgrade["ctr"+i].value;
1.289     albertel 3117: 		user = user.replace(new RegExp(':', 'g'),"_");
1.125     ng       3118: 		var scorename = document.classgrade["GD_"+user+"_"+partid+"_awarded"];
                   3119: 		var saveval   = document.classgrade["GD_"+user+"_"+partid+"_solved_s"].value;
                   3120: 		var selname   = document.classgrade["GD_"+user+"_"+partid+"_solved"];
1.265     www      3121: 		if ((saveval != "correct") || override) {
1.42      ng       3122: 		    scorename.value = "";
1.125     ng       3123: 		    if (selval[1].selected) {
                   3124: 			selname[1].selected = true;
                   3125: 		    } else {
                   3126: 			selname[2].selected = true;
                   3127: 			if (Number(document.classgrade["GD_"+user+"_"+partid+"_tries"].value)) 
                   3128: 			{document.classgrade["GD_"+user+"_"+partid+"_tries"].value = '0';}
                   3129: 		    }
1.42      ng       3130: 		}
                   3131: 	    }
1.43      ng       3132: 	} else {
                   3133: 	    for (i=0;i<document.classgrade.total.value;i++) {
1.125     ng       3134: 		var user = document.classgrade["ctr"+i].value;
1.289     albertel 3135: 		user = user.replace(new RegExp(':', 'g'),"_");
1.125     ng       3136: 		var scorename = document.classgrade["GD_"+user+"_"+partid+"_awarded"];
                   3137: 		var saveval   = document.classgrade["GD_"+user+"_"+partid+"_solved_s"].value;
                   3138: 		var selname   = document.classgrade["GD_"+user+"_"+partid+"_solved"];
1.265     www      3139: 		if ((saveval != "correct") || override) {
1.125     ng       3140: 		    scorename.value = document.classgrade["GD_"+user+"_"+partid+"_awarded_s"].value;
1.43      ng       3141: 		    selname[0].selected = true;
                   3142: 		}
                   3143: 	    }
                   3144: 	}	    
1.42      ng       3145:     }
                   3146: 
                   3147:     function changeSelect(partid,user) {
1.125     ng       3148: 	var selval = document.classgrade["GD_"+user+'_'+partid+"_solved"];
                   3149: 	var textbox = document.classgrade["GD_"+user+'_'+partid+"_awarded"];
1.44      ng       3150: 	var point  = textbox.value;
1.125     ng       3151: 	var weight = document.classgrade["weight_"+partid].value;
1.44      ng       3152: 
1.109     matthew  3153: 	if (isNaN(point) || parseFloat(point) < 0) {
1.539     riegler  3154: 	    alert("$alertmsg"+parseFloat(point));
1.44      ng       3155: 	    textbox.value = "";
                   3156: 	    return;
                   3157: 	}
1.109     matthew  3158: 	if (parseFloat(point) > parseFloat(weight)) {
                   3159: 	    var resp = confirm("You entered a value ("+parseFloat(point)+
1.44      ng       3160: 			       ") greater than the weight of the part. Accept?");
                   3161: 	    if (resp == false) {
                   3162: 		textbox.value = "";
                   3163: 		return;
                   3164: 	    }
                   3165: 	}
1.42      ng       3166: 	selval[0].selected = true;
                   3167:     }
                   3168: 
                   3169:     function changeOneScore(partid,user) {
1.125     ng       3170: 	var selval = document.classgrade["GD_"+user+'_'+partid+"_solved"];
                   3171: 	if (selval[1].selected || selval[2].selected) {
                   3172: 	    document.classgrade["GD_"+user+'_'+partid+"_awarded"].value = "";
                   3173: 	    if (selval[2].selected) {
                   3174: 		document.classgrade["GD_"+user+'_'+partid+"_tries"].value = "0";
                   3175: 	    }
1.269     raeburn  3176:         }
1.42      ng       3177:     }
                   3178: 
                   3179:     function resetEntry(numpart) {
                   3180: 	for (ctpart=0;ctpart<numpart;ctpart++) {
1.125     ng       3181: 	    var partid = document.classgrade["partid_"+ctpart].value;
                   3182: 	    var radioButton = document.classgrade["RADVAL_"+partid];
                   3183: 	    var textbox = document.classgrade["TEXTVAL_"+partid];
                   3184: 	    var selval  = document.classgrade["SELVAL_"+partid];
1.42      ng       3185: 	    for (var i=0; i<radioButton.length; i++) {
                   3186: 		radioButton[i].checked=false;
                   3187: 
                   3188: 	    }
                   3189: 	    textbox.value = "";
                   3190: 	    selval[0].selected = true;
                   3191: 
                   3192: 	    for (i=0;i<document.classgrade.total.value;i++) {
1.125     ng       3193: 		var user = document.classgrade["ctr"+i].value;
1.289     albertel 3194: 		user = user.replace(new RegExp(':', 'g'),"_");
1.125     ng       3195: 		var resetscore = document.classgrade["GD_"+user+"_"+partid+"_awarded"];
                   3196: 		resetscore.value = document.classgrade["GD_"+user+"_"+partid+"_awarded_s"].value;
                   3197: 		var resettries = document.classgrade["GD_"+user+"_"+partid+"_tries"];
                   3198: 		resettries.value = document.classgrade["GD_"+user+"_"+partid+"_tries_s"].value;
                   3199: 		var saveselval   = document.classgrade["GD_"+user+"_"+partid+"_solved_s"].value;
                   3200: 		var selname   = document.classgrade["GD_"+user+"_"+partid+"_solved"];
1.42      ng       3201: 		if (saveselval == "excused") {
1.43      ng       3202: 		    if (selname[1].selected == false) { selname[1].selected = true;}
1.42      ng       3203: 		} else {
1.43      ng       3204: 		    if (selname[0].selected == false) {selname[0].selected = true};
1.42      ng       3205: 		}
                   3206: 	    }
1.41      ng       3207: 	}
1.42      ng       3208:     }
                   3209: 
1.41      ng       3210: </script>
                   3211: VIEWJAVASCRIPT
1.42      ng       3212: }
                   3213: 
1.44      ng       3214: #--- show scores for a section or whole class w/ option to change/update a score
1.42      ng       3215: sub viewgrades {
                   3216:     my ($request) = shift;
                   3217:     &viewgrades_js($request);
1.41      ng       3218: 
1.324     albertel 3219:     my ($symb) = &get_symb($request);
1.168     albertel 3220:     #need to make sure we have the correct data for later EXT calls, 
                   3221:     #thus invalidate the cache
                   3222:     &Apache::lonnet::devalidatecourseresdata(
1.257     albertel 3223:                  $env{'course.'.$env{'request.course.id'}.'.num'},
                   3224:                  $env{'course.'.$env{'request.course.id'}.'.domain'});
1.168     albertel 3225:     &Apache::lonnet::clear_EXT_cache_status();
                   3226: 
1.398     albertel 3227:     my $result='<h3><span class="LC_info">'.&mt('Manual Grading').'</span></h3>';
1.485     albertel 3228:     $result.='<h4>'.&mt('<b>Current Resource: </b>[_1]',$env{'form.probTitle'}).'</h4>'."\n";
1.41      ng       3229: 
                   3230:     #view individual student submission form - called using Javascript viewOneStudent
1.324     albertel 3231:     $result.=&jscriptNform($symb);
1.41      ng       3232: 
1.44      ng       3233:     #beginning of class grading form
1.442     banghart 3234:     my $stu_status = join(':',&Apache::loncommon::get_env_multiple('form.Status'));
1.41      ng       3235:     $result.= '<form action="/adm/grades" method="post" name="classgrade">'."\n".
1.418     albertel 3236: 	'<input type="hidden" name="symb"    value="'.&Apache::lonenc::check_encrypt($symb).'" />'."\n".
1.38      ng       3237: 	'<input type="hidden" name="command" value="editgrades" />'."\n".
1.432     banghart 3238: 	&build_section_inputs().
1.257     albertel 3239: 	'<input type="hidden" name="saveState" value="'.$env{'form.saveState'}.'" />'."\n".
1.442     banghart 3240: 	'<input type="hidden" name="Status" value="'.$env{'stu_status'}.'" />'."\n".
1.257     albertel 3241: 	'<input type="hidden" name="probTitle" value="'.$env{'form.probTitle'}.'" />'."\n";
1.72      ng       3242: 
1.560     raeburn  3243:     my ($common_header,$specific_header);
1.257     albertel 3244:     if ($env{'form.section'} eq 'all') {
1.560     raeburn  3245: 	$common_header = &mt('Assign Common Grade to Class');
                   3246:         $specific_header = &mt('Assign Grade to Specific Students in Class');
1.257     albertel 3247:     } elsif ($env{'form.section'} eq 'none') {
1.560     raeburn  3248:         $common_header = &mt('Assign Common Grade to Students in no Section');
                   3249: 	$specific_header = &mt('Assign Grade to Specific Students in no Section');
1.52      albertel 3250:     } else {
1.560     raeburn  3251:         my $section_display = join (", ",&Apache::loncommon::get_env_multiple('form.section'));
                   3252:         $common_header = &mt('Assign Common Grade to Students in Section(s) [_1]',$section_display);
                   3253: 	$specific_header = &mt('Assign Grade to Specific Students in Section(s) [_1]',$section_display);
1.52      albertel 3254:     }
1.560     raeburn  3255:     $result.= '<h3>'.$common_header.'</h3>'.&Apache::loncommon::start_data_table();
1.44      ng       3256:     #radio buttons/text box for assigning points for a section or class.
                   3257:     #handles different parts of a problem
1.375     albertel 3258:     my ($partlist,$handgrade,$responseType) = &response_type($symb);
1.42      ng       3259:     my %weight = ();
                   3260:     my $ctsparts = 0;
1.45      ng       3261:     my %seen = ();
1.375     albertel 3262:     my @part_response_id = &flatten_responseType($responseType);
                   3263:     foreach my $part_response_id (@part_response_id) {
                   3264:     	my ($partid,$respid) = @{ $part_response_id };
                   3265: 	my $part_resp = join('_',@{ $part_response_id });
1.45      ng       3266: 	next if $seen{$partid};
                   3267: 	$seen{$partid}++;
1.375     albertel 3268: 	my $handgrade=$$handgrade{$part_resp};
1.42      ng       3269: 	my $wgt = &Apache::lonnet::EXT('resource.'.$partid.'.weight',$symb);
                   3270: 	$weight{$partid} = $wgt eq '' ? '1' : $wgt;
                   3271: 
1.324     albertel 3272: 	my $display_part=&get_display_part($partid,$symb);
1.485     albertel 3273: 	my $radio.='<table border="0"><tr>';  
1.41      ng       3274: 	my $ctr = 0;
1.42      ng       3275: 	while ($ctr<=$weight{$partid}) { # display radio buttons in a nice table 10 across
1.485     albertel 3276: 	    $radio.= '<td><label><input type="radio" name="RADVAL_'.$partid.'" '.
1.54      albertel 3277: 		'onclick="javascript:writePoint(\''.$partid.'\','.$weight{$partid}.
1.288     albertel 3278: 		','.$ctr.')" />'.$ctr."</label></td>\n";
1.41      ng       3279: 	    $result.=(($ctr+1)%10 == 0 ? '</tr><tr>' : '');
                   3280: 	    $ctr++;
                   3281: 	}
1.485     albertel 3282: 	$radio.='</tr></table>';
                   3283: 	my $line = '<input type="text" name="TEXTVAL_'.
1.54      albertel 3284: 	    $partid.'" size="4" '.'onChange="javascript:writePoint(\''.
                   3285: 		$partid.'\','.$weight{$partid}.',\'textval\')" /> /'.
1.539     riegler  3286: 	    $weight{$partid}.' '.&mt('(problem weight)').'</td>'."\n";
                   3287: 	$line.= '<td><b>'.&mt('Grade Status').':</b><select name="SELVAL_'.$partid.'"'.
1.54      albertel 3288: 	    'onChange="javascript:writeRadText(\''.$partid.'\','.
1.59      albertel 3289: 		$weight{$partid}.')"> '.
1.401     albertel 3290: 	    '<option selected="selected"> </option>'.
1.485     albertel 3291: 	    '<option value="excused">'.&mt('excused').'</option>'.
                   3292: 	    '<option value="reset status">'.&mt('reset status').'</option>'.
                   3293: 	    '</select></td>'.
                   3294:             '<td><label><input type="checkbox" name="FORCE_'.$partid.'" />'.&mt('Override "Correct"').'</label>';
                   3295: 	$line.='<input type="hidden" name="partid_'.
                   3296: 	    $ctsparts.'" value="'.$partid.'" />'."\n";
                   3297: 	$line.='<input type="hidden" name="weight_'.
                   3298: 	    $partid.'" value="'.$weight{$partid}.'" />'."\n";
                   3299: 
                   3300: 	$result.=
                   3301: 	    &Apache::loncommon::start_data_table_row()."\n".
1.577     bisitz   3302: 	    '<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>'.
1.485     albertel 3303: 	    &Apache::loncommon::end_data_table_row()."\n";
1.42      ng       3304: 	$ctsparts++;
1.41      ng       3305:     }
1.474     albertel 3306:     $result.=&Apache::loncommon::end_data_table()."\n".
1.52      albertel 3307: 	'<input type="hidden" name="totalparts" value="'.$ctsparts.'" />';
1.485     albertel 3308:     $result.='<input type="button" value="'.&mt('Revert to Default').'" '.
1.474     albertel 3309: 	'onClick="javascript:resetEntry('.$ctsparts.');" />';
1.41      ng       3310: 
1.44      ng       3311:     #table listing all the students in a section/class
                   3312:     #header of table
1.560     raeburn  3313:     $result.= '<h3>'.$specific_header.'</h3>'.
                   3314:               &Apache::loncommon::start_data_table().
                   3315: 	      &Apache::loncommon::start_data_table_header_row().
                   3316: 	      '<th>'.&mt('No.').'</th>'.
                   3317: 	      '<th>'.&nameUserString('header')."</th>\n";
1.324     albertel 3318:     my (@parts) = sort(&getpartlist($symb));
                   3319:     my (undef,undef,$url)=&Apache::lonnet::decode_symb($symb);
1.269     raeburn  3320:     my @partids = ();
1.41      ng       3321:     foreach my $part (@parts) {
                   3322: 	my $display=&Apache::lonnet::metadata($url,$part.'.display');
1.539     riegler  3323:         my $narrowtext = &mt('Tries');
                   3324: 	$display =~ s|^Number of Attempts|$narrowtext <br />|; # makes the column narrower
1.41      ng       3325: 	if  (!$display) { $display = &Apache::lonnet::metadata($url,$part.'.name'); }
1.207     albertel 3326: 	my ($partid) = &split_part_type($part);
1.524     raeburn  3327:         push(@partids,$partid);
1.324     albertel 3328: 	my $display_part=&get_display_part($partid,$symb);
1.41      ng       3329: 	if ($display =~ /^Partial Credit Factor/) {
1.485     albertel 3330: 	    $result.='<th>'.
                   3331: 		&mt('Score Part: [_1]<br /> (weight = [_2])',
                   3332: 		    $display_part,$weight{$partid}).'</th>'."\n";
1.41      ng       3333: 	    next;
1.485     albertel 3334: 	    
1.207     albertel 3335: 	} else {
1.485     albertel 3336: 	    if ($display =~ /Problem Status/) {
                   3337: 		my $grade_status_mt = &mt('Grade Status');
                   3338: 		$display =~ s{Problem Status}{$grade_status_mt<br />};
                   3339: 	    }
                   3340: 	    my $part_mt = &mt('Part:');
                   3341: 	    $display =~s{\[Part: \Q$partid\E\]}{$part_mt $display_part};
1.41      ng       3342: 	}
1.485     albertel 3343: 
1.474     albertel 3344: 	$result.='<th>'.$display.'</th>'."\n";
1.41      ng       3345:     }
1.474     albertel 3346:     $result.=&Apache::loncommon::end_data_table_header_row();
1.44      ng       3347: 
1.270     albertel 3348:     my %last_resets = 
                   3349: 	&get_last_resets($symb,$env{'request.course.id'},\@partids);
1.269     raeburn  3350: 
1.41      ng       3351:     #get info for each student
1.44      ng       3352:     #list all the students - with points and grade status
1.257     albertel 3353:     my (undef,undef,$fullname) = &getclasslist($env{'form.section'},'1');
1.41      ng       3354:     my $ctr = 0;
1.294     albertel 3355:     foreach (sort 
                   3356: 	     {
                   3357: 		 if (lc($$fullname{$a}) ne lc($$fullname{$b})) {
                   3358: 		     return (lc($$fullname{$a}) cmp lc($$fullname{$b}));
                   3359: 		 }
                   3360: 		 return $a cmp $b;
                   3361: 	     } (keys(%$fullname))) {
1.126     ng       3362: 	$ctr++;
1.324     albertel 3363: 	$result.=&viewstudentgrade($symb,$env{'request.course.id'},
1.269     raeburn  3364: 				   $_,$$fullname{$_},\@parts,\%weight,$ctr,\%last_resets);
1.41      ng       3365:     }
1.474     albertel 3366:     $result.=&Apache::loncommon::end_data_table();
1.41      ng       3367:     $result.='<input type="hidden" name="total" value="'.$ctr.'" />'."\n";
1.485     albertel 3368:     $result.='<input type="button" value="'.&mt('Save').'" '.
1.417     albertel 3369: 	'onClick="javascript:submit();" target="_self" /></form>'."\n";
1.96      albertel 3370:     if (scalar(%$fullname) eq 0) {
                   3371: 	my $colspan=3+scalar(@parts);
1.433     banghart 3372: 	my $section_display = join (", ",&Apache::loncommon::get_env_multiple('form.section'));
1.442     banghart 3373:         my $stu_status = join(' or ',&Apache::loncommon::get_env_multiple('form.Status'));
1.433     banghart 3374: 	$result='<span class="LC_warning">'.
1.485     albertel 3375: 	    &mt('There are no students in section(s) [_1] with enrollment status [_2] to modify or grade.',
1.442     banghart 3376: 	        $section_display, $stu_status).
1.433     banghart 3377: 	    '</span>';
1.96      albertel 3378:     }
1.324     albertel 3379:     $result.=&show_grading_menu_form($symb);
1.41      ng       3380:     return $result;
                   3381: }
                   3382: 
1.44      ng       3383: #--- call by previous routine to display each student
1.41      ng       3384: sub viewstudentgrade {
1.324     albertel 3385:     my ($symb,$courseid,$student,$fullname,$parts,$weight,$ctr,$last_resets) = @_;
1.44      ng       3386:     my ($uname,$udom) = split(/:/,$student);
                   3387:     my %record=&Apache::lonnet::restore($symb,$courseid,$udom,$uname);
1.269     raeburn  3388:     my %aggregates = (); 
1.474     albertel 3389:     my $result=&Apache::loncommon::start_data_table_row().'<td align="right">'.
1.233     albertel 3390: 	'<input type="hidden" name="ctr'.($ctr-1).'" value="'.$student.'" />'.
                   3391: 	"\n".$ctr.'&nbsp;</td><td>&nbsp;'.
1.44      ng       3392: 	'<a href="javascript:viewOneStudent(\''.$uname.'\',\''.$udom.
1.417     albertel 3393: 	'\');" target="_self">'.$fullname.'</a> '.
1.398     albertel 3394: 	'<span class="LC_internal_info">('.$uname.($env{'user.domain'} eq $udom ? '' : ':'.$udom).')</span></td>'."\n";
1.281     albertel 3395:     $student=~s/:/_/; # colon doen't work in javascript for names
1.63      albertel 3396:     foreach my $apart (@$parts) {
                   3397: 	my ($part,$type) = &split_part_type($apart);
1.41      ng       3398: 	my $score=$record{"resource.$part.$type"};
1.276     albertel 3399:         $result.='<td align="center">';
1.269     raeburn  3400:         my ($aggtries,$totaltries);
                   3401:         unless (exists($aggregates{$part})) {
1.270     albertel 3402: 	    $totaltries = $record{'resource.'.$part.'.tries'};
                   3403: 
                   3404: 	    $aggtries = $totaltries;
1.269     raeburn  3405:             if ($$last_resets{$part}) {  
1.270     albertel 3406:                 $aggtries = &get_num_tries(\%record,$$last_resets{$part},
                   3407: 					   $part);
                   3408:             }
1.269     raeburn  3409:             $result.='<input type="hidden" name="'.
                   3410:                 'GD_'.$student.'_'.$part.'_aggtries" value="'.$aggtries.'" />'."\n";
                   3411:             $result.='<input type="hidden" name="'.
                   3412:                 'GD_'.$student.'_'.$part.'_totaltries" value="'.$totaltries.'" />'."\n";
                   3413:             $aggregates{$part} = 1;
                   3414:         }
1.41      ng       3415: 	if ($type eq 'awarded') {
1.320     albertel 3416: 	    my $pts = $score eq '' ? '' : &compute_points($score,$$weight{$part});
1.42      ng       3417: 	    $result.='<input type="hidden" name="'.
1.89      albertel 3418: 		'GD_'.$student.'_'.$part.'_awarded_s" value="'.$pts.'" />'."\n";
1.233     albertel 3419: 	    $result.='<input type="text" name="'.
1.89      albertel 3420: 		'GD_'.$student.'_'.$part.'_awarded" '.
                   3421: 		'onChange="javascript:changeSelect(\''.$part.'\',\''.$student.
1.44      ng       3422: 		'\')" value="'.$pts.'" size="4" /></td>'."\n";
1.41      ng       3423: 	} elsif ($type eq 'solved') {
                   3424: 	    my ($status,$foo)=split(/_/,$score,2);
                   3425: 	    $status = 'nothing' if ($status eq '');
1.89      albertel 3426: 	    $result.='<input type="hidden" name="'.'GD_'.$student.'_'.
1.54      albertel 3427: 		$part.'_solved_s" value="'.$status.'" />'."\n";
1.233     albertel 3428: 	    $result.='&nbsp;<select name="'.
1.89      albertel 3429: 		'GD_'.$student.'_'.$part.'_solved" '.
                   3430: 		'onChange="javascript:changeOneScore(\''.$part.'\',\''.$student.'\')" >'."\n";
1.485     albertel 3431: 	    $result.= (($status eq 'excused') ? '<option> </option><option selected="selected" value="excused">'.&mt('excused').'</option>' 
                   3432: 		: '<option selected="selected"> </option><option value="excused">'.&mt('excused').'</option>')."\n";
                   3433: 	    $result.='<option value="reset status">'.&mt('reset status').'</option>';
1.126     ng       3434: 	    $result.="</select>&nbsp;</td>\n";
1.122     ng       3435: 	} else {
                   3436: 	    $result.='<input type="hidden" name="'.
                   3437: 		'GD_'.$student.'_'.$part.'_'.$type.'_s" value="'.$score.'" />'.
                   3438: 		    "\n";
1.233     albertel 3439: 	    $result.='<input type="text" name="'.
1.122     ng       3440: 		'GD_'.$student.'_'.$part.'_'.$type.'" '.
                   3441: 		'value="'.$score.'" size="4" /></td>'."\n";
1.41      ng       3442: 	}
                   3443:     }
1.474     albertel 3444:     $result.=&Apache::loncommon::end_data_table_row();
1.41      ng       3445:     return $result;
1.38      ng       3446: }
                   3447: 
1.44      ng       3448: #--- change scores for all the students in a section/class
                   3449: #    record does not get update if unchanged
1.38      ng       3450: sub editgrades {
1.41      ng       3451:     my ($request) = @_;
                   3452: 
1.324     albertel 3453:     my $symb=&get_symb($request);
1.433     banghart 3454:     my $section_display = join (", ",&Apache::loncommon::get_env_multiple('form.section'));
1.477     albertel 3455:     my $title='<h2>'.&mt('Current Grade Status').'</h2>';
                   3456:     $title.='<h4>'.&mt('<b>Current Resource: </b>[_1]',$env{'form.probTitle'}).'</h4>'."\n";
1.433     banghart 3457:     $title.='<h4>'.&mt('<b>Section: </b>[_1]',$section_display).'</h4>'."\n";
1.126     ng       3458: 
1.477     albertel 3459:     my $result= &Apache::loncommon::start_data_table().
                   3460: 	&Apache::loncommon::start_data_table_header_row().
                   3461: 	'<th rowspan="2" valign="middle">'.&mt('No.').'</th>'.
                   3462: 	'<th rowspan="2" valign="middle">'.&nameUserString('header')."</th>\n";
1.43      ng       3463:     my %scoreptr = (
                   3464: 		    'correct'  =>'correct_by_override',
                   3465: 		    'incorrect'=>'incorrect_by_override',
                   3466: 		    'excused'  =>'excused',
                   3467: 		    'ungraded' =>'ungraded_attempted',
                   3468: 		    'nothing'  => '',
                   3469: 		    );
1.257     albertel 3470:     my ($classlist,undef,$fullname) = &getclasslist($env{'form.section'},'0');
1.34      ng       3471: 
1.44      ng       3472:     my (@partid);
                   3473:     my %weight = ();
1.54      albertel 3474:     my %columns = ();
1.44      ng       3475:     my ($i,$ctr,$count,$rec_update) = (0,0,0,0);
1.54      albertel 3476: 
1.324     albertel 3477:     my (@parts) = sort(&getpartlist($symb));
1.54      albertel 3478:     my $header;
1.257     albertel 3479:     while ($ctr < $env{'form.totalparts'}) {
                   3480: 	my $partid = $env{'form.partid_'.$ctr};
1.524     raeburn  3481: 	push(@partid,$partid);
1.257     albertel 3482: 	$weight{$partid} = $env{'form.weight_'.$partid};
1.44      ng       3483: 	$ctr++;
1.54      albertel 3484:     }
1.324     albertel 3485:     my (undef,undef,$url) = &Apache::lonnet::decode_symb($symb);
1.54      albertel 3486:     foreach my $partid (@partid) {
1.478     albertel 3487: 	$header .= '<th align="center">'.&mt('Old Score').'</th>'.
                   3488: 	    '<th align="center">'.&mt('New Score').'</th>';
1.54      albertel 3489: 	$columns{$partid}=2;
                   3490: 	foreach my $stores (@parts) {
                   3491: 	    my ($part,$type) = &split_part_type($stores);
                   3492: 	    if ($part !~ m/^\Q$partid\E/) { next;}
                   3493: 	    if ($type eq 'awarded' || $type eq 'solved') { next; }
                   3494: 	    my $display=&Apache::lonnet::metadata($url,$stores.'.display');
1.551     raeburn  3495: 	    $display =~ s/\[Part: \Q$part\E\]//;
1.539     riegler  3496:             my $narrowtext = &mt('Tries');
                   3497: 	    $display =~ s/Number of Attempts/$narrowtext/;
                   3498: 	    $header .= '<th align="center">'.&mt('Old').' '.$display.'</th>'.
                   3499: 		'<th align="center">'.&mt('New').' '.$display.'</th>';
1.54      albertel 3500: 	    $columns{$partid}+=2;
                   3501: 	}
                   3502:     }
                   3503:     foreach my $partid (@partid) {
1.324     albertel 3504: 	my $display_part=&get_display_part($partid,$symb);
1.478     albertel 3505: 	$result .= '<th colspan="'.$columns{$partid}.'" align="center">'.
                   3506: 	    &mt('Part: [_1] (Weight = [_2])',$display_part,$weight{$partid}).
                   3507: 	    '</th>';
1.54      albertel 3508: 
1.44      ng       3509:     }
1.477     albertel 3510:     $result .= &Apache::loncommon::end_data_table_header_row().
                   3511: 	&Apache::loncommon::start_data_table_header_row().
                   3512: 	$header.
                   3513: 	&Apache::loncommon::end_data_table_header_row();
                   3514:     my @noupdate;
1.126     ng       3515:     my ($updateCtr,$noupdateCtr) = (1,1);
1.257     albertel 3516:     for ($i=0; $i<$env{'form.total'}; $i++) {
1.93      albertel 3517: 	my $line;
1.257     albertel 3518: 	my $user = $env{'form.ctr'.$i};
1.281     albertel 3519: 	my ($uname,$udom)=split(/:/,$user);
1.44      ng       3520: 	my %newrecord;
                   3521: 	my $updateflag = 0;
1.281     albertel 3522: 	$line .= '<td>'.&nameUserString(undef,$$fullname{$user},$uname,$udom).'</td>';
1.108     albertel 3523: 	my $usec=$classlist->{"$uname:$udom"}[5];
1.105     albertel 3524: 	if (!&canmodify($usec)) {
1.126     ng       3525: 	    my $numcols=scalar(@partid)*4+2;
1.477     albertel 3526: 	    push(@noupdate,
1.478     albertel 3527: 		 $line."<td colspan=\"$numcols\"><span class=\"LC_warning\">".
                   3528: 		 &mt('Not allowed to modify student')."</span></td></tr>");
1.105     albertel 3529: 	    next;
                   3530: 	}
1.269     raeburn  3531:         my %aggregate = ();
                   3532:         my $aggregateflag = 0;
1.281     albertel 3533: 	$user=~s/:/_/; # colon doen't work in javascript for names
1.44      ng       3534: 	foreach (@partid) {
1.257     albertel 3535: 	    my $old_aw    = $env{'form.GD_'.$user.'_'.$_.'_awarded_s'};
1.54      albertel 3536: 	    my $old_part_pcr = $old_aw/($weight{$_} ne '0' ? $weight{$_}:1);
                   3537: 	    my $old_part  = $old_aw eq '' ? '' : $old_part_pcr;
1.257     albertel 3538: 	    my $old_score = $scoreptr{$env{'form.GD_'.$user.'_'.$_.'_solved_s'}};
                   3539: 	    my $awarded   = $env{'form.GD_'.$user.'_'.$_.'_awarded'};
1.54      albertel 3540: 	    my $pcr       = $awarded/($weight{$_} ne '0' ? $weight{$_} : 1);
                   3541: 	    my $partial   = $awarded eq '' ? '' : $pcr;
1.44      ng       3542: 	    my $score;
                   3543: 	    if ($partial eq '') {
1.257     albertel 3544: 		$score = $scoreptr{$env{'form.GD_'.$user.'_'.$_.'_solved_s'}};
1.44      ng       3545: 	    } elsif ($partial > 0) {
                   3546: 		$score = 'correct_by_override';
                   3547: 	    } elsif ($partial == 0) {
                   3548: 		$score = 'incorrect_by_override';
                   3549: 	    }
1.257     albertel 3550: 	    my $dropMenu = $env{'form.GD_'.$user.'_'.$_.'_solved'};
1.125     ng       3551: 	    $score = 'excused' if (($dropMenu eq 'excused') && ($score ne 'excused'));
                   3552: 
1.292     albertel 3553: 	    $newrecord{'resource.'.$_.'.regrader'}=
                   3554: 		"$env{'user.name'}:$env{'user.domain'}";
1.125     ng       3555: 	    if ($dropMenu eq 'reset status' &&
                   3556: 		$old_score ne '') { # ignore if no previous attempts => nothing to reset
1.299     albertel 3557: 		$newrecord{'resource.'.$_.'.tries'} = '';
1.125     ng       3558: 		$newrecord{'resource.'.$_.'.solved'} = '';
                   3559: 		$newrecord{'resource.'.$_.'.award'} = '';
1.299     albertel 3560: 		$newrecord{'resource.'.$_.'.awarded'} = '';
1.125     ng       3561: 		$updateflag = 1;
1.269     raeburn  3562:                 if ($env{'form.GD_'.$user.'_'.$_.'_aggtries'} > 0) {
                   3563:                     my $aggtries = $env{'form.GD_'.$user.'_'.$_.'_aggtries'};
                   3564:                     my $totaltries = $env{'form.GD_'.$user.'_'.$_.'_totaltries'};
                   3565:                     my $solvedstatus = $env{'form.GD_'.$user.'_'.$_.'_solved_s'};
                   3566:                     &decrement_aggs($symb,$_,\%aggregate,$aggtries,$totaltries,$solvedstatus);
                   3567:                     $aggregateflag = 1;
                   3568:                 }
1.139     albertel 3569: 	    } elsif (!($old_part eq $partial && $old_score eq $score)) {
                   3570: 		$updateflag = 1;
                   3571: 		$newrecord{'resource.'.$_.'.awarded'}  = $partial if $partial ne '';
                   3572: 		$newrecord{'resource.'.$_.'.solved'}   = $score;
                   3573: 		$rec_update++;
1.125     ng       3574: 	    }
                   3575: 
1.93      albertel 3576: 	    $line .= '<td align="center">'.$old_aw.'&nbsp;</td>'.
1.44      ng       3577: 		'<td align="center">'.$awarded.
                   3578: 		($score eq 'excused' ? $score : '').'&nbsp;</td>';
1.5       albertel 3579: 
1.54      albertel 3580: 
                   3581: 	    my $partid=$_;
                   3582: 	    foreach my $stores (@parts) {
                   3583: 		my ($part,$type) = &split_part_type($stores);
                   3584: 		if ($part !~ m/^\Q$partid\E/) { next;}
                   3585: 		if ($type eq 'awarded' || $type eq 'solved') { next; }
1.257     albertel 3586: 		my $old_aw    = $env{'form.GD_'.$user.'_'.$part.'_'.$type.'_s'};
                   3587: 		my $awarded   = $env{'form.GD_'.$user.'_'.$part.'_'.$type};
1.54      albertel 3588: 		if ($awarded ne '' && $awarded ne $old_aw) {
                   3589: 		    $newrecord{'resource.'.$part.'.'.$type}= $awarded;
1.257     albertel 3590: 		    $newrecord{'resource.'.$part.'.regrader'}="$env{'user.name'}:$env{'user.domain'}";
1.54      albertel 3591: 		    $updateflag=1;
                   3592: 		}
1.93      albertel 3593: 		$line .= '<td align="center">'.$old_aw.'&nbsp;</td>'.
1.54      albertel 3594: 		    '<td align="center">'.$awarded.'&nbsp;</td>';
                   3595: 	    }
1.44      ng       3596: 	}
1.477     albertel 3597: 	$line.="\n";
1.301     albertel 3598: 
                   3599: 	my $cdom = $env{'course.'.$env{'request.course.id'}.'.domain'};
                   3600: 	my $cnum = $env{'course.'.$env{'request.course.id'}.'.num'};
                   3601: 
1.44      ng       3602: 	if ($updateflag) {
                   3603: 	    $count++;
1.257     albertel 3604: 	    &Apache::lonnet::cstore(\%newrecord,$symb,$env{'request.course.id'},
1.89      albertel 3605: 				    $udom,$uname);
1.301     albertel 3606: 
                   3607: 	    if (&Apache::bridgetask::in_queue('gradingqueue',$symb,$cdom,
                   3608: 					      $cnum,$udom,$uname)) {
                   3609: 		# need to figure out if should be in queue.
                   3610: 		my %record =  
                   3611: 		    &Apache::lonnet::restore($symb,$env{'request.course.id'},
                   3612: 					     $udom,$uname);
                   3613: 		my $all_graded = 1;
                   3614: 		my $none_graded = 1;
                   3615: 		foreach my $part (@parts) {
                   3616: 		    if ( $record{'resource.'.$part.'.awarded'} eq '' ) {
                   3617: 			$all_graded = 0;
                   3618: 		    } else {
                   3619: 			$none_graded = 0;
                   3620: 		    }
                   3621: 		}
                   3622: 
                   3623: 		if ($all_graded || $none_graded) {
                   3624: 		    &Apache::bridgetask::remove_from_queue('gradingqueue',
                   3625: 							   $symb,$cdom,$cnum,
                   3626: 							   $udom,$uname);
                   3627: 		}
                   3628: 	    }
                   3629: 
1.477     albertel 3630: 	    $result.=&Apache::loncommon::start_data_table_row().
                   3631: 		'<td align="right">&nbsp;'.$updateCtr.'&nbsp;</td>'.$line.
                   3632: 		&Apache::loncommon::end_data_table_row();
1.126     ng       3633: 	    $updateCtr++;
1.93      albertel 3634: 	} else {
1.477     albertel 3635: 	    push(@noupdate,
                   3636: 		 '<td align="right">&nbsp;'.$noupdateCtr.'&nbsp;</td>'.$line);
1.126     ng       3637: 	    $noupdateCtr++;
1.44      ng       3638: 	}
1.269     raeburn  3639:         if ($aggregateflag) {
                   3640:             &Apache::lonnet::cinc('nohist_resourcetracker',\%aggregate,
1.301     albertel 3641: 				  $cdom,$cnum);
1.269     raeburn  3642:         }
1.93      albertel 3643:     }
1.477     albertel 3644:     if (@noupdate) {
1.126     ng       3645: #	my $numcols=(scalar(@partid)*(scalar(@parts)-1)*2)+3;
                   3646: 	my $numcols=scalar(@partid)*4+2;
1.477     albertel 3647: 	$result .= &Apache::loncommon::start_data_table_row('LC_empty_row').
1.478     albertel 3648: 	    '<td align="center" colspan="'.$numcols.'">'.
                   3649: 	    &mt('No Changes Occurred For the Students Below').
                   3650: 	    '</td>'.
1.477     albertel 3651: 	    &Apache::loncommon::end_data_table_row();
                   3652: 	foreach my $line (@noupdate) {
                   3653: 	    $result.=
                   3654: 		&Apache::loncommon::start_data_table_row().
                   3655: 		$line.
                   3656: 		&Apache::loncommon::end_data_table_row();
                   3657: 	}
1.44      ng       3658:     }
1.477     albertel 3659:     $result .= &Apache::loncommon::end_data_table().
                   3660: 	&show_grading_menu_form($symb);
1.478     albertel 3661:     my $msg = '<p><b>'.
                   3662: 	&mt('Number of records updated = [_1] for [quant,_2,student].',
                   3663: 	    $rec_update,$count).'</b><br />'.
                   3664: 	'<b>'.&mt('Total number of students = [_1]',$env{'form.total'}).
                   3665: 	'</b></p>';
1.44      ng       3666:     return $title.$msg.$result;
1.5       albertel 3667: }
1.54      albertel 3668: 
                   3669: sub split_part_type {
                   3670:     my ($partstr) = @_;
                   3671:     my ($temp,@allparts)=split(/_/,$partstr);
                   3672:     my $type=pop(@allparts);
1.439     albertel 3673:     my $part=join('_',@allparts);
1.54      albertel 3674:     return ($part,$type);
                   3675: }
                   3676: 
1.44      ng       3677: #------------- end of section for handling grading by section/class ---------
                   3678: #
                   3679: #----------------------------------------------------------------------------
                   3680: 
1.5       albertel 3681: 
1.44      ng       3682: #----------------------------------------------------------------------------
                   3683: #
                   3684: #-------------------------- Next few routines handles grading by csv upload
                   3685: #
                   3686: #--- Javascript to handle csv upload
1.27      albertel 3687: sub csvupload_javascript_reverse_associate {
1.573     bisitz   3688:     my $error1=&mt('You need to specify the username or the student/employee ID');
1.246     albertel 3689:     my $error2=&mt('You need to specify at least one grading field');
1.27      albertel 3690:   return(<<ENDPICK);
                   3691:   function verify(vf) {
                   3692:     var foundsomething=0;
                   3693:     var founduname=0;
1.243     albertel 3694:     var foundID=0;
1.27      albertel 3695:     for (i=0;i<=vf.nfields.value;i++) {
                   3696:       tw=eval('vf.f'+i+'.selectedIndex');
1.243     albertel 3697:       if (i==0 && tw!=0) { foundID=1; }
                   3698:       if (i==1 && tw!=0) { founduname=1; }
                   3699:       if (i!=0 && i!=1 && i!=2 && tw!=0) { foundsomething=1; }
1.27      albertel 3700:     }
1.246     albertel 3701:     if (founduname==0 && foundID==0) {
                   3702: 	alert('$error1');
                   3703: 	return;
1.27      albertel 3704:     }
                   3705:     if (foundsomething==0) {
1.246     albertel 3706: 	alert('$error2');
                   3707: 	return;
1.27      albertel 3708:     }
                   3709:     vf.submit();
                   3710:   }
                   3711:   function flip(vf,tf) {
                   3712:     var nw=eval('vf.f'+tf+'.selectedIndex');
                   3713:     var i;
                   3714:     for (i=0;i<=vf.nfields.value;i++) {
                   3715:       //can not pick the same destination field for both name and domain
                   3716:       if (((i ==0)||(i ==1)) && 
                   3717:           ((tf==0)||(tf==1)) && 
                   3718:           (i!=tf) &&
                   3719:           (eval('vf.f'+i+'.selectedIndex')==nw)) {
                   3720:         eval('vf.f'+i+'.selectedIndex=0;')
                   3721:       }
                   3722:     }
                   3723:   }
                   3724: ENDPICK
                   3725: }
                   3726: 
                   3727: sub csvupload_javascript_forward_associate {
1.573     bisitz   3728:     my $error1=&mt('You need to specify the username or the student/employee ID');
1.246     albertel 3729:     my $error2=&mt('You need to specify at least one grading field');
1.27      albertel 3730:   return(<<ENDPICK);
                   3731:   function verify(vf) {
                   3732:     var foundsomething=0;
                   3733:     var founduname=0;
1.243     albertel 3734:     var foundID=0;
1.27      albertel 3735:     for (i=0;i<=vf.nfields.value;i++) {
                   3736:       tw=eval('vf.f'+i+'.selectedIndex');
1.243     albertel 3737:       if (tw==1) { foundID=1; }
                   3738:       if (tw==2) { founduname=1; }
                   3739:       if (tw>3) { foundsomething=1; }
1.27      albertel 3740:     }
1.246     albertel 3741:     if (founduname==0 && foundID==0) {
                   3742: 	alert('$error1');
                   3743: 	return;
1.27      albertel 3744:     }
                   3745:     if (foundsomething==0) {
1.246     albertel 3746: 	alert('$error2');
                   3747: 	return;
1.27      albertel 3748:     }
                   3749:     vf.submit();
                   3750:   }
                   3751:   function flip(vf,tf) {
                   3752:     var nw=eval('vf.f'+tf+'.selectedIndex');
                   3753:     var i;
                   3754:     //can not pick the same destination field twice
                   3755:     for (i=0;i<=vf.nfields.value;i++) {
                   3756:       if ((i!=tf) && (eval('vf.f'+i+'.selectedIndex')==nw)) {
                   3757:         eval('vf.f'+i+'.selectedIndex=0;')
                   3758:       }
                   3759:     }
                   3760:   }
                   3761: ENDPICK
                   3762: }
                   3763: 
1.26      albertel 3764: sub csvuploadmap_header {
1.324     albertel 3765:     my ($request,$symb,$datatoken,$distotal)= @_;
1.41      ng       3766:     my $javascript;
1.257     albertel 3767:     if ($env{'form.upfile_associate'} eq 'reverse') {
1.41      ng       3768: 	$javascript=&csvupload_javascript_reverse_associate();
                   3769:     } else {
                   3770: 	$javascript=&csvupload_javascript_forward_associate();
                   3771:     }
1.45      ng       3772: 
1.324     albertel 3773:     my ($result) = &showResourceInfo($symb,$env{'form.probTitle'});
1.257     albertel 3774:     my $checked=(($env{'form.noFirstLine'})?' checked="checked"':'');
1.245     albertel 3775:     my $ignore=&mt('Ignore First Line');
1.418     albertel 3776:     $symb = &Apache::lonenc::check_encrypt($symb);
1.41      ng       3777:     $request->print(<<ENDPICK);
1.26      albertel 3778: <form method="post" enctype="multipart/form-data" action="/adm/grades" name="gradesupload">
1.398     albertel 3779: <h3><span class="LC_info">Uploading Class Grades</span></h3>
1.45      ng       3780: $result
1.326     albertel 3781: <hr />
1.26      albertel 3782: <h3>Identify fields</h3>
                   3783: Total number of records found in file: $distotal <hr />
                   3784: Enter as many fields as you can. The system will inform you and bring you back
                   3785: to this page if the data selected is insufficient to run your class.<hr />
                   3786: <input type="button" value="Reverse Association" onClick="javascript:this.form.associate.value='Reverse Association';submit(this.form);" />
1.245     albertel 3787: <label><input type="checkbox" name="noFirstLine" $checked />$ignore</label>
1.26      albertel 3788: <input type="hidden" name="associate"  value="" />
                   3789: <input type="hidden" name="phase"      value="three" />
                   3790: <input type="hidden" name="datatoken"  value="$datatoken" />
1.257     albertel 3791: <input type="hidden" name="fileupload" value="$env{'form.fileupload'}" />
                   3792: <input type="hidden" name="upfiletype" value="$env{'form.upfiletype'}" />
1.26      albertel 3793: <input type="hidden" name="upfile_associate" 
1.257     albertel 3794:                                        value="$env{'form.upfile_associate'}" />
1.26      albertel 3795: <input type="hidden" name="symb"       value="$symb" />
1.257     albertel 3796: <input type="hidden" name="saveState"  value="$env{'form.saveState'}" />
                   3797: <input type="hidden" name="probTitle"  value="$env{'form.probTitle'}" />
1.246     albertel 3798: <input type="hidden" name="command"    value="csvuploadoptions" />
1.26      albertel 3799: <hr />
                   3800: <script type="text/javascript" language="Javascript">
                   3801: $javascript
                   3802: </script>
                   3803: ENDPICK
1.118     ng       3804:     return '';
1.26      albertel 3805: 
                   3806: }
                   3807: 
                   3808: sub csvupload_fields {
1.324     albertel 3809:     my ($symb) = @_;
                   3810:     my (@parts) = &getpartlist($symb);
1.556     weissno  3811:     my @fields=(['ID','Student/Employee ID'],
1.243     albertel 3812: 		['username','Student Username'],
                   3813: 		['domain','Student Domain']);
1.324     albertel 3814:     my (undef,undef,$url) = &Apache::lonnet::decode_symb($symb);
1.41      ng       3815:     foreach my $part (sort(@parts)) {
                   3816: 	my @datum;
                   3817: 	my $display=&Apache::lonnet::metadata($url,$part.'.display');
                   3818: 	my $name=$part;
                   3819: 	if  (!$display) { $display = $name; }
                   3820: 	@datum=($name,$display);
1.244     albertel 3821: 	if ($name=~/^stores_(.*)_awarded/) {
                   3822: 	    push(@fields,['stores_'.$1.'_points',"Points [Part: $1]"]);
                   3823: 	}
1.41      ng       3824: 	push(@fields,\@datum);
                   3825:     }
                   3826:     return (@fields);
1.26      albertel 3827: }
                   3828: 
                   3829: sub csvuploadmap_footer {
1.41      ng       3830:     my ($request,$i,$keyfields) =@_;
                   3831:     $request->print(<<ENDPICK);
1.26      albertel 3832: </table>
                   3833: <input type="hidden" name="nfields" value="$i" />
                   3834: <input type="hidden" name="keyfields" value="$keyfields" />
                   3835: <input type="button" onClick="javascript:verify(this.form)" value="Assign Grades" /><br />
                   3836: </form>
                   3837: ENDPICK
                   3838: }
                   3839: 
1.283     albertel 3840: sub checkforfile_js {
1.539     riegler  3841:     my $alertmsg = &mt('Please use the browse button to select a file from your local directory.');
1.86      ng       3842:     my $result =<<CSVFORMJS;
                   3843: <script type="text/javascript" language="javascript">
                   3844:     function checkUpload(formname) {
                   3845: 	if (formname.upfile.value == "") {
1.539     riegler  3846: 	    alert("$alertmsg");
1.86      ng       3847: 	    return false;
                   3848: 	}
                   3849: 	formname.submit();
                   3850:     }
                   3851:     </script>
                   3852: CSVFORMJS
1.283     albertel 3853:     return $result;
                   3854: }
                   3855: 
                   3856: sub upcsvScores_form {
                   3857:     my ($request) = shift;
1.324     albertel 3858:     my ($symb)=&get_symb($request);
1.283     albertel 3859:     if (!$symb) {return '';}
                   3860:     my $result=&checkforfile_js();
1.257     albertel 3861:     $env{'form.probTitle'} = &Apache::lonnet::gettitle($symb);
1.324     albertel 3862:     my ($table) = &showResourceInfo($symb,$env{'form.probTitle'});
1.118     ng       3863:     $result.=$table;
1.326     albertel 3864:     $result.='<br /><table width="100%" border="0"><tr><td bgcolor="#777777">'."\n";
                   3865:     $result.='<table width="100%" border="0"><tr bgcolor="#e6ffff"><td>'."\n";
1.538     schulted 3866:     $result.='&nbsp;<b>'.&mt('Specify a file containing the class scores for current resource.').
                   3867: 	'</b></td></tr>'."\n";
1.86      ng       3868:     $result.='<tr bgcolor=#ffffe6><td>'."\n";
1.370     www      3869:     my $upload=&mt("Upload Scores");
1.86      ng       3870:     my $upfile_select=&Apache::loncommon::upfile_select_html();
1.245     albertel 3871:     my $ignore=&mt('Ignore First Line');
1.418     albertel 3872:     $symb = &Apache::lonenc::check_encrypt($symb);
1.86      ng       3873:     $result.=<<ENDUPFORM;
1.106     albertel 3874: <form method="post" enctype="multipart/form-data" action="/adm/grades" name="gradesupload">
1.86      ng       3875: <input type="hidden" name="symb" value="$symb" />
                   3876: <input type="hidden" name="command" value="csvuploadmap" />
1.257     albertel 3877: <input type="hidden" name="probTitle" value="$env{'form.probTitle'}" />
                   3878: <input type="hidden" name="saveState"  value="$env{'form.saveState'}" />
1.86      ng       3879: $upfile_select
1.370     www      3880: <br /><input type="button" onClick="javascript:checkUpload(this.form);" value="$upload" />
1.283     albertel 3881: <label><input type="checkbox" name="noFirstLine" />$ignore</label>
1.86      ng       3882: </form>
                   3883: ENDUPFORM
1.370     www      3884:     $result.=&Apache::loncommon::help_open_topic("Course_Convert_To_CSV",
                   3885:                            &mt("How do I create a CSV file from a spreadsheet"))
                   3886:     .'</td></tr></table>'."\n";
1.86      ng       3887:     $result.='</td></tr></table><br /><br />'."\n";
1.324     albertel 3888:     $result.=&show_grading_menu_form($symb);
1.86      ng       3889:     return $result;
                   3890: }
                   3891: 
                   3892: 
1.26      albertel 3893: sub csvuploadmap {
1.41      ng       3894:     my ($request)= @_;
1.324     albertel 3895:     my ($symb)=&get_symb($request);
1.41      ng       3896:     if (!$symb) {return '';}
1.72      ng       3897: 
1.41      ng       3898:     my $datatoken;
1.257     albertel 3899:     if (!$env{'form.datatoken'}) {
1.41      ng       3900: 	$datatoken=&Apache::loncommon::upfile_store($request);
1.26      albertel 3901:     } else {
1.257     albertel 3902: 	$datatoken=$env{'form.datatoken'};
1.41      ng       3903: 	&Apache::loncommon::load_tmp_file($request);
1.26      albertel 3904:     }
1.41      ng       3905:     my @records=&Apache::loncommon::upfile_record_sep();
1.257     albertel 3906:     if ($env{'form.noFirstLine'}) { shift(@records); }
1.324     albertel 3907:     &csvuploadmap_header($request,$symb,$datatoken,$#records+1);
1.41      ng       3908:     my ($i,$keyfields);
                   3909:     if (@records) {
1.324     albertel 3910: 	my @fields=&csvupload_fields($symb);
1.45      ng       3911: 
1.257     albertel 3912: 	if ($env{'form.upfile_associate'} eq 'reverse') {	
1.41      ng       3913: 	    &Apache::loncommon::csv_print_samples($request,\@records);
                   3914: 	    $i=&Apache::loncommon::csv_print_select_table($request,\@records,
                   3915: 							  \@fields);
                   3916: 	    foreach (@fields) { $keyfields.=$_->[0].','; }
                   3917: 	    chop($keyfields);
                   3918: 	} else {
                   3919: 	    unshift(@fields,['none','']);
                   3920: 	    $i=&Apache::loncommon::csv_samples_select_table($request,\@records,
                   3921: 							    \@fields);
1.311     banghart 3922:             foreach my $rec (@records) {
                   3923:                 my %temp = &Apache::loncommon::record_sep($rec);
                   3924:                 if (%temp) {
                   3925:                     $keyfields=join(',',sort(keys(%temp)));
                   3926:                     last;
                   3927:                 }
                   3928:             }
1.41      ng       3929: 	}
                   3930:     }
                   3931:     &csvuploadmap_footer($request,$i,$keyfields);
1.324     albertel 3932:     $request->print(&show_grading_menu_form($symb));
1.72      ng       3933: 
1.41      ng       3934:     return '';
1.27      albertel 3935: }
                   3936: 
1.246     albertel 3937: sub csvuploadoptions {
1.41      ng       3938:     my ($request)= @_;
1.324     albertel 3939:     my ($symb)=&get_symb($request);
1.257     albertel 3940:     my $checked=(($env{'form.noFirstLine'})?'1':'0');
1.246     albertel 3941:     my $ignore=&mt('Ignore First Line');
                   3942:     $request->print(<<ENDPICK);
                   3943: <form method="post" enctype="multipart/form-data" action="/adm/grades" name="gradesupload">
1.398     albertel 3944: <h3><span class="LC_info">Uploading Class Grade Options</span></h3>
1.246     albertel 3945: <input type="hidden" name="command"    value="csvuploadassign" />
1.302     albertel 3946: <!--
1.246     albertel 3947: <p>
                   3948: <label>
                   3949:    <input type="checkbox" name="show_full_results" />
                   3950:    Show a table of all changes
                   3951: </label>
                   3952: </p>
1.302     albertel 3953: -->
1.246     albertel 3954: <p>
                   3955: <label>
                   3956:    <input type="checkbox" name="overwite_scores" checked="checked" />
                   3957:    Overwrite any existing score
                   3958: </label>
                   3959: </p>
                   3960: ENDPICK
                   3961:     my %fields=&get_fields();
                   3962:     if (!defined($fields{'domain'})) {
1.257     albertel 3963: 	my $domform = &Apache::loncommon::select_dom_form($env{'request.role.domain'},'default_domain');
1.246     albertel 3964: 	$request->print("\n<p> Users are in domain: ".$domform."</p>\n");
                   3965:     }
1.257     albertel 3966:     foreach my $key (sort(keys(%env))) {
1.246     albertel 3967: 	if ($key !~ /^form\.(.*)$/) { next; }
                   3968: 	my $cleankey=$1;
                   3969: 	if ($cleankey eq 'command') { next; }
                   3970: 	$request->print('<input type="hidden" name="'.$cleankey.
1.257     albertel 3971: 			'"  value="'.$env{$key}.'" />'."\n");
1.246     albertel 3972:     }
                   3973:     # FIXME do a check for any duplicated user ids...
                   3974:     # FIXME do a check for any invalid user ids?...
1.290     albertel 3975:     $request->print('<input type="submit" value="Assign Grades" /><br />
                   3976: <hr /></form>'."\n");
1.324     albertel 3977:     $request->print(&show_grading_menu_form($symb));
1.246     albertel 3978:     return '';
                   3979: }
                   3980: 
                   3981: sub get_fields {
                   3982:     my %fields;
1.257     albertel 3983:     my @keyfields = split(/\,/,$env{'form.keyfields'});
                   3984:     for (my $i=0; $i<=$env{'form.nfields'}; $i++) {
                   3985: 	if ($env{'form.upfile_associate'} eq 'reverse') {
                   3986: 	    if ($env{'form.f'.$i} ne 'none') {
                   3987: 		$fields{$keyfields[$i]}=$env{'form.f'.$i};
1.41      ng       3988: 	    }
                   3989: 	} else {
1.257     albertel 3990: 	    if ($env{'form.f'.$i} ne 'none') {
                   3991: 		$fields{$env{'form.f'.$i}}=$keyfields[$i];
1.41      ng       3992: 	    }
                   3993: 	}
1.27      albertel 3994:     }
1.246     albertel 3995:     return %fields;
                   3996: }
                   3997: 
                   3998: sub csvuploadassign {
                   3999:     my ($request)= @_;
1.324     albertel 4000:     my ($symb)=&get_symb($request);
1.246     albertel 4001:     if (!$symb) {return '';}
1.345     bowersj2 4002:     my $error_msg = '';
1.246     albertel 4003:     &Apache::loncommon::load_tmp_file($request);
                   4004:     my @gradedata = &Apache::loncommon::upfile_record_sep();
1.257     albertel 4005:     if ($env{'form.noFirstLine'}) { shift(@gradedata); }
1.246     albertel 4006:     my %fields=&get_fields();
1.41      ng       4007:     $request->print('<h3>Assigning Grades</h3>');
1.257     albertel 4008:     my $courseid=$env{'request.course.id'};
1.97      albertel 4009:     my ($classlist) = &getclasslist('all',0);
1.106     albertel 4010:     my @notallowed;
1.41      ng       4011:     my @skipped;
                   4012:     my $countdone=0;
                   4013:     foreach my $grade (@gradedata) {
                   4014: 	my %entries=&Apache::loncommon::record_sep($grade);
1.246     albertel 4015: 	my $domain;
                   4016: 	if ($entries{$fields{'domain'}}) {
                   4017: 	    $domain=$entries{$fields{'domain'}};
                   4018: 	} else {
1.257     albertel 4019: 	    $domain=$env{'form.default_domain'};
1.246     albertel 4020: 	}
1.243     albertel 4021: 	$domain=~s/\s//g;
1.41      ng       4022: 	my $username=$entries{$fields{'username'}};
1.160     albertel 4023: 	$username=~s/\s//g;
1.243     albertel 4024: 	if (!$username) {
                   4025: 	    my $id=$entries{$fields{'ID'}};
1.247     albertel 4026: 	    $id=~s/\s//g;
1.243     albertel 4027: 	    my %ids=&Apache::lonnet::idget($domain,$id);
                   4028: 	    $username=$ids{$id};
                   4029: 	}
1.41      ng       4030: 	if (!exists($$classlist{"$username:$domain"})) {
1.247     albertel 4031: 	    my $id=$entries{$fields{'ID'}};
                   4032: 	    $id=~s/\s//g;
                   4033: 	    if ($id) {
                   4034: 		push(@skipped,"$id:$domain");
                   4035: 	    } else {
                   4036: 		push(@skipped,"$username:$domain");
                   4037: 	    }
1.41      ng       4038: 	    next;
                   4039: 	}
1.108     albertel 4040: 	my $usec=$classlist->{"$username:$domain"}[5];
1.106     albertel 4041: 	if (!&canmodify($usec)) {
                   4042: 	    push(@notallowed,"$username:$domain");
                   4043: 	    next;
                   4044: 	}
1.244     albertel 4045: 	my %points;
1.41      ng       4046: 	my %grades;
                   4047: 	foreach my $dest (keys(%fields)) {
1.244     albertel 4048: 	    if ($dest eq 'ID' || $dest eq 'username' ||
                   4049: 		$dest eq 'domain') { next; }
                   4050: 	    if ($entries{$fields{$dest}} =~ /^\s*$/) { next; }
                   4051: 	    if ($dest=~/stores_(.*)_points/) {
                   4052: 		my $part=$1;
                   4053: 		my $wgt =&Apache::lonnet::EXT('resource.'.$part.'.weight',
                   4054: 					      $symb,$domain,$username);
1.345     bowersj2 4055:                 if ($wgt) {
                   4056:                     $entries{$fields{$dest}}=~s/\s//g;
                   4057:                     my $pcr=$entries{$fields{$dest}} / $wgt;
1.463     albertel 4058:                     my $award=($pcr == 0) ? 'incorrect_by_override'
                   4059:                                           : 'correct_by_override';
1.345     bowersj2 4060:                     $grades{"resource.$part.awarded"}=$pcr;
                   4061:                     $grades{"resource.$part.solved"}=$award;
                   4062:                     $points{$part}=1;
                   4063:                 } else {
                   4064:                     $error_msg = "<br />" .
                   4065:                         &mt("Some point values were assigned"
                   4066:                             ." for problems with a weight "
                   4067:                             ."of zero. These values were "
                   4068:                             ."ignored.");
                   4069:                 }
1.244     albertel 4070: 	    } else {
                   4071: 		if ($dest=~/stores_(.*)_awarded/) { if ($points{$1}) {next;} }
                   4072: 		if ($dest=~/stores_(.*)_solved/)  { if ($points{$1}) {next;} }
                   4073: 		my $store_key=$dest;
                   4074: 		$store_key=~s/^stores/resource/;
                   4075: 		$store_key=~s/_/\./g;
                   4076: 		$grades{$store_key}=$entries{$fields{$dest}};
                   4077: 	    }
1.41      ng       4078: 	}
1.508     www      4079: 	if (! %grades) { 
                   4080:            push(@skipped,&mt("[_1]: no data to save","$username:$domain")); 
                   4081:         } else {
                   4082: 	   $grades{"resource.regrader"}="$env{'user.name'}:$env{'user.domain'}";
                   4083: 	   my $result=&Apache::lonnet::cstore(\%grades,$symb,
1.302     albertel 4084: 					   $env{'request.course.id'},
                   4085: 					   $domain,$username);
1.508     www      4086: 	   if ($result eq 'ok') {
                   4087: 	      $request->print('.');
                   4088: 	   } else {
                   4089: 	      $request->print("<p><span class=\"LC_error\">".
                   4090:                               &mt("Failed to save data for student [_1]. Message when trying to save was: [_2]",
                   4091:                                   "$username:$domain",$result)."</span></p>");
                   4092: 	   }
                   4093: 	   $request->rflush();
                   4094: 	   $countdone++;
                   4095:         }
1.41      ng       4096:     }
1.570     www      4097:     $request->print('<br />'.&Apache::lonhtmlcommon::confirm_success(&mt("Saved scores for [quant,_1,student]",$countdone),$countdone==0));
1.41      ng       4098:     if (@skipped) {
1.571     www      4099: 	$request->print('<br />'.&Apache::lonhtmlcommon::confirm_success(&mt('No scores stored for the following username(s):'),1).'<br />');
                   4100:         $request->print(join(', ',@skipped));
1.106     albertel 4101:     }
                   4102:     if (@notallowed) {
1.571     www      4103: 	$request->print('<br />'.&Apache::lonhtmlcommon::confirm_success(&mt('Modification of scores not allowed for the following username(s):'),1).'<br />');
                   4104: 	$request->print(join(', ',@notallowed));
1.41      ng       4105:     }
1.106     albertel 4106:     $request->print("<br />\n");
1.324     albertel 4107:     $request->print(&show_grading_menu_form($symb));
1.345     bowersj2 4108:     return $error_msg;
1.26      albertel 4109: }
1.44      ng       4110: #------------- end of section for handling csv file upload ---------
                   4111: #
                   4112: #-------------------------------------------------------------------
                   4113: #
1.122     ng       4114: #-------------- Next few routines handle grading by page/sequence
1.72      ng       4115: #
                   4116: #--- Select a page/sequence and a student to grade
1.68      ng       4117: sub pickStudentPage {
                   4118:     my ($request) = shift;
                   4119: 
1.539     riegler  4120:     my $alertmsg = &mt('Please select the student you wish to grade.');
1.68      ng       4121:     $request->print(<<LISTJAVASCRIPT);
                   4122: <script type="text/javascript" language="javascript">
                   4123: 
                   4124: function checkPickOne(formname) {
1.76      ng       4125:     if (radioSelection(formname.student) == null) {
1.539     riegler  4126: 	alert("$alertmsg");
1.68      ng       4127: 	return;
                   4128:     }
1.125     ng       4129:     ptr = pullDownSelection(formname.selectpage);
                   4130:     formname.page.value = formname["page"+ptr].value;
                   4131:     formname.title.value = formname["title"+ptr].value;
1.68      ng       4132:     formname.submit();
                   4133: }
                   4134: 
                   4135: </script>
                   4136: LISTJAVASCRIPT
1.118     ng       4137:     &commonJSfunctions($request);
1.324     albertel 4138:     my ($symb) = &get_symb($request);
1.257     albertel 4139:     my $cdom      = $env{"course.$env{'request.course.id'}.domain"};
                   4140:     my $cnum      = $env{"course.$env{'request.course.id'}.num"};
                   4141:     my $getsec    = $env{'form.section'} eq '' ? 'all' : $env{'form.section'};
1.68      ng       4142: 
1.398     albertel 4143:     my $result='<h3><span class="LC_info">&nbsp;'.
1.485     albertel 4144: 	&mt('Manual Grading by Page or Sequence').'</span></h3>';
1.68      ng       4145: 
1.80      ng       4146:     $result.='<form action="/adm/grades" method="post" name="displayPage">'."\n";
1.423     albertel 4147:     my ($titles,$symbx) = &getSymbMap();
1.137     albertel 4148:     my ($curpage) =&Apache::lonnet::decode_symb($symb); 
                   4149: #    my ($curpage,$mapId) =&Apache::lonnet::decode_symb($symb); 
                   4150: #    my $type=($curpage =~ /\.(page|sequence)/);
1.485     albertel 4151:     my $select = '<select name="selectpage">'."\n";
1.70      ng       4152:     my $ctr=0;
1.68      ng       4153:     foreach (@$titles) {
                   4154: 	my ($minder,$showtitle) = ($_ =~ /(\d+)\.(.*)/);
1.485     albertel 4155: 	$select.='<option value="'.$ctr.'" '.
1.401     albertel 4156: 	    ($$symbx{$_} =~ /$curpage$/ ? 'selected="selected"' : '').
1.71      ng       4157: 	    '>'.$showtitle.'</option>'."\n";
1.70      ng       4158: 	$ctr++;
1.68      ng       4159:     }
1.485     albertel 4160:     $select.= '</select>';
1.539     riegler  4161:     $result.='&nbsp;<b>'.&mt('Problems from').':</b> '.$select."<br />\n";
1.485     albertel 4162: 
1.70      ng       4163:     $ctr=0;
                   4164:     foreach (@$titles) {
                   4165: 	my ($minder,$showtitle) = ($_ =~ /(\d+)\.(.*)/);
                   4166: 	$result.='<input type="hidden" name="page'.$ctr.'" value="'.$$symbx{$_}.'" />'."\n";
                   4167: 	$result.='<input type="hidden" name="title'.$ctr.'" value="'.$showtitle.'" />'."\n";
                   4168: 	$ctr++;
                   4169:     }
1.72      ng       4170:     $result.='<input type="hidden" name="page" />'."\n".
                   4171: 	'<input type="hidden" name="title" />'."\n";
1.68      ng       4172: 
1.485     albertel 4173:     my $options =
                   4174: 	'<label><input type="radio" name="vProb" value="no" checked="checked" /> '.&mt('no').' </label>'."\n".
                   4175: 	'<label><input type="radio" name="vProb" value="yes" /> '.&mt('yes').' </label>'."<br />\n";
1.539     riegler  4176:     $result.='&nbsp;<b>'.&mt('View Problem Text').': </b>'.$options;
1.485     albertel 4177: 
                   4178:     $options =
                   4179: 	'<label><input type="radio" name="lastSub" value="none" /> '.&mt('none').' </label>'."\n".
                   4180: 	'<label><input type="radio" name="lastSub" value="datesub" checked="checked" /> '.&mt('by dates and submissions').'</label>'."\n".
                   4181: 	'<label><input type="radio" name="lastSub" value="all" /> '.&mt('all details').' </label>'."\n";
1.539     riegler  4182:     $result.='&nbsp;<b>'.&mt('Submissions').': </b>'.$options;
1.432     banghart 4183:     
                   4184:     $result.=&build_section_inputs();
1.442     banghart 4185:     my $stu_status = join(':',&Apache::loncommon::get_env_multiple('form.Status'));
                   4186:     $result.='<input type="hidden" name="Status"  value="'.$stu_status.'" />'."\n".
1.72      ng       4187: 	'<input type="hidden" name="command" value="displayPage" />'."\n".
1.418     albertel 4188: 	'<input type="hidden" name="symb"    value="'.&Apache::lonenc::check_encrypt($symb).'" />'."\n".
1.257     albertel 4189: 	'<input type="hidden" name="saveState" value="'.$env{'form.saveState'}.'" />'."<br />\n";
1.72      ng       4190: 
1.539     riegler  4191:     $result.='&nbsp;<b>'.&mt('Use CODE').': </b> <input type="text" name="CODE" value="" /> <br />'."\n";
1.382     albertel 4192: 
1.80      ng       4193:     $result.='&nbsp;<input type="button" '.
1.539     riegler  4194: 	'onClick="javascript:checkPickOne(this.form);" value="'.&mt('Next').' &rarr;" /><br />'."\n";
1.72      ng       4195: 
1.68      ng       4196:     $request->print($result);
                   4197: 
1.485     albertel 4198:     my $studentTable.='&nbsp;<b>'.&mt('Select a student you wish to grade and then click on the Next button.').'</b><br />'.
1.484     albertel 4199: 	&Apache::loncommon::start_data_table().
                   4200: 	&Apache::loncommon::start_data_table_header_row().
1.485     albertel 4201: 	'<th align="right">&nbsp;'.&mt('No.').'</th>'.
1.484     albertel 4202: 	'<th>'.&nameUserString('header').'</th>'.
1.485     albertel 4203: 	'<th align="right">&nbsp;'.&mt('No.').'</th>'.
1.484     albertel 4204: 	'<th>'.&nameUserString('header').'</th>'.
                   4205: 	&Apache::loncommon::end_data_table_header_row();
1.68      ng       4206:  
1.76      ng       4207:     my (undef,undef,$fullname) = &getclasslist($getsec,'1');
1.68      ng       4208:     my $ptr = 1;
1.294     albertel 4209:     foreach my $student (sort 
                   4210: 			 {
                   4211: 			     if (lc($$fullname{$a}) ne lc($$fullname{$b})) {
                   4212: 				 return (lc($$fullname{$a}) cmp lc($$fullname{$b}));
                   4213: 			     }
                   4214: 			     return $a cmp $b;
                   4215: 			 } (keys(%$fullname))) {
1.68      ng       4216: 	my ($uname,$udom) = split(/:/,$student);
1.484     albertel 4217: 	$studentTable.=($ptr%2==1 ? &Apache::loncommon::start_data_table_row()
                   4218:                                   : '</td>');
1.126     ng       4219: 	$studentTable.='<td align="right">'.$ptr.'&nbsp;</td>';
1.288     albertel 4220: 	$studentTable.='<td>&nbsp;<label><input type="radio" name="student" value="'.$student.'" /> '
                   4221: 	    .&nameUserString(undef,$$fullname{$student},$uname,$udom)."</label>\n";
1.484     albertel 4222: 	$studentTable.=
                   4223: 	    ($ptr%2 == 0 ? '</td>'.&Apache::loncommon::end_data_table_row() 
                   4224:                          : '');
1.68      ng       4225: 	$ptr++;
                   4226:     }
1.484     albertel 4227:     if ($ptr%2 == 0) {
                   4228: 	$studentTable.='</td><td>&nbsp;</td><td>&nbsp;</td>'.
                   4229: 	    &Apache::loncommon::end_data_table_row();
                   4230:     }
                   4231:     $studentTable.=&Apache::loncommon::end_data_table()."\n";
1.126     ng       4232:     $studentTable.='<input type="button" '.
1.539     riegler  4233: 	'onClick="javascript:checkPickOne(this.form);" value="'.&mt('Next').' &rarr;" /></form>'."\n";
1.68      ng       4234: 
1.324     albertel 4235:     $studentTable.=&show_grading_menu_form($symb);
1.68      ng       4236:     $request->print($studentTable);
                   4237: 
                   4238:     return '';
                   4239: }
                   4240: 
                   4241: sub getSymbMap {
1.132     bowersj2 4242:     my $navmap = Apache::lonnavmaps::navmap->new();
1.68      ng       4243: 
                   4244:     my %symbx = ();
                   4245:     my @titles = ();
1.117     bowersj2 4246:     my $minder = 0;
                   4247: 
                   4248:     # Gather every sequence that has problems.
1.240     albertel 4249:     my @sequences = $navmap->retrieveResources(undef, sub { shift->is_map(); },
                   4250: 					       1,0,1);
1.117     bowersj2 4251:     for my $sequence ($navmap->getById('0.0'), @sequences) {
1.241     albertel 4252: 	if ($navmap->hasResource($sequence, sub { shift->is_problem(); }, 0) ) {
1.381     albertel 4253: 	    my $title = $minder.'.'.
                   4254: 		&HTML::Entities::encode($sequence->compTitle(),'"\'&');
                   4255: 	    push(@titles, $title); # minder in case two titles are identical
                   4256: 	    $symbx{$title} = &HTML::Entities::encode($sequence->symb(),'"\'&');
1.117     bowersj2 4257: 	    $minder++;
1.241     albertel 4258: 	}
1.68      ng       4259:     }
                   4260:     return \@titles,\%symbx;
                   4261: }
                   4262: 
1.72      ng       4263: #
                   4264: #--- Displays a page/sequence w/wo problems, w/wo submissions
1.68      ng       4265: sub displayPage {
                   4266:     my ($request) = shift;
                   4267: 
1.324     albertel 4268:     my ($symb) = &get_symb($request);
1.257     albertel 4269:     my $cdom      = $env{"course.$env{'request.course.id'}.domain"};
                   4270:     my $cnum      = $env{"course.$env{'request.course.id'}.num"};
                   4271:     my $getsec    = $env{'form.section'} eq '' ? 'all' : $env{'form.section'};
                   4272:     my $pageTitle = $env{'form.page'};
1.103     albertel 4273:     my ($classlist,undef,$fullname) = &getclasslist($getsec,'1');
1.257     albertel 4274:     my ($uname,$udom) = split(/:/,$env{'form.student'});
                   4275:     my $usec=$classlist->{$env{'form.student'}}[5];
1.168     albertel 4276: 
                   4277:     #need to make sure we have the correct data for later EXT calls, 
                   4278:     #thus invalidate the cache
                   4279:     &Apache::lonnet::devalidatecourseresdata(
1.257     albertel 4280:                  $env{'course.'.$env{'request.course.id'}.'.num'},
                   4281:                  $env{'course.'.$env{'request.course.id'}.'.domain'});
1.168     albertel 4282:     &Apache::lonnet::clear_EXT_cache_status();
                   4283: 
1.103     albertel 4284:     if (!&canview($usec)) {
1.485     albertel 4285: 	$request->print('<span class="LC_warning">'.&mt('Unable to view requested student. ([_1])',$env{'form.student'}).'</span>');
1.324     albertel 4286: 	$request->print(&show_grading_menu_form($symb));
1.103     albertel 4287: 	return;
                   4288:     }
1.398     albertel 4289:     my $result='<h3><span class="LC_info">&nbsp;'.$env{'form.title'}.'</span></h3>';
1.485     albertel 4290:     $result.='<h3>&nbsp;'.&mt('Student: [_1]',&nameUserString(undef,$$fullname{$env{'form.student'}},$uname,$udom)).
1.129     ng       4291: 	'</h3>'."\n";
1.500     albertel 4292:     $env{'form.CODE'} = uc($env{'form.CODE'});
1.501     foxr     4293:     if (&Apache::lonnet::validCODE(uc($env{'form.CODE'}))) {
1.485     albertel 4294: 	$result.='<h3>&nbsp;'.&mt('CODE: [_1]',$env{'form.CODE'}).'</h3>'."\n";
1.382     albertel 4295:     } else {
                   4296: 	delete($env{'form.CODE'});
                   4297:     }
1.71      ng       4298:     &sub_page_js($request);
                   4299:     $request->print($result);
                   4300: 
1.132     bowersj2 4301:     my $navmap = Apache::lonnavmaps::navmap->new();
1.257     albertel 4302:     my ($mapUrl, $id, $resUrl)=&Apache::lonnet::decode_symb($env{'form.page'});
1.68      ng       4303:     my $map = $navmap->getResourceByUrl($resUrl); # add to navmaps
1.288     albertel 4304:     if (!$map) {
1.485     albertel 4305: 	$request->print('<span class="LC_warning">'.&mt('Unable to view requested sequence. ([_1])',$resUrl).'</span>');
1.324     albertel 4306: 	$request->print(&show_grading_menu_form($symb));
1.288     albertel 4307: 	return; 
                   4308:     }
1.68      ng       4309:     my $iterator = $navmap->getIterator($map->map_start(),
                   4310: 					$map->map_finish());
                   4311: 
1.71      ng       4312:     my $studentTable='<form action="/adm/grades" method="post" name="gradePage">'."\n".
1.72      ng       4313: 	'<input type="hidden" name="command" value="gradeByPage" />'."\n".
1.257     albertel 4314: 	'<input type="hidden" name="fullname" value="'.$$fullname{$env{'form.student'}}.'" />'."\n".
                   4315: 	'<input type="hidden" name="student" value="'.$env{'form.student'}.'" />'."\n".
1.72      ng       4316: 	'<input type="hidden" name="page"    value="'.$pageTitle.'" />'."\n".
1.257     albertel 4317: 	'<input type="hidden" name="title"   value="'.$env{'form.title'}.'" />'."\n".
1.418     albertel 4318: 	'<input type="hidden" name="symb"    value="'.&Apache::lonenc::check_encrypt($symb).'" />'."\n".
1.125     ng       4319: 	'<input type="hidden" name="overRideScore" value="no" />'."\n".
1.257     albertel 4320: 	'<input type="hidden" name="saveState" value="'.$env{'form.saveState'}.'" />'."\n";
1.71      ng       4321: 
1.382     albertel 4322:     if (defined($env{'form.CODE'})) {
                   4323: 	$studentTable.=
                   4324: 	    '<input type="hidden" name="CODE" value="'.$env{'form.CODE'}.'" />'."\n";
                   4325:     }
1.381     albertel 4326:     my $checkIcon = '<img alt="'.&mt('Check Mark').
1.485     albertel 4327: 	'" src="'.&Apache::loncommon::lonhttpdurl($request->dir_config('lonIconsURL').'/check.gif').'" height="16" border="0" />';
1.71      ng       4328: 
1.485     albertel 4329:     $studentTable.='&nbsp;'.&mt('<b>Note:</b> Problems graded correct by the computer are marked with a [_1] symbol.',$checkIcon)."\n".
1.484     albertel 4330: 	&Apache::loncommon::start_data_table().
                   4331: 	&Apache::loncommon::start_data_table_header_row().
                   4332: 	'<th align="center">&nbsp;Prob.&nbsp;</th>'.
1.485     albertel 4333: 	'<th>&nbsp;'.($env{'form.vProb'} eq 'no' ? &mt('Title') : &mt('Problem Text')).'/'.&mt('Grade').'</th>'.
1.484     albertel 4334: 	&Apache::loncommon::end_data_table_header_row();
1.71      ng       4335: 
1.329     albertel 4336:     &Apache::lonxml::clear_problem_counter();
1.196     albertel 4337:     my ($depth,$question,$prob) = (1,1,1);
1.68      ng       4338:     $iterator->next(); # skip the first BEGIN_MAP
                   4339:     my $curRes = $iterator->next(); # for "current resource"
1.101     albertel 4340:     while ($depth > 0) {
1.68      ng       4341:         if($curRes == $iterator->BEGIN_MAP) { $depth++; }
1.100     bowersj2 4342:         if($curRes == $iterator->END_MAP) { $depth--; }
1.68      ng       4343: 
1.385     albertel 4344:         if (ref($curRes) && $curRes->is_problem()) {
1.91      albertel 4345: 	    my $parts = $curRes->parts();
1.68      ng       4346:             my $title = $curRes->compTitle();
1.71      ng       4347: 	    my $symbx = $curRes->symb();
1.484     albertel 4348: 	    $studentTable.=
                   4349: 		&Apache::loncommon::start_data_table_row().
                   4350: 		'<td align="center" valign="top" >'.$prob.
1.485     albertel 4351: 		(scalar(@{$parts}) == 1 ? '' 
                   4352: 		                        : '<br />('.&mt('[_1]&nbsp;parts)',
                   4353: 							scalar(@{$parts}))
                   4354: 		 ).
                   4355: 		 '</td>';
1.71      ng       4356: 	    $studentTable.='<td valign="top">';
1.382     albertel 4357: 	    my %form = ('CODE' => $env{'form.CODE'},);
1.257     albertel 4358: 	    if ($env{'form.vProb'} eq 'yes' ) {
1.144     albertel 4359: 		$studentTable.=&show_problem($request,$symbx,$uname,$udom,1,
1.383     albertel 4360: 					     undef,'both',\%form);
1.71      ng       4361: 	    } else {
1.382     albertel 4362: 		my $companswer = &Apache::loncommon::get_student_answers($symbx,$uname,$udom,$env{'request.course.id'},%form);
1.80      ng       4363: 		$companswer =~ s|<form(.*?)>||g;
                   4364: 		$companswer =~ s|</form>||g;
1.71      ng       4365: #		while ($companswer =~ /(<a href\=\"javascript:newWindow.*?Script Vars<\/a>)/s) { #<a href="javascript:newWindow</a>
1.116     ng       4366: #		    $companswer =~ s/$1/ /ms;
1.326     albertel 4367: #		    $request->print('match='.$1."<br />\n");
1.71      ng       4368: #		}
1.116     ng       4369: #		$companswer =~ s|<table border=\"1\">|<table border=\"0\">|g;
1.539     riegler  4370: 		$studentTable.='&nbsp;<b>'.$title.'</b>&nbsp;<br />&nbsp;<b>'.&mt('Correct answer').':</b><br />'.$companswer;
1.71      ng       4371: 	    }
                   4372: 
1.257     albertel 4373: 	    my %record = &Apache::lonnet::restore($symbx,$env{'request.course.id'},$udom,$uname);
1.125     ng       4374: 
1.257     albertel 4375: 	    if ($env{'form.lastSub'} eq 'datesub') {
1.71      ng       4376: 		if ($record{'version'} eq '') {
1.485     albertel 4377: 		    $studentTable.='<br />&nbsp;<span class="LC_warning">'.&mt('No recorded submission for this problem.').'</span><br />';
1.71      ng       4378: 		} else {
1.116     ng       4379: 		    my %responseType = ();
                   4380: 		    foreach my $partid (@{$parts}) {
1.147     albertel 4381: 			my @responseIds =$curRes->responseIds($partid);
                   4382: 			my @responseType =$curRes->responseType($partid);
                   4383: 			my %responseIds;
                   4384: 			for (my $i=0;$i<=$#responseIds;$i++) {
                   4385: 			    $responseIds{$responseIds[$i]}=$responseType[$i];
                   4386: 			}
                   4387: 			$responseType{$partid} = \%responseIds;
1.116     ng       4388: 		    }
1.148     albertel 4389: 		    $studentTable.= &displaySubByDates($symbx,\%record,$parts,\%responseType,$checkIcon,$uname,$udom);
1.147     albertel 4390: 
1.71      ng       4391: 		}
1.257     albertel 4392: 	    } elsif ($env{'form.lastSub'} eq 'all') {
                   4393: 		my $last = ($env{'form.lastSub'} eq 'last' ? 'last' : '');
1.71      ng       4394: 		$studentTable.=&Apache::loncommon::get_previous_attempt($symbx,$uname,$udom,
1.257     albertel 4395: 									$env{'request.course.id'},
1.71      ng       4396: 									'','.submission');
                   4397:  
                   4398: 	    }
1.103     albertel 4399: 	    if (&canmodify($usec)) {
                   4400: 		foreach my $partid (@{$parts}) {
                   4401: 		    $studentTable.=&gradeBox($request,$symbx,$uname,$udom,$question,$partid,\%record);
                   4402: 		    $studentTable.='<input type="hidden" name="q_'.$question.'" value="'.$partid.'" />'."\n";
                   4403: 		    $question++;
                   4404: 		}
1.196     albertel 4405: 		$prob++;
1.71      ng       4406: 	    }
                   4407: 	    $studentTable.='</td></tr>';
1.68      ng       4408: 
1.103     albertel 4409: 	}
1.68      ng       4410:         $curRes = $iterator->next();
                   4411:     }
                   4412: 
1.485     albertel 4413:     $studentTable.='</table>'."\n".
                   4414: 	'<input type="button" value="'.&mt('Save').'" '.
1.381     albertel 4415: 	'onClick="javascript:checkSubmitPage(this.form,'.$question.');" />'.
1.71      ng       4416: 	'</form>'."\n";
1.324     albertel 4417:     $studentTable.=&show_grading_menu_form($symb);
1.71      ng       4418:     $request->print($studentTable);
                   4419: 
                   4420:     return '';
1.119     ng       4421: }
                   4422: 
                   4423: sub displaySubByDates {
1.148     albertel 4424:     my ($symb,$record,$parts,$responseType,$checkIcon,$uname,$udom) = @_;
1.224     albertel 4425:     my $isCODE=0;
1.335     albertel 4426:     my $isTask = ($symb =~/\.task$/);
1.224     albertel 4427:     if (exists($record->{'resource.CODE'})) { $isCODE=1; }
1.467     albertel 4428:     my $studentTable=&Apache::loncommon::start_data_table().
                   4429: 	&Apache::loncommon::start_data_table_header_row().
                   4430: 	'<th>'.&mt('Date/Time').'</th>'.
                   4431: 	($isCODE?'<th>'.&mt('CODE').'</th>':'').
                   4432: 	'<th>'.&mt('Submission').'</th>'.
                   4433: 	'<th>'.&mt('Status').'</th>'.
                   4434: 	&Apache::loncommon::end_data_table_header_row();
1.119     ng       4435:     my ($version);
                   4436:     my %mark;
1.148     albertel 4437:     my %orders;
1.119     ng       4438:     $mark{'correct_by_student'} = $checkIcon;
1.147     albertel 4439:     if (!exists($$record{'1:timestamp'})) {
1.539     riegler  4440: 	return '<br />&nbsp;<span class="LC_warning">'.&mt('Nothing submitted - no attempts.').'</span><br />';
1.147     albertel 4441:     }
1.335     albertel 4442: 
                   4443:     my $interaction;
1.525     raeburn  4444:     my $no_increment = 1;
1.119     ng       4445:     for ($version=1;$version<=$$record{'version'};$version++) {
1.467     albertel 4446: 	my $timestamp = 
                   4447: 	    &Apache::lonlocal::locallocaltime($$record{$version.':timestamp'});
1.335     albertel 4448: 	if (exists($$record{$version.':resource.0.version'})) {
                   4449: 	    $interaction = $$record{$version.':resource.0.version'};
                   4450: 	}
                   4451: 
                   4452: 	my $where = ($isTask ? "$version:resource.$interaction"
                   4453: 		             : "$version:resource");
1.467     albertel 4454: 	$studentTable.=&Apache::loncommon::start_data_table_row().
                   4455: 	    '<td>'.$timestamp.'</td>';
1.224     albertel 4456: 	if ($isCODE) {
                   4457: 	    $studentTable.='<td>'.$record->{$version.':resource.CODE'}.'</td>';
                   4458: 	}
1.119     ng       4459: 	my @versionKeys = split(/\:/,$$record{$version.':keys'});
                   4460: 	my @displaySub = ();
                   4461: 	foreach my $partid (@{$parts}) {
1.335     albertel 4462: 	    my @matchKey = ($isTask ? sort(grep /^resource\.\d+\.\Q$partid\E\.award$/,@versionKeys)
                   4463: 			            : sort(grep /^resource\.\Q$partid\E\..*?\.submission$/,@versionKeys));
                   4464: 	    
                   4465: 
1.122     ng       4466: #	    next if ($$record{"$version:resource.$partid.solved"} eq '');
1.324     albertel 4467: 	    my $display_part=&get_display_part($partid,$symb);
1.147     albertel 4468: 	    foreach my $matchKey (@matchKey) {
1.198     albertel 4469: 		if (exists($$record{$version.':'.$matchKey}) &&
                   4470: 		    $$record{$version.':'.$matchKey} ne '') {
1.335     albertel 4471: 
                   4472: 		    my ($responseId)= ($isTask ? ($matchKey=~ /^resource\.(.*?)\.\Q$partid\E\.award$/)
                   4473: 				               : ($matchKey=~ /^resource\.\Q$partid\E\.(.*?)\.submission$/));
1.577     bisitz   4474:                     $displaySub[0].='<span class="LC_nobreak"';
                   4475:                     $displaySub[0].='<b>'.&mt('Part: [_1]',$display_part).'</b>'
                   4476:                                    .' <span class="LC_internal_info">'
                   4477:                                    .'('.&mt('Part ID: [_1]',$responseId).')'
                   4478:                                    .'</span>'
                   4479:                                    .' <b>';
1.335     albertel 4480: 		    if ($$record{"$where.$partid.tries"} eq '') {
1.577     bisitz   4481: 			$displaySub[0].=&mt('Trial not counted');
1.147     albertel 4482: 		    } else {
1.577     bisitz   4483: 			$displaySub[0].=&mt('Trial: [_1]',
1.467     albertel 4484: 					    $$record{"$where.$partid.tries"});
1.147     albertel 4485: 		    }
1.335     albertel 4486: 		    my $responseType=($isTask ? 'Task'
                   4487:                                               : $responseType->{$partid}->{$responseId});
1.148     albertel 4488: 		    if (!exists($orders{$partid})) { $orders{$partid}={}; }
                   4489: 		    if (!exists($orders{$partid}->{$responseId})) {
                   4490: 			$orders{$partid}->{$responseId}=
1.525     raeburn  4491: 			    &get_order($partid,$responseId,$symb,$uname,$udom,
                   4492:                                        $no_increment);
1.148     albertel 4493: 		    }
1.577     bisitz   4494: 		    $displaySub[0].='</b></span>'; # /nobreak
                   4495: 		    $displaySub[0].='&nbsp; '.
1.336     albertel 4496: 			&cleanRecord($$record{$version.':'.$matchKey},$responseType,$symb,$partid,$responseId,$record,$orders{$partid}->{$responseId},"$version:",$uname,$udom).'<br />';
1.147     albertel 4497: 		}
                   4498: 	    }
1.335     albertel 4499: 	    if (exists($$record{"$where.$partid.checkedin"})) {
1.485     albertel 4500: 		$displaySub[1].=&mt('Checked in by [_1] into slot [_2]',
                   4501: 				    $$record{"$where.$partid.checkedin"},
                   4502: 				    $$record{"$where.$partid.checkedin.slot"}).
                   4503: 					'<br />';
1.335     albertel 4504: 	    }
                   4505: 	    if (exists $$record{"$where.$partid.award"}) {
1.485     albertel 4506: 		$displaySub[1].='<b>'.&mt('Part:').'</b>&nbsp;'.$display_part.' &nbsp;'.
1.335     albertel 4507: 		    lc($$record{"$where.$partid.award"}).' '.
                   4508: 		    $mark{$$record{"$where.$partid.solved"}}.
1.147     albertel 4509: 		    '<br />';
                   4510: 	    }
1.335     albertel 4511: 	    if (exists $$record{"$where.$partid.regrader"}) {
                   4512: 		$displaySub[2].=$$record{"$where.$partid.regrader"}.
                   4513: 		    ' (<b>'.&mt('Part').':</b> '.$display_part.')';
                   4514: 	    } elsif ($$record{"$version:resource.$partid.regrader"} =~ /\S/) {
                   4515: 		$displaySub[2].=
                   4516: 		    $$record{"$version:resource.$partid.regrader"}.
1.207     albertel 4517: 		    ' (<b>'.&mt('Part').':</b> '.$display_part.')';
1.147     albertel 4518: 	    }
                   4519: 	}
                   4520: 	# needed because old essay regrader has not parts info
                   4521: 	if (exists $$record{"$version:resource.regrader"}) {
                   4522: 	    $displaySub[2].=$$record{"$version:resource.regrader"};
                   4523: 	}
                   4524: 	$studentTable.='<td>'.$displaySub[0].'&nbsp;</td><td>'.$displaySub[1];
                   4525: 	if ($displaySub[2]) {
1.467     albertel 4526: 	    $studentTable.=&mt('Manually graded by [_1]',$displaySub[2]);
1.147     albertel 4527: 	}
1.467     albertel 4528: 	$studentTable.='&nbsp;</td>'.
                   4529: 	    &Apache::loncommon::end_data_table_row();
1.119     ng       4530:     }
1.467     albertel 4531:     $studentTable.=&Apache::loncommon::end_data_table();
1.119     ng       4532:     return $studentTable;
1.71      ng       4533: }
                   4534: 
                   4535: sub updateGradeByPage {
                   4536:     my ($request) = shift;
                   4537: 
1.257     albertel 4538:     my $cdom      = $env{"course.$env{'request.course.id'}.domain"};
                   4539:     my $cnum      = $env{"course.$env{'request.course.id'}.num"};
                   4540:     my $getsec    = $env{'form.section'} eq '' ? 'all' : $env{'form.section'};
                   4541:     my $pageTitle = $env{'form.page'};
1.103     albertel 4542:     my ($classlist,undef,$fullname) = &getclasslist($getsec,'1');
1.257     albertel 4543:     my ($uname,$udom) = split(/:/,$env{'form.student'});
                   4544:     my $usec=$classlist->{$env{'form.student'}}[5];
1.103     albertel 4545:     if (!&canmodify($usec)) {
1.526     raeburn  4546: 	$request->print('<span class="LC_warning">'.&mt('Unable to modify requested student ([_1])',$env{'form.student'}).'</span>');
1.324     albertel 4547: 	$request->print(&show_grading_menu_form($env{'form.symb'}));
1.103     albertel 4548: 	return;
                   4549:     }
1.398     albertel 4550:     my $result='<h3><span class="LC_info">&nbsp;'.$env{'form.title'}.'</span></h3>';
1.526     raeburn  4551:     $result.='<h3>&nbsp;'.&mt('Student: ').&nameUserString(undef,$env{'form.fullname'},$uname,$udom).
1.129     ng       4552: 	'</h3>'."\n";
1.70      ng       4553: 
1.68      ng       4554:     $request->print($result);
                   4555: 
1.132     bowersj2 4556:     my $navmap = Apache::lonnavmaps::navmap->new();
1.257     albertel 4557:     my ($mapUrl, $id, $resUrl) = &Apache::lonnet::decode_symb( $env{'form.page'});
1.71      ng       4558:     my $map = $navmap->getResourceByUrl($resUrl); # add to navmaps
1.288     albertel 4559:     if (!$map) {
1.527     raeburn  4560: 	$request->print('<span class="LC_warning">'.&mt('Unable to grade requested sequence ([_1]).',$resUrl).'</span>');
1.324     albertel 4561: 	my ($symb)=&get_symb($request);
                   4562: 	$request->print(&show_grading_menu_form($symb));
1.288     albertel 4563: 	return; 
                   4564:     }
1.71      ng       4565:     my $iterator = $navmap->getIterator($map->map_start(),
                   4566: 					$map->map_finish());
1.70      ng       4567: 
1.484     albertel 4568:     my $studentTable=
                   4569: 	&Apache::loncommon::start_data_table().
                   4570: 	&Apache::loncommon::start_data_table_header_row().
1.485     albertel 4571: 	'<th align="center">&nbsp;'.&mt('Prob.').'&nbsp;</th>'.
                   4572: 	'<th>&nbsp;'.&mt('Title').'&nbsp;</th>'.
                   4573: 	'<th>&nbsp;'.&mt('Previous Score').'&nbsp;</th>'.
                   4574: 	'<th>&nbsp;'.&mt('New Score').'&nbsp;</th>'.
1.484     albertel 4575: 	&Apache::loncommon::end_data_table_header_row();
1.71      ng       4576: 
                   4577:     $iterator->next(); # skip the first BEGIN_MAP
                   4578:     my $curRes = $iterator->next(); # for "current resource"
1.196     albertel 4579:     my ($depth,$question,$prob,$changeflag)= (1,1,1,0);
1.101     albertel 4580:     while ($depth > 0) {
1.71      ng       4581:         if($curRes == $iterator->BEGIN_MAP) { $depth++; }
1.100     bowersj2 4582:         if($curRes == $iterator->END_MAP) { $depth--; }
1.71      ng       4583: 
1.385     albertel 4584:         if (ref($curRes) && $curRes->is_problem()) {
1.91      albertel 4585: 	    my $parts = $curRes->parts();
1.71      ng       4586:             my $title = $curRes->compTitle();
                   4587: 	    my $symbx = $curRes->symb();
1.484     albertel 4588: 	    $studentTable.=
                   4589: 		&Apache::loncommon::start_data_table_row().
                   4590: 		'<td align="center" valign="top" >'.$prob.
1.485     albertel 4591: 		(scalar(@{$parts}) == 1 ? '' 
1.526     raeburn  4592:                                         : '<br />('.&mt('[quant,_1,&nbsp;part]',scalar(@{$parts}))
                   4593: 		.')').'</td>';
1.71      ng       4594: 	    $studentTable.='<td valign="top">&nbsp;<b>'.$title.'</b>&nbsp;</td>';
                   4595: 
                   4596: 	    my %newrecord=();
                   4597: 	    my @displayPts=();
1.269     raeburn  4598:             my %aggregate = ();
                   4599:             my $aggregateflag = 0;
1.71      ng       4600: 	    foreach my $partid (@{$parts}) {
1.257     albertel 4601: 		my $newpts = $env{'form.GD_BOX'.$question.'_'.$partid};
                   4602: 		my $oldpts = $env{'form.oldpts'.$question.'_'.$partid};
1.71      ng       4603: 
1.257     albertel 4604: 		my $wgt = $env{'form.WGT'.$question.'_'.$partid} != 0 ? 
                   4605: 		    $env{'form.WGT'.$question.'_'.$partid} : 1;
1.71      ng       4606: 		my $partial = $newpts/$wgt;
                   4607: 		my $score;
                   4608: 		if ($partial > 0) {
                   4609: 		    $score = 'correct_by_override';
1.125     ng       4610: 		} elsif ($newpts ne '') { #empty is taken as 0
1.71      ng       4611: 		    $score = 'incorrect_by_override';
                   4612: 		}
1.257     albertel 4613: 		my $dropMenu = $env{'form.GD_SEL'.$question.'_'.$partid};
1.125     ng       4614: 		if ($dropMenu eq 'excused') {
1.71      ng       4615: 		    $partial = '';
                   4616: 		    $score = 'excused';
1.125     ng       4617: 		} elsif ($dropMenu eq 'reset status'
1.257     albertel 4618: 			 && $env{'form.solved'.$question.'_'.$partid} ne '') { #update only if previous record exists
1.125     ng       4619: 		    $newrecord{'resource.'.$partid.'.tries'} = 0;
                   4620: 		    $newrecord{'resource.'.$partid.'.solved'} = '';
                   4621: 		    $newrecord{'resource.'.$partid.'.award'} = '';
                   4622: 		    $newrecord{'resource.'.$partid.'.awarded'} = 0;
1.257     albertel 4623: 		    $newrecord{'resource.'.$partid.'.regrader'} = "$env{'user.name'}:$env{'user.domain'}";
1.125     ng       4624: 		    $changeflag++;
                   4625: 		    $newpts = '';
1.269     raeburn  4626:                     
                   4627:                     my $aggtries =  $env{'form.aggtries'.$question.'_'.$partid};
                   4628:                     my $totaltries = $env{'form.totaltries'.$question.'_'.$partid};
                   4629:                     my $solvedstatus = $env{'form.solved'.$question.'_'.$partid};
                   4630:                     if ($aggtries > 0) {
                   4631:                         &decrement_aggs($symbx,$partid,\%aggregate,$aggtries,$totaltries,$solvedstatus);
                   4632:                         $aggregateflag = 1;
                   4633:                     }
1.71      ng       4634: 		}
1.324     albertel 4635: 		my $display_part=&get_display_part($partid,$curRes->symb());
1.257     albertel 4636: 		my $oldstatus = $env{'form.solved'.$question.'_'.$partid};
1.526     raeburn  4637: 		$displayPts[0].='&nbsp;<b>'.&mt('Part').':</b> '.$display_part.' = '.
1.71      ng       4638: 		    (($oldstatus eq 'excused') ? 'excused' : $oldpts).
1.326     albertel 4639: 		    '&nbsp;<br />';
1.526     raeburn  4640: 		$displayPts[1].='&nbsp;<b>'.&mt('Part').':</b> '.$display_part.' = '.
1.125     ng       4641: 		     (($score eq 'excused') ? 'excused' : $newpts).
1.326     albertel 4642: 		    '&nbsp;<br />';
1.71      ng       4643: 		$question++;
1.380     albertel 4644: 		next if ($dropMenu eq 'reset status' || ($newpts eq $oldpts && $score ne 'excused'));
1.125     ng       4645: 
1.71      ng       4646: 		$newrecord{'resource.'.$partid.'.awarded'}  = $partial if $partial ne '';
1.125     ng       4647: 		$newrecord{'resource.'.$partid.'.solved'}   = $score if $score ne '';
1.257     albertel 4648: 		$newrecord{'resource.'.$partid.'.regrader'} = "$env{'user.name'}:$env{'user.domain'}"
1.125     ng       4649: 		    if (scalar(keys(%newrecord)) > 0);
1.71      ng       4650: 
                   4651: 		$changeflag++;
                   4652: 	    }
                   4653: 	    if (scalar(keys(%newrecord)) > 0) {
1.382     albertel 4654: 		my %record = 
                   4655: 		    &Apache::lonnet::restore($symbx,$env{'request.course.id'},
                   4656: 					     $udom,$uname);
                   4657: 
                   4658: 		if (&Apache::lonnet::validCODE($env{'form.CODE'})) {
                   4659: 		    $newrecord{'resource.CODE'} = $env{'form.CODE'};
                   4660: 		} elsif (&Apache::lonnet::validCODE($record{'resource.CODE'})) {
                   4661: 		    $newrecord{'resource.CODE'} = '';
                   4662: 		}
1.257     albertel 4663: 		&Apache::lonnet::cstore(\%newrecord,$symbx,$env{'request.course.id'},
1.71      ng       4664: 					$udom,$uname);
1.382     albertel 4665: 		%record = &Apache::lonnet::restore($symbx,
                   4666: 						   $env{'request.course.id'},
                   4667: 						   $udom,$uname);
1.380     albertel 4668: 		&check_and_remove_from_queue($parts,\%record,undef,$symbx,
                   4669: 					     $cdom,$cnum,$udom,$uname);
1.71      ng       4670: 	    }
1.380     albertel 4671: 	    
1.269     raeburn  4672:             if ($aggregateflag) {
                   4673:                 &Apache::lonnet::cinc('nohist_resourcetracker',\%aggregate,
                   4674:                       $env{'course.'.$env{'request.course.id'}.'.domain'},
                   4675:                       $env{'course.'.$env{'request.course.id'}.'.num'});
                   4676:             }
1.125     ng       4677: 
1.71      ng       4678: 	    $studentTable.='<td valign="top">'.$displayPts[0].'</td>'.
                   4679: 		'<td valign="top">'.$displayPts[1].'</td>'.
1.484     albertel 4680: 		&Apache::loncommon::end_data_table_row();
1.68      ng       4681: 
1.196     albertel 4682: 	    $prob++;
1.68      ng       4683: 	}
1.71      ng       4684:         $curRes = $iterator->next();
1.68      ng       4685:     }
1.98      albertel 4686: 
1.484     albertel 4687:     $studentTable.=&Apache::loncommon::end_data_table();
1.324     albertel 4688:     $studentTable.=&show_grading_menu_form($env{'form.symb'});
1.526     raeburn  4689:     my $grademsg=($changeflag == 0 ? &mt('No score was changed or updated.') :
                   4690: 		  &mt('The scores were changed for [quant,_1,problem].',
                   4691: 		  $changeflag));
1.76      ng       4692:     $request->print($grademsg.$studentTable);
1.68      ng       4693: 
1.70      ng       4694:     return '';
                   4695: }
                   4696: 
1.72      ng       4697: #-------- end of section for handling grading by page/sequence ---------
                   4698: #
                   4699: #-------------------------------------------------------------------
                   4700: 
1.75      albertel 4701: #--------------------Scantron Grading-----------------------------------
                   4702: #
                   4703: #------ start of section for handling grading by page/sequence ---------
                   4704: 
1.423     albertel 4705: =pod
                   4706: 
                   4707: =head1 Bubble sheet grading routines
                   4708: 
1.424     albertel 4709:   For this documentation:
                   4710: 
                   4711:    'scanline' refers to the full line of characters
                   4712:    from the file that we are parsing that represents one entire sheet
                   4713: 
                   4714:    'bubble line' refers to the data
                   4715:    representing the line of bubbles that are on the physical bubble sheet
                   4716: 
                   4717: 
                   4718: The overall process is that a scanned in bubble sheet data is uploaded
                   4719: into a course. When a user wants to grade, they select a
                   4720: sequence/folder of resources, a file of bubble sheet info, and pick
                   4721: one of the predefined configurations for what each scanline looks
                   4722: like.
                   4723: 
                   4724: Next each scanline is checked for any errors of either 'missing
1.435     foxr     4725: bubbles' (it's an error because it may have been mis-scanned
1.424     albertel 4726: because too light bubbling), 'double bubble' (each bubble line should
                   4727: have no more that one letter picked), invalid or duplicated CODE,
1.556     weissno  4728: invalid student/employee ID
1.424     albertel 4729: 
                   4730: If the CODE option is used that determines the randomization of the
1.556     weissno  4731: homework problems, either way the student/employee ID is looked up into a
1.424     albertel 4732: username:domain.
                   4733: 
                   4734: During the validation phase the instructor can choose to skip scanlines. 
                   4735: 
1.435     foxr     4736: After the validation phase, there are now 3 bubble sheet files
1.424     albertel 4737: 
                   4738:   scantron_original_filename (unmodified original file)
                   4739:   scantron_corrected_filename (file where the corrected information has replaced the original information)
                   4740:   scantron_skipped_filename (contains the exact text of scanlines that where skipped)
                   4741: 
                   4742: Also there is a separate hash nohist_scantrondata that contains extra
                   4743: correction information that isn't representable in the bubble sheet
                   4744: file (see &scantron_getfile() for more information)
                   4745: 
                   4746: After all scanlines are either valid, marked as valid or skipped, then
                   4747: foreach line foreach problem in the picked sequence, an ssi request is
                   4748: made that simulates a user submitting their selected letter(s) against
                   4749: the homework problem.
1.423     albertel 4750: 
                   4751: =over 4
                   4752: 
                   4753: 
                   4754: 
                   4755: =item defaultFormData
                   4756: 
                   4757:   Returns html hidden inputs used to hold context/default values.
                   4758: 
                   4759:  Arguments:
                   4760:   $symb - $symb of the current resource 
                   4761: 
                   4762: =cut
1.422     foxr     4763: 
1.81      albertel 4764: sub defaultFormData {
1.324     albertel 4765:     my ($symb)=@_;
1.447     foxr     4766:     return '<input type="hidden" name="symb"    value="'.&Apache::lonenc::check_encrypt($symb).'" />'."\n".
1.257     albertel 4767:      '<input type="hidden" name="saveState" value="'.$env{'form.saveState'}.'" />'."\n".
                   4768:      '<input type="hidden" name="probTitle" value="'.$env{'form.probTitle'}.'" />'."\n";
1.81      albertel 4769: }
                   4770: 
1.447     foxr     4771: 
1.423     albertel 4772: =pod 
                   4773: 
                   4774: =item getSequenceDropDown
                   4775: 
                   4776:    Return html dropdown of possible sequences to grade
                   4777:  
                   4778:  Arguments:
                   4779:    $symb - $symb of the current resource 
                   4780: 
                   4781: =cut
1.422     foxr     4782: 
1.75      albertel 4783: sub getSequenceDropDown {
1.423     albertel 4784:     my ($symb)=@_;
1.75      albertel 4785:     my $result='<select name="selectpage">'."\n";
1.423     albertel 4786:     my ($titles,$symbx) = &getSymbMap();
1.137     albertel 4787:     my ($curpage)=&Apache::lonnet::decode_symb($symb); 
1.75      albertel 4788:     my $ctr=0;
                   4789:     foreach (@$titles) {
                   4790: 	my ($minder,$showtitle) = ($_ =~ /(\d+)\.(.*)/);
                   4791: 	$result.='<option value="'.$$symbx{$_}.'" '.
1.401     albertel 4792: 	    ($$symbx{$_} =~ /$curpage$/ ? 'selected="selected"' : '').
1.75      albertel 4793: 	    '>'.$showtitle.'</option>'."\n";
                   4794: 	$ctr++;
                   4795:     }
                   4796:     $result.= '</select>';
                   4797:     return $result;
                   4798: }
                   4799: 
1.495     albertel 4800: my %bubble_lines_per_response;     # no. bubble lines for each response.
1.554     raeburn  4801:                                    # key is zero-based index - 0, 1, 2 ...
1.495     albertel 4802: 
                   4803: my %first_bubble_line;             # First bubble line no. for each bubble.
                   4804: 
1.509     raeburn  4805: my %subdivided_bubble_lines;       # no. bubble lines for optionresponse, 
                   4806:                                    # matchresponse or rankresponse, where 
                   4807:                                    # an individual response can have multiple 
                   4808:                                    # lines
1.503     raeburn  4809: 
                   4810: my %responsetype_per_response;     # responsetype for each response
                   4811: 
1.495     albertel 4812: # Save and restore the bubble lines array to the form env.
                   4813: 
                   4814: 
                   4815: sub save_bubble_lines {
                   4816:     foreach my $line (keys(%bubble_lines_per_response)) {
                   4817: 	$env{"form.scantron.bubblelines.$line"}  = $bubble_lines_per_response{$line};
                   4818: 	$env{"form.scantron.first_bubble_line.$line"} =
                   4819: 	    $first_bubble_line{$line};
1.503     raeburn  4820:         $env{"form.scantron.sub_bubblelines.$line"} = 
                   4821:             $subdivided_bubble_lines{$line};
                   4822:         $env{"form.scantron.responsetype.$line"} =
                   4823:             $responsetype_per_response{$line};
1.495     albertel 4824:     }
                   4825: }
                   4826: 
                   4827: 
                   4828: sub restore_bubble_lines {
                   4829:     my $line = 0;
                   4830:     %bubble_lines_per_response = ();
                   4831:     while ($env{"form.scantron.bubblelines.$line"}) {
                   4832: 	my $value = $env{"form.scantron.bubblelines.$line"};
                   4833: 	$bubble_lines_per_response{$line} = $value;
                   4834: 	$first_bubble_line{$line}  =
                   4835: 	    $env{"form.scantron.first_bubble_line.$line"};
1.503     raeburn  4836:         $subdivided_bubble_lines{$line} =
                   4837:             $env{"form.scantron.sub_bubblelines.$line"};
                   4838:         $responsetype_per_response{$line} =
                   4839:             $env{"form.scantron.responsetype.$line"};
1.495     albertel 4840: 	$line++;
                   4841:     }
                   4842: }
                   4843: 
                   4844: #  Given the parsed scanline, get the response for 
                   4845: #  'answer' number n:
                   4846: 
                   4847: sub get_response_bubbles {
                   4848:     my ($parsed_line, $response)  = @_;
                   4849: 
                   4850:     my $bubble_line = $first_bubble_line{$response-1} +1;
                   4851:     my $bubble_lines= $bubble_lines_per_response{$response-1};
                   4852:     
                   4853:     my $selected = "";
                   4854: 
                   4855:     for (my $bline = 0; $bline < $bubble_lines; $bline++) {
                   4856: 	$selected .= $$parsed_line{"scantron.$bubble_line.answer"}.":";
                   4857: 	$bubble_line++;
                   4858:     }
                   4859:     return $selected;
                   4860: }
1.423     albertel 4861: 
                   4862: =pod 
                   4863: 
                   4864: =item scantron_filenames
                   4865: 
                   4866:    Returns a list of the scantron files in the current course 
                   4867: 
                   4868: =cut
1.422     foxr     4869: 
1.202     albertel 4870: sub scantron_filenames {
1.257     albertel 4871:     my $cdom=$env{'course.'.$env{'request.course.id'}.'.domain'};
                   4872:     my $cname=$env{'course.'.$env{'request.course.id'}.'.num'};
1.517     raeburn  4873:     my $getpropath = 1;
1.157     albertel 4874:     my @files=&Apache::lonnet::dirlist('userfiles',$cdom,$cname,
1.517     raeburn  4875:                                        $getpropath);
1.202     albertel 4876:     my @possiblenames;
1.201     albertel 4877:     foreach my $filename (sort(@files)) {
1.157     albertel 4878: 	($filename)=split(/&/,$filename);
                   4879: 	if ($filename!~/^scantron_orig_/) { next ; }
                   4880: 	$filename=~s/^scantron_orig_//;
1.202     albertel 4881: 	push(@possiblenames,$filename);
                   4882:     }
                   4883:     return @possiblenames;
                   4884: }
                   4885: 
1.423     albertel 4886: =pod 
                   4887: 
                   4888: =item scantron_uploads
                   4889: 
                   4890:    Returns  html drop-down list of scantron files in current course.
                   4891: 
                   4892:  Arguments:
                   4893:    $file2grade - filename to set as selected in the dropdown
                   4894: 
                   4895: =cut
1.422     foxr     4896: 
1.202     albertel 4897: sub scantron_uploads {
1.209     ng       4898:     my ($file2grade) = @_;
1.202     albertel 4899:     my $result=	'<select name="scantron_selectfile">';
                   4900:     $result.="<option></option>";
                   4901:     foreach my $filename (sort(&scantron_filenames())) {
1.401     albertel 4902: 	$result.="<option".($filename eq $file2grade ? ' selected="selected"':'').">$filename</option>\n";
1.81      albertel 4903:     }
                   4904:     $result.="</select>";
                   4905:     return $result;
                   4906: }
                   4907: 
1.423     albertel 4908: =pod 
                   4909: 
                   4910: =item scantron_scantab
                   4911: 
                   4912:   Returns html drop down of the scantron formats in the scantronformat.tab
                   4913:   file.
                   4914: 
                   4915: =cut
1.422     foxr     4916: 
1.82      albertel 4917: sub scantron_scantab {
                   4918:     my $result='<select name="scantron_format">'."\n";
1.191     albertel 4919:     $result.='<option></option>'."\n";
1.518     raeburn  4920:     my @lines = &get_scantronformat_file();
                   4921:     if (@lines > 0) {
                   4922:         foreach my $line (@lines) {
                   4923:             next if (($line =~ /^\#/) || ($line eq ''));
                   4924: 	    my ($name,$descrip)=split(/:/,$line);
                   4925: 	    $result.='<option value="'.$name.'">'.$descrip.'</option>'."\n";
                   4926:         }
1.82      albertel 4927:     }
                   4928:     $result.='</select>'."\n";
1.518     raeburn  4929:     return $result;
                   4930: }
                   4931: 
                   4932: =pod
                   4933: 
                   4934: =item get_scantronformat_file
                   4935: 
                   4936:   Returns an array containing lines from the scantron format file for
                   4937:   the domain of the course.
                   4938: 
                   4939:   If a url for a custom.tab file is listed in domain's configuration.db, 
                   4940:   lines are from this file.
                   4941: 
                   4942:   Otherwise, if a default.tab has been published in RES space by the 
                   4943:   domainconfig user, lines are from this file.
                   4944: 
                   4945:   Otherwise, fall back to getting lines from the legacy file on the
1.519     raeburn  4946:   local server:  /home/httpd/lonTabs/default_scantronformat.tab    
1.82      albertel 4947: 
1.518     raeburn  4948: =cut
                   4949: 
                   4950: sub get_scantronformat_file {
                   4951:     my $cdom= $env{'course.'.$env{'request.course.id'}.'.domain'};
                   4952:     my %domconfig = &Apache::lonnet::get_dom('configuration',['scantron'],$cdom);
                   4953:     my $gottab = 0;
                   4954:     my @lines;
                   4955:     if (ref($domconfig{'scantron'}) eq 'HASH') {
                   4956:         if ($domconfig{'scantron'}{'scantronformat'} ne '') {
                   4957:             my $formatfile = &Apache::lonnet::getfile($Apache::lonnet::perlvar{'lonDocRoot'}.$domconfig{'scantron'}{'scantronformat'});
                   4958:             if ($formatfile ne '-1') {
                   4959:                 @lines = split("\n",$formatfile,-1);
                   4960:                 $gottab = 1;
                   4961:             }
                   4962:         }
                   4963:     }
                   4964:     if (!$gottab) {
                   4965:         my $confname = $cdom.'-domainconfig';
                   4966:         my $default = $Apache::lonnet::perlvar{'lonDocRoot'}.'/res/'.$cdom.'/'.$confname.'/default.tab';
                   4967:         my $formatfile =  &Apache::lonnet::getfile($default);
                   4968:         if ($formatfile ne '-1') {
                   4969:             @lines = split("\n",$formatfile,-1);
                   4970:             $gottab = 1;
                   4971:         }
                   4972:     }
                   4973:     if (!$gottab) {
1.519     raeburn  4974:         my @domains = &Apache::lonnet::current_machine_domains();
                   4975:         if (grep(/^\Q$cdom\E$/,@domains)) {
                   4976:             my $fh=Apache::File->new($Apache::lonnet::perlvar{'lonTabDir'}.'/scantronformat.tab');
                   4977:             @lines = <$fh>;
                   4978:             close($fh);
                   4979:         } else {
                   4980:             my $fh=Apache::File->new($Apache::lonnet::perlvar{'lonTabDir'}.'/default_scantronformat.tab');
                   4981:             @lines = <$fh>;
                   4982:             close($fh);
                   4983:         }
1.518     raeburn  4984:     }
                   4985:     return @lines;
1.82      albertel 4986: }
                   4987: 
1.423     albertel 4988: =pod 
                   4989: 
                   4990: =item scantron_CODElist
                   4991: 
                   4992:   Returns html drop down of the saved CODE lists from current course,
                   4993:   generated from earlier printings.
                   4994: 
                   4995: =cut
1.422     foxr     4996: 
1.186     albertel 4997: sub scantron_CODElist {
1.257     albertel 4998:     my $cdom = $env{'course.'.$env{'request.course.id'}.'.domain'};
                   4999:     my $cnum = $env{'course.'.$env{'request.course.id'}.'.num'};
1.186     albertel 5000:     my @names=&Apache::lonnet::getkeys('CODEs',$cdom,$cnum);
                   5001:     my $namechoice='<option></option>';
1.225     albertel 5002:     foreach my $name (sort {uc($a) cmp uc($b)} @names) {
1.191     albertel 5003: 	if ($name =~ /^error: 2 /) { next; }
1.278     albertel 5004: 	if ($name =~ /^type\0/) { next; }
1.186     albertel 5005: 	$namechoice.='<option value="'.$name.'">'.$name.'</option>';
                   5006:     }
                   5007:     $namechoice='<select name="scantron_CODElist">'.$namechoice.'</select>';
                   5008:     return $namechoice;
                   5009: }
                   5010: 
1.423     albertel 5011: =pod 
                   5012: 
                   5013: =item scantron_CODEunique
                   5014: 
                   5015:   Returns the html for "Each CODE to be used once" radio.
                   5016: 
                   5017: =cut
1.422     foxr     5018: 
1.186     albertel 5019: sub scantron_CODEunique {
1.532     bisitz   5020:     my $result='<span class="LC_nobreak">
1.272     albertel 5021:                  <label><input type="radio" name="scantron_CODEunique"
1.423     albertel 5022:                         value="yes" checked="checked" />'.&mt('Yes').' </label>
1.381     albertel 5023:                 </span>
1.532     bisitz   5024:                 <span class="LC_nobreak">
1.272     albertel 5025:                  <label><input type="radio" name="scantron_CODEunique"
1.423     albertel 5026:                         value="no" />'.&mt('No').' </label>
1.381     albertel 5027:                 </span>';
1.186     albertel 5028:     return $result;
                   5029: }
1.423     albertel 5030: 
                   5031: =pod 
                   5032: 
                   5033: =item scantron_selectphase
                   5034: 
                   5035:   Generates the initial screen to start the bubble sheet process.
                   5036:   Allows for - starting a grading run.
1.424     albertel 5037:              - downloading existing scan data (original, corrected
1.423     albertel 5038:                                                 or skipped info)
                   5039: 
                   5040:              - uploading new scan data
                   5041: 
                   5042:  Arguments:
                   5043:   $r          - The Apache request object
                   5044:   $file2grade - name of the file that contain the scanned data to score
                   5045: 
                   5046: =cut
1.186     albertel 5047: 
1.75      albertel 5048: sub scantron_selectphase {
1.209     ng       5049:     my ($r,$file2grade) = @_;
1.324     albertel 5050:     my ($symb)=&get_symb($r);
1.75      albertel 5051:     if (!$symb) {return '';}
1.423     albertel 5052:     my $sequence_selector=&getSequenceDropDown($symb);
1.324     albertel 5053:     my $default_form_data=&defaultFormData($symb);
                   5054:     my $grading_menu_button=&show_grading_menu_form($symb);
1.209     ng       5055:     my $file_selector=&scantron_uploads($file2grade);
1.82      albertel 5056:     my $format_selector=&scantron_scantab();
1.186     albertel 5057:     my $CODE_selector=&scantron_CODElist();
                   5058:     my $CODE_unique=&scantron_CODEunique();
1.75      albertel 5059:     my $result;
1.422     foxr     5060: 
1.513     foxr     5061:     $ssi_error = 0;
                   5062: 
1.422     foxr     5063:     # Chunk of form to prompt for a file to grade and how:
                   5064: 
1.489     albertel 5065:     $result.= '
                   5066:     <br />
                   5067:     <form method="post" enctype="multipart/form-data" action="/adm/grades" name="scantron_process">
                   5068:     <input type="hidden" name="command" value="scantron_warning" />
                   5069:     '.$default_form_data.'
                   5070:     '.&Apache::loncommon::start_data_table('LC_scantron_action').'
                   5071:        '.&Apache::loncommon::start_data_table_header_row().'
                   5072:             <th colspan="2">
1.492     albertel 5073:               &nbsp;'.&mt('Specify file and which Folder/Sequence to grade').'
1.489     albertel 5074:             </th>
                   5075:        '.&Apache::loncommon::end_data_table_header_row().'
                   5076:        '.&Apache::loncommon::start_data_table_row().'
1.492     albertel 5077:             <td> '.&mt('Sequence to grade:').' </td><td> '.$sequence_selector.' </td>
1.489     albertel 5078:        '.&Apache::loncommon::end_data_table_row().'
                   5079:        '.&Apache::loncommon::start_data_table_row().'
1.572     www      5080:             <td> '.&mt('Filename of bubblesheet data file:').' </td><td> '.$file_selector.' </td>
1.489     albertel 5081:        '.&Apache::loncommon::end_data_table_row().'
                   5082:        '.&Apache::loncommon::start_data_table_row().'
1.572     www      5083:             <td> '.&mt('Format of bubblesheet data file:').' </td><td> '.$format_selector.' </td>
1.489     albertel 5084:        '.&Apache::loncommon::end_data_table_row().'
                   5085:        '.&Apache::loncommon::start_data_table_row().'
1.492     albertel 5086:             <td> '.&mt('Saved CODEs to validate against:').' </td><td> '.$CODE_selector.' </td>
1.489     albertel 5087:        '.&Apache::loncommon::end_data_table_row().'
                   5088:        '.&Apache::loncommon::start_data_table_row().'
1.492     albertel 5089:             <td> '.&mt('Each CODE is only to be used once:').'</td><td> '.$CODE_unique.' </td>
1.489     albertel 5090:        '.&Apache::loncommon::end_data_table_row().'
                   5091:        '.&Apache::loncommon::start_data_table_row().'
1.492     albertel 5092: 	    <td> '.&mt('Options:').' </td>
1.187     albertel 5093:             <td>
1.492     albertel 5094: 	       <label><input type="checkbox" name="scantron_options_redo" value="redo_skipped"/> '.&mt('Do only previously skipped records').'</label> <br />
                   5095:                <label><input type="checkbox" name="scantron_options_ignore" value="ignore_corrections"/> '.&mt('Remove all existing corrections').'</label> <br />
                   5096:                <label><input type="checkbox" name="scantron_options_hidden" value="ignore_hidden"/> '.&mt('Skip hidden resources when grading').'</label>
1.187     albertel 5097: 	    </td>
1.489     albertel 5098:        '.&Apache::loncommon::end_data_table_row().'
                   5099:        '.&Apache::loncommon::start_data_table_row().'
1.174     albertel 5100:             <td colspan="2">
1.572     www      5101:               <input type="submit" value="'.&mt('Grading: Validate Bubblesheet Records').'" />
1.162     albertel 5102:             </td>
1.489     albertel 5103:        '.&Apache::loncommon::end_data_table_row().'
                   5104:     '.&Apache::loncommon::end_data_table().'
                   5105:     </form>
                   5106: ';
1.162     albertel 5107:    
                   5108:     $r->print($result);
                   5109: 
1.257     albertel 5110:     if (&Apache::lonnet::allowed('usc',$env{'request.role.domain'}) ||
                   5111:         &Apache::lonnet::allowed('usc',$env{'request.course.id'})) {
1.162     albertel 5112: 
1.422     foxr     5113: 	# Chunk of form to prompt for a scantron file upload.
                   5114: 
1.489     albertel 5115:         $r->print('
                   5116:     <br />
                   5117:     '.&Apache::loncommon::start_data_table('LC_scantron_action').'
                   5118:        '.&Apache::loncommon::start_data_table_header_row().'
                   5119:             <th>
1.572     www      5120:               &nbsp;'.&mt('Specify a bubblesheet data file to upload.').'
1.489     albertel 5121:             </th>
                   5122:        '.&Apache::loncommon::end_data_table_header_row().'
                   5123:        '.&Apache::loncommon::start_data_table_row().'
1.162     albertel 5124:             <td>
1.489     albertel 5125: ');
1.324     albertel 5126:     my $default_form_data=&defaultFormData(&get_symb($r,1));
1.257     albertel 5127:     my $cdom= $env{'course.'.$env{'request.course.id'}.'.domain'};
                   5128:     my $cnum= $env{'course.'.$env{'request.course.id'}.'.num'};
1.492     albertel 5129:     $r->print('
1.174     albertel 5130:               <script type="text/javascript" language="javascript">
                   5131:     function checkUpload(formname) {
                   5132: 	if (formname.upfile.value == "") {
1.492     albertel 5133: 	    alert("'.&mt('Please use the browse button to select a file from your local directory.').'");
1.174     albertel 5134: 	    return false;
                   5135: 	}
                   5136: 	formname.submit();
                   5137:     }
                   5138:               </script>
                   5139: 
1.492     albertel 5140:               <form enctype="multipart/form-data" action="/adm/grades" name="rules" method="post">
                   5141:                 '.$default_form_data.'
                   5142:                 <input name="courseid" type="hidden" value="'.$cnum.'" />
                   5143:                 <input name="domainid" type="hidden" value="'.$cdom.'" />
                   5144:                 <input name="command" value="scantronupload_save" type="hidden" />
                   5145:                 '.&mt('File to upload: [_1]','<input type="file" name="upfile" size="50" />').'
1.174     albertel 5146:                 <br />
1.572     www      5147:                 <input type="button" onClick="javascript:checkUpload(this.form);" value="'.&mt('Upload Bubblesheet Data').'" />
1.174     albertel 5148:               </form>
1.492     albertel 5149: ');
1.162     albertel 5150: 
1.489     albertel 5151:         $r->print('
1.162     albertel 5152:             </td>
1.489     albertel 5153:        '.&Apache::loncommon::end_data_table_row().'
                   5154:        '.&Apache::loncommon::end_data_table().'
                   5155: ');
1.162     albertel 5156:     }
1.422     foxr     5157: 
                   5158:     # Chunk of the form that prompts to view a scoring office file,
                   5159:     # corrected file, skipped records in a file.
                   5160: 
1.489     albertel 5161:     $r->print('
                   5162:    <br />
                   5163:    <form action="/adm/grades" name="scantron_download">
                   5164:      '.$default_form_data.'
                   5165:      <input type="hidden" name="command" value="scantron_download" />
                   5166:      '.&Apache::loncommon::start_data_table('LC_scantron_action').'
                   5167:        '.&Apache::loncommon::start_data_table_header_row().'
                   5168:               <th>
1.492     albertel 5169:                 &nbsp;'.&mt('Download a scoring office file').'
1.489     albertel 5170:               </th>
                   5171:        '.&Apache::loncommon::end_data_table_header_row().'
                   5172:        '.&Apache::loncommon::start_data_table_row().'
1.492     albertel 5173:               <td> '.&mt('Filename of scoring office file: [_1]',$file_selector).' 
1.489     albertel 5174:                 <br />
1.492     albertel 5175:                 <input type="submit" value="'.&mt('Download: Show List of Associated Files').'" />
1.489     albertel 5176:        '.&Apache::loncommon::end_data_table_row().'
                   5177:      '.&Apache::loncommon::end_data_table().'
                   5178:    </form>
                   5179:    <br />
                   5180: ');
1.162     albertel 5181: 
1.457     banghart 5182:     &Apache::lonpickcode::code_list($r,2);
1.523     raeburn  5183: 
1.528     raeburn  5184:     $r->print('<br /><form method="post" name="checkscantron">'.
1.523     raeburn  5185:              $default_form_data."\n".
                   5186:              &Apache::loncommon::start_data_table('LC_scantron_action')."\n".
                   5187:              &Apache::loncommon::start_data_table_header_row()."\n".
                   5188:              '<th colspan="2">
1.572     www      5189:               &nbsp;'.&mt('Review bubblesheet data and submissions for a previously graded folder/sequence')."\n".
1.523     raeburn  5190:              '</th>'."\n".
                   5191:               &Apache::loncommon::end_data_table_header_row()."\n".
                   5192:               &Apache::loncommon::start_data_table_row()."\n".
                   5193:               '<td> '.&mt('Graded folder/sequence:').' </td>'."\n".
                   5194:               '<td> '.$sequence_selector.' </td>'.
                   5195:               &Apache::loncommon::end_data_table_row()."\n".
                   5196:               &Apache::loncommon::start_data_table_row()."\n".
                   5197:               '<td> '.&mt('Filename of scoring office file:').' </td>'."\n".
                   5198:               '<td> '.$file_selector.' </td>'."\n".
                   5199:               &Apache::loncommon::end_data_table_row()."\n".
                   5200:               &Apache::loncommon::start_data_table_row()."\n".
                   5201:               '<td> '.&mt('Format of data file:').' </td>'."\n".
                   5202:               '<td> '.$format_selector.' </td>'."\n".
                   5203:               &Apache::loncommon::end_data_table_row()."\n".
                   5204:               &Apache::loncommon::start_data_table_row()."\n".
1.557     raeburn  5205:               '<td> '.&mt('Options').' </td>'."\n".
                   5206:               '<td> <label><input type="checkbox" name="scantron_options_hidden" value="ignore_hidden"/> '.&mt('Skip hidden resources').'</label></td>'.
                   5207:               &Apache::loncommon::end_data_table_row()."\n".
                   5208:               &Apache::loncommon::start_data_table_row()."\n".
1.523     raeburn  5209:               '<td colspan="2">'."\n".
                   5210:               '<input type="hidden" name="command" value="checksubmissions" />'."\n".
1.575     www      5211:               '<input type="submit" value="'.&mt('Review Bubblesheet Data and Submission Records').'" />'."\n".
1.523     raeburn  5212:               '</td>'."\n".
                   5213:               &Apache::loncommon::end_data_table_row()."\n".
                   5214:               &Apache::loncommon::end_data_table()."\n".
                   5215:               '</form><br />');
1.457     banghart 5216:     $r->print($grading_menu_button);
1.523     raeburn  5217:     return;
1.75      albertel 5218: }
                   5219: 
1.423     albertel 5220: =pod
                   5221: 
                   5222: =item get_scantron_config
                   5223: 
                   5224:    Parse and return the scantron configuration line selected as a
                   5225:    hash of configuration file fields.
                   5226: 
                   5227:  Arguments:
                   5228:     which - the name of the configuration to parse from the file.
                   5229: 
                   5230: 
                   5231:  Returns:
                   5232:             If the named configuration is not in the file, an empty
                   5233:             hash is returned.
                   5234:     a hash with the fields
                   5235:       name         - internal name for the this configuration setup
                   5236:       description  - text to display to operator that describes this config
                   5237:       CODElocation - if 0 or the string 'none'
                   5238:                           - no CODE exists for this config
                   5239:                      if -1 || the string 'letter'
                   5240:                           - a CODE exists for this config and is
                   5241:                             a string of letters
                   5242:                      Unsupported value (but planned for future support)
                   5243:                           if a positive integer
                   5244:                                - The CODE exists as the first n items from
                   5245:                                  the question section of the form
                   5246:                           if the string 'number'
                   5247:                                - The CODE exists for this config and is
                   5248:                                  a string of numbers
                   5249:       CODEstart   - (only matter if a CODE exists) column in the line where
                   5250:                      the CODE starts
                   5251:       CODElength  - length of the CODE
1.573     bisitz   5252:       IDstart     - column where the student/employee ID starts
1.556     weissno  5253:       IDlength    - length of the student/employee ID info
1.423     albertel 5254:       Qstart      - column where the information from the bubbled
                   5255:                     'questions' start
                   5256:       Qlength     - number of columns comprising a single bubble line from
                   5257:                     the sheet. (usually either 1 or 10)
1.424     albertel 5258:       Qon         - either a single character representing the character used
1.423     albertel 5259:                     to signal a bubble was chosen in the positional setup, or
                   5260:                     the string 'letter' if the letter of the chosen bubble is
                   5261:                     in the final, or 'number' if a number representing the
                   5262:                     chosen bubble is in the file (1->A 0->J)
1.424     albertel 5263:       Qoff        - the character used to represent that a bubble was
                   5264:                     left blank
1.423     albertel 5265:       PaperID     - if the scanning process generates a unique number for each
                   5266:                     sheet scanned the column that this ID number starts in
                   5267:       PaperIDlength - number of columns that comprise the unique ID number
                   5268:                       for the sheet of paper
1.424     albertel 5269:       FirstName   - column that the first name starts in
1.423     albertel 5270:       FirstNameLength - number of columns that the first name spans
                   5271:  
                   5272:       LastName    - column that the last name starts in
                   5273:       LastNameLength - number of columns that the last name spans
                   5274: 
                   5275: =cut
1.422     foxr     5276: 
1.82      albertel 5277: sub get_scantron_config {
                   5278:     my ($which) = @_;
1.518     raeburn  5279:     my @lines = &get_scantronformat_file();
1.82      albertel 5280:     my %config;
1.157     albertel 5281:     #FIXME probably should move to XML it has already gotten a bit much now
1.518     raeburn  5282:     foreach my $line (@lines) {
1.82      albertel 5283: 	my ($name,$descrip)=split(/:/,$line);
                   5284: 	if ($name ne $which ) { next; }
                   5285: 	chomp($line);
                   5286: 	my @config=split(/:/,$line);
                   5287: 	$config{'name'}=$config[0];
                   5288: 	$config{'description'}=$config[1];
                   5289: 	$config{'CODElocation'}=$config[2];
                   5290: 	$config{'CODEstart'}=$config[3];
                   5291: 	$config{'CODElength'}=$config[4];
                   5292: 	$config{'IDstart'}=$config[5];
                   5293: 	$config{'IDlength'}=$config[6];
                   5294: 	$config{'Qstart'}=$config[7];
1.497     foxr     5295:  	$config{'Qlength'}=$config[8];
1.82      albertel 5296: 	$config{'Qoff'}=$config[9];
                   5297: 	$config{'Qon'}=$config[10];
1.157     albertel 5298: 	$config{'PaperID'}=$config[11];
                   5299: 	$config{'PaperIDlength'}=$config[12];
                   5300: 	$config{'FirstName'}=$config[13];
                   5301: 	$config{'FirstNamelength'}=$config[14];
                   5302: 	$config{'LastName'}=$config[15];
                   5303: 	$config{'LastNamelength'}=$config[16];
1.82      albertel 5304: 	last;
                   5305:     }
                   5306:     return %config;
                   5307: }
                   5308: 
1.423     albertel 5309: =pod 
                   5310: 
                   5311: =item username_to_idmap
                   5312: 
1.556     weissno  5313:     creates a hash keyed by student/employee ID with values of the corresponding
1.423     albertel 5314:     student username:domain.
                   5315: 
                   5316:   Arguments:
                   5317: 
                   5318:     $classlist - reference to the class list hash. This is a hash
                   5319:                  keyed by student name:domain  whose elements are references
1.424     albertel 5320:                  to arrays containing various chunks of information
1.423     albertel 5321:                  about the student. (See loncoursedata for more info).
                   5322: 
                   5323:   Returns
                   5324:     %idmap - the constructed hash
                   5325: 
                   5326: =cut
                   5327: 
1.82      albertel 5328: sub username_to_idmap {
                   5329:     my ($classlist)= @_;
                   5330:     my %idmap;
                   5331:     foreach my $student (keys(%$classlist)) {
                   5332: 	$idmap{$classlist->{$student}->[&Apache::loncoursedata::CL_ID]}=
                   5333: 	    $student;
                   5334:     }
                   5335:     return %idmap;
                   5336: }
1.423     albertel 5337: 
                   5338: =pod
                   5339: 
1.424     albertel 5340: =item scantron_fixup_scanline
1.423     albertel 5341: 
                   5342:    Process a requested correction to a scanline.
                   5343: 
                   5344:   Arguments:
                   5345:     $scantron_config   - hash from &get_scantron_config()
                   5346:     $scan_data         - hash of correction information 
                   5347:                           (see &scantron_getfile())
                   5348:     $line              - existing scanline
                   5349:     $whichline         - line number of the passed in scanline
                   5350:     $field             - type of change to process 
                   5351:                          (either 
1.573     bisitz   5352:                           'ID'     -> correct the student/employee ID
1.423     albertel 5353:                           'CODE'   -> correct the CODE
                   5354:                           'answer' -> fixup the submitted answers)
                   5355:     
                   5356:    $args               - hash of additional info,
                   5357:                           - 'ID' 
                   5358:                                'newid' -> studentID to use in replacement
1.424     albertel 5359:                                           of existing one
1.423     albertel 5360:                           - 'CODE' 
                   5361:                                'CODE_ignore_dup' - set to true if duplicates
                   5362:                                                    should be ignored.
                   5363: 	                       'CODE' - is new code or 'use_unfound'
1.424     albertel 5364:                                         if the existing unfound code should
1.423     albertel 5365:                                         be used as is
                   5366:                           - 'answer'
                   5367:                                'response' - new answer or 'none' if blank
                   5368:                                'question' - the bubble line to change
1.503     raeburn  5369:                                'questionnum' - the question identifier,
                   5370:                                                may include subquestion. 
1.423     albertel 5371: 
                   5372:   Returns:
                   5373:     $line - the modified scanline
                   5374: 
                   5375:   Side effects: 
                   5376:     $scan_data - may be updated
                   5377: 
                   5378: =cut
                   5379: 
1.82      albertel 5380: 
1.157     albertel 5381: sub scantron_fixup_scanline {
                   5382:     my ($scantron_config,$scan_data,$line,$whichline,$field,$args)=@_;
                   5383:     if ($field eq 'ID') {
                   5384: 	if (length($args->{'newid'}) > $$scantron_config{'IDlength'}) {
1.186     albertel 5385: 	    return ($line,1,'New value too large');
1.157     albertel 5386: 	}
                   5387: 	if (length($args->{'newid'}) < $$scantron_config{'IDlength'}) {
                   5388: 	    $args->{'newid'}=sprintf('%-'.$$scantron_config{'IDlength'}.'s',
                   5389: 				     $args->{'newid'});
                   5390: 	}
                   5391: 	substr($line,$$scantron_config{'IDstart'}-1,
                   5392: 	       $$scantron_config{'IDlength'})=$args->{'newid'};
                   5393: 	if ($args->{'newid'}=~/^\s*$/) {
                   5394: 	    &scan_data($scan_data,"$whichline.user",
                   5395: 		       $args->{'username'}.':'.$args->{'domain'});
                   5396: 	}
1.186     albertel 5397:     } elsif ($field eq 'CODE') {
1.192     albertel 5398: 	if ($args->{'CODE_ignore_dup'}) {
                   5399: 	    &scan_data($scan_data,"$whichline.CODE_ignore_dup",'1');
                   5400: 	}
                   5401: 	&scan_data($scan_data,"$whichline.useCODE",'1');
                   5402: 	if ($args->{'CODE'} ne 'use_unfound') {
1.191     albertel 5403: 	    if (length($args->{'CODE'}) > $$scantron_config{'CODElength'}) {
                   5404: 		return ($line,1,'New CODE value too large');
                   5405: 	    }
                   5406: 	    if (length($args->{'CODE'}) < $$scantron_config{'CODElength'}) {
                   5407: 		$args->{'CODE'}=sprintf('%-'.$$scantron_config{'CODElength'}.'s',$args->{'CODE'});
                   5408: 	    }
                   5409: 	    substr($line,$$scantron_config{'CODEstart'}-1,
                   5410: 		   $$scantron_config{'CODElength'})=$args->{'CODE'};
1.186     albertel 5411: 	}
1.157     albertel 5412:     } elsif ($field eq 'answer') {
1.497     foxr     5413: 	my $length=$scantron_config->{'Qlength'};
1.157     albertel 5414: 	my $off=$scantron_config->{'Qoff'};
                   5415: 	my $on=$scantron_config->{'Qon'};
1.497     foxr     5416: 	my $answer=${off}x$length;
                   5417: 	if ($args->{'response'} eq 'none') {
                   5418: 	    &scan_data($scan_data,
1.503     raeburn  5419: 		       "$whichline.no_bubble.".$args->{'questionnum'},'1');
1.497     foxr     5420: 	} else {
                   5421: 	    if ($on eq 'letter') {
                   5422: 		my @alphabet=('A'..'Z');
                   5423: 		$answer=$alphabet[$args->{'response'}];
                   5424: 	    } elsif ($on eq 'number') {
                   5425: 		$answer=$args->{'response'}+1;
                   5426: 		if ($answer == 10) { $answer = '0'; }
1.274     albertel 5427: 	    } else {
1.497     foxr     5428: 		substr($answer,$args->{'response'},1)=$on;
1.274     albertel 5429: 	    }
1.497     foxr     5430: 	    &scan_data($scan_data,
1.503     raeburn  5431: 		       "$whichline.no_bubble.".$args->{'questionnum'},undef,'1');
1.157     albertel 5432: 	}
1.497     foxr     5433: 	my $where=$length*($args->{'question'}-1)+$scantron_config->{'Qstart'};
                   5434: 	substr($line,$where-1,$length)=$answer;
1.157     albertel 5435:     }
                   5436:     return $line;
                   5437: }
1.423     albertel 5438: 
                   5439: =pod
                   5440: 
                   5441: =item scan_data
                   5442: 
                   5443:     Edit or look up  an item in the scan_data hash.
                   5444: 
                   5445:   Arguments:
                   5446:     $scan_data  - The hash (see scantron_getfile)
                   5447:     $key        - shorthand of the key to edit (actual key is
1.424     albertel 5448:                   scantronfilename_key).
1.423     albertel 5449:     $data        - New value of the hash entry.
                   5450:     $delete      - If true, the entry is removed from the hash.
                   5451: 
                   5452:   Returns:
                   5453:     The new value of the hash table field (undefined if deleted).
                   5454: 
                   5455: =cut
                   5456: 
                   5457: 
1.157     albertel 5458: sub scan_data {
                   5459:     my ($scan_data,$key,$value,$delete)=@_;
1.257     albertel 5460:     my $filename=$env{'form.scantron_selectfile'};
1.157     albertel 5461:     if (defined($value)) {
                   5462: 	$scan_data->{$filename.'_'.$key} = $value;
                   5463:     }
                   5464:     if ($delete) { delete($scan_data->{$filename.'_'.$key}); }
                   5465:     return $scan_data->{$filename.'_'.$key};
                   5466: }
1.423     albertel 5467: 
1.495     albertel 5468: # ----- These first few routines are general use routines.----
                   5469: 
                   5470: # Return the number of occurences of a pattern in a string.
                   5471: 
                   5472: sub occurence_count {
                   5473:     my ($string, $pattern) = @_;
                   5474: 
                   5475:     my @matches = ($string =~ /$pattern/g);
                   5476: 
                   5477:     return scalar(@matches);
                   5478: }
                   5479: 
                   5480: 
                   5481: # Take a string known to have digits and convert all the
                   5482: # digits into letters in the range J,A..I.
                   5483: 
                   5484: sub digits_to_letters {
                   5485:     my ($input) = @_;
                   5486: 
                   5487:     my @alphabet = ('J', 'A'..'I');
                   5488: 
                   5489:     my @input    = split(//, $input);
                   5490:     my $output ='';
                   5491:     for (my $i = 0; $i < scalar(@input); $i++) {
                   5492: 	if ($input[$i] =~ /\d/) {
                   5493: 	    $output .= $alphabet[$input[$i]];
                   5494: 	} else {
                   5495: 	    $output .= $input[$i];
                   5496: 	}
                   5497:     }
                   5498:     return $output;
                   5499: }
                   5500: 
1.423     albertel 5501: =pod 
                   5502: 
                   5503: =item scantron_parse_scanline
                   5504: 
                   5505:   Decodes a scanline from the selected scantron file
                   5506: 
                   5507:  Arguments:
                   5508:     line             - The text of the scantron file line to process
                   5509:     whichline        - Line number
                   5510:     scantron_config  - Hash describing the format of the scantron lines.
                   5511:     scan_data        - Hash of extra information about the scanline
                   5512:                        (see scantron_getfile for more information)
                   5513:     just_header      - True if should not process question answers but only
                   5514:                        the stuff to the left of the answers.
                   5515:  Returns:
                   5516:    Hash containing the result of parsing the scanline
                   5517: 
                   5518:    Keys are all proceeded by the string 'scantron.'
                   5519: 
                   5520:        CODE    - the CODE in use for this scanline
                   5521:        useCODE - 1 if the CODE is invalid but it usage has been forced
                   5522:                  by the operator
                   5523:        CODE_ignore_dup - 1 if the CODE is a duplicated use when unique
                   5524:                             CODEs were selected, but the usage has been
                   5525:                             forced by the operator
1.556     weissno  5526:        ID  - student/employee ID
1.423     albertel 5527:        PaperID - if used, the ID number printed on the sheet when the 
                   5528:                  paper was scanned
                   5529:        FirstName - first name from the sheet
                   5530:        LastName  - last name from the sheet
                   5531: 
                   5532:      if just_header was not true these key may also exist
                   5533: 
1.447     foxr     5534:        missingerror - a list of bubble ranges that are considered to be answers
                   5535:                       to a single question that don't have any bubbles filled in.
                   5536:                       Of the form questionnumber:firstbubblenumber:count.
                   5537:        doubleerror  - a list of bubble ranges that are considered to be answers
                   5538:                       to a single question that have more than one bubble filled in.
                   5539:                       Of the form questionnumber::firstbubblenumber:count
                   5540:    
                   5541:                 In the above, count is the number of bubble responses in the
                   5542:                 input line needed to represent the possible answers to the question.
                   5543:                 e.g. a radioresponse with 15 choices in an answer sheet with 10 choices
                   5544:                 per line would have count = 2.
                   5545: 
1.423     albertel 5546:        maxquest     - the number of the last bubble line that was parsed
                   5547: 
                   5548:        (<number> starts at 1)
                   5549:        <number>.answer - zero or more letters representing the selected
                   5550:                          letters from the scanline for the bubble line 
                   5551:                          <number>.
                   5552:                          if blank there was either no bubble or there where
                   5553:                          multiple bubbles, (consult the keys missingerror and
                   5554:                          doubleerror if this is an error condition)
                   5555: 
                   5556: =cut
                   5557: 
1.82      albertel 5558: sub scantron_parse_scanline {
1.423     albertel 5559:     my ($line,$whichline,$scantron_config,$scan_data,$just_header)=@_;
1.470     foxr     5560: 
1.82      albertel 5561:     my %record;
1.550     raeburn  5562:     my $lastpos = $env{'form.scantron_maxbubble'}*$$scantron_config{'Qlength'};
                   5563:     my $questions=substr($line,$$scantron_config{'Qstart'}-1,$lastpos);  # Answers
1.422     foxr     5564:     my $data=substr($line,0,$$scantron_config{'Qstart'}-1);     # earlier stuff
1.278     albertel 5565:     if (!($$scantron_config{'CODElocation'} eq 0 ||
                   5566: 	  $$scantron_config{'CODElocation'} eq 'none')) {
                   5567: 	if ($$scantron_config{'CODElocation'} < 0 ||
                   5568: 	    $$scantron_config{'CODElocation'} eq 'letter' ||
                   5569: 	    $$scantron_config{'CODElocation'} eq 'number') {
1.191     albertel 5570: 	    $record{'scantron.CODE'}=substr($data,
                   5571: 					    $$scantron_config{'CODEstart'}-1,
1.83      albertel 5572: 					    $$scantron_config{'CODElength'});
1.191     albertel 5573: 	    if (&scan_data($scan_data,"$whichline.useCODE")) {
                   5574: 		$record{'scantron.useCODE'}=1;
                   5575: 	    }
1.192     albertel 5576: 	    if (&scan_data($scan_data,"$whichline.CODE_ignore_dup")) {
                   5577: 		$record{'scantron.CODE_ignore_dup'}=1;
                   5578: 	    }
1.82      albertel 5579: 	} else {
                   5580: 	    #FIXME interpret first N questions
                   5581: 	}
                   5582:     }
1.83      albertel 5583:     $record{'scantron.ID'}=substr($data,$$scantron_config{'IDstart'}-1,
                   5584: 				  $$scantron_config{'IDlength'});
1.157     albertel 5585:     $record{'scantron.PaperID'}=
                   5586: 	substr($data,$$scantron_config{'PaperID'}-1,
                   5587: 	       $$scantron_config{'PaperIDlength'});
                   5588:     $record{'scantron.FirstName'}=
                   5589: 	substr($data,$$scantron_config{'FirstName'}-1,
                   5590: 	       $$scantron_config{'FirstNamelength'});
                   5591:     $record{'scantron.LastName'}=
                   5592: 	substr($data,$$scantron_config{'LastName'}-1,
                   5593: 	       $$scantron_config{'LastNamelength'});
1.423     albertel 5594:     if ($just_header) { return \%record; }
1.194     albertel 5595: 
1.82      albertel 5596:     my @alphabet=('A'..'Z');
                   5597:     my $questnum=0;
1.447     foxr     5598:     my $ansnum  =1;		# Multiple 'answer lines'/question.
                   5599: 
1.470     foxr     5600:     chomp($questions);		# Get rid of any trailing \n.
                   5601:     $questions =~ s/\r$//;      # Get rid of trailing \r too (MAC or Win uploads).
                   5602:     while (length($questions)) {
1.447     foxr     5603: 	my $answers_needed = $bubble_lines_per_response{$questnum};
1.503     raeburn  5604:         my $answer_length  = ($$scantron_config{'Qlength'} * $answers_needed)
                   5605:                              || 1;
                   5606:         $questnum++;
                   5607:         my $quest_id = $questnum;
                   5608:         my $currentquest = substr($questions,0,$answer_length);
                   5609:         $questions       = substr($questions,$answer_length);
                   5610:         if (length($currentquest) < $answer_length) { next; }
                   5611: 
                   5612:         if ($subdivided_bubble_lines{$questnum-1} =~ /,/) {
                   5613:             my $subquestnum = 1;
                   5614:             my $subquestions = $currentquest;
                   5615:             my @subanswers_needed = 
                   5616:                 split(/,/,$subdivided_bubble_lines{$questnum-1});  
                   5617:             foreach my $subans (@subanswers_needed) {
                   5618:                 my $subans_length =
                   5619:                     ($$scantron_config{'Qlength'} * $subans)  || 1;
                   5620:                 my $currsubquest = substr($subquestions,0,$subans_length);
                   5621:                 $subquestions   = substr($subquestions,$subans_length);
                   5622:                 $quest_id = "$questnum.$subquestnum";
                   5623:                 if (($$scantron_config{'Qon'} eq 'letter') ||
                   5624:                     ($$scantron_config{'Qon'} eq 'number')) {
                   5625:                     $ansnum = &scantron_validator_lettnum($ansnum, 
                   5626:                         $questnum,$quest_id,$subans,$currsubquest,$whichline,
                   5627:                         \@alphabet,\%record,$scantron_config,$scan_data);
                   5628:                 } else {
                   5629:                     $ansnum = &scantron_validator_positional($ansnum,
                   5630:                         $questnum,$quest_id,$subans,$currsubquest,$whichline,                        \@alphabet,\%record,$scantron_config,$scan_data);
                   5631:                 }
                   5632:                 $subquestnum ++;
                   5633:             }
                   5634:         } else {
                   5635:             if (($$scantron_config{'Qon'} eq 'letter') ||
                   5636:                 ($$scantron_config{'Qon'} eq 'number')) {
                   5637:                 $ansnum = &scantron_validator_lettnum($ansnum,$questnum,
                   5638:                     $quest_id,$answers_needed,$currentquest,$whichline,
                   5639:                     \@alphabet,\%record,$scantron_config,$scan_data);
                   5640:             } else {
                   5641:                 $ansnum = &scantron_validator_positional($ansnum,$questnum,
                   5642:                     $quest_id,$answers_needed,$currentquest,$whichline,
                   5643:                     \@alphabet,\%record,$scantron_config,$scan_data);
                   5644:             }
                   5645:         }
                   5646:     }
                   5647:     $record{'scantron.maxquest'}=$questnum;
                   5648:     return \%record;
                   5649: }
1.447     foxr     5650: 
1.503     raeburn  5651: sub scantron_validator_lettnum {
                   5652:     my ($ansnum,$questnum,$quest_id,$answers_needed,$currquest,$whichline,
                   5653:         $alphabet,$record,$scantron_config,$scan_data) = @_;
                   5654: 
                   5655:     # Qon 'letter' implies for each slot in currquest we have:
                   5656:     #    ? or * for doubles, a letter in A-Z for a bubble, and
                   5657:     #    about anything else (esp. a value of Qoff) for missing
                   5658:     #    bubbles.
                   5659:     #
                   5660:     # Qon 'number' implies each slot gives a digit that indexes the
                   5661:     #    bubbles filled, or Qoff, or a non-number for unbubbled lines,
                   5662:     #    and * or ? for double bubbles on a single line.
                   5663:     #
1.447     foxr     5664: 
1.503     raeburn  5665:     my $matchon;
                   5666:     if ($$scantron_config{'Qon'} eq 'letter') {
                   5667:         $matchon = '[A-Z]';
                   5668:     } elsif ($$scantron_config{'Qon'} eq 'number') {
                   5669:         $matchon = '\d';
                   5670:     }
                   5671:     my $occurrences = 0;
                   5672:     if (($responsetype_per_response{$questnum-1} eq 'essayresponse') ||
                   5673:         ($responsetype_per_response{$questnum-1} eq 'formularesponse') ||
1.510     raeburn  5674:         ($responsetype_per_response{$questnum-1} eq 'stringresponse') ||
                   5675:         ($responsetype_per_response{$questnum-1} eq 'imageresponse') ||
                   5676:         ($responsetype_per_response{$questnum-1} eq 'reactionresponse') ||
                   5677:         ($responsetype_per_response{$questnum-1} eq 'organicresponse')) {
1.503     raeburn  5678:         my @singlelines = split('',$currquest);
                   5679:         foreach my $entry (@singlelines) {
                   5680:             $occurrences = &occurence_count($entry,$matchon);
                   5681:             if ($occurrences > 1) {
                   5682:                 last;
                   5683:             }
                   5684:         } 
                   5685:     } else {
                   5686:         $occurrences = &occurence_count($currquest,$matchon); 
                   5687:     }
                   5688:     if (($currquest =~ /\?/ || $currquest =~ /\*/) || ($occurrences > 1)) {
                   5689:         push(@{$record->{'scantron.doubleerror'}},$quest_id);
                   5690:         for (my $ans=0; $ans<$answers_needed; $ans++) {
                   5691:             my $bubble = substr($currquest,$ans,1);
                   5692:             if ($bubble =~ /$matchon/ ) {
                   5693:                 if ($$scantron_config{'Qon'} eq 'number') {
                   5694:                     if ($bubble == 0) {
                   5695:                         $bubble = 10; 
                   5696:                     }
                   5697:                     $record->{"scantron.$ansnum.answer"} = 
                   5698:                         $alphabet->[$bubble-1];
                   5699:                 } else {
                   5700:                     $record->{"scantron.$ansnum.answer"} = $bubble;
                   5701:                 }
                   5702:             } else {
                   5703:                 $record->{"scantron.$ansnum.answer"}='';
                   5704:             }
                   5705:             $ansnum++;
                   5706:         }
                   5707:     } elsif (!defined($currquest)
                   5708:             || (&occurence_count($currquest, $$scantron_config{'Qoff'}) == length($currquest))
                   5709:             || (&occurence_count($currquest,$matchon) == 0)) {
                   5710:         for (my $ans=0; $ans<$answers_needed; $ans++ ) {
                   5711:             $record->{"scantron.$ansnum.answer"}='';
                   5712:             $ansnum++;
                   5713:         }
                   5714:         if (!&scan_data($scan_data,"$whichline.no_bubble.$quest_id")) {
                   5715:             push(@{$record->{'scantron.missingerror'}},$quest_id);
                   5716:         }
                   5717:     } else {
                   5718:         if ($$scantron_config{'Qon'} eq 'number') {
                   5719:             $currquest = &digits_to_letters($currquest);            
                   5720:         }
                   5721:         for (my $ans=0; $ans<$answers_needed; $ans++) {
                   5722:             my $bubble = substr($currquest,$ans,1);
                   5723:             $record->{"scantron.$ansnum.answer"} = $bubble;
                   5724:             $ansnum++;
                   5725:         }
                   5726:     }
                   5727:     return $ansnum;
                   5728: }
1.447     foxr     5729: 
1.503     raeburn  5730: sub scantron_validator_positional {
                   5731:     my ($ansnum,$questnum,$quest_id,$answers_needed,$currquest,
                   5732:         $whichline,$alphabet,$record,$scantron_config,$scan_data) = @_;
1.447     foxr     5733: 
1.503     raeburn  5734:     # Otherwise there's a positional notation;
                   5735:     # each bubble line requires Qlength items, and there are filled in
                   5736:     # bubbles for each case where there 'Qon' characters.
                   5737:     #
1.447     foxr     5738: 
1.503     raeburn  5739:     my @array=split($$scantron_config{'Qon'},$currquest,-1);
1.447     foxr     5740: 
1.503     raeburn  5741:     # If the split only gives us one element.. the full length of the
                   5742:     # answer string, no bubbles are filled in:
1.447     foxr     5743: 
1.507     raeburn  5744:     if ($answers_needed eq '') {
                   5745:         return;
                   5746:     }
                   5747: 
1.503     raeburn  5748:     if (length($array[0]) eq $$scantron_config{'Qlength'}*$answers_needed) {
                   5749:         for (my $ans=0; $ans<$answers_needed; $ans++ ) {
                   5750:             $record->{"scantron.$ansnum.answer"}='';
                   5751:             $ansnum++;
                   5752:         }
                   5753:         if (!&scan_data($scan_data,"$whichline.no_bubble.$quest_id")) {
                   5754:             push(@{$record->{"scantron.missingerror"}},$quest_id);
                   5755:         }
                   5756:     } elsif (scalar(@array) == 2) {
                   5757:         my $location = length($array[0]);
                   5758:         my $line_num = int($location / $$scantron_config{'Qlength'});
                   5759:         my $bubble   = $alphabet->[$location % $$scantron_config{'Qlength'}];
                   5760:         for (my $ans=0; $ans<$answers_needed; $ans++) {
                   5761:             if ($ans eq $line_num) {
                   5762:                 $record->{"scantron.$ansnum.answer"} = $bubble;
                   5763:             } else {
                   5764:                 $record->{"scantron.$ansnum.answer"} = ' ';
                   5765:             }
                   5766:             $ansnum++;
                   5767:          }
                   5768:     } else {
                   5769:         #  If there's more than one instance of a bubble character
                   5770:         #  That's a double bubble; with positional notation we can
                   5771:         #  record all the bubbles filled in as well as the
                   5772:         #  fact this response consists of multiple bubbles.
                   5773:         #
                   5774:         if (($responsetype_per_response{$questnum-1} eq 'essayresponse') ||
                   5775:             ($responsetype_per_response{$questnum-1} eq 'formularesponse') ||
1.510     raeburn  5776:             ($responsetype_per_response{$questnum-1} eq 'stringresponse') ||
                   5777:             ($responsetype_per_response{$questnum-1} eq 'imageresponse') ||
                   5778:             ($responsetype_per_response{$questnum-1} eq 'reactionresponse') ||
                   5779:             ($responsetype_per_response{$questnum-1} eq 'organicresponse')) {
1.503     raeburn  5780:             my $doubleerror = 0;
                   5781:             while (($currquest >= $$scantron_config{'Qlength'}) && 
                   5782:                    (!$doubleerror)) {
                   5783:                my $currline = substr($currquest,0,$$scantron_config{'Qlength'});
                   5784:                $currquest = substr($currquest,$$scantron_config{'Qlength'});
                   5785:                my @currarray = split($$scantron_config{'Qon'},$currline,-1);
                   5786:                if (length(@currarray) > 2) {
                   5787:                    $doubleerror = 1;
                   5788:                } 
                   5789:             }
                   5790:             if ($doubleerror) {
                   5791:                 push(@{$record->{'scantron.doubleerror'}},$quest_id);
                   5792:             }
                   5793:         } else {
                   5794:             push(@{$record->{'scantron.doubleerror'}},$quest_id);
                   5795:         }
                   5796:         my $item = $ansnum;
                   5797:         for (my $ans=0; $ans<$answers_needed; $ans++) {
                   5798:             $record->{"scantron.$item.answer"} = '';
                   5799:             $item ++;
                   5800:         }
1.447     foxr     5801: 
1.503     raeburn  5802:         my @ans=@array;
                   5803:         my $i=0;
                   5804:         my $increment = 0;
                   5805:         while ($#ans) {
                   5806:             $i+=length($ans[0]) + $increment;
                   5807:             my $line   = int($i/$$scantron_config{'Qlength'} + $ansnum);
                   5808:             my $bubble = $i%$$scantron_config{'Qlength'};
                   5809:             $record->{"scantron.$line.answer"}.=$alphabet->[$bubble];
                   5810:             shift(@ans);
                   5811:             $increment = 1;
                   5812:         }
                   5813:         $ansnum += $answers_needed;
1.82      albertel 5814:     }
1.503     raeburn  5815:     return $ansnum;
1.82      albertel 5816: }
                   5817: 
1.423     albertel 5818: =pod
                   5819: 
                   5820: =item scantron_add_delay
                   5821: 
                   5822:    Adds an error message that occurred during the grading phase to a
                   5823:    queue of messages to be shown after grading pass is complete
                   5824: 
                   5825:  Arguments:
1.424     albertel 5826:    $delayqueue  - arrary ref of hash ref of error messages
1.423     albertel 5827:    $scanline    - the scanline that caused the error
                   5828:    $errormesage - the error message
                   5829:    $errorcode   - a numeric code for the error
                   5830: 
                   5831:  Side Effects:
1.424     albertel 5832:    updates the $delayqueue to have a new hash ref of the error
1.423     albertel 5833: 
                   5834: =cut
                   5835: 
1.82      albertel 5836: sub scantron_add_delay {
1.140     albertel 5837:     my ($delayqueue,$scanline,$errormessage,$errorcode)=@_;
                   5838:     push(@$delayqueue,
                   5839: 	 {'line' => $scanline, 'emsg' => $errormessage,
                   5840: 	  'ecode' => $errorcode }
                   5841: 	 );
1.82      albertel 5842: }
                   5843: 
1.423     albertel 5844: =pod
                   5845: 
                   5846: =item scantron_find_student
                   5847: 
1.424     albertel 5848:    Finds the username for the current scanline
                   5849: 
                   5850:   Arguments:
                   5851:    $scantron_record - hash result from scantron_parse_scanline
                   5852:    $scan_data       - hash of correction information 
                   5853:                       (see &scantron_getfile() form more information)
                   5854:    $idmap           - hash from &username_to_idmap()
                   5855:    $line            - number of current scanline
                   5856:  
                   5857:   Returns:
                   5858:    Either 'username:domain' or undef if unknown
                   5859: 
1.423     albertel 5860: =cut
                   5861: 
1.82      albertel 5862: sub scantron_find_student {
1.157     albertel 5863:     my ($scantron_record,$scan_data,$idmap,$line)=@_;
1.83      albertel 5864:     my $scanID=$$scantron_record{'scantron.ID'};
1.157     albertel 5865:     if ($scanID =~ /^\s*$/) {
                   5866:  	return &scan_data($scan_data,"$line.user");
                   5867:     }
1.83      albertel 5868:     foreach my $id (keys(%$idmap)) {
1.157     albertel 5869:  	if (lc($id) eq lc($scanID)) {
                   5870:  	    return $$idmap{$id};
                   5871:  	}
1.83      albertel 5872:     }
                   5873:     return undef;
                   5874: }
                   5875: 
1.423     albertel 5876: =pod
                   5877: 
                   5878: =item scantron_filter
                   5879: 
1.424     albertel 5880:    Filter sub for lonnavmaps, filters out hidden resources if ignore
                   5881:    hidden resources was selected
                   5882: 
1.423     albertel 5883: =cut
                   5884: 
1.83      albertel 5885: sub scantron_filter {
                   5886:     my ($curres)=@_;
1.331     albertel 5887: 
                   5888:     if (ref($curres) && $curres->is_problem()) {
                   5889: 	# if the user has asked to not have either hidden
                   5890: 	# or 'randomout' controlled resources to be graded
                   5891: 	# don't include them
                   5892: 	if ($env{'form.scantron_options_hidden'} eq 'ignore_hidden'
                   5893: 	    && $curres->randomout) {
                   5894: 	    return 0;
                   5895: 	}
1.83      albertel 5896: 	return 1;
                   5897:     }
                   5898:     return 0;
1.82      albertel 5899: }
                   5900: 
1.423     albertel 5901: =pod
                   5902: 
                   5903: =item scantron_process_corrections
                   5904: 
1.424     albertel 5905:    Gets correction information out of submitted form data and corrects
                   5906:    the scanline
                   5907: 
1.423     albertel 5908: =cut
                   5909: 
1.157     albertel 5910: sub scantron_process_corrections {
                   5911:     my ($r) = @_;
1.257     albertel 5912:     my %scantron_config=&get_scantron_config($env{'form.scantron_format'});
1.157     albertel 5913:     my ($scanlines,$scan_data)=&scantron_getfile();
                   5914:     my $classlist=&Apache::loncoursedata::get_classlist();
1.257     albertel 5915:     my $which=$env{'form.scantron_line'};
1.200     albertel 5916:     my $line=&scantron_get_line($scanlines,$scan_data,$which);
1.157     albertel 5917:     my ($skip,$err,$errmsg);
1.257     albertel 5918:     if ($env{'form.scantron_skip_record'}) {
1.157     albertel 5919: 	$skip=1;
1.257     albertel 5920:     } elsif ($env{'form.scantron_corrections'} =~ /^(duplicate|incorrect)ID$/) {
                   5921: 	my $newstudent=$env{'form.scantron_username'}.':'.
                   5922: 	    $env{'form.scantron_domain'};
1.157     albertel 5923: 	my $newid=$classlist->{$newstudent}->[&Apache::loncoursedata::CL_ID];
                   5924: 	($line,$err,$errmsg)=
                   5925: 	    &scantron_fixup_scanline(\%scantron_config,$scan_data,$line,$which,
                   5926: 				     'ID',{'newid'=>$newid,
1.257     albertel 5927: 				    'username'=>$env{'form.scantron_username'},
                   5928: 				    'domain'=>$env{'form.scantron_domain'}});
                   5929:     } elsif ($env{'form.scantron_corrections'} =~ /^(duplicate|incorrect)CODE$/) {
                   5930: 	my $resolution=$env{'form.scantron_CODE_resolution'};
1.190     albertel 5931: 	my $newCODE;
1.192     albertel 5932: 	my %args;
1.190     albertel 5933: 	if      ($resolution eq 'use_unfound') {
1.191     albertel 5934: 	    $newCODE='use_unfound';
1.190     albertel 5935: 	} elsif ($resolution eq 'use_found') {
1.257     albertel 5936: 	    $newCODE=$env{'form.scantron_CODE_selectedvalue'};
1.190     albertel 5937: 	} elsif ($resolution eq 'use_typed') {
1.257     albertel 5938: 	    $newCODE=$env{'form.scantron_CODE_newvalue'};
1.194     albertel 5939: 	} elsif ($resolution =~ /^use_closest_(\d+)/) {
1.257     albertel 5940: 	    $newCODE=$env{"form.scantron_CODE_closest_$1"};
1.190     albertel 5941: 	}
1.257     albertel 5942: 	if ($env{'form.scantron_corrections'} eq 'duplicateCODE') {
1.192     albertel 5943: 	    $args{'CODE_ignore_dup'}=1;
                   5944: 	}
                   5945: 	$args{'CODE'}=$newCODE;
1.186     albertel 5946: 	($line,$err,$errmsg)=
                   5947: 	    &scantron_fixup_scanline(\%scantron_config,$scan_data,$line,$which,
1.192     albertel 5948: 				     'CODE',\%args);
1.257     albertel 5949:     } elsif ($env{'form.scantron_corrections'} =~ /^(missing|double)bubble$/) {
                   5950: 	foreach my $question (split(',',$env{'form.scantron_questions'})) {
1.157     albertel 5951: 	    ($line,$err,$errmsg)=
                   5952: 		&scantron_fixup_scanline(\%scantron_config,$scan_data,$line,
                   5953: 					 $which,'answer',
                   5954: 					 { 'question'=>$question,
1.503     raeburn  5955: 		      		   'response'=>$env{"form.scantron_correct_Q_$question"},
                   5956:                                    'questionnum'=>$env{"form.scantron_questionnum_Q_$question"}});
1.157     albertel 5957: 	    if ($err) { last; }
                   5958: 	}
                   5959:     }
                   5960:     if ($err) {
1.398     albertel 5961: 	$r->print("<span class=\"LC_warning\">Unable to accept last correction, an error occurred :$errmsg:</span>");
1.157     albertel 5962:     } else {
1.200     albertel 5963: 	&scantron_put_line($scanlines,$scan_data,$which,$line,$skip);
1.157     albertel 5964: 	&scantron_putfile($scanlines,$scan_data);
                   5965:     }
                   5966: }
                   5967: 
1.423     albertel 5968: =pod
                   5969: 
                   5970: =item reset_skipping_status
                   5971: 
1.424     albertel 5972:    Forgets the current set of remember skipped scanlines (and thus
                   5973:    reverts back to considering all lines in the
                   5974:    scantron_skipped_<filename> file)
                   5975: 
1.423     albertel 5976: =cut
                   5977: 
1.200     albertel 5978: sub reset_skipping_status {
                   5979:     my ($scanlines,$scan_data)=&scantron_getfile();
                   5980:     &scan_data($scan_data,'remember_skipping',undef,1);
                   5981:     &scantron_putfile(undef,$scan_data);
                   5982: }
                   5983: 
1.423     albertel 5984: =pod
                   5985: 
                   5986: =item start_skipping
                   5987: 
1.424     albertel 5988:    Marks a scanline to be skipped. 
                   5989: 
1.423     albertel 5990: =cut
                   5991: 
1.376     albertel 5992: sub start_skipping {
1.200     albertel 5993:     my ($scan_data,$i)=@_;
                   5994:     my %remembered=split(':',&scan_data($scan_data,'remember_skipping'));
1.376     albertel 5995:     if ($env{'form.scantron_options_redo'} =~ /^redo_/) {
                   5996: 	$remembered{$i}=2;
                   5997:     } else {
                   5998: 	$remembered{$i}=1;
                   5999:     }
1.200     albertel 6000:     &scan_data($scan_data,'remember_skipping',join(':',%remembered));
                   6001: }
                   6002: 
1.423     albertel 6003: =pod
                   6004: 
                   6005: =item should_be_skipped
                   6006: 
1.424     albertel 6007:    Checks whether a scanline should be skipped.
                   6008: 
1.423     albertel 6009: =cut
                   6010: 
1.200     albertel 6011: sub should_be_skipped {
1.376     albertel 6012:     my ($scanlines,$scan_data,$i)=@_;
1.257     albertel 6013:     if ($env{'form.scantron_options_redo'} !~ /^redo_/) {
1.200     albertel 6014: 	# not redoing old skips
1.376     albertel 6015: 	if ($scanlines->{'skipped'}[$i]) { return 1; }
1.200     albertel 6016: 	return 0;
                   6017:     }
                   6018:     my %remembered=split(':',&scan_data($scan_data,'remember_skipping'));
1.376     albertel 6019: 
                   6020:     if (exists($remembered{$i}) && $remembered{$i} != 2 ) {
                   6021: 	return 0;
                   6022:     }
1.200     albertel 6023:     return 1;
                   6024: }
                   6025: 
1.423     albertel 6026: =pod
                   6027: 
                   6028: =item remember_current_skipped
                   6029: 
1.424     albertel 6030:    Discovers what scanlines are in the scantron_skipped_<filename>
                   6031:    file and remembers them into scan_data for later use.
                   6032: 
1.423     albertel 6033: =cut
                   6034: 
1.200     albertel 6035: sub remember_current_skipped {
                   6036:     my ($scanlines,$scan_data)=&scantron_getfile();
                   6037:     my %to_remember;
                   6038:     for (my $i=0;$i<=$scanlines->{'count'};$i++) {
                   6039: 	if ($scanlines->{'skipped'}[$i]) {
                   6040: 	    $to_remember{$i}=1;
                   6041: 	}
                   6042:     }
1.376     albertel 6043: 
1.200     albertel 6044:     &scan_data($scan_data,'remember_skipping',join(':',%to_remember));
                   6045:     &scantron_putfile(undef,$scan_data);
                   6046: }
                   6047: 
1.423     albertel 6048: =pod
                   6049: 
                   6050: =item check_for_error
                   6051: 
1.424     albertel 6052:     Checks if there was an error when attempting to remove a specific
                   6053:     scantron_.. bubble sheet data file. Prints out an error if
                   6054:     something went wrong.
                   6055: 
1.423     albertel 6056: =cut
                   6057: 
1.200     albertel 6058: sub check_for_error {
                   6059:     my ($r,$result)=@_;
                   6060:     if ($result ne 'ok' && $result ne 'not_found' ) {
1.492     albertel 6061: 	$r->print(&mt("An error occurred ([_1]) when trying to remove the existing corrections.",$result));
1.200     albertel 6062:     }
                   6063: }
1.157     albertel 6064: 
1.423     albertel 6065: =pod
                   6066: 
                   6067: =item scantron_warning_screen
                   6068: 
1.424     albertel 6069:    Interstitial screen to make sure the operator has selected the
                   6070:    correct options before we start the validation phase.
                   6071: 
1.423     albertel 6072: =cut
                   6073: 
1.203     albertel 6074: sub scantron_warning_screen {
                   6075:     my ($button_text)=@_;
1.257     albertel 6076:     my $title=&Apache::lonnet::gettitle($env{'form.selectpage'});
1.284     albertel 6077:     my %scantron_config=&get_scantron_config($env{'form.scantron_format'});
1.373     albertel 6078:     my $CODElist;
1.284     albertel 6079:     if ($scantron_config{'CODElocation'} &&
                   6080: 	$scantron_config{'CODEstart'} &&
                   6081: 	$scantron_config{'CODElength'}) {
                   6082: 	$CODElist=$env{'form.scantron_CODElist'};
1.398     albertel 6083: 	if ($env{'form.scantron_CODElist'} eq '') { $CODElist='<span class="LC_warning">None</span>'; }
1.284     albertel 6084: 	$CODElist=
1.492     albertel 6085: 	    '<tr><td><b>'.&mt('List of CODES to validate against:').'</b></td><td><tt>'.
1.373     albertel 6086: 	    $env{'form.scantron_CODElist'}.'</tt></td></tr>';
1.284     albertel 6087:     }
1.492     albertel 6088:     return ('
1.203     albertel 6089: <p>
1.492     albertel 6090: <span class="LC_warning">
                   6091: '.&mt('Please double check the information below before clicking on \'[_1]\'',&mt($button_text)).'</span>
1.203     albertel 6092: </p>
                   6093: <table>
1.492     albertel 6094: <tr><td><b>'.&mt('Sequence to be Graded:').'</b></td><td>'.$title.'</td></tr>
                   6095: <tr><td><b>'.&mt('Data File that will be used:').'</b></td><td><tt>'.$env{'form.scantron_selectfile'}.'</tt></td></tr>
                   6096: '.$CODElist.'
1.203     albertel 6097: </table>
                   6098: <br />
1.492     albertel 6099: <p> '.&mt('If this information is correct, please click on \'[_1]\'.',&mt($button_text)).'</p>
                   6100: <p> '.&mt('If something is incorrect, please click the \'Grading Menu\' button to start over.').'</p>
1.203     albertel 6101: 
                   6102: <br />
1.492     albertel 6103: ');
1.203     albertel 6104: }
                   6105: 
1.423     albertel 6106: =pod
                   6107: 
                   6108: =item scantron_do_warning
                   6109: 
1.424     albertel 6110:    Check if the operator has picked something for all required
                   6111:    fields. Error out if something is missing.
                   6112: 
1.423     albertel 6113: =cut
                   6114: 
1.203     albertel 6115: sub scantron_do_warning {
                   6116:     my ($r)=@_;
1.324     albertel 6117:     my ($symb)=&get_symb($r);
1.203     albertel 6118:     if (!$symb) {return '';}
1.324     albertel 6119:     my $default_form_data=&defaultFormData($symb);
1.203     albertel 6120:     $r->print(&scantron_form_start().$default_form_data);
1.257     albertel 6121:     if ( $env{'form.selectpage'} eq '' ||
                   6122: 	 $env{'form.scantron_selectfile'} eq '' ||
                   6123: 	 $env{'form.scantron_format'} eq '' ) {
1.492     albertel 6124: 	$r->print("<p>".&mt('You have forgetten to specify some information. Please go Back and try again.')."</p>");
1.257     albertel 6125: 	if ( $env{'form.selectpage'} eq '') {
1.492     albertel 6126: 	    $r->print('<p><span class="LC_error">'.&mt('You have not selected a Sequence to grade').'</span></p>');
1.237     albertel 6127: 	} 
1.257     albertel 6128: 	if ( $env{'form.scantron_selectfile'} eq '') {
1.492     albertel 6129: 	    $r->print('<p><span class="LC_error">'.&mt('You have not selected a file that contains the student\'s response data.').'</span></p>');
1.237     albertel 6130: 	} 
1.257     albertel 6131: 	if ( $env{'form.scantron_format'} eq '') {
1.492     albertel 6132: 	    $r->print('<p><span class="LC_error">'.&mt('You have not selected a the format of the student\'s response data.').'</span></p>');
1.237     albertel 6133: 	} 
                   6134:     } else {
1.265     www      6135: 	my $warning=&scantron_warning_screen('Grading: Validate Records');
1.492     albertel 6136: 	$r->print('
                   6137: '.$warning.'
                   6138: <input type="submit" name="submit" value="'.&mt('Grading: Validate Records').'" />
1.203     albertel 6139: <input type="hidden" name="command" value="scantron_validate" />
1.492     albertel 6140: ');
1.237     albertel 6141:     }
1.352     albertel 6142:     $r->print("</form><br />".&show_grading_menu_form($symb));
1.203     albertel 6143:     return '';
                   6144: }
                   6145: 
1.423     albertel 6146: =pod
                   6147: 
                   6148: =item scantron_form_start
                   6149: 
1.424     albertel 6150:     html hidden input for remembering all selected grading options
                   6151: 
1.423     albertel 6152: =cut
                   6153: 
1.203     albertel 6154: sub scantron_form_start {
                   6155:     my ($max_bubble)=@_;
                   6156:     my $result= <<SCANTRONFORM;
                   6157: <form method="post" enctype="multipart/form-data" action="/adm/grades" name="scantronupload">
1.257     albertel 6158:   <input type="hidden" name="selectpage" value="$env{'form.selectpage'}" />
                   6159:   <input type="hidden" name="scantron_format" value="$env{'form.scantron_format'}" />
                   6160:   <input type="hidden" name="scantron_selectfile" value="$env{'form.scantron_selectfile'}" />
1.218     albertel 6161:   <input type="hidden" name="scantron_maxbubble" value="$max_bubble" />
1.257     albertel 6162:   <input type="hidden" name="scantron_CODElist" value="$env{'form.scantron_CODElist'}" />
                   6163:   <input type="hidden" name="scantron_CODEunique" value="$env{'form.scantron_CODEunique'}" />
                   6164:   <input type="hidden" name="scantron_options_redo" value="$env{'form.scantron_options_redo'}" />
                   6165:   <input type="hidden" name="scantron_options_ignore" value="$env{'form.scantron_options_ignore'}" />
1.331     albertel 6166:   <input type="hidden" name="scantron_options_hidden" value="$env{'form.scantron_options_hidden'}" />
1.203     albertel 6167: SCANTRONFORM
1.447     foxr     6168: 
                   6169:   my $line = 0;
                   6170:     while (defined($env{"form.scantron.bubblelines.$line"})) {
                   6171:        my $chunk =
                   6172: 	   '<input type="hidden" name="scantron.bubblelines.'.$line.'" value="'.$env{"form.scantron.bubblelines.$line"}.'" />'."\n";
1.448     foxr     6173:        $chunk .=
                   6174: 	   '<input type="hidden" name="scantron.first_bubble_line.'.$line.'" value="'.$env{"form.scantron.first_bubble_line.$line"}.'" />'."\n";
1.503     raeburn  6175:        $chunk .= 
                   6176:            '<input type="hidden" name="scantron.sub_bubblelines.'.$line.'" value="'.$env{"form.scantron.sub_bubblelines.$line"}.'" />'."\n";
1.504     raeburn  6177:        $chunk .=
                   6178:            '<input type="hidden" name="scantron.responsetype.'.$line.'" value="'.$env{"form.scantron.responsetype.$line"}.'" />'."\n";
1.447     foxr     6179:        $result .= $chunk;
                   6180:        $line++;
                   6181:    }
1.203     albertel 6182:     return $result;
                   6183: }
                   6184: 
1.423     albertel 6185: =pod
                   6186: 
                   6187: =item scantron_validate_file
                   6188: 
1.424     albertel 6189:     Dispatch routine for doing validation of a bubble sheet data file.
                   6190: 
                   6191:     Also processes any necessary information resets that need to
                   6192:     occur before validation begins (ignore previous corrections,
                   6193:     restarting the skipped records processing)
                   6194: 
1.423     albertel 6195: =cut
                   6196: 
1.157     albertel 6197: sub scantron_validate_file {
                   6198:     my ($r) = @_;
1.324     albertel 6199:     my ($symb)=&get_symb($r);
1.157     albertel 6200:     if (!$symb) {return '';}
1.324     albertel 6201:     my $default_form_data=&defaultFormData($symb);
1.200     albertel 6202:     
                   6203:     # do the detection of only doing skipped records first befroe we delete
1.424     albertel 6204:     # them when doing the corrections reset
1.257     albertel 6205:     if ($env{'form.scantron_options_redo'} ne 'redo_skipped_ready') {
1.200     albertel 6206: 	&reset_skipping_status();
                   6207:     }
1.257     albertel 6208:     if ($env{'form.scantron_options_redo'} eq 'redo_skipped') {
1.200     albertel 6209: 	&remember_current_skipped();
1.257     albertel 6210: 	$env{'form.scantron_options_redo'}='redo_skipped_ready';
1.200     albertel 6211:     }
                   6212: 
1.257     albertel 6213:     if ($env{'form.scantron_options_ignore'} eq 'ignore_corrections') {
1.200     albertel 6214: 	&check_for_error($r,&scantron_remove_file('corrected'));
                   6215: 	&check_for_error($r,&scantron_remove_file('skipped'));
                   6216: 	&check_for_error($r,&scantron_remove_scan_data());
1.257     albertel 6217: 	$env{'form.scantron_options_ignore'}='done';
1.192     albertel 6218:     }
1.200     albertel 6219: 
1.257     albertel 6220:     if ($env{'form.scantron_corrections'}) {
1.157     albertel 6221: 	&scantron_process_corrections($r);
                   6222:     }
1.503     raeburn  6223:     $r->print('<p>'.&mt('Gathering necessary information.').'</p>');$r->rflush();
1.157     albertel 6224:     #get the student pick code ready
                   6225:     $r->print(&Apache::loncommon::studentbrowser_javascript());
1.330     albertel 6226:     my $max_bubble=&scantron_get_maxbubble();
1.203     albertel 6227:     my $result=&scantron_form_start($max_bubble).$default_form_data;
1.157     albertel 6228:     $r->print($result);
                   6229:     
1.334     albertel 6230:     my @validate_phases=( 'sequence',
                   6231: 			  'ID',
1.157     albertel 6232: 			  'CODE',
                   6233: 			  'doublebubble',
                   6234: 			  'missingbubbles');
1.257     albertel 6235:     if (!$env{'form.validatepass'}) {
                   6236: 	$env{'form.validatepass'} = 0;
1.157     albertel 6237:     }
1.257     albertel 6238:     my $currentphase=$env{'form.validatepass'};
1.157     albertel 6239: 
1.448     foxr     6240: 
1.157     albertel 6241:     my $stop=0;
                   6242:     while (!$stop && $currentphase < scalar(@validate_phases)) {
1.503     raeburn  6243: 	$r->print(&mt('Validating '.$validate_phases[$currentphase]).'<br />');
1.157     albertel 6244: 	$r->rflush();
                   6245: 	my $which="scantron_validate_".$validate_phases[$currentphase];
                   6246: 	{
                   6247: 	    no strict 'refs';
                   6248: 	    ($stop,$currentphase)=&$which($r,$currentphase);
                   6249: 	}
                   6250:     }
                   6251:     if (!$stop) {
1.203     albertel 6252: 	my $warning=&scantron_warning_screen('Start Grading');
1.542     raeburn  6253: 	$r->print(&mt('Validation process complete.').'<br />'.
                   6254:                   $warning.
                   6255:                   &mt('Perform verification for each student after storage of submissions?').
                   6256:                   '&nbsp;<span class="LC_nobreak"><label>'.
                   6257:                   '<input type="radio" name="verifyrecord" value="1" />'.&mt('Yes').'</label>'.
                   6258:                   ('&nbsp;'x3).'<label>'.
                   6259:                   '<input type="radio" name="verifyrecord" value="0" checked="checked" />'.&mt('No').
                   6260:                   '</label></span><br />'.
                   6261:                   &mt('Grading will take longer if you use verification.').'<br />'.
1.572     www      6262:                   &mt("Alternatively, the 'Review bubblesheet data' utility (see grading menu) can be used for all students after grading is complete.").'<br /><br />'.
1.542     raeburn  6263:                   '<input type="submit" name="submit" value="'.&mt('Start Grading').'" />'.
                   6264:                   '<input type="hidden" name="command" value="scantron_process" />'."\n");
1.157     albertel 6265:     } else {
                   6266: 	$r->print('<input type="hidden" name="command" value="scantron_validate" />');
                   6267: 	$r->print("<input type='hidden' name='validatepass' value='".$currentphase."' />");
                   6268:     }
                   6269:     if ($stop) {
1.334     albertel 6270: 	if ($validate_phases[$currentphase] eq 'sequence') {
1.539     riegler  6271: 	    $r->print('<input type="submit" name="submit" value="'.&mt('Ignore').' &rarr; " />');
1.492     albertel 6272: 	    $r->print(' '.&mt('this error').' <br />');
1.334     albertel 6273: 
1.492     albertel 6274: 	    $r->print(" <p>".&mt("Or click the 'Grading Menu' button to start over.")."</p>");
1.334     albertel 6275: 	} else {
1.503     raeburn  6276:             if ($validate_phases[$currentphase] eq 'doublebubble' || $validate_phases[$currentphase] eq 'missingbubbles') {
1.539     riegler  6277: 	        $r->print('<input type="button" name="submitbutton" value="'.&mt('Continue').' &rarr;" onclick="javascript:verify_bubble_radio(this.form)" />');
1.503     raeburn  6278:             } else {
1.539     riegler  6279:                 $r->print('<input type="submit" name="submit" value="'.&mt('Continue').' &rarr;" />');
1.503     raeburn  6280:             }
1.492     albertel 6281: 	    $r->print(' '.&mt('using corrected info').' <br />');
                   6282: 	    $r->print("<input type='submit' value='".&mt("Skip")."' name='scantron_skip_record' />");
                   6283: 	    $r->print(" ".&mt("this scanline saving it for later."));
1.334     albertel 6284: 	}
1.157     albertel 6285:     }
1.352     albertel 6286:     $r->print(" </form><br />".&show_grading_menu_form($symb));
1.157     albertel 6287:     return '';
                   6288: }
                   6289: 
1.423     albertel 6290: 
                   6291: =pod
                   6292: 
                   6293: =item scantron_remove_file
                   6294: 
1.424     albertel 6295:    Removes the requested bubble sheet data file, makes sure that
                   6296:    scantron_original_<filename> is never removed
                   6297: 
                   6298: 
1.423     albertel 6299: =cut
                   6300: 
1.200     albertel 6301: sub scantron_remove_file {
1.192     albertel 6302:     my ($which)=@_;
1.257     albertel 6303:     my $cname=$env{'course.'.$env{'request.course.id'}.'.num'};
                   6304:     my $cdom=$env{'course.'.$env{'request.course.id'}.'.domain'};
1.192     albertel 6305:     my $file='scantron_';
1.200     albertel 6306:     if ($which eq 'corrected' || $which eq 'skipped') {
                   6307: 	$file.=$which.'_';
1.192     albertel 6308:     } else {
                   6309: 	return 'refused';
                   6310:     }
1.257     albertel 6311:     $file.=$env{'form.scantron_selectfile'};
1.200     albertel 6312:     return &Apache::lonnet::removeuserfile($cname,$cdom,$file);
                   6313: }
                   6314: 
1.423     albertel 6315: 
                   6316: =pod
                   6317: 
                   6318: =item scantron_remove_scan_data
                   6319: 
1.424     albertel 6320:    Removes all scan_data correction for the requested bubble sheet
                   6321:    data file.  (In the case that both the are doing skipped records we need
                   6322:    to remember the old skipped lines for the time being so that element
                   6323:    persists for a while.)
                   6324: 
1.423     albertel 6325: =cut
                   6326: 
1.200     albertel 6327: sub scantron_remove_scan_data {
1.257     albertel 6328:     my $cname=$env{'course.'.$env{'request.course.id'}.'.num'};
                   6329:     my $cdom=$env{'course.'.$env{'request.course.id'}.'.domain'};
1.192     albertel 6330:     my @keys=&Apache::lonnet::getkeys('nohist_scantrondata',$cdom,$cname);
                   6331:     my @todelete;
1.257     albertel 6332:     my $filename=$env{'form.scantron_selectfile'};
1.192     albertel 6333:     foreach my $key (@keys) {
                   6334: 	if ($key=~/^\Q$filename\E_/) {
1.257     albertel 6335: 	    if ($env{'form.scantron_options_redo'} eq 'redo_skipped_ready' &&
1.200     albertel 6336: 		$key=~/remember_skipping/) {
                   6337: 		next;
                   6338: 	    }
1.192     albertel 6339: 	    push(@todelete,$key);
                   6340: 	}
                   6341:     }
1.200     albertel 6342:     my $result;
1.192     albertel 6343:     if (@todelete) {
1.491     albertel 6344: 	$result = &Apache::lonnet::del('nohist_scantrondata',
                   6345: 				       \@todelete,$cdom,$cname);
                   6346:     } else {
                   6347: 	$result = 'ok';
1.192     albertel 6348:     }
                   6349:     return $result;
                   6350: }
                   6351: 
1.423     albertel 6352: 
                   6353: =pod
                   6354: 
                   6355: =item scantron_getfile
                   6356: 
1.424     albertel 6357:     Fetches the requested bubble sheet data file (all 3 versions), and
                   6358:     the scan_data hash
                   6359:   
                   6360:   Arguments:
                   6361:     None
                   6362: 
                   6363:   Returns:
                   6364:     2 hash references
                   6365: 
                   6366:      - first one has 
                   6367:          orig      -
                   6368:          corrected -
                   6369:          skipped   -  each of which points to an array ref of the specified
                   6370:                       file broken up into individual lines
                   6371:          count     - number of scanlines
                   6372:  
                   6373:      - second is the scan_data hash possible keys are
1.425     albertel 6374:        ($number refers to scanline numbered $number and thus the key affects
                   6375:         only that scanline
                   6376:         $bubline refers to the specific bubble line element and the aspects
                   6377:         refers to that specific bubble line element)
                   6378: 
                   6379:        $number.user - username:domain to use
                   6380:        $number.CODE_ignore_dup 
                   6381:                     - ignore the duplicate CODE error 
                   6382:        $number.useCODE
                   6383:                     - use the CODE in the scanline as is
                   6384:        $number.no_bubble.$bubline
                   6385:                     - it is valid that there is no bubbled in bubble
                   6386:                       at $number $bubline
                   6387:        remember_skipping
                   6388:                     - a frozen hash containing keys of $number and values
                   6389:                       of either 
                   6390:                         1 - we are on a 'do skipped records pass' and plan
                   6391:                             on processing this line
                   6392:                         2 - we are on a 'do skipped records pass' and this
                   6393:                             scanline has been marked to skip yet again
1.424     albertel 6394: 
1.423     albertel 6395: =cut
                   6396: 
1.157     albertel 6397: sub scantron_getfile {
1.200     albertel 6398:     #FIXME really would prefer a scantron directory
1.257     albertel 6399:     my $cname=$env{'course.'.$env{'request.course.id'}.'.num'};
                   6400:     my $cdom=$env{'course.'.$env{'request.course.id'}.'.domain'};
1.157     albertel 6401:     my $lines;
                   6402:     $lines=&Apache::lonnet::getfile('/uploaded/'.$cdom.'/'.$cname.'/'.
1.257     albertel 6403: 		       'scantron_orig_'.$env{'form.scantron_selectfile'});
1.157     albertel 6404:     my %scanlines;
                   6405:     $scanlines{'orig'}=[(split("\n",$lines,-1))];
                   6406:     my $temp=$scanlines{'orig'};
                   6407:     $scanlines{'count'}=$#$temp;
                   6408: 
                   6409:     $lines=&Apache::lonnet::getfile('/uploaded/'.$cdom.'/'.$cname.'/'.
1.257     albertel 6410: 		       'scantron_corrected_'.$env{'form.scantron_selectfile'});
1.157     albertel 6411:     if ($lines eq '-1') {
                   6412: 	$scanlines{'corrected'}=[];
                   6413:     } else {
                   6414: 	$scanlines{'corrected'}=[(split("\n",$lines,-1))];
                   6415:     }
                   6416:     $lines=&Apache::lonnet::getfile('/uploaded/'.$cdom.'/'.$cname.'/'.
1.257     albertel 6417: 		       'scantron_skipped_'.$env{'form.scantron_selectfile'});
1.157     albertel 6418:     if ($lines eq '-1') {
                   6419: 	$scanlines{'skipped'}=[];
                   6420:     } else {
                   6421: 	$scanlines{'skipped'}=[(split("\n",$lines,-1))];
                   6422:     }
1.175     albertel 6423:     my @tmp=&Apache::lonnet::dump('nohist_scantrondata',$cdom,$cname);
1.157     albertel 6424:     if ($tmp[0] =~ /^(error:|no_such_host)/) { @tmp=(); }
                   6425:     my %scan_data = @tmp;
                   6426:     return (\%scanlines,\%scan_data);
                   6427: }
                   6428: 
1.423     albertel 6429: =pod
                   6430: 
                   6431: =item lonnet_putfile
                   6432: 
1.424     albertel 6433:    Wrapper routine to call &Apache::lonnet::finishuserfileupload
                   6434: 
                   6435:  Arguments:
                   6436:    $contents - data to store
                   6437:    $filename - filename to store $contents into
                   6438: 
                   6439:  Returns:
                   6440:    result value from &Apache::lonnet::finishuserfileupload
                   6441: 
1.423     albertel 6442: =cut
                   6443: 
1.157     albertel 6444: sub lonnet_putfile {
                   6445:     my ($contents,$filename)=@_;
1.257     albertel 6446:     my $docuname=$env{'course.'.$env{'request.course.id'}.'.num'};
                   6447:     my $docudom=$env{'course.'.$env{'request.course.id'}.'.domain'};
                   6448:     $env{'form.sillywaytopassafilearound'}=$contents;
1.275     albertel 6449:     &Apache::lonnet::finishuserfileupload($docuname,$docudom,'sillywaytopassafilearound',$filename);
1.157     albertel 6450: 
                   6451: }
                   6452: 
1.423     albertel 6453: =pod
                   6454: 
                   6455: =item scantron_putfile
                   6456: 
1.424     albertel 6457:     Stores the current version of the bubble sheet data files, and the
                   6458:     scan_data hash. (Does not modify the original version only the
                   6459:     corrected and skipped versions.
                   6460: 
                   6461:  Arguments:
                   6462:     $scanlines - hash ref that looks like the first return value from
                   6463:                  &scantron_getfile()
                   6464:     $scan_data - hash ref that looks like the second return value from
                   6465:                  &scantron_getfile()
                   6466: 
1.423     albertel 6467: =cut
                   6468: 
1.157     albertel 6469: sub scantron_putfile {
                   6470:     my ($scanlines,$scan_data) = @_;
1.200     albertel 6471:     #FIXME really would prefer a scantron directory
1.257     albertel 6472:     my $cname=$env{'course.'.$env{'request.course.id'}.'.num'};
                   6473:     my $cdom=$env{'course.'.$env{'request.course.id'}.'.domain'};
1.200     albertel 6474:     if ($scanlines) {
                   6475: 	my $prefix='scantron_';
1.157     albertel 6476: # no need to update orig, shouldn't change
                   6477: #   &lonnet_putfile(join("\n",@{$scanlines->{'orig'}}),$prefix.'orig_'.
1.257     albertel 6478: #		    $env{'form.scantron_selectfile'});
1.200     albertel 6479: 	&lonnet_putfile(join("\n",@{$scanlines->{'corrected'}}),
                   6480: 			$prefix.'corrected_'.
1.257     albertel 6481: 			$env{'form.scantron_selectfile'});
1.200     albertel 6482: 	&lonnet_putfile(join("\n",@{$scanlines->{'skipped'}}),
                   6483: 			$prefix.'skipped_'.
1.257     albertel 6484: 			$env{'form.scantron_selectfile'});
1.200     albertel 6485:     }
1.175     albertel 6486:     &Apache::lonnet::put('nohist_scantrondata',$scan_data,$cdom,$cname);
1.157     albertel 6487: }
                   6488: 
1.423     albertel 6489: =pod
                   6490: 
                   6491: =item scantron_get_line
                   6492: 
1.424     albertel 6493:    Returns the correct version of the scanline
                   6494: 
                   6495:  Arguments:
                   6496:     $scanlines - hash ref that looks like the first return value from
                   6497:                  &scantron_getfile()
                   6498:     $scan_data - hash ref that looks like the second return value from
                   6499:                  &scantron_getfile()
                   6500:     $i         - number of the requested line (starts at 0)
                   6501: 
                   6502:  Returns:
                   6503:    A scanline, (either the original or the corrected one if it
                   6504:    exists), or undef if the requested scanline should be
                   6505:    skipped. (Either because it's an skipped scanline, or it's an
                   6506:    unskipped scanline and we are not doing a 'do skipped scanlines'
                   6507:    pass.
                   6508: 
1.423     albertel 6509: =cut
                   6510: 
1.157     albertel 6511: sub scantron_get_line {
1.200     albertel 6512:     my ($scanlines,$scan_data,$i)=@_;
1.376     albertel 6513:     if (&should_be_skipped($scanlines,$scan_data,$i)) { return undef; }
                   6514:     #if ($scanlines->{'skipped'}[$i]) { return undef; }
1.157     albertel 6515:     if ($scanlines->{'corrected'}[$i]) {return $scanlines->{'corrected'}[$i];}
                   6516:     return $scanlines->{'orig'}[$i]; 
                   6517: }
                   6518: 
1.423     albertel 6519: =pod
                   6520: 
                   6521: =item scantron_todo_count
                   6522: 
1.424     albertel 6523:     Counts the number of scanlines that need processing.
                   6524: 
                   6525:  Arguments:
                   6526:     $scanlines - hash ref that looks like the first return value from
                   6527:                  &scantron_getfile()
                   6528:     $scan_data - hash ref that looks like the second return value from
                   6529:                  &scantron_getfile()
                   6530: 
                   6531:  Returns:
                   6532:     $count - number of scanlines to process
                   6533: 
1.423     albertel 6534: =cut
                   6535: 
1.200     albertel 6536: sub get_todo_count {
                   6537:     my ($scanlines,$scan_data)=@_;
                   6538:     my $count=0;
                   6539:     for (my $i=0;$i<=$scanlines->{'count'};$i++) {
                   6540: 	my $line=&scantron_get_line($scanlines,$scan_data,$i);
                   6541: 	if ($line=~/^[\s\cz]*$/) { next; }
                   6542: 	$count++;
                   6543:     }
                   6544:     return $count;
                   6545: }
                   6546: 
1.423     albertel 6547: =pod
                   6548: 
                   6549: =item scantron_put_line
                   6550: 
1.424     albertel 6551:     Updates the 'corrected' or 'skipped' versions of the bubble sheet
                   6552:     data file.
                   6553: 
                   6554:  Arguments:
                   6555:     $scanlines - hash ref that looks like the first return value from
                   6556:                  &scantron_getfile()
                   6557:     $scan_data - hash ref that looks like the second return value from
                   6558:                  &scantron_getfile()
                   6559:     $i         - line number to update
                   6560:     $newline   - contents of the updated scanline
                   6561:     $skip      - if true make the line for skipping and update the
                   6562:                  'skipped' file
                   6563: 
1.423     albertel 6564: =cut
                   6565: 
1.157     albertel 6566: sub scantron_put_line {
1.200     albertel 6567:     my ($scanlines,$scan_data,$i,$newline,$skip)=@_;
1.157     albertel 6568:     if ($skip) {
                   6569: 	$scanlines->{'skipped'}[$i]=$newline;
1.376     albertel 6570: 	&start_skipping($scan_data,$i);
1.157     albertel 6571: 	return;
                   6572:     }
                   6573:     $scanlines->{'corrected'}[$i]=$newline;
                   6574: }
                   6575: 
1.423     albertel 6576: =pod
                   6577: 
                   6578: =item scantron_clear_skip
                   6579: 
1.424     albertel 6580:    Remove a line from the 'skipped' file
                   6581: 
                   6582:  Arguments:
                   6583:     $scanlines - hash ref that looks like the first return value from
                   6584:                  &scantron_getfile()
                   6585:     $scan_data - hash ref that looks like the second return value from
                   6586:                  &scantron_getfile()
                   6587:     $i         - line number to update
                   6588: 
1.423     albertel 6589: =cut
                   6590: 
1.376     albertel 6591: sub scantron_clear_skip {
                   6592:     my ($scanlines,$scan_data,$i)=@_;
                   6593:     if (exists($scanlines->{'skipped'}[$i])) {
                   6594: 	undef($scanlines->{'skipped'}[$i]);
                   6595: 	return 1;
                   6596:     }
                   6597:     return 0;
                   6598: }
                   6599: 
1.423     albertel 6600: =pod
                   6601: 
                   6602: =item scantron_filter_not_exam
                   6603: 
1.424     albertel 6604:    Filter routine used by &Apache::lonnavmaps::retrieveResources(), to
                   6605:    filter out resources that are not marked as 'exam' mode
                   6606: 
1.423     albertel 6607: =cut
                   6608: 
1.334     albertel 6609: sub scantron_filter_not_exam {
                   6610:     my ($curres)=@_;
                   6611:     
                   6612:     if (ref($curres) && $curres->is_problem() && !$curres->is_exam()) {
                   6613: 	# if the user has asked to not have either hidden
                   6614: 	# or 'randomout' controlled resources to be graded
                   6615: 	# don't include them
                   6616: 	if ($env{'form.scantron_options_hidden'} eq 'ignore_hidden'
                   6617: 	    && $curres->randomout) {
                   6618: 	    return 0;
                   6619: 	}
                   6620: 	return 1;
                   6621:     }
                   6622:     return 0;
                   6623: }
                   6624: 
1.423     albertel 6625: =pod
                   6626: 
                   6627: =item scantron_validate_sequence
                   6628: 
1.424     albertel 6629:     Validates the selected sequence, checking for resource that are
                   6630:     not set to exam mode.
                   6631: 
1.423     albertel 6632: =cut
                   6633: 
1.334     albertel 6634: sub scantron_validate_sequence {
                   6635:     my ($r,$currentphase) = @_;
                   6636: 
                   6637:     my $navmap=Apache::lonnavmaps::navmap->new();
                   6638:     my (undef,undef,$sequence)=
                   6639: 	&Apache::lonnet::decode_symb($env{'form.selectpage'});
                   6640: 
                   6641:     my $map=$navmap->getResourceByUrl($sequence);
                   6642: 
                   6643:     $r->print('<input type="hidden" name="validate_sequence_exam"
                   6644:                                     value="ignore" />');
                   6645:     if ($env{'form.validate_sequence_exam'} ne 'ignore') {
                   6646: 	my @resources=
                   6647: 	    $navmap->retrieveResources($map,\&scantron_filter_not_exam,1,0);
                   6648: 	if (@resources) {
1.357     banghart 6649: 	    $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>");
1.334     albertel 6650: 	    return (1,$currentphase);
                   6651: 	}
                   6652:     }
                   6653: 
                   6654:     return (0,$currentphase+1);
                   6655: }
                   6656: 
1.423     albertel 6657: 
                   6658: 
1.157     albertel 6659: sub scantron_validate_ID {
                   6660:     my ($r,$currentphase) = @_;
                   6661:     
                   6662:     #get student info
                   6663:     my $classlist=&Apache::loncoursedata::get_classlist();
                   6664:     my %idmap=&username_to_idmap($classlist);
                   6665: 
                   6666:     #get scantron line setup
1.257     albertel 6667:     my %scantron_config=&get_scantron_config($env{'form.scantron_format'});
1.157     albertel 6668:     my ($scanlines,$scan_data)=&scantron_getfile();
1.447     foxr     6669:     
                   6670:     &scantron_get_maxbubble();	# parse needs the bubble_lines.. array.
1.157     albertel 6671: 
                   6672:     my %found=('ids'=>{},'usernames'=>{});
                   6673:     for (my $i=0;$i<=$scanlines->{'count'};$i++) {
1.200     albertel 6674: 	my $line=&scantron_get_line($scanlines,$scan_data,$i);
1.157     albertel 6675: 	if ($line=~/^[\s\cz]*$/) { next; }
                   6676: 	my $scan_record=&scantron_parse_scanline($line,$i,\%scantron_config,
                   6677: 						 $scan_data);
                   6678: 	my $id=$$scan_record{'scantron.ID'};
                   6679: 	my $found;
                   6680: 	foreach my $checkid (keys(%idmap)) {
                   6681: 	    if (lc($checkid) eq lc($id)) { $found=$checkid;last; }
                   6682: 	}
                   6683: 	if ($found) {
                   6684: 	    my $username=$idmap{$found};
                   6685: 	    if ($found{'ids'}{$found}) {
                   6686: 		&scantron_get_correction($r,$i,$scan_record,\%scantron_config,
                   6687: 					 $line,'duplicateID',$found);
1.194     albertel 6688: 		return(1,$currentphase);
1.157     albertel 6689: 	    } elsif ($found{'usernames'}{$username}) {
                   6690: 		&scantron_get_correction($r,$i,$scan_record,\%scantron_config,
                   6691: 					 $line,'duplicateID',$username);
1.194     albertel 6692: 		return(1,$currentphase);
1.157     albertel 6693: 	    }
1.186     albertel 6694: 	    #FIXME store away line we previously saw the ID on to use above
1.157     albertel 6695: 	    $found{'ids'}{$found}++;
                   6696: 	    $found{'usernames'}{$username}++;
                   6697: 	} else {
                   6698: 	    if ($id =~ /^\s*$/) {
1.158     albertel 6699: 		my $username=&scan_data($scan_data,"$i.user");
1.157     albertel 6700: 		if (defined($username) && $found{'usernames'}{$username}) {
                   6701: 		    &scantron_get_correction($r,$i,$scan_record,
                   6702: 					     \%scantron_config,
                   6703: 					     $line,'duplicateID',$username);
1.194     albertel 6704: 		    return(1,$currentphase);
1.157     albertel 6705: 		} elsif (!defined($username)) {
                   6706: 		    &scantron_get_correction($r,$i,$scan_record,
                   6707: 					     \%scantron_config,
                   6708: 					     $line,'incorrectID');
1.194     albertel 6709: 		    return(1,$currentphase);
1.157     albertel 6710: 		}
                   6711: 		$found{'usernames'}{$username}++;
                   6712: 	    } else {
                   6713: 		&scantron_get_correction($r,$i,$scan_record,\%scantron_config,
                   6714: 					 $line,'incorrectID');
1.194     albertel 6715: 		return(1,$currentphase);
1.157     albertel 6716: 	    }
                   6717: 	}
                   6718:     }
                   6719: 
                   6720:     return (0,$currentphase+1);
                   6721: }
                   6722: 
1.423     albertel 6723: 
1.157     albertel 6724: sub scantron_get_correction {
                   6725:     my ($r,$i,$scan_record,$scan_config,$line,$error,$arg)=@_;
1.454     banghart 6726: #FIXME in the case of a duplicated ID the previous line, probably need
1.157     albertel 6727: #to show both the current line and the previous one and allow skipping
                   6728: #the previous one or the current one
                   6729: 
1.333     albertel 6730:     if ( $$scan_record{'scantron.PaperID'} =~ /\S/) {
1.492     albertel 6731: 	$r->print("<p>".&mt("<b>An error was detected ($error)</b>".
                   6732: 			    " for PaperID <tt>[_1]</tt>",
                   6733: 			    $$scan_record{'scantron.PaperID'})."</p> \n");
1.157     albertel 6734:     } else {
1.492     albertel 6735: 	$r->print("<p>".&mt("<b>An error was detected ($error)</b>".
                   6736: 			    " in scanline [_1] <pre>[_2]</pre>",
                   6737: 			    $i,$line)."</p> \n");
                   6738:     }
                   6739:     my $message="<p>".&mt("The ID on the form is  <tt>[_1]</tt><br />".
                   6740: 			  "The name on the paper is [_2],[_3]",
                   6741: 			  $$scan_record{'scantron.ID'},
                   6742: 			  $$scan_record{'scantron.LastName'},
                   6743: 			  $$scan_record{'scantron.FirstName'})."</p>";
1.242     albertel 6744: 
1.157     albertel 6745:     $r->print('<input type="hidden" name="scantron_corrections" value="'.$error.'" />'."\n");
                   6746:     $r->print('<input type="hidden" name="scantron_line" value="'.$i.'" />'."\n");
1.503     raeburn  6747:                            # Array populated for doublebubble or
                   6748:     my @lines_to_correct;  # missingbubble errors to build javascript
                   6749:                            # to validate radio button checking   
                   6750: 
1.157     albertel 6751:     if ($error =~ /ID$/) {
1.186     albertel 6752: 	if ($error eq 'incorrectID') {
1.492     albertel 6753: 	    $r->print("<p>".&mt("The encoded ID is not in the classlist").
                   6754: 		      "</p>\n");
1.157     albertel 6755: 	} elsif ($error eq 'duplicateID') {
1.492     albertel 6756: 	    $r->print("<p>".&mt("The encoded ID has also been used by a previous paper [_1]",$arg)."</p>\n");
1.157     albertel 6757: 	}
1.242     albertel 6758: 	$r->print($message);
1.492     albertel 6759: 	$r->print("<p>".&mt("How should I handle this?")." <br /> \n");
1.157     albertel 6760: 	$r->print("\n<ul><li> ");
                   6761: 	#FIXME it would be nice if this sent back the user ID and
                   6762: 	#could do partial userID matches
                   6763: 	$r->print(&Apache::loncommon::selectstudent_link('scantronupload',
                   6764: 				       'scantron_username','scantron_domain'));
                   6765: 	$r->print(": <input type='text' name='scantron_username' value='' />");
                   6766: 	$r->print("\n@".
1.257     albertel 6767: 		 &Apache::loncommon::select_dom_form($env{'request.role.domain'},'scantron_domain'));
1.157     albertel 6768: 
                   6769: 	$r->print('</li>');
1.186     albertel 6770:     } elsif ($error =~ /CODE$/) {
                   6771: 	if ($error eq 'incorrectCODE') {
1.492     albertel 6772: 	    $r->print("<p>".&mt("The encoded CODE is not in the list of possible CODEs.")."</p>\n");
1.186     albertel 6773: 	} elsif ($error eq 'duplicateCODE') {
1.492     albertel 6774: 	    $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");
1.186     albertel 6775: 	}
1.492     albertel 6776: 	$r->print("<p>".&mt("The CODE on the form is  <tt>'[_1]'</tt>",
                   6777: 			    $$scan_record{'scantron.CODE'})."<br />\n");
1.242     albertel 6778: 	$r->print($message);
1.492     albertel 6779: 	$r->print("<p>".&mt("How should I handle this?")." <br /> \n");
1.187     albertel 6780: 	$r->print("\n<br /> ");
1.194     albertel 6781: 	my $i=0;
1.273     albertel 6782: 	if ($error eq 'incorrectCODE' 
                   6783: 	    && $$scan_record{'scantron.CODE'}=~/\S/ ) {
1.194     albertel 6784: 	    my ($max,$closest)=&scantron_get_closely_matching_CODEs($arg,$$scan_record{'scantron.CODE'});
1.278     albertel 6785: 	    if ($closest > 0) {
                   6786: 		foreach my $testcode (@{$closest}) {
                   6787: 		    my $checked='';
1.569     bisitz   6788: 		    if (!$i) { $checked=' checked="checked"'; }
1.492     albertel 6789: 		    $r->print("
                   6790:    <label>
1.569     bisitz   6791:        <input type='radio' name='scantron_CODE_resolution' value='use_closest_$i'$checked />
1.492     albertel 6792:        ".&mt("Use the similar CODE [_1] instead.",
                   6793: 	    "<b><tt>".$testcode."</tt></b>")."
                   6794:     </label>
                   6795:     <input type='hidden' name='scantron_CODE_closest_$i' value='$testcode' />");
1.278     albertel 6796: 		    $r->print("\n<br />");
                   6797: 		    $i++;
                   6798: 		}
1.194     albertel 6799: 	    }
                   6800: 	}
1.273     albertel 6801: 	if ($$scan_record{'scantron.CODE'}=~/\S/ ) {
1.569     bisitz   6802: 	    my $checked; if (!$i) { $checked=' checked="checked"'; }
1.492     albertel 6803: 	    $r->print("
                   6804:     <label>
1.569     bisitz   6805:         <input type='radio' name='scantron_CODE_resolution' value='use_unfound'$checked />
1.492     albertel 6806:        ".&mt("Use the CODE [_1] that is was on the paper, ignoring the error.",
                   6807: 	     "<b><tt>".$$scan_record{'scantron.CODE'}."</tt></b>")."
                   6808:     </label>");
1.273     albertel 6809: 	    $r->print("\n<br />");
                   6810: 	}
1.194     albertel 6811: 
1.188     albertel 6812: 	$r->print(<<ENDSCRIPT);
                   6813: <script type="text/javascript">
                   6814: function change_radio(field) {
1.190     albertel 6815:     var slct=document.scantronupload.scantron_CODE_resolution;
1.188     albertel 6816:     var i;
                   6817:     for (i=0;i<slct.length;i++) {
                   6818:         if (slct[i].value==field) { slct[i].checked=true; }
                   6819:     }
                   6820: }
                   6821: </script>
                   6822: ENDSCRIPT
1.187     albertel 6823: 	my $href="/adm/pickcode?".
1.359     www      6824: 	   "form=".&escape("scantronupload").
                   6825: 	   "&scantron_format=".&escape($env{'form.scantron_format'}).
                   6826: 	   "&scantron_CODElist=".&escape($env{'form.scantron_CODElist'}).
                   6827: 	   "&curCODE=".&escape($$scan_record{'scantron.CODE'}).
                   6828: 	   "&scantron_selectfile=".&escape($env{'form.scantron_selectfile'});
1.332     albertel 6829: 	if ($env{'form.scantron_CODElist'} =~ /\S/) { 
1.492     albertel 6830: 	    $r->print("
                   6831:     <label>
                   6832:        <input type='radio' name='scantron_CODE_resolution' value='use_found' />
                   6833:        ".&mt("[_1]Select[_2] a CODE from the list of all CODEs and use it.",
                   6834: 	     "<a target='_blank' href='$href'>","</a>")."
                   6835:     </label> 
1.558     bisitz   6836:     ".&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\')" />'));
1.332     albertel 6837: 	    $r->print("\n<br />");
                   6838: 	}
1.492     albertel 6839: 	$r->print("
                   6840:     <label>
                   6841:        <input type='radio' name='scantron_CODE_resolution' value='use_typed' />
                   6842:        ".&mt("Use [_1] as the CODE.",
                   6843: 	     "</label><input type='text' size='8' name='scantron_CODE_newvalue' onfocus=\"javascript:change_radio('use_typed')\" onkeypress=\"javascript:change_radio('use_typed')\" />"));
1.187     albertel 6844: 	$r->print("\n<br /><br />");
1.157     albertel 6845:     } elsif ($error eq 'doublebubble') {
1.503     raeburn  6846: 	$r->print("<p>".&mt("There have been multiple bubbles scanned for some question(s)")."</p>\n");
1.497     foxr     6847: 
                   6848: 	# The form field scantron_questions is acutally a list of line numbers.
                   6849: 	# represented by this form so:
                   6850: 
                   6851: 	my $line_list = &questions_to_line_list($arg);
                   6852: 
1.157     albertel 6853: 	$r->print('<input type="hidden" name="scantron_questions" value="'.
1.497     foxr     6854: 		  $line_list.'" />');
1.242     albertel 6855: 	$r->print($message);
1.492     albertel 6856: 	$r->print("<p>".&mt("Please indicate which bubble should be used for grading")."</p>");
1.157     albertel 6857: 	foreach my $question (@{$arg}) {
1.503     raeburn  6858: 	    my @linenums = &prompt_for_corrections($r,$question,$scan_config,
                   6859:                                                    $scan_record, $error);
1.524     raeburn  6860:             push(@lines_to_correct,@linenums);
1.157     albertel 6861: 	}
1.503     raeburn  6862:         $r->print(&verify_bubbles_checked(@lines_to_correct));
1.157     albertel 6863:     } elsif ($error eq 'missingbubble') {
1.492     albertel 6864: 	$r->print("<p>".&mt("There have been <b>no</b> bubbles scanned for some question(s)")."</p>\n");
1.242     albertel 6865: 	$r->print($message);
1.492     albertel 6866: 	$r->print("<p>".&mt("Please indicate which bubble should be used for grading.")."</p>");
1.503     raeburn  6867: 	$r->print(&mt("Some questions have no scanned bubbles.")."\n");
1.497     foxr     6868: 
1.503     raeburn  6869: 	# The form field scantron_questions is actually a list of line numbers not
1.497     foxr     6870: 	# a list of question numbers. Therefore:
                   6871: 	#
                   6872: 	
                   6873: 	my $line_list = &questions_to_line_list($arg);
                   6874: 
1.157     albertel 6875: 	$r->print('<input type="hidden" name="scantron_questions" value="'.
1.497     foxr     6876: 		  $line_list.'" />');
1.157     albertel 6877: 	foreach my $question (@{$arg}) {
1.503     raeburn  6878: 	    my @linenums = &prompt_for_corrections($r,$question,$scan_config,
                   6879:                                                    $scan_record, $error);
1.524     raeburn  6880:             push(@lines_to_correct,@linenums);
1.157     albertel 6881: 	}
1.503     raeburn  6882:         $r->print(&verify_bubbles_checked(@lines_to_correct));
1.157     albertel 6883:     } else {
                   6884: 	$r->print("\n<ul>");
                   6885:     }
                   6886:     $r->print("\n</li></ul>");
1.497     foxr     6887: }
                   6888: 
1.503     raeburn  6889: sub verify_bubbles_checked {
                   6890:     my (@ansnums) = @_;
                   6891:     my $ansnumstr = join('","',@ansnums);
                   6892:     my $warning = &mt("A bubble or 'No bubble' selection has not been made for one or more lines.");
                   6893:     my $output = (<<ENDSCRIPT);
                   6894: <script type="text/javascript">
                   6895: function verify_bubble_radio(form) {
                   6896:     var ansnumArray = new Array ("$ansnumstr");
                   6897:     var need_bubble_count = 0;
                   6898:     for (var i=0; i<ansnumArray.length; i++) {
                   6899:         if (form.elements["scantron_correct_Q_"+ansnumArray[i]].length > 1) {
                   6900:             var bubble_picked = 0; 
                   6901:             for (var j=0; j<form.elements["scantron_correct_Q_"+ansnumArray[i]].length; j++) {
                   6902:                 if (form.elements["scantron_correct_Q_"+ansnumArray[i]][j].checked == true) {
                   6903:                     bubble_picked = 1;
                   6904:                 }
                   6905:             }
                   6906:             if (bubble_picked == 0) {
                   6907:                 need_bubble_count ++;
                   6908:             }
                   6909:         }
                   6910:     }
                   6911:     if (need_bubble_count) {
                   6912:         alert("$warning");
                   6913:         return;
                   6914:     }
                   6915:     form.submit(); 
                   6916: }
                   6917: </script>
                   6918: ENDSCRIPT
                   6919:     return $output;
                   6920: }
                   6921: 
1.497     foxr     6922: =pod
                   6923: 
                   6924: =item  questions_to_line_list
1.157     albertel 6925: 
1.497     foxr     6926: Converts a list of questions into a string of comma separated
                   6927: line numbers in the answer sheet used by the questions.  This is
                   6928: used to fill in the scantron_questions form field.
                   6929: 
                   6930:   Arguments:
                   6931:      questions    - Reference to an array of questions.
                   6932: 
                   6933: =cut
                   6934: 
                   6935: 
                   6936: sub questions_to_line_list {
                   6937:     my ($questions) = @_;
                   6938:     my @lines;
                   6939: 
1.503     raeburn  6940:     foreach my $item (@{$questions}) {
                   6941:         my $question = $item;
                   6942:         my ($first,$count,$last);
                   6943:         if ($item =~ /^(\d+)\.(\d+)$/) {
                   6944:             $question = $1;
                   6945:             my $subquestion = $2;
                   6946:             $first = $first_bubble_line{$question-1} + 1;
                   6947:             my @subans = split(/,/,$subdivided_bubble_lines{$question-1});
                   6948:             my $subcount = 1;
                   6949:             while ($subcount<$subquestion) {
                   6950:                 $first += $subans[$subcount-1];
                   6951:                 $subcount ++;
                   6952:             }
                   6953:             $count = $subans[$subquestion-1];
                   6954:         } else {
                   6955: 	    $first   = $first_bubble_line{$question-1} + 1;
                   6956: 	    $count   = $bubble_lines_per_response{$question-1};
                   6957:         }
1.506     raeburn  6958:         $last = $first+$count-1;
1.503     raeburn  6959:         push(@lines, ($first..$last));
1.497     foxr     6960:     }
                   6961:     return join(',', @lines);
                   6962: }
                   6963: 
                   6964: =pod 
                   6965: 
                   6966: =item prompt_for_corrections
                   6967: 
                   6968: Prompts for a potentially multiline correction to the
                   6969: user's bubbling (factors out common code from scantron_get_correction
                   6970: for multi and missing bubble cases).
                   6971: 
                   6972:  Arguments:
                   6973:    $r           - Apache request object.
                   6974:    $question    - The question number to prompt for.
                   6975:    $scan_config - The scantron file configuration hash.
                   6976:    $scan_record - Reference to the hash that has the the parsed scanlines.
1.503     raeburn  6977:    $error       - Type of error
1.497     foxr     6978: 
                   6979:  Implicit inputs:
                   6980:    %bubble_lines_per_response   - Starting line numbers for each question.
                   6981:                                   Numbered from 0 (but question numbers are from
                   6982:                                   1.
                   6983:    %first_bubble_line           - Starting bubble line for each question.
1.509     raeburn  6984:    %subdivided_bubble_lines     - optionresponse, matchresponse and rankresponse 
                   6985:                                   type problems render as separate sub-questions, 
1.503     raeburn  6986:                                   in exam mode. This hash contains a 
                   6987:                                   comma-separated list of the lines per 
                   6988:                                   sub-question.
1.510     raeburn  6989:    %responsetype_per_response   - essayresponse, formularesponse,
                   6990:                                   stringresponse, imageresponse, reactionresponse,
                   6991:                                   and organicresponse type problem parts can have
1.503     raeburn  6992:                                   multiple lines per response if the weight
                   6993:                                   assigned exceeds 10.  In this case, only
                   6994:                                   one bubble per line is permitted, but more 
                   6995:                                   than one line might contain bubbles, e.g.
                   6996:                                   bubbling of: line 1 - J, line 2 - J, 
                   6997:                                   line 3 - B would assign 22 points.  
1.497     foxr     6998: 
                   6999: =cut
                   7000: 
                   7001: sub prompt_for_corrections {
1.503     raeburn  7002:     my ($r, $question, $scan_config, $scan_record, $error) = @_;
                   7003:     my ($current_line,$lines);
                   7004:     my @linenums;
                   7005:     my $questionnum = $question;
                   7006:     if ($question =~ /^(\d+)\.(\d+)$/) {
                   7007:         $question = $1;
                   7008:         $current_line = $first_bubble_line{$question-1} + 1 ;
                   7009:         my $subquestion = $2;
                   7010:         my @subans = split(/,/,$subdivided_bubble_lines{$question-1});
                   7011:         my $subcount = 1;
                   7012:         while ($subcount<$subquestion) {
                   7013:             $current_line += $subans[$subcount-1];
                   7014:             $subcount ++;
                   7015:         }
                   7016:         $lines = $subans[$subquestion-1];
                   7017:     } else {
                   7018:         $current_line = $first_bubble_line{$question-1} + 1 ;
                   7019:         $lines        = $bubble_lines_per_response{$question-1};
                   7020:     }
1.497     foxr     7021:     if ($lines > 1) {
1.503     raeburn  7022:         $r->print(&mt('The group of bubble lines below responds to a single question.').'<br />');
                   7023:         if (($responsetype_per_response{$question-1} eq 'essayresponse') ||
                   7024:             ($responsetype_per_response{$question-1} eq 'formularesponse') ||
1.510     raeburn  7025:             ($responsetype_per_response{$question-1} eq 'stringresponse') ||
                   7026:             ($responsetype_per_response{$question-1} eq 'imageresponse') ||
                   7027:             ($responsetype_per_response{$question-1} eq 'reactionresponse') ||
                   7028:             ($responsetype_per_response{$question-1} eq 'organicresponse')) {
1.572     www      7029:             $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 />');
1.503     raeburn  7030:         } else {
                   7031:             $r->print(&mt("Select at most one bubble in a single line and select 'No Bubble' in all the other lines. ")."<br />");
                   7032:         }
1.497     foxr     7033:     }
                   7034:     for (my $i =0; $i < $lines; $i++) {
1.503     raeburn  7035:         my $selected = $$scan_record{"scantron.$current_line.answer"};
                   7036: 	&scantron_bubble_selector($r,$scan_config,$current_line, 
                   7037: 	        		  $questionnum,$error,split('', $selected));
1.524     raeburn  7038:         push(@linenums,$current_line);
1.497     foxr     7039: 	$current_line++;
                   7040:     }
                   7041:     if ($lines > 1) {
                   7042: 	$r->print("<hr /><br />");
                   7043:     }
1.503     raeburn  7044:     return @linenums;
1.157     albertel 7045: }
1.423     albertel 7046: 
                   7047: =pod
                   7048: 
                   7049: =item scantron_bubble_selector
                   7050:   
                   7051:    Generates the html radiobuttons to correct a single bubble line
1.424     albertel 7052:    possibly showing the existing the selected bubbles if known
1.423     albertel 7053: 
                   7054:  Arguments:
                   7055:     $r           - Apache request object
                   7056:     $scan_config - hash from &get_scantron_config()
1.497     foxr     7057:     $line        - Number of the line being displayed.
1.503     raeburn  7058:     $questionnum - Question number (may include subquestion)
                   7059:     $error       - Type of error.
1.497     foxr     7060:     @selected    - Array of bubbles picked on this line.
1.423     albertel 7061: 
                   7062: =cut
                   7063: 
1.157     albertel 7064: sub scantron_bubble_selector {
1.503     raeburn  7065:     my ($r,$scan_config,$line,$questionnum,$error,@selected)=@_;
1.157     albertel 7066:     my $max=$$scan_config{'Qlength'};
1.274     albertel 7067: 
                   7068:     my $scmode=$$scan_config{'Qon'};
                   7069:     if ($scmode eq 'number' || $scmode eq 'letter') { $max=10; }	     
                   7070: 
1.157     albertel 7071:     my @alphabet=('A'..'Z');
1.503     raeburn  7072:     $r->print(&Apache::loncommon::start_data_table().
                   7073:               &Apache::loncommon::start_data_table_row());
                   7074:     $r->print('<td rowspan="2" class="LC_leftcol_header">'.$line.'</td>');
1.497     foxr     7075:     for (my $i=0;$i<$max+1;$i++) {
                   7076: 	$r->print("\n".'<td align="center">');
                   7077: 	if ($selected[0] eq $alphabet[$i]) { $r->print('X'); shift(@selected) }
                   7078: 	else { $r->print('&nbsp;'); }
                   7079: 	$r->print('</td>');
                   7080:     }
1.503     raeburn  7081:     $r->print(&Apache::loncommon::end_data_table_row().
                   7082:               &Apache::loncommon::start_data_table_row());
1.497     foxr     7083:     for (my $i=0;$i<$max;$i++) {
                   7084: 	$r->print("\n".
                   7085: 		  '<td><label><input type="radio" name="scantron_correct_Q_'.
                   7086: 		  $line.'" value="'.$i.'" />'.$alphabet[$i]."</label></td>");
                   7087:     }
1.503     raeburn  7088:     my $nobub_checked = ' ';
                   7089:     if ($error eq 'missingbubble') {
                   7090:         $nobub_checked = ' checked = "checked" ';
                   7091:     }
                   7092:     $r->print("\n".'<td><label><input type="radio" name="scantron_correct_Q_'.
                   7093: 	      $line.'" value="none"'.$nobub_checked.'/>'.&mt('No bubble').
                   7094:               '</label>'."\n".'<input type="hidden" name="scantron_questionnum_Q_'.
                   7095:               $line.'" value="'.$questionnum.'" /></td>');
                   7096:     $r->print(&Apache::loncommon::end_data_table_row().
                   7097:               &Apache::loncommon::end_data_table());
1.157     albertel 7098: }
                   7099: 
1.423     albertel 7100: =pod
                   7101: 
                   7102: =item num_matches
                   7103: 
1.424     albertel 7104:    Counts the number of characters that are the same between the two arguments.
                   7105: 
                   7106:  Arguments:
                   7107:    $orig - CODE from the scanline
                   7108:    $code - CODE to match against
                   7109: 
                   7110:  Returns:
                   7111:    $count - integer count of the number of same characters between the
                   7112:             two arguments
                   7113: 
1.423     albertel 7114: =cut
                   7115: 
1.194     albertel 7116: sub num_matches {
                   7117:     my ($orig,$code) = @_;
                   7118:     my @code=split(//,$code);
                   7119:     my @orig=split(//,$orig);
                   7120:     my $same=0;
                   7121:     for (my $i=0;$i<scalar(@code);$i++) {
                   7122: 	if ($code[$i] eq $orig[$i]) { $same++; }
                   7123:     }
                   7124:     return $same;
                   7125: }
                   7126: 
1.423     albertel 7127: =pod
                   7128: 
                   7129: =item scantron_get_closely_matching_CODEs
                   7130: 
1.424     albertel 7131:    Cycles through all CODEs and finds the set that has the greatest
                   7132:    number of same characters as the provided CODE
                   7133: 
                   7134:  Arguments:
                   7135:    $allcodes - hash ref returned by &get_codes()
                   7136:    $CODE     - CODE from the current scanline
                   7137: 
                   7138:  Returns:
                   7139:    2 element list
                   7140:     - first elements is number of how closely matching the best fit is 
                   7141:       (5 means best set has 5 matching characters)
                   7142:     - second element is an arrary ref containing the set of valid CODEs
                   7143:       that best fit the passed in CODE
                   7144: 
1.423     albertel 7145: =cut
                   7146: 
1.194     albertel 7147: sub scantron_get_closely_matching_CODEs {
                   7148:     my ($allcodes,$CODE)=@_;
                   7149:     my @CODEs;
                   7150:     foreach my $testcode (sort(keys(%{$allcodes}))) {
                   7151: 	push(@{$CODEs[&num_matches($CODE,$testcode)]},$testcode);
                   7152:     }
                   7153: 
                   7154:     return ($#CODEs,$CODEs[-1]);
                   7155: }
                   7156: 
1.423     albertel 7157: =pod
                   7158: 
                   7159: =item get_codes
                   7160: 
1.424     albertel 7161:    Builds a hash which has keys of all of the valid CODEs from the selected
                   7162:    set of remembered CODEs.
                   7163: 
                   7164:  Arguments:
                   7165:   $old_name - name of the set of remembered CODEs
                   7166:   $cdom     - domain of the course
                   7167:   $cnum     - internal course name
                   7168: 
                   7169:  Returns:
                   7170:   %allcodes - keys are the valid CODEs, values are all 1
                   7171: 
1.423     albertel 7172: =cut
                   7173: 
1.194     albertel 7174: sub get_codes {
1.280     foxr     7175:     my ($old_name, $cdom, $cnum) = @_;
                   7176:     if (!$old_name) {
                   7177: 	$old_name=$env{'form.scantron_CODElist'};
                   7178:     }
                   7179:     if (!$cdom) {
                   7180: 	$cdom =$env{'course.'.$env{'request.course.id'}.'.domain'};
                   7181:     }
                   7182:     if (!$cnum) {
                   7183: 	$cnum =$env{'course.'.$env{'request.course.id'}.'.num'};
                   7184:     }
1.278     albertel 7185:     my %result=&Apache::lonnet::get('CODEs',[$old_name,"type\0$old_name"],
                   7186: 				    $cdom,$cnum);
                   7187:     my %allcodes;
                   7188:     if ($result{"type\0$old_name"} eq 'number') {
                   7189: 	%allcodes=map {($_,1)} split(',',$result{$old_name});
                   7190:     } else {
                   7191: 	%allcodes=map {(&Apache::lonprintout::num_to_letters($_),1)} split(',',$result{$old_name});
                   7192:     }
1.194     albertel 7193:     return %allcodes;
                   7194: }
                   7195: 
1.423     albertel 7196: =pod
                   7197: 
                   7198: =item scantron_validate_CODE
                   7199: 
1.424     albertel 7200:    Validates all scanlines in the selected file to not have any
                   7201:    invalid or underspecified CODEs and that none of the codes are
                   7202:    duplicated if this was requested.
                   7203: 
1.423     albertel 7204: =cut
                   7205: 
1.157     albertel 7206: sub scantron_validate_CODE {
                   7207:     my ($r,$currentphase) = @_;
1.257     albertel 7208:     my %scantron_config=&get_scantron_config($env{'form.scantron_format'});
1.186     albertel 7209:     if ($scantron_config{'CODElocation'} &&
                   7210: 	$scantron_config{'CODEstart'} &&
                   7211: 	$scantron_config{'CODElength'}) {
1.257     albertel 7212: 	if (!defined($env{'form.scantron_CODElist'})) {
1.186     albertel 7213: 	    &FIXME_blow_up()
                   7214: 	}
                   7215:     } else {
                   7216: 	return (0,$currentphase+1);
                   7217:     }
                   7218:     
                   7219:     my %usedCODEs;
                   7220: 
1.194     albertel 7221:     my %allcodes=&get_codes();
1.186     albertel 7222: 
1.447     foxr     7223:     &scantron_get_maxbubble();	# parse needs the lines per response array.
                   7224: 
1.186     albertel 7225:     my ($scanlines,$scan_data)=&scantron_getfile();
                   7226:     for (my $i=0;$i<=$scanlines->{'count'};$i++) {
1.200     albertel 7227: 	my $line=&scantron_get_line($scanlines,$scan_data,$i);
1.186     albertel 7228: 	if ($line=~/^[\s\cz]*$/) { next; }
                   7229: 	my $scan_record=&scantron_parse_scanline($line,$i,\%scantron_config,
                   7230: 						 $scan_data);
                   7231: 	my $CODE=$$scan_record{'scantron.CODE'};
                   7232: 	my $error=0;
1.224     albertel 7233: 	if (!&Apache::lonnet::validCODE($CODE)) {
                   7234: 	    &scantron_get_correction($r,$i,$scan_record,
                   7235: 				     \%scantron_config,
                   7236: 				     $line,'incorrectCODE',\%allcodes);
                   7237: 	    return(1,$currentphase);
                   7238: 	}
1.221     albertel 7239: 	if (%allcodes && !exists($allcodes{$CODE}) 
                   7240: 	    && !$$scan_record{'scantron.useCODE'}) {
1.186     albertel 7241: 	    &scantron_get_correction($r,$i,$scan_record,
                   7242: 				     \%scantron_config,
1.194     albertel 7243: 				     $line,'incorrectCODE',\%allcodes);
                   7244: 	    return(1,$currentphase);
1.186     albertel 7245: 	}
1.214     albertel 7246: 	if (exists($usedCODEs{$CODE}) 
1.257     albertel 7247: 	    && $env{'form.scantron_CODEunique'} eq 'yes'
1.192     albertel 7248: 	    && !$$scan_record{'scantron.CODE_ignore_dup'}) {
1.186     albertel 7249: 	    &scantron_get_correction($r,$i,$scan_record,
                   7250: 				     \%scantron_config,
1.194     albertel 7251: 				     $line,'duplicateCODE',$usedCODEs{$CODE});
                   7252: 	    return(1,$currentphase);
1.186     albertel 7253: 	}
1.524     raeburn  7254: 	push(@{$usedCODEs{$CODE}},$$scan_record{'scantron.PaperID'});
1.186     albertel 7255:     }
1.157     albertel 7256:     return (0,$currentphase+1);
                   7257: }
                   7258: 
1.423     albertel 7259: =pod
                   7260: 
                   7261: =item scantron_validate_doublebubble
                   7262: 
1.424     albertel 7263:    Validates all scanlines in the selected file to not have any
                   7264:    bubble lines with multiple bubbles marked.
                   7265: 
1.423     albertel 7266: =cut
                   7267: 
1.157     albertel 7268: sub scantron_validate_doublebubble {
                   7269:     my ($r,$currentphase) = @_;
                   7270:     #get student info
                   7271:     my $classlist=&Apache::loncoursedata::get_classlist();
                   7272:     my %idmap=&username_to_idmap($classlist);
                   7273: 
                   7274:     #get scantron line setup
1.257     albertel 7275:     my %scantron_config=&get_scantron_config($env{'form.scantron_format'});
1.157     albertel 7276:     my ($scanlines,$scan_data)=&scantron_getfile();
1.447     foxr     7277:     &scantron_get_maxbubble();	# parse needs the bubble line array.
                   7278: 
1.157     albertel 7279:     for (my $i=0;$i<=$scanlines->{'count'};$i++) {
1.200     albertel 7280: 	my $line=&scantron_get_line($scanlines,$scan_data,$i);
1.157     albertel 7281: 	if ($line=~/^[\s\cz]*$/) { next; }
                   7282: 	my $scan_record=&scantron_parse_scanline($line,$i,\%scantron_config,
                   7283: 						 $scan_data);
                   7284: 	if (!defined($$scan_record{'scantron.doubleerror'})) { next; }
                   7285: 	&scantron_get_correction($r,$i,$scan_record,\%scantron_config,$line,
                   7286: 				 'doublebubble',
                   7287: 				 $$scan_record{'scantron.doubleerror'});
                   7288:     	return (1,$currentphase);
                   7289:     }
                   7290:     return (0,$currentphase+1);
                   7291: }
                   7292: 
1.423     albertel 7293: 
1.503     raeburn  7294: sub scantron_get_maxbubble {
1.257     albertel 7295:     if (defined($env{'form.scantron_maxbubble'}) &&
                   7296: 	$env{'form.scantron_maxbubble'}) {
1.447     foxr     7297: 	&restore_bubble_lines();
1.257     albertel 7298: 	return $env{'form.scantron_maxbubble'};
1.191     albertel 7299:     }
1.330     albertel 7300: 
1.447     foxr     7301:     my (undef, undef, $sequence) =
1.257     albertel 7302: 	&Apache::lonnet::decode_symb($env{'form.selectpage'});
1.330     albertel 7303: 
1.447     foxr     7304:     my $navmap=Apache::lonnavmaps::navmap->new();
1.191     albertel 7305:     my $map=$navmap->getResourceByUrl($sequence);
                   7306:     my @resources=$navmap->retrieveResources($map,\&scantron_filter,1,0);
1.330     albertel 7307: 
                   7308:     &Apache::lonxml::clear_problem_counter();
                   7309: 
1.557     raeburn  7310:     my $uname       = $env{'user.name'};
                   7311:     my $udom        = $env{'user.domain'};
1.435     foxr     7312:     my $cid         = $env{'request.course.id'};
                   7313:     my $total_lines = 0;
                   7314:     %bubble_lines_per_response = ();
1.447     foxr     7315:     %first_bubble_line         = ();
1.503     raeburn  7316:     %subdivided_bubble_lines   = ();
                   7317:     %responsetype_per_response = ();
1.554     raeburn  7318: 
1.447     foxr     7319:     my $response_number = 0;
                   7320:     my $bubble_line     = 0;
1.191     albertel 7321:     foreach my $resource (@resources) {
1.542     raeburn  7322:         my ($analysis,$parts) = &scantron_partids_tograde($resource,$cid,$uname,$udom);
                   7323:         if ((ref($analysis) eq 'HASH') && (ref($parts) eq 'ARRAY')) {
                   7324: 	    foreach my $part_id (@{$parts}) {
                   7325:                 my $lines;
                   7326: 
                   7327: 	        # TODO - make this a persistent hash not an array.
                   7328: 
                   7329:                 # optionresponse, matchresponse and rankresponse type items 
                   7330:                 # render as separate sub-questions in exam mode.
                   7331:                 if (($analysis->{$part_id.'.type'} eq 'optionresponse') ||
                   7332:                     ($analysis->{$part_id.'.type'} eq 'matchresponse') ||
                   7333:                     ($analysis->{$part_id.'.type'} eq 'rankresponse')) {
                   7334:                     my ($numbub,$numshown);
                   7335:                     if ($analysis->{$part_id.'.type'} eq 'optionresponse') {
                   7336:                         if (ref($analysis->{$part_id.'.options'}) eq 'ARRAY') {
                   7337:                             $numbub = scalar(@{$analysis->{$part_id.'.options'}});
                   7338:                         }
                   7339:                     } elsif ($analysis->{$part_id.'.type'} eq 'matchresponse') {
                   7340:                         if (ref($analysis->{$part_id.'.items'}) eq 'ARRAY') {
                   7341:                             $numbub = scalar(@{$analysis->{$part_id.'.items'}});
                   7342:                         }
                   7343:                     } elsif ($analysis->{$part_id.'.type'} eq 'rankresponse') {
                   7344:                         if (ref($analysis->{$part_id.'.foils'}) eq 'ARRAY') {
                   7345:                             $numbub = scalar(@{$analysis->{$part_id.'.foils'}});
                   7346:                         }
                   7347:                     }
                   7348:                     if (ref($analysis->{$part_id.'.shown'}) eq 'ARRAY') {
                   7349:                         $numshown = scalar(@{$analysis->{$part_id.'.shown'}});
                   7350:                     }
                   7351:                     my $bubbles_per_line = 10;
                   7352:                     my $inner_bubble_lines = int($numbub/$bubbles_per_line);
                   7353:                     if (($numbub % $bubbles_per_line) != 0) {
                   7354:                         $inner_bubble_lines++;
                   7355:                     }
                   7356:                     for (my $i=0; $i<$numshown; $i++) {
                   7357:                         $subdivided_bubble_lines{$response_number} .= 
                   7358:                             $inner_bubble_lines.',';
                   7359:                     }
                   7360:                     $subdivided_bubble_lines{$response_number} =~ s/,$//;
                   7361:                     $lines = $numshown * $inner_bubble_lines;
                   7362:                 } else {
                   7363:                     $lines = $analysis->{"$part_id.bubble_lines"};
                   7364:                 } 
                   7365: 
                   7366:                 $first_bubble_line{$response_number} = $bubble_line;
                   7367: 	        $bubble_lines_per_response{$response_number} = $lines;
                   7368:                 $responsetype_per_response{$response_number} = 
                   7369:                     $analysis->{$part_id.'.type'};
                   7370: 	        $response_number++;
                   7371: 
                   7372: 	        $bubble_line +=  $lines;
                   7373: 	        $total_lines +=  $lines;
                   7374: 	    }
                   7375:         }
                   7376:     }
1.552     raeburn  7377:     &Apache::lonnet::delenv('scantron.');
1.542     raeburn  7378: 
                   7379:     &save_bubble_lines();
                   7380:     $env{'form.scantron_maxbubble'} =
                   7381: 	$total_lines;
                   7382:     return $env{'form.scantron_maxbubble'};
                   7383: }
1.523     raeburn  7384: 
1.157     albertel 7385: sub scantron_validate_missingbubbles {
                   7386:     my ($r,$currentphase) = @_;
                   7387:     #get student info
                   7388:     my $classlist=&Apache::loncoursedata::get_classlist();
                   7389:     my %idmap=&username_to_idmap($classlist);
                   7390: 
                   7391:     #get scantron line setup
1.257     albertel 7392:     my %scantron_config=&get_scantron_config($env{'form.scantron_format'});
1.157     albertel 7393:     my ($scanlines,$scan_data)=&scantron_getfile();
1.191     albertel 7394:     my $max_bubble=&scantron_get_maxbubble();
1.157     albertel 7395:     if (!$max_bubble) { $max_bubble=2**31; }
                   7396:     for (my $i=0;$i<=$scanlines->{'count'};$i++) {
1.200     albertel 7397: 	my $line=&scantron_get_line($scanlines,$scan_data,$i);
1.157     albertel 7398: 	if ($line=~/^[\s\cz]*$/) { next; }
                   7399: 	my $scan_record=&scantron_parse_scanline($line,$i,\%scantron_config,
                   7400: 						 $scan_data);
                   7401: 	if (!defined($$scan_record{'scantron.missingerror'})) { next; }
                   7402: 	my @to_correct;
1.470     foxr     7403: 	
                   7404: 	# Probably here's where the error is...
                   7405: 
1.157     albertel 7406: 	foreach my $missing (@{$$scan_record{'scantron.missingerror'}}) {
1.505     raeburn  7407:             my $lastbubble;
                   7408:             if ($missing =~ /^(\d+)\.(\d+)$/) {
                   7409:                my $question = $1;
                   7410:                my $subquestion = $2;
                   7411:                if (!defined($first_bubble_line{$question -1})) { next; }
                   7412:                my $first = $first_bubble_line{$question-1};
                   7413:                my @subans = split(/,/,$subdivided_bubble_lines{$question-1});
                   7414:                my $subcount = 1;
                   7415:                while ($subcount<$subquestion) {
                   7416:                    $first += $subans[$subcount-1];
                   7417:                    $subcount ++;
                   7418:                }
                   7419:                my $count = $subans[$subquestion-1];
                   7420:                $lastbubble = $first + $count;
                   7421:             } else {
                   7422:                 if (!defined($first_bubble_line{$missing - 1})) { next; }
                   7423:                 $lastbubble = $first_bubble_line{$missing - 1} + $bubble_lines_per_response{$missing - 1};
                   7424:             }
                   7425:             if ($lastbubble > $max_bubble) { next; }
1.157     albertel 7426: 	    push(@to_correct,$missing);
                   7427: 	}
                   7428: 	if (@to_correct) {
                   7429: 	    &scantron_get_correction($r,$i,$scan_record,\%scantron_config,
                   7430: 				     $line,'missingbubble',\@to_correct);
                   7431: 	    return (1,$currentphase);
                   7432: 	}
                   7433: 
                   7434:     }
                   7435:     return (0,$currentphase+1);
                   7436: }
                   7437: 
1.423     albertel 7438: 
1.82      albertel 7439: sub scantron_process_students {
1.75      albertel 7440:     my ($r) = @_;
1.513     foxr     7441: 
1.257     albertel 7442:     my (undef,undef,$sequence)=&Apache::lonnet::decode_symb($env{'form.selectpage'});
1.324     albertel 7443:     my ($symb)=&get_symb($r);
1.513     foxr     7444:     if (!$symb) {
                   7445: 	return '';
                   7446:     }
1.324     albertel 7447:     my $default_form_data=&defaultFormData($symb);
1.82      albertel 7448: 
1.257     albertel 7449:     my %scantron_config=&get_scantron_config($env{'form.scantron_format'});
1.157     albertel 7450:     my ($scanlines,$scan_data)=&scantron_getfile();
1.82      albertel 7451:     my $classlist=&Apache::loncoursedata::get_classlist();
                   7452:     my %idmap=&username_to_idmap($classlist);
1.132     bowersj2 7453:     my $navmap=Apache::lonnavmaps::navmap->new();
1.83      albertel 7454:     my $map=$navmap->getResourceByUrl($sequence);
                   7455:     my @resources=$navmap->retrieveResources($map,\&scantron_filter,1,0);
1.557     raeburn  7456:     my (%grader_partids_by_symb,%grader_randomlists_by_symb);
                   7457:     &graders_resources_pass(\@resources,\%grader_partids_by_symb,
                   7458:                             \%grader_randomlists_by_symb);
                   7459:     foreach my $resource (@resources) {
                   7460:         my $ressymb = $resource->symb();
                   7461:         my ($analysis,$parts) =
                   7462:             &scantron_partids_tograde($resource,$env{'request.course.id'},
                   7463:                                       $env{'user.name'},$env{'user.domain'},1);
                   7464:         $grader_partids_by_symb{$ressymb} = $parts;
                   7465:         if (ref($analysis) eq 'HASH') {
                   7466:             if (ref($analysis->{'parts_withrandomlist'}) eq 'ARRAY') {
                   7467:                 $grader_randomlists_by_symb{$ressymb} = 
                   7468:                     $analysis->{'parts_withrandomlist'};
                   7469:             }
                   7470:         }
                   7471:     }
                   7472: 
1.554     raeburn  7473:     my ($uname,$udom);
1.82      albertel 7474:     my $result= <<SCANTRONFORM;
1.81      albertel 7475: <form method="post" enctype="multipart/form-data" action="/adm/grades" name="scantronupload">
                   7476:   <input type="hidden" name="command" value="scantron_configphase" />
                   7477:   $default_form_data
                   7478: SCANTRONFORM
1.82      albertel 7479:     $r->print($result);
                   7480: 
                   7481:     my @delayqueue;
1.542     raeburn  7482:     my (%completedstudents,%scandata);
1.140     albertel 7483:     
1.520     www      7484:     my $lock=&Apache::lonnet::set_lock(&mt('Grading bubblesheet exam'));
1.200     albertel 7485:     my $count=&get_todo_count($scanlines,$scan_data);
1.575     www      7486:     my %prog_state=&Apache::lonhtmlcommon::Create_PrgWin($r,'Bubblesheet Status',
                   7487:  				    'Bubblesheet Progress',$count,
1.195     albertel 7488: 				    'inline',undef,'scantronupload');
1.140     albertel 7489:     &Apache::lonhtmlcommon::Update_PrgWin($r,\%prog_state,
                   7490: 					  'Processing first student');
1.542     raeburn  7491:     $r->print('<br />');
1.140     albertel 7492:     my $start=&Time::HiRes::time();
1.158     albertel 7493:     my $i=-1;
1.542     raeburn  7494:     my $started;
1.447     foxr     7495: 
                   7496:     &scantron_get_maxbubble();	# Need the bubble lines array to parse.
1.513     foxr     7497: 
                   7498:     # If an ssi failed in scantron_get_maxbubble, put an error message out to
                   7499:     # the user and return.
                   7500: 
                   7501:     if ($ssi_error) {
                   7502: 	$r->print("</form>");
                   7503: 	&ssi_print_error($r);
                   7504: 	$r->print(&show_grading_menu_form($symb));
1.520     www      7505:         &Apache::lonnet::remove_lock($lock);
1.513     foxr     7506: 	return '';		# Dunno why the other returns return '' rather than just returning.
                   7507:     }
1.447     foxr     7508: 
1.542     raeburn  7509:     my %lettdig = &letter_to_digits();
                   7510:     my $numletts = scalar(keys(%lettdig));
                   7511: 
1.157     albertel 7512:     while ($i<$scanlines->{'count'}) {
                   7513:  	($uname,$udom)=('','');
                   7514:  	$i++;
1.200     albertel 7515:  	my $line=&scantron_get_line($scanlines,$scan_data,$i);
1.157     albertel 7516:  	if ($line=~/^[\s\cz]*$/) { next; }
1.200     albertel 7517: 	if ($started) {
                   7518: 	    &Apache::lonhtmlcommon::Increment_PrgWin($r,\%prog_state,
                   7519: 						     'last student');
                   7520: 	}
                   7521: 	$started=1;
1.157     albertel 7522:  	my $scan_record=&scantron_parse_scanline($line,$i,\%scantron_config,
                   7523:  						 $scan_data);
                   7524:  	unless ($uname=&scantron_find_student($scan_record,$scan_data,
                   7525:  					      \%idmap,$i)) {
                   7526:   	    &scantron_add_delay(\@delayqueue,$line,
                   7527:  				'Unable to find a student that matches',1);
                   7528:  	    next;
                   7529:   	}
                   7530:  	if (exists $completedstudents{$uname}) {
                   7531:  	    &scantron_add_delay(\@delayqueue,$line,
                   7532:  				'Student '.$uname.' has multiple sheets',2);
                   7533:  	    next;
                   7534:  	}
                   7535:   	($uname,$udom)=split(/:/,$uname);
1.330     albertel 7536: 
1.554     raeburn  7537:         my %partids_by_symb;
                   7538:         foreach my $resource (@resources) {
                   7539:             my $ressymb = $resource->symb();
1.557     raeburn  7540:             if ((exists($grader_randomlists_by_symb{$ressymb})) ||
                   7541:                 (ref($grader_partids_by_symb{$ressymb}) ne 'ARRAY')) {
                   7542:                 my ($analysis,$parts) =
                   7543:                     &scantron_partids_tograde($resource,$env{'request.course.id'},$uname,$udom);
                   7544:                 $partids_by_symb{$ressymb} = $parts;
                   7545:             } else {
                   7546:                 $partids_by_symb{$ressymb} = $grader_partids_by_symb{$ressymb};
                   7547:             }
1.554     raeburn  7548:         }
                   7549: 
1.330     albertel 7550: 	&Apache::lonxml::clear_problem_counter();
1.514     raeburn  7551:   	&Apache::lonnet::appenv($scan_record);
1.376     albertel 7552: 
                   7553: 	if (&scantron_clear_skip($scanlines,$scan_data,$i)) {
                   7554: 	    &scantron_putfile($scanlines,$scan_data);
                   7555: 	}
1.161     albertel 7556: 	
1.542     raeburn  7557:         my $scancode;
                   7558:         if ((exists($scan_record->{'scantron.CODE'})) &&
                   7559:             (&Apache::lonnet::validCODE($scan_record->{'scantron.CODE'}))) {
                   7560:             $scancode = $scan_record->{'scantron.CODE'};
                   7561:         } else {
                   7562:             $scancode = '';
                   7563:         }
                   7564: 
                   7565:         if (&grade_student_bubbles($r,$uname,$udom,$scan_record,$scancode,
1.554     raeburn  7566:                                    \@resources,\%partids_by_symb) eq 'ssi_error') {
1.542     raeburn  7567:             $ssi_error = 0; # So end of handler error message does not trigger.
                   7568:             $r->print("</form>");
                   7569:             &ssi_print_error($r);
                   7570:             $r->print(&show_grading_menu_form($symb));
                   7571:             &Apache::lonnet::remove_lock($lock);
                   7572:             return '';      # Why return ''?  Beats me.
                   7573:         }
1.513     foxr     7574: 
1.140     albertel 7575: 	$completedstudents{$uname}={'line'=>$line};
1.542     raeburn  7576:         if ($env{'form.verifyrecord'}) {
                   7577:             my $lastpos = $env{'form.scantron_maxbubble'}*$scantron_config{'Qlength'};
                   7578:             my $studentdata = substr($line,$scantron_config{'Qstart'}-1,$lastpos);
                   7579:             chomp($studentdata);
                   7580:             $studentdata =~ s/\r$//;
                   7581:             my $studentrecord = '';
                   7582:             my $counter = -1;
                   7583:             foreach my $resource (@resources) {
1.554     raeburn  7584:                 my $ressymb = $resource->symb();
1.542     raeburn  7585:                 ($counter,my $recording) =
                   7586:                     &verify_scantron_grading($resource,$udom,$uname,$env{'request.course.id'},
1.554     raeburn  7587:                                              $counter,$studentdata,$partids_by_symb{$ressymb},
1.542     raeburn  7588:                                              \%scantron_config,\%lettdig,$numletts);
                   7589:                 $studentrecord .= $recording;
                   7590:             }
                   7591:             if ($studentrecord ne $studentdata) {
1.554     raeburn  7592:                 &Apache::lonxml::clear_problem_counter();
                   7593:                 if (&grade_student_bubbles($r,$uname,$udom,$scan_record,$scancode,
                   7594:                                            \@resources,\%partids_by_symb) eq 'ssi_error') {
                   7595:                     $ssi_error = 0; # So end of handler error message does not trigger.
                   7596:                     $r->print("</form>");
                   7597:                     &ssi_print_error($r);
                   7598:                     $r->print(&show_grading_menu_form($symb));
                   7599:                     &Apache::lonnet::remove_lock($lock);
                   7600:                     delete($completedstudents{$uname});
                   7601:                     return '';
                   7602:                 }
1.542     raeburn  7603:                 $counter = -1;
                   7604:                 $studentrecord = '';
                   7605:                 foreach my $resource (@resources) {
1.554     raeburn  7606:                     my $ressymb = $resource->symb();
1.542     raeburn  7607:                     ($counter,my $recording) =
                   7608:                         &verify_scantron_grading($resource,$udom,$uname,$env{'request.course.id'},
1.554     raeburn  7609:                                                  $counter,$studentdata,$partids_by_symb{$ressymb},
1.542     raeburn  7610:                                                  \%scantron_config,\%lettdig,$numletts);
                   7611:                     $studentrecord .= $recording;
                   7612:                 }
                   7613:                 if ($studentrecord ne $studentdata) {
                   7614:                     $r->print('<p><span class="LC_error">');
                   7615:                     if ($scancode eq '') {
                   7616:                         $r->print(&mt('Mismatch grading bubble sheet for user: [_1] with ID: [_2].',
                   7617:                                   $uname.':'.$udom,$scan_record->{'scantron.ID'}));
                   7618:                     } else {
                   7619:                         $r->print(&mt('Mismatch grading bubble sheet for user: [_1] with ID: [_2] and CODE: [_3].',
                   7620:                                   $uname.':'.$udom,$scan_record->{'scantron.ID'},$scancode));
                   7621:                     }
                   7622:                     $r->print('</span><br />'.&Apache::loncommon::start_data_table()."\n".
                   7623:                               &Apache::loncommon::start_data_table_header_row()."\n".
                   7624:                               '<th>'.&mt('Source').'</th><th>'.&mt('Bubbled responses').'</th>'.
                   7625:                               &Apache::loncommon::end_data_table_header_row()."\n".
                   7626:                               &Apache::loncommon::start_data_table_row().
                   7627:                               '<td>'.&mt('Bubble Sheet').'</td>'.
                   7628:                               '<td><span class="LC_nobreak">'.$studentdata.'</span></td>'.
                   7629:                               &Apache::loncommon::end_data_table_row().
                   7630:                               &Apache::loncommon::start_data_table_row().
                   7631:                               '<td>Stored submissions</td>'.
                   7632:                               '<td><span class="LC_nobreak">'.$studentrecord.'</span></td>'."\n".
                   7633:                               &Apache::loncommon::end_data_table_row().
                   7634:                               &Apache::loncommon::end_data_table().'</p>');
                   7635:                 } else {
                   7636:                     $r->print('<br /><span class="LC_warning">'.
                   7637:                              &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 />'.
                   7638:                              &mt("As a consequence, this user's submission history records two tries.").
                   7639:                                  '</span><br />');
                   7640:                 }
                   7641:             }
                   7642:         }
1.543     raeburn  7643:         if (&Apache::loncommon::connection_aborted($r)) { last; }
1.140     albertel 7644:     } continue {
1.330     albertel 7645: 	&Apache::lonxml::clear_problem_counter();
1.552     raeburn  7646: 	&Apache::lonnet::delenv('scantron.');
1.82      albertel 7647:     }
1.140     albertel 7648:     &Apache::lonhtmlcommon::Close_PrgWin($r,\%prog_state);
1.520     www      7649:     &Apache::lonnet::remove_lock($lock);
1.172     albertel 7650: #    my $lasttime = &Time::HiRes::time()-$start;
                   7651: #    $r->print("<p>took $lasttime</p>");
1.140     albertel 7652: 
1.200     albertel 7653:     $r->print("</form>");
1.324     albertel 7654:     $r->print(&show_grading_menu_form($symb));
1.157     albertel 7655:     return '';
1.75      albertel 7656: }
1.157     albertel 7657: 
1.557     raeburn  7658: sub graders_resources_pass {
                   7659:     my ($resources,$grader_partids_by_symb,$grader_randomlists_by_symb) = @_;
                   7660:     if ((ref($resources) eq 'ARRAY') && (ref($grader_partids_by_symb)) && 
                   7661:         (ref($grader_randomlists_by_symb) eq 'HASH')) {
                   7662:         foreach my $resource (@{$resources}) {
                   7663:             my $ressymb = $resource->symb();
                   7664:             my ($analysis,$parts) =
                   7665:                 &scantron_partids_tograde($resource,$env{'request.course.id'},
                   7666:                                           $env{'user.name'},$env{'user.domain'},1);
                   7667:             $grader_partids_by_symb->{$ressymb} = $parts;
                   7668:             if (ref($analysis) eq 'HASH') {
                   7669:                 if (ref($analysis->{'parts_withrandomlist'}) eq 'ARRAY') {
                   7670:                     $grader_randomlists_by_symb->{$ressymb} =
                   7671:                         $analysis->{'parts_withrandomlist'};
                   7672:                 }
                   7673:             }
                   7674:         }
                   7675:     }
                   7676:     return;
                   7677: }
                   7678: 
1.542     raeburn  7679: sub grade_student_bubbles {
1.554     raeburn  7680:     my ($r,$uname,$udom,$scan_record,$scancode,$resources,$parts) = @_;
                   7681:     if (ref($resources) eq 'ARRAY') {
                   7682:         my $count = 0;
                   7683:         foreach my $resource (@{$resources}) {
                   7684:             my $ressymb = $resource->symb();
                   7685:             my %form = ('submitted'      => 'scantron',
                   7686:                         'grade_target'   => 'grade',
                   7687:                         'grade_username' => $uname,
                   7688:                         'grade_domain'   => $udom,
                   7689:                         'grade_courseid' => $env{'request.course.id'},
                   7690:                         'grade_symb'     => $ressymb,
                   7691:                         'CODE'           => $scancode
                   7692:                        );
                   7693:             if (ref($parts) eq 'HASH') {
                   7694:                 if (ref($parts->{$ressymb}) eq 'ARRAY') {
                   7695:                     foreach my $part (@{$parts->{$ressymb}}) {
                   7696:                         $form{'scantron_questnum_start.'.$part} =
                   7697:                             1+$env{'form.scantron.first_bubble_line.'.$count};
                   7698:                         $count++;
                   7699:                     }
                   7700:                 }
                   7701:             }
                   7702:             my $result=&ssi_with_retries($resource->src(),$ssi_retries,%form);
                   7703:             return 'ssi_error' if ($ssi_error);
                   7704:             last if (&Apache::loncommon::connection_aborted($r));
                   7705:         }
1.542     raeburn  7706:     }
                   7707:     return;
                   7708: }
                   7709: 
1.157     albertel 7710: sub scantron_upload_scantron_data {
                   7711:     my ($r)=@_;
1.565     raeburn  7712:     my $dom = $env{'request.role.domain'};
                   7713:     my $domdesc = &Apache::lonnet::domain($dom,'description');
                   7714:     $r->print(&Apache::loncommon::coursebrowser_javascript($dom));
1.157     albertel 7715:     my $select_link=&Apache::loncommon::selectcourse_link('rules','courseid',
1.181     albertel 7716: 							  'domainid',
1.565     raeburn  7717: 							  'coursename',$dom);
                   7718:     my $syllabuslink = '<a href="javascript:ToSyllabus();">'.&mt('Syllabus').'</a>'.
                   7719:                        ('&nbsp'x2).&mt('(shows course personnel)'); 
1.324     albertel 7720:     my $default_form_data=&defaultFormData(&get_symb($r,1));
1.579   ! raeburn  7721:     my $nofile_alert = &mt('Please use the browse button to select a file from your local directory.');
        !          7722:     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.");
1.492     albertel 7723:     $r->print('
1.157     albertel 7724: <script type="text/javascript" language="javascript">
                   7725:     function checkUpload(formname) {
                   7726: 	if (formname.upfile.value == "") {
1.579   ! raeburn  7727: 	    alert("'.$nofile_alert.'");
1.157     albertel 7728: 	    return false;
                   7729: 	}
1.565     raeburn  7730:         if (formname.courseid.value == "") {
1.579   ! raeburn  7731:             alert("'.$nocourseid_alert.'");
1.565     raeburn  7732:             return false;
                   7733:         }
1.157     albertel 7734: 	formname.submit();
                   7735:     }
1.565     raeburn  7736: 
                   7737:     function ToSyllabus() {
                   7738:         var cdom = '."'$dom'".';
                   7739:         var cnum = document.rules.courseid.value;
                   7740:         if (cdom == "" || cdom == null) {
                   7741:             return;
                   7742:         }
                   7743:         if (cnum == "" || cnum == null) {
                   7744:            return;
                   7745:         }
                   7746:         syllwin=window.open("/public/"+cdom+"/"+cnum+"/syllabus","LONCAPASyllabus",
                   7747:                             "height=350,width=350,scrollbars=yes,menubar=no");
                   7748:         return;
                   7749:     }
                   7750: 
1.157     albertel 7751: </script>
                   7752: 
1.566     raeburn  7753: <h3>'.&mt('Send scanned bubblesheet data to a course').'</h3>
                   7754: 
1.492     albertel 7755: <form enctype="multipart/form-data" action="/adm/grades" name="rules" method="post">
1.565     raeburn  7756: '.$default_form_data.
                   7757:   &Apache::lonhtmlcommon::start_pick_box().
                   7758:   &Apache::lonhtmlcommon::row_title(&mt('Course ID')).
                   7759:   '<input name="courseid" type="text" size="30" />'.$select_link.
                   7760:   &Apache::lonhtmlcommon::row_closure().
                   7761:   &Apache::lonhtmlcommon::row_title(&mt('Course Name')).
                   7762:   '<input name="coursename" type="text" size="30" />'.$syllabuslink.
                   7763:   &Apache::lonhtmlcommon::row_closure().
                   7764:   &Apache::lonhtmlcommon::row_title(&mt('Domain')).
                   7765:   '<input name="domainid" type="hidden" />'.$domdesc.
                   7766:   &Apache::lonhtmlcommon::row_closure().
                   7767:   &Apache::lonhtmlcommon::row_title(&mt('File to upload')).
                   7768:   '<input type="file" name="upfile" size="50" />'.
                   7769:   &Apache::lonhtmlcommon::row_closure(1).
                   7770:   &Apache::lonhtmlcommon::end_pick_box().'<br />
                   7771: 
1.492     albertel 7772: <input name="command" value="scantronupload_save" type="hidden" />
1.575     www      7773: <input type="button" onClick="javascript:checkUpload(this.form);" value="'.&mt('Upload Bubblesheet Data').'" />
1.157     albertel 7774: </form>
1.492     albertel 7775: ');
1.157     albertel 7776:     return '';
                   7777: }
                   7778: 
1.423     albertel 7779: 
1.157     albertel 7780: sub scantron_upload_scantron_data_save {
                   7781:     my($r)=@_;
1.324     albertel 7782:     my ($symb)=&get_symb($r,1);
1.182     albertel 7783:     my $doanotherupload=
                   7784: 	'<br /><form action="/adm/grades" method="post">'."\n".
                   7785: 	'<input type="hidden" name="command" value="scantronupload" />'."\n".
1.492     albertel 7786: 	'<input type="submit" name="submit" value="'.&mt('Do Another Upload').'" />'."\n".
1.182     albertel 7787: 	'</form>'."\n";
1.257     albertel 7788:     if (!&Apache::lonnet::allowed('usc',$env{'form.domainid'}) &&
1.162     albertel 7789: 	!&Apache::lonnet::allowed('usc',
1.257     albertel 7790: 			    $env{'form.domainid'}.'_'.$env{'form.courseid'})) {
1.575     www      7791: 	$r->print(&mt("You are not allowed to upload bubblesheet data to the requested course.")."<br />");
1.182     albertel 7792: 	if ($symb) {
1.324     albertel 7793: 	    $r->print(&show_grading_menu_form($symb));
1.182     albertel 7794: 	} else {
                   7795: 	    $r->print($doanotherupload);
                   7796: 	}
1.162     albertel 7797: 	return '';
                   7798:     }
1.257     albertel 7799:     my %coursedata=&Apache::lonnet::coursedescription($env{'form.domainid'}.'_'.$env{'form.courseid'});
1.568     raeburn  7800:     my $uploadedfile;
1.567     raeburn  7801:     $r->print('<h3>'.&mt("Uploading file to [_1]",$coursedata{'description'}).'</h3>');
1.257     albertel 7802:     if (length($env{'form.upfile'}) < 2) {
1.568     raeburn  7803:         $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>'));
1.183     albertel 7804:     } else {
1.568     raeburn  7805:         my $result = 
                   7806:             &Apache::lonnet::userfileupload('upfile','','scantron','','','',
                   7807:                                             $env{'form.courseid'},$env{'form.domainid'});
                   7808: 	if ($result =~ m{^/uploaded/}) {
1.567     raeburn  7809: 	    $r->print(&mt('[_1]Success:[_2] Successfully uploaded [_3] bytes of data into location: [_4]',
                   7810:                           '<span class="LC_success">','</span>',(length($env{'form.upfile'})-1),
                   7811: 			  '<span class="LC_filename">'.$result.'</span>'));
1.568     raeburn  7812:             ($uploadedfile) = ($result =~ m{/([^/]+)$});
1.567     raeburn  7813:             $r->print(&validate_uploaded_scantron_file($env{'form.domainid'},
1.568     raeburn  7814:                                                        $env{'form.courseid'},$uploadedfile));
1.210     albertel 7815: 	} else {
1.567     raeburn  7816: 	    $r->print(&mt('[_1]Error:[_2] An error ([_3]) occurred when attempting to upload the file, [_4]',
                   7817:                           '<span class="LC_error">','</span>',$result,
1.568     raeburn  7818: 			  '<span class="LC_filename">'.&HTML::Entities::encode($env{'form.upfile.filename'},'<>&"').'</span>'));
1.183     albertel 7819: 	}
                   7820:     }
1.174     albertel 7821:     if ($symb) {
1.209     ng       7822: 	$r->print(&scantron_selectphase($r,$uploadedfile));
1.174     albertel 7823:     } else {
1.182     albertel 7824: 	$r->print($doanotherupload);
1.174     albertel 7825:     }
1.157     albertel 7826:     return '';
                   7827: }
                   7828: 
1.567     raeburn  7829: sub validate_uploaded_scantron_file {
                   7830:     my ($cdom,$cname,$fname) = @_;
                   7831:     my $scanlines=&Apache::lonnet::getfile('/uploaded/'.$cdom.'/'.$cname.'/'.$fname);
                   7832:     my @lines;
                   7833:     if ($scanlines ne '-1') {
                   7834:         @lines=split("\n",$scanlines,-1);
                   7835:     }
                   7836:     my $output;
                   7837:     if (@lines) {
                   7838:         my (%counts,$max_match_format);
                   7839:         my ($max_match_count,$max_match_pct) = (0,0);
                   7840:         my $classlist = &Apache::loncoursedata::get_classlist($cdom,$cname);
                   7841:         my %idmap = &username_to_idmap($classlist);
                   7842:         foreach my $key (keys(%idmap)) {
                   7843:             my $lckey = lc($key);
                   7844:             $idmap{$lckey} = $idmap{$key};
                   7845:         }
                   7846:         my %unique_formats;
                   7847:         my @formatlines = &get_scantronformat_file();
                   7848:         foreach my $line (@formatlines) {
                   7849:             chomp($line);
                   7850:             my @config = split(/:/,$line);
                   7851:             my $idstart = $config[5];
                   7852:             my $idlength = $config[6];
                   7853:             if (($idstart ne '') && ($idlength > 0)) {
                   7854:                 if (ref($unique_formats{$idstart.':'.$idlength}) eq 'ARRAY') {
                   7855:                     push(@{$unique_formats{$idstart.':'.$idlength}},$config[0].':'.$config[1]); 
                   7856:                 } else {
                   7857:                     $unique_formats{$idstart.':'.$idlength} = [$config[0].':'.$config[1]];
                   7858:                 }
                   7859:             }
                   7860:         }
                   7861:         foreach my $key (keys(%unique_formats)) {
                   7862:             my ($idstart,$idlength) = split(':',$key);
                   7863:             %{$counts{$key}} = (
                   7864:                                'found'   => 0,
                   7865:                                'total'   => 0,
                   7866:                               );
                   7867:             foreach my $line (@lines) {
                   7868:                 next if ($line =~ /^#/);
                   7869:                 next if ($line =~ /^[\s\cz]*$/);
                   7870:                 my $id = substr($line,$idstart-1,$idlength);
                   7871:                 $id = lc($id);
                   7872:                 if (exists($idmap{$id})) {
                   7873:                     $counts{$key}{'found'} ++;
                   7874:                 }
                   7875:                 $counts{$key}{'total'} ++;
                   7876:             }
                   7877:             if ($counts{$key}{'total'}) {
                   7878:                 my $percent_match = (100*$counts{$key}{'found'})/($counts{$key}{'total'});
                   7879:                 if (($max_match_format eq '') || ($percent_match > $max_match_pct)) {
                   7880:                     $max_match_pct = $percent_match;
                   7881:                     $max_match_format = $key;
                   7882:                     $max_match_count = $counts{$key}{'total'};
                   7883:                 }
                   7884:             }
                   7885:         }
                   7886:         if (ref($unique_formats{$max_match_format}) eq 'ARRAY') {
                   7887:             my $format_descs;
                   7888:             my $numwithformat = @{$unique_formats{$max_match_format}};
                   7889:             for (my $i=0; $i<$numwithformat; $i++) {
                   7890:                 my ($name,$desc) = split(':',$unique_formats{$max_match_format}[$i]);
                   7891:                 if ($i<$numwithformat-2) {
                   7892:                     $format_descs .= '"<i>'.$desc.'</i>", ';
                   7893:                 } elsif ($i==$numwithformat-2) {
                   7894:                     $format_descs .= '"<i>'.$desc.'</i>" '.&mt('and').' ';
                   7895:                 } elsif ($i==$numwithformat-1) {
                   7896:                     $format_descs .= '"<i>'.$desc.'</i>"';
                   7897:                 }
                   7898:             }
                   7899:             my $showpct = sprintf("%.0f",$max_match_pct).'%';
                   7900:             $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).
                   7901:                        '<br />'.&mt('A low percentage of matches results from one of the following:').'<ul>'.
                   7902:                        '<li>'.&mt('The file was uploaded to the wrong course').'</li>'.
                   7903:                        '<li>'.&mt('The data are not in the format expected for the domain: [_1]',
                   7904:                                   '<i>'.$cdom.'</i>').'</li>'.
                   7905:                        '<li>'.&mt('Students did not bubble their IDs, or mis-bubbled them').'</li>'.
                   7906:                        '<li>'.&mt('The course roster is not up to date').'</li>'.
                   7907:                        '</ul>';
                   7908:         }
                   7909:     } else {
                   7910:         $output = '<span class="LC_warning">'.&mt('Uploaded file contained no data').'</span>';
                   7911:     }
                   7912:     return $output;
                   7913: }
                   7914: 
1.202     albertel 7915: sub valid_file {
                   7916:     my ($requested_file)=@_;
                   7917:     foreach my $filename (sort(&scantron_filenames())) {
                   7918: 	if ($requested_file eq $filename) { return 1; }
                   7919:     }
                   7920:     return 0;
                   7921: }
                   7922: 
                   7923: sub scantron_download_scantron_data {
                   7924:     my ($r)=@_;
1.324     albertel 7925:     my $default_form_data=&defaultFormData(&get_symb($r,1));
1.257     albertel 7926:     my $cname=$env{'course.'.$env{'request.course.id'}.'.num'};
                   7927:     my $cdom=$env{'course.'.$env{'request.course.id'}.'.domain'};
                   7928:     my $file=$env{'form.scantron_selectfile'};
1.202     albertel 7929:     if (! &valid_file($file)) {
1.492     albertel 7930: 	$r->print('
1.202     albertel 7931: 	<p>
1.492     albertel 7932: 	    '.&mt('The requested file name was invalid.').'
1.202     albertel 7933:         </p>
1.492     albertel 7934: ');
1.324     albertel 7935: 	$r->print(&show_grading_menu_form(&get_symb($r,1)));
1.202     albertel 7936: 	return;
                   7937:     }
                   7938:     my $orig='/uploaded/'.$cdom.'/'.$cname.'/scantron_orig_'.$file;
                   7939:     my $corrected='/uploaded/'.$cdom.'/'.$cname.'/scantron_corrected_'.$file;
                   7940:     my $skipped='/uploaded/'.$cdom.'/'.$cname.'/scantron_skipped_'.$file;
                   7941:     &Apache::lonnet::allowuploaded('/adm/grades',$orig);
                   7942:     &Apache::lonnet::allowuploaded('/adm/grades',$corrected);
                   7943:     &Apache::lonnet::allowuploaded('/adm/grades',$skipped);
1.492     albertel 7944:     $r->print('
1.202     albertel 7945:     <p>
1.492     albertel 7946: 	'.&mt('[_1]Original[_2] file as uploaded by the scantron office.',
                   7947: 	      '<a href="'.$orig.'">','</a>').'
1.202     albertel 7948:     </p>
                   7949:     <p>
1.492     albertel 7950: 	'.&mt('[_1]Corrections[_2], a file of corrected records that were used in grading.',
                   7951: 	      '<a href="'.$corrected.'">','</a>').'
1.202     albertel 7952:     </p>
                   7953:     <p>
1.492     albertel 7954: 	'.&mt('[_1]Skipped[_2], a file of records that were skipped.',
                   7955: 	      '<a href="'.$skipped.'">','</a>').'
1.202     albertel 7956:     </p>
1.492     albertel 7957: ');
1.324     albertel 7958:     $r->print(&show_grading_menu_form(&get_symb($r,1)));
1.202     albertel 7959:     return '';
                   7960: }
1.157     albertel 7961: 
1.523     raeburn  7962: sub checkscantron_results {
                   7963:     my ($r) = @_;
                   7964:     my ($symb)=&get_symb($r);
                   7965:     if (!$symb) {return '';}
                   7966:     my $grading_menu_button=&show_grading_menu_form($symb);
                   7967:     my $cid = $env{'request.course.id'};
1.542     raeburn  7968:     my %lettdig = &letter_to_digits();
1.523     raeburn  7969:     my $numletts = scalar(keys(%lettdig));
                   7970:     my $cnum = $env{'course.'.$cid.'.num'};
                   7971:     my $cdom = $env{'course.'.$cid.'.domain'};
                   7972:     my (undef, undef, $sequence) = &Apache::lonnet::decode_symb($env{'form.selectpage'});
                   7973:     my %record;
                   7974:     my %scantron_config =
                   7975:         &Apache::grades::get_scantron_config($env{'form.scantron_format'});
                   7976:     my ($scanlines,$scan_data)=&Apache::grades::scantron_getfile();
                   7977:     my $classlist=&Apache::loncoursedata::get_classlist();
                   7978:     my %idmap=&Apache::grades::username_to_idmap($classlist);
                   7979:     my $navmap=Apache::lonnavmaps::navmap->new();
                   7980:     my $map=$navmap->getResourceByUrl($sequence);
1.557     raeburn  7981:     my @resources=$navmap->retrieveResources($map,\&scantron_filter,1,0);
                   7982:     my (%grader_partids_by_symb,%grader_randomlists_by_symb);
                   7983:     &graders_resources_pass(\@resources,\%grader_partids_by_symb,                             \%grader_randomlists_by_symb);
                   7984: 
1.554     raeburn  7985:     my ($uname,$udom);
1.523     raeburn  7986:     my (%scandata,%lastname,%bylast);
                   7987:     $r->print('
                   7988: <form method="post" enctype="multipart/form-data" action="/adm/grades" name="checkscantron">'."\n");
                   7989: 
                   7990:     my @delayqueue;
                   7991:     my %completedstudents;
                   7992: 
                   7993:     my $count=&Apache::grades::get_todo_count($scanlines,$scan_data);
                   7994:     my %prog_state=&Apache::lonhtmlcommon::Create_PrgWin($r,'Scantron/Submissions Comparison Status',
                   7995:                                     'Progress of Scantron Data/Submission Records Comparison',$count,
                   7996:                                     'inline',undef,'checkscantron');
1.546     raeburn  7997:     my ($username,$domain,$started);
1.523     raeburn  7998: 
1.557     raeburn  7999:     &scantron_get_maxbubble();  # Need the bubble lines array to parse.
1.523     raeburn  8000: 
                   8001:     &Apache::lonhtmlcommon::Update_PrgWin($r,\%prog_state,
                   8002:                                           'Processing first student');
                   8003:     my $start=&Time::HiRes::time();
                   8004:     my $i=-1;
                   8005: 
                   8006:     while ($i<$scanlines->{'count'}) {
                   8007:         ($username,$domain,$uname)=('','','');
                   8008:         $i++;
                   8009:         my $line=&Apache::grades::scantron_get_line($scanlines,$scan_data,$i);
                   8010:         if ($line=~/^[\s\cz]*$/) { next; }
                   8011:         if ($started) {
                   8012:             &Apache::lonhtmlcommon::Increment_PrgWin($r,\%prog_state,
                   8013:                                                      'last student');
                   8014:         }
                   8015:         $started=1;
                   8016:         my $scan_record=
                   8017:             &Apache::grades::scantron_parse_scanline($line,$i,\%scantron_config,
                   8018:                                                      $scan_data);
                   8019:         unless ($uname=&Apache::grades::scantron_find_student($scan_record,$scan_data,
                   8020:                                                               \%idmap,$i)) {
                   8021:             &Apache::grades::scantron_add_delay(\@delayqueue,$line,
                   8022:                                 'Unable to find a student that matches',1);
                   8023:             next;
                   8024:         }
                   8025:         if (exists $completedstudents{$uname}) {
                   8026:             &Apache::grades::scantron_add_delay(\@delayqueue,$line,
                   8027:                                 'Student '.$uname.' has multiple sheets',2);
                   8028:             next;
                   8029:         }
                   8030:         my $pid = $scan_record->{'scantron.ID'};
                   8031:         $lastname{$pid} = $scan_record->{'scantron.LastName'};
                   8032:         push(@{$bylast{$lastname{$pid}}},$pid);
                   8033:         my $lastpos = $env{'form.scantron_maxbubble'}*$scantron_config{'Qlength'};
                   8034:         $scandata{$pid} = substr($line,$scantron_config{'Qstart'}-1,$lastpos);
                   8035:         chomp($scandata{$pid});
                   8036:         $scandata{$pid} =~ s/\r$//;
                   8037:         ($username,$domain)=split(/:/,$uname);
                   8038:         my $counter = -1;
                   8039:         foreach my $resource (@resources) {
1.557     raeburn  8040:             my $parts;
1.554     raeburn  8041:             my $ressymb = $resource->symb();
1.557     raeburn  8042:             if ((exists($grader_randomlists_by_symb{$ressymb})) ||
                   8043:                 (ref($grader_partids_by_symb{$ressymb}) ne 'ARRAY')) {
                   8044:                 (my $analysis,$parts) =
                   8045:                     &scantron_partids_tograde($resource,$env{'request.course.id'},$username,$domain);
                   8046:             } else {
                   8047:                 $parts = $grader_partids_by_symb{$ressymb};
                   8048:             }
1.542     raeburn  8049:             ($counter,my $recording) =
                   8050:                 &verify_scantron_grading($resource,$domain,$username,$cid,$counter,
1.554     raeburn  8051:                                          $scandata{$pid},$parts,
1.542     raeburn  8052:                                          \%scantron_config,\%lettdig,$numletts);
                   8053:             $record{$pid} .= $recording;
1.523     raeburn  8054:         }
                   8055:     }
                   8056:     &Apache::lonhtmlcommon::Close_PrgWin($r,\%prog_state);
                   8057:     $r->print('<br />');
                   8058:     my ($okstudents,$badstudents,$numstudents,$passed,$failed);
                   8059:     $passed = 0;
                   8060:     $failed = 0;
                   8061:     $numstudents = 0;
                   8062:     foreach my $last (sort(keys(%bylast))) {
                   8063:         if (ref($bylast{$last}) eq 'ARRAY') {
                   8064:             foreach my $pid (sort(@{$bylast{$last}})) {
                   8065:                 my $showscandata = $scandata{$pid};
                   8066:                 my $showrecord = $record{$pid};
                   8067:                 $showscandata =~ s/\s/&nbsp;/g;
                   8068:                 $showrecord =~ s/\s/&nbsp;/g;
                   8069:                 if ($scandata{$pid} eq $record{$pid}) {
                   8070:                     my $css_class = ($passed % 2)?'LC_odd_row':'LC_even_row';
                   8071:                     $okstudents .= '<tr class="'.$css_class.'">'.
                   8072: '<td>'.&mt('Scantron').'</td><td>'.$showscandata.'</td><td rowspan="2">'.$last.'</td><td rowspan="2">'.$pid.'</td>'."\n".
                   8073: '</tr>'."\n".
                   8074: '<tr class="'.$css_class.'">'."\n".
                   8075: '<td>Submissions</td><td>'.$showrecord.'</td></tr>'."\n";
                   8076:                     $passed ++;
                   8077:                 } else {
                   8078:                     my $css_class = ($failed % 2)?'LC_odd_row':'LC_even_row';
                   8079:                     $badstudents .= '<tr class="'.$css_class.'"><td>'.&mt('Scantron').'</td><td><span class="LC_nobreak">'.$scandata{$pid}.'</span></td><td rowspan="2">'.$last.'</td><td rowspan="2">'.$pid.'</td>'."\n".
                   8080: '</tr>'."\n".
                   8081: '<tr class="'.$css_class.'">'."\n".
                   8082: '<td>Submissions</td><td><span class="LC_nobreak">'.$record{$pid}.'</span></td>'."\n".
                   8083: '</tr>'."\n";
                   8084:                     $failed ++;
                   8085:                 }
                   8086:                 $numstudents ++;
                   8087:             }
                   8088:         }
                   8089:     }
1.572     www      8090:     $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>');
1.523     raeburn  8091:     $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>');
                   8092:     if ($passed) {
1.572     www      8093:         $r->print(&mt('Students with exact correspondence between bubblesheet data and submissions are as follows:').'<br /><br />');
1.523     raeburn  8094:         $r->print(&Apache::loncommon::start_data_table()."\n".
                   8095:                  &Apache::loncommon::start_data_table_header_row()."\n".
                   8096:                  '<th>'.&mt('Source').'</th><th>'.&mt('Bubble records').'</th><th>'.&mt('Name').'</th><th>'.&mt('ID').'</th>'.
                   8097:                  &Apache::loncommon::end_data_table_header_row()."\n".
                   8098:                  $okstudents."\n".
                   8099:                  &Apache::loncommon::end_data_table().'<br />');
                   8100:     }
                   8101:     if ($failed) {
1.572     www      8102:         $r->print(&mt('Students with differences between bubblesheet data and submissions are as follows:').'<br /><br />');
1.523     raeburn  8103:         $r->print(&Apache::loncommon::start_data_table()."\n".
                   8104:                  &Apache::loncommon::start_data_table_header_row()."\n".
                   8105:                  '<th>'.&mt('Source').'</th><th>'.&mt('Bubble records').'</th><th>'.&mt('Name').'</th><th>'.&mt('ID').'</th>'.
                   8106:                  &Apache::loncommon::end_data_table_header_row()."\n".
                   8107:                  $badstudents."\n".
                   8108:                  &Apache::loncommon::end_data_table()).'<br />'.
1.572     www      8109:                  &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.');  
1.523     raeburn  8110:     }
                   8111:     $r->print('</form><br />'.$grading_menu_button);
                   8112:     return;
                   8113: }
                   8114: 
1.542     raeburn  8115: sub verify_scantron_grading {
1.554     raeburn  8116:     my ($resource,$domain,$username,$cid,$counter,$scandata,$partids,
1.542     raeburn  8117:         $scantron_config,$lettdig,$numletts) = @_;
                   8118:     my ($record,%expected,%startpos);
                   8119:     return ($counter,$record) if (!ref($resource));
                   8120:     return ($counter,$record) if (!$resource->is_problem());
                   8121:     my $symb = $resource->symb();
1.554     raeburn  8122:     return ($counter,$record) if (ref($partids) ne 'ARRAY');
                   8123:     foreach my $part_id (@{$partids}) {
1.542     raeburn  8124:         $counter ++;
                   8125:         $expected{$part_id} = 0;
                   8126:         if ($env{"form.scantron.sub_bubblelines.$counter"}) {
                   8127:             my @sub_lines = split(/,/,$env{"form.scantron.sub_bubblelines.$counter"});
                   8128:             foreach my $item (@sub_lines) {
                   8129:                 $expected{$part_id} += $item;
                   8130:             }
                   8131:         } else {
                   8132:             $expected{$part_id} = $env{"form.scantron.bubblelines.$counter"};
                   8133:         }
                   8134:         $startpos{$part_id} = $env{"form.scantron.first_bubble_line.$counter"};
                   8135:     }
                   8136:     if ($symb) {
                   8137:         my %recorded;
                   8138:         my (%returnhash) = &Apache::lonnet::restore($symb,$cid,$domain,$username);
                   8139:         if ($returnhash{'version'}) {
                   8140:             my %lasthash=();
                   8141:             my $version;
                   8142:             for ($version=1;$version<=$returnhash{'version'};$version++) {
                   8143:                 foreach my $key (sort(split(/\:/,$returnhash{$version.':keys'}))) {
                   8144:                     $lasthash{$key}=$returnhash{$version.':'.$key};
                   8145:                 }
                   8146:             }
                   8147:             foreach my $key (keys(%lasthash)) {
                   8148:                 if ($key =~ /\.scantron$/) {
                   8149:                     my $value = &unescape($lasthash{$key});
                   8150:                     my ($part_id) = ($key =~ /^resource\.(.+)\.scantron$/);
                   8151:                     if ($value eq '') {
                   8152:                         for (my $i=0; $i<$expected{$part_id}; $i++) {
                   8153:                             for (my $j=0; $j<$scantron_config->{'length'}; $j++) {
                   8154:                                 $recorded{$part_id} .= $scantron_config->{'Qoff'};
                   8155:                             }
                   8156:                         }
                   8157:                     } else {
                   8158:                         my @tocheck;
                   8159:                         my @items = split(//,$value);
                   8160:                         if (($scantron_config->{'Qon'} eq 'letter') ||
                   8161:                             ($scantron_config->{'Qon'} eq 'number')) {
                   8162:                             if (@items < $expected{$part_id}) {
                   8163:                                 my $fragment = substr($scandata,$startpos{$part_id},$expected{$part_id});
                   8164:                                 my @singles = split(//,$fragment);
                   8165:                                 foreach my $pos (@singles) {
                   8166:                                     if ($pos eq ' ') {
                   8167:                                         push(@tocheck,$pos);
                   8168:                                     } else {
                   8169:                                         my $next = shift(@items);
                   8170:                                         push(@tocheck,$next);
                   8171:                                     }
                   8172:                                 }
                   8173:                             } else {
                   8174:                                 @tocheck = @items;
                   8175:                             }
                   8176:                             foreach my $letter (@tocheck) {
                   8177:                                 if ($scantron_config->{'Qon'} eq 'letter') {
                   8178:                                     if ($letter !~ /^[A-J]$/) {
                   8179:                                         $letter = $scantron_config->{'Qoff'};
                   8180:                                     }
                   8181:                                     $recorded{$part_id} .= $letter;
                   8182:                                 } elsif ($scantron_config->{'Qon'} eq 'number') {
                   8183:                                     my $digit;
                   8184:                                     if ($letter !~ /^[A-J]$/) {
                   8185:                                         $digit = $scantron_config->{'Qoff'};
                   8186:                                     } else {
                   8187:                                         $digit = $lettdig->{$letter};
                   8188:                                     }
                   8189:                                     $recorded{$part_id} .= $digit;
                   8190:                                 }
                   8191:                             }
                   8192:                         } else {
                   8193:                             @tocheck = @items;
                   8194:                             for (my $i=0; $i<$expected{$part_id}; $i++) {
                   8195:                                 my $curr_sub = shift(@tocheck);
                   8196:                                 my $digit;
                   8197:                                 if ($curr_sub =~ /^[A-J]$/) {
                   8198:                                     $digit = $lettdig->{$curr_sub}-1;
                   8199:                                 }
                   8200:                                 if ($curr_sub eq 'J') {
                   8201:                                     $digit += scalar($numletts);
                   8202:                                 }
                   8203:                                 for (my $j=0; $j<$scantron_config->{'Qlength'}; $j++) {
                   8204:                                     if ($j == $digit) {
                   8205:                                         $recorded{$part_id} .= $scantron_config->{'Qon'};
                   8206:                                     } else {
                   8207:                                         $recorded{$part_id} .= $scantron_config->{'Qoff'};
                   8208:                                     }
                   8209:                                 }
                   8210:                             }
                   8211:                         }
                   8212:                     }
                   8213:                 }
                   8214:             }
                   8215:         }
1.554     raeburn  8216:         foreach my $part_id (@{$partids}) {
1.542     raeburn  8217:             if ($recorded{$part_id} eq '') {
                   8218:                 for (my $i=0; $i<$expected{$part_id}; $i++) {
                   8219:                     for (my $j=0; $j<$scantron_config->{'Qlength'}; $j++) {
                   8220:                         $recorded{$part_id} .= $scantron_config->{'Qoff'};
                   8221:                     }
                   8222:                 }
                   8223:             }
                   8224:             $record .= $recorded{$part_id};
                   8225:         }
                   8226:     }
                   8227:     return ($counter,$record);
                   8228: }
                   8229: 
                   8230: sub letter_to_digits { 
                   8231:     my %lettdig = (
                   8232:                     A => 1,
                   8233:                     B => 2,
                   8234:                     C => 3,
                   8235:                     D => 4,
                   8236:                     E => 5,
                   8237:                     F => 6,
                   8238:                     G => 7,
                   8239:                     H => 8,
                   8240:                     I => 9,
                   8241:                     J => 0,
                   8242:                   );
                   8243:     return %lettdig;
                   8244: }
                   8245: 
1.423     albertel 8246: 
1.75      albertel 8247: #-------- end of section for handling grading scantron forms -------
                   8248: #
                   8249: #-------------------------------------------------------------------
                   8250: 
1.72      ng       8251: #-------------------------- Menu interface -------------------------
                   8252: #
                   8253: #--- Show a Grading Menu button - Calls the next routine ---
                   8254: sub show_grading_menu_form {
1.324     albertel 8255:     my ($symb)=@_;
1.125     ng       8256:     my $result.='<br /><form action="/adm/grades" method="post">'."\n".
1.418     albertel 8257: 	'<input type="hidden" name="symb" value="'.&Apache::lonenc::check_encrypt($symb).'" />'."\n".
1.257     albertel 8258: 	'<input type="hidden" name="saveState"  value="'.$env{'form.saveState'}.'" />'."\n".
1.72      ng       8259: 	'<input type="hidden" name="command" value="gradingmenu" />'."\n".
1.478     albertel 8260: 	'<input type="submit" name="submit" value="'.&mt('Grading Menu').'" />'."\n".
1.72      ng       8261: 	'</form>'."\n";
                   8262:     return $result;
                   8263: }
                   8264: 
1.77      ng       8265: # -- Retrieve choices for grading form
                   8266: sub savedState {
                   8267:     my %savedState = ();
1.257     albertel 8268:     if ($env{'form.saveState'}) {
                   8269: 	foreach (split(/:/,$env{'form.saveState'})) {
1.77      ng       8270: 	    my ($key,$value) = split(/=/,$_,2);
                   8271: 	    $savedState{$key} = $value;
                   8272: 	}
                   8273:     }
                   8274:     return \%savedState;
                   8275: }
1.76      ng       8276: 
1.443     banghart 8277: sub grading_menu {
                   8278:     my ($request) = @_;
                   8279:     my ($symb)=&get_symb($request);
                   8280:     if (!$symb) {return '';}
                   8281:     my $probTitle = &Apache::lonnet::gettitle($symb);
                   8282:     my ($table,undef,$hdgrade) = &showResourceInfo($symb,$probTitle);
                   8283: 
1.444     banghart 8284:     $request->print($table);
1.443     banghart 8285:     my %fields = ('symb'=>&Apache::lonenc::check_encrypt($symb),
                   8286:                   'handgrade'=>$hdgrade,
                   8287:                   'probTitle'=>$probTitle,
                   8288:                   'command'=>'submit_options',
                   8289:                   'saveState'=>"",
                   8290:                   'gradingMenu'=>1,
                   8291:                   'showgrading'=>"yes");
1.538     schulted 8292:     
                   8293:     my $url1 = &Apache::lonhtmlcommon::build_url('grades/',\%fields);
                   8294:     
1.443     banghart 8295:     $fields{'command'} = 'csvform';
1.538     schulted 8296:     my $url2 = &Apache::lonhtmlcommon::build_url('grades/',\%fields);
                   8297:     
1.443     banghart 8298:     $fields{'command'} = 'processclicker';
1.538     schulted 8299:     my $url3 = &Apache::lonhtmlcommon::build_url('grades/',\%fields);
                   8300:     
1.443     banghart 8301:     $fields{'command'} = 'scantron_selectphase';
1.538     schulted 8302:     my $url4 = &Apache::lonhtmlcommon::build_url('grades/',\%fields);
                   8303:     
                   8304:     my @menu = ({	categorytitle=>'Course Grading',
                   8305:             items =>[
                   8306:                         {	linktext => 'Manual Grading/View Submissions',
                   8307:                     		url => $url1,
                   8308:                     		permission => 'F',
                   8309:                     		icon => 'edit-find-replace.png',
                   8310:                     		linktitle => 'Start the process of hand grading submissions.'
                   8311:                         },
                   8312:                 	    {	linktext => 'Upload Scores',
                   8313:                     		url => $url2,
                   8314:                     		permission => 'F',
                   8315:                     		icon => 'uploadscores.png',
                   8316:                     		linktitle => 'Specify a file containing the class scores for current resource.'
                   8317:                 	    },
                   8318:                 	    {	linktext => 'Process Clicker',
                   8319:                     		url => $url3,
                   8320:                     		permission => 'F',
                   8321:                     		icon => 'addClickerInfoFile.png',
                   8322:                     		linktitle => 'Specify a file containing the clicker information for this resource.'
                   8323:                 	    },
                   8324:                 	    {	linktext => 'Grade/Manage/Review Scantron Forms',
                   8325:                     		url => $url4,
                   8326:                     		permission => 'F',
                   8327:                     		icon => 'stat.png',
                   8328:                     		linktitle => 'Grade scantron exams, upload/download scantron data files, and review previously graded scantron exams.'
                   8329:                 	    }
                   8330:                     ]
                   8331:             });
                   8332: 
                   8333:     #$fields{'command'} = 'verify';
                   8334:     #$url = &Apache::lonhtmlcommon::build_url('grades/',\%fields);
1.443     banghart 8335:     #
                   8336:     # Create the menu
                   8337:     my $Str;
1.444     banghart 8338:     # $Str .= '<h2>'.&mt('Please select a grading task').'</h2>';
1.445     banghart 8339:     $Str .= '<form method="post" action="" name="gradingMenu">';
                   8340:     $Str .= '<input type="hidden" name="command" value="" />'.
                   8341:     	'<input type="hidden" name="symb"        value="'.&Apache::lonenc::check_encrypt($symb).'" />'."\n".
                   8342: 	'<input type="hidden" name="handgrade"   value="'.$hdgrade.'" />'."\n".
1.476     albertel 8343: 	'<input type="hidden" name="probTitle"   value="'.$probTitle.'" />'."\n".
1.445     banghart 8344: 	'<input type="hidden" name="saveState"   value="" />'."\n".
                   8345: 	'<input type="hidden" name="gradingMenu" value="1" />'."\n".
                   8346: 	'<input type="hidden" name="showgrading" value="yes" />'."\n";
                   8347: 
1.538     schulted 8348:     $Str .= Apache::lonhtmlcommon::generate_menu(@menu);
                   8349:     #$menudata->{'jscript'}
                   8350:     $Str .='<hr /><input type="button" value="'.&mt('Verify Receipt').'" '.
                   8351:         ' onClick="javascript:checkChoice(document.forms.gradingMenu,\'5\',\'verify\')" '.
                   8352:         ' /> '.
                   8353:         &Apache::lonnet::recprefix($env{'request.course.id'}).
                   8354:         '-<input type="text" name="receipt" size="4" onChange="javascript:checkReceiptNo(this.form,\'OK\')" />';
                   8355: 
1.444     banghart 8356:     $Str .="</form>\n";
1.539     riegler  8357:     my $receiptalert = &mt("Please enter a receipt number given by a student in the receipt box.");
1.443     banghart 8358:     $request->print(<<GRADINGMENUJS);
                   8359: <script type="text/javascript" language="javascript">
                   8360:     function checkChoice(formname,val,cmdx) {
                   8361: 	if (val <= 2) {
                   8362: 	    var cmd = radioSelection(formname.radioChoice);
                   8363: 	    var cmdsave = cmd;
                   8364: 	} else {
                   8365: 	    cmd = cmdx;
                   8366: 	    cmdsave = 'submission';
                   8367: 	}
                   8368: 	formname.command.value = cmd;
                   8369: 	if (val < 5) formname.submit();
                   8370: 	if (val == 5) {
1.458     banghart 8371: 	    if (!checkReceiptNo(formname,'notOK')) { 
                   8372: 	        return false;
                   8373: 	    } else {
                   8374: 	        formname.submit();
                   8375: 	    }
1.445     banghart 8376: 	}
                   8377:     }
1.443     banghart 8378: 
                   8379:     function checkReceiptNo(formname,nospace) {
                   8380: 	var receiptNo = formname.receipt.value;
                   8381: 	var checkOpt = false;
                   8382: 	if (nospace == "OK" && isNaN(receiptNo)) {checkOpt = true;}
                   8383: 	if (nospace == "notOK" && (isNaN(receiptNo) || receiptNo == "")) {checkOpt = true;}
                   8384: 	if (checkOpt) {
1.539     riegler  8385: 	    alert("$receiptalert");
1.443     banghart 8386: 	    formname.receipt.value = "";
                   8387: 	    formname.receipt.focus();
                   8388: 	    return false;
                   8389: 	}
                   8390: 	return true;
                   8391:     }
                   8392: </script>
                   8393: GRADINGMENUJS
                   8394:     &commonJSfunctions($request);
                   8395:     return $Str;    
                   8396: }
                   8397: 
                   8398: 
                   8399: #--- Displays the submissions first page -------
                   8400: sub submit_options {
1.72      ng       8401:     my ($request) = @_;
1.324     albertel 8402:     my ($symb)=&get_symb($request);
1.72      ng       8403:     if (!$symb) {return '';}
1.76      ng       8404:     my $probTitle = &Apache::lonnet::gettitle($symb);
1.72      ng       8405: 
1.539     riegler  8406:     my $receiptalert = &mt("Please enter a receipt number given by a student in the receipt box."); 
1.72      ng       8407:     $request->print(<<GRADINGMENUJS);
                   8408: <script type="text/javascript" language="javascript">
1.116     ng       8409:     function checkChoice(formname,val,cmdx) {
                   8410: 	if (val <= 2) {
                   8411: 	    var cmd = radioSelection(formname.radioChoice);
1.118     ng       8412: 	    var cmdsave = cmd;
1.116     ng       8413: 	} else {
                   8414: 	    cmd = cmdx;
1.118     ng       8415: 	    cmdsave = 'submission';
1.116     ng       8416: 	}
                   8417: 	formname.command.value = cmd;
1.118     ng       8418: 	formname.saveState.value = "saveCmd="+cmdsave+":saveSec="+pullDownSelection(formname.section)+
1.145     albertel 8419: 	    ":saveSub="+pullDownSelection(formname.submitonly)+":saveStatus="+pullDownSelection(formname.Status);
1.116     ng       8420: 	if (val < 5) formname.submit();
                   8421: 	if (val == 5) {
1.72      ng       8422: 	    if (!checkReceiptNo(formname,'notOK')) { return false;}
                   8423: 	    formname.submit();
                   8424: 	}
1.238     albertel 8425: 	if (val < 7) formname.submit();
1.72      ng       8426:     }
                   8427: 
                   8428:     function checkReceiptNo(formname,nospace) {
                   8429: 	var receiptNo = formname.receipt.value;
                   8430: 	var checkOpt = false;
                   8431: 	if (nospace == "OK" && isNaN(receiptNo)) {checkOpt = true;}
                   8432: 	if (nospace == "notOK" && (isNaN(receiptNo) || receiptNo == "")) {checkOpt = true;}
                   8433: 	if (checkOpt) {
1.539     riegler  8434: 	    alert("$receiptalert");
1.72      ng       8435: 	    formname.receipt.value = "";
                   8436: 	    formname.receipt.focus();
                   8437: 	    return false;
                   8438: 	}
                   8439: 	return true;
                   8440:     }
                   8441: </script>
                   8442: GRADINGMENUJS
1.118     ng       8443:     &commonJSfunctions($request);
1.324     albertel 8444:     my ($table,undef,$hdgrade) = &showResourceInfo($symb,$probTitle);
1.473     albertel 8445:     my $result;
1.76      ng       8446:     my (undef,$sections) = &getclasslist('all','0');
1.77      ng       8447:     my $savedState = &savedState();
1.118     ng       8448:     my $saveCmd = ($$savedState{'saveCmd'} eq '' ? 'submission' : $$savedState{'saveCmd'});
1.77      ng       8449:     my $saveSec = ($$savedState{'saveSec'} eq '' ? 'all' : $$savedState{'saveSec'});
1.118     ng       8450:     my $saveSub = ($$savedState{'saveSub'} eq '' ? 'all' : $$savedState{'saveSub'});
1.77      ng       8451:     my $saveStatus = ($$savedState{'saveStatus'} eq '' ? 'Active' : $$savedState{'saveStatus'});
1.72      ng       8452: 
1.533     bisitz   8453:     # Preselect sections
                   8454:     my $selsec="";
                   8455:     if (ref($sections)) {
                   8456:         foreach my $section (sort(@$sections)) {
                   8457:             $selsec.='<option value="'.$section.'" '.
                   8458:                 ($saveSec eq $section ? 'selected="selected"':'').'>'.$section.'</option>'."\n";
                   8459:         }
                   8460:     }
                   8461: 
1.72      ng       8462:     $result.='<form action="/adm/grades" method="post" name="gradingMenu">'."\n".
1.418     albertel 8463: 	'<input type="hidden" name="symb"        value="'.&Apache::lonenc::check_encrypt($symb).'" />'."\n".
1.72      ng       8464: 	'<input type="hidden" name="handgrade"   value="'.$hdgrade.'" />'."\n".
                   8465: 	'<input type="hidden" name="probTitle"   value="'.$probTitle.'" />'."\n".
1.116     ng       8466: 	'<input type="hidden" name="command"     value="" />'."\n".
1.77      ng       8467: 	'<input type="hidden" name="saveState"   value="" />'."\n".
1.124     ng       8468: 	'<input type="hidden" name="gradingMenu" value="1" />'."\n".
1.72      ng       8469: 	'<input type="hidden" name="showgrading" value="yes" />'."\n";
                   8470: 
1.472     albertel 8471:     $result.='
1.533     bisitz   8472: <h2>
                   8473:   '.&mt('Grade Current Resource').'
                   8474: </h2>
                   8475: <div>
                   8476:   '.$table.'
                   8477: </div>
                   8478: 
1.537     harmsja  8479: <div class="LC_columnSection">
                   8480:   
1.533     bisitz   8481:     <fieldset>
                   8482:       <legend>
                   8483:        '.&mt('Sections').'
                   8484:       </legend>
                   8485:       <select name="section" multiple="multiple" size="5">'."\n";
                   8486:     $result.= $selsec;
1.401     albertel 8487:     $result.= '<option value="all" '.($saveSec eq 'all' ? 'selected="selected"' : ''). '>all</option></select> &nbsp; ';
1.472     albertel 8488:     $result.='
1.533     bisitz   8489:     </fieldset>
1.537     harmsja  8490:   
1.533     bisitz   8491:     <fieldset>
                   8492:       <legend>
                   8493:         '.&mt('Groups').'
                   8494:       </legend>
                   8495:       '.&Apache::lonstatistics::GroupSelect('group','multiple',5).'
                   8496:     </fieldset>
1.537     harmsja  8497:   
1.533     bisitz   8498:     <fieldset>
                   8499:       <legend>
                   8500:         '.&mt('Access Status').'
                   8501:       </legend>
                   8502:       '.&Apache::lonhtmlcommon::StatusOptions($saveStatus,undef,5,undef,'mult').'
                   8503:     </fieldset>
1.537     harmsja  8504:   
1.533     bisitz   8505:     <fieldset>
                   8506:       <legend>
                   8507:         '.&mt('Submission Status').'
                   8508:       </legend>
                   8509:       <select name="submitonly" size="5">
1.473     albertel 8510: 	         <option value="yes" '.      ($saveSub eq 'yes'       ? 'selected="selected"' : '').'>'.&mt('with submissions').'</option>
                   8511: 	         <option value="queued" '.   ($saveSub eq 'queued'    ? 'selected="selected"' : '').'>'.&mt('in grading queue').'</option>
                   8512: 	         <option value="graded" '.   ($saveSub eq 'graded'    ? 'selected="selected"' : '').'>'.&mt('with ungraded submissions').'</option>
                   8513: 	         <option value="incorrect" '.($saveSub eq 'incorrect' ? 'selected="selected"' : '').'>'.&mt('with incorrect submissions').'</option>
                   8514:                  <option value="all" '.      ($saveSub eq 'all'       ? 'selected="selected"' : '').'>'.&mt('with any status').'</option>
1.533     bisitz   8515:       </select>
                   8516:     </fieldset>
1.537     harmsja  8517:   
1.533     bisitz   8518: </div>
                   8519: 
                   8520: <br />
                   8521:           <div>
                   8522:             <div>
1.473     albertel 8523:               <label>
                   8524:                 <input type="radio" name="radioChoice" value="submission" '.
                   8525:                   ($saveCmd eq 'submission' ? 'checked="checked"' : '').' /> '.
                   8526:              &mt('Select individual students to grade and view submissions.').'
                   8527: 	      </label> 
                   8528:             </div>
1.533     bisitz   8529:             <div>
1.473     albertel 8530: 	      <label>
                   8531:                 <input type="radio" name="radioChoice" value="viewgrades" '.
                   8532:                   ($saveCmd eq 'viewgrades' ? 'checked="checked"' : '').' /> '.
                   8533:                     &mt('Grade all selected students in a grading table.').'
                   8534:               </label>
                   8535:             </div>
1.533     bisitz   8536:             <div>
1.539     riegler  8537: 	      <input type="button" onClick="javascript:checkChoice(this.form,\'2\');" value="'.&mt('Next').' &rarr;" />
1.473     albertel 8538:             </div>
1.472     albertel 8539:           </div>
1.533     bisitz   8540: 
                   8541: 
1.473     albertel 8542:         <h2>
                   8543:          '.&mt('Grade Complete Folder for One Student').'
                   8544:         </h2>
1.533     bisitz   8545:         <div>
                   8546:             <div>
1.473     albertel 8547:               <label>
                   8548:                 <input type="radio" name="radioChoice" value="pickStudentPage" '.
                   8549: 	  ($saveCmd eq 'pickStudentPage' ? 'checked="checked"' : '').' /> '.
                   8550:   &mt('The <b>complete</b> page/sequence/folder: For one student').'
                   8551:               </label>
                   8552:             </div>
1.533     bisitz   8553:             <div>
1.539     riegler  8554: 	      <input type="button" onClick="javascript:checkChoice(this.form,\'2\');" value="'.&mt('Next').' &rarr;" />
1.473     albertel 8555:             </div>
1.472     albertel 8556:         </div>
                   8557:   </form>';
1.499     albertel 8558:     $result .= &show_grading_menu_form($symb);
1.44      ng       8559:     return $result;
1.2       albertel 8560: }
                   8561: 
1.285     albertel 8562: sub reset_perm {
                   8563:     undef(%perm);
                   8564: }
                   8565: 
                   8566: sub init_perm {
                   8567:     &reset_perm();
1.300     albertel 8568:     foreach my $test_perm ('vgr','mgr','opa') {
                   8569: 
                   8570: 	my $scope = $env{'request.course.id'};
                   8571: 	if (!($perm{$test_perm}=&Apache::lonnet::allowed($test_perm,$scope))) {
                   8572: 
                   8573: 	    $scope .= '/'.$env{'request.course.sec'};
                   8574: 	    if ( $perm{$test_perm}=
                   8575: 		 &Apache::lonnet::allowed($test_perm,$scope)) {
                   8576: 		$perm{$test_perm.'_section'}=$env{'request.course.sec'};
                   8577: 	    } else {
                   8578: 		delete($perm{$test_perm});
                   8579: 	    }
1.285     albertel 8580: 	}
                   8581:     }
                   8582: }
                   8583: 
1.400     www      8584: sub gather_clicker_ids {
1.408     albertel 8585:     my %clicker_ids;
1.400     www      8586: 
                   8587:     my $classlist = &Apache::loncoursedata::get_classlist();
                   8588: 
                   8589:     # Set up a couple variables.
1.407     albertel 8590:     my $username_idx = &Apache::loncoursedata::CL_SNAME();
                   8591:     my $domain_idx   = &Apache::loncoursedata::CL_SDOM();
1.438     www      8592:     my $status_idx   = &Apache::loncoursedata::CL_STATUS();
1.400     www      8593: 
1.407     albertel 8594:     foreach my $student (keys(%$classlist)) {
1.438     www      8595:         if ($classlist->{$student}->[$status_idx] ne 'Active') { next; }
1.407     albertel 8596:         my $username = $classlist->{$student}->[$username_idx];
                   8597:         my $domain   = $classlist->{$student}->[$domain_idx];
1.400     www      8598:         my $clickers =
1.408     albertel 8599: 	    (&Apache::lonnet::userenvironment($domain,$username,'clickers'))[1];
1.400     www      8600:         foreach my $id (split(/\,/,$clickers)) {
1.414     www      8601:             $id=~s/^[\#0]+//;
1.421     www      8602:             $id=~s/[\-\:]//g;
1.407     albertel 8603:             if (exists($clicker_ids{$id})) {
1.408     albertel 8604: 		$clicker_ids{$id}.=','.$username.':'.$domain;
1.400     www      8605:             } else {
1.408     albertel 8606: 		$clicker_ids{$id}=$username.':'.$domain;
1.400     www      8607:             }
                   8608:         }
                   8609:     }
1.407     albertel 8610:     return %clicker_ids;
1.400     www      8611: }
                   8612: 
1.402     www      8613: sub gather_adv_clicker_ids {
1.408     albertel 8614:     my %clicker_ids;
1.402     www      8615:     my $cnum=$env{'course.'.$env{'request.course.id'}.'.num'};
                   8616:     my $cdom=$env{'course.'.$env{'request.course.id'}.'.domain'};
                   8617:     my %coursepersonnel=&Apache::lonnet::get_course_adv_roles($cdom.'/'.$cnum);
1.409     albertel 8618:     foreach my $element (sort(keys(%coursepersonnel))) {
1.402     www      8619:         foreach my $person (split(/\,/,$coursepersonnel{$element})) {
                   8620:             my ($puname,$pudom)=split(/\:/,$person);
                   8621:             my $clickers =
1.408     albertel 8622: 		(&Apache::lonnet::userenvironment($pudom,$puname,'clickers'))[1];
1.405     www      8623:             foreach my $id (split(/\,/,$clickers)) {
1.414     www      8624: 		$id=~s/^[\#0]+//;
1.421     www      8625:                 $id=~s/[\-\:]//g;
1.408     albertel 8626: 		if (exists($clicker_ids{$id})) {
                   8627: 		    $clicker_ids{$id}.=','.$puname.':'.$pudom;
                   8628: 		} else {
                   8629: 		    $clicker_ids{$id}=$puname.':'.$pudom;
                   8630: 		}
1.405     www      8631:             }
1.402     www      8632:         }
                   8633:     }
1.407     albertel 8634:     return %clicker_ids;
1.402     www      8635: }
                   8636: 
1.413     www      8637: sub clicker_grading_parameters {
                   8638:     return ('gradingmechanism' => 'scalar',
                   8639:             'upfiletype' => 'scalar',
                   8640:             'specificid' => 'scalar',
                   8641:             'pcorrect' => 'scalar',
                   8642:             'pincorrect' => 'scalar');
                   8643: }
                   8644: 
1.400     www      8645: sub process_clicker {
                   8646:     my ($r)=@_;
                   8647:     my ($symb)=&get_symb($r);
                   8648:     if (!$symb) {return '';}
                   8649:     my $result=&checkforfile_js();
                   8650:     $env{'form.probTitle'} = &Apache::lonnet::gettitle($symb);
                   8651:     my ($table) = &showResourceInfo($symb,$env{'form.probTitle'});
                   8652:     $result.=$table;
                   8653:     $result.='<br /><table width="100%" border="0"><tr><td bgcolor="#777777">'."\n";
                   8654:     $result.='<table width="100%" border="0"><tr bgcolor="#e6ffff"><td>'."\n";
1.538     schulted 8655:     $result.='&nbsp;<b>'.&mt('Specify a file containing the clicker information for this resource.').
                   8656:         '</b></td></tr>'."\n";
1.400     www      8657:     $result.='<tr bgcolor=#ffffe6><td>'."\n";
1.413     www      8658: # Attempt to restore parameters from last session, set defaults if not present
                   8659:     my %Saveable_Parameters=&clicker_grading_parameters();
                   8660:     &Apache::loncommon::restore_course_settings('grades_clicker',
                   8661:                                                  \%Saveable_Parameters);
                   8662:     if (!$env{'form.pcorrect'}) { $env{'form.pcorrect'}=100; }
                   8663:     if (!$env{'form.pincorrect'}) { $env{'form.pincorrect'}=100; }
                   8664:     if (!$env{'form.gradingmechanism'}) { $env{'form.gradingmechanism'}='attendance'; }
                   8665:     if (!$env{'form.upfiletype'}) { $env{'form.upfiletype'}='iclicker'; }
                   8666: 
                   8667:     my %checked;
1.521     www      8668:     foreach my $gradingmechanism ('attendance','personnel','specific','given') {
1.413     www      8669:        if ($env{'form.gradingmechanism'} eq $gradingmechanism) {
1.569     bisitz   8670:           $checked{$gradingmechanism}=' checked="checked"';
1.413     www      8671:        }
                   8672:     }
                   8673: 
1.400     www      8674:     my $upload=&mt("Upload File");
                   8675:     my $type=&mt("Type");
1.402     www      8676:     my $attendance=&mt("Award points just for participation");
                   8677:     my $personnel=&mt("Correctness determined from response by course personnel");
1.414     www      8678:     my $specific=&mt("Correctness determined from response with clicker ID(s)"); 
1.521     www      8679:     my $given=&mt("Correctness determined from given list of answers").' '.
                   8680:               '<font size="-2"><tt>('.&mt("Provide comma-separated list. Use '*' for any answer correct, '-' for skip").')</tt></font>';
1.402     www      8681:     my $pcorrect=&mt("Percentage points for correct solution");
                   8682:     my $pincorrect=&mt("Percentage points for incorrect solution");
1.413     www      8683:     my $selectform=&Apache::loncommon::select_form($env{'form.upfiletype'},'upfiletype',
1.419     www      8684: 						   ('iclicker' => 'i>clicker',
                   8685:                                                     'interwrite' => 'interwrite PRS'));
1.418     albertel 8686:     $symb = &Apache::lonenc::check_encrypt($symb);
1.400     www      8687:     $result.=<<ENDUPFORM;
1.402     www      8688: <script type="text/javascript">
                   8689: function sanitycheck() {
                   8690: // Accept only integer percentages
                   8691:    document.forms.gradesupload.pcorrect.value=Math.round(document.forms.gradesupload.pcorrect.value);
                   8692:    document.forms.gradesupload.pincorrect.value=Math.round(document.forms.gradesupload.pincorrect.value);
                   8693: // Find out grading choice
                   8694:    for (i=0; i<document.forms.gradesupload.gradingmechanism.length; i++) {
                   8695:       if (document.forms.gradesupload.gradingmechanism[i].checked) {
                   8696:          gradingchoice=document.forms.gradesupload.gradingmechanism[i].value;
                   8697:       }
                   8698:    }
                   8699: // By default, new choice equals user selection
                   8700:    newgradingchoice=gradingchoice;
                   8701: // Not good to give more points for false answers than correct ones
                   8702:    if (Math.round(document.forms.gradesupload.pcorrect.value)<Math.round(document.forms.gradesupload.pincorrect.value)) {
                   8703:       document.forms.gradesupload.pcorrect.value=document.forms.gradesupload.pincorrect.value;
                   8704:    }
                   8705: // If new choice is attendance only, and old choice was correctness-based, restore defaults
                   8706:    if ((gradingchoice=='attendance') && (document.forms.gradesupload.waschecked.value!='attendance')) {
                   8707:       document.forms.gradesupload.pcorrect.value=100;
                   8708:       document.forms.gradesupload.pincorrect.value=100;
                   8709:    }
                   8710: // If the values are different, cannot be attendance only
                   8711:    if ((Math.round(document.forms.gradesupload.pcorrect.value)!=Math.round(document.forms.gradesupload.pincorrect.value)) &&
                   8712:        (gradingchoice=='attendance')) {
                   8713:        newgradingchoice='personnel';
                   8714:    }
                   8715: // Change grading choice to new one
                   8716:    for (i=0; i<document.forms.gradesupload.gradingmechanism.length; i++) {
                   8717:       if (document.forms.gradesupload.gradingmechanism[i].value==newgradingchoice) {
                   8718:          document.forms.gradesupload.gradingmechanism[i].checked=true;
                   8719:       } else {
                   8720:          document.forms.gradesupload.gradingmechanism[i].checked=false;
                   8721:       }
                   8722:    }
                   8723: // Remember the old state
                   8724:    document.forms.gradesupload.waschecked.value=newgradingchoice;
                   8725: }
                   8726: </script>
1.400     www      8727: <form method="post" enctype="multipart/form-data" action="/adm/grades" name="gradesupload">
                   8728: <input type="hidden" name="symb" value="$symb" />
                   8729: <input type="hidden" name="command" value="processclickerfile" />
                   8730: <input type="hidden" name="probTitle" value="$env{'form.probTitle'}" />
                   8731: <input type="hidden" name="saveState"  value="$env{'form.saveState'}" />
                   8732: <input type="file" name="upfile" size="50" />
                   8733: <br /><label>$type: $selectform</label>
1.569     bisitz   8734: <br /><label><input type="radio" name="gradingmechanism" value="attendance"$checked{'attendance'} onClick="sanitycheck()" />$attendance </label>
                   8735: <br /><label><input type="radio" name="gradingmechanism" value="personnel"$checked{'personnel'} onClick="sanitycheck()" />$personnel</label>
                   8736: <br /><label><input type="radio" name="gradingmechanism" value="specific"$checked{'specific'} onClick="sanitycheck()" />$specific </label>
1.414     www      8737: <input type="text" name="specificid" value="$env{'form.specificid'}" size="20" />
1.569     bisitz   8738: <br /><label><input type="radio" name="gradingmechanism" value="given"$checked{'given'} onClick="sanitycheck()" />$given </label>
1.521     www      8739: <br />&nbsp;&nbsp;&nbsp;
                   8740: <input type="text" name="givenanswer" size="50" />
1.413     www      8741: <input type="hidden" name="waschecked" value="$env{'form.gradingmechanism'}" />
                   8742: <br /><label>$pcorrect: <input type="text" name="pcorrect" size="4" value="$env{'form.pcorrect'}" onChange="sanitycheck()" /></label>
                   8743: <br /><label>$pincorrect: <input type="text" name="pincorrect" size="4" value="$env{'form.pincorrect'}" onChange="sanitycheck()" /></label>
1.400     www      8744: <br /><input type="button" onClick="javascript:checkUpload(this.form);" value="$upload" />
                   8745: </form>
                   8746: ENDUPFORM
                   8747:     $result.='</td></tr></table>'."\n".
                   8748:              '</td></tr></table><br /><br />'."\n";
                   8749:     $result.=&show_grading_menu_form($symb);
                   8750:     return $result;
                   8751: }
                   8752: 
                   8753: sub process_clicker_file {
                   8754:     my ($r)=@_;
                   8755:     my ($symb)=&get_symb($r);
                   8756:     if (!$symb) {return '';}
1.413     www      8757: 
                   8758:     my %Saveable_Parameters=&clicker_grading_parameters();
                   8759:     &Apache::loncommon::store_course_settings('grades_clicker',
                   8760:                                               \%Saveable_Parameters);
                   8761: 
1.400     www      8762:     my ($result) = &showResourceInfo($symb,$env{'form.probTitle'});
1.404     www      8763:     if (($env{'form.gradingmechanism'} eq 'specific') && ($env{'form.specificid'}!~/\w/)) {
1.408     albertel 8764: 	$result.='<span class="LC_error">'.&mt('You need to specify a clicker ID for the correct answer').'</span>';
                   8765: 	return $result.&show_grading_menu_form($symb);
1.404     www      8766:     }
1.522     www      8767:     if (($env{'form.gradingmechanism'} eq 'given') && ($env{'form.givenanswer'}!~/\S/)) {
1.521     www      8768:         $result.='<span class="LC_error">'.&mt('You need to specify the correct answer').'</span>';
                   8769:         return $result.&show_grading_menu_form($symb);
                   8770:     }
1.522     www      8771:     my $foundgiven=0;
1.521     www      8772:     if ($env{'form.gradingmechanism'} eq 'given') {
                   8773:         $env{'form.givenanswer'}=~s/^\s*//gs;
                   8774:         $env{'form.givenanswer'}=~s/\s*$//gs;
                   8775:         $env{'form.givenanswer'}=~s/[^a-zA-Z0-9\.\*\-]+/\,/g;
                   8776:         $env{'form.givenanswer'}=uc($env{'form.givenanswer'});
1.522     www      8777:         my @answers=split(/\,/,$env{'form.givenanswer'});
                   8778:         $foundgiven=$#answers+1;
1.521     www      8779:     }
1.407     albertel 8780:     my %clicker_ids=&gather_clicker_ids();
1.408     albertel 8781:     my %correct_ids;
1.404     www      8782:     if ($env{'form.gradingmechanism'} eq 'personnel') {
1.408     albertel 8783: 	%correct_ids=&gather_adv_clicker_ids();
1.404     www      8784:     }
                   8785:     if ($env{'form.gradingmechanism'} eq 'specific') {
1.414     www      8786: 	foreach my $correct_id (split(/[\s\,]/,$env{'form.specificid'})) {;
                   8787: 	   $correct_id=~tr/a-z/A-Z/;
                   8788: 	   $correct_id=~s/\s//gs;
                   8789: 	   $correct_id=~s/^[\#0]+//;
1.421     www      8790:            $correct_id=~s/[\-\:]//g;
1.414     www      8791:            if ($correct_id) {
                   8792: 	      $correct_ids{$correct_id}='specified';
                   8793:            }
                   8794:         }
1.400     www      8795:     }
1.404     www      8796:     if ($env{'form.gradingmechanism'} eq 'attendance') {
1.408     albertel 8797: 	$result.=&mt('Score based on attendance only');
1.521     www      8798:     } elsif ($env{'form.gradingmechanism'} eq 'given') {
1.522     www      8799:         $result.=&mt('Score based on [_1] ([_2] answers)','<tt>'.$env{'form.givenanswer'}.'</tt>',$foundgiven);
1.404     www      8800:     } else {
1.408     albertel 8801: 	my $number=0;
1.411     www      8802: 	$result.='<p><b>'.&mt('Correctness determined by the following IDs').'</b>';
1.408     albertel 8803: 	foreach my $id (sort(keys(%correct_ids))) {
1.411     www      8804: 	    $result.='<br /><tt>'.$id.'</tt> - ';
1.408     albertel 8805: 	    if ($correct_ids{$id} eq 'specified') {
                   8806: 		$result.=&mt('specified');
                   8807: 	    } else {
                   8808: 		my ($uname,$udom)=split(/\:/,$correct_ids{$id});
                   8809: 		$result.=&Apache::loncommon::plainname($uname,$udom);
                   8810: 	    }
                   8811: 	    $number++;
                   8812: 	}
1.411     www      8813:         $result.="</p>\n";
1.408     albertel 8814: 	if ($number==0) {
                   8815: 	    $result.='<span class="LC_error">'.&mt('No IDs found to determine correct answer').'</span>';
                   8816: 	    return $result.&show_grading_menu_form($symb);
                   8817: 	}
1.404     www      8818:     }
1.405     www      8819:     if (length($env{'form.upfile'}) < 2) {
1.407     albertel 8820:         $result.=&mt('[_1] Error: [_2] The file you attempted to upload, [_3] contained no information. Please check that you entered the correct filename.',
                   8821: 		     '<span class="LC_error">',
                   8822: 		     '</span>',
                   8823: 		     '<span class="LC_filename">'.&HTML::Entities::encode($env{'form.upfile.filename'},'<>&"').'</span>');
1.405     www      8824:         return $result.&show_grading_menu_form($symb);
                   8825:     }
1.410     www      8826: 
                   8827: # Were able to get all the info needed, now analyze the file
                   8828: 
1.411     www      8829:     $result.=&Apache::loncommon::studentbrowser_javascript();
1.418     albertel 8830:     $symb = &Apache::lonenc::check_encrypt($symb);
1.410     www      8831:     my $heading=&mt('Scanning clicker file');
                   8832:     $result.=(<<ENDHEADER);
                   8833: <br /><table width="100%" border="0"><tr><td bgcolor="#777777">
                   8834: <table width="100%" border="0"><tr bgcolor="#e6ffff"><td>
                   8835: <b>$heading</b></td></tr><tr bgcolor=#ffffe6><td>
                   8836: <form method="post" action="/adm/grades" name="clickeranalysis">
                   8837: <input type="hidden" name="symb" value="$symb" />
                   8838: <input type="hidden" name="command" value="assignclickergrades" />
                   8839: <input type="hidden" name="probTitle" value="$env{'form.probTitle'}" />
                   8840: <input type="hidden" name="saveState"  value="$env{'form.saveState'}" />
1.411     www      8841: <input type="hidden" name="gradingmechanism" value="$env{'form.gradingmechanism'}" />
                   8842: <input type="hidden" name="pcorrect" value="$env{'form.pcorrect'}" />
                   8843: <input type="hidden" name="pincorrect" value="$env{'form.pincorrect'}" />
1.410     www      8844: ENDHEADER
1.522     www      8845:     if ($env{'form.gradingmechanism'} eq 'given') {
                   8846:        $result.='<input type="hidden" name="correct:given" value="'.$env{'form.givenanswer'}.'" />';
                   8847:     } 
1.408     albertel 8848:     my %responses;
                   8849:     my @questiontitles;
1.405     www      8850:     my $errormsg='';
                   8851:     my $number=0;
                   8852:     if ($env{'form.upfiletype'} eq 'iclicker') {
1.408     albertel 8853: 	($errormsg,$number)=&iclicker_eval(\@questiontitles,\%responses);
1.406     www      8854:     }
1.419     www      8855:     if ($env{'form.upfiletype'} eq 'interwrite') {
                   8856:         ($errormsg,$number)=&interwrite_eval(\@questiontitles,\%responses);
                   8857:     }
1.411     www      8858:     $result.='<br />'.&mt('Found [_1] question(s)',$number).'<br />'.
                   8859:              '<input type="hidden" name="number" value="'.$number.'" />'.
                   8860:              &mt('Awarding [_1] percent for correct and [_2] percent for incorrect responses',
                   8861:                  $env{'form.pcorrect'},$env{'form.pincorrect'}).
                   8862:              '<br />';
1.522     www      8863:     if (($env{'form.gradingmechanism'} eq 'given') && ($number!=$foundgiven)) {
                   8864:        $result.='<span class="LC_error">'.&mt('Number of given answers does not agree with number of questions in file.').'</span>';
                   8865:        return $result.&show_grading_menu_form($symb);
                   8866:     } 
1.414     www      8867: # Remember Question Titles
                   8868: # FIXME: Possibly need delimiter other than ":"
                   8869:     for (my $i=0;$i<$number;$i++) {
                   8870:         $result.='<input type="hidden" name="question:'.$i.'" value="'.
                   8871:                  &HTML::Entities::encode($questiontitles[$i],'"&<>').'" />';
                   8872:     }
1.411     www      8873:     my $correct_count=0;
                   8874:     my $student_count=0;
                   8875:     my $unknown_count=0;
1.414     www      8876: # Match answers with usernames
                   8877: # FIXME: Possibly need delimiter other than ":"
1.409     albertel 8878:     foreach my $id (keys(%responses)) {
1.410     www      8879:        if ($correct_ids{$id}) {
1.414     www      8880:           $result.="\n".'<input type="hidden" name="correct:'.$correct_count.':'.$correct_ids{$id}.'" value="'.$responses{$id}.'" />';
1.411     www      8881:           $correct_count++;
1.410     www      8882:        } elsif ($clicker_ids{$id}) {
1.437     www      8883:           if ($clicker_ids{$id}=~/\,/) {
                   8884: # More than one user with the same clicker!
                   8885:              $result.="\n<hr />".&mt('Clicker registered more than once').": <tt>".$id."</tt><br />";
                   8886:              $result.="\n".'<input type="hidden" name="unknown:'.$id.'" value="'.$responses{$id}.'" />'.
                   8887:                            "<select name='multi".$id."'>";
                   8888:              foreach my $reguser (sort(split(/\,/,$clicker_ids{$id}))) {
                   8889:                  $result.="<option value='".$reguser."'>".&Apache::loncommon::plainname(split(/\:/,$reguser)).' ('.$reguser.')</option>';
                   8890:              }
                   8891:              $result.='</select>';
                   8892:              $unknown_count++;
                   8893:           } else {
                   8894: # Good: found one and only one user with the right clicker
                   8895:              $result.="\n".'<input type="hidden" name="student:'.$clicker_ids{$id}.'" value="'.$responses{$id}.'" />';
                   8896:              $student_count++;
                   8897:           }
1.410     www      8898:        } else {
1.411     www      8899:           $result.="\n<hr />".&mt('Unregistered Clicker')." <tt>".$id."</tt><br />";
                   8900:           $result.="\n".'<input type="hidden" name="unknown:'.$id.'" value="'.$responses{$id}.'" />'.
                   8901:                    "\n".&mt("Username").": <input type='text' name='uname".$id."' />&nbsp;".
                   8902:                    "\n".&mt("Domain").": ".
                   8903:                    &Apache::loncommon::select_dom_form($env{'course.'.$env{'request.course.id'}.'.domain'},'udom'.$id).'&nbsp;'.
                   8904:                    &Apache::loncommon::selectstudent_link('clickeranalysis','uname'.$id,'udom'.$id);
                   8905:           $unknown_count++;
1.410     www      8906:        }
1.405     www      8907:     }
1.412     www      8908:     $result.='<hr />'.
                   8909:              &mt('Found [_1] registered and [_2] unregistered clickers.',$student_count,$unknown_count);
1.521     www      8910:     if (($env{'form.gradingmechanism'} ne 'attendance') && ($env{'form.gradingmechanism'} ne 'given')) {
1.412     www      8911:        if ($correct_count==0) {
                   8912:           $errormsg.="Found no correct answers answers for grading!";
                   8913:        } elsif ($correct_count>1) {
1.414     www      8914:           $result.='<br /><span class="LC_warning">'.&mt("Found [_1] entries for grading!",$correct_count).'</span>';
1.412     www      8915:        }
                   8916:     }
1.428     www      8917:     if ($number<1) {
                   8918:        $errormsg.="Found no questions.";
                   8919:     }
1.412     www      8920:     if ($errormsg) {
                   8921:        $result.='<br /><span class="LC_error">'.&mt($errormsg).'</span>';
                   8922:     } else {
                   8923:        $result.='<br /><input type="submit" name="finalize" value="'.&mt('Finalize Grading').'" />';
                   8924:     }
                   8925:     $result.='</form></td></tr></table>'."\n".
1.410     www      8926:              '</td></tr></table><br /><br />'."\n";
1.404     www      8927:     return $result.&show_grading_menu_form($symb);
1.400     www      8928: }
                   8929: 
1.405     www      8930: sub iclicker_eval {
1.406     www      8931:     my ($questiontitles,$responses)=@_;
1.405     www      8932:     my $number=0;
                   8933:     my $errormsg='';
                   8934:     foreach my $line (split(/[\n\r]/,$env{'form.upfile'})) {
1.410     www      8935:         my %components=&Apache::loncommon::record_sep($line);
                   8936:         my @entries=map {$components{$_}} (sort(keys(%components)));
1.408     albertel 8937: 	if ($entries[0] eq 'Question') {
                   8938: 	    for (my $i=3;$i<$#entries;$i+=6) {
                   8939: 		$$questiontitles[$number]=$entries[$i];
                   8940: 		$number++;
                   8941: 	    }
                   8942: 	}
                   8943: 	if ($entries[0]=~/^\#/) {
                   8944: 	    my $id=$entries[0];
                   8945: 	    my @idresponses;
                   8946: 	    $id=~s/^[\#0]+//;
                   8947: 	    for (my $i=0;$i<$number;$i++) {
                   8948: 		my $idx=3+$i*6;
                   8949: 		push(@idresponses,$entries[$idx]);
                   8950: 	    }
                   8951: 	    $$responses{$id}=join(',',@idresponses);
                   8952: 	}
1.405     www      8953:     }
                   8954:     return ($errormsg,$number);
                   8955: }
                   8956: 
1.419     www      8957: sub interwrite_eval {
                   8958:     my ($questiontitles,$responses)=@_;
                   8959:     my $number=0;
                   8960:     my $errormsg='';
1.420     www      8961:     my $skipline=1;
                   8962:     my $questionnumber=0;
                   8963:     my %idresponses=();
1.419     www      8964:     foreach my $line (split(/[\n\r]/,$env{'form.upfile'})) {
                   8965:         my %components=&Apache::loncommon::record_sep($line);
                   8966:         my @entries=map {$components{$_}} (sort(keys(%components)));
1.420     www      8967:         if ($entries[1] eq 'Time') { $skipline=0; next; }
                   8968:         if ($entries[1] eq 'Response') { $skipline=1; }
                   8969:         next if $skipline;
                   8970:         if ($entries[0]!=$questionnumber) {
                   8971:            $questionnumber=$entries[0];
                   8972:            $$questiontitles[$number]=&mt('Question [_1]',$questionnumber);
                   8973:            $number++;
1.419     www      8974:         }
1.420     www      8975:         my $id=$entries[4];
                   8976:         $id=~s/^[\#0]+//;
1.421     www      8977:         $id=~s/^v\d*\://i;
                   8978:         $id=~s/[\-\:]//g;
1.420     www      8979:         $idresponses{$id}[$number]=$entries[6];
                   8980:     }
1.524     raeburn  8981:     foreach my $id (keys(%idresponses)) {
1.420     www      8982:        $$responses{$id}=join(',',@{$idresponses{$id}});
                   8983:        $$responses{$id}=~s/^\s*\,//;
1.419     www      8984:     }
                   8985:     return ($errormsg,$number);
                   8986: }
                   8987: 
1.414     www      8988: sub assign_clicker_grades {
                   8989:     my ($r)=@_;
                   8990:     my ($symb)=&get_symb($r);
                   8991:     if (!$symb) {return '';}
1.416     www      8992: # See which part we are saving to
                   8993:     my ($partlist,$handgrade,$responseType) = &response_type($symb);
                   8994: # FIXME: This should probably look for the first handgradeable part
                   8995:     my $part=$$partlist[0];
                   8996: # Start screen output
1.414     www      8997:     my ($result) = &showResourceInfo($symb,$env{'form.probTitle'});
1.416     www      8998: 
1.414     www      8999:     my $heading=&mt('Assigning grades based on clicker file');
                   9000:     $result.=(<<ENDHEADER);
                   9001: <br /><table width="100%" border="0"><tr><td bgcolor="#777777">
                   9002: <table width="100%" border="0"><tr bgcolor="#e6ffff"><td>
                   9003: <b>$heading</b></td></tr><tr bgcolor=#ffffe6><td>
                   9004: ENDHEADER
                   9005: # Get correct result
                   9006: # FIXME: Possibly need delimiter other than ":"
                   9007:     my @correct=();
1.415     www      9008:     my $gradingmechanism=$env{'form.gradingmechanism'};
                   9009:     my $number=$env{'form.number'};
                   9010:     if ($gradingmechanism ne 'attendance') {
1.414     www      9011:        foreach my $key (keys(%env)) {
                   9012:           if ($key=~/^form\.correct\:/) {
                   9013:              my @input=split(/\,/,$env{$key});
                   9014:              for (my $i=0;$i<=$#input;$i++) {
                   9015:                  if (($correct[$i]) && ($input[$i]) &&
                   9016:                      ($correct[$i] ne $input[$i])) {
                   9017:                     $result.='<br /><span class="LC_warning">'.
                   9018:                              &mt('More than one correct result given for question "[_1]": [_2] versus [_3].',
                   9019:                                  $env{'form.question:'.$i},$correct[$i],$input[$i]).'</span>';
                   9020:                  } elsif ($input[$i]) {
                   9021:                     $correct[$i]=$input[$i];
                   9022:                  }
                   9023:              }
                   9024:           }
                   9025:        }
1.415     www      9026:        for (my $i=0;$i<$number;$i++) {
1.414     www      9027:           if (!$correct[$i]) {
                   9028:              $result.='<br /><span class="LC_error">'.
                   9029:                       &mt('No correct result given for question "[_1]"!',
                   9030:                           $env{'form.question:'.$i}).'</span>';
                   9031:           }
                   9032:        }
                   9033:        $result.='<br />'.&mt("Correct answer: [_1]",join(', ',map { ($_?$_:'-') } @correct));
                   9034:     }
                   9035: # Start grading
1.415     www      9036:     my $pcorrect=$env{'form.pcorrect'};
                   9037:     my $pincorrect=$env{'form.pincorrect'};
1.416     www      9038:     my $storecount=0;
1.415     www      9039:     foreach my $key (keys(%env)) {
1.420     www      9040:        my $user='';
1.415     www      9041:        if ($key=~/^form\.student\:(.*)$/) {
1.420     www      9042:           $user=$1;
                   9043:        }
                   9044:        if ($key=~/^form\.unknown\:(.*)$/) {
                   9045:           my $id=$1;
                   9046:           if (($env{'form.uname'.$id}) && ($env{'form.udom'.$id})) {
                   9047:              $user=$env{'form.uname'.$id}.':'.$env{'form.udom'.$id};
1.437     www      9048:           } elsif ($env{'form.multi'.$id}) {
                   9049:              $user=$env{'form.multi'.$id};
1.420     www      9050:           }
                   9051:        }
                   9052:        if ($user) { 
1.415     www      9053:           my @answer=split(/\,/,$env{$key});
                   9054:           my $sum=0;
1.522     www      9055:           my $realnumber=$number;
1.415     www      9056:           for (my $i=0;$i<$number;$i++) {
1.576     www      9057:              if  ($correct[$i] eq '-') {
                   9058:                 $realnumber--;
                   9059:              } elsif ($answer[$i]) {
1.415     www      9060:                 if ($gradingmechanism eq 'attendance') {
                   9061:                    $sum+=$pcorrect;
1.576     www      9062:                 } elsif ($correct[$i] eq '*') {
1.522     www      9063:                    $sum+=$pcorrect;
1.415     www      9064:                 } else {
                   9065:                    if ($answer[$i] eq $correct[$i]) {
                   9066:                       $sum+=$pcorrect;
                   9067:                    } else {
                   9068:                       $sum+=$pincorrect;
                   9069:                    }
                   9070:                 }
                   9071:              }
                   9072:           }
1.522     www      9073:           my $ave=$sum/(100*$realnumber);
1.416     www      9074: # Store
                   9075:           my ($username,$domain)=split(/\:/,$user);
                   9076:           my %grades=();
                   9077:           $grades{"resource.$part.solved"}='correct_by_override';
                   9078:           $grades{"resource.$part.awarded"}=$ave;
                   9079:           $grades{"resource.regrader"}="$env{'user.name'}:$env{'user.domain'}";
                   9080:           my $returncode=&Apache::lonnet::cstore(\%grades,$symb,
                   9081:                                                  $env{'request.course.id'},
                   9082:                                                  $domain,$username);
                   9083:           if ($returncode ne 'ok') {
                   9084:              $result.="<br /><span class=\"LC_error\">Failed to save student $username:$domain. Message when trying to save was ($returncode)</span>";
                   9085:           } else {
                   9086:              $storecount++;
                   9087:           }
1.415     www      9088:        }
                   9089:     }
                   9090: # We are done
1.549     hauer    9091:     $result.='<br />'.&mt('Successfully stored grades for [quant,_1,student].',$storecount).
1.416     www      9092:              '</td></tr></table>'."\n".
1.414     www      9093:              '</td></tr></table><br /><br />'."\n";
                   9094:     return $result.&show_grading_menu_form($symb);
                   9095: }
                   9096: 
1.1       albertel 9097: sub handler {
1.41      ng       9098:     my $request=$_[0];
1.434     albertel 9099:     &reset_caches();
1.257     albertel 9100:     if ($env{'browser.mathml'}) {
1.141     www      9101: 	&Apache::loncommon::content_type($request,'text/xml');
1.41      ng       9102:     } else {
1.141     www      9103: 	&Apache::loncommon::content_type($request,'text/html');
1.41      ng       9104:     }
                   9105:     $request->send_http_header;
1.44      ng       9106:     return '' if $request->header_only;
1.41      ng       9107:     &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'});
1.324     albertel 9108:     my $symb=&get_symb($request,1);
1.160     albertel 9109:     my @commands=&Apache::loncommon::get_env_multiple('form.command');
                   9110:     my $command=$commands[0];
1.447     foxr     9111: 
1.160     albertel 9112:     if ($#commands > 0) {
                   9113: 	&Apache::lonnet::logthis("grades got multiple commands ".join(':',@commands));
                   9114:     }
1.447     foxr     9115: 
1.513     foxr     9116:     $ssi_error = 0;
1.535     raeburn  9117:     my $brcrum = [{href=>"/adm/grades",text=>"Grading"}];
                   9118:     $request->print(&Apache::loncommon::start_page('Grading',undef,
                   9119:                                           {'bread_crumbs' => $brcrum}));
1.324     albertel 9120:     if ($symb eq '' && $command eq '') {
1.257     albertel 9121: 	if ($env{'user.adv'}) {
                   9122: 	    if (($env{'form.codeone'}) && ($env{'form.codetwo'}) &&
                   9123: 		($env{'form.codethree'})) {
                   9124: 		my $token=$env{'form.codeone'}.'*'.$env{'form.codetwo'}.'*'.
                   9125: 		    $env{'form.codethree'};
1.41      ng       9126: 		my ($tsymb,$tuname,$tudom,$tcrsid)=
                   9127: 		    &Apache::lonnet::checkin($token);
                   9128: 		if ($tsymb) {
1.137     albertel 9129: 		    my ($map,$id,$url)=&Apache::lonnet::decode_symb($tsymb);
1.41      ng       9130: 		    if (&Apache::lonnet::allowed('mgr',$tcrsid)) {
1.513     foxr     9131: 			$request->print(&ssi_with_retries('/res/'.$url, $ssi_retries,
1.99      albertel 9132: 					  ('grade_username' => $tuname,
                   9133: 					   'grade_domain' => $tudom,
                   9134: 					   'grade_courseid' => $tcrsid,
                   9135: 					   'grade_symb' => $tsymb)));
1.41      ng       9136: 		    } else {
1.45      ng       9137: 			$request->print('<h3>Not authorized: '.$token.'</h3>');
1.99      albertel 9138: 		    }
1.41      ng       9139: 		} else {
1.45      ng       9140: 		    $request->print('<h3>Not a valid DocID: '.$token.'</h3>');
1.41      ng       9141: 		}
1.14      www      9142: 	    } else {
1.41      ng       9143: 		$request->print(&Apache::lonxml::tokeninputfield());
                   9144: 	    }
                   9145: 	}
                   9146:     } else {
1.285     albertel 9147: 	&init_perm();
1.104     albertel 9148: 	if ($command eq 'submission' && $perm{'vgr'}) {
1.257     albertel 9149: 	    ($env{'form.student'} eq '' ? &listStudents($request) : &submission($request,0,0));
1.103     albertel 9150: 	} elsif ($command eq 'pickStudentPage' && $perm{'vgr'}) {
1.68      ng       9151: 	    &pickStudentPage($request);
1.103     albertel 9152: 	} elsif ($command eq 'displayPage' && $perm{'vgr'}) {
1.68      ng       9153: 	    &displayPage($request);
1.104     albertel 9154: 	} elsif ($command eq 'gradeByPage' && $perm{'mgr'}) {
1.71      ng       9155: 	    &updateGradeByPage($request);
1.104     albertel 9156: 	} elsif ($command eq 'processGroup' && $perm{'vgr'}) {
1.41      ng       9157: 	    &processGroup($request);
1.104     albertel 9158: 	} elsif ($command eq 'gradingmenu' && $perm{'vgr'}) {
1.443     banghart 9159: 	    $request->print(&grading_menu($request));
                   9160: 	} elsif ($command eq 'submit_options' && $perm{'vgr'}) {
                   9161: 	    $request->print(&submit_options($request));
1.104     albertel 9162: 	} elsif ($command eq 'viewgrades' && $perm{'vgr'}) {
1.41      ng       9163: 	    $request->print(&viewgrades($request));
1.104     albertel 9164: 	} elsif ($command eq 'handgrade' && $perm{'mgr'}) {
1.41      ng       9165: 	    $request->print(&processHandGrade($request));
1.106     albertel 9166: 	} elsif ($command eq 'editgrades' && $perm{'mgr'}) {
1.41      ng       9167: 	    $request->print(&editgrades($request));
1.106     albertel 9168: 	} elsif ($command eq 'verify' && $perm{'vgr'}) {
1.41      ng       9169: 	    $request->print(&verifyreceipt($request));
1.400     www      9170:         } elsif ($command eq 'processclicker' && $perm{'mgr'}) {
                   9171:             $request->print(&process_clicker($request));
                   9172:         } elsif ($command eq 'processclickerfile' && $perm{'mgr'}) {
                   9173:             $request->print(&process_clicker_file($request));
1.414     www      9174:         } elsif ($command eq 'assignclickergrades' && $perm{'mgr'}) {
                   9175:             $request->print(&assign_clicker_grades($request));
1.106     albertel 9176: 	} elsif ($command eq 'csvform' && $perm{'mgr'}) {
1.72      ng       9177: 	    $request->print(&upcsvScores_form($request));
1.106     albertel 9178: 	} elsif ($command eq 'csvupload' && $perm{'mgr'}) {
1.41      ng       9179: 	    $request->print(&csvupload($request));
1.106     albertel 9180: 	} elsif ($command eq 'csvuploadmap' && $perm{'mgr'} ) {
1.41      ng       9181: 	    $request->print(&csvuploadmap($request));
1.246     albertel 9182: 	} elsif ($command eq 'csvuploadoptions' && $perm{'mgr'}) {
1.257     albertel 9183: 	    if ($env{'form.associate'} ne 'Reverse Association') {
1.246     albertel 9184: 		$request->print(&csvuploadoptions($request));
1.41      ng       9185: 	    } else {
1.257     albertel 9186: 		if ( $env{'form.upfile_associate'} ne 'reverse' ) {
                   9187: 		    $env{'form.upfile_associate'} = 'reverse';
1.41      ng       9188: 		} else {
1.257     albertel 9189: 		    $env{'form.upfile_associate'} = 'forward';
1.41      ng       9190: 		}
                   9191: 		$request->print(&csvuploadmap($request));
                   9192: 	    }
1.246     albertel 9193: 	} elsif ($command eq 'csvuploadassign' && $perm{'mgr'} ) {
                   9194: 	    $request->print(&csvuploadassign($request));
1.106     albertel 9195: 	} elsif ($command eq 'scantron_selectphase' && $perm{'mgr'}) {
1.75      albertel 9196: 	    $request->print(&scantron_selectphase($request));
1.203     albertel 9197:  	} elsif ($command eq 'scantron_warning' && $perm{'mgr'}) {
                   9198:  	    $request->print(&scantron_do_warning($request));
1.142     albertel 9199: 	} elsif ($command eq 'scantron_validate' && $perm{'mgr'}) {
                   9200: 	    $request->print(&scantron_validate_file($request));
1.106     albertel 9201: 	} elsif ($command eq 'scantron_process' && $perm{'mgr'}) {
1.82      albertel 9202: 	    $request->print(&scantron_process_students($request));
1.157     albertel 9203:  	} elsif ($command eq 'scantronupload' && 
1.257     albertel 9204:  		 (&Apache::lonnet::allowed('usc',$env{'request.role.domain'})||
                   9205: 		  &Apache::lonnet::allowed('usc',$env{'request.course.id'}))) {
1.162     albertel 9206:  	    $request->print(&scantron_upload_scantron_data($request)); 
1.157     albertel 9207:  	} elsif ($command eq 'scantronupload_save' &&
1.257     albertel 9208:  		 (&Apache::lonnet::allowed('usc',$env{'request.role.domain'})||
                   9209: 		  &Apache::lonnet::allowed('usc',$env{'request.course.id'}))) {
1.157     albertel 9210:  	    $request->print(&scantron_upload_scantron_data_save($request));
1.202     albertel 9211:  	} elsif ($command eq 'scantron_download' &&
1.257     albertel 9212: 		 &Apache::lonnet::allowed('usc',$env{'request.course.id'})) {
1.162     albertel 9213:  	    $request->print(&scantron_download_scantron_data($request));
1.523     raeburn  9214:         } elsif ($command eq 'checksubmissions' && $perm{'vgr'}) {
                   9215:             $request->print(&checkscantron_results($request));     
1.106     albertel 9216: 	} elsif ($command) {
1.562     bisitz   9217: 	    $request->print('<p class="LC_error">'.&mt('Access Denied ([_1])',$command).'</p>');
1.26      albertel 9218: 	}
1.2       albertel 9219:     }
1.513     foxr     9220:     if ($ssi_error) {
                   9221: 	&ssi_print_error($request);
                   9222:     }
1.353     albertel 9223:     $request->print(&Apache::loncommon::end_page());
1.434     albertel 9224:     &reset_caches();
1.44      ng       9225:     return '';
                   9226: }
                   9227: 
1.1       albertel 9228: 1;
                   9229: 
1.13      albertel 9230: __END__;
1.531     jms      9231: 
                   9232: 
                   9233: =head1 NAME
                   9234: 
                   9235: Apache::grades
                   9236: 
                   9237: =head1 SYNOPSIS
                   9238: 
                   9239: Handles the viewing of grades.
                   9240: 
                   9241: This is part of the LearningOnline Network with CAPA project
                   9242: described at http://www.lon-capa.org.
                   9243: 
                   9244: =head1 OVERVIEW
                   9245: 
                   9246: Do an ssi with retries:
                   9247: While I'd love to factor out this with the vesrion in lonprintout,
                   9248: 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
                   9249: I'm not quite ready to invent (e.g. an ssi_with_retry object).
                   9250: 
                   9251: At least the logic that drives this has been pulled out into loncommon.
                   9252: 
                   9253: 
                   9254: 
                   9255: ssi_with_retries - Does the server side include of a resource.
                   9256:                      if the ssi call returns an error we'll retry it up to
                   9257:                      the number of times requested by the caller.
                   9258:                      If we still have a proble, no text is appended to the
                   9259:                      output and we set some global variables.
                   9260:                      to indicate to the caller an SSI error occurred.  
                   9261:                      All of this is supposed to deal with the issues described
                   9262:                      in LonCAPA BZ 5631 see:
                   9263:                      http://bugs.lon-capa.org/show_bug.cgi?id=5631
                   9264:                      by informing the user that this happened.
                   9265: 
                   9266: Parameters:
                   9267:   resource   - The resource to include.  This is passed directly, without
                   9268:                interpretation to lonnet::ssi.
                   9269:   form       - The form hash parameters that guide the interpretation of the resource
                   9270:                
                   9271:   retries    - Number of retries allowed before giving up completely.
                   9272: Returns:
                   9273:   On success, returns the rendered resource identified by the resource parameter.
                   9274: Side Effects:
                   9275:   The following global variables can be set:
                   9276:    ssi_error                - If an unrecoverable error occurred this becomes true.
                   9277:                               It is up to the caller to initialize this to false
                   9278:                               if desired.
                   9279:    ssi_error_resource  - If an unrecoverable error occurred, this is the value
                   9280:                               of the resource that could not be rendered by the ssi
                   9281:                               call.
                   9282:    ssi_error_message   - The error string fetched from the ssi response
                   9283:                               in the event of an error.
                   9284: 
                   9285: 
                   9286: =head1 HANDLER SUBROUTINE
                   9287: 
                   9288: ssi_with_retries()
                   9289: 
                   9290: =head1 SUBROUTINES
                   9291: 
                   9292: =over
                   9293: 
                   9294: =item scantron_get_correction() : 
                   9295: 
                   9296:    Builds the interface screen to interact with the operator to fix a
                   9297:    specific error condition in a specific scanline
                   9298: 
                   9299:  Arguments:
                   9300:     $r           - Apache request object
                   9301:     $i           - number of the current scanline
                   9302:     $scan_record - hash ref as returned from &scantron_parse_scanline()
                   9303:     $scan_config - hash ref as returned from &get_scantron_config()
                   9304:     $line        - full contents of the current scanline
                   9305:     $error       - error condition, valid values are
                   9306:                    'incorrectCODE', 'duplicateCODE',
                   9307:                    'doublebubble', 'missingbubble',
                   9308:                    'duplicateID', 'incorrectID'
                   9309:     $arg         - extra information needed
                   9310:        For errors:
                   9311:          - duplicateID   - paper number that this studentID was seen before on
                   9312:          - duplicateCODE - array ref of the paper numbers this CODE was
                   9313:                            seen on before
                   9314:          - incorrectCODE - current incorrect CODE 
                   9315:          - doublebubble  - array ref of the bubble lines that have double
                   9316:                            bubble errors
                   9317:          - missingbubble - array ref of the bubble lines that have missing
                   9318:                            bubble errors
                   9319: 
                   9320: =item  scantron_get_maxbubble() : 
                   9321: 
                   9322:    Returns the maximum number of bubble lines that are expected to
                   9323:    occur. Does this by walking the selected sequence rendering the
                   9324:    resource and then checking &Apache::lonxml::get_problem_counter()
                   9325:    for what the current value of the problem counter is.
                   9326: 
                   9327:    Caches the results to $env{'form.scantron_maxbubble'},
                   9328:    $env{'form.scantron.bubble_lines.n'}, 
                   9329:    $env{'form.scantron.first_bubble_line.n'} and
                   9330:    $env{"form.scantron.sub_bubblelines.n"}
                   9331:    which are the total number of bubble, lines, the number of bubble
                   9332:    lines for response n and number of the first bubble line for response n,
                   9333:    and a comma separated list of numbers of bubble lines for sub-questions
                   9334:    (for optionresponse, matchresponse, and rankresponse items), for response n.  
                   9335: 
                   9336: 
                   9337: =item  scantron_validate_missingbubbles() : 
                   9338: 
                   9339:    Validates all scanlines in the selected file to not have any
                   9340:     answers that don't have bubbles that have not been verified
                   9341:     to be bubble free.
                   9342: 
                   9343: =item  scantron_process_students() : 
                   9344: 
                   9345:    Routine that does the actual grading of the bubble sheet information.
                   9346: 
                   9347:    The parsed scanline hash is added to %env 
                   9348: 
                   9349:    Then foreach unskipped scanline it does an &Apache::lonnet::ssi()
                   9350:    foreach resource , with the form data of
                   9351: 
                   9352: 	'submitted'     =>'scantron' 
                   9353: 	'grade_target'  =>'grade',
                   9354: 	'grade_username'=> username of student
                   9355: 	'grade_domain'  => domain of student
                   9356: 	'grade_courseid'=> of course
                   9357: 	'grade_symb'    => symb of resource to grade
                   9358: 
                   9359:     This triggers a grading pass. The problem grading code takes care
                   9360:     of converting the bubbled letter information (now in %env) into a
                   9361:     valid submission.
                   9362: 
                   9363: =item  scantron_upload_scantron_data() :
                   9364: 
                   9365:     Creates the screen for adding a new bubble sheet data file to a course.
                   9366: 
                   9367: =item  scantron_upload_scantron_data_save() : 
                   9368: 
                   9369:    Adds a provided bubble information data file to the course if user
                   9370:    has the correct privileges to do so. 
                   9371: 
                   9372: =item  valid_file() :
                   9373: 
                   9374:    Validates that the requested bubble data file exists in the course.
                   9375: 
                   9376: =item  scantron_download_scantron_data() : 
                   9377: 
                   9378:    Shows a list of the three internal files (original, corrected,
                   9379:    skipped) for a specific bubble sheet data file that exists in the
                   9380:    course.
                   9381: 
                   9382: =item  scantron_validate_ID() : 
                   9383: 
                   9384:    Validates all scanlines in the selected file to not have any
1.556     weissno  9385:    invalid or underspecified student/employee IDs
1.531     jms      9386: 
                   9387: =back
                   9388: 
                   9389: =cut

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