File:  [LON-CAPA] / loncom / homework / grades.pm
Revision 1.596.2.4: download - view: text, annotated - select for diffs
Sun Oct 9 23:24:27 2011 UTC (12 years, 7 months ago) by raeburn
Branches: version_2_10_X
- Backport 1.597 (part), 1.598 (part), 1.601 (part), 1.606 (part), 1.623 (part),
           1.627 (part), 1.630, 1.631, 1.632 (part), 1.638 (part), 1.642, 1.643,
           1.644, 1.646, 1.647, 1.648, 1.651, 1.652, 1.653, 1.654, 1.655, 1.656,
           1.657.

    1: # The LearningOnline Network with CAPA
    2: # The LON-CAPA Grading handler
    3: #
    4: # $Id: grades.pm,v 1.596.2.4 2011/10/09 23:24:27 raeburn Exp $
    5: #
    6: # Copyright Michigan State University Board of Trustees
    7: #
    8: # This file is part of the LearningOnline Network with CAPA (LON-CAPA).
    9: #
   10: # LON-CAPA is free software; you can redistribute it and/or modify
   11: # it under the terms of the GNU General Public License as published by
   12: # the Free Software Foundation; either version 2 of the License, or
   13: # (at your option) any later version.
   14: #
   15: # LON-CAPA is distributed in the hope that it will be useful,
   16: # but WITHOUT ANY WARRANTY; without even the implied warranty of
   17: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   18: # GNU General Public License for more details.
   19: #
   20: # You should have received a copy of the GNU General Public License
   21: # along with LON-CAPA; if not, write to the Free Software
   22: # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
   23: #
   24: # /home/httpd/html/adm/gpl.txt
   25: #
   26: # http://www.lon-capa.org/
   27: #
   28: 
   29: 
   30: 
   31: package Apache::grades;
   32: use strict;
   33: use Apache::style;
   34: use Apache::lonxml;
   35: use Apache::lonnet;
   36: use Apache::loncommon;
   37: use Apache::lonhtmlcommon;
   38: use Apache::lonnavmaps;
   39: use Apache::lonhomework;
   40: use Apache::lonpickcode;
   41: use Apache::loncoursedata;
   42: use Apache::lonmsg();
   43: use Apache::Constants qw(:common :http);
   44: use Apache::lonlocal;
   45: use Apache::lonenc;
   46: use Apache::bridgetask();
   47: use String::Similarity;
   48: use LONCAPA;
   49: 
   50: use POSIX qw(floor);
   51: 
   52: 
   53: 
   54: my %perm=();
   55: 
   56: #  These variables are used to recover from ssi errors
   57: 
   58: my $ssi_retries = 5;
   59: my $ssi_error;
   60: my $ssi_error_resource;
   61: my $ssi_error_message;
   62: 
   63: 
   64: sub ssi_with_retries {
   65:     my ($resource, $retries, %form) = @_;
   66:     my ($content, $response) = &Apache::loncommon::ssi_with_retries($resource, $retries, %form);
   67:     if ($response->is_error) {
   68: 	$ssi_error          = 1;
   69: 	$ssi_error_resource = $resource;
   70: 	$ssi_error_message  = $response->code . " " . $response->message;
   71:     }
   72: 
   73:     return $content;
   74: 
   75: }
   76: #
   77: #  Prodcuces an ssi retry failure error message to the user:
   78: #
   79: 
   80: sub ssi_print_error {
   81:     my ($r) = @_;
   82:     my $helpurl = &Apache::loncommon::top_nav_help('Helpdesk');
   83:     $r->print('
   84: <br />
   85: <h2>'.&mt('An unrecoverable network error occurred:').'</h2>
   86: <p>
   87: '.&mt('Unable to retrieve a resource from a server:').'<br />
   88: '.&mt('Resource:').' '.$ssi_error_resource.'<br />
   89: '.&mt('Error:').' '.$ssi_error_message.'
   90: </p>
   91: <p>'.
   92: &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 />'.
   93: &mt('If the error persists, please contact the [_1] for assistance.',$helpurl).
   94: '</p>');
   95:     return;
   96: }
   97: 
   98: #
   99: # --- Retrieve the parts from the metadata file.---
  100: sub getpartlist {
  101:     my ($symb,$errorref) = @_;
  102: 
  103:     my $navmap   = Apache::lonnavmaps::navmap->new();
  104:     unless (ref($navmap)) {
  105:         if (ref($errorref)) { 
  106:             $$errorref = 'navmap';
  107:             return;
  108:         }
  109:     }
  110:     my $res      = $navmap->getBySymb($symb);
  111:     my $partlist = $res->parts();
  112:     my $url      = $res->src();
  113:     my @metakeys = split(/,/,&Apache::lonnet::metadata($url,'keys'));
  114: 
  115:     my @stores;
  116:     foreach my $part (@{ $partlist }) {
  117: 	foreach my $key (@metakeys) {
  118: 	    if ($key =~ m/^stores_\Q$part\E_/) { push(@stores,$key); }
  119: 	}
  120:     }
  121:     return @stores;
  122: }
  123: 
  124: # --- Get the symbolic name of a problem and the url
  125: sub get_symb {
  126:     my ($request,$silent) = @_;
  127:     (my $url=$env{'form.url'}) =~ s-^http://($ENV{'SERVER_NAME'}|$ENV{'HTTP_HOST'})--;
  128:     my $symb=($env{'form.symb'} ne '' ? $env{'form.symb'} : (&Apache::lonnet::symbread($url)));
  129:     if ($symb eq '') { 
  130: 	if (!$silent) {
  131:             $request->print(&mt("Unable to handle ambiguous references: [_1].",$url));
  132: 	    return ();
  133: 	}
  134:     }
  135:     &Apache::lonenc::check_decrypt(\$symb);
  136:     return ($symb);
  137: }
  138: 
  139: #--- Format fullname, username:domain if different for display
  140: #--- Use anywhere where the student names are listed
  141: sub nameUserString {
  142:     my ($type,$fullname,$uname,$udom) = @_;
  143:     if ($type eq 'header') {
  144: 	return '<b>&nbsp;'.&mt('Fullname').'&nbsp;</b><span class="LC_internal_info">('.&mt('Username').')</span>';
  145:     } else {
  146: 	return '&nbsp;'.$fullname.'<span class="LC_internal_info">&nbsp;('.$uname.
  147: 	    ($env{'user.domain'} eq $udom ? '' : ' ('.$udom.')').')</span>';
  148:     }
  149: }
  150: 
  151: #--- Get the partlist and the response type for a given problem. ---
  152: #--- Indicate if a response type is coded handgraded or not. ---
  153: sub response_type {
  154:     my ($symb,$response_error) = @_;
  155: 
  156:     my $navmap = Apache::lonnavmaps::navmap->new();
  157:     unless (ref($navmap)) {
  158:         if (ref($response_error)) {
  159:             $$response_error = 1;
  160:         }
  161:         return;
  162:     }
  163:     my $res = $navmap->getBySymb($symb);
  164:     unless (ref($res)) {
  165:         $$response_error = 1;
  166:         return;
  167:     }
  168:     my $partlist = $res->parts();
  169:     my %vPart = 
  170: 	map { $_ => 1 } (&Apache::loncommon::get_env_multiple('form.vPart'));
  171:     my (%response_types,%handgrade);
  172:     foreach my $part (@{ $partlist }) {
  173: 	next if (%vPart && !exists($vPart{$part}));
  174: 
  175: 	my @types = $res->responseType($part);
  176: 	my @ids = $res->responseIds($part);
  177: 	for (my $i=0; $i < scalar(@ids); $i++) {
  178: 	    $response_types{$part}{$ids[$i]} = $types[$i];
  179: 	    $handgrade{$part.'_'.$ids[$i]} = 
  180: 		&Apache::lonnet::EXT('resource.'.$part.'_'.$ids[$i].
  181: 				     '.handgrade',$symb);
  182: 	}
  183:     }
  184:     return ($partlist,\%handgrade,\%response_types);
  185: }
  186: 
  187: sub flatten_responseType {
  188:     my ($responseType) = @_;
  189:     my @part_response_id =
  190: 	map { 
  191: 	    my $part = $_;
  192: 	    map {
  193: 		[$part,$_]
  194: 		} sort(keys(%{ $responseType->{$part} }));
  195: 	} sort(keys(%$responseType));
  196:     return @part_response_id;
  197: }
  198: 
  199: sub get_display_part {
  200:     my ($partID,$symb)=@_;
  201:     my $display=&Apache::lonnet::EXT('resource.'.$partID.'.display',$symb);
  202:     if (defined($display) and $display ne '') {
  203:         $display.= ' (<span class="LC_internal_info">'
  204:                   .&mt('Part ID: [_1]',$partID).'</span>)';
  205:     } else {
  206: 	$display=$partID;
  207:     }
  208:     return $display;
  209: }
  210: 
  211: #--- Show resource title
  212: #--- and parts and response type
  213: sub showResourceInfo {
  214:     my ($symb,$probTitle,$checkboxes,$res_error) = @_;
  215:     my $result = '<h3>'.&mt('Current Resource').': '.$probTitle.'</h3>'."\n";
  216:     my ($partlist,$handgrade,$responseType) = &response_type($symb,$res_error);
  217:     if (ref($res_error)) {
  218:         if ($$res_error) {
  219:             return;
  220:         }
  221:     }
  222:     $result.=&Apache::loncommon::start_data_table()
  223:             .&Apache::loncommon::start_data_table_header_row();
  224:     if ($checkboxes) {
  225:         $result.='<th>&nbsp;</th>';
  226:     }
  227:     $result.='<th>'.&mt('Problem Part').'</th>'
  228:             .'<th>'.&mt('Res. ID').'</th>'
  229:             .'<th>'.&mt('Type').'</th>'
  230:             .&Apache::loncommon::end_data_table_header_row();
  231:     my %resptype = ();
  232:     my $hdgrade='no';
  233:     my %partsseen;
  234:     foreach my $partID (sort(keys(%$responseType))) {
  235:         foreach my $resID (sort(keys(%{ $responseType->{$partID} }))) {
  236:             my $handgrade=$$handgrade{$partID.'_'.$resID};
  237:             my $responsetype = $responseType->{$partID}->{$resID};
  238:             $hdgrade = $handgrade if ($handgrade eq 'yes');
  239:             $result.=&Apache::loncommon::start_data_table_row();
  240:             if ($checkboxes) {
  241:                 if (exists($partsseen{$partID})) {
  242:                     $result.="<td>&nbsp;</td>";
  243:                 } else {
  244:                     $result.="<td><input type='checkbox' name='vPart' value='$partID' checked='checked' /></td>";
  245:                 }
  246:                 $partsseen{$partID}=1;
  247:             }
  248:             my $display_part=&get_display_part($partID,$symb);
  249:             $result.='<td>'.$display_part.'</td>'
  250:                     .'<td>'.'<span class="LC_internal_info">'.$resID.'</span></td>'
  251:                     .'<td>'.&mt($responsetype).'</td>'
  252: #                   .'<td>'.&mt('<b>Handgrade: </b>[_1]',$handgrade).'</td>'
  253:                     .&Apache::loncommon::end_data_table_row();
  254:         }
  255:     }
  256:     $result.=&Apache::loncommon::end_data_table();
  257:     return $result,$responseType,$hdgrade,$partlist,$handgrade;
  258: }
  259: 
  260: sub reset_caches {
  261:     &reset_analyze_cache();
  262:     &reset_perm();
  263: }
  264: 
  265: {
  266:     my %analyze_cache;
  267:     my %analyze_cache_formkeys;
  268: 
  269:     sub reset_analyze_cache {
  270: 	undef(%analyze_cache);
  271:         undef(%analyze_cache_formkeys);
  272:     }
  273: 
  274:     sub get_analyze {
  275: 	my ($symb,$uname,$udom,$no_increment,$add_to_hash,$type,$trial,$rndseed)=@_;
  276: 	my $key = "$symb\0$uname\0$udom";
  277:         if ($type eq 'randomizetry') {
  278:             if ($trial ne '') {
  279:                 $key .= "\0".$trial;
  280:             }
  281:         }
  282: 	if (exists($analyze_cache{$key})) {
  283:             my $getupdate = 0;
  284:             if (ref($add_to_hash) eq 'HASH') {
  285:                 foreach my $item (keys(%{$add_to_hash})) {
  286:                     if (ref($analyze_cache_formkeys{$key}) eq 'HASH') {
  287:                         if (!exists($analyze_cache_formkeys{$key}{$item})) {
  288:                             $getupdate = 1;
  289:                             last;
  290:                         }
  291:                     } else {
  292:                         $getupdate = 1;
  293:                     }
  294:                 }
  295:             }
  296:             if (!$getupdate) {
  297:                 return $analyze_cache{$key};
  298:             }
  299:         }
  300: 
  301: 	my (undef,undef,$url)=&Apache::lonnet::decode_symb($symb);
  302: 	$url=&Apache::lonnet::clutter($url);
  303:         my %form = ('grade_target'      => 'analyze',
  304:                     'grade_domain'      => $udom,
  305:                     'grade_symb'        => $symb,
  306:                     'grade_courseid'    =>  $env{'request.course.id'},
  307:                     'grade_username'    => $uname,
  308:                     'grade_noincrement' => $no_increment);
  309:         if ($type eq 'randomizetry') {
  310:             $form{'grade_questiontype'} = $type;
  311:             if ($rndseed ne '') {
  312:                 $form{'grade_rndseed'} = $rndseed;
  313:             }
  314:         }
  315:         if (ref($add_to_hash)) {
  316:             %form = (%form,%{$add_to_hash});
  317:         }
  318: 	my $subresult=&ssi_with_retries($url, $ssi_retries,%form);
  319: 	(undef,$subresult)=split(/_HASH_REF__/,$subresult,2);
  320: 	my %analyze=&Apache::lonnet::str2hash($subresult);
  321:         if (ref($add_to_hash) eq 'HASH') {
  322:             $analyze_cache_formkeys{$key} = $add_to_hash;
  323:         } else {
  324:             $analyze_cache_formkeys{$key} = {};
  325:         }
  326: 	return $analyze_cache{$key} = \%analyze;
  327:     }
  328: 
  329:     sub get_order {
  330: 	my ($partid,$respid,$symb,$uname,$udom,$no_increment,$type,$trial,$rndseed)=@_;
  331: 	my $analyze = &get_analyze($symb,$uname,$udom,$no_increment,undef,$type,$trial,$rndseed);
  332: 	return $analyze->{"$partid.$respid.shown"};
  333:     }
  334: 
  335:     sub get_radiobutton_correct_foil {
  336: 	my ($partid,$respid,$symb,$uname,$udom,$type,$trial,$rndseed)=@_;
  337: 	my $analyze = &get_analyze($symb,$uname,$udom,undef,undef,$type,$trial,$rndseed);
  338:         my $foils = &get_order($partid,$respid,$symb,$uname,$udom,undef,$type,$trial,$rndseed);
  339:         if (ref($foils) eq 'ARRAY') {
  340: 	    foreach my $foil (@{$foils}) {
  341: 	        if ($analyze->{"$partid.$respid.foil.value.$foil"} eq 'true') {
  342: 		    return $foil;
  343: 	        }
  344: 	    }
  345: 	}
  346:     }
  347: 
  348:     sub scantron_partids_tograde {
  349:         my ($resource,$cid,$uname,$udom,$check_for_randomlist) = @_;
  350:         my (%analysis,@parts);
  351:         if (ref($resource)) {
  352:             my $symb = $resource->symb();
  353:             my $add_to_form;
  354:             if ($check_for_randomlist) {
  355:                 $add_to_form = { 'check_parts_withrandomlist' => 1,};
  356:             }
  357:             my $analyze = &get_analyze($symb,$uname,$udom,undef,$add_to_form);
  358:             if (ref($analyze) eq 'HASH') {
  359:                 %analysis = %{$analyze};
  360:             }
  361:             if (ref($analysis{'parts'}) eq 'ARRAY') {
  362:                 foreach my $part (@{$analysis{'parts'}}) {
  363:                     my ($id,$respid) = split(/\./,$part);
  364:                     if (!&Apache::loncommon::check_if_partid_hidden($id,$symb,$udom,$uname)) {
  365:                         push(@parts,$part);
  366:                     }
  367:                 }
  368:             }
  369:         }
  370:         return (\%analysis,\@parts);
  371:     }
  372: 
  373: }
  374: 
  375: #--- Clean response type for display
  376: #--- Currently filters option/rank/radiobutton/match/essay/Task
  377: #        response types only.
  378: sub cleanRecord {
  379:     my ($answer,$response,$symb,$partid,$respid,$record,$order,$version,
  380: 	$uname,$udom,$type,$trial,$rndseed) = @_;
  381:     my $grayFont = '<span class="LC_internal_info">';
  382:     if ($response =~ /^(option|rank)$/) {
  383: 	my %answer=&Apache::lonnet::str2hash($answer);
  384: 	my %grading=&Apache::lonnet::str2hash($record->{$version."resource.$partid.$respid.submissiongrading"});
  385: 	my ($toprow,$bottomrow);
  386: 	foreach my $foil (@$order) {
  387: 	    if ($grading{$foil} == 1) {
  388: 		$toprow.='<td><b>'.$answer{$foil}.'&nbsp;</b></td>';
  389: 	    } else {
  390: 		$toprow.='<td><i>'.$answer{$foil}.'&nbsp;</i></td>';
  391: 	    }
  392: 	    $bottomrow.='<td>'.$grayFont.$foil.'</span>&nbsp;</td>';
  393: 	}
  394: 	return '<blockquote><table border="1">'.
  395: 	    '<tr valign="top"><td>'.&mt('Answer').'</td>'.$toprow.'</tr>'.
  396: 	    '<tr valign="top"><td>'.$grayFont.&mt('Option ID').'</span></td>'.
  397: 	    $bottomrow.'</tr></table></blockquote>';
  398:     } elsif ($response eq 'match') {
  399: 	my %answer=&Apache::lonnet::str2hash($answer);
  400: 	my %grading=&Apache::lonnet::str2hash($record->{$version."resource.$partid.$respid.submissiongrading"});
  401: 	my @items=&Apache::lonnet::str2array($record->{$version."resource.$partid.$respid.submissionitems"});
  402: 	my ($toprow,$middlerow,$bottomrow);
  403: 	foreach my $foil (@$order) {
  404: 	    my $item=shift(@items);
  405: 	    if ($grading{$foil} == 1) {
  406: 		$toprow.='<td><b>'.$item.'&nbsp;</b></td>';
  407: 		$middlerow.='<td><b>'.$grayFont.$answer{$foil}.'&nbsp;</span></b></td>';
  408: 	    } else {
  409: 		$toprow.='<td><i>'.$item.'&nbsp;</i></td>';
  410: 		$middlerow.='<td><i>'.$grayFont.$answer{$foil}.'&nbsp;</span></i></td>';
  411: 	    }
  412: 	    $bottomrow.='<td>'.$grayFont.$foil.'</span>&nbsp;</td>';
  413: 	}
  414: 	return '<blockquote><table border="1">'.
  415: 	    '<tr valign="top"><td>'.&mt('Answer').'</td>'.$toprow.'</tr>'.
  416: 	    '<tr valign="top"><td>'.$grayFont.&mt('Item ID').'</span></td>'.
  417: 	    $middlerow.'</tr>'.
  418: 	    '<tr valign="top"><td>'.$grayFont.&mt('Option ID').'</span></td>'.
  419: 	    $bottomrow.'</tr>'.'</table></blockquote>';
  420:     } elsif ($response eq 'radiobutton') {
  421: 	my %answer=&Apache::lonnet::str2hash($answer);
  422: 	my ($toprow,$bottomrow);
  423: 	my $correct = 
  424: 	    &get_radiobutton_correct_foil($partid,$respid,$symb,$uname,$udom,$type,$trial,$rndseed);
  425: 	foreach my $foil (@$order) {
  426: 	    if (exists($answer{$foil})) {
  427: 		if ($foil eq $correct) {
  428: 		    $toprow.='<td><b>'.&mt('true').'</b></td>';
  429: 		} else {
  430: 		    $toprow.='<td><i>'.&mt('true').'</i></td>';
  431: 		}
  432: 	    } else {
  433: 		$toprow.='<td>'.&mt('false').'</td>';
  434: 	    }
  435: 	    $bottomrow.='<td>'.$grayFont.$foil.'</span>&nbsp;</td>';
  436: 	}
  437: 	return '<blockquote><table border="1">'.
  438: 	    '<tr valign="top"><td>'.&mt('Answer').'</td>'.$toprow.'</tr>'.
  439: 	    '<tr valign="top"><td>'.$grayFont.&mt('Option ID').'</span></td>'.
  440: 	    $bottomrow.'</tr></table></blockquote>';
  441:     } elsif ($response eq 'essay') {
  442: 	if (! exists ($env{'form.'.$symb})) {
  443: 	    my (%keyhash) = &Apache::lonnet::dump('nohist_handgrade',
  444: 						  $env{'course.'.$env{'request.course.id'}.'.domain'},
  445: 						  $env{'course.'.$env{'request.course.id'}.'.num'});
  446: 
  447: 	    my $loginuser = $env{'user.name'}.':'.$env{'user.domain'};
  448: 	    $env{'form.keywords'} = $keyhash{$symb.'_keywords'} ne '' ? $keyhash{$symb.'_keywords'} : '';
  449: 	    $env{'form.kwclr'}    = $keyhash{$loginuser.'_kwclr'} ne '' ? $keyhash{$loginuser.'_kwclr'} : 'red';
  450: 	    $env{'form.kwsize'}   = $keyhash{$loginuser.'_kwsize'} ne '' ? $keyhash{$loginuser.'_kwsize'} : '0';
  451: 	    $env{'form.kwstyle'}  = $keyhash{$loginuser.'_kwstyle'} ne '' ? $keyhash{$loginuser.'_kwstyle'} : '';
  452: 	    $env{'form.'.$symb} = 1; # so that we don't have to read it from disk for multiple sub of the same prob.
  453: 	}
  454: 	$answer =~ s-\n-<br />-g;
  455: 	return '<br /><br /><blockquote><tt>'.&keywords_highlight($answer).'</tt></blockquote>';
  456:     } elsif ( $response eq 'organic') {
  457: 	my $result='Smile representation: "<tt>'.$answer.'</tt>"';
  458: 	my $jme=$record->{$version."resource.$partid.$respid.molecule"};
  459: 	$result.=&Apache::chemresponse::jme_img($jme,$answer,400);
  460: 	return $result;
  461:     } elsif ( $response eq 'Task') {
  462: 	if ( $answer eq 'SUBMITTED') {
  463: 	    my $files = $record->{$version."resource.$respid.$partid.bridgetask.portfiles"};
  464: 	    my $result = &Apache::bridgetask::file_list($files,$uname,$udom);
  465: 	    return $result;
  466: 	} elsif ( grep(/^\Q$version\E.*?\.instance$/, keys(%{$record})) ) {
  467: 	    my @matches = grep(/^\Q$version\E.*?\.instance$/,
  468: 			       keys(%{$record}));
  469: 	    return join('<br />',($version,@matches));
  470: 			       
  471: 			       
  472: 	} else {
  473: 	    my $result =
  474: 		'<p>'
  475: 		.&mt('Overall result: [_1]',
  476: 		     $record->{$version."resource.$respid.$partid.status"})
  477: 		.'</p>';
  478: 	    
  479: 	    $result .= '<ul>';
  480: 	    my @grade = grep(/^\Q${version}resource.$respid.$partid.\E[^.]*[.]status$/,
  481: 			     keys(%{$record}));
  482: 	    foreach my $grade (sort(@grade)) {
  483: 		my ($dim) = ($grade =~/[.]([^.]+)[.]status$/);
  484: 		$result.= '<li>'.&mt("Dimension: [_1], status [_2] ",
  485: 				     $dim, $record->{$grade}).
  486: 			  '</li>';
  487: 	    }
  488: 	    $result.='</ul>';
  489: 	    return $result;
  490: 	}
  491:     } elsif ( $response =~ m/(?:numerical|formula)/) {
  492: 	$answer = 
  493: 	    &Apache::loncommon::format_previous_attempt_value('submission',
  494: 							      $answer);
  495:     }
  496:     return $answer;
  497: }
  498: 
  499: #-- A couple of common js functions
  500: sub commonJSfunctions {
  501:     my $request = shift;
  502:     $request->print(<<COMMONJSFUNCTIONS);
  503: <script type="text/javascript" language="javascript">
  504:     function radioSelection(radioButton) {
  505: 	var selection=null;
  506: 	if (radioButton.length > 1) {
  507: 	    for (var i=0; i<radioButton.length; i++) {
  508: 		if (radioButton[i].checked) {
  509: 		    return radioButton[i].value;
  510: 		}
  511: 	    }
  512: 	} else {
  513: 	    if (radioButton.checked) return radioButton.value;
  514: 	}
  515: 	return selection;
  516:     }
  517: 
  518:     function pullDownSelection(selectOne) {
  519: 	var selection="";
  520: 	if (selectOne.length > 1) {
  521: 	    for (var i=0; i<selectOne.length; i++) {
  522: 		if (selectOne[i].selected) {
  523: 		    return selectOne[i].value;
  524: 		}
  525: 	    }
  526: 	} else {
  527:             // only one value it must be the selected one
  528: 	    return selectOne.value;
  529: 	}
  530:     }
  531: </script>
  532: COMMONJSFUNCTIONS
  533: }
  534: 
  535: #--- Dumps the class list with usernames,list of sections,
  536: #--- section, ids and fullnames for each user.
  537: sub getclasslist {
  538:     my ($getsec,$filterlist,$getgroup) = @_;
  539:     my @getsec;
  540:     my @getgroup;
  541:     my $stu_status = join(':',&Apache::loncommon::get_env_multiple('form.Status'));
  542:     if (!ref($getsec)) {
  543: 	if ($getsec ne '' && $getsec ne 'all') {
  544: 	    @getsec=($getsec);
  545: 	}
  546:     } else {
  547: 	@getsec=@{$getsec};
  548:     }
  549:     if (grep(/^all$/,@getsec)) { undef(@getsec); }
  550:     if (!ref($getgroup)) {
  551: 	if ($getgroup ne '' && $getgroup ne 'all') {
  552: 	    @getgroup=($getgroup);
  553: 	}
  554:     } else {
  555: 	@getgroup=@{$getgroup};
  556:     }
  557:     if (grep(/^all$/,@getgroup)) { undef(@getgroup); }
  558: 
  559:     my ($classlist,$keylist)=&Apache::loncoursedata::get_classlist();
  560:     # Bail out if we were unable to get the classlist
  561:     return if (! defined($classlist));
  562:     &Apache::loncoursedata::get_group_memberships($classlist,$keylist);
  563:     #
  564:     my %sections;
  565:     my %fullnames;
  566:     foreach my $student (keys(%$classlist)) {
  567:         my $end      = 
  568:             $classlist->{$student}->[&Apache::loncoursedata::CL_END()];
  569:         my $start    = 
  570:             $classlist->{$student}->[&Apache::loncoursedata::CL_START()];
  571:         my $id       = 
  572:             $classlist->{$student}->[&Apache::loncoursedata::CL_ID()];
  573:         my $section  = 
  574:             $classlist->{$student}->[&Apache::loncoursedata::CL_SECTION()];
  575:         my $fullname = 
  576:             $classlist->{$student}->[&Apache::loncoursedata::CL_FULLNAME()];
  577:         my $status   = 
  578:             $classlist->{$student}->[&Apache::loncoursedata::CL_STATUS()];
  579:         my $group   = 
  580:             $classlist->{$student}->[&Apache::loncoursedata::CL_GROUP()];
  581: 	# filter students according to status selected
  582: 	if ($filterlist && (!($stu_status =~ /Any/))) {
  583: 	    if (!($stu_status =~ $status)) {
  584: 		delete($classlist->{$student});
  585: 		next;
  586: 	    }
  587: 	}
  588: 	# filter students according to groups selected
  589: 	my @stu_groups = split(/,/,$group);
  590: 	if (@getgroup) {
  591: 	    my $exclude = 1;
  592: 	    foreach my $grp (@getgroup) {
  593: 	        foreach my $stu_group (@stu_groups) {
  594: 	            if ($stu_group eq $grp) {
  595: 	                $exclude = 0;
  596:     	            } 
  597: 	        }
  598:     	        if (($grp eq 'none') && !$group) {
  599:         	        $exclude = 0;
  600:         	}
  601: 	    }
  602: 	    if ($exclude) {
  603: 	        delete($classlist->{$student});
  604: 	    }
  605: 	}
  606: 	$section = ($section ne '' ? $section : 'none');
  607: 	if (&canview($section)) {
  608: 	    if (!@getsec || grep(/^\Q$section\E$/,@getsec)) {
  609: 		$sections{$section}++;
  610: 		if ($classlist->{$student}) {
  611: 		    $fullnames{$student}=$fullname;
  612: 		}
  613: 	    } else {
  614: 		delete($classlist->{$student});
  615: 	    }
  616: 	} else {
  617: 	    delete($classlist->{$student});
  618: 	}
  619:     }
  620:     my %seen = ();
  621:     my @sections = sort(keys(%sections));
  622:     return ($classlist,\@sections,\%fullnames);
  623: }
  624: 
  625: sub canmodify {
  626:     my ($sec)=@_;
  627:     if ($perm{'mgr'}) {
  628: 	if (!defined($perm{'mgr_section'})) {
  629: 	    # can modify whole class
  630: 	    return 1;
  631: 	} else {
  632: 	    if ($sec eq $perm{'mgr_section'}) {
  633: 		#can modify the requested section
  634: 		return 1;
  635: 	    } else {
  636: 		# can't modify the request section
  637: 		return 0;
  638: 	    }
  639: 	}
  640:     }
  641:     #can't modify
  642:     return 0;
  643: }
  644: 
  645: sub canview {
  646:     my ($sec)=@_;
  647:     if ($perm{'vgr'}) {
  648: 	if (!defined($perm{'vgr_section'})) {
  649: 	    # can modify whole class
  650: 	    return 1;
  651: 	} else {
  652: 	    if ($sec eq $perm{'vgr_section'}) {
  653: 		#can modify the requested section
  654: 		return 1;
  655: 	    } else {
  656: 		# can't modify the request section
  657: 		return 0;
  658: 	    }
  659: 	}
  660:     }
  661:     #can't modify
  662:     return 0;
  663: }
  664: 
  665: #--- Retrieve the grade status of a student for all the parts
  666: sub student_gradeStatus {
  667:     my ($symb,$udom,$uname,$partlist) = @_;
  668:     my %record     = &Apache::lonnet::restore($symb,$env{'request.course.id'},$udom,$uname);
  669:     my %partstatus = ();
  670:     foreach (@$partlist) {
  671: 	my ($status,undef)   = split(/_/,$record{"resource.$_.solved"},2);
  672: 	$status              = 'nothing' if ($status eq '');
  673: 	$partstatus{$_}      = $status;
  674: 	my $subkey           = "resource.$_.submitted_by";
  675: 	$partstatus{$subkey} = $record{$subkey} if ($record{$subkey} ne '');
  676:     }
  677:     return %partstatus;
  678: }
  679: 
  680: # hidden form and javascript that calls the form
  681: # Use by verifyscript and viewgrades
  682: # Shows a student's view of problem and submission
  683: sub jscriptNform {
  684:     my ($symb) = @_;
  685:     my $stu_status = join(':',&Apache::loncommon::get_env_multiple('form.Status'));
  686:     my $jscript='<script type="text/javascript" language="javascript">'."\n".
  687: 	'    function viewOneStudent(user,domain) {'."\n".
  688: 	'	document.onestudent.student.value = user;'."\n".
  689: 	'	document.onestudent.userdom.value = domain;'."\n".
  690: 	'	document.onestudent.submit();'."\n".
  691: 	'    }'."\n".
  692: 	'</script>'."\n";
  693:     $jscript.= '<form action="/adm/grades" method="post" name="onestudent">'."\n".
  694: 	'<input type="hidden" name="symb"    value="'.&Apache::lonenc::check_encrypt($symb).'" />'."\n".
  695: 	'<input type="hidden" name="saveState" value="'.$env{'form.saveState'}.'" />'."\n".
  696: 	'<input type="hidden" name="probTitle" value="'.$env{'form.probTitle'}.'" />'."\n".
  697: 	'<input type="hidden" name="Status"  value="'.$stu_status.'" />'."\n".
  698: 	'<input type="hidden" name="command" value="submission" />'."\n".
  699: 	'<input type="hidden" name="student" value="" />'."\n".
  700: 	'<input type="hidden" name="userdom" value="" />'."\n".
  701: 	'</form>'."\n";
  702:     return $jscript;
  703: }
  704: 
  705: 
  706: 
  707: # Given the score (as a number [0-1] and the weight) what is the final
  708: # point value? This function will round to the nearest tenth, third,
  709: # or quarter if one of those is within the tolerance of .00001.
  710: sub compute_points {
  711:     my ($score, $weight) = @_;
  712:     
  713:     my $tolerance = .00001;
  714:     my $points = $score * $weight;
  715: 
  716:     # Check for nearness to 1/x.
  717:     my $check_for_nearness = sub {
  718:         my ($factor) = @_;
  719:         my $num = ($points * $factor) + $tolerance;
  720:         my $floored_num = floor($num);
  721:         if ($num - $floored_num < 2 * $tolerance * $factor) {
  722:             return $floored_num / $factor;
  723:         }
  724:         return $points;
  725:     };
  726: 
  727:     $points = $check_for_nearness->(10);
  728:     $points = $check_for_nearness->(3);
  729:     $points = $check_for_nearness->(4);
  730:     
  731:     return $points;
  732: }
  733: 
  734: #------------------ End of general use routines --------------------
  735: 
  736: #
  737: # Find most similar essay
  738: #
  739: 
  740: sub most_similar {
  741:     my ($uname,$udom,$uessay,$old_essays)=@_;
  742: 
  743: # ignore spaces and punctuation
  744: 
  745:     $uessay=~s/\W+/ /gs;
  746: 
  747: # ignore empty submissions (occuring when only files are sent)
  748: 
  749:     unless ($uessay=~/\w+/s) { return ''; }
  750: 
  751: # these will be returned. Do not care if not at least 50 percent similar
  752:     my $limit=0.6;
  753:     my $sname='';
  754:     my $sdom='';
  755:     my $scrsid='';
  756:     my $sessay='';
  757: # go through all essays ...
  758:     foreach my $tkey (keys(%$old_essays)) {
  759: 	my ($tname,$tdom,$tcrsid)=map {&unescape($_)} (split(/\./,$tkey));
  760: # ... except the same student
  761:         next if (($tname eq $uname) && ($tdom eq $udom));
  762: 	my $tessay=$old_essays->{$tkey};
  763: 	$tessay=~s/\W+/ /gs;
  764: # String similarity gives up if not even limit
  765: 	my $tsimilar=&String::Similarity::similarity($uessay,$tessay,$limit);
  766: # Found one
  767: 	if ($tsimilar>$limit) {
  768: 	    $limit=$tsimilar;
  769: 	    $sname=$tname;
  770: 	    $sdom=$tdom;
  771: 	    $scrsid=$tcrsid;
  772: 	    $sessay=$old_essays->{$tkey};
  773: 	}
  774:     }
  775:     if ($limit>0.6) {
  776:        return ($sname,$sdom,$scrsid,$sessay,$limit);
  777:     } else {
  778:        return ('','','','',0);
  779:     }
  780: }
  781: 
  782: #-------------------------------------------------------------------
  783: 
  784: #------------------------------------ Receipt Verification Routines
  785: #
  786: #--- Check whether a receipt number is valid.---
  787: sub verifyreceipt {
  788:     my $request  = shift;
  789: 
  790:     my $courseid = $env{'request.course.id'};
  791:     my $receipt  = &Apache::lonnet::recprefix($courseid).'-'.
  792: 	$env{'form.receipt'};
  793:     $receipt     =~ s/[^\-\d]//g;
  794:     my ($symb)   = &get_symb($request);
  795: 
  796:     my $title.=
  797: 	'<h3><span class="LC_info">'.
  798: 	&mt('Verifying Receipt No. [_1]',$receipt).
  799: 	'</span></h3>'."\n".
  800: 	'<h4>'.&mt('<b>Resource: </b>[_1]',$env{'form.probTitle'}).
  801: 	'</h4>'."\n";
  802: 
  803:     my ($string,$contents,$matches) = ('','',0);
  804:     my (undef,undef,$fullname) = &getclasslist('all','0');
  805:     
  806:     my $receiptparts=0;
  807:     if ($env{"course.$courseid.receiptalg"} eq 'receipt2' ||
  808: 	$env{"course.$courseid.receiptalg"} eq 'receipt3') { $receiptparts=1; }
  809:     my $parts=['0'];
  810:     if ($receiptparts) {
  811:         my $res_error; 
  812:         ($parts)=&response_type($symb,\$res_error);
  813:         if ($res_error) {
  814:             return &navmap_errormsg();
  815:         } 
  816:     }
  817:     
  818:     my $header = 
  819: 	&Apache::loncommon::start_data_table().
  820: 	&Apache::loncommon::start_data_table_header_row().
  821: 	'<th>&nbsp;'.&mt('Fullname').'&nbsp;</th>'."\n".
  822: 	'<th>&nbsp;'.&mt('Username').'&nbsp;</th>'."\n".
  823: 	'<th>&nbsp;'.&mt('Domain').'&nbsp;</th>';
  824:     if ($receiptparts) {
  825: 	$header.='<th>&nbsp;'.&mt('Problem Part').'&nbsp;</th>';
  826:     }
  827:     $header.=
  828: 	&Apache::loncommon::end_data_table_header_row();
  829: 
  830:     foreach (sort 
  831: 	     {
  832: 		 if (lc($$fullname{$a}) ne lc($$fullname{$b})) {
  833: 		     return (lc($$fullname{$a}) cmp lc($$fullname{$b}));
  834: 		 }
  835: 		 return $a cmp $b;
  836: 	     } (keys(%$fullname))) {
  837: 	my ($uname,$udom)=split(/\:/);
  838: 	foreach my $part (@$parts) {
  839: 	    if ($receipt eq &Apache::lonnet::ireceipt($uname,$udom,$courseid,$symb,$part)) {
  840: 		$contents.=
  841: 		    &Apache::loncommon::start_data_table_row().
  842: 		    '<td>&nbsp;'."\n".
  843: 		    '<a href="javascript:viewOneStudent(\''.$uname.'\',\''.$udom.
  844: 		    '\');" target="_self">'.$$fullname{$_}.'</a>&nbsp;</td>'."\n".
  845: 		    '<td>&nbsp;'.$uname.'&nbsp;</td>'.
  846: 		    '<td>&nbsp;'.$udom.'&nbsp;</td>';
  847: 		if ($receiptparts) {
  848: 		    $contents.='<td>&nbsp;'.$part.'&nbsp;</td>';
  849: 		}
  850: 		$contents.= 
  851: 		    &Apache::loncommon::end_data_table_row()."\n";
  852: 		
  853: 		$matches++;
  854: 	    }
  855: 	}
  856:     }
  857:     if ($matches == 0) {
  858:         $string = $title
  859:                  .'<p class="LC_warning">'
  860:                  .&mt('No match found for the above receipt number.')
  861:                  .'</p>';
  862:     } else {
  863: 	$string = &jscriptNform($symb).$title.
  864: 	    '<p>'.
  865: 	    &mt('The above receipt number matches the following [quant,_1,student].',$matches).
  866: 	    '</p>'.
  867: 	    $header.
  868: 	    $contents.
  869: 	    &Apache::loncommon::end_data_table()."\n";
  870:     }
  871:     return $string.&show_grading_menu_form($symb);
  872: }
  873: 
  874: #--- This is called by a number of programs.
  875: #--- Called from the Grading Menu - View/Grade an individual student
  876: #--- Also called directly when one clicks on the subm button 
  877: #    on the problem page.
  878: sub listStudents {
  879:     my ($request) = shift;
  880: 
  881:     my ($symb) = &get_symb($request);
  882:     my $cdom      = $env{"course.$env{'request.course.id'}.domain"};
  883:     my $cnum      = $env{"course.$env{'request.course.id'}.num"};
  884:     my $getsec    = $env{'form.section'} eq '' ? 'all' : $env{'form.section'};
  885:     my $getgroup  = $env{'form.group'} eq '' ? 'all' : $env{'form.group'};
  886:     my $submitonly= $env{'form.submitonly'} eq '' ? 'all' : $env{'form.submitonly'};
  887:     my $viewgrade = $env{'form.showgrading'} eq 'yes' ? 'View/Grade/Regrade' : 'View';
  888:     $env{'form.probTitle'} = $env{'form.probTitle'} eq '' ? 
  889: 	&Apache::lonnet::gettitle($symb) : $env{'form.probTitle'};
  890: 
  891:     my $result='<h3><span class="LC_info">&nbsp;'
  892: 	.&mt("$viewgrade Submissions for a Student or a Group of Students")
  893: 	.'</span></h3>';
  894: 
  895:     my ($table,undef,$hdgrade,$partlist,$handgrade) = &showResourceInfo($symb,$env{'form.probTitle'},($env{'form.showgrading'} eq 'yes'));
  896: 
  897:     my %lt = &Apache::lonlocal::texthash (
  898: 		'multiple' => 'Please select a student or group of students before clicking on the Next button.',
  899: 		'single'   => 'Please select the student before clicking on the Next button.',
  900: 	     );
  901:     $request->print(<<LISTJAVASCRIPT);
  902: <script type="text/javascript" language="javascript">
  903:     function checkSelect(checkBox) {
  904: 	var ctr=0;
  905: 	var sense="";
  906: 	if (checkBox.length > 1) {
  907: 	    for (var i=0; i<checkBox.length; i++) {
  908: 		if (checkBox[i].checked) {
  909: 		    ctr++;
  910: 		}
  911: 	    }
  912: 	    sense = '$lt{'multiple'}';
  913: 	} else {
  914: 	    if (checkBox.checked) {
  915: 		ctr = 1;
  916: 	    }
  917: 	    sense = '$lt{'single'}';
  918: 	}
  919: 	if (ctr == 0) {
  920: 	    alert(sense);
  921: 	    return false;
  922: 	}
  923: 	document.gradesub.submit();
  924:     }
  925: 
  926:     function reLoadList(formname) {
  927: 	if (formname.saveStatusOld.value == pullDownSelection(formname.Status)) {return;}
  928: 	formname.command.value = 'submission';
  929: 	formname.submit();
  930:     }
  931: </script>
  932: LISTJAVASCRIPT
  933: 
  934:     &commonJSfunctions($request);
  935:     $request->print($result);
  936: 
  937:     my $checkhdgrade = ($env{'form.handgrade'} eq 'yes' && scalar(@$partlist) > 1 ) ? 'checked="checked"' : '';
  938:     my $checklastsub = $checkhdgrade eq '' ? 'checked="checked"' : '';
  939:     my $gradeTable='<form action="/adm/grades" method="post" name="gradesub">'.
  940: 	"\n".$table;
  941: 	
  942:     $gradeTable .= &Apache::lonhtmlcommon::start_pick_box();
  943:     $gradeTable .= &Apache::lonhtmlcommon::row_title(&mt('View Problem Text'))
  944:                   .'<label><input type="radio" name="vProb" value="no" checked="checked" /> '.&mt('no').' </label>'."\n"
  945:                   .'<label><input type="radio" name="vProb" value="yes" /> '.&mt('one student').' </label>'."\n"
  946:                   .'<label><input type="radio" name="vProb" value="all" /> '.&mt('all students').' </label><br />'."\n"
  947:                   .&Apache::lonhtmlcommon::row_closure();
  948:     $gradeTable .= &Apache::lonhtmlcommon::row_title(&mt('View Answer'))
  949:                   .'<label><input type="radio" name="vAns" value="no"  /> '.&mt('no').' </label>'."\n"
  950:                   .'<label><input type="radio" name="vAns" value="yes" /> '.&mt('one student').' </label>'."\n"
  951:                   .'<label><input type="radio" name="vAns" value="all" checked="checked" /> '.&mt('all students').' </label><br />'."\n"
  952:                   .&Apache::lonhtmlcommon::row_closure();
  953: 
  954:     my $submission_options;
  955:     if ($env{'form.handgrade'} eq 'yes' && scalar(@$partlist) > 1) {
  956: 	$submission_options.=
  957: 	    '<label><input type="radio" name="lastSub" value="hdgrade" '.$checkhdgrade.' /> '.&mt('essay part only').' </label>'."\n";
  958:     }
  959:     my $stu_status = join(':',&Apache::loncommon::get_env_multiple('form.Status'));
  960:     my $saveStatus = $stu_status eq '' ? 'Active' : $stu_status;
  961:     $env{'form.Status'} = $saveStatus;
  962:     $submission_options.=
  963:         '<span class="LC_nobreak">'.
  964:         '<label><input type="radio" name="lastSub" value="lastonly" '.$checklastsub.' /> '.
  965:         &mt('last submission only').' </label></span>'."\n".
  966:         '<span class="LC_nobreak">'.
  967:         '<label><input type="radio" name="lastSub" value="last" /> '.
  968:         &mt('last submission &amp; parts info').' </label></span>'."\n".
  969:         '<span class="LC_nobreak">'.
  970:         '<label><input type="radio" name="lastSub" value="datesub" /> '.
  971:         &mt('by dates and submissions').'</label></span>'."\n".
  972:         '<span class="LC_nobreak">'.
  973:         '<label><input type="radio" name="lastSub" value="all" /> '.
  974:         &mt('all details').'</label></span>';
  975:     $gradeTable .= &Apache::lonhtmlcommon::row_title(&mt('Submissions'))
  976:                   .$submission_options
  977:                   .&Apache::lonhtmlcommon::row_closure();
  978: 
  979:     $gradeTable .= &Apache::lonhtmlcommon::row_title(&mt('Grading Increments'))
  980:                   .'<select name="increment">'
  981:                   .'<option value="1">'.&mt('Whole Points').'</option>'
  982:                   .'<option value=".5">'.&mt('Half Points').'</option>'
  983:                   .'<option value=".25">'.&mt('Quarter Points').'</option>'
  984:                   .'<option value=".1">'.&mt('Tenths of a Point').'</option>'
  985:                   .'</select>'
  986:                   .&Apache::lonhtmlcommon::row_closure();
  987: 
  988:     $gradeTable .= 
  989:         &build_section_inputs().
  990: 	'<input type="hidden" name="submitonly"  value="'.$submitonly.'" />'."\n".
  991: 	'<input type="hidden" name="handgrade"   value="'.$env{'form.handgrade'}.'" /><br />'."\n".
  992: 	'<input type="hidden" name="showgrading" value="'.$env{'form.showgrading'}.'" /><br />'."\n".
  993: 	'<input type="hidden" name="saveState"   value="'.$env{'form.saveState'}.'" />'."\n".
  994: 	'<input type="hidden" name="probTitle"   value="'.$env{'form.probTitle'}.'" />'."\n".
  995: 	'<input type="hidden" name="symb" value="'.&Apache::lonenc::check_encrypt($symb).'" />'."\n".
  996: 	'<input type="hidden" name="saveStatusOld" value="'.$saveStatus.'" />'."\n";
  997: 
  998:     if (exists($env{'form.gradingMenu'}) && exists($env{'form.Status'})) {
  999: 	$gradeTable .= '<input type="hidden" name="Status" value="'.$stu_status.'" />'."\n";
 1000:     } else {
 1001:         $gradeTable .= &Apache::lonhtmlcommon::row_title(&mt('Student Status'))
 1002:                       .&Apache::lonhtmlcommon::StatusOptions(
 1003:                            $saveStatus,undef,1,'javascript:reLoadList(this.form);')
 1004:                       .&Apache::lonhtmlcommon::row_closure();
 1005:     }
 1006: 
 1007:     $gradeTable .= &Apache::lonhtmlcommon::row_title(&mt('Check For Plagiarism'))
 1008:                   .'<input type="checkbox" name="checkPlag" checked="checked" />'
 1009:                   .&Apache::lonhtmlcommon::row_closure(1)
 1010:                   .&Apache::lonhtmlcommon::end_pick_box();
 1011: 
 1012:     $gradeTable .= '<p>'
 1013:                   .&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"
 1014:                   .'<input type="hidden" name="command" value="processGroup" />'
 1015:                   .'</p>';
 1016: 
 1017: # checkall buttons
 1018:     $gradeTable.=&check_script('gradesub', 'stuinfo');
 1019:     $gradeTable.='<input type="button" '."\n".
 1020:         'onclick="javascript:checkSelect(this.form.stuinfo);" '."\n".
 1021:         'value="'.&mt('Next').' &rarr;" /> <br />'."\n";
 1022:     $gradeTable.=&check_buttons();
 1023:     my ($classlist, undef, $fullname) = &getclasslist($getsec,'1',$getgroup);
 1024:     $gradeTable.= &Apache::loncommon::start_data_table().
 1025: 	&Apache::loncommon::start_data_table_header_row();
 1026:     my $loop = 0;
 1027:     while ($loop < 2) {
 1028: 	$gradeTable.='<th>'.&mt('No.').'</th><th>'.&mt('Select').'</th>'.
 1029: 	    '<th>'.&nameUserString('header').'&nbsp;'.&mt('Section/Group').'</th>';
 1030: 	if ($env{'form.showgrading'} eq 'yes' 
 1031: 	    && $submitonly ne 'queued'
 1032: 	    && $submitonly ne 'all') {
 1033: 	    foreach my $part (sort(@$partlist)) {
 1034: 		my $display_part=
 1035: 		    &get_display_part((split(/_/,$part))[0],$symb);
 1036: 		$gradeTable.=
 1037: 		    '<th>'.&mt('Part: [_1] Status',$display_part).'</th>';
 1038: 	    }
 1039: 	} elsif ($submitonly eq 'queued') {
 1040: 	    $gradeTable.='<th>'.&mt('Queue Status').'&nbsp;</th>';
 1041: 	}
 1042: 	$loop++;
 1043: #	$gradeTable.='<td></td>' if ($loop%2 ==1);
 1044:     }
 1045:     $gradeTable.=&Apache::loncommon::end_data_table_header_row()."\n";
 1046: 
 1047:     my $ctr = 0;
 1048:     foreach my $student (sort 
 1049: 			 {
 1050: 			     if (lc($$fullname{$a}) ne lc($$fullname{$b})) {
 1051: 				 return (lc($$fullname{$a}) cmp lc($$fullname{$b}));
 1052: 			     }
 1053: 			     return $a cmp $b;
 1054: 			 }
 1055: 			 (keys(%$fullname))) {
 1056: 	my ($uname,$udom) = split(/:/,$student);
 1057: 
 1058: 	my %status = ();
 1059: 
 1060: 	if ($submitonly eq 'queued') {
 1061: 	    my %queue_status = 
 1062: 		&Apache::bridgetask::get_student_status($symb,$cdom,$cnum,
 1063: 							$udom,$uname);
 1064: 	    next if (!defined($queue_status{'gradingqueue'}));
 1065: 	    $status{'gradingqueue'} = $queue_status{'gradingqueue'};
 1066: 	}
 1067: 
 1068: 	if ($env{'form.showgrading'} eq 'yes' 
 1069: 	    && $submitonly ne 'queued'
 1070: 	    && $submitonly ne 'all') {
 1071: 	    (%status) =&student_gradeStatus($symb,$udom,$uname,$partlist);
 1072: 	    my $submitted = 0;
 1073: 	    my $graded = 0;
 1074: 	    my $incorrect = 0;
 1075: 	    foreach (keys(%status)) {
 1076: 		$submitted = 1 if ($status{$_} ne 'nothing');
 1077: 		$graded = 1 if ($status{$_} =~ /^ungraded/);
 1078: 		$incorrect = 1 if ($status{$_} =~ /^incorrect/);
 1079: 		
 1080: 		my ($foo,$partid,$foo1) = split(/\./,$_);
 1081: 		if ($status{'resource.'.$partid.'.submitted_by'} ne '') {
 1082: 		    $submitted = 0;
 1083: 		    my ($part)=split(/\./,$partid);
 1084: 		    $gradeTable.='<input type="hidden" name="'.
 1085: 			$student.':'.$part.':submitted_by" value="'.
 1086: 			$status{'resource.'.$partid.'.submitted_by'}.'" />';
 1087: 		}
 1088: 	    }
 1089: 	    
 1090: 	    next if (!$submitted && ($submitonly eq 'yes' ||
 1091: 				     $submitonly eq 'incorrect' ||
 1092: 				     $submitonly eq 'graded'));
 1093: 	    next if (!$graded && ($submitonly eq 'graded'));
 1094: 	    next if (!$incorrect && $submitonly eq 'incorrect');
 1095: 	}
 1096: 
 1097: 	$ctr++;
 1098: 	my $section = $classlist->{$student}->[&Apache::loncoursedata::CL_SECTION()];
 1099:         my $group = $classlist->{$student}->[&Apache::loncoursedata::CL_GROUP()];
 1100: 	if ( $perm{'vgr'} eq 'F' ) {
 1101: 	    if ($ctr%2 ==1) {
 1102: 		$gradeTable.= &Apache::loncommon::start_data_table_row();
 1103: 	    }
 1104: 	    $gradeTable.='<td align="right">'.$ctr.'&nbsp;</td>'.
 1105:                '<td align="center"><label><input type="checkbox" name="stuinfo" value="'.
 1106:                $student.':'.$$fullname{$student}.':::SECTION'.$section.
 1107: 	       ')&nbsp;" />&nbsp;&nbsp;</label></td>'."\n".'<td>'.
 1108: 	       &nameUserString(undef,$$fullname{$student},$uname,$udom).
 1109: 	       '&nbsp;'.$section.($group ne '' ?'/'.$group:'').'</td>'."\n";
 1110: 
 1111: 	    if ($env{'form.showgrading'} eq 'yes' && $submitonly ne 'all') {
 1112: 		foreach (sort(keys(%status))) {
 1113: 		    next if ($_ =~ /^resource.*?submitted_by$/);
 1114: 		    $gradeTable.='<td align="center">&nbsp;'.&mt($status{$_}).'&nbsp;</td>'."\n";
 1115: 		}
 1116: 	    }
 1117: #	    $gradeTable.='<td></td>' if ($ctr%2 ==1);
 1118: 	    if ($ctr%2 ==0) {
 1119: 		$gradeTable.=&Apache::loncommon::end_data_table_row()."\n";
 1120: 	    }
 1121: 	}
 1122:     }
 1123:     if ($ctr%2 ==1) {
 1124: 	$gradeTable.='<td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td>';
 1125: 	    if ($env{'form.showgrading'} eq 'yes' 
 1126: 		&& $submitonly ne 'queued'
 1127: 		&& $submitonly ne 'all') {
 1128: 		foreach (@$partlist) {
 1129: 		    $gradeTable.='<td>&nbsp;</td>';
 1130: 		}
 1131: 	    } elsif ($submitonly eq 'queued') {
 1132: 		$gradeTable.='<td>&nbsp;</td>';
 1133: 	    }
 1134: 	$gradeTable.=&Apache::loncommon::end_data_table_row();
 1135:     }
 1136: 
 1137:     $gradeTable.=&Apache::loncommon::end_data_table()."\n".
 1138:         '<input type="button" '.
 1139:         'onclick="javascript:checkSelect(this.form.stuinfo);" '.
 1140:         'value="'.&mt('Next').' &rarr;" /></form>'."\n";
 1141:     if ($ctr == 0) {
 1142: 	my $num_students=(scalar(keys(%$fullname)));
 1143: 	if ($num_students eq 0) {
 1144: 	    $gradeTable='<br />&nbsp;<span class="LC_warning">'.&mt('There are no students currently enrolled.').'</span>';
 1145: 	} else {
 1146: 	    my $submissions='submissions';
 1147: 	    if ($submitonly eq 'incorrect') { $submissions = 'incorrect submissions'; }
 1148: 	    if ($submitonly eq 'graded'   ) { $submissions = 'ungraded submissions'; }
 1149: 	    if ($submitonly eq 'queued'   ) { $submissions = 'queued submissions'; }
 1150: 	    $gradeTable='<br />&nbsp;<span class="LC_warning">'.
 1151: 		&mt('No '.$submissions.' found for this resource for any students. ([_1] students checked for '.$submissions.')',
 1152: 		    $num_students).
 1153: 		'</span><br />';
 1154: 	}
 1155:     } elsif ($ctr == 1) {
 1156: 	$gradeTable =~ s/type="checkbox"/type="checkbox" checked="checked"/;
 1157:     }
 1158:     $gradeTable.=&show_grading_menu_form($symb);
 1159:     $request->print($gradeTable);
 1160:     return '';
 1161: }
 1162: 
 1163: #---- Called from the listStudents routine
 1164: 
 1165: sub check_script {
 1166:     my ($form, $type)=@_;
 1167:     my $chkallscript='<script type="text/javascript">
 1168:     function checkall() {
 1169:         for (i=0; i<document.forms.'.$form.'.elements.length; i++) {
 1170:             ele = document.forms.'.$form.'.elements[i];
 1171:             if (ele.name == "'.$type.'") {
 1172:             document.forms.'.$form.'.elements[i].checked=true;
 1173:                                        }
 1174:         }
 1175:     }
 1176: 
 1177:     function checksec() {
 1178:         for (i=0; i<document.forms.'.$form.'.elements.length; i++) {
 1179:             ele = document.forms.'.$form.'.elements[i];
 1180:            string = document.forms.'.$form.'.chksec.value;
 1181:            if
 1182:           (ele.value.indexOf(":::SECTION"+string)>0) {
 1183:               document.forms.'.$form.'.elements[i].checked=true;
 1184:             }
 1185:         }
 1186:     }
 1187: 
 1188: 
 1189:     function uncheckall() {
 1190:         for (i=0; i<document.forms.'.$form.'.elements.length; i++) {
 1191:             ele = document.forms.'.$form.'.elements[i];
 1192:             if (ele.name == "'.$type.'") {
 1193:             document.forms.'.$form.'.elements[i].checked=false;
 1194:                                        }
 1195:         }
 1196:     }
 1197: 
 1198: </script>'."\n";
 1199:     return $chkallscript;
 1200: }
 1201: 
 1202: sub check_buttons {
 1203:     my $buttons.='<input type="button" onclick="checkall()" value="'.&mt('Check All').'" />';
 1204:     $buttons.='<input type="button" onclick="uncheckall()" value="'.&mt('Uncheck All').'" />&nbsp;';
 1205:     $buttons.='<input type="button" onclick="checksec()" value="'.&mt('Check Section/Group').'" />';
 1206:     $buttons.='<input type="text" size="5" name="chksec" />&nbsp;';
 1207:     return $buttons;
 1208: }
 1209: 
 1210: #     Displays the submissions for one student or a group of students
 1211: sub processGroup {
 1212:     my ($request)  = shift;
 1213:     my $ctr        = 0;
 1214:     my @stuchecked = &Apache::loncommon::get_env_multiple('form.stuinfo');
 1215:     my $total      = scalar(@stuchecked)-1;
 1216: 
 1217:     foreach my $student (@stuchecked) {
 1218: 	my ($uname,$udom,$fullname) = split(/:/,$student);
 1219: 	$env{'form.student'}        = $uname;
 1220: 	$env{'form.userdom'}        = $udom;
 1221: 	$env{'form.fullname'}       = $fullname;
 1222: 	&submission($request,$ctr,$total);
 1223: 	$ctr++;
 1224:     }
 1225:     return '';
 1226: }
 1227: 
 1228: #------------------------------------------------------------------------------------
 1229: #
 1230: #-------------------------- Next few routines handles grading by student, essentially
 1231: #                           handles essay response type problem/part
 1232: #
 1233: #--- Javascript to handle the submission page functionality ---
 1234: sub sub_page_js {
 1235:     my $request = shift;
 1236: 	    my $alertmsg = &mt('A number equal or greater than 0 is expected. Entered value = ');
 1237:     $request->print(<<SUBJAVASCRIPT);
 1238: <script type="text/javascript" language="javascript">
 1239:     function updateRadio(formname,id,weight) {
 1240: 	var gradeBox = formname["GD_BOX"+id];
 1241: 	var radioButton = formname["RADVAL"+id];
 1242: 	var oldpts = formname["oldpts"+id].value;
 1243: 	var pts = checkSolved(formname,id) == 'update' ? gradeBox.value : oldpts;
 1244: 	gradeBox.value = pts;
 1245: 	var resetbox = false;
 1246: 	if (isNaN(pts) || pts < 0) {
 1247: 	    alert("$alertmsg"+pts);
 1248: 	    for (var i=0; i<radioButton.length; i++) {
 1249: 		if (radioButton[i].checked) {
 1250: 		    gradeBox.value = i;
 1251: 		    resetbox = true;
 1252: 		}
 1253: 	    }
 1254: 	    if (!resetbox) {
 1255: 		formtextbox.value = "";
 1256: 	    }
 1257: 	    return;
 1258: 	}
 1259: 
 1260: 	if (pts > weight) {
 1261: 	    var resp = confirm("You entered a value ("+pts+
 1262: 			       ") greater than the weight for the part. Accept?");
 1263: 	    if (resp == false) {
 1264: 		gradeBox.value = oldpts;
 1265: 		return;
 1266: 	    }
 1267: 	}
 1268: 
 1269: 	for (var i=0; i<radioButton.length; i++) {
 1270: 	    radioButton[i].checked=false;
 1271: 	    if (pts == i && pts != "") {
 1272: 		radioButton[i].checked=true;
 1273: 	    }
 1274: 	}
 1275: 	updateSelect(formname,id);
 1276: 	formname["stores"+id].value = "0";
 1277:     }
 1278: 
 1279:     function writeBox(formname,id,pts) {
 1280: 	var gradeBox = formname["GD_BOX"+id];
 1281: 	if (checkSolved(formname,id) == 'update') {
 1282: 	    gradeBox.value = pts;
 1283: 	} else {
 1284: 	    var oldpts = formname["oldpts"+id].value;
 1285: 	    gradeBox.value = oldpts;
 1286: 	    var radioButton = formname["RADVAL"+id];
 1287: 	    for (var i=0; i<radioButton.length; i++) {
 1288: 		radioButton[i].checked=false;
 1289: 		if (i == oldpts) {
 1290: 		    radioButton[i].checked=true;
 1291: 		}
 1292: 	    }
 1293: 	}
 1294: 	formname["stores"+id].value = "0";
 1295: 	updateSelect(formname,id);
 1296: 	return;
 1297:     }
 1298: 
 1299:     function clearRadBox(formname,id) {
 1300: 	if (checkSolved(formname,id) == 'noupdate') {
 1301: 	    updateSelect(formname,id);
 1302: 	    return;
 1303: 	}
 1304: 	gradeSelect = formname["GD_SEL"+id];
 1305: 	for (var i=0; i<gradeSelect.length; i++) {
 1306: 	    if (gradeSelect[i].selected) {
 1307: 		var selectx=i;
 1308: 	    }
 1309: 	}
 1310: 	var stores = formname["stores"+id];
 1311: 	if (selectx == stores.value) { return };
 1312: 	var gradeBox = formname["GD_BOX"+id];
 1313: 	gradeBox.value = "";
 1314: 	var radioButton = formname["RADVAL"+id];
 1315: 	for (var i=0; i<radioButton.length; i++) {
 1316: 	    radioButton[i].checked=false;
 1317: 	}
 1318: 	stores.value = selectx;
 1319:     }
 1320: 
 1321:     function checkSolved(formname,id) {
 1322: 	if (formname["solved"+id].value == "correct_by_student" && formname.overRideScore.value == 'no') {
 1323: 	    var reply = confirm("This problem has been graded correct by the computer. Do you want to change the score?");
 1324: 	    if (!reply) {return "noupdate";}
 1325: 	    formname.overRideScore.value = 'yes';
 1326: 	}
 1327: 	return "update";
 1328:     }
 1329: 
 1330:     function updateSelect(formname,id) {
 1331: 	formname["GD_SEL"+id][0].selected = true;
 1332: 	return;
 1333:     }
 1334: 
 1335: //=========== Check that a point is assigned for all the parts  ============
 1336:     function checksubmit(formname,val,total,parttot) {
 1337: 	formname.gradeOpt.value = val;
 1338: 	if (val == "Save & Next") {
 1339: 	    for (i=0;i<=total;i++) {
 1340: 		for (j=0;j<parttot;j++) {
 1341: 		    var partid = formname["partid"+i+"_"+j].value;
 1342: 		    if (formname["GD_SEL"+i+"_"+partid][0].selected) {
 1343: 			var points = formname["GD_BOX"+i+"_"+partid].value;
 1344: 			if (points == "") {
 1345: 			    var name = formname["name"+i].value;
 1346: 			    var studentID = (name != '' ? name : formname["unamedom"+i].value);
 1347: 			    var resp = confirm("You did not assign a score for "+studentID+
 1348: 					       ", part "+partid+". Continue?");
 1349: 			    if (resp == false) {
 1350: 				formname["GD_BOX"+i+"_"+partid].focus();
 1351: 				return false;
 1352: 			    }
 1353: 			}
 1354: 		    }
 1355: 		    
 1356: 		}
 1357: 	    }
 1358: 	    
 1359: 	}
 1360: 	if (val == "Grade Student") {
 1361: 	    formname.showgrading.value = "yes";
 1362: 	    if (formname.Status.value == "") {
 1363: 		formname.Status.value = "Active";
 1364: 	    }
 1365: 	    formname.studentNo.value = total;
 1366: 	}
 1367: 	formname.submit();
 1368:     }
 1369: 
 1370: //======= Check that a score is assigned for all the problems (page/sequence grading only) =========
 1371:     function checkSubmitPage(formname,total) {
 1372: 	noscore = new Array(100);
 1373: 	var ptr = 0;
 1374: 	for (i=1;i<total;i++) {
 1375: 	    var partid = formname["q_"+i].value;
 1376: 	    if (formname["GD_SEL"+i+"_"+partid][0].selected) {
 1377: 		var points = formname["GD_BOX"+i+"_"+partid].value;
 1378: 		var status = formname["solved"+i+"_"+partid].value;
 1379: 		if (points == "" && status != "correct_by_student") {
 1380: 		    noscore[ptr] = i;
 1381: 		    ptr++;
 1382: 		}
 1383: 	    }
 1384: 	}
 1385: 	if (ptr != 0) {
 1386: 	    var sense = ptr == 1 ? ": " : "s: ";
 1387: 	    var prolist = "";
 1388: 	    if (ptr == 1) {
 1389: 		prolist = noscore[0];
 1390: 	    } else {
 1391: 		var i = 0;
 1392: 		while (i < ptr-1) {
 1393: 		    prolist += noscore[i]+", ";
 1394: 		    i++;
 1395: 		}
 1396: 		prolist += "and "+noscore[i];
 1397: 	    }
 1398: 	    var resp = confirm("You did not assign any score for the following problem"+sense+prolist+". Continue?");
 1399: 	    if (resp == false) {
 1400: 		return false;
 1401: 	    }
 1402: 	}
 1403: 
 1404: 	formname.submit();
 1405:     }
 1406: </script>
 1407: SUBJAVASCRIPT
 1408: }
 1409: 
 1410: #--- javascript for essay type problem --
 1411: sub sub_page_kw_js {
 1412:     my $request = shift;
 1413:     my $iconpath = $request->dir_config('lonIconsURL');
 1414:     &commonJSfunctions($request);
 1415: 
 1416:     my $inner_js_msg_central=<<INNERJS;
 1417:     <script text="text/javascript">
 1418:     function checkInput() {
 1419:       opener.document.SCORE.msgsub.value = opener.checkEntities(document.msgcenter.msgsub.value);
 1420:       var nmsg   = opener.document.SCORE.savemsgN.value;
 1421:       var usrctr = document.msgcenter.usrctr.value;
 1422:       var newval = opener.document.SCORE["newmsg"+usrctr];
 1423:       newval.value = opener.checkEntities(document.msgcenter.newmsg.value);
 1424: 
 1425:       var msgchk = "";
 1426:       if (document.msgcenter.subchk.checked) {
 1427:          msgchk = "msgsub,";
 1428:       }
 1429:       var includemsg = 0;
 1430:       for (var i=1; i<=nmsg; i++) {
 1431:           var opnmsg = opener.document.SCORE["savemsg"+i];
 1432:           var frmmsg = document.msgcenter["msg"+i];
 1433:           opnmsg.value = opener.checkEntities(frmmsg.value);
 1434:           var showflg = opener.document.SCORE["shownOnce"+i];
 1435:           showflg.value = "1";
 1436:           var chkbox = document.msgcenter["msgn"+i];
 1437:           if (chkbox.checked) {
 1438:              msgchk += "savemsg"+i+",";
 1439:              includemsg = 1;
 1440:           }
 1441:       }
 1442:       if (document.msgcenter.newmsgchk.checked) {
 1443:          msgchk += "newmsg"+usrctr;
 1444:          includemsg = 1;
 1445:       }
 1446:       imgformname = opener.document.SCORE["mailicon"+usrctr];
 1447:       imgformname.src = "$iconpath/"+((includemsg) ? "mailto.gif" : "mailbkgrd.gif");
 1448:       var includemsg = opener.document.SCORE["includemsg"+usrctr];
 1449:       includemsg.value = msgchk;
 1450: 
 1451:       self.close()
 1452: 
 1453:     }
 1454:     </script>
 1455: INNERJS
 1456: 
 1457:     my $inner_js_highlight_central=<<INNERJS;
 1458:  <script type="text/javascript">
 1459:     function updateChoice(flag) {
 1460:       opener.document.SCORE.kwclr.value = opener.radioSelection(document.hlCenter.kwdclr);
 1461:       opener.document.SCORE.kwsize.value = opener.radioSelection(document.hlCenter.kwdsize);
 1462:       opener.document.SCORE.kwstyle.value = opener.radioSelection(document.hlCenter.kwdstyle);
 1463:       opener.document.SCORE.refresh.value = "on";
 1464:       if (opener.document.SCORE.keywords.value!=""){
 1465:          opener.document.SCORE.submit();
 1466:       }
 1467:       self.close()
 1468:     }
 1469: </script>
 1470: INNERJS
 1471: 
 1472:     my $start_page_msg_central = 
 1473:         &Apache::loncommon::start_page('Message Central',$inner_js_msg_central,
 1474: 				       {'js_ready'  => 1,
 1475: 					'only_body' => 1,
 1476: 					'bgcolor'   =>'#FFFFFF',});
 1477:     my $end_page_msg_central = 
 1478: 	&Apache::loncommon::end_page({'js_ready' => 1});
 1479: 
 1480: 
 1481:     my $start_page_highlight_central = 
 1482:         &Apache::loncommon::start_page('Highlight Central',
 1483: 				       $inner_js_highlight_central,
 1484: 				       {'js_ready'  => 1,
 1485: 					'only_body' => 1,
 1486: 					'bgcolor'   =>'#FFFFFF',});
 1487:     my $end_page_highlight_central = 
 1488: 	&Apache::loncommon::end_page({'js_ready' => 1});
 1489: 
 1490:     my $docopen=&Apache::lonhtmlcommon::javascript_docopen();
 1491:     $docopen=~s/^document\.//;
 1492:     my %lt = &Apache::lonlocal::texthash(
 1493:                 keyw => 'Keywords list, separated by a space. Add/delete to list if desired.',
 1494:                 plse => 'Please select a word or group of words from document and then click this link.',
 1495:                 adds => 'Add selection to keyword list? Edit if desired.',
 1496:                 comp => 'Compose Message for: ',
 1497:                 incl => 'Include',
 1498:                 type => 'Type',
 1499:                 subj => 'Subject',
 1500:                 mesa => 'Message',
 1501:                 new  => 'New',
 1502:                 save => 'Save',
 1503:                 canc => 'Cancel',
 1504:                 kehi => 'Keyword Highlight Options',
 1505:                 txtc => 'Text Color',
 1506:                 font => 'Font Size',
 1507:                 fnst => 'Font Style',
 1508:              );
 1509:     $request->print(<<SUBJAVASCRIPT);
 1510: <script type="text/javascript" language="javascript">
 1511: 
 1512: //===================== Show list of keywords ====================
 1513:   function keywords(formname) {
 1514:     var nret = prompt("$lt{'keyw'}",formname.keywords.value);
 1515:     if (nret==null) return;
 1516:     formname.keywords.value = nret;
 1517: 
 1518:     if (formname.keywords.value != "") {
 1519: 	formname.refresh.value = "on";
 1520: 	formname.submit();
 1521:     }
 1522:     return;
 1523:   }
 1524: 
 1525: //===================== Script to view submitted by ==================
 1526:   function viewSubmitter(submitter) {
 1527:     document.SCORE.refresh.value = "on";
 1528:     document.SCORE.NCT.value = "1";
 1529:     document.SCORE.unamedom0.value = submitter;
 1530:     document.SCORE.submit();
 1531:     return;
 1532:   }
 1533: 
 1534: //===================== Script to add keyword(s) ==================
 1535:   function getSel() {
 1536:     if (document.getSelection) txt = document.getSelection();
 1537:     else if (document.selection) txt = document.selection.createRange().text;
 1538:     else return;
 1539:     var cleantxt = txt.replace(new RegExp('([\\f\\n\\r\\t\\v ])+', 'g')," ");
 1540:     if (cleantxt=="") {
 1541: 	alert("$lt{'plse'}");
 1542: 	return;
 1543:     }
 1544:     var nret = prompt("$lt{'adds'}",cleantxt);
 1545:     if (nret==null) return;
 1546:     document.SCORE.keywords.value = document.SCORE.keywords.value+" "+nret;
 1547:     if (document.SCORE.keywords.value != "") {
 1548: 	document.SCORE.refresh.value = "on";
 1549: 	document.SCORE.submit();
 1550:     }
 1551:     return;
 1552:   }
 1553: 
 1554: //====================== Script for composing message ==============
 1555:    // preload images
 1556:    img1 = new Image();
 1557:    img1.src = "$iconpath/mailbkgrd.gif";
 1558:    img2 = new Image();
 1559:    img2.src = "$iconpath/mailto.gif";
 1560: 
 1561:   function msgCenter(msgform,usrctr,fullname) {
 1562:     var Nmsg  = msgform.savemsgN.value;
 1563:     savedMsgHeader(Nmsg,usrctr,fullname);
 1564:     var subject = msgform.msgsub.value;
 1565:     var msgchk = document.SCORE["includemsg"+usrctr].value;
 1566:     re = /msgsub/;
 1567:     var shwsel = "";
 1568:     if (re.test(msgchk)) { shwsel = "checked" }
 1569:     subject = (document.SCORE.shownSub.value == 0 ? checkEntities(subject) : subject);
 1570:     displaySubject(checkEntities(subject),shwsel);
 1571:     for (var i=1; i<=Nmsg; i++) {
 1572: 	var testmsg = "savemsg"+i+",";
 1573: 	re = new RegExp(testmsg,"g");
 1574: 	shwsel = "";
 1575: 	if (re.test(msgchk)) { shwsel = "checked" }
 1576: 	var message = document.SCORE["savemsg"+i].value;
 1577: 	message = (document.SCORE["shownOnce"+i].value == 0 ? checkEntities(message) : message);
 1578: 	displaySavedMsg(i,message,shwsel); //I do not get it. w/o checkEntities on saved messages,
 1579: 	                                   //any &lt; is already converted to <, etc. However, only once!!
 1580:     }
 1581:     newmsg = document.SCORE["newmsg"+usrctr].value;
 1582:     shwsel = "";
 1583:     re = /newmsg/;
 1584:     if (re.test(msgchk)) { shwsel = "checked" }
 1585:     newMsg(newmsg,shwsel);
 1586:     msgTail(); 
 1587:     return;
 1588:   }
 1589: 
 1590:   function checkEntities(strx) {
 1591:     if (strx.length == 0) return strx;
 1592:     var orgStr = ["&", "<", ">", '"']; 
 1593:     var newStr = ["&amp;", "&lt;", "&gt;", "&quot;"];
 1594:     var counter = 0;
 1595:     while (counter < 4) {
 1596: 	strx = strReplace(strx,orgStr[counter],newStr[counter]);
 1597: 	counter++;
 1598:     }
 1599:     return strx;
 1600:   }
 1601: 
 1602:   function strReplace(strx, orgStr, newStr) {
 1603:     return strx.split(orgStr).join(newStr);
 1604:   }
 1605: 
 1606:   function savedMsgHeader(Nmsg,usrctr,fullname) {
 1607:     var height = 70*Nmsg+250;
 1608:     var scrollbar = "no";
 1609:     if (height > 600) {
 1610: 	height = 600;
 1611: 	scrollbar = "yes";
 1612:     }
 1613:     var xpos = (screen.width-600)/2;
 1614:     xpos = (xpos < 0) ? '0' : xpos;
 1615:     var ypos = (screen.height-height)/2-30;
 1616:     ypos = (ypos < 0) ? '0' : ypos;
 1617: 
 1618:     pWin = window.open('', 'MessageCenter', 'resizable=yes,toolbar=no,location=no,scrollbars='+scrollbar+',screenx='+xpos+',screeny='+ypos+',width=700,height='+height);
 1619:     pWin.focus();
 1620:     pDoc = pWin.document;
 1621:     pDoc.$docopen;
 1622:     pDoc.write('$start_page_msg_central');
 1623: 
 1624:     pDoc.write("<form action=\\"inactive\\" name=\\"msgcenter\\">");
 1625:     pDoc.write("<input value=\\""+usrctr+"\\" name=\\"usrctr\\" type=\\"hidden\\">");
 1626:     pDoc.write("<h3><span class=\\"LC_info\\">&nbsp;$lt{'comp'}\"+fullname+\"<\\/span><\\/h3><br /><br />");
 1627: 
 1628:     pDoc.write('<table border="0" width="100%"><tr><td bgcolor="#777777">');
 1629:     pDoc.write('<table border="0" width="100%"><tr bgcolor="#DDFFFF">');
 1630:     pDoc.write("<td><b>$lt{'type'}<\\/b><\\/td><td><b>$lt{'incl'}<\\/b><\\/td><td><b>$lt{'mesa'}<\\/td><\\/tr>");
 1631: }
 1632:     function displaySubject(msg,shwsel) {
 1633:     pDoc = pWin.document;
 1634:     pDoc.write("<tr bgcolor=\\"#ffffdd\\">");
 1635:     pDoc.write("<td>$lt{'subj'}<\\/td>");
 1636:     pDoc.write("<td align=\\"center\\"><input name=\\"subchk\\" type=\\"checkbox\\"" +shwsel+"><\\/td>");
 1637:     pDoc.write("<td><input name=\\"msgsub\\" type=\\"text\\" value=\\""+msg+"\\"size=\\"60\\" maxlength=\\"80\\"><\\/td><\\/tr>");
 1638: }
 1639: 
 1640:   function displaySavedMsg(ctr,msg,shwsel) {
 1641:     pDoc = pWin.document;
 1642:     pDoc.write("<tr bgcolor=\\"#ffffdd\\">");
 1643:     pDoc.write("<td align=\\"center\\">"+ctr+"<\\/td>");
 1644:     pDoc.write("<td align=\\"center\\"><input name=\\"msgn"+ctr+"\\" type=\\"checkbox\\"" +shwsel+"><\\/td>");
 1645:     pDoc.write("<td><textarea name=\\"msg"+ctr+"\\" cols=\\"60\\" rows=\\"3\\">"+msg+"<\\/textarea><\\/td><\\/tr>");
 1646: }
 1647: 
 1648:   function newMsg(newmsg,shwsel) {
 1649:     pDoc = pWin.document;
 1650:     pDoc.write("<tr bgcolor=\\"#ffffdd\\">");
 1651:     pDoc.write("<td align=\\"center\\">$lt{'new'}<\\/td>");
 1652:     pDoc.write("<td align=\\"center\\"><input name=\\"newmsgchk\\" type=\\"checkbox\\"" +shwsel+"><\\/td>");
 1653:     pDoc.write("<td><textarea name=\\"newmsg\\" cols=\\"60\\" rows=\\"3\\" onchange=\\"javascript:this.form.newmsgchk.checked=true\\" >"+newmsg+"<\\/textarea><\\/td><\\/tr>");
 1654: }
 1655: 
 1656:   function msgTail() {
 1657:     pDoc = pWin.document;
 1658:     pDoc.write("<\\/table>");
 1659:     pDoc.write("<\\/td><\\/tr><\\/table>&nbsp;");
 1660:     pDoc.write("<input type=\\"button\\" value=\\"$lt{'save'}\\" onclick=\\"javascript:checkInput()\\">&nbsp;&nbsp;");
 1661:     pDoc.write("<input type=\\"button\\" value=\\"$lt{'canc'}\\" onclick=\\"self.close()\\"><br /><br />");
 1662:     pDoc.write("<\\/form>");
 1663:     pDoc.write('$end_page_msg_central');
 1664:     pDoc.close();
 1665: }
 1666: 
 1667: //====================== Script for keyword highlight options ==============
 1668:   function kwhighlight() {
 1669:     var kwclr    = document.SCORE.kwclr.value;
 1670:     var kwsize   = document.SCORE.kwsize.value;
 1671:     var kwstyle  = document.SCORE.kwstyle.value;
 1672:     var redsel = "";
 1673:     var grnsel = "";
 1674:     var blusel = "";
 1675:     if (kwclr=="red")   {var redsel="checked"};
 1676:     if (kwclr=="green") {var grnsel="checked"};
 1677:     if (kwclr=="blue")  {var blusel="checked"};
 1678:     var sznsel = "";
 1679:     var sz1sel = "";
 1680:     var sz2sel = "";
 1681:     if (kwsize=="0")  {var sznsel="checked"};
 1682:     if (kwsize=="+1") {var sz1sel="checked"};
 1683:     if (kwsize=="+2") {var sz2sel="checked"};
 1684:     var synsel = "";
 1685:     var syisel = "";
 1686:     var sybsel = "";
 1687:     if (kwstyle=="")    {var synsel="checked"};
 1688:     if (kwstyle=="<i>") {var syisel="checked"};
 1689:     if (kwstyle=="<b>") {var sybsel="checked"};
 1690:     highlightCentral();
 1691:     highlightbody('red','red',redsel,'0','normal',sznsel,'','normal',synsel);
 1692:     highlightbody('green','green',grnsel,'+1','+1',sz1sel,'<i>','italic',syisel);
 1693:     highlightbody('blue','blue',blusel,'+2','+2',sz2sel,'<b>','bold',sybsel);
 1694:     highlightend();
 1695:     return;
 1696:   }
 1697: 
 1698:   function highlightCentral() {
 1699: //    if (window.hwdWin) window.hwdWin.close();
 1700:     var xpos = (screen.width-400)/2;
 1701:     xpos = (xpos < 0) ? '0' : xpos;
 1702:     var ypos = (screen.height-330)/2-30;
 1703:     ypos = (ypos < 0) ? '0' : ypos;
 1704: 
 1705:     hwdWin = window.open('', 'KeywordHighlightCentral', 'resizeable=yes,toolbar=no,location=no,scrollbars=no,width=400,height=300,screenx='+xpos+',screeny='+ypos);
 1706:     hwdWin.focus();
 1707:     var hDoc = hwdWin.document;
 1708:     hDoc.$docopen;
 1709:     hDoc.write('$start_page_highlight_central');
 1710:     hDoc.write("<form action=\\"inactive\\" name=\\"hlCenter\\">");
 1711:     hDoc.write("<h3><span class=\\"LC_info\\">&nbsp;$lt{'kehi'}<\\/span><\\/h3><br /><br />");
 1712: 
 1713:     hDoc.write('<table border="0" width="100%"><tr><td bgcolor="#777777">');
 1714:     hDoc.write('<table border="0" width="100%"><tr bgcolor="#DDFFFF">');
 1715:     hDoc.write("<td><b>$lt{'txtc'}<\\/b><\\/td><td><b>$lt{'font'}<\\/b><\\/td><td><b>$lt{'fnst'}<\\/td><\\/tr>");
 1716:   }
 1717: 
 1718:   function highlightbody(clrval,clrtxt,clrsel,szval,sztxt,szsel,syval,sytxt,sysel) { 
 1719:     var hDoc = hwdWin.document;
 1720:     hDoc.write("<tr bgcolor=\\"#ffffdd\\">");
 1721:     hDoc.write("<td align=\\"left\\">");
 1722:     hDoc.write("<input name=\\"kwdclr\\" type=\\"radio\\" value=\\""+clrval+"\\" "+clrsel+">&nbsp;"+clrtxt+"<\\/td>");
 1723:     hDoc.write("<td align=\\"left\\">");
 1724:     hDoc.write("<input name=\\"kwdsize\\" type=\\"radio\\" value=\\""+szval+"\\" "+szsel+">&nbsp;"+sztxt+"<\\/td>");
 1725:     hDoc.write("<td align=\\"left\\">");
 1726:     hDoc.write("<input name=\\"kwdstyle\\" type=\\"radio\\" value=\\""+syval+"\\" "+sysel+">&nbsp;"+sytxt+"<\\/td>");
 1727:     hDoc.write("<\\/tr>");
 1728:   }
 1729: 
 1730:   function highlightend() { 
 1731:     var hDoc = hwdWin.document;
 1732:     hDoc.write("<\\/table>");
 1733:     hDoc.write("<\\/td><\\/tr><\\/table>&nbsp;");
 1734:     hDoc.write("<input type=\\"button\\" value=\\"$lt{'save'}\\" onclick=\\"javascript:updateChoice(1)\\">&nbsp;&nbsp;");
 1735:     hDoc.write("<input type=\\"button\\" value=\\"$lt{'canc'}\\" onclick=\\"self.close()\\"><br /><br />");
 1736:     hDoc.write("<\\/form>");
 1737:     hDoc.write('$end_page_highlight_central');
 1738:     hDoc.close();
 1739:   }
 1740: 
 1741: </script>
 1742: SUBJAVASCRIPT
 1743: }
 1744: 
 1745: sub get_increment {
 1746:     my $increment = $env{'form.increment'};
 1747:     if ($increment != 1 && $increment != .5 && $increment != .25 &&
 1748:         $increment != .1) {
 1749:         $increment = 1;
 1750:     }
 1751:     return $increment;
 1752: }
 1753: 
 1754: sub gradeBox_start {
 1755:     return (
 1756:         &Apache::loncommon::start_data_table()
 1757:        .&Apache::loncommon::start_data_table_header_row()
 1758:        .'<th>'.&mt('Part').'</th>'
 1759:        .'<th>'.&mt('Points').'</th>'
 1760:        .'<th>&nbsp;</th>'
 1761:        .'<th>'.&mt('Assign Grade').'</th>'
 1762:        .'<th>'.&mt('Weight').'</th>'
 1763:        .'<th>'.&mt('Grade Status').'</th>'
 1764:        .&Apache::loncommon::end_data_table_header_row()
 1765:     );
 1766: }
 1767: 
 1768: sub gradeBox_end {
 1769:     return (
 1770:         &Apache::loncommon::end_data_table()
 1771:     );
 1772: }
 1773: #--- displays the grading box, used in essay type problem and grading by page/sequence
 1774: sub gradeBox {
 1775:     my ($request,$symb,$uname,$udom,$counter,$partid,$record) = @_;
 1776:     my $checkIcon = '<img alt="'.&mt('Check Mark').
 1777: 	'" src="'.&Apache::loncommon::lonhttpdurl($request->dir_config('lonIconsURL').'/check.gif').'" height="16" border="0" />';
 1778:     my $wgt    = &Apache::lonnet::EXT('resource.'.$partid.'.weight',$symb,$udom,$uname);
 1779:     my $wgtmsg = ($wgt > 0) ? &mt('(problem weight)') 
 1780:                            : '<span class="LC_info">'.&mt('problem weight assigned by computer').'</span>';
 1781:     $wgt       = ($wgt > 0 ? $wgt : '1');
 1782:     my $score  = ($$record{'resource.'.$partid.'.awarded'} eq '' ?
 1783: 		  '' : &compute_points($$record{'resource.'.$partid.'.awarded'},$wgt));
 1784:     my $result='<input type="hidden" name="WGT'.$counter.'_'.$partid.'" value="'.$wgt.'" />'."\n";
 1785:     my $display_part= &get_display_part($partid,$symb);
 1786:     my %last_resets = &get_last_resets($symb,$env{'request.course.id'},
 1787: 				       [$partid]);
 1788:     my $aggtries = $$record{'resource.'.$partid.'.tries'};
 1789:     if ($last_resets{$partid}) {
 1790:         $aggtries = &get_num_tries($record,$last_resets{$partid},$partid);
 1791:     }
 1792:     $result.=&Apache::loncommon::start_data_table_row();
 1793:     my $ctr = 0;
 1794:     my $thisweight = 0;
 1795:     my $increment = &get_increment();
 1796: 
 1797:     my $radio.='<table border="0"><tr>'."\n";  # display radio buttons in a nice table 10 across
 1798:     while ($thisweight<=$wgt) {
 1799: 	$radio.= '<td><span class="LC_nobreak"><label><input type="radio" name="RADVAL'.$counter.'_'.$partid.'" '.
 1800:         'onclick="javascript:writeBox(this.form,\''.$counter.'_'.$partid.'\','.
 1801: 	    $thisweight.')" value="'.$thisweight.'" '.
 1802: 	    ($score eq $thisweight ? 'checked="checked"':'').' /> '.$thisweight."</label></span></td>\n";
 1803: 	$radio.=(($ctr+1)%10 == 0 ? '</tr><tr>' : '');
 1804:         $thisweight += $increment;
 1805: 	$ctr++;
 1806:     }
 1807:     $radio.='</tr></table>';
 1808: 
 1809:     my $line.='<input type="text" name="GD_BOX'.$counter.'_'.$partid.'"'.
 1810: 	($score ne ''? ' value = "'.$score.'"':'').' size="4" '.
 1811: 	'onchange="javascript:updateRadio(this.form,\''.$counter.'_'.$partid.'\','.
 1812: 	$wgt.')" /></td>'."\n";
 1813:     $line.='<td>/'.$wgt.' '.$wgtmsg.
 1814: 	($$record{'resource.'.$partid.'.solved'} eq 'correct_by_student' ? '&nbsp;'.$checkIcon : '').
 1815: 	' </td>'."\n";
 1816:     $line.='<td><select name="GD_SEL'.$counter.'_'.$partid.'" '.
 1817: 	'onchange="javascript:clearRadBox(this.form,\''.$counter.'_'.$partid.'\')" >'."\n";
 1818:     if ($$record{'resource.'.$partid.'.solved'} eq 'excused') {
 1819: 	$line.='<option></option>'.
 1820: 	    '<option value="excused" selected="selected">'.&mt('excused').'</option>';
 1821:     } else {
 1822: 	$line.='<option selected="selected"></option>'.
 1823: 	    '<option value="excused" >'.&mt('excused').'</option>';
 1824:     }
 1825:     $line.='<option value="reset status">'.&mt('reset status').'</option></select>'."\n";
 1826: 
 1827: 
 1828: 	#&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);
 1829:     $result .= 
 1830: 	    '<td>'.$display_part.'</td><td>'.$radio.'</td><td>'.&mt('or').'</td><td>'.$line.'</td>';
 1831:     $result.=&Apache::loncommon::end_data_table_row();
 1832:     $result.='<input type="hidden" name="stores'.$counter.'_'.$partid.'" value="" />'."\n".
 1833: 	'<input type="hidden" name="oldpts'.$counter.'_'.$partid.'" value="'.$score.'" />'."\n".
 1834: 	'<input type="hidden" name="solved'.$counter.'_'.$partid.'" value="'.
 1835: 	$$record{'resource.'.$partid.'.solved'}.'" />'."\n".
 1836:         '<input type="hidden" name="totaltries'.$counter.'_'.$partid.'" value="'.
 1837:         $$record{'resource.'.$partid.'.tries'}.'" />'."\n".
 1838:         '<input type="hidden" name="aggtries'.$counter.'_'.$partid.'" value="'.
 1839:         $aggtries.'" />'."\n";
 1840:     my $res_error;
 1841:     $result.=&handback_box($symb,$uname,$udom,$counter,$partid,$record,\$res_error);
 1842:     if ($res_error) {
 1843:         return &navmap_errormsg();
 1844:     }
 1845:     return $result;
 1846: }
 1847: 
 1848: sub handback_box {
 1849:     my ($symb,$uname,$udom,$counter,$partid,$record,$res_error) = @_;
 1850:     my ($partlist,$handgrade,$responseType) = &response_type($symb,$res_error);
 1851:     my (@respids);
 1852:     my @part_response_id = &flatten_responseType($responseType);
 1853:     foreach my $part_response_id (@part_response_id) {
 1854:     	my ($part,$resp) = @{ $part_response_id };
 1855:         if ($part eq $partid) {
 1856:             push(@respids,$resp);
 1857:         }
 1858:     }
 1859:     my $result;
 1860:     foreach my $respid (@respids) {
 1861: 	my $prefix = $counter.'_'.$partid.'_'.$respid.'_';
 1862: 	my $files=&get_submitted_files($udom,$uname,$partid,$respid,$record);
 1863: 	next if (!@$files);
 1864: 	my $file_counter = 0;
 1865: 	foreach my $file (@$files) {
 1866: 	    if ($file =~ /\/portfolio\//) {
 1867:                 $file_counter++;
 1868:     	        my ($file_path, $file_disp) = ($file =~ m|(.+/)(.+)$|);
 1869:     	        my ($name,$version,$ext) = &file_name_version_ext($file_disp);
 1870:     	        $file_disp = "$name.$ext";
 1871:     	        $file = $file_path.$file_disp;
 1872:     	        $result.=&mt('Return commented version of [_1] to student.',
 1873:     			 '<span class="LC_filename">'.$file_disp.'</span>');
 1874:     	        $result.='<input type="file"   name="'.$prefix.'returndoc'.$file_counter.'" />'."\n";
 1875:     	        $result.='<input type="hidden" name="'.$prefix.'origdoc'.$file_counter.'" value="'.$file.'" /><br />'."\n";
 1876: 	    }
 1877: 	}
 1878:         if ($file_counter) {
 1879:             $result .= '<input type="hidden" name="'.$prefix.'countreturndoc" value="'.$file_counter.'" />'."\n".
 1880:                        '<span class="LC_info">'.
 1881:                        '('.&mt('File(s) will be uploaded when you click on Save &amp; Next below.',$file_counter).')</span><br /><br />';
 1882:         }
 1883:     }
 1884:     return $result;    
 1885: }
 1886: 
 1887: sub show_problem {
 1888:     my ($request,$symb,$uname,$udom,$removeform,$viewon,$mode,$form) = @_;
 1889:     my $rendered;
 1890:     my %form = ((ref($form) eq 'HASH')? %{$form} : ());
 1891:     &Apache::lonxml::remember_problem_counter();
 1892:     if ($mode eq 'both' or $mode eq 'text') {
 1893: 	$rendered=&Apache::loncommon::get_student_view($symb,$uname,$udom,
 1894: 						       $env{'request.course.id'},
 1895: 						       undef,\%form);
 1896:     }
 1897:     if ($removeform) {
 1898: 	$rendered=~s|<form(.*?)>||g;
 1899: 	$rendered=~s|</form>||g;
 1900: 	$rendered=~s|(<input[^>]*name\s*=\s*"?)(\w+)("?)|$1would_have_been_$2$3|g;
 1901:     }
 1902:     my $companswer;
 1903:     if ($mode eq 'both' or $mode eq 'answer') {
 1904: 	&Apache::lonxml::restore_problem_counter();
 1905: 	$companswer=
 1906: 	    &Apache::loncommon::get_student_answers($symb,$uname,$udom,
 1907: 						    $env{'request.course.id'},
 1908: 						    %form);
 1909:     }
 1910:     if ($removeform) {
 1911: 	$companswer=~s|<form(.*?)>||g;
 1912: 	$companswer=~s|</form>||g;
 1913: 	$companswer=~s|name="submit"|name="would_have_been_submit"|g;
 1914:     }
 1915:     $rendered=
 1916:         '<div class="LC_Box">'
 1917:        .'<h3 class="LC_hcell">'.&mt('View of the problem').'</h3>'
 1918:        .$rendered
 1919:        .'</div>';
 1920:     $companswer=
 1921:         '<div class="LC_Box">'
 1922:        .'<h3 class="LC_hcell">'.&mt('Correct answer').'</h3>'
 1923:        .$companswer
 1924:        .'</div>';
 1925:     my $result;
 1926:     if ($mode eq 'both') {
 1927:         $result=$rendered.$companswer;
 1928:     } elsif ($mode eq 'text') {
 1929:         $result=$rendered;
 1930:     } elsif ($mode eq 'answer') {
 1931:         $result=$companswer;
 1932:     }
 1933:     return $result;
 1934: }
 1935: 
 1936: sub files_exist {
 1937:     my ($r, $symb) = @_;
 1938:     my @students = &Apache::loncommon::get_env_multiple('form.stuinfo');
 1939: 
 1940:     foreach my $student (@students) {
 1941:         my ($uname,$udom,$fullname) = split(/:/,$student);
 1942:         my %record = &Apache::lonnet::restore($symb,$env{'request.course.id'},
 1943: 					      $udom,$uname);
 1944:         my ($string,$timestamp)= &get_last_submission(\%record);
 1945:         foreach my $submission (@$string) {
 1946:             my ($partid,$respid) =
 1947: 		($submission =~ /^resource\.([^\.]*)\.([^\.]*)\.submission/);
 1948:             my $files=&get_submitted_files($udom,$uname,$partid,$respid,
 1949: 					   \%record);
 1950:             return 1 if (@$files);
 1951:         }
 1952:     }
 1953:     return 0;
 1954: }
 1955: 
 1956: sub download_all_link {
 1957:     my ($r,$symb) = @_;
 1958:     my $all_students = 
 1959: 	join("\n", &Apache::loncommon::get_env_multiple('form.stuinfo'));
 1960: 
 1961:     my $parts =
 1962: 	join("\n",&Apache::loncommon::get_env_multiple('form.vPart'));
 1963: 
 1964:     my $identifier = &Apache::loncommon::get_cgi_id();
 1965:     &Apache::lonnet::appenv({'cgi.'.$identifier.'.students' => $all_students,
 1966:                              'cgi.'.$identifier.'.symb' => $symb,
 1967:                              'cgi.'.$identifier.'.parts' => $parts,});
 1968:     $r->print('<a href="/cgi-bin/multidownload.pl?'.$identifier.'">'.
 1969: 	      &mt('Download All Submitted Documents').'</a>');
 1970:     return
 1971: }
 1972: 
 1973: sub build_section_inputs {
 1974:     my $section_inputs;
 1975:     if ($env{'form.section'} eq '') {
 1976:         $section_inputs .= '<input type="hidden" name="section" value="all" />'."\n";
 1977:     } else {
 1978:         my @sections = &Apache::loncommon::get_env_multiple('form.section');
 1979:         foreach my $section (@sections) {
 1980:             $section_inputs .= '<input type="hidden" name="section" value="'.$section.'" />'."\n";
 1981:         }
 1982:     }
 1983:     return $section_inputs;
 1984: }
 1985: 
 1986: # --------------------------- show submissions of a student, option to grade 
 1987: sub submission {
 1988:     my ($request,$counter,$total) = @_;
 1989:     my ($uname,$udom)     = ($env{'form.student'},$env{'form.userdom'});
 1990:     $udom = ($udom eq '' ? $env{'user.domain'} : $udom); #has form.userdom changed for a student?
 1991:     my $usec = &Apache::lonnet::getsection($udom,$uname,$env{'request.course.id'});
 1992:     $env{'form.fullname'} = &Apache::loncommon::plainname($uname,$udom,'lastname') if $env{'form.fullname'} eq '';
 1993:     my $symb = &get_symb($request); 
 1994:     if ($symb eq '') { $request->print("Unable to handle ambiguous references:."); return ''; }
 1995: 
 1996:     if (!&canview($usec)) {
 1997: 	$request->print('<span class="LC_warning">Unable to view requested student.('.
 1998: 			$uname.':'.$udom.' in section '.$usec.' in course id '.
 1999: 			$env{'request.course.id'}.')</span>');
 2000: 	$request->print(&show_grading_menu_form($symb));
 2001: 	return;
 2002:     }
 2003: 
 2004:     if (!$env{'form.lastSub'}) { $env{'form.lastSub'} = 'datesub'; }
 2005:     if (!$env{'form.vProb'}) { $env{'form.vProb'} = 'yes'; }
 2006:     if (!$env{'form.vAns'}) { $env{'form.vAns'} = 'yes'; }
 2007:     my $last = ($env{'form.lastSub'} eq 'last' ? 'last' : '');
 2008:     my $checkIcon = '<img alt="'.&mt('Check Mark').
 2009: 	'" src="'.$request->dir_config('lonIconsURL').
 2010: 	'/check.gif" height="16" border="0" />';
 2011: 
 2012:     my %old_essays;
 2013:     # header info
 2014:     if ($counter == 0) {
 2015: 	&sub_page_js($request);
 2016: 	&sub_page_kw_js($request) if ($env{'form.handgrade'} eq 'yes');
 2017: 	$env{'form.probTitle'} = $env{'form.probTitle'} eq '' ? 
 2018: 	    &Apache::lonnet::gettitle($symb) : $env{'form.probTitle'};
 2019: 	if ($env{'form.handgrade'} eq 'yes' && &files_exist($request, $symb)) {
 2020: 	    &download_all_link($request, $symb);
 2021: 	}
 2022: 	$request->print('<h3>&nbsp;<span class="LC_info">'.&mt('Submission Record').'</span></h3>'."\n".
 2023: 			'<h4>&nbsp;'.&mt('<b>Resource: </b> [_1]',$env{'form.probTitle'}).'</h4>'."\n");
 2024: 
 2025: 	# option to display problem, only once else it cause problems 
 2026:         # with the form later since the problem has a form.
 2027: 	if ($env{'form.vProb'} eq 'yes' or $env{'form.vAns'} eq 'yes') {
 2028: 	    my $mode;
 2029: 	    if ($env{'form.vProb'} eq 'yes' && $env{'form.vAns'} eq 'yes') {
 2030: 		$mode='both';
 2031: 	    } elsif ($env{'form.vProb'} eq 'yes') {
 2032: 		$mode='text';
 2033: 	    } elsif ($env{'form.vAns'} eq 'yes') {
 2034: 		$mode='answer';
 2035: 	    }
 2036: 	    &Apache::lonxml::clear_problem_counter();
 2037: 	    $request->print(&show_problem($request,$symb,$uname,$udom,0,1,$mode));
 2038: 	}
 2039: 
 2040: 	# kwclr is the only variable that is guaranteed to be non blank 
 2041:         # if this subroutine has been called once.
 2042: 	my %keyhash = ();
 2043: 	if ($env{'form.kwclr'} eq '' && $env{'form.handgrade'} eq 'yes') {
 2044: 	    %keyhash = &Apache::lonnet::dump('nohist_handgrade',
 2045: 					     $env{'course.'.$env{'request.course.id'}.'.domain'},
 2046: 					     $env{'course.'.$env{'request.course.id'}.'.num'});
 2047: 
 2048: 	    my $loginuser = $env{'user.name'}.':'.$env{'user.domain'};
 2049: 	    $env{'form.keywords'} = $keyhash{$symb.'_keywords'} ne '' ? $keyhash{$symb.'_keywords'} : '';
 2050: 	    $env{'form.kwclr'}    = $keyhash{$loginuser.'_kwclr'} ne '' ? $keyhash{$loginuser.'_kwclr'} : 'red';
 2051: 	    $env{'form.kwsize'}   = $keyhash{$loginuser.'_kwsize'} ne '' ? $keyhash{$loginuser.'_kwsize'} : '0';
 2052: 	    $env{'form.kwstyle'}  = $keyhash{$loginuser.'_kwstyle'} ne '' ? $keyhash{$loginuser.'_kwstyle'} : '';
 2053: 	    $env{'form.msgsub'}   = $keyhash{$symb.'_subject'} ne '' ? 
 2054: 		$keyhash{$symb.'_subject'} : $env{'form.probTitle'};
 2055: 	    $env{'form.savemsgN'} = $keyhash{$symb.'_savemsgN'} ne '' ? $keyhash{$symb.'_savemsgN'} : '0';
 2056: 	}
 2057: 	my $overRideScore = $env{'form.overRideScore'} eq '' ? 'no' : $env{'form.overRideScore'};
 2058: 	my $stu_status = join(':',&Apache::loncommon::get_env_multiple('form.Status'));
 2059: 	$request->print('<form action="/adm/grades" method="post" name="SCORE" enctype="multipart/form-data">'."\n".
 2060: 			'<input type="hidden" name="command"    value="handgrade" />'."\n".
 2061: 			'<input type="hidden" name="saveState"  value="'.$env{'form.saveState'}.'" />'."\n".
 2062: 			'<input type="hidden" name="Status"     value="'.$stu_status.'" />'."\n".
 2063: 			'<input type="hidden" name="overRideScore" value="'.$overRideScore.'" />'."\n".
 2064: 			'<input type="hidden" name="probTitle"  value="'.$env{'form.probTitle'}.'" />'."\n".
 2065: 			'<input type="hidden" name="refresh"    value="off" />'."\n".
 2066: 			'<input type="hidden" name="studentNo"  value="" />'."\n".
 2067: 			'<input type="hidden" name="gradeOpt"   value="" />'."\n".
 2068: 			'<input type="hidden" name="symb"       value="'.&Apache::lonenc::check_encrypt($symb).'" />'."\n".
 2069: 			'<input type="hidden" name="showgrading" value="'.$env{'form.showgrading'}.'" />'."\n".
 2070: 			'<input type="hidden" name="vProb"      value="'.$env{'form.vProb'}.'" />'."\n".
 2071: 			'<input type="hidden" name="vAns"       value="'.$env{'form.vAns'}.'" />'."\n".
 2072: 			'<input type="hidden" name="lastSub"    value="'.$env{'form.lastSub'}.'" />'."\n".
 2073: 			&build_section_inputs().
 2074: 			'<input type="hidden" name="submitonly" value="'.$env{'form.submitonly'}.'" />'."\n".
 2075: 			'<input type="hidden" name="handgrade"  value="'.$env{'form.handgrade'}.'" />'."\n".
 2076: 			'<input type="hidden" name="NCT"'.
 2077: 			' value="'.($env{'form.NTSTU'} ne '' ? $env{'form.NTSTU'} : $total+1).'" />'."\n");
 2078: 	if ($env{'form.handgrade'} eq 'yes') {
 2079: 	    $request->print('<input type="hidden" name="keywords" value="'.$env{'form.keywords'}.'" />'."\n".
 2080: 			    '<input type="hidden" name="kwclr"    value="'.$env{'form.kwclr'}.'" />'."\n".
 2081: 			    '<input type="hidden" name="kwsize"   value="'.$env{'form.kwsize'}.'" />'."\n".
 2082: 			    '<input type="hidden" name="kwstyle"  value="'.$env{'form.kwstyle'}.'" />'."\n".
 2083: 			    '<input type="hidden" name="msgsub"   value="'.$env{'form.msgsub'}.'" />'."\n".
 2084: 			    '<input type="hidden" name="shownSub" value="0" />'."\n".
 2085: 			    '<input type="hidden" name="savemsgN" value="'.$env{'form.savemsgN'}.'" />'."\n");
 2086: 	    foreach my $partid (&Apache::loncommon::get_env_multiple('form.vPart')) {
 2087: 		$request->print('<input type="hidden" name="vPart" value="'.$partid.'" />'."\n");
 2088: 	    }
 2089: 	}
 2090: 	
 2091: 	my ($cts,$prnmsg) = (1,'');
 2092: 	while ($cts <= $env{'form.savemsgN'}) {
 2093: 	    $prnmsg.='<input type="hidden" name="savemsg'.$cts.'" value="'.
 2094: 		(!exists($keyhash{$symb.'_savemsg'.$cts}) ? 
 2095: 		 &Apache::lonfeedback::clear_out_html($env{'form.savemsg'.$cts}) :
 2096: 		 &Apache::lonfeedback::clear_out_html($keyhash{$symb.'_savemsg'.$cts})).
 2097: 		'" />'."\n".
 2098: 		'<input type="hidden" name="shownOnce'.$cts.'" value="0" />'."\n";
 2099: 	    $cts++;
 2100: 	}
 2101: 	$request->print($prnmsg);
 2102: 
 2103: 	if ($env{'form.handgrade'} eq 'yes' && $env{'form.showgrading'} eq 'yes') {
 2104: 
 2105:             my %lt = &Apache::lonlocal::texthash(
 2106:                           keyw => 'Keyword Options',
 2107:                           list => 'List',
 2108:                           past => 'Paste Selection to List',
 2109:                           high => 'Hightlight Attribute',
 2110:                      );
 2111: #
 2112: # Print out the keyword options line
 2113: #
 2114: 	    $request->print(<<KEYWORDS);
 2115: &nbsp;<b>$lt{'keyw'}:</b>&nbsp;
 2116: <a href="javascript:keywords(document.SCORE);" target="_self">$lt{'list'}</a>&nbsp; &nbsp;
 2117: <a href="#" onmousedown="javascript:getSel(); return false"
 2118:  CLASS="page">$lt{'past'}</a>&nbsp; &nbsp;
 2119: <a href="javascript:kwhighlight();" target="_self">$lt{'high'}</a><br /><br />
 2120: KEYWORDS
 2121: #
 2122: # Load the other essays for similarity check
 2123: #
 2124:             my (undef,undef,$essayurl) = &Apache::lonnet::decode_symb($symb);
 2125: 	    my ($adom,$aname,$apath)=($essayurl=~/^($LONCAPA::domain_re)\/($LONCAPA::username_re)\/(.*)$/);
 2126: 	    $apath=&escape($apath);
 2127: 	    $apath=~s/\W/\_/gs;
 2128: 	    %old_essays=&Apache::lonnet::dump('nohist_essay_'.$apath,$adom,$aname);
 2129:         }
 2130:     }
 2131: 
 2132: # This is where output for one specific student would start
 2133:     my $add_class = ($counter%2) ? ' LC_grade_show_user_odd_row' : '';
 2134:     $request->print(
 2135:         "\n\n"
 2136:        .'<div class="LC_grade_show_user'.$add_class.'">'
 2137:        .'<h2>'.&nameUserString(undef,$env{'form.fullname'},$uname,$udom).'</h2>'
 2138:        ."\n"
 2139:     );
 2140: 
 2141:     # Show additional functions if allowed
 2142:     if ($perm{'vgr'}) {
 2143:         $request->print(
 2144:             &Apache::loncommon::track_student_link(
 2145:                 &mt('View recent activity'),
 2146:                 $uname,$udom,'check')
 2147:            .' '
 2148:         );
 2149:     }
 2150:     if ($perm{'opa'}) {
 2151:         $request->print(
 2152:             &Apache::loncommon::pprmlink(
 2153:                 &mt('Set/Change parameters'),
 2154:                 $uname,$udom,$symb,'check'));
 2155:     }
 2156: 
 2157:     # Show Problem
 2158:     if ($env{'form.vProb'} eq 'all' or $env{'form.vAns'} eq 'all') {
 2159: 	my $mode;
 2160: 	if ($env{'form.vProb'} eq 'all' && $env{'form.vAns'} eq 'all') {
 2161: 	    $mode='both';
 2162: 	} elsif ($env{'form.vProb'} eq 'all' ) {
 2163: 	    $mode='text';
 2164: 	} elsif ($env{'form.vAns'} eq 'all') {
 2165: 	    $mode='answer';
 2166: 	}
 2167: 	&Apache::lonxml::clear_problem_counter();
 2168: 	$request->print(&show_problem($request,$symb,$uname,$udom,1,1,$mode,{'request.prefix' => 'ctr'.$counter}));
 2169:     }
 2170: 
 2171:     my %record = &Apache::lonnet::restore($symb,$env{'request.course.id'},$udom,$uname);
 2172:     my $res_error;
 2173:     my ($partlist,$handgrade,$responseType) = &response_type($symb,\$res_error);
 2174:     if ($res_error) {
 2175:         $request->print(&navmap_errormsg());
 2176:         return;
 2177:     }
 2178: 
 2179:     # Display student info
 2180:     $request->print(($counter == 0 ? '' : '<br />'));
 2181: 
 2182:     my $result='<div class="LC_Box">'
 2183:               .'<h3 class="LC_hcell">'.&mt('Submissions').'</h3>';
 2184:     $result.='<input type="hidden" name="name'.$counter.
 2185:              '" value="'.$env{'form.fullname'}.'" />'."\n";
 2186:     if ($env{'form.handgrade'} eq 'no') {
 2187:         $result.='<p class="LC_info">'
 2188:                 .&mt('Part(s) graded correct by the computer is marked with a [_1] symbol.',$checkIcon)
 2189:                 ."</p>\n";
 2190:     }
 2191: 
 2192:     # If any part of the problem is an essay-response (handgraded), then check for collaborators
 2193:     my $fullname;
 2194:     my $col_fullnames = [];
 2195:     if ($env{'form.handgrade'} eq 'yes') {
 2196: 	(my $sub_result,$fullname,$col_fullnames)=
 2197: 	    &check_collaborators($symb,$uname,$udom,\%record,$handgrade,
 2198: 				 $counter);
 2199: 	$result.=$sub_result;
 2200:     }
 2201:     $request->print($result."\n");
 2202: 
 2203:     # print student answer/submission
 2204:     # Options are (1) Handgraded submission only
 2205:     #             (2) Last submission, includes submission that is not handgraded 
 2206:     #                  (for multi-response type part)
 2207:     #             (3) Last submission plus the parts info
 2208:     #             (4) The whole record for this student
 2209:     if ($env{'form.lastSub'} =~ /^(lastonly|hdgrade)$/) {
 2210: 	my ($string,$timestamp)= &get_last_submission(\%record);
 2211: 	
 2212: 	my $lastsubonly;
 2213: 
 2214:         if ($$timestamp eq '') {
 2215:             $lastsubonly.='<div class="LC_grade_submissions_body">'.$$string[0].'</div>'; 
 2216:         } else {
 2217:             $lastsubonly =
 2218:                 '<div class="LC_grade_submissions_body">'
 2219:                .'<b>'.&mt('Date Submitted:').'</b> '.$$timestamp."\n";
 2220: 
 2221: 	    my %seenparts;
 2222: 	    my @part_response_id = &flatten_responseType($responseType);
 2223: 	    foreach my $part (@part_response_id) {
 2224: 		next if ($env{'form.lastSub'} eq 'hdgrade' 
 2225: 			 && $$handgrade{$$part[0].'_'.$$part[1]} ne 'yes');
 2226: 
 2227: 		my ($partid,$respid) = @{ $part };
 2228: 		my $display_part=&get_display_part($partid,$symb);
 2229: 		if ($env{"form.$uname:$udom:$partid:submitted_by"}) {
 2230: 		    if (exists($seenparts{$partid})) { next; }
 2231: 		    $seenparts{$partid}=1;
 2232: 		    my $submitby='<b>Part:</b> '.$display_part.
 2233: 			' <b>Collaborative submission by:</b> '.
 2234: 			'<a href="javascript:viewSubmitter(\''.
 2235: 			$env{"form.$uname:$udom:$partid:submitted_by"}.
 2236: 			'\');" target="_self">'.
 2237: 			$$fullname{$env{"form.$uname:$udom:$partid:submitted_by"}}.'</a><br />';
 2238: 		    $request->print($submitby);
 2239: 		    next;
 2240: 		}
 2241: 		my $responsetype = $responseType->{$partid}->{$respid};
 2242: 		if (!exists($record{"resource.$partid.$respid.submission"})) {
 2243:                     $lastsubonly.="\n".'<div class="LC_grade_submission_part">'.
 2244:                         '<b>'.&mt('Part: [_1]',$display_part).'</b>'.
 2245:                         ' <span class="LC_internal_info">'.
 2246:                         '('.&mt('Response ID: [_1]',$respid).')'.
 2247:                         '</span>&nbsp; &nbsp;'.
 2248: 			'<span class="LC_warning">'.&mt('Nothing submitted - no attempts.').'</span><br /><br /></div>';
 2249: 		    next;
 2250: 		}
 2251: 		foreach my $submission (@$string) {
 2252: 		    my ($partid,$respid) = ($submission =~ /^resource\.([^\.]*)\.([^\.]*)\.submission/);
 2253: 		    if (join('_',@{$part}) ne ($partid.'_'.$respid)) { next; }
 2254: 		    my ($ressub,$hide,$subval) = split(/:/,$submission,3);
 2255: 		    # Similarity check
 2256: 		    my $similar='';
 2257:                     my ($type,$trial,$rndseed);
 2258:                     if ($hide eq 'rand') {
 2259:                         $type = 'randomizetry';
 2260:                         $trial = $record{"resource.$partid.tries"};
 2261:                         $rndseed = $record{"resource.$partid.rndseed"};
 2262:                     }
 2263: 		    if($env{'form.checkPlag'}){
 2264: 			my ($oname,$odom,$ocrsid,$oessay,$osim)=
 2265: 			    &most_similar($uname,$udom,$subval,\%old_essays);
 2266: 			if ($osim) {
 2267: 			    $osim=int($osim*100.0);
 2268: 			    my %old_course_desc = 
 2269: 				&Apache::lonnet::coursedescription($ocrsid,
 2270: 								   {'one_time' => 1});
 2271: 
 2272:                             if ($hide eq 'anon') {
 2273:                                 $similar='<hr /><span class="LC_warning">'.&mt("Essay was found to be similar to another essay submitted for this assignment.").'<br />'.
 2274:                                          &mt('As the current submission is for an anonymous survey, no other details are available.').'</span><hr />';
 2275:                             } else {
 2276: 			        $similar="<hr /><h3><span class=\"LC_warning\">".
 2277: 				    &mt('Essay is [_1]% similar to an essay by [_2] in course [_3] (course id [_4]:[_5])',
 2278: 				        $osim,
 2279: 				        &Apache::loncommon::plainname($oname,$odom).' ('.$oname.':'.$odom.')',
 2280: 				        $old_course_desc{'description'},
 2281: 				        $old_course_desc{'num'},
 2282: 				        $old_course_desc{'domain'}).
 2283: 				    '</span></h3><blockquote><i>'.
 2284: 				    &keywords_highlight($oessay).
 2285: 				    '</i></blockquote><hr />';
 2286:                             }
 2287: 			}
 2288: 		    }
 2289: 		    my $order=&get_order($partid,$respid,$symb,$uname,$udom,
 2290:                                          undef,$type,$trial,$rndseed);
 2291: 		    if ($env{'form.lastSub'} eq 'lastonly' || 
 2292: 			($env{'form.lastSub'} eq 'hdgrade' && 
 2293: 			 $$handgrade{$$part[0].'_'.$$part[1]} eq 'yes')) {
 2294: 			my $display_part=&get_display_part($partid,$symb);
 2295:                         $lastsubonly.='<div class="LC_grade_submission_part">'.
 2296:                             '<b>'.&mt('Part: [_1]',$display_part).'</b>'.
 2297:                             ' <span class="LC_internal_info">'.
 2298:                             '('.&mt('Response ID: [_1]',$respid).')'.
 2299:                             '</span>&nbsp; &nbsp;';
 2300: 			my $files=&get_submitted_files($udom,$uname,$partid,$respid,\%record);
 2301: 			if (@$files) {
 2302:                             if ($hide eq 'anon') {
 2303:                                 $lastsubonly.='<br />'.&mt('[quant,_1,file] uploaded to this anonymous survey',scalar(@{$files}));
 2304:                             } else {
 2305:                                 $lastsubonly.='<br /><span class="LC_warning">'.&mt('Like all files provided by users, this file may contain viruses').'</span><br />';
 2306:                                 foreach my $file (@$files) {
 2307:                                     &Apache::lonnet::allowuploaded('/adm/grades',$file);
 2308:                                     $lastsubonly.='<br /><a href="'.$file.'?rawmode=1" target="lonGRDs"><img src="'.&Apache::loncommon::icon($file).'" border="0" /> '.$file.'</a>';
 2309:                                 }
 2310:                             }
 2311: 			    $lastsubonly.='<br />';
 2312: 			}
 2313:                         if ($hide eq 'anon') {
 2314:                             $lastsubonly.='<b>'.&mt('Anonymous Survey').'</b>'; 
 2315:                         } else {
 2316: 			    $lastsubonly.='<b>'.&mt('Submitted Answer:').' </b>'.
 2317: 			        &cleanRecord($subval,$responsetype,$symb,$partid,
 2318: 					     $respid,\%record,$order,undef,$uname,$udom,$type,$trial,$rndseed);
 2319:                         }
 2320: 			if ($similar) {$lastsubonly.="<br /><br />$similar\n";}
 2321: 			$lastsubonly.='</div>';
 2322: 		    }
 2323: 		}
 2324: 	    }
 2325: 	    $lastsubonly.='</div>'."\n"; # End: LC_grade_submissions_body
 2326: 	}
 2327: 	$request->print($lastsubonly);
 2328:    } elsif ($env{'form.lastSub'} eq 'datesub') {
 2329: 	my (undef,$responseType,undef,$parts) = &showResourceInfo($symb);
 2330: 	$request->print(&displaySubByDates($symb,\%record,$parts,$responseType,$checkIcon,$uname,$udom));
 2331:     } elsif ($env{'form.lastSub'} =~ /^(last|all)$/) {
 2332: 	$request->print(&Apache::loncommon::get_previous_attempt($symb,$uname,$udom,
 2333: 								 $env{'request.course.id'},
 2334: 								 $last,'.submission',
 2335: 								 'Apache::grades::keywords_highlight'));
 2336:     }
 2337: 
 2338:     $request->print('<input type="hidden" name="unamedom'.$counter.'" value="'.$uname.':'
 2339: 	.$udom.'" />'."\n");
 2340:     # return if view submission with no grading option
 2341:     if ($env{'form.showgrading'} eq '' || (!&canmodify($usec))) {
 2342: 	my $toGrade.='<input type="button" value="Grade Student" '.
 2343: 	    'onclick="javascript:checksubmit(this.form,\'Grade Student\',\''
 2344: 	    .$counter.'\');" target="_self" /> &nbsp;'."\n" if (&canmodify($usec));
 2345: 	$toGrade.='</div>'."\n";
 2346: 	if (($env{'form.command'} eq 'submission') || 
 2347: 	    ($env{'form.command'} eq 'processGroup' && $counter == $total)) {
 2348: 	    $toGrade.='</form>'.&show_grading_menu_form($symb); 
 2349: 	}
 2350: 	$request->print($toGrade);
 2351: 	return;
 2352:     } else {
 2353: 	$request->print('</div>'."\n");
 2354:     }
 2355: 
 2356:     # essay grading message center
 2357:     if ($env{'form.handgrade'} eq 'yes') {
 2358: 	my $result='<div class="LC_grade_message_center">';
 2359:     
 2360: 	$result.='<div class="LC_grade_message_center_header">'.
 2361: 	    &mt('Send Message').'</div><div class="LC_grade_message_center_body">';
 2362: 	my ($lastname,$givenn) = split(/,/,$env{'form.fullname'});
 2363: 	my $msgfor = $givenn.' '.$lastname;
 2364: 	if (scalar(@$col_fullnames) > 0) {
 2365: 	    my $lastone = pop(@$col_fullnames);
 2366: 	    $msgfor .= ', '.(join ', ',@$col_fullnames).' and '.$lastone.'.';
 2367: 	}
 2368: 	$msgfor =~ s/\'/\\'/g; #' stupid emacs - no! javascript
 2369: 	$result.='<input type="hidden" name="includemsg'.$counter.'" value="" />'."\n".
 2370: 	    '<input type="hidden" name="newmsg'.$counter.'" value="" />'."\n";
 2371: 	$result.='&nbsp;<a href="javascript:msgCenter(document.SCORE,'.$counter.
 2372: 	    ',\''.$msgfor.'\');" target="_self">'.
 2373: 	    &mt('Compose message to student').(scalar(@$col_fullnames) >= 1 ? 's' : '').'</a><label> ('.
 2374: 	    &mt('incl. grades').' <input type="checkbox" name="withgrades'.$counter.'" /></label>)'.
 2375: 	    '<img src="'.$request->dir_config('lonIconsURL').
 2376: 	    '/mailbkgrd.gif" width="14" height="10" name="mailicon'.$counter.'" />'."\n".
 2377: 	    '<br />&nbsp;('.
 2378: 	    &mt('Message will be sent when you click on Save &amp; Next below.').")\n";
 2379: 	$result.='</div></div>';
 2380: 	$request->print($result);
 2381:     }
 2382: 
 2383:     my %seen = ();
 2384:     my @partlist;
 2385:     my @gradePartRespid;
 2386:     my @part_response_id = &flatten_responseType($responseType);
 2387:     $request->print(
 2388:         '<div class="LC_Box">'
 2389:        .'<h3 class="LC_hcell">'.&mt('Assign Grades').'</h3>'
 2390:     );
 2391:     $request->print(&gradeBox_start());
 2392:     foreach my $part_response_id (@part_response_id) {
 2393:     	my ($partid,$respid) = @{ $part_response_id };
 2394: 	my $part_resp = join('_',@{ $part_response_id });
 2395: 	next if ($seen{$partid} > 0);
 2396: 	$seen{$partid}++;
 2397: 	next if ($$handgrade{$part_resp} ne 'yes' 
 2398: 		 && $env{'form.lastSub'} eq 'hdgrade');
 2399: 	push(@partlist,$partid);
 2400: 	push(@gradePartRespid,$partid.'.'.$respid);
 2401: 	$request->print(&gradeBox($request,$symb,$uname,$udom,$counter,$partid,\%record));
 2402:     }
 2403:     $request->print(&gradeBox_end()); # </div>
 2404:     $request->print('</div>');
 2405: 
 2406:     $request->print('<div class="LC_grade_info_links">');
 2407:     $request->print('</div>');
 2408: 
 2409:     $result='<input type="hidden" name="partlist'.$counter.
 2410: 	'" value="'.(join ":",@partlist).'" />'."\n";
 2411:     $result.='<input type="hidden" name="gradePartRespid'.
 2412: 	'" value="'.(join ":",@gradePartRespid).'" />'."\n" if ($counter == 0);
 2413:     my $ctr = 0;
 2414:     while ($ctr < scalar(@partlist)) {
 2415: 	$result.='<input type="hidden" name="partid'.$counter.'_'.$ctr.'" value="'.
 2416: 	    $partlist[$ctr].'" />'."\n";
 2417: 	$ctr++;
 2418:     }
 2419:     $request->print($result.''."\n");
 2420: 
 2421: # Done with printing info for one student
 2422: 
 2423:     $request->print('</div>');#LC_grade_show_user
 2424: 
 2425: 
 2426:     # print end of form
 2427:     if ($counter == $total) {
 2428:         my $endform='<br /><hr /><table border="0"><tr><td>'."\n";
 2429: 	$endform.='<input type="button" value="'.&mt('Save &amp; Next').'" '.
 2430: 	    'onclick="javascript:checksubmit(this.form,\'Save & Next\','.
 2431: 	    $total.','.scalar(@partlist).');" target="_self" /> &nbsp;'."\n";
 2432: 	my $ntstu ='<select name="NTSTU">'.
 2433: 	    '<option>1</option><option>2</option>'.
 2434: 	    '<option>3</option><option>5</option>'.
 2435: 	    '<option>7</option><option>10</option></select>'."\n";
 2436: 	my $nsel = ($env{'form.NTSTU'} ne '' ? $env{'form.NTSTU'} : '1');
 2437: 	$ntstu =~ s/<option>$nsel</<option selected="selected">$nsel</;
 2438:         $endform.=&mt('[_1]student(s)',$ntstu);
 2439: 	$endform.='&nbsp;&nbsp;<input type="button" value="'.&mt('Previous').'" '.
 2440: 	    'onclick="javascript:checksubmit(this.form,\'Previous\');" target="_self" /> &nbsp;'."\n".
 2441: 	    '<input type="button" value="'.&mt('Next').'" '.
 2442: 	    'onclick="javascript:checksubmit(this.form,\'Next\');" target="_self" /> &nbsp;';
 2443:         $endform.='<span class="LC_warning">'.
 2444:                   &mt('(Next and Previous (student) do not save the scores.)').
 2445:                   '</span>'."\n" ;
 2446:         $endform.="<input type='hidden' value='".&get_increment().
 2447:             "' name='increment' />";
 2448: 	$endform.='</td></tr></table></form>';
 2449: 	$endform.=&show_grading_menu_form($symb);
 2450: 	$request->print($endform);
 2451:     }
 2452:     return '';
 2453: }
 2454: 
 2455: sub check_collaborators {
 2456:     my ($symb,$uname,$udom,$record,$handgrade,$counter) = @_;
 2457:     my ($result,@col_fullnames);
 2458:     my ($classlist,undef,$fullname) = &getclasslist('all','0');
 2459:     foreach my $part (keys(%$handgrade)) {
 2460: 	my $ncol = &Apache::lonnet::EXT('resource.'.$part.
 2461: 					'.maxcollaborators',
 2462: 					$symb,$udom,$uname);
 2463: 	next if ($ncol <= 0);
 2464: 	$part =~ s/\_/\./g;
 2465: 	next if ($record->{'resource.'.$part.'.collaborators'} eq '');
 2466: 	my (@good_collaborators, @bad_collaborators);
 2467: 	foreach my $possible_collaborator
 2468: 	    (split(/[,;\s]+/,$record->{'resource.'.$part.'.collaborators'})) { 
 2469: 	    $possible_collaborator =~ s/[\$\^\(\)]//g;
 2470: 	    next if ($possible_collaborator eq '');
 2471: 	    my ($co_name,$co_dom) = split(/\@|:/,$possible_collaborator);
 2472: 	    $co_dom = $udom if (! defined($co_dom) || $co_dom =~ /^domain$/i);
 2473: 	    next if ($co_name eq $uname && $co_dom eq $udom);
 2474: 	    # Doing this grep allows 'fuzzy' specification
 2475: 	    my @matches = grep(/^\Q$co_name\E:\Q$co_dom\E$/i, 
 2476: 			       keys(%$classlist));
 2477: 	    if (! scalar(@matches)) {
 2478: 		push(@bad_collaborators, $possible_collaborator);
 2479: 	    } else {
 2480: 		push(@good_collaborators, @matches);
 2481: 	    }
 2482: 	}
 2483: 	if (scalar(@good_collaborators) != 0) {
 2484: 	    $result.='<br />'.&mt('Collaborators: ').'<ol>';
 2485: 	    foreach my $name (@good_collaborators) {
 2486: 		my ($lastname,$givenn) = split(/,/,$$fullname{$name});
 2487: 		push(@col_fullnames, $givenn.' '.$lastname);
 2488: 		$result.='<li>'.$fullname->{$name}.'</li>';
 2489: 	    }
 2490: 	    $result.='</ol><br />'."\n";
 2491: 	    my ($part)=split(/\./,$part);
 2492: 	    $result.='<input type="hidden" name="collaborator'.$counter.
 2493: 		'" value="'.$part.':'.(join ':',@good_collaborators).'" />'.
 2494: 		"\n";
 2495: 	}
 2496: 	if (scalar(@bad_collaborators) > 0) {
 2497: 	    $result.='<div class="LC_warning">';
 2498: 	    $result.=&mt('This student has submitted [quant,_1,invalid collaborator]: [_2]',scalar(@bad_collaborators),join(', ',@bad_collaborators));
 2499: 	    $result .= '</div>';
 2500: 	}         
 2501: 	if (scalar(@bad_collaborators > $ncol)) {
 2502: 	    $result .= '<div class="LC_warning">';
 2503: 	    $result .= &mt('This student has submitted too many '.
 2504: 		'collaborators.  Maximum is [_1].',$ncol);
 2505: 	    $result .= '</div>';
 2506: 	}
 2507:     }
 2508:     return ($result,$fullname,\@col_fullnames);
 2509: }
 2510: 
 2511: #--- Retrieve the last submission for all the parts
 2512: sub get_last_submission {
 2513:     my ($returnhash)=@_;
 2514:     my (@string,$timestamp,%lasthidden);
 2515:     if ($$returnhash{'version'}) {
 2516: 	my %lasthash=();
 2517: 	my ($version);
 2518: 	for ($version=1;$version<=$$returnhash{'version'};$version++) {
 2519: 	    foreach my $key (sort(split(/\:/,
 2520: 					$$returnhash{$version.':keys'}))) {
 2521: 		$lasthash{$key}=$$returnhash{$version.':'.$key};
 2522: 		$timestamp = 
 2523: 		    &Apache::lonlocal::locallocaltime($$returnhash{$version.':timestamp'});
 2524: 	    }
 2525: 	}
 2526:         my (%typeparts,%randombytry);
 2527:         my $showsurv = 
 2528:             &Apache::lonnet::allowed('vas',$env{'request.course.id'});
 2529:         foreach my $key (sort(keys(%lasthash))) {
 2530:             if ($key =~ /\.type$/) {
 2531:                 if (($lasthash{$key} eq 'anonsurvey') || 
 2532:                     ($lasthash{$key} eq 'anonsurveycred') ||
 2533:                     ($lasthash{$key} eq 'randomizetry')) {
 2534:                     my ($ign,@parts) = split(/\./,$key);
 2535:                     pop(@parts);
 2536:                     my $id = join('.',@parts);
 2537:                     if ($lasthash{$key} eq 'randomizetry') {
 2538:                         $randombytry{$ign.'.'.$id} = $lasthash{$key};
 2539:                     } else {
 2540:                         unless ($showsurv) {
 2541:                             $typeparts{$ign.'.'.$id} = $lasthash{$key};
 2542:                         }
 2543:                     }
 2544:                     delete($lasthash{$key});
 2545:                 }
 2546:             }
 2547:         }
 2548:         my @hidden = keys(%typeparts);
 2549:         my @randomize = keys(%randombytry);
 2550: 	foreach my $key (keys(%lasthash)) {
 2551: 	    next if ($key !~ /\.submission$/);
 2552:             my $hide;
 2553:             if (@hidden) {
 2554:                 foreach my $id (@hidden) {
 2555:                     if ($key =~ /^\Q$id\E/) {
 2556:                         $hide = 'anon';
 2557:                         last;
 2558:                     }
 2559:                 }
 2560:             }
 2561:             unless ($hide) {
 2562:                 if (@randomize) {
 2563:                     foreach my $id (@hidden) {
 2564:                         if ($key =~ /^\Q$id\E/) {
 2565:                             $hide = 'rand';
 2566:                             last;
 2567:                         }
 2568:                     }
 2569:                 }
 2570:             }
 2571: 	    my ($partid,$foo) = split(/submission$/,$key);
 2572: 	    my $draft  = $lasthash{$partid.'awarddetail'} eq 'DRAFT' ?
 2573: 		'<span class="LC_warning">Draft Copy</span> ' : '';
 2574: 	    push(@string, join(':', $key, $hide, $draft.$lasthash{$key}));
 2575: 	}
 2576:     }
 2577:     if (!@string) {
 2578: 	$string[0] =
 2579: 	    '<span class="LC_warning">'.&mt('Nothing submitted - no attempts.').'</span>';
 2580:     }
 2581:     return (\@string,\$timestamp);
 2582: }
 2583: 
 2584: #--- High light keywords, with style choosen by user.
 2585: sub keywords_highlight {
 2586:     my $string    = shift;
 2587:     my $size      = $env{'form.kwsize'} eq '0' ? '' : 'size='.$env{'form.kwsize'};
 2588:     my $styleon   = $env{'form.kwstyle'} eq ''  ? '' : $env{'form.kwstyle'};
 2589:     (my $styleoff = $styleon) =~ s/\</\<\//;
 2590:     my @keylist   = split(/[,\s+]/,$env{'form.keywords'});
 2591:     foreach my $keyword (@keylist) {
 2592: 	$string =~ s/\b\Q$keyword\E(\b|\.)/<font color\=$env{'form.kwclr'} $size\>$styleon$keyword$styleoff<\/font>/gi;
 2593:     }
 2594:     return $string;
 2595: }
 2596: 
 2597: #--- Called from submission routine
 2598: sub processHandGrade {
 2599:     my ($request) = shift;
 2600:     my $symb   = &get_symb($request);
 2601:     my (undef,undef,$url) = &Apache::lonnet::decode_symb($symb);
 2602:     my $button = $env{'form.gradeOpt'};
 2603:     my $ngrade = $env{'form.NCT'};
 2604:     my $ntstu  = $env{'form.NTSTU'};
 2605:     my $cdom   = $env{'course.'.$env{'request.course.id'}.'.domain'};
 2606:     my $cnum   = $env{'course.'.$env{'request.course.id'}.'.num'};
 2607: 
 2608:     if ($button eq 'Save & Next') {
 2609: 	my $ctr = 0;
 2610: 	while ($ctr < $ngrade) {
 2611: 	    my ($uname,$udom) = split(/:/,$env{'form.unamedom'.$ctr});
 2612: 	    my ($errorflag,$pts,$wgt) = &saveHandGrade($request,$symb,$uname,$udom,$ctr);
 2613: 	    if ($errorflag eq 'no_score') {
 2614: 		$ctr++;
 2615: 		next;
 2616: 	    }
 2617: 	    if ($errorflag eq 'not_allowed') {
 2618: 		$request->print("<span class=\"LC_warning\">Not allowed to modify grades for $uname:$udom</span>");
 2619: 		$ctr++;
 2620: 		next;
 2621: 	    }
 2622: 	    my $includemsg = $env{'form.includemsg'.$ctr};
 2623: 	    my ($subject,$message,$msgstatus) = ('','','');
 2624: 	    my $restitle = &Apache::lonnet::gettitle($symb);
 2625:             my ($feedurl,$showsymb) =
 2626: 		&get_feedurl_and_symb($symb,$uname,$udom);
 2627: 	    my $messagetail;
 2628: 	    if ($includemsg =~ /savemsg|newmsg\Q$ctr\E/) {
 2629: 		$subject = $env{'form.msgsub'} if ($includemsg =~ /msgsub/);
 2630: 		unless ($subject=~/\w/) { $subject=&mt('Grading Feedback'); }
 2631: 		$subject.=' ['.$restitle.']';
 2632: 		my (@msgnum) = split(/,/,$includemsg);
 2633: 		foreach (@msgnum) {
 2634: 		    $message.=$env{'form.'.$_} if ($_ =~ /savemsg|newmsg/ && $_ ne '');
 2635: 		}
 2636: 		$message =&Apache::lonfeedback::clear_out_html($message);
 2637: 		if ($env{'form.withgrades'.$ctr}) {
 2638: 		    $message.="\n\nPoint".($pts > 1 ? 's':'').' awarded = '.$pts.' out of '.$wgt;
 2639: 		    $messagetail = " for <a href=\"".
 2640: 		                   $feedurl."?symb=$showsymb\">$env{'form.probTitle'}</a>";
 2641: 		}
 2642: 		$msgstatus = 
 2643:                     &Apache::lonmsg::user_normal_msg($uname,$udom,$subject,
 2644: 						     $message.$messagetail,
 2645:                                                      undef,$feedurl,undef,
 2646:                                                      undef,undef,$showsymb,
 2647:                                                      $restitle);
 2648: 		$request->print('<br />'.&mt('Sending message to [_1]',$uname.':'.$udom).': '.
 2649: 				$msgstatus.'<br />');
 2650: 	    }
 2651: 	    if ($env{'form.collaborator'.$ctr}) {
 2652: 		my @collabstrs=&Apache::loncommon::get_env_multiple("form.collaborator$ctr");
 2653: 		foreach my $collabstr (@collabstrs) {
 2654: 		    my ($part,@collaborators) = split(/:/,$collabstr);
 2655: 		    foreach my $collaborator (@collaborators) {
 2656: 			my ($errorflag,$pts,$wgt) = 
 2657: 			    &saveHandGrade($request,$symb,$collaborator,$udom,$ctr,
 2658: 					   $env{'form.unamedom'.$ctr},$part);
 2659: 			if ($errorflag eq 'not_allowed') {
 2660: 			    $request->print("<span class=\"LC_error\">".&mt('Not allowed to modify grades for [_1]',"$collaborator:$udom")."</span>");
 2661: 			    next;
 2662: 			} elsif ($message ne '') {
 2663: 			    my ($baseurl,$showsymb) = 
 2664: 				&get_feedurl_and_symb($symb,$collaborator,
 2665: 						      $udom);
 2666: 			    if ($env{'form.withgrades'.$ctr}) {
 2667: 				$messagetail = " for <a href=\"".
 2668:                                     $baseurl."?symb=$showsymb\">$env{'form.probTitle'}</a>";
 2669: 			    }
 2670: 			    $msgstatus = 
 2671: 				&Apache::lonmsg::user_normal_msg($collaborator,$udom,$subject,$message.$messagetail,undef,$baseurl,undef,undef,undef,$showsymb,$restitle);
 2672: 			}
 2673: 		    }
 2674: 		}
 2675: 	    }
 2676: 	    $ctr++;
 2677: 	}
 2678:     }
 2679: 
 2680:     if ($env{'form.handgrade'} eq 'yes') {
 2681: 	# Keywords sorted in alphabatical order
 2682: 	my $loginuser = $env{'user.name'}.':'.$env{'user.domain'};
 2683: 	my %keyhash = ();
 2684: 	$env{'form.keywords'}           =~ s/,\s{0,}|\s+/ /g;
 2685: 	$env{'form.keywords'}           =~ s/^\s+|\s+$//;
 2686: 	my (@keywords) = sort(split(/\s+/,$env{'form.keywords'}));
 2687: 	$env{'form.keywords'} = join(' ',@keywords);
 2688: 	$keyhash{$symb.'_keywords'}     = $env{'form.keywords'};
 2689: 	$keyhash{$symb.'_subject'}      = $env{'form.msgsub'};
 2690: 	$keyhash{$loginuser.'_kwclr'}   = $env{'form.kwclr'};
 2691: 	$keyhash{$loginuser.'_kwsize'}  = $env{'form.kwsize'};
 2692: 	$keyhash{$loginuser.'_kwstyle'} = $env{'form.kwstyle'};
 2693: 
 2694: 	# message center - Order of message gets changed. Blank line is eliminated.
 2695: 	# New messages are saved in env for the next student.
 2696: 	# All messages are saved in nohist_handgrade.db
 2697: 	my ($ctr,$idx) = (1,1);
 2698: 	while ($ctr <= $env{'form.savemsgN'}) {
 2699: 	    if ($env{'form.savemsg'.$ctr} ne '') {
 2700: 		$keyhash{$symb.'_savemsg'.$idx} = $env{'form.savemsg'.$ctr};
 2701: 		$idx++;
 2702: 	    }
 2703: 	    $ctr++;
 2704: 	}
 2705: 	$ctr = 0;
 2706: 	while ($ctr < $ngrade) {
 2707: 	    if ($env{'form.newmsg'.$ctr} ne '') {
 2708: 		$keyhash{$symb.'_savemsg'.$idx} = $env{'form.newmsg'.$ctr};
 2709: 		$env{'form.savemsg'.$idx} = $env{'form.newmsg'.$ctr};
 2710: 		$idx++;
 2711: 	    }
 2712: 	    $ctr++;
 2713: 	}
 2714: 	$env{'form.savemsgN'} = --$idx;
 2715: 	$keyhash{$symb.'_savemsgN'} = $env{'form.savemsgN'};
 2716: 	my $putresult = &Apache::lonnet::put
 2717: 	    ('nohist_handgrade',\%keyhash,$cdom,$cnum);
 2718:     }
 2719:     # Called by Save & Refresh from Highlight Attribute Window
 2720:     my (undef,undef,$fullname) = &getclasslist($env{'form.section'},'1');
 2721:     if ($env{'form.refresh'} eq 'on') {
 2722: 	my ($ctr,$total) = (0,0);
 2723: 	while ($ctr < $ngrade) {
 2724: 	    $total++ if  $env{'form.unamedom'.$ctr} ne '';
 2725: 	    $ctr++;
 2726: 	}
 2727: 	$env{'form.NTSTU'}=$ngrade;
 2728: 	$ctr = 0;
 2729: 	while ($ctr < $total) {
 2730: 	    my $processUser = $env{'form.unamedom'.$ctr};
 2731: 	    ($env{'form.student'},$env{'form.userdom'}) = split(/:/,$processUser);
 2732: 	    $env{'form.fullname'} = $$fullname{$processUser};
 2733: 	    &submission($request,$ctr,$total-1);
 2734: 	    $ctr++;
 2735: 	}
 2736: 	return '';
 2737:     }
 2738: 
 2739: # Go directly to grade student - from submission or link from chart page
 2740:     if ($button eq 'Grade Student') {
 2741: 	(undef,undef,$env{'form.handgrade'},undef,undef) = &showResourceInfo($symb);
 2742: 	my $processUser = $env{'form.unamedom'.$env{'form.studentNo'}};
 2743: 	($env{'form.student'},$env{'form.userdom'}) = split(/:/,$processUser);
 2744: 	$env{'form.fullname'} = $$fullname{$processUser};
 2745: 	&submission($request,0,0);
 2746: 	return '';
 2747:     }
 2748: 
 2749:     # Get the next/previous one or group of students
 2750:     my $firststu = $env{'form.unamedom0'};
 2751:     my $laststu = $env{'form.unamedom'.($ngrade-1)};
 2752:     my $ctr = 2;
 2753:     while ($laststu eq '') {
 2754: 	$laststu  = $env{'form.unamedom'.($ngrade-$ctr)};
 2755: 	$ctr++;
 2756: 	$laststu = $firststu if ($ctr > $ngrade);
 2757:     }
 2758: 
 2759:     my (@parsedlist,@nextlist);
 2760:     my ($nextflg) = 0;
 2761:     foreach my $item (sort 
 2762: 	     {
 2763: 		 if (lc($$fullname{$a}) ne lc($$fullname{$b})) {
 2764: 		     return (lc($$fullname{$a}) cmp lc($$fullname{$b}));
 2765: 		 }
 2766: 		 return $a cmp $b;
 2767: 	     } (keys(%$fullname))) {
 2768: 	if ($nextflg == 1 && $button =~ /Next$/) {
 2769: 	    push(@parsedlist,$item);
 2770: 	}
 2771: 	$nextflg = 1 if ($item eq $laststu);
 2772: 	if ($button eq 'Previous') {
 2773: 	    last if ($item eq $firststu);
 2774: 	    push(@parsedlist,$item);
 2775: 	}
 2776:     }
 2777:     $ctr = 0;
 2778:     @parsedlist = reverse @parsedlist if ($button eq 'Previous');
 2779:     my $res_error;
 2780:     my ($partlist) = &response_type($symb,\$res_error);
 2781:     if ($res_error) {
 2782:         $request->print(&navmap_errormsg());
 2783:         return;
 2784:     }
 2785:     foreach my $student (@parsedlist) {
 2786: 	my $submitonly=$env{'form.submitonly'};
 2787: 	my ($uname,$udom) = split(/:/,$student);
 2788: 	
 2789: 	if ($submitonly eq 'queued') {
 2790: 	    my %queue_status = 
 2791: 		&Apache::bridgetask::get_student_status($symb,$cdom,$cnum,
 2792: 							$udom,$uname);
 2793: 	    next if (!defined($queue_status{'gradingqueue'}));
 2794: 	}
 2795: 
 2796: 	if ($submitonly =~ /^(yes|graded|incorrect)$/) {
 2797: #	    my %record = &Apache::lonnet::restore($symb,$env{'request.course.id'},$udom,$uname);
 2798: 	    my %status=&student_gradeStatus($symb,$udom,$uname,$partlist);
 2799: 	    my $submitted = 0;
 2800: 	    my $ungraded = 0;
 2801: 	    my $incorrect = 0;
 2802: 	    foreach my $item (keys(%status)) {
 2803: 		$submitted = 1 if ($status{$item} ne 'nothing');
 2804: 		$ungraded = 1 if ($status{$item} =~ /^ungraded/);
 2805: 		$incorrect = 1 if ($status{$item} =~ /^incorrect/);
 2806: 		my ($foo,$partid,$foo1) = split(/\./,$item);
 2807: 		if ($status{'resource.'.$partid.'.submitted_by'} ne '') {
 2808: 		    $submitted = 0;
 2809: 		}
 2810: 	    }
 2811: 	    next if (!$submitted && ($submitonly eq 'yes' ||
 2812: 				     $submitonly eq 'incorrect' ||
 2813: 				     $submitonly eq 'graded'));
 2814: 	    next if (!$ungraded && ($submitonly eq 'graded'));
 2815: 	    next if (!$incorrect && $submitonly eq 'incorrect');
 2816: 	}
 2817: 	push(@nextlist,$student) if ($ctr < $ntstu);
 2818: 	last if ($ctr == $ntstu);
 2819: 	$ctr++;
 2820:     }
 2821: 
 2822:     $ctr = 0;
 2823:     my $total = scalar(@nextlist)-1;
 2824: 
 2825:     foreach (sort(@nextlist)) {
 2826: 	my ($uname,$udom,$submitter) = split(/:/);
 2827: 	$env{'form.student'}  = $uname;
 2828: 	$env{'form.userdom'}  = $udom;
 2829: 	$env{'form.fullname'} = $$fullname{$_};
 2830: 	&submission($request,$ctr,$total);
 2831: 	$ctr++;
 2832:     }
 2833:     if ($total < 0) {
 2834: 	my $the_end = '<h3><span class="LC_info">'.&mt('LON-CAPA User Message').'</span></h3><br />'."\n";
 2835: 	$the_end.='<p>'.&mt('[_1]Message:[_2] No more students for this section or class.','<b>','</b>').'</p>'."\n";
 2836: 	$the_end.=&mt('Click on the button below to return to the grading menu.').'<br /><br />'."\n";
 2837: 	$the_end.=&show_grading_menu_form($symb);
 2838: 	$request->print($the_end);
 2839:     }
 2840:     return '';
 2841: }
 2842: 
 2843: #---- Save the score and award for each student, if changed
 2844: sub saveHandGrade {
 2845:     my ($request,$symb,$stuname,$domain,$newflg,$submitter,$part) = @_;
 2846:     my @version_parts;
 2847:     my $usec = &Apache::lonnet::getsection($domain,$stuname,
 2848: 					   $env{'request.course.id'});
 2849:     if (!&canmodify($usec)) { return('not_allowed'); }
 2850:     my %record = &Apache::lonnet::restore($symb,$env{'request.course.id'},$domain,$stuname);
 2851:     my @parts_graded;
 2852:     my %newrecord  = ();
 2853:     my ($pts,$wgt) = ('','');
 2854:     my %aggregate = ();
 2855:     my $aggregateflag = 0;
 2856:     my @parts = split(/:/,$env{'form.partlist'.$newflg});
 2857:     foreach my $new_part (@parts) {
 2858: 	#collaborator ($submi may vary for different parts
 2859: 	if ($submitter && $new_part ne $part) { next; }
 2860: 	my $dropMenu = $env{'form.GD_SEL'.$newflg.'_'.$new_part};
 2861: 	if ($dropMenu eq 'excused') {
 2862: 	    if ($record{'resource.'.$new_part.'.solved'} ne 'excused') {
 2863: 		$newrecord{'resource.'.$new_part.'.solved'} = 'excused';
 2864: 		if (exists($record{'resource.'.$new_part.'.awarded'})) {
 2865: 		    $newrecord{'resource.'.$new_part.'.awarded'} = '';
 2866: 		}
 2867: 	        $newrecord{'resource.'.$new_part.'.regrader'}="$env{'user.name'}:$env{'user.domain'}";
 2868: 	    }
 2869: 	} elsif ($dropMenu eq 'reset status'
 2870: 		 && exists($record{'resource.'.$new_part.'.solved'})) { #don't bother if no old records -> no attempts
 2871: 	    foreach my $key (keys(%record)) {
 2872: 		if ($key=~/^resource\.\Q$new_part\E\./) { $newrecord{$key} = ''; }
 2873: 	    }
 2874: 	    $newrecord{'resource.'.$new_part.'.regrader'}=
 2875: 		"$env{'user.name'}:$env{'user.domain'}";
 2876:             my $totaltries = $record{'resource.'.$part.'.tries'};
 2877: 
 2878:             my %last_resets = &get_last_resets($symb,$env{'request.course.id'},
 2879: 					       [$new_part]);
 2880:             my $aggtries =$totaltries;
 2881:             if ($last_resets{$new_part}) {
 2882:                 $aggtries = &get_num_tries(\%record,$last_resets{$new_part},
 2883: 					   $new_part);
 2884:             }
 2885: 
 2886:             my $solvedstatus = $record{'resource.'.$new_part.'.solved'};
 2887:             if ($aggtries > 0) {
 2888:                 &decrement_aggs($symb,$new_part,\%aggregate,$aggtries,$totaltries,$solvedstatus);
 2889:                 $aggregateflag = 1;
 2890:             }
 2891: 	} elsif ($dropMenu eq '') {
 2892: 	    $pts = ($env{'form.GD_BOX'.$newflg.'_'.$new_part} ne '' ? 
 2893: 		    $env{'form.GD_BOX'.$newflg.'_'.$new_part} : 
 2894: 		    $env{'form.RADVAL'.$newflg.'_'.$new_part});
 2895: 	    if ($pts eq '' && $env{'form.GD_SEL'.$newflg.'_'.$new_part} eq '') {
 2896: 		next;
 2897: 	    }
 2898: 	    $wgt = $env{'form.WGT'.$newflg.'_'.$new_part} eq '' ? 1 : 
 2899: 		$env{'form.WGT'.$newflg.'_'.$new_part};
 2900: 	    my $partial= $pts/$wgt;
 2901: 	    if ($partial eq $record{'resource.'.$new_part.'.awarded'}) {
 2902: 		#do not update score for part if not changed.
 2903:                 &handback_files($request,$symb,$stuname,$domain,$newflg,$new_part,\%newrecord);
 2904: 		next;
 2905: 	    } else {
 2906: 	        push(@parts_graded,$new_part);
 2907: 	    }
 2908: 	    if ($record{'resource.'.$new_part.'.awarded'} ne $partial) {
 2909: 		$newrecord{'resource.'.$new_part.'.awarded'}  = $partial;
 2910: 	    }
 2911: 	    my $reckey = 'resource.'.$new_part.'.solved';
 2912: 	    if ($partial == 0) {
 2913: 		if ($record{$reckey} ne 'incorrect_by_override') {
 2914: 		    $newrecord{$reckey} = 'incorrect_by_override';
 2915: 		}
 2916: 	    } else {
 2917: 		if ($record{$reckey} ne 'correct_by_override') {
 2918: 		    $newrecord{$reckey} = 'correct_by_override';
 2919: 		}
 2920: 	    }	    
 2921: 	    if ($submitter && 
 2922: 		($record{'resource.'.$new_part.'.submitted_by'} ne $submitter)) {
 2923: 		$newrecord{'resource.'.$new_part.'.submitted_by'} = $submitter;
 2924: 	    }
 2925: 	    $newrecord{'resource.'.$new_part.'.regrader'}=
 2926: 		"$env{'user.name'}:$env{'user.domain'}";
 2927: 	}
 2928: 	# unless problem has been graded, set flag to version the submitted files
 2929: 	unless ($record{'resource.'.$new_part.'.solved'} =~ /^correct_/  || 
 2930: 	        $record{'resource.'.$new_part.'.solved'} eq 'incorrect_by_override' ||
 2931: 	        $dropMenu eq 'reset status')
 2932: 	   {
 2933: 	    push(@version_parts,$new_part);
 2934: 	}
 2935:     }
 2936:     my $cdom = $env{'course.'.$env{'request.course.id'}.'.domain'};
 2937:     my $cnum = $env{'course.'.$env{'request.course.id'}.'.num'};
 2938: 
 2939:     if (%newrecord) {
 2940:         if (@version_parts) {
 2941:             my @changed_keys = &version_portfiles(\%record, \@parts_graded, 
 2942:                                 $env{'request.course.id'}, $symb, $domain, $stuname, \@version_parts);
 2943: 	    @newrecord{@changed_keys} = @record{@changed_keys};
 2944: 	    foreach my $new_part (@version_parts) {
 2945: 		&handback_files($request,$symb,$stuname,$domain,$newflg,
 2946: 				$new_part,\%newrecord);
 2947: 	    }
 2948:         }
 2949: 	&Apache::lonnet::cstore(\%newrecord,$symb,
 2950: 				$env{'request.course.id'},$domain,$stuname);
 2951: 	&check_and_remove_from_queue(\@parts,\%record,\%newrecord,$symb,
 2952: 				     $cdom,$cnum,$domain,$stuname);
 2953:     }
 2954:     if ($aggregateflag) {
 2955:         &Apache::lonnet::cinc('nohist_resourcetracker',\%aggregate,
 2956: 			      $cdom,$cnum);
 2957:     }
 2958:     return ('',$pts,$wgt);
 2959: }
 2960: 
 2961: sub check_and_remove_from_queue {
 2962:     my ($parts,$record,$newrecord,$symb,$cdom,$cnum,$domain,$stuname) = @_;
 2963:     my @ungraded_parts;
 2964:     foreach my $part (@{$parts}) {
 2965: 	if (    $record->{   'resource.'.$part.'.awarded'} eq ''
 2966: 	     && $record->{   'resource.'.$part.'.solved' } ne 'excused'
 2967: 	     && $newrecord->{'resource.'.$part.'.awarded'} eq ''
 2968: 	     && $newrecord->{'resource.'.$part.'.solved' } ne 'excused'
 2969: 		) {
 2970: 	    push(@ungraded_parts, $part);
 2971: 	}
 2972:     }
 2973:     if ( !@ungraded_parts ) {
 2974: 	&Apache::bridgetask::remove_from_queue('gradingqueue',$symb,$cdom,
 2975: 					       $cnum,$domain,$stuname);
 2976:     }
 2977: }
 2978: 
 2979: sub handback_files {
 2980:     my ($request,$symb,$stuname,$domain,$newflg,$new_part,$newrecord) = @_;
 2981:     my $portfolio_root = '/userfiles/portfolio';
 2982:     my $res_error;
 2983:     my ($partlist,$handgrade,$responseType) = &response_type($symb,\$res_error);
 2984:     if ($res_error) {
 2985:         $request->print('<br />'.&navmap_errormsg().'<br />');
 2986:         return;
 2987:     }
 2988:     my @handedback;
 2989:     my $file_msg;
 2990:     my @part_response_id = &flatten_responseType($responseType);
 2991:     foreach my $part_response_id (@part_response_id) {
 2992:     	my ($part_id,$resp_id) = @{ $part_response_id };
 2993: 	my $part_resp = join('_',@{ $part_response_id });
 2994:         if (($env{'form.'.$newflg.'_'.$part_resp.'_countreturndoc'} =~ /^\d+$/) & ($new_part eq $part_id)) {
 2995:             for (my $counter=1; $counter<=$env{'form.'.$newflg.'_'.$part_resp.'_countreturndoc'}; $counter++) {
 2996:                 # if multiple files are uploaded names will be 'returndoc2','returndoc3'
 2997: 		if ($env{'form.'.$newflg.'_'.$part_resp.'_returndoc'.$counter}) {
 2998:                     my $fname=$env{'form.'.$newflg.'_'.$part_resp.'_returndoc'.$counter.'.filename'};
 2999:                     my ($directory,$answer_file) = 
 3000:                         ($env{'form.'.$newflg.'_'.$part_resp.'_origdoc'.$counter} =~ /^(.*?)([^\/]*)$/);
 3001:                     my ($answer_name,$answer_ver,$answer_ext) =
 3002: 		        &file_name_version_ext($answer_file);
 3003: 		    my ($portfolio_path) = ($directory =~ /^.+$stuname\/portfolio(.*)/);
 3004:                     my $getpropath = 1;
 3005: 		    my @dir_list = &Apache::lonnet::dirlist($portfolio_root.$portfolio_path,$domain,$stuname,$getpropath);
 3006: 		    my $version = &get_next_version($answer_name, $answer_ext, \@dir_list);
 3007:                     # fix file name
 3008:                     my ($save_file_name) = (($directory.$answer_name.".$version.".$answer_ext) =~ /^.+\/${stuname}\/(.*)/);
 3009:                     my $result=&Apache::lonnet::finishuserfileupload($stuname,$domain,
 3010:             	                                $newflg.'_'.$part_resp.'_returndoc'.$counter,
 3011:             	                                $save_file_name);
 3012:                     if ($result !~ m|^/uploaded/|) {
 3013:                         $request->print('<br /><span class="LC_error">'.
 3014:                             &mt('An error occurred ([_1]) while trying to upload [_2].',
 3015:                                 $result,$newflg.'_'.$part_resp.'_returndoc'.$counter).
 3016:                                         '</span>');
 3017:                     } else {
 3018:                         # mark the file as read only
 3019:                         push(@handedback,$save_file_name);
 3020: 			if (exists($$newrecord{"resource.$new_part.$resp_id.handback"})) {
 3021: 			    $$newrecord{"resource.$new_part.$resp_id.handback"}.=',';
 3022: 			}
 3023:                         $$newrecord{"resource.$new_part.$resp_id.handback"} .= $save_file_name;
 3024: 			$file_msg.='<span class="LC_filename"><a href="/uploaded/'."$domain/$stuname/".$save_file_name.'">'.$save_file_name."</a></span> <br />";
 3025: 
 3026:                     }
 3027:                     $request->print('<br />'.&mt('[_1] will be the uploaded file name [_2]','<span class="LC_info">'.$fname.'</span>','<span class="LC_filename">'.$env{'form.'.$newflg.'_'.$part_resp.'_origdoc'.$counter}.'</span>'));
 3028:                 }
 3029:             }
 3030:         }
 3031:     }
 3032:     if (@handedback > 0) {
 3033:         $request->print('<br />');
 3034:         my @what = ($symb,$env{'request.course.id'},'handback');
 3035:         &Apache::lonnet::mark_as_readonly($domain,$stuname,\@handedback,\@what);
 3036:         my $user_lh = &Apache::loncommon::user_lang($stuname,$domain,$env{'request.course.id'});
 3037:         my ($subject,$message);
 3038:         if (scalar(@handedback) == 1) {
 3039:             $subject = &mt_user($user_lh,'File Handed Back by Instructor');
 3040:         } else {
 3041:             $subject = &mt_user($user_lh,'Files Handed Back by Instructor');
 3042:             $message = &mt_user($user_lh,'Files have been returned that were originally submitted in response to: ');
 3043:         }
 3044:         $message .= "<p><strong>".&Apache::lonnet::gettitle($symb)." </strong></p>";
 3045:         $message .= &mt_user($user_lh,'The returned file(s) are named: [_1]',"<br />$file_msg <br />").
 3046:                     &mt_user($user_lh,'The file(s) can be found in your [_1]portfolio[_2].','<a href="/adm/portfolio">','</a>');
 3047:         my ($feedurl,$showsymb) =
 3048:             &get_feedurl_and_symb($symb,$domain,$stuname);
 3049:         my $restitle = &Apache::lonnet::gettitle($symb);
 3050:         $subject .= ' '.&mt_user($user_lh,'(File Returned)').' ['.$restitle.']';
 3051:         my $msgstatus =
 3052:              &Apache::lonmsg::user_normal_msg($stuname,$domain,$subject,
 3053:                  $message,undef,$feedurl,undef,undef,undef,$showsymb,
 3054:                  $restitle);
 3055:         if ($msgstatus) {
 3056:             $request->print(&mt('Notification message status: [_1]','<span class="LC_info">'.$msgstatus.'</span>').'<br />');
 3057:         }
 3058:     }
 3059:     return;
 3060: }
 3061: 
 3062: sub get_feedurl_and_symb {
 3063:     my ($symb,$uname,$udom) = @_;
 3064:     my (undef,undef,$url) = &Apache::lonnet::decode_symb($symb);
 3065:     $url = &Apache::lonnet::clutter($url);
 3066:     my $encrypturl=&Apache::lonnet::EXT('resource.0.encrypturl',
 3067: 					$symb,$udom,$uname);
 3068:     if ($encrypturl =~ /^yes$/i) {
 3069: 	&Apache::lonenc::encrypted(\$url,1);
 3070: 	&Apache::lonenc::encrypted(\$symb,1);
 3071:     }
 3072:     return ($url,$symb);
 3073: }
 3074: 
 3075: sub get_submitted_files {
 3076:     my ($udom,$uname,$partid,$respid,$record) = @_;
 3077:     my @files;
 3078:     if ($$record{"resource.$partid.$respid.portfiles"}) {
 3079:         my $file_url = '/uploaded/'.$udom.'/'.$uname.'/portfolio';
 3080:         foreach my $file (split(',',$$record{"resource.$partid.$respid.portfiles"})) {
 3081:     	    push(@files,$file_url.$file);
 3082:         }
 3083:     }
 3084:     if ($$record{"resource.$partid.$respid.uploadedurl"}) {
 3085:         push(@files,$$record{"resource.$partid.$respid.uploadedurl"});
 3086:     }
 3087:     return (\@files);
 3088: }
 3089: 
 3090: # ----------- Provides number of tries since last reset.
 3091: sub get_num_tries {
 3092:     my ($record,$last_reset,$part) = @_;
 3093:     my $timestamp = '';
 3094:     my $num_tries = 0;
 3095:     if ($$record{'version'}) {
 3096:         for (my $version=$$record{'version'};$version>=1;$version--) {
 3097:             if (exists($$record{$version.':resource.'.$part.'.solved'})) {
 3098:                 $timestamp = $$record{$version.':timestamp'};
 3099:                 if ($timestamp > $last_reset) {
 3100:                     $num_tries ++;
 3101:                 } else {
 3102:                     last;
 3103:                 }
 3104:             }
 3105:         }
 3106:     }
 3107:     return $num_tries;
 3108: }
 3109: 
 3110: # ----------- Determine decrements required in aggregate totals 
 3111: sub decrement_aggs {
 3112:     my ($symb,$part,$aggregate,$aggtries,$totaltries,$solvedstatus) = @_;
 3113:     my %decrement = (
 3114:                         attempts => 0,
 3115:                         users => 0,
 3116:                         correct => 0
 3117:                     );
 3118:     $decrement{'attempts'} = $aggtries;
 3119:     if ($solvedstatus =~ /^correct/) {
 3120:         $decrement{'correct'} = 1;
 3121:     }
 3122:     if ($aggtries == $totaltries) {
 3123:         $decrement{'users'} = 1;
 3124:     }
 3125:     foreach my $type (keys(%decrement)) {
 3126:         $$aggregate{$symb."\0".$part."\0".$type} = -$decrement{$type};
 3127:     }
 3128:     return;
 3129: }
 3130: 
 3131: # ----------- Determine timestamps for last reset of aggregate totals for parts  
 3132: sub get_last_resets {
 3133:     my ($symb,$courseid,$partids) =@_;
 3134:     my %last_resets;
 3135:     my $cdom = $env{'course.'.$courseid.'.domain'};
 3136:     my $cname = $env{'course.'.$courseid.'.num'};
 3137:     my @keys;
 3138:     foreach my $part (@{$partids}) {
 3139: 	push(@keys,"$symb\0$part\0resettime");
 3140:     }
 3141:     my %results=&Apache::lonnet::get('nohist_resourcetracker',\@keys,
 3142: 				     $cdom,$cname);
 3143:     foreach my $part (@{$partids}) {
 3144: 	$last_resets{$part}=$results{"$symb\0$part\0resettime"};
 3145:     }
 3146:     return %last_resets;
 3147: }
 3148: 
 3149: # ----------- Handles creating versions for portfolio files as answers
 3150: sub version_portfiles {
 3151:     my ($record, $parts_graded, $courseid, $symb, $domain, $stu_name, $v_flag) = @_;
 3152:     my $version_parts = join('|',@$v_flag);
 3153:     my @returned_keys;
 3154:     my $parts = join('|', @$parts_graded);
 3155:     my $portfolio_root = '/userfiles/portfolio';
 3156:     foreach my $key (keys(%$record)) {
 3157:         my $new_portfiles;
 3158:         if ($key =~ /^resource\.($version_parts)\./ && $key =~ /\.portfiles$/ ) {
 3159:             my @versioned_portfiles;
 3160:             my @portfiles = split(/\s*,\s*/,$$record{$key});
 3161:             foreach my $file (@portfiles) {
 3162:                 &Apache::lonnet::unmark_as_readonly($domain,$stu_name,[$symb,$env{'request.course.id'}],$file);
 3163:                 my ($directory,$answer_file) =($file =~ /^(.*?)([^\/]*)$/);
 3164: 		my ($answer_name,$answer_ver,$answer_ext) =
 3165: 		    &file_name_version_ext($answer_file);
 3166:                 my $getpropath = 1;    
 3167:                 my @dir_list = &Apache::lonnet::dirlist($portfolio_root.$directory,$domain,$stu_name,$getpropath);
 3168:                 my $version = &get_next_version($answer_name, $answer_ext, \@dir_list);
 3169:                 my $new_answer = &version_selected_portfile($domain, $stu_name, $directory, $answer_file, $version);
 3170:                 if ($new_answer ne 'problem getting file') {
 3171:                     push(@versioned_portfiles, $directory.$new_answer);
 3172:                     &Apache::lonnet::mark_as_readonly($domain,$stu_name,
 3173:                         [$directory.$new_answer],
 3174:                         [$symb,$env{'request.course.id'},'graded']);
 3175:                 }
 3176:             }
 3177:             $$record{$key} = join(',',@versioned_portfiles);
 3178:             push(@returned_keys,$key);
 3179:         }
 3180:     } 
 3181:     return (@returned_keys);   
 3182: }
 3183: 
 3184: sub get_next_version {
 3185:     my ($answer_name, $answer_ext, $dir_list) = @_;
 3186:     my $version;
 3187:     foreach my $row (@$dir_list) {
 3188:         my ($file) = split(/\&/,$row,2);
 3189:         my ($file_name,$file_version,$file_ext) =
 3190: 	    &file_name_version_ext($file);
 3191:         if (($file_name eq $answer_name) && 
 3192: 	    ($file_ext eq $answer_ext)) {
 3193:                 # gets here if filename and extension match, regardless of version
 3194:                 if ($file_version ne '') {
 3195:                 # a versioned file is found  so save it for later
 3196:                 if ($file_version > $version) {
 3197: 		    $version = $file_version;
 3198: 	        }
 3199:             }
 3200:         }
 3201:     } 
 3202:     $version ++;
 3203:     return($version);
 3204: }
 3205: 
 3206: sub version_selected_portfile {
 3207:     my ($domain,$stu_name,$directory,$file_name,$version) = @_;
 3208:     my ($answer_name,$answer_ver,$answer_ext) =
 3209:         &file_name_version_ext($file_name);
 3210:     my $new_answer;
 3211:     $env{'form.copy'} = &Apache::lonnet::getfile("/uploaded/$domain/$stu_name/portfolio$directory$file_name");
 3212:     if($env{'form.copy'} eq '-1') {
 3213:         $new_answer = 'problem getting file';
 3214:     } else {
 3215:         $new_answer = $answer_name.'.'.$version.'.'.$answer_ext;
 3216:         my $copy_result = &Apache::lonnet::finishuserfileupload(
 3217:                             $stu_name,$domain,'copy',
 3218: 		        '/portfolio'.$directory.$new_answer);
 3219:     }    
 3220:     return ($new_answer);
 3221: }
 3222: 
 3223: sub file_name_version_ext {
 3224:     my ($file)=@_;
 3225:     my @file_parts = split(/\./, $file);
 3226:     my ($name,$version,$ext);
 3227:     if (@file_parts > 1) {
 3228: 	$ext=pop(@file_parts);
 3229: 	if (@file_parts > 1 && $file_parts[-1] =~ /^\d+$/) {
 3230: 	    $version=pop(@file_parts);
 3231: 	}
 3232: 	$name=join('.',@file_parts);
 3233:     } else {
 3234: 	$name=join('.',@file_parts);
 3235:     }
 3236:     return($name,$version,$ext);
 3237: }
 3238: 
 3239: #--------------------------------------------------------------------------------------
 3240: #
 3241: #-------------------------- Next few routines handles grading by section or whole class
 3242: #
 3243: #--- Javascript to handle grading by section or whole class
 3244: sub viewgrades_js {
 3245:     my ($request) = shift;
 3246: 
 3247:     my $alertmsg = &mt('A number equal or greater than 0 is expected. Entered value = ');
 3248:     $request->print(<<VIEWJAVASCRIPT);
 3249: <script type="text/javascript" language="javascript">
 3250:    function writePoint(partid,weight,point) {
 3251: 	var radioButton = document.classgrade["RADVAL_"+partid];
 3252: 	var textbox = document.classgrade["TEXTVAL_"+partid];
 3253: 	if (point == "textval") {
 3254: 	    point = document.classgrade["TEXTVAL_"+partid].value;
 3255: 	    if (isNaN(point) || parseFloat(point) < 0) {
 3256: 		alert("$alertmsg"+parseFloat(point));
 3257: 		var resetbox = false;
 3258: 		for (var i=0; i<radioButton.length; i++) {
 3259: 		    if (radioButton[i].checked) {
 3260: 			textbox.value = i;
 3261: 			resetbox = true;
 3262: 		    }
 3263: 		}
 3264: 		if (!resetbox) {
 3265: 		    textbox.value = "";
 3266: 		}
 3267: 		return;
 3268: 	    }
 3269: 	    if (parseFloat(point) > parseFloat(weight)) {
 3270: 		var resp = confirm("You entered a value ("+parseFloat(point)+
 3271: 				   ") greater than the weight for the part. Accept?");
 3272: 		if (resp == false) {
 3273: 		    textbox.value = "";
 3274: 		    return;
 3275: 		}
 3276: 	    }
 3277: 	    for (var i=0; i<radioButton.length; i++) {
 3278: 		radioButton[i].checked=false;
 3279: 		if (parseFloat(point) == i) {
 3280: 		    radioButton[i].checked=true;
 3281: 		}
 3282: 	    }
 3283: 
 3284: 	} else {
 3285: 	    textbox.value = parseFloat(point);
 3286: 	}
 3287: 	for (i=0;i<document.classgrade.total.value;i++) {
 3288: 	    var user = document.classgrade["ctr"+i].value;
 3289: 	    user = user.replace(new RegExp(':', 'g'),"_");
 3290: 	    var scorename = document.classgrade["GD_"+user+"_"+partid+"_awarded"];
 3291: 	    var saveval   = document.classgrade["GD_"+user+"_"+partid+"_solved_s"].value;
 3292: 	    var selname   = document.classgrade["GD_"+user+"_"+partid+"_solved"];
 3293: 	    if (saveval != "correct") {
 3294: 		scorename.value = point;
 3295: 		if (selname[0].selected != true) {
 3296: 		    selname[0].selected = true;
 3297: 		}
 3298: 	    }
 3299: 	}
 3300: 	document.classgrade["SELVAL_"+partid][0].selected = true;
 3301:     }
 3302: 
 3303:     function writeRadText(partid,weight) {
 3304: 	var selval   = document.classgrade["SELVAL_"+partid];
 3305: 	var radioButton = document.classgrade["RADVAL_"+partid];
 3306:         var override = document.classgrade["FORCE_"+partid].checked;
 3307: 	var textbox = document.classgrade["TEXTVAL_"+partid];
 3308: 	if (selval[1].selected || selval[2].selected) {
 3309: 	    for (var i=0; i<radioButton.length; i++) {
 3310: 		radioButton[i].checked=false;
 3311: 
 3312: 	    }
 3313: 	    textbox.value = "";
 3314: 
 3315: 	    for (i=0;i<document.classgrade.total.value;i++) {
 3316: 		var user = document.classgrade["ctr"+i].value;
 3317: 		user = user.replace(new RegExp(':', 'g'),"_");
 3318: 		var scorename = document.classgrade["GD_"+user+"_"+partid+"_awarded"];
 3319: 		var saveval   = document.classgrade["GD_"+user+"_"+partid+"_solved_s"].value;
 3320: 		var selname   = document.classgrade["GD_"+user+"_"+partid+"_solved"];
 3321: 		if ((saveval != "correct") || override) {
 3322: 		    scorename.value = "";
 3323: 		    if (selval[1].selected) {
 3324: 			selname[1].selected = true;
 3325: 		    } else {
 3326: 			selname[2].selected = true;
 3327: 			if (Number(document.classgrade["GD_"+user+"_"+partid+"_tries"].value)) 
 3328: 			{document.classgrade["GD_"+user+"_"+partid+"_tries"].value = '0';}
 3329: 		    }
 3330: 		}
 3331: 	    }
 3332: 	} else {
 3333: 	    for (i=0;i<document.classgrade.total.value;i++) {
 3334: 		var user = document.classgrade["ctr"+i].value;
 3335: 		user = user.replace(new RegExp(':', 'g'),"_");
 3336: 		var scorename = document.classgrade["GD_"+user+"_"+partid+"_awarded"];
 3337: 		var saveval   = document.classgrade["GD_"+user+"_"+partid+"_solved_s"].value;
 3338: 		var selname   = document.classgrade["GD_"+user+"_"+partid+"_solved"];
 3339: 		if ((saveval != "correct") || override) {
 3340: 		    scorename.value = document.classgrade["GD_"+user+"_"+partid+"_awarded_s"].value;
 3341: 		    selname[0].selected = true;
 3342: 		}
 3343: 	    }
 3344: 	}	    
 3345:     }
 3346: 
 3347:     function changeSelect(partid,user) {
 3348: 	var selval = document.classgrade["GD_"+user+'_'+partid+"_solved"];
 3349: 	var textbox = document.classgrade["GD_"+user+'_'+partid+"_awarded"];
 3350: 	var point  = textbox.value;
 3351: 	var weight = document.classgrade["weight_"+partid].value;
 3352: 
 3353: 	if (isNaN(point) || parseFloat(point) < 0) {
 3354: 	    alert("$alertmsg"+parseFloat(point));
 3355: 	    textbox.value = "";
 3356: 	    return;
 3357: 	}
 3358: 	if (parseFloat(point) > parseFloat(weight)) {
 3359: 	    var resp = confirm("You entered a value ("+parseFloat(point)+
 3360: 			       ") greater than the weight of the part. Accept?");
 3361: 	    if (resp == false) {
 3362: 		textbox.value = "";
 3363: 		return;
 3364: 	    }
 3365: 	}
 3366: 	selval[0].selected = true;
 3367:     }
 3368: 
 3369:     function changeOneScore(partid,user) {
 3370: 	var selval = document.classgrade["GD_"+user+'_'+partid+"_solved"];
 3371: 	if (selval[1].selected || selval[2].selected) {
 3372: 	    document.classgrade["GD_"+user+'_'+partid+"_awarded"].value = "";
 3373: 	    if (selval[2].selected) {
 3374: 		document.classgrade["GD_"+user+'_'+partid+"_tries"].value = "0";
 3375: 	    }
 3376:         }
 3377:     }
 3378: 
 3379:     function resetEntry(numpart) {
 3380: 	for (ctpart=0;ctpart<numpart;ctpart++) {
 3381: 	    var partid = document.classgrade["partid_"+ctpart].value;
 3382: 	    var radioButton = document.classgrade["RADVAL_"+partid];
 3383: 	    var textbox = document.classgrade["TEXTVAL_"+partid];
 3384: 	    var selval  = document.classgrade["SELVAL_"+partid];
 3385: 	    for (var i=0; i<radioButton.length; i++) {
 3386: 		radioButton[i].checked=false;
 3387: 
 3388: 	    }
 3389: 	    textbox.value = "";
 3390: 	    selval[0].selected = true;
 3391: 
 3392: 	    for (i=0;i<document.classgrade.total.value;i++) {
 3393: 		var user = document.classgrade["ctr"+i].value;
 3394: 		user = user.replace(new RegExp(':', 'g'),"_");
 3395: 		var resetscore = document.classgrade["GD_"+user+"_"+partid+"_awarded"];
 3396: 		resetscore.value = document.classgrade["GD_"+user+"_"+partid+"_awarded_s"].value;
 3397: 		var resettries = document.classgrade["GD_"+user+"_"+partid+"_tries"];
 3398: 		resettries.value = document.classgrade["GD_"+user+"_"+partid+"_tries_s"].value;
 3399: 		var saveselval   = document.classgrade["GD_"+user+"_"+partid+"_solved_s"].value;
 3400: 		var selname   = document.classgrade["GD_"+user+"_"+partid+"_solved"];
 3401: 		if (saveselval == "excused") {
 3402: 		    if (selname[1].selected == false) { selname[1].selected = true;}
 3403: 		} else {
 3404: 		    if (selname[0].selected == false) {selname[0].selected = true};
 3405: 		}
 3406: 	    }
 3407: 	}
 3408:     }
 3409: 
 3410: </script>
 3411: VIEWJAVASCRIPT
 3412: }
 3413: 
 3414: #--- show scores for a section or whole class w/ option to change/update a score
 3415: sub viewgrades {
 3416:     my ($request) = shift;
 3417:     &viewgrades_js($request);
 3418: 
 3419:     my ($symb) = &get_symb($request);
 3420:     #need to make sure we have the correct data for later EXT calls, 
 3421:     #thus invalidate the cache
 3422:     &Apache::lonnet::devalidatecourseresdata(
 3423:                  $env{'course.'.$env{'request.course.id'}.'.num'},
 3424:                  $env{'course.'.$env{'request.course.id'}.'.domain'});
 3425:     &Apache::lonnet::clear_EXT_cache_status();
 3426: 
 3427:     my $result='<h3><span class="LC_info">'.&mt('Manual Grading').'</span></h3>';
 3428:     $result.='<h4>'.&mt('<b>Current Resource: </b>[_1]',$env{'form.probTitle'}).'</h4>'."\n";
 3429: 
 3430:     #view individual student submission form - called using Javascript viewOneStudent
 3431:     $result.=&jscriptNform($symb);
 3432: 
 3433:     #beginning of class grading form
 3434:     my $stu_status = join(':',&Apache::loncommon::get_env_multiple('form.Status'));
 3435:     $result.= '<form action="/adm/grades" method="post" name="classgrade">'."\n".
 3436: 	'<input type="hidden" name="symb"    value="'.&Apache::lonenc::check_encrypt($symb).'" />'."\n".
 3437: 	'<input type="hidden" name="command" value="editgrades" />'."\n".
 3438: 	&build_section_inputs().
 3439: 	'<input type="hidden" name="saveState" value="'.$env{'form.saveState'}.'" />'."\n".
 3440: 	'<input type="hidden" name="Status" value="'.$env{'stu_status'}.'" />'."\n".
 3441: 	'<input type="hidden" name="probTitle" value="'.$env{'form.probTitle'}.'" />'."\n";
 3442: 
 3443:     my ($common_header,$specific_header);
 3444:     if ($env{'form.section'} eq 'all') {
 3445: 	$common_header = &mt('Assign Common Grade to Class');
 3446:         $specific_header = &mt('Assign Grade to Specific Students in Class');
 3447:     } elsif ($env{'form.section'} eq 'none') {
 3448:         $common_header = &mt('Assign Common Grade to Students in no Section');
 3449: 	$specific_header = &mt('Assign Grade to Specific Students in no Section');
 3450:     } else {
 3451:         my $section_display = join (", ",&Apache::loncommon::get_env_multiple('form.section'));
 3452:         $common_header = &mt('Assign Common Grade to Students in Section(s) [_1]',$section_display);
 3453: 	$specific_header = &mt('Assign Grade to Specific Students in Section(s) [_1]',$section_display);
 3454:     }
 3455:     $result.= '<h3>'.$common_header.'</h3>'.&Apache::loncommon::start_data_table();
 3456:     #radio buttons/text box for assigning points for a section or class.
 3457:     #handles different parts of a problem
 3458:     my $res_error;
 3459:     my ($partlist,$handgrade,$responseType) = &response_type($symb,\$res_error);
 3460:     if ($res_error) {
 3461:         return &navmap_errormsg();
 3462:     }
 3463:     my %weight = ();
 3464:     my $ctsparts = 0;
 3465:     my %seen = ();
 3466:     my @part_response_id = &flatten_responseType($responseType);
 3467:     foreach my $part_response_id (@part_response_id) {
 3468:     	my ($partid,$respid) = @{ $part_response_id };
 3469: 	my $part_resp = join('_',@{ $part_response_id });
 3470: 	next if $seen{$partid};
 3471: 	$seen{$partid}++;
 3472: 	my $handgrade=$$handgrade{$part_resp};
 3473: 	my $wgt = &Apache::lonnet::EXT('resource.'.$partid.'.weight',$symb);
 3474: 	$weight{$partid} = $wgt eq '' ? '1' : $wgt;
 3475: 
 3476: 	my $display_part=&get_display_part($partid,$symb);
 3477: 	my $radio.='<table border="0"><tr>';  
 3478: 	my $ctr = 0;
 3479: 	while ($ctr<=$weight{$partid}) { # display radio buttons in a nice table 10 across
 3480: 	    $radio.= '<td><label><input type="radio" name="RADVAL_'.$partid.'" '.
 3481: 		'onclick="javascript:writePoint(\''.$partid.'\','.$weight{$partid}.
 3482: 		','.$ctr.')" />'.$ctr."</label></td>\n";
 3483: 	    $result.=(($ctr+1)%10 == 0 ? '</tr><tr>' : '');
 3484: 	    $ctr++;
 3485: 	}
 3486: 	$radio.='</tr></table>';
 3487: 	my $line = '<input type="text" name="TEXTVAL_'.
 3488: 	    $partid.'" size="4" '.'onchange="javascript:writePoint(\''.
 3489: 		$partid.'\','.$weight{$partid}.',\'textval\')" /> /'.
 3490: 	    $weight{$partid}.' '.&mt('(problem weight)').'</td>'."\n";
 3491: 	$line.= '<td><b>'.&mt('Grade Status').':</b><select name="SELVAL_'.$partid.'"'.
 3492: 	    'onchange="javascript:writeRadText(\''.$partid.'\','.
 3493: 		$weight{$partid}.')"> '.
 3494: 	    '<option selected="selected"> </option>'.
 3495: 	    '<option value="excused">'.&mt('excused').'</option>'.
 3496: 	    '<option value="reset status">'.&mt('reset status').'</option>'.
 3497: 	    '</select></td>'.
 3498:             '<td><label><input type="checkbox" name="FORCE_'.$partid.'" />'.&mt('Override "Correct"').'</label>';
 3499: 	$line.='<input type="hidden" name="partid_'.
 3500: 	    $ctsparts.'" value="'.$partid.'" />'."\n";
 3501: 	$line.='<input type="hidden" name="weight_'.
 3502: 	    $partid.'" value="'.$weight{$partid}.'" />'."\n";
 3503: 
 3504: 	$result.=
 3505: 	    &Apache::loncommon::start_data_table_row()."\n".
 3506: 	    '<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>'.
 3507: 	    &Apache::loncommon::end_data_table_row()."\n";
 3508: 	$ctsparts++;
 3509:     }
 3510:     $result.=&Apache::loncommon::end_data_table()."\n".
 3511: 	'<input type="hidden" name="totalparts" value="'.$ctsparts.'" />';
 3512:     $result.='<input type="button" value="'.&mt('Revert to Default').'" '.
 3513: 	'onclick="javascript:resetEntry('.$ctsparts.');" />';
 3514: 
 3515:     #table listing all the students in a section/class
 3516:     #header of table
 3517:     $result.= '<h3>'.$specific_header.'</h3>'.
 3518:               &Apache::loncommon::start_data_table().
 3519: 	      &Apache::loncommon::start_data_table_header_row().
 3520: 	      '<th>'.&mt('No.').'</th>'.
 3521: 	      '<th>'.&nameUserString('header')."</th>\n";
 3522:     my $partserror;
 3523:     my (@parts) = sort(&getpartlist($symb,\$partserror));
 3524:     if ($partserror) {
 3525:         return &navmap_errormsg();
 3526:     }
 3527:     my (undef,undef,$url)=&Apache::lonnet::decode_symb($symb);
 3528:     my @partids = ();
 3529:     foreach my $part (@parts) {
 3530: 	my $display=&Apache::lonnet::metadata($url,$part.'.display');
 3531:         my $narrowtext = &mt('Tries');
 3532: 	$display =~ s|^Number of Attempts|$narrowtext <br />|; # makes the column narrower
 3533: 	if  (!$display) { $display = &Apache::lonnet::metadata($url,$part.'.name'); }
 3534: 	my ($partid) = &split_part_type($part);
 3535:         push(@partids,$partid);
 3536: 	my $display_part=&get_display_part($partid,$symb);
 3537: 	if ($display =~ /^Partial Credit Factor/) {
 3538: 	    $result.='<th>'.
 3539: 		&mt('Score Part: [_1]<br /> (weight = [_2])',
 3540: 		    $display_part,$weight{$partid}).'</th>'."\n";
 3541: 	    next;
 3542: 	    
 3543: 	} else {
 3544: 	    if ($display =~ /Problem Status/) {
 3545: 		my $grade_status_mt = &mt('Grade Status');
 3546: 		$display =~ s{Problem Status}{$grade_status_mt<br />};
 3547: 	    }
 3548: 	    my $part_mt = &mt('Part:');
 3549: 	    $display =~s{\[Part: \Q$partid\E\]}{$part_mt $display_part};
 3550: 	}
 3551: 
 3552: 	$result.='<th>'.$display.'</th>'."\n";
 3553:     }
 3554:     $result.=&Apache::loncommon::end_data_table_header_row();
 3555: 
 3556:     my %last_resets = 
 3557: 	&get_last_resets($symb,$env{'request.course.id'},\@partids);
 3558: 
 3559:     #get info for each student
 3560:     #list all the students - with points and grade status
 3561:     my (undef,undef,$fullname) = &getclasslist($env{'form.section'},'1');
 3562:     my $ctr = 0;
 3563:     foreach (sort 
 3564: 	     {
 3565: 		 if (lc($$fullname{$a}) ne lc($$fullname{$b})) {
 3566: 		     return (lc($$fullname{$a}) cmp lc($$fullname{$b}));
 3567: 		 }
 3568: 		 return $a cmp $b;
 3569: 	     } (keys(%$fullname))) {
 3570: 	$ctr++;
 3571: 	$result.=&viewstudentgrade($symb,$env{'request.course.id'},
 3572: 				   $_,$$fullname{$_},\@parts,\%weight,$ctr,\%last_resets);
 3573:     }
 3574:     $result.=&Apache::loncommon::end_data_table();
 3575:     $result.='<input type="hidden" name="total" value="'.$ctr.'" />'."\n";
 3576:     $result.='<input type="button" value="'.&mt('Save').'" '.
 3577: 	'onclick="javascript:submit();" target="_self" /></form>'."\n";
 3578:     if (scalar(%$fullname) eq 0) {
 3579: 	my $colspan=3+scalar(@parts);
 3580: 	my $section_display = join (", ",&Apache::loncommon::get_env_multiple('form.section'));
 3581:         my $stu_status = join(' or ',&Apache::loncommon::get_env_multiple('form.Status'));
 3582: 	$result='<span class="LC_warning">'.
 3583: 	    &mt('There are no students in section(s) [_1] with enrollment status [_2] to modify or grade.',
 3584: 	        $section_display, $stu_status).
 3585: 	    '</span>';
 3586:     }
 3587:     $result.=&show_grading_menu_form($symb);
 3588:     return $result;
 3589: }
 3590: 
 3591: #--- call by previous routine to display each student
 3592: sub viewstudentgrade {
 3593:     my ($symb,$courseid,$student,$fullname,$parts,$weight,$ctr,$last_resets) = @_;
 3594:     my ($uname,$udom) = split(/:/,$student);
 3595:     my %record=&Apache::lonnet::restore($symb,$courseid,$udom,$uname);
 3596:     my %aggregates = (); 
 3597:     my $result=&Apache::loncommon::start_data_table_row().'<td align="right">'.
 3598: 	'<input type="hidden" name="ctr'.($ctr-1).'" value="'.$student.'" />'.
 3599: 	"\n".$ctr.'&nbsp;</td><td>&nbsp;'.
 3600: 	'<a href="javascript:viewOneStudent(\''.$uname.'\',\''.$udom.
 3601: 	'\');" target="_self">'.$fullname.'</a> '.
 3602: 	'<span class="LC_internal_info">('.$uname.($env{'user.domain'} eq $udom ? '' : ':'.$udom).')</span></td>'."\n";
 3603:     $student=~s/:/_/; # colon doen't work in javascript for names
 3604:     foreach my $apart (@$parts) {
 3605: 	my ($part,$type) = &split_part_type($apart);
 3606: 	my $score=$record{"resource.$part.$type"};
 3607:         $result.='<td align="center">';
 3608:         my ($aggtries,$totaltries);
 3609:         unless (exists($aggregates{$part})) {
 3610: 	    $totaltries = $record{'resource.'.$part.'.tries'};
 3611: 
 3612: 	    $aggtries = $totaltries;
 3613:             if ($$last_resets{$part}) {  
 3614:                 $aggtries = &get_num_tries(\%record,$$last_resets{$part},
 3615: 					   $part);
 3616:             }
 3617:             $result.='<input type="hidden" name="'.
 3618:                 'GD_'.$student.'_'.$part.'_aggtries" value="'.$aggtries.'" />'."\n";
 3619:             $result.='<input type="hidden" name="'.
 3620:                 'GD_'.$student.'_'.$part.'_totaltries" value="'.$totaltries.'" />'."\n";
 3621:             $aggregates{$part} = 1;
 3622:         }
 3623: 	if ($type eq 'awarded') {
 3624: 	    my $pts = $score eq '' ? '' : &compute_points($score,$$weight{$part});
 3625: 	    $result.='<input type="hidden" name="'.
 3626: 		'GD_'.$student.'_'.$part.'_awarded_s" value="'.$pts.'" />'."\n";
 3627: 	    $result.='<input type="text" name="'.
 3628: 		'GD_'.$student.'_'.$part.'_awarded" '.
 3629:                 'onchange="javascript:changeSelect(\''.$part.'\',\''.$student.
 3630: 		'\')" value="'.$pts.'" size="4" /></td>'."\n";
 3631: 	} elsif ($type eq 'solved') {
 3632: 	    my ($status,$foo)=split(/_/,$score,2);
 3633: 	    $status = 'nothing' if ($status eq '');
 3634: 	    $result.='<input type="hidden" name="'.'GD_'.$student.'_'.
 3635: 		$part.'_solved_s" value="'.$status.'" />'."\n";
 3636: 	    $result.='&nbsp;<select name="'.
 3637: 		'GD_'.$student.'_'.$part.'_solved" '.
 3638:                 'onchange="javascript:changeOneScore(\''.$part.'\',\''.$student.'\')" >'."\n";
 3639: 	    $result.= (($status eq 'excused') ? '<option> </option><option selected="selected" value="excused">'.&mt('excused').'</option>' 
 3640: 		: '<option selected="selected"> </option><option value="excused">'.&mt('excused').'</option>')."\n";
 3641: 	    $result.='<option value="reset status">'.&mt('reset status').'</option>';
 3642: 	    $result.="</select>&nbsp;</td>\n";
 3643: 	} else {
 3644: 	    $result.='<input type="hidden" name="'.
 3645: 		'GD_'.$student.'_'.$part.'_'.$type.'_s" value="'.$score.'" />'.
 3646: 		    "\n";
 3647: 	    $result.='<input type="text" name="'.
 3648: 		'GD_'.$student.'_'.$part.'_'.$type.'" '.
 3649: 		'value="'.$score.'" size="4" /></td>'."\n";
 3650: 	}
 3651:     }
 3652:     $result.=&Apache::loncommon::end_data_table_row();
 3653:     return $result;
 3654: }
 3655: 
 3656: #--- change scores for all the students in a section/class
 3657: #    record does not get update if unchanged
 3658: sub editgrades {
 3659:     my ($request) = @_;
 3660: 
 3661:     my $symb=&get_symb($request);
 3662:     my $section_display = join (", ",&Apache::loncommon::get_env_multiple('form.section'));
 3663:     my $title='<h2>'.&mt('Current Grade Status').'</h2>';
 3664:     $title.='<h4>'.&mt('<b>Current Resource: </b>[_1]',$env{'form.probTitle'}).'</h4>'."\n";
 3665:     $title.='<h4>'.&mt('<b>Section: </b>[_1]',$section_display).'</h4>'."\n";
 3666: 
 3667:     my $result= &Apache::loncommon::start_data_table().
 3668: 	&Apache::loncommon::start_data_table_header_row().
 3669: 	'<th rowspan="2" valign="middle">'.&mt('No.').'</th>'.
 3670: 	'<th rowspan="2" valign="middle">'.&nameUserString('header')."</th>\n";
 3671:     my %scoreptr = (
 3672: 		    'correct'  =>'correct_by_override',
 3673: 		    'incorrect'=>'incorrect_by_override',
 3674: 		    'excused'  =>'excused',
 3675: 		    'ungraded' =>'ungraded_attempted',
 3676:                     'credited' =>'credit_attempted',
 3677: 		    'nothing'  => '',
 3678: 		    );
 3679:     my ($classlist,undef,$fullname) = &getclasslist($env{'form.section'},'0');
 3680: 
 3681:     my (@partid);
 3682:     my %weight = ();
 3683:     my %columns = ();
 3684:     my ($i,$ctr,$count,$rec_update) = (0,0,0,0);
 3685: 
 3686:     my $partserror;
 3687:     my (@parts) = sort(&getpartlist($symb,\$partserror));
 3688:     if ($partserror) {
 3689:         return &navmap_errormsg();
 3690:     }
 3691:     my $header;
 3692:     while ($ctr < $env{'form.totalparts'}) {
 3693: 	my $partid = $env{'form.partid_'.$ctr};
 3694: 	push(@partid,$partid);
 3695: 	$weight{$partid} = $env{'form.weight_'.$partid};
 3696: 	$ctr++;
 3697:     }
 3698:     my (undef,undef,$url) = &Apache::lonnet::decode_symb($symb);
 3699:     foreach my $partid (@partid) {
 3700: 	$header .= '<th align="center">'.&mt('Old Score').'</th>'.
 3701: 	    '<th align="center">'.&mt('New Score').'</th>';
 3702: 	$columns{$partid}=2;
 3703: 	foreach my $stores (@parts) {
 3704: 	    my ($part,$type) = &split_part_type($stores);
 3705: 	    if ($part !~ m/^\Q$partid\E/) { next;}
 3706: 	    if ($type eq 'awarded' || $type eq 'solved') { next; }
 3707: 	    my $display=&Apache::lonnet::metadata($url,$stores.'.display');
 3708: 	    $display =~ s/\[Part: \Q$part\E\]//;
 3709:             my $narrowtext = &mt('Tries');
 3710: 	    $display =~ s/Number of Attempts/$narrowtext/;
 3711: 	    $header .= '<th align="center">'.&mt('Old').' '.$display.'</th>'.
 3712: 		'<th align="center">'.&mt('New').' '.$display.'</th>';
 3713: 	    $columns{$partid}+=2;
 3714: 	}
 3715:     }
 3716:     foreach my $partid (@partid) {
 3717: 	my $display_part=&get_display_part($partid,$symb);
 3718: 	$result .= '<th colspan="'.$columns{$partid}.'" align="center">'.
 3719: 	    &mt('Part: [_1] (Weight = [_2])',$display_part,$weight{$partid}).
 3720: 	    '</th>';
 3721: 
 3722:     }
 3723:     $result .= &Apache::loncommon::end_data_table_header_row().
 3724: 	&Apache::loncommon::start_data_table_header_row().
 3725: 	$header.
 3726: 	&Apache::loncommon::end_data_table_header_row();
 3727:     my @noupdate;
 3728:     my ($updateCtr,$noupdateCtr) = (1,1);
 3729:     for ($i=0; $i<$env{'form.total'}; $i++) {
 3730: 	my $line;
 3731: 	my $user = $env{'form.ctr'.$i};
 3732: 	my ($uname,$udom)=split(/:/,$user);
 3733: 	my %newrecord;
 3734: 	my $updateflag = 0;
 3735: 	$line .= '<td>'.&nameUserString(undef,$$fullname{$user},$uname,$udom).'</td>';
 3736: 	my $usec=$classlist->{"$uname:$udom"}[5];
 3737: 	if (!&canmodify($usec)) {
 3738: 	    my $numcols=scalar(@partid)*4+2;
 3739: 	    push(@noupdate,
 3740: 		 $line."<td colspan=\"$numcols\"><span class=\"LC_warning\">".
 3741: 		 &mt('Not allowed to modify student')."</span></td></tr>");
 3742: 	    next;
 3743: 	}
 3744:         my %aggregate = ();
 3745:         my $aggregateflag = 0;
 3746: 	$user=~s/:/_/; # colon doen't work in javascript for names
 3747: 	foreach (@partid) {
 3748: 	    my $old_aw    = $env{'form.GD_'.$user.'_'.$_.'_awarded_s'};
 3749: 	    my $old_part_pcr = $old_aw/($weight{$_} ne '0' ? $weight{$_}:1);
 3750: 	    my $old_part  = $old_aw eq '' ? '' : $old_part_pcr;
 3751: 	    my $old_score = $scoreptr{$env{'form.GD_'.$user.'_'.$_.'_solved_s'}};
 3752: 	    my $awarded   = $env{'form.GD_'.$user.'_'.$_.'_awarded'};
 3753: 	    my $pcr       = $awarded/($weight{$_} ne '0' ? $weight{$_} : 1);
 3754: 	    my $partial   = $awarded eq '' ? '' : $pcr;
 3755: 	    my $score;
 3756: 	    if ($partial eq '') {
 3757: 		$score = $scoreptr{$env{'form.GD_'.$user.'_'.$_.'_solved_s'}};
 3758: 	    } elsif ($partial > 0) {
 3759: 		$score = 'correct_by_override';
 3760: 	    } elsif ($partial == 0) {
 3761: 		$score = 'incorrect_by_override';
 3762: 	    }
 3763: 	    my $dropMenu = $env{'form.GD_'.$user.'_'.$_.'_solved'};
 3764: 	    $score = 'excused' if (($dropMenu eq 'excused') && ($score ne 'excused'));
 3765: 
 3766: 	    $newrecord{'resource.'.$_.'.regrader'}=
 3767: 		"$env{'user.name'}:$env{'user.domain'}";
 3768: 	    if ($dropMenu eq 'reset status' &&
 3769: 		$old_score ne '') { # ignore if no previous attempts => nothing to reset
 3770: 		$newrecord{'resource.'.$_.'.tries'} = '';
 3771: 		$newrecord{'resource.'.$_.'.solved'} = '';
 3772: 		$newrecord{'resource.'.$_.'.award'} = '';
 3773: 		$newrecord{'resource.'.$_.'.awarded'} = '';
 3774: 		$updateflag = 1;
 3775:                 if ($env{'form.GD_'.$user.'_'.$_.'_aggtries'} > 0) {
 3776:                     my $aggtries = $env{'form.GD_'.$user.'_'.$_.'_aggtries'};
 3777:                     my $totaltries = $env{'form.GD_'.$user.'_'.$_.'_totaltries'};
 3778:                     my $solvedstatus = $env{'form.GD_'.$user.'_'.$_.'_solved_s'};
 3779:                     &decrement_aggs($symb,$_,\%aggregate,$aggtries,$totaltries,$solvedstatus);
 3780:                     $aggregateflag = 1;
 3781:                 }
 3782: 	    } elsif (!($old_part eq $partial && $old_score eq $score)) {
 3783: 		$updateflag = 1;
 3784: 		$newrecord{'resource.'.$_.'.awarded'}  = $partial if $partial ne '';
 3785: 		$newrecord{'resource.'.$_.'.solved'}   = $score;
 3786: 		$rec_update++;
 3787: 	    }
 3788: 
 3789: 	    $line .= '<td align="center">'.$old_aw.'&nbsp;</td>'.
 3790: 		'<td align="center">'.$awarded.
 3791: 		($score eq 'excused' ? $score : '').'&nbsp;</td>';
 3792: 
 3793: 
 3794: 	    my $partid=$_;
 3795: 	    foreach my $stores (@parts) {
 3796: 		my ($part,$type) = &split_part_type($stores);
 3797: 		if ($part !~ m/^\Q$partid\E/) { next;}
 3798: 		if ($type eq 'awarded' || $type eq 'solved') { next; }
 3799: 		my $old_aw    = $env{'form.GD_'.$user.'_'.$part.'_'.$type.'_s'};
 3800: 		my $awarded   = $env{'form.GD_'.$user.'_'.$part.'_'.$type};
 3801: 		if ($awarded ne '' && $awarded ne $old_aw) {
 3802: 		    $newrecord{'resource.'.$part.'.'.$type}= $awarded;
 3803: 		    $newrecord{'resource.'.$part.'.regrader'}="$env{'user.name'}:$env{'user.domain'}";
 3804: 		    $updateflag=1;
 3805: 		}
 3806: 		$line .= '<td align="center">'.$old_aw.'&nbsp;</td>'.
 3807: 		    '<td align="center">'.$awarded.'&nbsp;</td>';
 3808: 	    }
 3809: 	}
 3810: 	$line.="\n";
 3811: 
 3812: 	my $cdom = $env{'course.'.$env{'request.course.id'}.'.domain'};
 3813: 	my $cnum = $env{'course.'.$env{'request.course.id'}.'.num'};
 3814: 
 3815: 	if ($updateflag) {
 3816: 	    $count++;
 3817: 	    &Apache::lonnet::cstore(\%newrecord,$symb,$env{'request.course.id'},
 3818: 				    $udom,$uname);
 3819: 
 3820: 	    if (&Apache::bridgetask::in_queue('gradingqueue',$symb,$cdom,
 3821: 					      $cnum,$udom,$uname)) {
 3822: 		# need to figure out if should be in queue.
 3823: 		my %record =  
 3824: 		    &Apache::lonnet::restore($symb,$env{'request.course.id'},
 3825: 					     $udom,$uname);
 3826: 		my $all_graded = 1;
 3827: 		my $none_graded = 1;
 3828: 		foreach my $part (@parts) {
 3829: 		    if ( $record{'resource.'.$part.'.awarded'} eq '' ) {
 3830: 			$all_graded = 0;
 3831: 		    } else {
 3832: 			$none_graded = 0;
 3833: 		    }
 3834: 		}
 3835: 
 3836: 		if ($all_graded || $none_graded) {
 3837: 		    &Apache::bridgetask::remove_from_queue('gradingqueue',
 3838: 							   $symb,$cdom,$cnum,
 3839: 							   $udom,$uname);
 3840: 		}
 3841: 	    }
 3842: 
 3843: 	    $result.=&Apache::loncommon::start_data_table_row().
 3844: 		'<td align="right">&nbsp;'.$updateCtr.'&nbsp;</td>'.$line.
 3845: 		&Apache::loncommon::end_data_table_row();
 3846: 	    $updateCtr++;
 3847: 	} else {
 3848: 	    push(@noupdate,
 3849: 		 '<td align="right">&nbsp;'.$noupdateCtr.'&nbsp;</td>'.$line);
 3850: 	    $noupdateCtr++;
 3851: 	}
 3852:         if ($aggregateflag) {
 3853:             &Apache::lonnet::cinc('nohist_resourcetracker',\%aggregate,
 3854: 				  $cdom,$cnum);
 3855:         }
 3856:     }
 3857:     if (@noupdate) {
 3858: #	my $numcols=(scalar(@partid)*(scalar(@parts)-1)*2)+3;
 3859: 	my $numcols=scalar(@partid)*4+2;
 3860: 	$result .= &Apache::loncommon::start_data_table_row('LC_empty_row').
 3861: 	    '<td align="center" colspan="'.$numcols.'">'.
 3862: 	    &mt('No Changes Occurred For the Students Below').
 3863: 	    '</td>'.
 3864: 	    &Apache::loncommon::end_data_table_row();
 3865: 	foreach my $line (@noupdate) {
 3866: 	    $result.=
 3867: 		&Apache::loncommon::start_data_table_row().
 3868: 		$line.
 3869: 		&Apache::loncommon::end_data_table_row();
 3870: 	}
 3871:     }
 3872:     $result .= &Apache::loncommon::end_data_table().
 3873: 	&show_grading_menu_form($symb);
 3874:     my $msg = '<p><b>'.
 3875: 	&mt('Number of records updated = [_1] for [quant,_2,student].',
 3876: 	    $rec_update,$count).'</b><br />'.
 3877: 	'<b>'.&mt('Total number of students = [_1]',$env{'form.total'}).
 3878: 	'</b></p>';
 3879:     return $title.$msg.$result;
 3880: }
 3881: 
 3882: sub split_part_type {
 3883:     my ($partstr) = @_;
 3884:     my ($temp,@allparts)=split(/_/,$partstr);
 3885:     my $type=pop(@allparts);
 3886:     my $part=join('_',@allparts);
 3887:     return ($part,$type);
 3888: }
 3889: 
 3890: #------------- end of section for handling grading by section/class ---------
 3891: #
 3892: #----------------------------------------------------------------------------
 3893: 
 3894: 
 3895: #----------------------------------------------------------------------------
 3896: #
 3897: #-------------------------- Next few routines handles grading by csv upload
 3898: #
 3899: #--- Javascript to handle csv upload
 3900: sub csvupload_javascript_reverse_associate {
 3901:     my $error1=&mt('You need to specify the username or the student/employee ID');
 3902:     my $error2=&mt('You need to specify at least one grading field');
 3903:   return(<<ENDPICK);
 3904:   function verify(vf) {
 3905:     var foundsomething=0;
 3906:     var founduname=0;
 3907:     var foundID=0;
 3908:     for (i=0;i<=vf.nfields.value;i++) {
 3909:       tw=eval('vf.f'+i+'.selectedIndex');
 3910:       if (i==0 && tw!=0) { foundID=1; }
 3911:       if (i==1 && tw!=0) { founduname=1; }
 3912:       if (i!=0 && i!=1 && i!=2 && tw!=0) { foundsomething=1; }
 3913:     }
 3914:     if (founduname==0 && foundID==0) {
 3915: 	alert('$error1');
 3916: 	return;
 3917:     }
 3918:     if (foundsomething==0) {
 3919: 	alert('$error2');
 3920: 	return;
 3921:     }
 3922:     vf.submit();
 3923:   }
 3924:   function flip(vf,tf) {
 3925:     var nw=eval('vf.f'+tf+'.selectedIndex');
 3926:     var i;
 3927:     for (i=0;i<=vf.nfields.value;i++) {
 3928:       //can not pick the same destination field for both name and domain
 3929:       if (((i ==0)||(i ==1)) && 
 3930:           ((tf==0)||(tf==1)) && 
 3931:           (i!=tf) &&
 3932:           (eval('vf.f'+i+'.selectedIndex')==nw)) {
 3933:         eval('vf.f'+i+'.selectedIndex=0;')
 3934:       }
 3935:     }
 3936:   }
 3937: ENDPICK
 3938: }
 3939: 
 3940: sub csvupload_javascript_forward_associate {
 3941:     my $error1=&mt('You need to specify the username or the student/employee ID');
 3942:     my $error2=&mt('You need to specify at least one grading field');
 3943:   return(<<ENDPICK);
 3944:   function verify(vf) {
 3945:     var foundsomething=0;
 3946:     var founduname=0;
 3947:     var foundID=0;
 3948:     for (i=0;i<=vf.nfields.value;i++) {
 3949:       tw=eval('vf.f'+i+'.selectedIndex');
 3950:       if (tw==1) { foundID=1; }
 3951:       if (tw==2) { founduname=1; }
 3952:       if (tw>3) { foundsomething=1; }
 3953:     }
 3954:     if (founduname==0 && foundID==0) {
 3955: 	alert('$error1');
 3956: 	return;
 3957:     }
 3958:     if (foundsomething==0) {
 3959: 	alert('$error2');
 3960: 	return;
 3961:     }
 3962:     vf.submit();
 3963:   }
 3964:   function flip(vf,tf) {
 3965:     var nw=eval('vf.f'+tf+'.selectedIndex');
 3966:     var i;
 3967:     //can not pick the same destination field twice
 3968:     for (i=0;i<=vf.nfields.value;i++) {
 3969:       if ((i!=tf) && (eval('vf.f'+i+'.selectedIndex')==nw)) {
 3970:         eval('vf.f'+i+'.selectedIndex=0;')
 3971:       }
 3972:     }
 3973:   }
 3974: ENDPICK
 3975: }
 3976: 
 3977: sub csvuploadmap_header {
 3978:     my ($request,$symb,$datatoken,$distotal)= @_;
 3979:     my $javascript;
 3980:     if ($env{'form.upfile_associate'} eq 'reverse') {
 3981: 	$javascript=&csvupload_javascript_reverse_associate();
 3982:     } else {
 3983: 	$javascript=&csvupload_javascript_forward_associate();
 3984:     }
 3985: 
 3986:     my ($result) = &showResourceInfo($symb,$env{'form.probTitle'});
 3987:     my $checked=(($env{'form.noFirstLine'})?' checked="checked"':'');
 3988:     my $ignore=&mt('Ignore First Line');
 3989:     $symb = &Apache::lonenc::check_encrypt($symb);
 3990:     $request->print(<<ENDPICK);
 3991: <form method="post" enctype="multipart/form-data" action="/adm/grades" name="gradesupload">
 3992: <h3><span class="LC_info">Uploading Class Grades</span></h3>
 3993: $result
 3994: <hr />
 3995: <h3>Identify fields</h3>
 3996: Total number of records found in file: $distotal <hr />
 3997: Enter as many fields as you can. The system will inform you and bring you back
 3998: to this page if the data selected is insufficient to run your class.<hr />
 3999: <input type="button" value="Reverse Association" onclick="javascript:this.form.associate.value='Reverse Association';submit(this.form);" />
 4000: <label><input type="checkbox" name="noFirstLine" $checked />$ignore</label>
 4001: <input type="hidden" name="associate"  value="" />
 4002: <input type="hidden" name="phase"      value="three" />
 4003: <input type="hidden" name="datatoken"  value="$datatoken" />
 4004: <input type="hidden" name="fileupload" value="$env{'form.fileupload'}" />
 4005: <input type="hidden" name="upfiletype" value="$env{'form.upfiletype'}" />
 4006: <input type="hidden" name="upfile_associate" 
 4007:                                        value="$env{'form.upfile_associate'}" />
 4008: <input type="hidden" name="symb"       value="$symb" />
 4009: <input type="hidden" name="saveState"  value="$env{'form.saveState'}" />
 4010: <input type="hidden" name="probTitle"  value="$env{'form.probTitle'}" />
 4011: <input type="hidden" name="command"    value="csvuploadoptions" />
 4012: <hr />
 4013: <script type="text/javascript" language="Javascript">
 4014: $javascript
 4015: </script>
 4016: ENDPICK
 4017:     return '';
 4018: 
 4019: }
 4020: 
 4021: sub csvupload_fields {
 4022:     my ($symb,$errorref) = @_;
 4023:     my (@parts) = &getpartlist($symb,$errorref);
 4024:     if (ref($errorref)) {
 4025:         if ($$errorref) {
 4026:             return;
 4027:         }
 4028:     }
 4029: 
 4030:     my @fields=(['ID','Student/Employee ID'],
 4031: 		['username','Student Username'],
 4032: 		['domain','Student Domain']);
 4033:     my (undef,undef,$url) = &Apache::lonnet::decode_symb($symb);
 4034:     foreach my $part (sort(@parts)) {
 4035: 	my @datum;
 4036: 	my $display=&Apache::lonnet::metadata($url,$part.'.display');
 4037: 	my $name=$part;
 4038: 	if  (!$display) { $display = $name; }
 4039: 	@datum=($name,$display);
 4040: 	if ($name=~/^stores_(.*)_awarded/) {
 4041: 	    push(@fields,['stores_'.$1.'_points',"Points [Part: $1]"]);
 4042: 	}
 4043: 	push(@fields,\@datum);
 4044:     }
 4045:     return (@fields);
 4046: }
 4047: 
 4048: sub csvuploadmap_footer {
 4049:     my ($request,$i,$keyfields) =@_;
 4050:     $request->print(<<ENDPICK);
 4051: </table>
 4052: <input type="hidden" name="nfields" value="$i" />
 4053: <input type="hidden" name="keyfields" value="$keyfields" />
 4054: <input type="button" onclick="javascript:verify(this.form)" value="Assign Grades" /><br />
 4055: </form>
 4056: ENDPICK
 4057: }
 4058: 
 4059: sub checkforfile_js {
 4060:     my $alertmsg = &mt('Please use the browse button to select a file from your local directory.');
 4061:     my $result =<<CSVFORMJS;
 4062: <script type="text/javascript" language="javascript">
 4063:     function checkUpload(formname) {
 4064: 	if (formname.upfile.value == "") {
 4065: 	    alert("$alertmsg");
 4066: 	    return false;
 4067: 	}
 4068: 	formname.submit();
 4069:     }
 4070:     </script>
 4071: CSVFORMJS
 4072:     return $result;
 4073: }
 4074: 
 4075: sub upcsvScores_form {
 4076:     my ($request) = shift;
 4077:     my ($symb)=&get_symb($request);
 4078:     if (!$symb) {return '';}
 4079:     my $result=&checkforfile_js();
 4080:     $env{'form.probTitle'} = &Apache::lonnet::gettitle($symb);
 4081:     my ($table) = &showResourceInfo($symb,$env{'form.probTitle'});
 4082:     $result.=$table;
 4083:     $result.='<br /><table width="100%" border="0"><tr><td bgcolor="#777777">'."\n";
 4084:     $result.='<table width="100%" border="0"><tr bgcolor="#e6ffff"><td>'."\n";
 4085:     $result.='&nbsp;<b>'.&mt('Specify a file containing the class scores for current resource.').
 4086: 	'</b></td></tr>'."\n";
 4087:     $result.='<tr bgcolor="#ffffe6"><td>'."\n";
 4088:     my $upload=&mt("Upload Scores");
 4089:     my $upfile_select=&Apache::loncommon::upfile_select_html();
 4090:     my $ignore=&mt('Ignore First Line');
 4091:     $symb = &Apache::lonenc::check_encrypt($symb);
 4092:     $result.=<<ENDUPFORM;
 4093: <form method="post" enctype="multipart/form-data" action="/adm/grades" name="gradesupload">
 4094: <input type="hidden" name="symb" value="$symb" />
 4095: <input type="hidden" name="command" value="csvuploadmap" />
 4096: <input type="hidden" name="probTitle" value="$env{'form.probTitle'}" />
 4097: <input type="hidden" name="saveState"  value="$env{'form.saveState'}" />
 4098: $upfile_select
 4099: <br /><input type="button" onclick="javascript:checkUpload(this.form);" value="$upload" />
 4100: <label><input type="checkbox" name="noFirstLine" />$ignore</label>
 4101: </form>
 4102: ENDUPFORM
 4103:     $result.=&Apache::loncommon::help_open_topic("Course_Convert_To_CSV",
 4104:                            &mt("How do I create a CSV file from a spreadsheet"))
 4105:     .'</td></tr></table>'."\n";
 4106:     $result.='</td></tr></table><br /><br />'."\n";
 4107:     $result.=&show_grading_menu_form($symb);
 4108:     return $result;
 4109: }
 4110: 
 4111: 
 4112: sub csvuploadmap {
 4113:     my ($request)= @_;
 4114:     my ($symb)=&get_symb($request);
 4115:     if (!$symb) {return '';}
 4116: 
 4117:     my $datatoken;
 4118:     if (!$env{'form.datatoken'}) {
 4119: 	$datatoken=&Apache::loncommon::upfile_store($request);
 4120:     } else {
 4121: 	$datatoken=$env{'form.datatoken'};
 4122: 	&Apache::loncommon::load_tmp_file($request);
 4123:     }
 4124:     my @records=&Apache::loncommon::upfile_record_sep();
 4125:     if ($env{'form.noFirstLine'}) { shift(@records); }
 4126:     &csvuploadmap_header($request,$symb,$datatoken,$#records+1);
 4127:     my ($i,$keyfields);
 4128:     if (@records) {
 4129:         my $fieldserror;
 4130: 	my @fields=&csvupload_fields($symb,\$fieldserror);
 4131:         if ($fieldserror) {
 4132:             $request->print(&navmap_errormsg());
 4133:             return;
 4134:         }
 4135: 	if ($env{'form.upfile_associate'} eq 'reverse') {	
 4136: 	    &Apache::loncommon::csv_print_samples($request,\@records);
 4137: 	    $i=&Apache::loncommon::csv_print_select_table($request,\@records,
 4138: 							  \@fields);
 4139: 	    foreach (@fields) { $keyfields.=$_->[0].','; }
 4140: 	    chop($keyfields);
 4141: 	} else {
 4142: 	    unshift(@fields,['none','']);
 4143: 	    $i=&Apache::loncommon::csv_samples_select_table($request,\@records,
 4144: 							    \@fields);
 4145:             foreach my $rec (@records) {
 4146:                 my %temp = &Apache::loncommon::record_sep($rec);
 4147:                 if (%temp) {
 4148:                     $keyfields=join(',',sort(keys(%temp)));
 4149:                     last;
 4150:                 }
 4151:             }
 4152: 	}
 4153:     }
 4154:     &csvuploadmap_footer($request,$i,$keyfields);
 4155:     $request->print(&show_grading_menu_form($symb));
 4156: 
 4157:     return '';
 4158: }
 4159: 
 4160: sub csvuploadoptions {
 4161:     my ($request)= @_;
 4162:     my ($symb)=&get_symb($request);
 4163:     my $checked=(($env{'form.noFirstLine'})?'1':'0');
 4164:     my $ignore=&mt('Ignore First Line');
 4165:     $request->print(<<ENDPICK);
 4166: <form method="post" enctype="multipart/form-data" action="/adm/grades" name="gradesupload">
 4167: <h3><span class="LC_info">Uploading Class Grade Options</span></h3>
 4168: <input type="hidden" name="command"    value="csvuploadassign" />
 4169: <!--
 4170: <p>
 4171: <label>
 4172:    <input type="checkbox" name="show_full_results" />
 4173:    Show a table of all changes
 4174: </label>
 4175: </p>
 4176: -->
 4177: <p>
 4178: <label>
 4179:    <input type="checkbox" name="overwite_scores" checked="checked" />
 4180:    Overwrite any existing score
 4181: </label>
 4182: </p>
 4183: ENDPICK
 4184:     my %fields=&get_fields();
 4185:     if (!defined($fields{'domain'})) {
 4186: 	my $domform = &Apache::loncommon::select_dom_form($env{'request.role.domain'},'default_domain');
 4187: 	$request->print("\n<p> Users are in domain: ".$domform."</p>\n");
 4188:     }
 4189:     foreach my $key (sort(keys(%env))) {
 4190: 	if ($key !~ /^form\.(.*)$/) { next; }
 4191: 	my $cleankey=$1;
 4192: 	if ($cleankey eq 'command') { next; }
 4193: 	$request->print('<input type="hidden" name="'.$cleankey.
 4194: 			'"  value="'.$env{$key}.'" />'."\n");
 4195:     }
 4196:     # FIXME do a check for any duplicated user ids...
 4197:     # FIXME do a check for any invalid user ids?...
 4198:     $request->print('<input type="submit" value="Assign Grades" /><br />
 4199: <hr /></form>'."\n");
 4200:     $request->print(&show_grading_menu_form($symb));
 4201:     return '';
 4202: }
 4203: 
 4204: sub get_fields {
 4205:     my %fields;
 4206:     my @keyfields = split(/\,/,$env{'form.keyfields'});
 4207:     for (my $i=0; $i<=$env{'form.nfields'}; $i++) {
 4208: 	if ($env{'form.upfile_associate'} eq 'reverse') {
 4209: 	    if ($env{'form.f'.$i} ne 'none') {
 4210: 		$fields{$keyfields[$i]}=$env{'form.f'.$i};
 4211: 	    }
 4212: 	} else {
 4213: 	    if ($env{'form.f'.$i} ne 'none') {
 4214: 		$fields{$env{'form.f'.$i}}=$keyfields[$i];
 4215: 	    }
 4216: 	}
 4217:     }
 4218:     return %fields;
 4219: }
 4220: 
 4221: sub csvuploadassign {
 4222:     my ($request)= @_;
 4223:     my ($symb)=&get_symb($request);
 4224:     if (!$symb) {return '';}
 4225:     my $error_msg = '';
 4226:     &Apache::loncommon::load_tmp_file($request);
 4227:     my @gradedata = &Apache::loncommon::upfile_record_sep();
 4228:     if ($env{'form.noFirstLine'}) { shift(@gradedata); }
 4229:     my %fields=&get_fields();
 4230:     $request->print('<h3>Assigning Grades</h3>');
 4231:     my $courseid=$env{'request.course.id'};
 4232:     my ($classlist) = &getclasslist('all',0);
 4233:     my @notallowed;
 4234:     my @skipped;
 4235:     my @warnings;
 4236:     my $countdone=0;
 4237:     foreach my $grade (@gradedata) {
 4238: 	my %entries=&Apache::loncommon::record_sep($grade);
 4239: 	my $domain;
 4240: 	if ($entries{$fields{'domain'}}) {
 4241: 	    $domain=$entries{$fields{'domain'}};
 4242: 	} else {
 4243: 	    $domain=$env{'form.default_domain'};
 4244: 	}
 4245: 	$domain=~s/\s//g;
 4246: 	my $username=$entries{$fields{'username'}};
 4247: 	$username=~s/\s//g;
 4248: 	if (!$username) {
 4249: 	    my $id=$entries{$fields{'ID'}};
 4250: 	    $id=~s/\s//g;
 4251: 	    my %ids=&Apache::lonnet::idget($domain,$id);
 4252: 	    $username=$ids{$id};
 4253: 	}
 4254: 	if (!exists($$classlist{"$username:$domain"})) {
 4255: 	    my $id=$entries{$fields{'ID'}};
 4256: 	    $id=~s/\s//g;
 4257: 	    if ($id) {
 4258: 		push(@skipped,"$id:$domain");
 4259: 	    } else {
 4260: 		push(@skipped,"$username:$domain");
 4261: 	    }
 4262: 	    next;
 4263: 	}
 4264: 	my $usec=$classlist->{"$username:$domain"}[5];
 4265: 	if (!&canmodify($usec)) {
 4266: 	    push(@notallowed,"$username:$domain");
 4267: 	    next;
 4268: 	}
 4269: 	my %points;
 4270: 	my %grades;
 4271: 	foreach my $dest (keys(%fields)) {
 4272: 	    if ($dest eq 'ID' || $dest eq 'username' ||
 4273: 		$dest eq 'domain') { next; }
 4274: 	    if ($entries{$fields{$dest}} =~ /^\s*$/) { next; }
 4275: 	    if ($dest=~/stores_(.*)_points/) {
 4276: 		my $part=$1;
 4277: 		my $wgt =&Apache::lonnet::EXT('resource.'.$part.'.weight',
 4278: 					      $symb,$domain,$username);
 4279:                 if ($wgt) {
 4280:                     $entries{$fields{$dest}}=~s/\s//g;
 4281:                     my $pcr=$entries{$fields{$dest}} / $wgt;
 4282:                     my $award=($pcr == 0) ? 'incorrect_by_override'
 4283:                                           : 'correct_by_override';
 4284:                     if ($pcr>1) {
 4285:                         push(@warnings,&mt("[_1]: point value larger than weight","$username:$domain"));
 4286:                     }
 4287:                     $grades{"resource.$part.awarded"}=$pcr;
 4288:                     $grades{"resource.$part.solved"}=$award;
 4289:                     $points{$part}=1;
 4290:                 } else {
 4291:                     $error_msg = "<br />" .
 4292:                         &mt("Some point values were assigned"
 4293:                             ." for problems with a weight "
 4294:                             ."of zero. These values were "
 4295:                             ."ignored.");
 4296:                 }
 4297: 	    } else {
 4298: 		if ($dest=~/stores_(.*)_awarded/) { if ($points{$1}) {next;} }
 4299: 		if ($dest=~/stores_(.*)_solved/)  { if ($points{$1}) {next;} }
 4300: 		my $store_key=$dest;
 4301: 		$store_key=~s/^stores/resource/;
 4302: 		$store_key=~s/_/\./g;
 4303: 		$grades{$store_key}=$entries{$fields{$dest}};
 4304: 	    }
 4305: 	}
 4306: 	if (! %grades) { 
 4307:            push(@skipped,&mt("[_1]: no data to save","$username:$domain")); 
 4308:         } else {
 4309: 	   $grades{"resource.regrader"}="$env{'user.name'}:$env{'user.domain'}";
 4310: 	   my $result=&Apache::lonnet::cstore(\%grades,$symb,
 4311: 					   $env{'request.course.id'},
 4312: 					   $domain,$username);
 4313: 	   if ($result eq 'ok') {
 4314: 	      $request->print('.');
 4315: # Remove from grading queue
 4316:               &Apache::bridgetask::remove_from_queue('gradingqueue',$symb,
 4317:                                              $env{'course.'.$env{'request.course.id'}.'.domain'},
 4318:                                              $env{'course.'.$env{'request.course.id'}.'.num'},
 4319:                                              $domain,$username);
 4320: 	   } else {
 4321: 	      $request->print("<p><span class=\"LC_error\">".
 4322:                               &mt("Failed to save data for student [_1]. Message when trying to save was: [_2]",
 4323:                                   "$username:$domain",$result)."</span></p>");
 4324: 	   }
 4325: 	   $request->rflush();
 4326: 	   $countdone++;
 4327:         }
 4328:     }
 4329:     $request->print('<br />'.&Apache::lonhtmlcommon::confirm_success(&mt("Saved scores for [quant,_1,student]",$countdone),$countdone==0));
 4330:     if (@warnings) {
 4331:         $request->print('<br />'.&Apache::lonhtmlcommon::confirm_success(&mt('Warnings generated for the following saved scores:'),1).'<br />');
 4332:         $request->print(join(', ',@warnings));
 4333:     }
 4334:     if (@skipped) {
 4335: 	$request->print('<br />'.&Apache::lonhtmlcommon::confirm_success(&mt('No scores stored for the following username(s):'),1).'<br />');
 4336:         $request->print(join(', ',@skipped));
 4337:     }
 4338:     if (@notallowed) {
 4339: 	$request->print('<br />'.&Apache::lonhtmlcommon::confirm_success(&mt('Modification of scores not allowed for the following username(s):'),1).'<br />');
 4340: 	$request->print(join(', ',@notallowed));
 4341:     }
 4342:     $request->print("<br />\n");
 4343:     $request->print(&show_grading_menu_form($symb));
 4344:     return $error_msg;
 4345: }
 4346: #------------- end of section for handling csv file upload ---------
 4347: #
 4348: #-------------------------------------------------------------------
 4349: #
 4350: #-------------- Next few routines handle grading by page/sequence
 4351: #
 4352: #--- Select a page/sequence and a student to grade
 4353: sub pickStudentPage {
 4354:     my ($request) = shift;
 4355: 
 4356:     my $alertmsg = &mt('Please select the student you wish to grade.');
 4357:     $request->print(<<LISTJAVASCRIPT);
 4358: <script type="text/javascript" language="javascript">
 4359: 
 4360: function checkPickOne(formname) {
 4361:     if (radioSelection(formname.student) == null) {
 4362: 	alert("$alertmsg");
 4363: 	return;
 4364:     }
 4365:     ptr = pullDownSelection(formname.selectpage);
 4366:     formname.page.value = formname["page"+ptr].value;
 4367:     formname.title.value = formname["title"+ptr].value;
 4368:     formname.submit();
 4369: }
 4370: 
 4371: </script>
 4372: LISTJAVASCRIPT
 4373:     &commonJSfunctions($request);
 4374:     my ($symb) = &get_symb($request);
 4375:     my $cdom      = $env{"course.$env{'request.course.id'}.domain"};
 4376:     my $cnum      = $env{"course.$env{'request.course.id'}.num"};
 4377:     my $getsec    = $env{'form.section'} eq '' ? 'all' : $env{'form.section'};
 4378: 
 4379:     my $result='<h3><span class="LC_info">&nbsp;'.
 4380: 	&mt('Manual Grading by Page or Sequence').'</span></h3>';
 4381: 
 4382:     $result.='<form action="/adm/grades" method="post" name="displayPage">'."\n";
 4383:     my $map_error;
 4384:     my ($titles,$symbx) = &getSymbMap($map_error);
 4385:     if ($map_error) {
 4386:         $request->print(&navmap_errormsg());
 4387:         return; 
 4388:     }
 4389:     my ($curpage) =&Apache::lonnet::decode_symb($symb); 
 4390: #    my ($curpage,$mapId) =&Apache::lonnet::decode_symb($symb); 
 4391: #    my $type=($curpage =~ /\.(page|sequence)/);
 4392:     my $select = '<select name="selectpage">'."\n";
 4393:     my $ctr=0;
 4394:     foreach (@$titles) {
 4395: 	my ($minder,$showtitle) = ($_ =~ /(\d+)\.(.*)/);
 4396: 	$select.='<option value="'.$ctr.'" '.
 4397: 	    ($$symbx{$_} =~ /$curpage$/ ? 'selected="selected"' : '').
 4398: 	    '>'.$showtitle.'</option>'."\n";
 4399: 	$ctr++;
 4400:     }
 4401:     $select.= '</select>';
 4402:     $result.='&nbsp;<b>'.&mt('Problems from').':</b> '.$select."<br />\n";
 4403: 
 4404:     $ctr=0;
 4405:     foreach (@$titles) {
 4406: 	my ($minder,$showtitle) = ($_ =~ /(\d+)\.(.*)/);
 4407: 	$result.='<input type="hidden" name="page'.$ctr.'" value="'.$$symbx{$_}.'" />'."\n";
 4408: 	$result.='<input type="hidden" name="title'.$ctr.'" value="'.$showtitle.'" />'."\n";
 4409: 	$ctr++;
 4410:     }
 4411:     $result.='<input type="hidden" name="page" />'."\n".
 4412: 	'<input type="hidden" name="title" />'."\n";
 4413: 
 4414:     my $options =
 4415: 	'<label><input type="radio" name="vProb" value="no" checked="checked" /> '.&mt('no').' </label>'."\n".
 4416: 	'<label><input type="radio" name="vProb" value="yes" /> '.&mt('yes').' </label>'."<br />\n";
 4417:     $result.='&nbsp;<b>'.&mt('View Problem Text').': </b>'.$options;
 4418: 
 4419:     $options =
 4420: 	'<label><input type="radio" name="lastSub" value="none" /> '.&mt('none').' </label>'."\n".
 4421: 	'<label><input type="radio" name="lastSub" value="datesub" checked="checked" /> '.&mt('by dates and submissions').'</label>'."\n".
 4422: 	'<label><input type="radio" name="lastSub" value="all" /> '.&mt('all details').' </label>'."\n";
 4423:     $result.='&nbsp;<b>'.&mt('Submissions').': </b>'.$options;
 4424:     
 4425:     $result.=&build_section_inputs();
 4426:     my $stu_status = join(':',&Apache::loncommon::get_env_multiple('form.Status'));
 4427:     $result.='<input type="hidden" name="Status"  value="'.$stu_status.'" />'."\n".
 4428: 	'<input type="hidden" name="command" value="displayPage" />'."\n".
 4429: 	'<input type="hidden" name="symb"    value="'.&Apache::lonenc::check_encrypt($symb).'" />'."\n".
 4430: 	'<input type="hidden" name="saveState" value="'.$env{'form.saveState'}.'" />'."<br />\n";
 4431: 
 4432:     $result.='&nbsp;<b>'.&mt('Use CODE').': </b> <input type="text" name="CODE" value="" /> <br />'."\n";
 4433: 
 4434:     $result.='&nbsp;<input type="button" '.
 4435:              'onclick="javascript:checkPickOne(this.form);" value="'.&mt('Next').' &rarr;" /><br />'."\n";
 4436: 
 4437:     $request->print($result);
 4438: 
 4439:     my $studentTable.='&nbsp;<b>'.&mt('Select a student you wish to grade and then click on the Next button.').'</b><br />'.
 4440: 	&Apache::loncommon::start_data_table().
 4441: 	&Apache::loncommon::start_data_table_header_row().
 4442: 	'<th align="right">&nbsp;'.&mt('No.').'</th>'.
 4443: 	'<th>'.&nameUserString('header').'</th>'.
 4444: 	'<th align="right">&nbsp;'.&mt('No.').'</th>'.
 4445: 	'<th>'.&nameUserString('header').'</th>'.
 4446: 	&Apache::loncommon::end_data_table_header_row();
 4447:  
 4448:     my (undef,undef,$fullname) = &getclasslist($getsec,'1');
 4449:     my $ptr = 1;
 4450:     foreach my $student (sort 
 4451: 			 {
 4452: 			     if (lc($$fullname{$a}) ne lc($$fullname{$b})) {
 4453: 				 return (lc($$fullname{$a}) cmp lc($$fullname{$b}));
 4454: 			     }
 4455: 			     return $a cmp $b;
 4456: 			 } (keys(%$fullname))) {
 4457: 	my ($uname,$udom) = split(/:/,$student);
 4458: 	$studentTable.=($ptr%2==1 ? &Apache::loncommon::start_data_table_row()
 4459:                                   : '</td>');
 4460: 	$studentTable.='<td align="right">'.$ptr.'&nbsp;</td>';
 4461: 	$studentTable.='<td>&nbsp;<label><input type="radio" name="student" value="'.$student.'" /> '
 4462: 	    .&nameUserString(undef,$$fullname{$student},$uname,$udom)."</label>\n";
 4463: 	$studentTable.=
 4464: 	    ($ptr%2 == 0 ? '</td>'.&Apache::loncommon::end_data_table_row() 
 4465:                          : '');
 4466: 	$ptr++;
 4467:     }
 4468:     if ($ptr%2 == 0) {
 4469: 	$studentTable.='</td><td>&nbsp;</td><td>&nbsp;</td>'.
 4470: 	    &Apache::loncommon::end_data_table_row();
 4471:     }
 4472:     $studentTable.=&Apache::loncommon::end_data_table()."\n";
 4473:     $studentTable.='<input type="button" '.
 4474:                    'onclick="javascript:checkPickOne(this.form);" value="'.&mt('Next').' &rarr;" /></form>'."\n";
 4475: 
 4476:     $studentTable.=&show_grading_menu_form($symb);
 4477:     $request->print($studentTable);
 4478: 
 4479:     return '';
 4480: }
 4481: 
 4482: sub getSymbMap {
 4483:     my ($map_error) = @_;
 4484:     my $navmap = Apache::lonnavmaps::navmap->new();
 4485:     unless (ref($navmap)) {
 4486:         if (ref($map_error)) {
 4487:             $$map_error = 'navmap';
 4488:         }
 4489:         return;
 4490:     }
 4491:     my %symbx = ();
 4492:     my @titles = ();
 4493:     my $minder = 0;
 4494: 
 4495:     # Gather every sequence that has problems.
 4496:     my @sequences = $navmap->retrieveResources(undef, sub { shift->is_map(); },
 4497: 					       1,0,1);
 4498:     for my $sequence ($navmap->getById('0.0'), @sequences) {
 4499: 	if ($navmap->hasResource($sequence, sub { shift->is_problem(); }, 0) ) {
 4500: 	    my $title = $minder.'.'.
 4501: 		&HTML::Entities::encode($sequence->compTitle(),'"\'&');
 4502: 	    push(@titles, $title); # minder in case two titles are identical
 4503: 	    $symbx{$title} = &HTML::Entities::encode($sequence->symb(),'"\'&');
 4504: 	    $minder++;
 4505: 	}
 4506:     }
 4507:     return \@titles,\%symbx;
 4508: }
 4509: 
 4510: #
 4511: #--- Displays a page/sequence w/wo problems, w/wo submissions
 4512: sub displayPage {
 4513:     my ($request) = shift;
 4514: 
 4515:     my ($symb) = &get_symb($request);
 4516:     my $cdom      = $env{"course.$env{'request.course.id'}.domain"};
 4517:     my $cnum      = $env{"course.$env{'request.course.id'}.num"};
 4518:     my $getsec    = $env{'form.section'} eq '' ? 'all' : $env{'form.section'};
 4519:     my $pageTitle = $env{'form.page'};
 4520:     my ($classlist,undef,$fullname) = &getclasslist($getsec,'1');
 4521:     my ($uname,$udom) = split(/:/,$env{'form.student'});
 4522:     my $usec=$classlist->{$env{'form.student'}}[5];
 4523: 
 4524:     #need to make sure we have the correct data for later EXT calls, 
 4525:     #thus invalidate the cache
 4526:     &Apache::lonnet::devalidatecourseresdata(
 4527:                  $env{'course.'.$env{'request.course.id'}.'.num'},
 4528:                  $env{'course.'.$env{'request.course.id'}.'.domain'});
 4529:     &Apache::lonnet::clear_EXT_cache_status();
 4530: 
 4531:     if (!&canview($usec)) {
 4532: 	$request->print('<span class="LC_warning">'.&mt('Unable to view requested student. ([_1])',$env{'form.student'}).'</span>');
 4533: 	$request->print(&show_grading_menu_form($symb));
 4534: 	return;
 4535:     }
 4536:     my $result='<h3><span class="LC_info">&nbsp;'.$env{'form.title'}.'</span></h3>';
 4537:     $result.='<h3>&nbsp;'.&mt('Student: [_1]',&nameUserString(undef,$$fullname{$env{'form.student'}},$uname,$udom)).
 4538: 	'</h3>'."\n";
 4539:     $env{'form.CODE'} = uc($env{'form.CODE'});
 4540:     if (&Apache::lonnet::validCODE(uc($env{'form.CODE'}))) {
 4541: 	$result.='<h3>&nbsp;'.&mt('CODE: [_1]',$env{'form.CODE'}).'</h3>'."\n";
 4542:     } else {
 4543: 	delete($env{'form.CODE'});
 4544:     }
 4545:     &sub_page_js($request);
 4546:     $request->print($result);
 4547: 
 4548:     my $navmap = Apache::lonnavmaps::navmap->new();
 4549:     unless (ref($navmap)) {
 4550:         $request->print(&navmap_errormsg());
 4551:         $request->print(&show_grading_menu_form($symb));
 4552:         return;
 4553:     }
 4554:     my ($mapUrl, $id, $resUrl)=&Apache::lonnet::decode_symb($env{'form.page'});
 4555:     my $map = $navmap->getResourceByUrl($resUrl); # add to navmaps
 4556:     if (!$map) {
 4557: 	$request->print('<span class="LC_warning">'.&mt('Unable to view requested sequence. ([_1])',$resUrl).'</span>');
 4558: 	$request->print(&show_grading_menu_form($symb));
 4559: 	return; 
 4560:     }
 4561:     my $iterator = $navmap->getIterator($map->map_start(),
 4562: 					$map->map_finish());
 4563: 
 4564:     my $studentTable='<form action="/adm/grades" method="post" name="gradePage">'."\n".
 4565: 	'<input type="hidden" name="command" value="gradeByPage" />'."\n".
 4566: 	'<input type="hidden" name="fullname" value="'.$$fullname{$env{'form.student'}}.'" />'."\n".
 4567: 	'<input type="hidden" name="student" value="'.$env{'form.student'}.'" />'."\n".
 4568: 	'<input type="hidden" name="page"    value="'.$pageTitle.'" />'."\n".
 4569: 	'<input type="hidden" name="title"   value="'.$env{'form.title'}.'" />'."\n".
 4570: 	'<input type="hidden" name="symb"    value="'.&Apache::lonenc::check_encrypt($symb).'" />'."\n".
 4571: 	'<input type="hidden" name="overRideScore" value="no" />'."\n".
 4572: 	'<input type="hidden" name="saveState" value="'.$env{'form.saveState'}.'" />'."\n";
 4573: 
 4574:     if (defined($env{'form.CODE'})) {
 4575: 	$studentTable.=
 4576: 	    '<input type="hidden" name="CODE" value="'.$env{'form.CODE'}.'" />'."\n";
 4577:     }
 4578:     my $checkIcon = '<img alt="'.&mt('Check Mark').
 4579: 	'" src="'.&Apache::loncommon::lonhttpdurl($request->dir_config('lonIconsURL').'/check.gif').'" height="16" border="0" />';
 4580: 
 4581:     $studentTable.='&nbsp;<span class="LC_info">'.
 4582:         &mt('Problems graded correct by the computer are marked with a [_1] symbol.',$checkIcon).
 4583:         '</span>'."\n".
 4584: 	&Apache::loncommon::start_data_table().
 4585: 	&Apache::loncommon::start_data_table_header_row().
 4586: 	'<th align="center">&nbsp;Prob.&nbsp;</th>'.
 4587: 	'<th>&nbsp;'.($env{'form.vProb'} eq 'no' ? &mt('Title') : &mt('Problem Text')).'/'.&mt('Grade').'</th>'.
 4588: 	&Apache::loncommon::end_data_table_header_row();
 4589: 
 4590:     &Apache::lonxml::clear_problem_counter();
 4591:     my ($depth,$question,$prob) = (1,1,1);
 4592:     $iterator->next(); # skip the first BEGIN_MAP
 4593:     my $curRes = $iterator->next(); # for "current resource"
 4594:     while ($depth > 0) {
 4595:         if($curRes == $iterator->BEGIN_MAP) { $depth++; }
 4596:         if($curRes == $iterator->END_MAP) { $depth--; }
 4597: 
 4598:         if (ref($curRes) && $curRes->is_problem()) {
 4599: 	    my $parts = $curRes->parts();
 4600:             my $title = $curRes->compTitle();
 4601: 	    my $symbx = $curRes->symb();
 4602: 	    $studentTable.=
 4603: 		&Apache::loncommon::start_data_table_row().
 4604: 		'<td align="center" valign="top" >'.$prob.
 4605: 		(scalar(@{$parts}) == 1 ? '' 
 4606: 		                        : '<br />('.&mt('[_1]parts)',
 4607: 							scalar(@{$parts}).'&nbsp;')
 4608: 		 ).
 4609: 		 '</td>';
 4610: 	    $studentTable.='<td valign="top">';
 4611: 	    my %form = ('CODE' => $env{'form.CODE'},);
 4612: 	    if ($env{'form.vProb'} eq 'yes' ) {
 4613: 		$studentTable.=&show_problem($request,$symbx,$uname,$udom,1,
 4614: 					     undef,'both',\%form);
 4615: 	    } else {
 4616: 		my $companswer = &Apache::loncommon::get_student_answers($symbx,$uname,$udom,$env{'request.course.id'},%form);
 4617: 		$companswer =~ s|<form(.*?)>||g;
 4618: 		$companswer =~ s|</form>||g;
 4619: #		while ($companswer =~ /(<a href\=\"javascript:newWindow.*?Script Vars<\/a>)/s) { #<a href="javascript:newWindow</a>
 4620: #		    $companswer =~ s/$1/ /ms;
 4621: #		    $request->print('match='.$1."<br />\n");
 4622: #		}
 4623: #		$companswer =~ s|<table border=\"1\">|<table border=\"0\">|g;
 4624: 		$studentTable.='&nbsp;<b>'.$title.'</b>&nbsp;<br />&nbsp;<b>'.&mt('Correct answer').':</b><br />'.$companswer;
 4625: 	    }
 4626: 
 4627: 	    my %record = &Apache::lonnet::restore($symbx,$env{'request.course.id'},$udom,$uname);
 4628: 
 4629: 	    if ($env{'form.lastSub'} eq 'datesub') {
 4630: 		if ($record{'version'} eq '') {
 4631: 		    $studentTable.='<br />&nbsp;<span class="LC_warning">'.&mt('No recorded submission for this problem.').'</span><br />';
 4632: 		} else {
 4633: 		    my %responseType = ();
 4634: 		    foreach my $partid (@{$parts}) {
 4635: 			my @responseIds =$curRes->responseIds($partid);
 4636: 			my @responseType =$curRes->responseType($partid);
 4637: 			my %responseIds;
 4638: 			for (my $i=0;$i<=$#responseIds;$i++) {
 4639: 			    $responseIds{$responseIds[$i]}=$responseType[$i];
 4640: 			}
 4641: 			$responseType{$partid} = \%responseIds;
 4642: 		    }
 4643: 		    $studentTable.= &displaySubByDates($symbx,\%record,$parts,\%responseType,$checkIcon,$uname,$udom);
 4644: 
 4645: 		}
 4646: 	    } elsif ($env{'form.lastSub'} eq 'all') {
 4647: 		my $last = ($env{'form.lastSub'} eq 'last' ? 'last' : '');
 4648: 		$studentTable.=&Apache::loncommon::get_previous_attempt($symbx,$uname,$udom,
 4649: 									$env{'request.course.id'},
 4650: 									'','.submission');
 4651:  
 4652: 	    }
 4653: 	    if (&canmodify($usec)) {
 4654:             $studentTable.=&gradeBox_start();
 4655: 		foreach my $partid (@{$parts}) {
 4656: 		    $studentTable.=&gradeBox($request,$symbx,$uname,$udom,$question,$partid,\%record);
 4657: 		    $studentTable.='<input type="hidden" name="q_'.$question.'" value="'.$partid.'" />'."\n";
 4658: 		    $question++;
 4659: 		}
 4660:             $studentTable.=&gradeBox_end();
 4661: 		$prob++;
 4662: 	    }
 4663: 	    $studentTable.='</td></tr>';
 4664: 
 4665: 	}
 4666:         $curRes = $iterator->next();
 4667:     }
 4668: 
 4669:     $studentTable.=
 4670:         '</table>'."\n".
 4671:         '<input type="button" value="'.&mt('Save').'" '.
 4672:         'onclick="javascript:checkSubmitPage(this.form,'.$question.');" />'.
 4673:         '</form>'."\n";
 4674:     $studentTable.=&show_grading_menu_form($symb);
 4675:     $request->print($studentTable);
 4676: 
 4677:     return '';
 4678: }
 4679: 
 4680: sub displaySubByDates {
 4681:     my ($symb,$record,$parts,$responseType,$checkIcon,$uname,$udom) = @_;
 4682:     my $isCODE=0;
 4683:     my $isTask = ($symb =~/\.task$/);
 4684:     if (exists($record->{'resource.CODE'})) { $isCODE=1; }
 4685:     my $studentTable=&Apache::loncommon::start_data_table().
 4686: 	&Apache::loncommon::start_data_table_header_row().
 4687: 	'<th>'.&mt('Date/Time').'</th>'.
 4688: 	($isCODE?'<th>'.&mt('CODE').'</th>':'').
 4689: 	'<th>'.&mt('Submission').'</th>'.
 4690: 	'<th>'.&mt('Status').'</th>'.
 4691: 	&Apache::loncommon::end_data_table_header_row();
 4692:     my ($version);
 4693:     my %mark;
 4694:     my %orders;
 4695:     $mark{'correct_by_student'} = $checkIcon;
 4696:     if (!exists($$record{'1:timestamp'})) {
 4697: 	return '<br />&nbsp;<span class="LC_warning">'.&mt('Nothing submitted - no attempts.').'</span><br />';
 4698:     }
 4699: 
 4700:     my $interaction;
 4701:     my $no_increment = 1;
 4702:     my %lastrndseed;
 4703:     for ($version=1;$version<=$$record{'version'};$version++) {
 4704: 	my $timestamp = 
 4705: 	    &Apache::lonlocal::locallocaltime($$record{$version.':timestamp'});
 4706: 	if (exists($$record{$version.':resource.0.version'})) {
 4707: 	    $interaction = $$record{$version.':resource.0.version'};
 4708: 	}
 4709: 
 4710: 	my $where = ($isTask ? "$version:resource.$interaction"
 4711: 		             : "$version:resource");
 4712: 	$studentTable.=&Apache::loncommon::start_data_table_row().
 4713: 	    '<td>'.$timestamp.'</td>';
 4714: 	if ($isCODE) {
 4715: 	    $studentTable.='<td>'.$record->{$version.':resource.CODE'}.'</td>';
 4716: 	}
 4717: 	my @versionKeys = split(/\:/,$$record{$version.':keys'});
 4718: 	my @displaySub = ();
 4719: 	foreach my $partid (@{$parts}) {
 4720:             my ($hidden,$type);
 4721:             $type = $$record{$version.':resource.'.$partid.'.type'};
 4722:             if (($type eq 'anonsurvey') || ($type eq 'anonsurveycred')) {
 4723:                 $hidden = 1;
 4724:             }
 4725: 	    my @matchKey = ($isTask ? sort(grep /^resource\.\d+\.\Q$partid\E\.award$/,@versionKeys)
 4726: 			            : sort(grep /^resource\.\Q$partid\E\..*?\.submission$/,@versionKeys));
 4727: 	    
 4728: #	    next if ($$record{"$version:resource.$partid.solved"} eq '');
 4729: 	    my $display_part=&get_display_part($partid,$symb);
 4730: 	    foreach my $matchKey (@matchKey) {
 4731: 		if (exists($$record{$version.':'.$matchKey}) &&
 4732: 		    $$record{$version.':'.$matchKey} ne '') {
 4733:                     
 4734: 		    my ($responseId)= ($isTask ? ($matchKey=~ /^resource\.(.*?)\.\Q$partid\E\.award$/)
 4735: 				               : ($matchKey=~ /^resource\.\Q$partid\E\.(.*?)\.submission$/));
 4736:                     $displaySub[0].='<span class="LC_nobreak"';
 4737:                     $displaySub[0].='<b>'.&mt('Part: [_1]',$display_part).'</b>'
 4738:                                    .' <span class="LC_internal_info">'
 4739:                                    .'('.&mt('Response ID: [_1]',$responseId).')'
 4740:                                    .'</span>'
 4741:                                    .' <b>';
 4742:                     if ($hidden) {
 4743:                         $displaySub[0].= &mt('Anonymous Survey').'</b>';
 4744:                     } else {
 4745:                         my ($trial,$rndseed,$newvariation);
 4746:                         if ($type eq 'randomizetry') {
 4747:                             $trial = $$record{"$where.$partid.tries"};
 4748:                             $rndseed = $$record{"$where.$partid.rndseed"};
 4749:                         }
 4750: 		        if ($$record{"$where.$partid.tries"} eq '') {
 4751: 			    $displaySub[0].=&mt('Trial not counted');
 4752: 		        } else {
 4753: 			    $displaySub[0].=&mt('Trial: [_1]',
 4754: 					    $$record{"$where.$partid.tries"});
 4755:                             if ($rndseed || $lastrndseed{$partid}) {
 4756:                                 if ($rndseed ne $lastrndseed{$partid}) {
 4757:                                     $newvariation = '&nbsp;('.&mt('New variation this try').')';
 4758:                                 }
 4759:                             }
 4760: 		        }
 4761: 		        my $responseType=($isTask ? 'Task'
 4762:                                               : $responseType->{$partid}->{$responseId});
 4763: 		        if (!exists($orders{$partid})) { $orders{$partid}={}; }
 4764: 		        if ((!exists($orders{$partid}->{$responseId})) || ($trial)) {
 4765: 			    $orders{$partid}->{$responseId}=
 4766: 			        &get_order($partid,$responseId,$symb,$uname,$udom,
 4767:                                            $no_increment,$type,$trial,$rndseed);
 4768: 		        }
 4769: 		        $displaySub[0].='</b>'.$newvariation.'</span>'; # /nobreak
 4770: 		        $displaySub[0].='&nbsp; '.
 4771: 			    &cleanRecord($$record{$version.':'.$matchKey},$responseType,$symb,$partid,$responseId,$record,$orders{$partid}->{$responseId},"$version:",$uname,$udom,$type,$trial,$rndseed).'<br />';
 4772:                     }
 4773: 		}
 4774: 	    }
 4775: 	    if (exists($$record{"$where.$partid.checkedin"})) {
 4776: 		$displaySub[1].=&mt('Checked in by [_1] into slot [_2]',
 4777: 				    $$record{"$where.$partid.checkedin"},
 4778: 				    $$record{"$where.$partid.checkedin.slot"}).
 4779: 					'<br />';
 4780: 	    }
 4781: 	    if (exists $$record{"$where.$partid.award"}) {
 4782: 		$displaySub[1].='<b>'.&mt('Part:').'</b>&nbsp;'.$display_part.' &nbsp;'.
 4783: 		    lc($$record{"$where.$partid.award"}).' '.
 4784: 		    $mark{$$record{"$where.$partid.solved"}}.
 4785: 		    '<br />';
 4786: 	    }
 4787: 	    if (exists $$record{"$where.$partid.regrader"}) {
 4788: 		$displaySub[2].=$$record{"$where.$partid.regrader"}.
 4789: 		    ' (<b>'.&mt('Part').':</b> '.$display_part.')';
 4790: 	    } elsif ($$record{"$version:resource.$partid.regrader"} =~ /\S/) {
 4791: 		$displaySub[2].=
 4792: 		    $$record{"$version:resource.$partid.regrader"}.
 4793: 		    ' (<b>'.&mt('Part').':</b> '.$display_part.')';
 4794: 	    }
 4795: 	}
 4796: 	# needed because old essay regrader has not parts info
 4797: 	if (exists $$record{"$version:resource.regrader"}) {
 4798: 	    $displaySub[2].=$$record{"$version:resource.regrader"};
 4799: 	}
 4800: 	$studentTable.='<td>'.$displaySub[0].'&nbsp;</td><td>'.$displaySub[1];
 4801: 	if ($displaySub[2]) {
 4802: 	    $studentTable.=&mt('Manually graded by [_1]',$displaySub[2]);
 4803: 	}
 4804: 	$studentTable.='&nbsp;</td>'.
 4805: 	    &Apache::loncommon::end_data_table_row();
 4806:     }
 4807:     $studentTable.=&Apache::loncommon::end_data_table();
 4808:     return $studentTable;
 4809: }
 4810: 
 4811: sub updateGradeByPage {
 4812:     my ($request) = shift;
 4813: 
 4814:     my $cdom      = $env{"course.$env{'request.course.id'}.domain"};
 4815:     my $cnum      = $env{"course.$env{'request.course.id'}.num"};
 4816:     my $getsec    = $env{'form.section'} eq '' ? 'all' : $env{'form.section'};
 4817:     my $pageTitle = $env{'form.page'};
 4818:     my ($classlist,undef,$fullname) = &getclasslist($getsec,'1');
 4819:     my ($uname,$udom) = split(/:/,$env{'form.student'});
 4820:     my $usec=$classlist->{$env{'form.student'}}[5];
 4821:     if (!&canmodify($usec)) {
 4822: 	$request->print('<span class="LC_warning">'.&mt('Unable to modify requested student ([_1])',$env{'form.student'}).'</span>');
 4823: 	$request->print(&show_grading_menu_form($env{'form.symb'}));
 4824: 	return;
 4825:     }
 4826:     my $result='<h3><span class="LC_info">&nbsp;'.$env{'form.title'}.'</span></h3>';
 4827:     $result.='<h3>&nbsp;'.&mt('Student: ').&nameUserString(undef,$env{'form.fullname'},$uname,$udom).
 4828: 	'</h3>'."\n";
 4829: 
 4830:     $request->print($result);
 4831: 
 4832: 
 4833:     my $navmap = Apache::lonnavmaps::navmap->new();
 4834:     unless (ref($navmap)) {
 4835:         $request->print(&navmap_errormsg());
 4836:         return;
 4837:     }
 4838:     my ($mapUrl, $id, $resUrl) = &Apache::lonnet::decode_symb( $env{'form.page'});
 4839:     my $map = $navmap->getResourceByUrl($resUrl); # add to navmaps
 4840:     if (!$map) {
 4841: 	$request->print('<span class="LC_warning">'.&mt('Unable to grade requested sequence ([_1]).',$resUrl).'</span>');
 4842: 	my ($symb)=&get_symb($request);
 4843: 	$request->print(&show_grading_menu_form($symb));
 4844: 	return; 
 4845:     }
 4846:     my $iterator = $navmap->getIterator($map->map_start(),
 4847: 					$map->map_finish());
 4848: 
 4849:     my $studentTable=
 4850: 	&Apache::loncommon::start_data_table().
 4851: 	&Apache::loncommon::start_data_table_header_row().
 4852: 	'<th align="center">&nbsp;'.&mt('Prob.').'&nbsp;</th>'.
 4853: 	'<th>&nbsp;'.&mt('Title').'&nbsp;</th>'.
 4854: 	'<th>&nbsp;'.&mt('Previous Score').'&nbsp;</th>'.
 4855: 	'<th>&nbsp;'.&mt('New Score').'&nbsp;</th>'.
 4856: 	&Apache::loncommon::end_data_table_header_row();
 4857: 
 4858:     $iterator->next(); # skip the first BEGIN_MAP
 4859:     my $curRes = $iterator->next(); # for "current resource"
 4860:     my ($depth,$question,$prob,$changeflag)= (1,1,1,0);
 4861:     while ($depth > 0) {
 4862:         if($curRes == $iterator->BEGIN_MAP) { $depth++; }
 4863:         if($curRes == $iterator->END_MAP) { $depth--; }
 4864: 
 4865:         if (ref($curRes) && $curRes->is_problem()) {
 4866: 	    my $parts = $curRes->parts();
 4867:             my $title = $curRes->compTitle();
 4868: 	    my $symbx = $curRes->symb();
 4869: 	    $studentTable.=
 4870: 		&Apache::loncommon::start_data_table_row().
 4871: 		'<td align="center" valign="top" >'.$prob.
 4872: 		(scalar(@{$parts}) == 1 ? '' 
 4873:                                         : '<br />('.&mt('[quant,_1,part]',scalar(@{$parts}))
 4874: 		.')').'</td>';
 4875: 	    $studentTable.='<td valign="top">&nbsp;<b>'.$title.'</b>&nbsp;</td>';
 4876: 
 4877: 	    my %newrecord=();
 4878: 	    my @displayPts=();
 4879:             my %aggregate = ();
 4880:             my $aggregateflag = 0;
 4881: 	    foreach my $partid (@{$parts}) {
 4882: 		my $newpts = $env{'form.GD_BOX'.$question.'_'.$partid};
 4883: 		my $oldpts = $env{'form.oldpts'.$question.'_'.$partid};
 4884: 
 4885: 		my $wgt = $env{'form.WGT'.$question.'_'.$partid} != 0 ? 
 4886: 		    $env{'form.WGT'.$question.'_'.$partid} : 1;
 4887: 		my $partial = $newpts/$wgt;
 4888: 		my $score;
 4889: 		if ($partial > 0) {
 4890: 		    $score = 'correct_by_override';
 4891: 		} elsif ($newpts ne '') { #empty is taken as 0
 4892: 		    $score = 'incorrect_by_override';
 4893: 		}
 4894: 		my $dropMenu = $env{'form.GD_SEL'.$question.'_'.$partid};
 4895: 		if ($dropMenu eq 'excused') {
 4896: 		    $partial = '';
 4897: 		    $score = 'excused';
 4898: 		} elsif ($dropMenu eq 'reset status'
 4899: 			 && $env{'form.solved'.$question.'_'.$partid} ne '') { #update only if previous record exists
 4900: 		    $newrecord{'resource.'.$partid.'.tries'} = 0;
 4901: 		    $newrecord{'resource.'.$partid.'.solved'} = '';
 4902: 		    $newrecord{'resource.'.$partid.'.award'} = '';
 4903: 		    $newrecord{'resource.'.$partid.'.awarded'} = 0;
 4904: 		    $newrecord{'resource.'.$partid.'.regrader'} = "$env{'user.name'}:$env{'user.domain'}";
 4905: 		    $changeflag++;
 4906: 		    $newpts = '';
 4907:                     
 4908:                     my $aggtries =  $env{'form.aggtries'.$question.'_'.$partid};
 4909:                     my $totaltries = $env{'form.totaltries'.$question.'_'.$partid};
 4910:                     my $solvedstatus = $env{'form.solved'.$question.'_'.$partid};
 4911:                     if ($aggtries > 0) {
 4912:                         &decrement_aggs($symbx,$partid,\%aggregate,$aggtries,$totaltries,$solvedstatus);
 4913:                         $aggregateflag = 1;
 4914:                     }
 4915: 		}
 4916: 		my $display_part=&get_display_part($partid,$curRes->symb());
 4917: 		my $oldstatus = $env{'form.solved'.$question.'_'.$partid};
 4918: 		$displayPts[0].='&nbsp;<b>'.&mt('Part').':</b> '.$display_part.' = '.
 4919: 		    (($oldstatus eq 'excused') ? 'excused' : $oldpts).
 4920: 		    '&nbsp;<br />';
 4921: 		$displayPts[1].='&nbsp;<b>'.&mt('Part').':</b> '.$display_part.' = '.
 4922: 		     (($score eq 'excused') ? 'excused' : $newpts).
 4923: 		    '&nbsp;<br />';
 4924: 		$question++;
 4925: 		next if ($dropMenu eq 'reset status' || ($newpts eq $oldpts && $score ne 'excused'));
 4926: 
 4927: 		$newrecord{'resource.'.$partid.'.awarded'}  = $partial if $partial ne '';
 4928: 		$newrecord{'resource.'.$partid.'.solved'}   = $score if $score ne '';
 4929: 		$newrecord{'resource.'.$partid.'.regrader'} = "$env{'user.name'}:$env{'user.domain'}"
 4930: 		    if (scalar(keys(%newrecord)) > 0);
 4931: 
 4932: 		$changeflag++;
 4933: 	    }
 4934: 	    if (scalar(keys(%newrecord)) > 0) {
 4935: 		my %record = 
 4936: 		    &Apache::lonnet::restore($symbx,$env{'request.course.id'},
 4937: 					     $udom,$uname);
 4938: 
 4939: 		if (&Apache::lonnet::validCODE($env{'form.CODE'})) {
 4940: 		    $newrecord{'resource.CODE'} = $env{'form.CODE'};
 4941: 		} elsif (&Apache::lonnet::validCODE($record{'resource.CODE'})) {
 4942: 		    $newrecord{'resource.CODE'} = '';
 4943: 		}
 4944: 		&Apache::lonnet::cstore(\%newrecord,$symbx,$env{'request.course.id'},
 4945: 					$udom,$uname);
 4946: 		%record = &Apache::lonnet::restore($symbx,
 4947: 						   $env{'request.course.id'},
 4948: 						   $udom,$uname);
 4949: 		&check_and_remove_from_queue($parts,\%record,undef,$symbx,
 4950: 					     $cdom,$cnum,$udom,$uname);
 4951: 	    }
 4952: 	    
 4953:             if ($aggregateflag) {
 4954:                 &Apache::lonnet::cinc('nohist_resourcetracker',\%aggregate,
 4955:                       $env{'course.'.$env{'request.course.id'}.'.domain'},
 4956:                       $env{'course.'.$env{'request.course.id'}.'.num'});
 4957:             }
 4958: 
 4959: 	    $studentTable.='<td valign="top">'.$displayPts[0].'</td>'.
 4960: 		'<td valign="top">'.$displayPts[1].'</td>'.
 4961: 		&Apache::loncommon::end_data_table_row();
 4962: 
 4963: 	    $prob++;
 4964: 	}
 4965:         $curRes = $iterator->next();
 4966:     }
 4967: 
 4968:     $studentTable.=&Apache::loncommon::end_data_table();
 4969:     $studentTable.=&show_grading_menu_form($env{'form.symb'});
 4970:     my $grademsg=($changeflag == 0 ? &mt('No score was changed or updated.') :
 4971: 		  &mt('The scores were changed for [quant,_1,problem].',
 4972: 		  $changeflag));
 4973:     $request->print($grademsg.$studentTable);
 4974: 
 4975:     return '';
 4976: }
 4977: 
 4978: #-------- end of section for handling grading by page/sequence ---------
 4979: #
 4980: #-------------------------------------------------------------------
 4981: 
 4982: #-------------------- Bubblesheet (Scantron) Grading -------------------
 4983: #
 4984: #------ start of section for handling grading by page/sequence ---------
 4985: 
 4986: =pod
 4987: 
 4988: =head1 Bubble sheet grading routines
 4989: 
 4990:   For this documentation:
 4991: 
 4992:    'scanline' refers to the full line of characters
 4993:    from the file that we are parsing that represents one entire sheet
 4994: 
 4995:    'bubble line' refers to the data
 4996:    representing the line of bubbles that are on the physical bubble sheet
 4997: 
 4998: 
 4999: The overall process is that a scanned in bubble sheet data is uploaded
 5000: into a course. When a user wants to grade, they select a
 5001: sequence/folder of resources, a file of bubble sheet info, and pick
 5002: one of the predefined configurations for what each scanline looks
 5003: like.
 5004: 
 5005: Next each scanline is checked for any errors of either 'missing
 5006: bubbles' (it's an error because it may have been mis-scanned
 5007: because too light bubbling), 'double bubble' (each bubble line should
 5008: have no more that one letter picked), invalid or duplicated CODE,
 5009: invalid student/employee ID
 5010: 
 5011: If the CODE option is used that determines the randomization of the
 5012: homework problems, either way the student/employee ID is looked up into a
 5013: username:domain.
 5014: 
 5015: During the validation phase the instructor can choose to skip scanlines. 
 5016: 
 5017: After the validation phase, there are now 3 bubble sheet files
 5018: 
 5019:   scantron_original_filename (unmodified original file)
 5020:   scantron_corrected_filename (file where the corrected information has replaced the original information)
 5021:   scantron_skipped_filename (contains the exact text of scanlines that where skipped)
 5022: 
 5023: Also there is a separate hash nohist_scantrondata that contains extra
 5024: correction information that isn't representable in the bubble sheet
 5025: file (see &scantron_getfile() for more information)
 5026: 
 5027: After all scanlines are either valid, marked as valid or skipped, then
 5028: foreach line foreach problem in the picked sequence, an ssi request is
 5029: made that simulates a user submitting their selected letter(s) against
 5030: the homework problem.
 5031: 
 5032: =over 4
 5033: 
 5034: 
 5035: 
 5036: =item defaultFormData
 5037: 
 5038:   Returns html hidden inputs used to hold context/default values.
 5039: 
 5040:  Arguments:
 5041:   $symb - $symb of the current resource 
 5042: 
 5043: =cut
 5044: 
 5045: sub defaultFormData {
 5046:     my ($symb)=@_;
 5047:     return '<input type="hidden" name="symb"    value="'.&Apache::lonenc::check_encrypt($symb).'" />'."\n".
 5048:      '<input type="hidden" name="saveState" value="'.$env{'form.saveState'}.'" />'."\n".
 5049:      '<input type="hidden" name="probTitle" value="'.$env{'form.probTitle'}.'" />'."\n";
 5050: }
 5051: 
 5052: 
 5053: =pod 
 5054: 
 5055: =item getSequenceDropDown
 5056: 
 5057:    Return html dropdown of possible sequences to grade
 5058:  
 5059:  Arguments:
 5060:    $symb - $symb of the current resource
 5061:    $map_error - ref to scalar which will container error if
 5062:                 $navmap object is unavailable in &getSymbMap().
 5063: 
 5064: =cut
 5065: 
 5066: sub getSequenceDropDown {
 5067:     my ($symb,$map_error)=@_;
 5068:     my $result='<select name="selectpage">'."\n";
 5069:     my ($titles,$symbx) = &getSymbMap($map_error);
 5070:     if (ref($map_error)) {
 5071:         return if ($$map_error);
 5072:     }
 5073:     my ($curpage)=&Apache::lonnet::decode_symb($symb); 
 5074:     my $ctr=0;
 5075:     foreach (@$titles) {
 5076: 	my ($minder,$showtitle) = ($_ =~ /(\d+)\.(.*)/);
 5077: 	$result.='<option value="'.$$symbx{$_}.'" '.
 5078: 	    ($$symbx{$_} =~ /$curpage$/ ? 'selected="selected"' : '').
 5079: 	    '>'.$showtitle.'</option>'."\n";
 5080: 	$ctr++;
 5081:     }
 5082:     $result.= '</select>';
 5083:     return $result;
 5084: }
 5085: 
 5086: my %bubble_lines_per_response;     # no. bubble lines for each response.
 5087:                                    # key is zero-based index - 0, 1, 2 ...
 5088: 
 5089: my %first_bubble_line;             # First bubble line no. for each bubble.
 5090: 
 5091: my %subdivided_bubble_lines;       # no. bubble lines for optionresponse, 
 5092:                                    # matchresponse or rankresponse, where 
 5093:                                    # an individual response can have multiple 
 5094:                                    # lines
 5095: 
 5096: my %responsetype_per_response;     # responsetype for each response
 5097: 
 5098: # Save and restore the bubble lines array to the form env.
 5099: 
 5100: 
 5101: sub save_bubble_lines {
 5102:     foreach my $line (keys(%bubble_lines_per_response)) {
 5103: 	$env{"form.scantron.bubblelines.$line"}  = $bubble_lines_per_response{$line};
 5104: 	$env{"form.scantron.first_bubble_line.$line"} =
 5105: 	    $first_bubble_line{$line};
 5106:         $env{"form.scantron.sub_bubblelines.$line"} = 
 5107:             $subdivided_bubble_lines{$line};
 5108:         $env{"form.scantron.responsetype.$line"} =
 5109:             $responsetype_per_response{$line};
 5110:     }
 5111: }
 5112: 
 5113: 
 5114: sub restore_bubble_lines {
 5115:     my $line = 0;
 5116:     %bubble_lines_per_response = ();
 5117:     while ($env{"form.scantron.bubblelines.$line"}) {
 5118: 	my $value = $env{"form.scantron.bubblelines.$line"};
 5119: 	$bubble_lines_per_response{$line} = $value;
 5120: 	$first_bubble_line{$line}  =
 5121: 	    $env{"form.scantron.first_bubble_line.$line"};
 5122:         $subdivided_bubble_lines{$line} =
 5123:             $env{"form.scantron.sub_bubblelines.$line"};
 5124:         $responsetype_per_response{$line} =
 5125:             $env{"form.scantron.responsetype.$line"};
 5126: 	$line++;
 5127:     }
 5128: }
 5129: 
 5130: #  Given the parsed scanline, get the response for 
 5131: #  'answer' number n:
 5132: 
 5133: sub get_response_bubbles {
 5134:     my ($parsed_line, $response)  = @_;
 5135: 
 5136:     my $bubble_line = $first_bubble_line{$response-1} +1;
 5137:     my $bubble_lines= $bubble_lines_per_response{$response-1};
 5138:     
 5139:     my $selected = "";
 5140: 
 5141:     for (my $bline = 0; $bline < $bubble_lines; $bline++) {
 5142: 	$selected .= $$parsed_line{"scantron.$bubble_line.answer"}.":";
 5143: 	$bubble_line++;
 5144:     }
 5145:     return $selected;
 5146: }
 5147: 
 5148: =pod 
 5149: 
 5150: =item scantron_filenames
 5151: 
 5152:    Returns a list of the scantron files in the current course 
 5153: 
 5154: =cut
 5155: 
 5156: sub scantron_filenames {
 5157:     my $cdom=$env{'course.'.$env{'request.course.id'}.'.domain'};
 5158:     my $cname=$env{'course.'.$env{'request.course.id'}.'.num'};
 5159:     my $getpropath = 1;
 5160:     my @files=&Apache::lonnet::dirlist('userfiles',$cdom,$cname,
 5161:                                        $getpropath);
 5162:     my @possiblenames;
 5163:     foreach my $filename (sort(@files)) {
 5164: 	($filename)=split(/&/,$filename);
 5165: 	if ($filename!~/^scantron_orig_/) { next ; }
 5166: 	$filename=~s/^scantron_orig_//;
 5167: 	push(@possiblenames,$filename);
 5168:     }
 5169:     return @possiblenames;
 5170: }
 5171: 
 5172: =pod 
 5173: 
 5174: =item scantron_uploads
 5175: 
 5176:    Returns  html drop-down list of scantron files in current course.
 5177: 
 5178:  Arguments:
 5179:    $file2grade - filename to set as selected in the dropdown
 5180: 
 5181: =cut
 5182: 
 5183: sub scantron_uploads {
 5184:     my ($file2grade) = @_;
 5185:     my $result=	'<select name="scantron_selectfile">';
 5186:     $result.="<option></option>";
 5187:     foreach my $filename (sort(&scantron_filenames())) {
 5188: 	$result.="<option".($filename eq $file2grade ? ' selected="selected"':'').">$filename</option>\n";
 5189:     }
 5190:     $result.="</select>";
 5191:     return $result;
 5192: }
 5193: 
 5194: =pod 
 5195: 
 5196: =item scantron_scantab
 5197: 
 5198:   Returns html drop down of the scantron formats in the scantronformat.tab
 5199:   file.
 5200: 
 5201: =cut
 5202: 
 5203: sub scantron_scantab {
 5204:     my $result='<select name="scantron_format">'."\n";
 5205:     $result.='<option></option>'."\n";
 5206:     my @lines = &get_scantronformat_file();
 5207:     if (@lines > 0) {
 5208:         foreach my $line (@lines) {
 5209:             next if (($line =~ /^\#/) || ($line eq ''));
 5210: 	    my ($name,$descrip)=split(/:/,$line);
 5211: 	    $result.='<option value="'.$name.'">'.$descrip.'</option>'."\n";
 5212:         }
 5213:     }
 5214:     $result.='</select>'."\n";
 5215:     return $result;
 5216: }
 5217: 
 5218: =pod
 5219: 
 5220: =item get_scantronformat_file
 5221: 
 5222:   Returns an array containing lines from the scantron format file for
 5223:   the domain of the course.
 5224: 
 5225:   If a url for a custom.tab file is listed in domain's configuration.db, 
 5226:   lines are from this file.
 5227: 
 5228:   Otherwise, if a default.tab has been published in RES space by the 
 5229:   domainconfig user, lines are from this file.
 5230: 
 5231:   Otherwise, fall back to getting lines from the legacy file on the
 5232:   local server:  /home/httpd/lonTabs/default_scantronformat.tab    
 5233: 
 5234: =cut
 5235: 
 5236: sub get_scantronformat_file {
 5237:     my $cdom= $env{'course.'.$env{'request.course.id'}.'.domain'};
 5238:     my %domconfig = &Apache::lonnet::get_dom('configuration',['scantron'],$cdom);
 5239:     my $gottab = 0;
 5240:     my @lines;
 5241:     if (ref($domconfig{'scantron'}) eq 'HASH') {
 5242:         if ($domconfig{'scantron'}{'scantronformat'} ne '') {
 5243:             my $formatfile = &Apache::lonnet::getfile($Apache::lonnet::perlvar{'lonDocRoot'}.$domconfig{'scantron'}{'scantronformat'});
 5244:             if ($formatfile ne '-1') {
 5245:                 @lines = split("\n",$formatfile,-1);
 5246:                 $gottab = 1;
 5247:             }
 5248:         }
 5249:     }
 5250:     if (!$gottab) {
 5251:         my $confname = $cdom.'-domainconfig';
 5252:         my $default = $Apache::lonnet::perlvar{'lonDocRoot'}.'/res/'.$cdom.'/'.$confname.'/default.tab';
 5253:         my $formatfile =  &Apache::lonnet::getfile($default);
 5254:         if ($formatfile ne '-1') {
 5255:             @lines = split("\n",$formatfile,-1);
 5256:             $gottab = 1;
 5257:         }
 5258:     }
 5259:     if (!$gottab) {
 5260:         my @domains = &Apache::lonnet::current_machine_domains();
 5261:         if (grep(/^\Q$cdom\E$/,@domains)) {
 5262:             my $fh=Apache::File->new($Apache::lonnet::perlvar{'lonTabDir'}.'/scantronformat.tab');
 5263:             @lines = <$fh>;
 5264:             close($fh);
 5265:         } else {
 5266:             my $fh=Apache::File->new($Apache::lonnet::perlvar{'lonTabDir'}.'/default_scantronformat.tab');
 5267:             @lines = <$fh>;
 5268:             close($fh);
 5269:         }
 5270:     }
 5271:     return @lines;
 5272: }
 5273: 
 5274: =pod 
 5275: 
 5276: =item scantron_CODElist
 5277: 
 5278:   Returns html drop down of the saved CODE lists from current course,
 5279:   generated from earlier printings.
 5280: 
 5281: =cut
 5282: 
 5283: sub scantron_CODElist {
 5284:     my $cdom = $env{'course.'.$env{'request.course.id'}.'.domain'};
 5285:     my $cnum = $env{'course.'.$env{'request.course.id'}.'.num'};
 5286:     my @names=&Apache::lonnet::getkeys('CODEs',$cdom,$cnum);
 5287:     my $namechoice='<option></option>';
 5288:     foreach my $name (sort {uc($a) cmp uc($b)} @names) {
 5289: 	if ($name =~ /^error: 2 /) { next; }
 5290: 	if ($name =~ /^type\0/) { next; }
 5291: 	$namechoice.='<option value="'.$name.'">'.$name.'</option>';
 5292:     }
 5293:     $namechoice='<select name="scantron_CODElist">'.$namechoice.'</select>';
 5294:     return $namechoice;
 5295: }
 5296: 
 5297: =pod 
 5298: 
 5299: =item scantron_CODEunique
 5300: 
 5301:   Returns the html for "Each CODE to be used once" radio.
 5302: 
 5303: =cut
 5304: 
 5305: sub scantron_CODEunique {
 5306:     my $result='<span class="LC_nobreak">
 5307:                  <label><input type="radio" name="scantron_CODEunique"
 5308:                         value="yes" checked="checked" />'.&mt('Yes').' </label>
 5309:                 </span>
 5310:                 <span class="LC_nobreak">
 5311:                  <label><input type="radio" name="scantron_CODEunique"
 5312:                         value="no" />'.&mt('No').' </label>
 5313:                 </span>';
 5314:     return $result;
 5315: }
 5316: 
 5317: =pod 
 5318: 
 5319: =item scantron_selectphase
 5320: 
 5321:   Generates the initial screen to start the bubble sheet process.
 5322:   Allows for - starting a grading run.
 5323:              - downloading existing scan data (original, corrected
 5324:                                                 or skipped info)
 5325: 
 5326:              - uploading new scan data
 5327: 
 5328:  Arguments:
 5329:   $r          - The Apache request object
 5330:   $file2grade - name of the file that contain the scanned data to score
 5331: 
 5332: =cut
 5333: 
 5334: sub scantron_selectphase {
 5335:     my ($r,$file2grade) = @_;
 5336:     my ($symb)=&get_symb($r);
 5337:     if (!$symb) {return '';}
 5338:     my $map_error;
 5339:     my $sequence_selector=&getSequenceDropDown($symb,\$map_error);
 5340:     if ($map_error) {
 5341:         $r->print('<br />'.&navmap_errormsg().'<br />');
 5342:         return;
 5343:     }
 5344:     my $default_form_data=&defaultFormData($symb);
 5345:     my $grading_menu_button=&show_grading_menu_form($symb);
 5346:     my $file_selector=&scantron_uploads($file2grade);
 5347:     my $format_selector=&scantron_scantab();
 5348:     my $CODE_selector=&scantron_CODElist();
 5349:     my $CODE_unique=&scantron_CODEunique();
 5350:     my $result;
 5351: 
 5352:     $ssi_error = 0;
 5353: 
 5354:     if (&Apache::lonnet::allowed('usc',$env{'request.role.domain'}) ||
 5355:         &Apache::lonnet::allowed('usc',$env{'request.course.id'})) {
 5356: 
 5357:         # Chunk of form to prompt for a scantron file upload.
 5358: 
 5359:         $r->print('
 5360:     <br />
 5361:     '.&Apache::loncommon::start_data_table('LC_scantron_action').'
 5362:        '.&Apache::loncommon::start_data_table_header_row().'
 5363:             <th>
 5364:               &nbsp;'.&mt('Specify a bubblesheet data file to upload.').'
 5365:             </th>
 5366:        '.&Apache::loncommon::end_data_table_header_row().'
 5367:        '.&Apache::loncommon::start_data_table_row().'
 5368:             <td>
 5369: ');
 5370:     my $default_form_data=&defaultFormData(&get_symb($r,1));
 5371:     my $cdom= $env{'course.'.$env{'request.course.id'}.'.domain'};
 5372:     my $cnum= $env{'course.'.$env{'request.course.id'}.'.num'};
 5373:     $r->print('
 5374:               <script type="text/javascript" language="javascript">
 5375:     function checkUpload(formname) {
 5376:         if (formname.upfile.value == "") {
 5377:             alert("'.&mt('Please use the browse button to select a file from your local directory.').'");
 5378:             return false;
 5379:         }
 5380:         formname.submit();
 5381:     }
 5382:               </script>
 5383: 
 5384:               <form enctype="multipart/form-data" action="/adm/grades" name="rules" method="post">
 5385:                 '.$default_form_data.'
 5386:                 <input name="courseid" type="hidden" value="'.$cnum.'" />
 5387:                 <input name="domainid" type="hidden" value="'.$cdom.'" />
 5388:                 <input name="command" value="scantronupload_save" type="hidden" />
 5389:                 '.&mt('File to upload: [_1]','<input type="file" name="upfile" size="50" />').'
 5390:                 <br />
 5391:                 <input type="button" onclick="javascript:checkUpload(this.form);" value="'.&mt('Upload Bubblesheet Data').'" />
 5392:               </form>
 5393: ');
 5394: 
 5395:         $r->print('
 5396:             </td>
 5397:        '.&Apache::loncommon::end_data_table_row().'
 5398:        '.&Apache::loncommon::end_data_table().'
 5399: ');
 5400:     }
 5401: 
 5402:     # Chunk of form to prompt for a file to grade and how:
 5403: 
 5404:     $result.= '
 5405:     <br />
 5406:     <form method="post" enctype="multipart/form-data" action="/adm/grades" name="scantron_process">
 5407:     <input type="hidden" name="command" value="scantron_warning" />
 5408:     '.$default_form_data.'
 5409:     '.&Apache::loncommon::start_data_table('LC_scantron_action').'
 5410:        '.&Apache::loncommon::start_data_table_header_row().'
 5411:             <th colspan="2">
 5412:               &nbsp;'.&mt('Specify file and which Folder/Sequence to grade').'
 5413:             </th>
 5414:        '.&Apache::loncommon::end_data_table_header_row().'
 5415:        '.&Apache::loncommon::start_data_table_row().'
 5416:             <td> '.&mt('Sequence to grade:').' </td><td> '.$sequence_selector.' </td>
 5417:        '.&Apache::loncommon::end_data_table_row().'
 5418:        '.&Apache::loncommon::start_data_table_row().'
 5419:             <td> '.&mt('Filename of bubblesheet data file:').' </td><td> '.$file_selector.' </td>
 5420:        '.&Apache::loncommon::end_data_table_row().'
 5421:        '.&Apache::loncommon::start_data_table_row().'
 5422:             <td> '.&mt('Format of bubblesheet data file:').' </td><td> '.$format_selector.' </td>
 5423:        '.&Apache::loncommon::end_data_table_row().'
 5424:        '.&Apache::loncommon::start_data_table_row().'
 5425:             <td> '.&mt('Saved CODEs to validate against:').' </td><td> '.$CODE_selector.' </td>
 5426:        '.&Apache::loncommon::end_data_table_row().'
 5427:        '.&Apache::loncommon::start_data_table_row().'
 5428:             <td> '.&mt('Each CODE is only to be used once:').'</td><td> '.$CODE_unique.' </td>
 5429:        '.&Apache::loncommon::end_data_table_row().'
 5430:        '.&Apache::loncommon::start_data_table_row().'
 5431: 	    <td> '.&mt('Options:').' </td>
 5432:             <td>
 5433: 	       <label><input type="checkbox" name="scantron_options_redo" value="redo_skipped"/> '.&mt('Do only previously skipped records').'</label> <br />
 5434:                <label><input type="checkbox" name="scantron_options_ignore" value="ignore_corrections"/> '.&mt('Remove all existing corrections').'</label> <br />
 5435:                <label><input type="checkbox" name="scantron_options_hidden" value="ignore_hidden"/> '.&mt('Skip hidden resources when grading').'</label>
 5436: 	    </td>
 5437:        '.&Apache::loncommon::end_data_table_row().'
 5438:        '.&Apache::loncommon::start_data_table_row().'
 5439:             <td colspan="2">
 5440:               <input type="submit" value="'.&mt('Grading: Validate Bubblesheet Records').'" />
 5441:             </td>
 5442:        '.&Apache::loncommon::end_data_table_row().'
 5443:     '.&Apache::loncommon::end_data_table().'
 5444:     </form>
 5445: ';
 5446:    
 5447:     $r->print($result);
 5448: 
 5449:     # Chunk of the form that prompts to view a scoring office file,
 5450:     # corrected file, skipped records in a file.
 5451: 
 5452:     $r->print('
 5453:    <br />
 5454:    <form action="/adm/grades" name="scantron_download">
 5455:      '.$default_form_data.'
 5456:      <input type="hidden" name="command" value="scantron_download" />
 5457:      '.&Apache::loncommon::start_data_table('LC_scantron_action').'
 5458:        '.&Apache::loncommon::start_data_table_header_row().'
 5459:               <th>
 5460:                 &nbsp;'.&mt('Download a scoring office file').'
 5461:               </th>
 5462:        '.&Apache::loncommon::end_data_table_header_row().'
 5463:        '.&Apache::loncommon::start_data_table_row().'
 5464:               <td> '.&mt('Filename of scoring office file: [_1]',$file_selector).' 
 5465:                 <br />
 5466:                 <input type="submit" value="'.&mt('Download: Show List of Associated Files').'" />
 5467:        '.&Apache::loncommon::end_data_table_row().'
 5468:      '.&Apache::loncommon::end_data_table().'
 5469:    </form>
 5470:    <br />
 5471: ');
 5472: 
 5473:     &Apache::lonpickcode::code_list($r,2);
 5474: 
 5475:     $r->print('<br /><form method="post" name="checkscantron">'.
 5476:              $default_form_data."\n".
 5477:              &Apache::loncommon::start_data_table('LC_scantron_action')."\n".
 5478:              &Apache::loncommon::start_data_table_header_row()."\n".
 5479:              '<th colspan="2">
 5480:               &nbsp;'.&mt('Review bubblesheet data and submissions for a previously graded folder/sequence')."\n".
 5481:              '</th>'."\n".
 5482:               &Apache::loncommon::end_data_table_header_row()."\n".
 5483:               &Apache::loncommon::start_data_table_row()."\n".
 5484:               '<td> '.&mt('Graded folder/sequence:').' </td>'."\n".
 5485:               '<td> '.$sequence_selector.' </td>'.
 5486:               &Apache::loncommon::end_data_table_row()."\n".
 5487:               &Apache::loncommon::start_data_table_row()."\n".
 5488:               '<td> '.&mt('Filename of scoring office file:').' </td>'."\n".
 5489:               '<td> '.$file_selector.' </td>'."\n".
 5490:               &Apache::loncommon::end_data_table_row()."\n".
 5491:               &Apache::loncommon::start_data_table_row()."\n".
 5492:               '<td> '.&mt('Format of data file:').' </td>'."\n".
 5493:               '<td> '.$format_selector.' </td>'."\n".
 5494:               &Apache::loncommon::end_data_table_row()."\n".
 5495:               &Apache::loncommon::start_data_table_row()."\n".
 5496:               '<td> '.&mt('Options').' </td>'."\n".
 5497:               '<td> <label><input type="checkbox" name="scantron_options_hidden" value="ignore_hidden"/> '.&mt('Skip hidden resources').'</label></td>'.
 5498:               &Apache::loncommon::end_data_table_row()."\n".
 5499:               &Apache::loncommon::start_data_table_row()."\n".
 5500:               '<td colspan="2">'."\n".
 5501:               '<input type="hidden" name="command" value="checksubmissions" />'."\n".
 5502:               '<input type="submit" value="'.&mt('Review Bubblesheet Data and Submission Records').'" />'."\n".
 5503:               '</td>'."\n".
 5504:               &Apache::loncommon::end_data_table_row()."\n".
 5505:               &Apache::loncommon::end_data_table()."\n".
 5506:               '</form><br />');
 5507:     $r->print($grading_menu_button);
 5508:     return;
 5509: }
 5510: 
 5511: =pod
 5512: 
 5513: =item get_scantron_config
 5514: 
 5515:    Parse and return the scantron configuration line selected as a
 5516:    hash of configuration file fields.
 5517: 
 5518:  Arguments:
 5519:     which - the name of the configuration to parse from the file.
 5520: 
 5521: 
 5522:  Returns:
 5523:             If the named configuration is not in the file, an empty
 5524:             hash is returned.
 5525:     a hash with the fields
 5526:       name         - internal name for the this configuration setup
 5527:       description  - text to display to operator that describes this config
 5528:       CODElocation - if 0 or the string 'none'
 5529:                           - no CODE exists for this config
 5530:                      if -1 || the string 'letter'
 5531:                           - a CODE exists for this config and is
 5532:                             a string of letters
 5533:                      Unsupported value (but planned for future support)
 5534:                           if a positive integer
 5535:                                - The CODE exists as the first n items from
 5536:                                  the question section of the form
 5537:                           if the string 'number'
 5538:                                - The CODE exists for this config and is
 5539:                                  a string of numbers
 5540:       CODEstart   - (only matter if a CODE exists) column in the line where
 5541:                      the CODE starts
 5542:       CODElength  - length of the CODE
 5543:       IDstart     - column where the student/employee ID starts
 5544:       IDlength    - length of the student/employee ID info
 5545:       Qstart      - column where the information from the bubbled
 5546:                     'questions' start
 5547:       Qlength     - number of columns comprising a single bubble line from
 5548:                     the sheet. (usually either 1 or 10)
 5549:       Qon         - either a single character representing the character used
 5550:                     to signal a bubble was chosen in the positional setup, or
 5551:                     the string 'letter' if the letter of the chosen bubble is
 5552:                     in the final, or 'number' if a number representing the
 5553:                     chosen bubble is in the file (1->A 0->J)
 5554:       Qoff        - the character used to represent that a bubble was
 5555:                     left blank
 5556:       PaperID     - if the scanning process generates a unique number for each
 5557:                     sheet scanned the column that this ID number starts in
 5558:       PaperIDlength - number of columns that comprise the unique ID number
 5559:                       for the sheet of paper
 5560:       FirstName   - column that the first name starts in
 5561:       FirstNameLength - number of columns that the first name spans
 5562:  
 5563:       LastName    - column that the last name starts in
 5564:       LastNameLength - number of columns that the last name spans
 5565: 
 5566: =cut
 5567: 
 5568: sub get_scantron_config {
 5569:     my ($which) = @_;
 5570:     my @lines = &get_scantronformat_file();
 5571:     my %config;
 5572:     #FIXME probably should move to XML it has already gotten a bit much now
 5573:     foreach my $line (@lines) {
 5574: 	my ($name,$descrip)=split(/:/,$line);
 5575: 	if ($name ne $which ) { next; }
 5576: 	chomp($line);
 5577: 	my @config=split(/:/,$line);
 5578: 	$config{'name'}=$config[0];
 5579: 	$config{'description'}=$config[1];
 5580: 	$config{'CODElocation'}=$config[2];
 5581: 	$config{'CODEstart'}=$config[3];
 5582: 	$config{'CODElength'}=$config[4];
 5583: 	$config{'IDstart'}=$config[5];
 5584: 	$config{'IDlength'}=$config[6];
 5585: 	$config{'Qstart'}=$config[7];
 5586:  	$config{'Qlength'}=$config[8];
 5587: 	$config{'Qoff'}=$config[9];
 5588: 	$config{'Qon'}=$config[10];
 5589: 	$config{'PaperID'}=$config[11];
 5590: 	$config{'PaperIDlength'}=$config[12];
 5591: 	$config{'FirstName'}=$config[13];
 5592: 	$config{'FirstNamelength'}=$config[14];
 5593: 	$config{'LastName'}=$config[15];
 5594: 	$config{'LastNamelength'}=$config[16];
 5595: 	last;
 5596:     }
 5597:     return %config;
 5598: }
 5599: 
 5600: =pod 
 5601: 
 5602: =item username_to_idmap
 5603: 
 5604:     creates a hash keyed by student/employee ID with values of the corresponding
 5605:     student username:domain.
 5606: 
 5607:   Arguments:
 5608: 
 5609:     $classlist - reference to the class list hash. This is a hash
 5610:                  keyed by student name:domain  whose elements are references
 5611:                  to arrays containing various chunks of information
 5612:                  about the student. (See loncoursedata for more info).
 5613: 
 5614:   Returns
 5615:     %idmap - the constructed hash
 5616: 
 5617: =cut
 5618: 
 5619: sub username_to_idmap {
 5620:     my ($classlist)= @_;
 5621:     my %idmap;
 5622:     foreach my $student (keys(%$classlist)) {
 5623: 	$idmap{$classlist->{$student}->[&Apache::loncoursedata::CL_ID]}=
 5624: 	    $student;
 5625:     }
 5626:     return %idmap;
 5627: }
 5628: 
 5629: =pod
 5630: 
 5631: =item scantron_fixup_scanline
 5632: 
 5633:    Process a requested correction to a scanline.
 5634: 
 5635:   Arguments:
 5636:     $scantron_config   - hash from &get_scantron_config()
 5637:     $scan_data         - hash of correction information 
 5638:                           (see &scantron_getfile())
 5639:     $line              - existing scanline
 5640:     $whichline         - line number of the passed in scanline
 5641:     $field             - type of change to process 
 5642:                          (either 
 5643:                           'ID'     -> correct the student/employee ID
 5644:                           'CODE'   -> correct the CODE
 5645:                           'answer' -> fixup the submitted answers)
 5646:     
 5647:    $args               - hash of additional info,
 5648:                           - 'ID' 
 5649:                                'newid' -> studentID to use in replacement
 5650:                                           of existing one
 5651:                           - 'CODE' 
 5652:                                'CODE_ignore_dup' - set to true if duplicates
 5653:                                                    should be ignored.
 5654: 	                       'CODE' - is new code or 'use_unfound'
 5655:                                         if the existing unfound code should
 5656:                                         be used as is
 5657:                           - 'answer'
 5658:                                'response' - new answer or 'none' if blank
 5659:                                'question' - the bubble line to change
 5660:                                'questionnum' - the question identifier,
 5661:                                                may include subquestion. 
 5662: 
 5663:   Returns:
 5664:     $line - the modified scanline
 5665: 
 5666:   Side effects: 
 5667:     $scan_data - may be updated
 5668: 
 5669: =cut
 5670: 
 5671: 
 5672: sub scantron_fixup_scanline {
 5673:     my ($scantron_config,$scan_data,$line,$whichline,$field,$args)=@_;
 5674:     if ($field eq 'ID') {
 5675: 	if (length($args->{'newid'}) > $$scantron_config{'IDlength'}) {
 5676: 	    return ($line,1,'New value too large');
 5677: 	}
 5678: 	if (length($args->{'newid'}) < $$scantron_config{'IDlength'}) {
 5679: 	    $args->{'newid'}=sprintf('%-'.$$scantron_config{'IDlength'}.'s',
 5680: 				     $args->{'newid'});
 5681: 	}
 5682: 	substr($line,$$scantron_config{'IDstart'}-1,
 5683: 	       $$scantron_config{'IDlength'})=$args->{'newid'};
 5684: 	if ($args->{'newid'}=~/^\s*$/) {
 5685: 	    &scan_data($scan_data,"$whichline.user",
 5686: 		       $args->{'username'}.':'.$args->{'domain'});
 5687: 	}
 5688:     } elsif ($field eq 'CODE') {
 5689: 	if ($args->{'CODE_ignore_dup'}) {
 5690: 	    &scan_data($scan_data,"$whichline.CODE_ignore_dup",'1');
 5691: 	}
 5692: 	&scan_data($scan_data,"$whichline.useCODE",'1');
 5693: 	if ($args->{'CODE'} ne 'use_unfound') {
 5694: 	    if (length($args->{'CODE'}) > $$scantron_config{'CODElength'}) {
 5695: 		return ($line,1,'New CODE value too large');
 5696: 	    }
 5697: 	    if (length($args->{'CODE'}) < $$scantron_config{'CODElength'}) {
 5698: 		$args->{'CODE'}=sprintf('%-'.$$scantron_config{'CODElength'}.'s',$args->{'CODE'});
 5699: 	    }
 5700: 	    substr($line,$$scantron_config{'CODEstart'}-1,
 5701: 		   $$scantron_config{'CODElength'})=$args->{'CODE'};
 5702: 	}
 5703:     } elsif ($field eq 'answer') {
 5704: 	my $length=$scantron_config->{'Qlength'};
 5705: 	my $off=$scantron_config->{'Qoff'};
 5706: 	my $on=$scantron_config->{'Qon'};
 5707: 	my $answer=${off}x$length;
 5708: 	if ($args->{'response'} eq 'none') {
 5709: 	    &scan_data($scan_data,
 5710: 		       "$whichline.no_bubble.".$args->{'questionnum'},'1');
 5711: 	} else {
 5712: 	    if ($on eq 'letter') {
 5713: 		my @alphabet=('A'..'Z');
 5714: 		$answer=$alphabet[$args->{'response'}];
 5715: 	    } elsif ($on eq 'number') {
 5716: 		$answer=$args->{'response'}+1;
 5717: 		if ($answer == 10) { $answer = '0'; }
 5718: 	    } else {
 5719: 		substr($answer,$args->{'response'},1)=$on;
 5720: 	    }
 5721: 	    &scan_data($scan_data,
 5722: 		       "$whichline.no_bubble.".$args->{'questionnum'},undef,'1');
 5723: 	}
 5724: 	my $where=$length*($args->{'question'}-1)+$scantron_config->{'Qstart'};
 5725: 	substr($line,$where-1,$length)=$answer;
 5726:     }
 5727:     return $line;
 5728: }
 5729: 
 5730: =pod
 5731: 
 5732: =item scan_data
 5733: 
 5734:     Edit or look up  an item in the scan_data hash.
 5735: 
 5736:   Arguments:
 5737:     $scan_data  - The hash (see scantron_getfile)
 5738:     $key        - shorthand of the key to edit (actual key is
 5739:                   scantronfilename_key).
 5740:     $data        - New value of the hash entry.
 5741:     $delete      - If true, the entry is removed from the hash.
 5742: 
 5743:   Returns:
 5744:     The new value of the hash table field (undefined if deleted).
 5745: 
 5746: =cut
 5747: 
 5748: 
 5749: sub scan_data {
 5750:     my ($scan_data,$key,$value,$delete)=@_;
 5751:     my $filename=$env{'form.scantron_selectfile'};
 5752:     if (defined($value)) {
 5753: 	$scan_data->{$filename.'_'.$key} = $value;
 5754:     }
 5755:     if ($delete) { delete($scan_data->{$filename.'_'.$key}); }
 5756:     return $scan_data->{$filename.'_'.$key};
 5757: }
 5758: 
 5759: # ----- These first few routines are general use routines.----
 5760: 
 5761: # Return the number of occurences of a pattern in a string.
 5762: 
 5763: sub occurence_count {
 5764:     my ($string, $pattern) = @_;
 5765: 
 5766:     my @matches = ($string =~ /$pattern/g);
 5767: 
 5768:     return scalar(@matches);
 5769: }
 5770: 
 5771: 
 5772: # Take a string known to have digits and convert all the
 5773: # digits into letters in the range J,A..I.
 5774: 
 5775: sub digits_to_letters {
 5776:     my ($input) = @_;
 5777: 
 5778:     my @alphabet = ('J', 'A'..'I');
 5779: 
 5780:     my @input    = split(//, $input);
 5781:     my $output ='';
 5782:     for (my $i = 0; $i < scalar(@input); $i++) {
 5783: 	if ($input[$i] =~ /\d/) {
 5784: 	    $output .= $alphabet[$input[$i]];
 5785: 	} else {
 5786: 	    $output .= $input[$i];
 5787: 	}
 5788:     }
 5789:     return $output;
 5790: }
 5791: 
 5792: =pod 
 5793: 
 5794: =item scantron_parse_scanline
 5795: 
 5796:   Decodes a scanline from the selected scantron file
 5797: 
 5798:  Arguments:
 5799:     line             - The text of the scantron file line to process
 5800:     whichline        - Line number
 5801:     scantron_config  - Hash describing the format of the scantron lines.
 5802:     scan_data        - Hash of extra information about the scanline
 5803:                        (see scantron_getfile for more information)
 5804:     just_header      - True if should not process question answers but only
 5805:                        the stuff to the left of the answers.
 5806:  Returns:
 5807:    Hash containing the result of parsing the scanline
 5808: 
 5809:    Keys are all proceeded by the string 'scantron.'
 5810: 
 5811:        CODE    - the CODE in use for this scanline
 5812:        useCODE - 1 if the CODE is invalid but it usage has been forced
 5813:                  by the operator
 5814:        CODE_ignore_dup - 1 if the CODE is a duplicated use when unique
 5815:                             CODEs were selected, but the usage has been
 5816:                             forced by the operator
 5817:        ID  - student/employee ID
 5818:        PaperID - if used, the ID number printed on the sheet when the 
 5819:                  paper was scanned
 5820:        FirstName - first name from the sheet
 5821:        LastName  - last name from the sheet
 5822: 
 5823:      if just_header was not true these key may also exist
 5824: 
 5825:        missingerror - a list of bubble ranges that are considered to be answers
 5826:                       to a single question that don't have any bubbles filled in.
 5827:                       Of the form questionnumber:firstbubblenumber:count.
 5828:        doubleerror  - a list of bubble ranges that are considered to be answers
 5829:                       to a single question that have more than one bubble filled in.
 5830:                       Of the form questionnumber::firstbubblenumber:count
 5831:    
 5832:                 In the above, count is the number of bubble responses in the
 5833:                 input line needed to represent the possible answers to the question.
 5834:                 e.g. a radioresponse with 15 choices in an answer sheet with 10 choices
 5835:                 per line would have count = 2.
 5836: 
 5837:        maxquest     - the number of the last bubble line that was parsed
 5838: 
 5839:        (<number> starts at 1)
 5840:        <number>.answer - zero or more letters representing the selected
 5841:                          letters from the scanline for the bubble line 
 5842:                          <number>.
 5843:                          if blank there was either no bubble or there where
 5844:                          multiple bubbles, (consult the keys missingerror and
 5845:                          doubleerror if this is an error condition)
 5846: 
 5847: =cut
 5848: 
 5849: sub scantron_parse_scanline {
 5850:     my ($line,$whichline,$scantron_config,$scan_data,$just_header)=@_;
 5851: 
 5852:     my %record;
 5853:     my $lastpos = $env{'form.scantron_maxbubble'}*$$scantron_config{'Qlength'};
 5854:     my $questions=substr($line,$$scantron_config{'Qstart'}-1,$lastpos);  # Answers
 5855:     my $data=substr($line,0,$$scantron_config{'Qstart'}-1);     # earlier stuff
 5856:     if (!($$scantron_config{'CODElocation'} eq 0 ||
 5857: 	  $$scantron_config{'CODElocation'} eq 'none')) {
 5858: 	if ($$scantron_config{'CODElocation'} < 0 ||
 5859: 	    $$scantron_config{'CODElocation'} eq 'letter' ||
 5860: 	    $$scantron_config{'CODElocation'} eq 'number') {
 5861: 	    $record{'scantron.CODE'}=substr($data,
 5862: 					    $$scantron_config{'CODEstart'}-1,
 5863: 					    $$scantron_config{'CODElength'});
 5864: 	    if (&scan_data($scan_data,"$whichline.useCODE")) {
 5865: 		$record{'scantron.useCODE'}=1;
 5866: 	    }
 5867: 	    if (&scan_data($scan_data,"$whichline.CODE_ignore_dup")) {
 5868: 		$record{'scantron.CODE_ignore_dup'}=1;
 5869: 	    }
 5870: 	} else {
 5871: 	    #FIXME interpret first N questions
 5872: 	}
 5873:     }
 5874:     $record{'scantron.ID'}=substr($data,$$scantron_config{'IDstart'}-1,
 5875: 				  $$scantron_config{'IDlength'});
 5876:     $record{'scantron.PaperID'}=
 5877: 	substr($data,$$scantron_config{'PaperID'}-1,
 5878: 	       $$scantron_config{'PaperIDlength'});
 5879:     $record{'scantron.FirstName'}=
 5880: 	substr($data,$$scantron_config{'FirstName'}-1,
 5881: 	       $$scantron_config{'FirstNamelength'});
 5882:     $record{'scantron.LastName'}=
 5883: 	substr($data,$$scantron_config{'LastName'}-1,
 5884: 	       $$scantron_config{'LastNamelength'});
 5885:     if ($just_header) { return \%record; }
 5886: 
 5887:     my @alphabet=('A'..'Z');
 5888:     my $questnum=0;
 5889:     my $ansnum  =1;		# Multiple 'answer lines'/question.
 5890: 
 5891:     chomp($questions);		# Get rid of any trailing \n.
 5892:     $questions =~ s/\r$//;      # Get rid of trailing \r too (MAC or Win uploads).
 5893:     while (length($questions)) {
 5894: 	my $answers_needed = $bubble_lines_per_response{$questnum};
 5895:         my $answer_length  = ($$scantron_config{'Qlength'} * $answers_needed)
 5896:                              || 1;
 5897:         $questnum++;
 5898:         my $quest_id = $questnum;
 5899:         my $currentquest = substr($questions,0,$answer_length);
 5900:         $questions       = substr($questions,$answer_length);
 5901:         if (length($currentquest) < $answer_length) { next; }
 5902: 
 5903:         if ($subdivided_bubble_lines{$questnum-1} =~ /,/) {
 5904:             my $subquestnum = 1;
 5905:             my $subquestions = $currentquest;
 5906:             my @subanswers_needed = 
 5907:                 split(/,/,$subdivided_bubble_lines{$questnum-1});  
 5908:             foreach my $subans (@subanswers_needed) {
 5909:                 my $subans_length =
 5910:                     ($$scantron_config{'Qlength'} * $subans)  || 1;
 5911:                 my $currsubquest = substr($subquestions,0,$subans_length);
 5912:                 $subquestions   = substr($subquestions,$subans_length);
 5913:                 $quest_id = "$questnum.$subquestnum";
 5914:                 if (($$scantron_config{'Qon'} eq 'letter') ||
 5915:                     ($$scantron_config{'Qon'} eq 'number')) {
 5916:                     $ansnum = &scantron_validator_lettnum($ansnum, 
 5917:                         $questnum,$quest_id,$subans,$currsubquest,$whichline,
 5918:                         \@alphabet,\%record,$scantron_config,$scan_data);
 5919:                 } else {
 5920:                     $ansnum = &scantron_validator_positional($ansnum,
 5921:                         $questnum,$quest_id,$subans,$currsubquest,$whichline,                        \@alphabet,\%record,$scantron_config,$scan_data);
 5922:                 }
 5923:                 $subquestnum ++;
 5924:             }
 5925:         } else {
 5926:             if (($$scantron_config{'Qon'} eq 'letter') ||
 5927:                 ($$scantron_config{'Qon'} eq 'number')) {
 5928:                 $ansnum = &scantron_validator_lettnum($ansnum,$questnum,
 5929:                     $quest_id,$answers_needed,$currentquest,$whichline,
 5930:                     \@alphabet,\%record,$scantron_config,$scan_data);
 5931:             } else {
 5932:                 $ansnum = &scantron_validator_positional($ansnum,$questnum,
 5933:                     $quest_id,$answers_needed,$currentquest,$whichline,
 5934:                     \@alphabet,\%record,$scantron_config,$scan_data);
 5935:             }
 5936:         }
 5937:     }
 5938:     $record{'scantron.maxquest'}=$questnum;
 5939:     return \%record;
 5940: }
 5941: 
 5942: sub scantron_validator_lettnum {
 5943:     my ($ansnum,$questnum,$quest_id,$answers_needed,$currquest,$whichline,
 5944:         $alphabet,$record,$scantron_config,$scan_data) = @_;
 5945: 
 5946:     # Qon 'letter' implies for each slot in currquest we have:
 5947:     #    ? or * for doubles, a letter in A-Z for a bubble, and
 5948:     #    about anything else (esp. a value of Qoff) for missing
 5949:     #    bubbles.
 5950:     #
 5951:     # Qon 'number' implies each slot gives a digit that indexes the
 5952:     #    bubbles filled, or Qoff, or a non-number for unbubbled lines,
 5953:     #    and * or ? for double bubbles on a single line.
 5954:     #
 5955: 
 5956:     my $matchon;
 5957:     if ($$scantron_config{'Qon'} eq 'letter') {
 5958:         $matchon = '[A-Z]';
 5959:     } elsif ($$scantron_config{'Qon'} eq 'number') {
 5960:         $matchon = '\d';
 5961:     }
 5962:     my $occurrences = 0;
 5963:     if (($responsetype_per_response{$questnum-1} eq 'essayresponse') ||
 5964:         ($responsetype_per_response{$questnum-1} eq 'formularesponse') ||
 5965:         ($responsetype_per_response{$questnum-1} eq 'stringresponse') ||
 5966:         ($responsetype_per_response{$questnum-1} eq 'imageresponse') ||
 5967:         ($responsetype_per_response{$questnum-1} eq 'reactionresponse') ||
 5968:         ($responsetype_per_response{$questnum-1} eq 'organicresponse')) {
 5969:         my @singlelines = split('',$currquest);
 5970:         foreach my $entry (@singlelines) {
 5971:             $occurrences = &occurence_count($entry,$matchon);
 5972:             if ($occurrences > 1) {
 5973:                 last;
 5974:             }
 5975:         } 
 5976:     } else {
 5977:         $occurrences = &occurence_count($currquest,$matchon); 
 5978:     }
 5979:     if (($currquest =~ /\?/ || $currquest =~ /\*/) || ($occurrences > 1)) {
 5980:         push(@{$record->{'scantron.doubleerror'}},$quest_id);
 5981:         for (my $ans=0; $ans<$answers_needed; $ans++) {
 5982:             my $bubble = substr($currquest,$ans,1);
 5983:             if ($bubble =~ /$matchon/ ) {
 5984:                 if ($$scantron_config{'Qon'} eq 'number') {
 5985:                     if ($bubble == 0) {
 5986:                         $bubble = 10; 
 5987:                     }
 5988:                     $record->{"scantron.$ansnum.answer"} = 
 5989:                         $alphabet->[$bubble-1];
 5990:                 } else {
 5991:                     $record->{"scantron.$ansnum.answer"} = $bubble;
 5992:                 }
 5993:             } else {
 5994:                 $record->{"scantron.$ansnum.answer"}='';
 5995:             }
 5996:             $ansnum++;
 5997:         }
 5998:     } elsif (!defined($currquest)
 5999:             || (&occurence_count($currquest, $$scantron_config{'Qoff'}) == length($currquest))
 6000:             || (&occurence_count($currquest,$matchon) == 0)) {
 6001:         for (my $ans=0; $ans<$answers_needed; $ans++ ) {
 6002:             $record->{"scantron.$ansnum.answer"}='';
 6003:             $ansnum++;
 6004:         }
 6005:         if (!&scan_data($scan_data,"$whichline.no_bubble.$quest_id")) {
 6006:             push(@{$record->{'scantron.missingerror'}},$quest_id);
 6007:         }
 6008:     } else {
 6009:         if ($$scantron_config{'Qon'} eq 'number') {
 6010:             $currquest = &digits_to_letters($currquest);            
 6011:         }
 6012:         for (my $ans=0; $ans<$answers_needed; $ans++) {
 6013:             my $bubble = substr($currquest,$ans,1);
 6014:             $record->{"scantron.$ansnum.answer"} = $bubble;
 6015:             $ansnum++;
 6016:         }
 6017:     }
 6018:     return $ansnum;
 6019: }
 6020: 
 6021: sub scantron_validator_positional {
 6022:     my ($ansnum,$questnum,$quest_id,$answers_needed,$currquest,
 6023:         $whichline,$alphabet,$record,$scantron_config,$scan_data) = @_;
 6024: 
 6025:     # Otherwise there's a positional notation;
 6026:     # each bubble line requires Qlength items, and there are filled in
 6027:     # bubbles for each case where there 'Qon' characters.
 6028:     #
 6029: 
 6030:     my @array=split($$scantron_config{'Qon'},$currquest,-1);
 6031: 
 6032:     # If the split only gives us one element.. the full length of the
 6033:     # answer string, no bubbles are filled in:
 6034: 
 6035:     if ($answers_needed eq '') {
 6036:         return;
 6037:     }
 6038: 
 6039:     if (length($array[0]) eq $$scantron_config{'Qlength'}*$answers_needed) {
 6040:         for (my $ans=0; $ans<$answers_needed; $ans++ ) {
 6041:             $record->{"scantron.$ansnum.answer"}='';
 6042:             $ansnum++;
 6043:         }
 6044:         if (!&scan_data($scan_data,"$whichline.no_bubble.$quest_id")) {
 6045:             push(@{$record->{"scantron.missingerror"}},$quest_id);
 6046:         }
 6047:     } elsif (scalar(@array) == 2) {
 6048:         my $location = length($array[0]);
 6049:         my $line_num = int($location / $$scantron_config{'Qlength'});
 6050:         my $bubble   = $alphabet->[$location % $$scantron_config{'Qlength'}];
 6051:         for (my $ans=0; $ans<$answers_needed; $ans++) {
 6052:             if ($ans eq $line_num) {
 6053:                 $record->{"scantron.$ansnum.answer"} = $bubble;
 6054:             } else {
 6055:                 $record->{"scantron.$ansnum.answer"} = ' ';
 6056:             }
 6057:             $ansnum++;
 6058:          }
 6059:     } else {
 6060:         #  If there's more than one instance of a bubble character
 6061:         #  That's a double bubble; with positional notation we can
 6062:         #  record all the bubbles filled in as well as the
 6063:         #  fact this response consists of multiple bubbles.
 6064:         #
 6065:         if (($responsetype_per_response{$questnum-1} eq 'essayresponse') ||
 6066:             ($responsetype_per_response{$questnum-1} eq 'formularesponse') ||
 6067:             ($responsetype_per_response{$questnum-1} eq 'stringresponse') ||
 6068:             ($responsetype_per_response{$questnum-1} eq 'imageresponse') ||
 6069:             ($responsetype_per_response{$questnum-1} eq 'reactionresponse') ||
 6070:             ($responsetype_per_response{$questnum-1} eq 'organicresponse')) {
 6071:             my $doubleerror = 0;
 6072:             while (($currquest >= $$scantron_config{'Qlength'}) && 
 6073:                    (!$doubleerror)) {
 6074:                my $currline = substr($currquest,0,$$scantron_config{'Qlength'});
 6075:                $currquest = substr($currquest,$$scantron_config{'Qlength'});
 6076:                my @currarray = split($$scantron_config{'Qon'},$currline,-1);
 6077:                if (length(@currarray) > 2) {
 6078:                    $doubleerror = 1;
 6079:                } 
 6080:             }
 6081:             if ($doubleerror) {
 6082:                 push(@{$record->{'scantron.doubleerror'}},$quest_id);
 6083:             }
 6084:         } else {
 6085:             push(@{$record->{'scantron.doubleerror'}},$quest_id);
 6086:         }
 6087:         my $item = $ansnum;
 6088:         for (my $ans=0; $ans<$answers_needed; $ans++) {
 6089:             $record->{"scantron.$item.answer"} = '';
 6090:             $item ++;
 6091:         }
 6092: 
 6093:         my @ans=@array;
 6094:         my $i=0;
 6095:         my $increment = 0;
 6096:         while ($#ans) {
 6097:             $i+=length($ans[0]) + $increment;
 6098:             my $line   = int($i/$$scantron_config{'Qlength'} + $ansnum);
 6099:             my $bubble = $i%$$scantron_config{'Qlength'};
 6100:             $record->{"scantron.$line.answer"}.=$alphabet->[$bubble];
 6101:             shift(@ans);
 6102:             $increment = 1;
 6103:         }
 6104:         $ansnum += $answers_needed;
 6105:     }
 6106:     return $ansnum;
 6107: }
 6108: 
 6109: =pod
 6110: 
 6111: =item scantron_add_delay
 6112: 
 6113:    Adds an error message that occurred during the grading phase to a
 6114:    queue of messages to be shown after grading pass is complete
 6115: 
 6116:  Arguments:
 6117:    $delayqueue  - arrary ref of hash ref of error messages
 6118:    $scanline    - the scanline that caused the error
 6119:    $errormesage - the error message
 6120:    $errorcode   - a numeric code for the error
 6121: 
 6122:  Side Effects:
 6123:    updates the $delayqueue to have a new hash ref of the error
 6124: 
 6125: =cut
 6126: 
 6127: sub scantron_add_delay {
 6128:     my ($delayqueue,$scanline,$errormessage,$errorcode)=@_;
 6129:     push(@$delayqueue,
 6130: 	 {'line' => $scanline, 'emsg' => $errormessage,
 6131: 	  'ecode' => $errorcode }
 6132: 	 );
 6133: }
 6134: 
 6135: =pod
 6136: 
 6137: =item scantron_find_student
 6138: 
 6139:    Finds the username for the current scanline
 6140: 
 6141:   Arguments:
 6142:    $scantron_record - hash result from scantron_parse_scanline
 6143:    $scan_data       - hash of correction information 
 6144:                       (see &scantron_getfile() form more information)
 6145:    $idmap           - hash from &username_to_idmap()
 6146:    $line            - number of current scanline
 6147:  
 6148:   Returns:
 6149:    Either 'username:domain' or undef if unknown
 6150: 
 6151: =cut
 6152: 
 6153: sub scantron_find_student {
 6154:     my ($scantron_record,$scan_data,$idmap,$line)=@_;
 6155:     my $scanID=$$scantron_record{'scantron.ID'};
 6156:     if ($scanID =~ /^\s*$/) {
 6157:  	return &scan_data($scan_data,"$line.user");
 6158:     }
 6159:     foreach my $id (keys(%$idmap)) {
 6160:  	if (lc($id) eq lc($scanID)) {
 6161:  	    return $$idmap{$id};
 6162:  	}
 6163:     }
 6164:     return undef;
 6165: }
 6166: 
 6167: =pod
 6168: 
 6169: =item scantron_filter
 6170: 
 6171:    Filter sub for lonnavmaps, filters out hidden resources if ignore
 6172:    hidden resources was selected
 6173: 
 6174: =cut
 6175: 
 6176: sub scantron_filter {
 6177:     my ($curres)=@_;
 6178: 
 6179:     if (ref($curres) && $curres->is_problem()) {
 6180: 	# if the user has asked to not have either hidden
 6181: 	# or 'randomout' controlled resources to be graded
 6182: 	# don't include them
 6183: 	if ($env{'form.scantron_options_hidden'} eq 'ignore_hidden'
 6184: 	    && $curres->randomout) {
 6185: 	    return 0;
 6186: 	}
 6187: 	return 1;
 6188:     }
 6189:     return 0;
 6190: }
 6191: 
 6192: =pod
 6193: 
 6194: =item scantron_process_corrections
 6195: 
 6196:    Gets correction information out of submitted form data and corrects
 6197:    the scanline
 6198: 
 6199: =cut
 6200: 
 6201: sub scantron_process_corrections {
 6202:     my ($r) = @_;
 6203:     my %scantron_config=&get_scantron_config($env{'form.scantron_format'});
 6204:     my ($scanlines,$scan_data)=&scantron_getfile();
 6205:     my $classlist=&Apache::loncoursedata::get_classlist();
 6206:     my $which=$env{'form.scantron_line'};
 6207:     my $line=&scantron_get_line($scanlines,$scan_data,$which);
 6208:     my ($skip,$err,$errmsg);
 6209:     if ($env{'form.scantron_skip_record'}) {
 6210: 	$skip=1;
 6211:     } elsif ($env{'form.scantron_corrections'} =~ /^(duplicate|incorrect)ID$/) {
 6212: 	my $newstudent=$env{'form.scantron_username'}.':'.
 6213: 	    $env{'form.scantron_domain'};
 6214: 	my $newid=$classlist->{$newstudent}->[&Apache::loncoursedata::CL_ID];
 6215: 	($line,$err,$errmsg)=
 6216: 	    &scantron_fixup_scanline(\%scantron_config,$scan_data,$line,$which,
 6217: 				     'ID',{'newid'=>$newid,
 6218: 				    'username'=>$env{'form.scantron_username'},
 6219: 				    'domain'=>$env{'form.scantron_domain'}});
 6220:     } elsif ($env{'form.scantron_corrections'} =~ /^(duplicate|incorrect)CODE$/) {
 6221: 	my $resolution=$env{'form.scantron_CODE_resolution'};
 6222: 	my $newCODE;
 6223: 	my %args;
 6224: 	if      ($resolution eq 'use_unfound') {
 6225: 	    $newCODE='use_unfound';
 6226: 	} elsif ($resolution eq 'use_found') {
 6227: 	    $newCODE=$env{'form.scantron_CODE_selectedvalue'};
 6228: 	} elsif ($resolution eq 'use_typed') {
 6229: 	    $newCODE=$env{'form.scantron_CODE_newvalue'};
 6230: 	} elsif ($resolution =~ /^use_closest_(\d+)/) {
 6231: 	    $newCODE=$env{"form.scantron_CODE_closest_$1"};
 6232: 	}
 6233: 	if ($env{'form.scantron_corrections'} eq 'duplicateCODE') {
 6234: 	    $args{'CODE_ignore_dup'}=1;
 6235: 	}
 6236: 	$args{'CODE'}=$newCODE;
 6237: 	($line,$err,$errmsg)=
 6238: 	    &scantron_fixup_scanline(\%scantron_config,$scan_data,$line,$which,
 6239: 				     'CODE',\%args);
 6240:     } elsif ($env{'form.scantron_corrections'} =~ /^(missing|double)bubble$/) {
 6241: 	foreach my $question (split(',',$env{'form.scantron_questions'})) {
 6242: 	    ($line,$err,$errmsg)=
 6243: 		&scantron_fixup_scanline(\%scantron_config,$scan_data,$line,
 6244: 					 $which,'answer',
 6245: 					 { 'question'=>$question,
 6246: 		      		   'response'=>$env{"form.scantron_correct_Q_$question"},
 6247:                                    'questionnum'=>$env{"form.scantron_questionnum_Q_$question"}});
 6248: 	    if ($err) { last; }
 6249: 	}
 6250:     }
 6251:     if ($err) {
 6252: 	$r->print("<span class=\"LC_warning\">Unable to accept last correction, an error occurred :$errmsg:</span>");
 6253:     } else {
 6254: 	&scantron_put_line($scanlines,$scan_data,$which,$line,$skip);
 6255: 	&scantron_putfile($scanlines,$scan_data);
 6256:     }
 6257: }
 6258: 
 6259: =pod
 6260: 
 6261: =item reset_skipping_status
 6262: 
 6263:    Forgets the current set of remember skipped scanlines (and thus
 6264:    reverts back to considering all lines in the
 6265:    scantron_skipped_<filename> file)
 6266: 
 6267: =cut
 6268: 
 6269: sub reset_skipping_status {
 6270:     my ($scanlines,$scan_data)=&scantron_getfile();
 6271:     &scan_data($scan_data,'remember_skipping',undef,1);
 6272:     &scantron_putfile(undef,$scan_data);
 6273: }
 6274: 
 6275: =pod
 6276: 
 6277: =item start_skipping
 6278: 
 6279:    Marks a scanline to be skipped. 
 6280: 
 6281: =cut
 6282: 
 6283: sub start_skipping {
 6284:     my ($scan_data,$i)=@_;
 6285:     my %remembered=split(':',&scan_data($scan_data,'remember_skipping'));
 6286:     if ($env{'form.scantron_options_redo'} =~ /^redo_/) {
 6287: 	$remembered{$i}=2;
 6288:     } else {
 6289: 	$remembered{$i}=1;
 6290:     }
 6291:     &scan_data($scan_data,'remember_skipping',join(':',%remembered));
 6292: }
 6293: 
 6294: =pod
 6295: 
 6296: =item should_be_skipped
 6297: 
 6298:    Checks whether a scanline should be skipped.
 6299: 
 6300: =cut
 6301: 
 6302: sub should_be_skipped {
 6303:     my ($scanlines,$scan_data,$i)=@_;
 6304:     if ($env{'form.scantron_options_redo'} !~ /^redo_/) {
 6305: 	# not redoing old skips
 6306: 	if ($scanlines->{'skipped'}[$i]) { return 1; }
 6307: 	return 0;
 6308:     }
 6309:     my %remembered=split(':',&scan_data($scan_data,'remember_skipping'));
 6310: 
 6311:     if (exists($remembered{$i}) && $remembered{$i} != 2 ) {
 6312: 	return 0;
 6313:     }
 6314:     return 1;
 6315: }
 6316: 
 6317: =pod
 6318: 
 6319: =item remember_current_skipped
 6320: 
 6321:    Discovers what scanlines are in the scantron_skipped_<filename>
 6322:    file and remembers them into scan_data for later use.
 6323: 
 6324: =cut
 6325: 
 6326: sub remember_current_skipped {
 6327:     my ($scanlines,$scan_data)=&scantron_getfile();
 6328:     my %to_remember;
 6329:     for (my $i=0;$i<=$scanlines->{'count'};$i++) {
 6330: 	if ($scanlines->{'skipped'}[$i]) {
 6331: 	    $to_remember{$i}=1;
 6332: 	}
 6333:     }
 6334: 
 6335:     &scan_data($scan_data,'remember_skipping',join(':',%to_remember));
 6336:     &scantron_putfile(undef,$scan_data);
 6337: }
 6338: 
 6339: =pod
 6340: 
 6341: =item check_for_error
 6342: 
 6343:     Checks if there was an error when attempting to remove a specific
 6344:     scantron_.. bubble sheet data file. Prints out an error if
 6345:     something went wrong.
 6346: 
 6347: =cut
 6348: 
 6349: sub check_for_error {
 6350:     my ($r,$result)=@_;
 6351:     if ($result ne 'ok' && $result ne 'not_found' ) {
 6352: 	$r->print(&mt("An error occurred ([_1]) when trying to remove the existing corrections.",$result));
 6353:     }
 6354: }
 6355: 
 6356: =pod
 6357: 
 6358: =item scantron_warning_screen
 6359: 
 6360:    Interstitial screen to make sure the operator has selected the
 6361:    correct options before we start the validation phase.
 6362: 
 6363: =cut
 6364: 
 6365: sub scantron_warning_screen {
 6366:     my ($button_text)=@_;
 6367:     my $title=&Apache::lonnet::gettitle($env{'form.selectpage'});
 6368:     my %scantron_config=&get_scantron_config($env{'form.scantron_format'});
 6369:     my $CODElist;
 6370:     if ($scantron_config{'CODElocation'} &&
 6371: 	$scantron_config{'CODEstart'} &&
 6372: 	$scantron_config{'CODElength'}) {
 6373: 	$CODElist=$env{'form.scantron_CODElist'};
 6374: 	if ($env{'form.scantron_CODElist'} eq '') { $CODElist='<span class="LC_warning">None</span>'; }
 6375: 	$CODElist=
 6376: 	    '<tr><td><b>'.&mt('List of CODES to validate against:').'</b></td><td><tt>'.
 6377: 	    $env{'form.scantron_CODElist'}.'</tt></td></tr>';
 6378:     }
 6379:     return ('
 6380: <p>
 6381: <span class="LC_warning">
 6382: '.&mt('Please double check the information below before clicking on \'[_1]\'',&mt($button_text)).'</span>
 6383: </p>
 6384: <table>
 6385: <tr><td><b>'.&mt('Sequence to be Graded:').'</b></td><td>'.$title.'</td></tr>
 6386: <tr><td><b>'.&mt('Data File that will be used:').'</b></td><td><tt>'.$env{'form.scantron_selectfile'}.'</tt></td></tr>
 6387: '.$CODElist.'
 6388: </table>
 6389: <br />
 6390: <p> '.&mt('If this information is correct, please click on \'[_1]\'.',&mt($button_text)).'</p>
 6391: <p> '.&mt('If something is incorrect, please click the \'Grading Menu\' button to start over.').'</p>
 6392: 
 6393: <br />
 6394: ');
 6395: }
 6396: 
 6397: =pod
 6398: 
 6399: =item scantron_do_warning
 6400: 
 6401:    Check if the operator has picked something for all required
 6402:    fields. Error out if something is missing.
 6403: 
 6404: =cut
 6405: 
 6406: sub scantron_do_warning {
 6407:     my ($r)=@_;
 6408:     my ($symb)=&get_symb($r);
 6409:     if (!$symb) {return '';}
 6410:     my $default_form_data=&defaultFormData($symb);
 6411:     $r->print(&scantron_form_start().$default_form_data);
 6412:     if ( $env{'form.selectpage'} eq '' ||
 6413: 	 $env{'form.scantron_selectfile'} eq '' ||
 6414: 	 $env{'form.scantron_format'} eq '' ) {
 6415: 	$r->print("<p>".&mt('You have forgotten to specify some information. Please go Back and try again.')."</p>");
 6416: 	if ( $env{'form.selectpage'} eq '') {
 6417: 	    $r->print('<p><span class="LC_error">'.&mt('You have not selected a Sequence to grade').'</span></p>');
 6418: 	} 
 6419: 	if ( $env{'form.scantron_selectfile'} eq '') {
 6420: 	    $r->print('<p><span class="LC_error">'.&mt("You have not selected a file that contains the student's response data.").'</span></p>');
 6421: 	} 
 6422: 	if ( $env{'form.scantron_format'} eq '') {
 6423: 	    $r->print('<p><span class="LC_error">'.&mt(:You have not selected the format of the student's response data.").'</span></p>');
 6424: 	} 
 6425:     } else {
 6426: 	my $warning=&scantron_warning_screen('Grading: Validate Records');
 6427: 	$r->print('
 6428: '.$warning.'
 6429: <input type="submit" name="submit" value="'.&mt('Grading: Validate Records').'" />
 6430: <input type="hidden" name="command" value="scantron_validate" />
 6431: ');
 6432:     }
 6433:     $r->print("</form><br />".&show_grading_menu_form($symb));
 6434:     return '';
 6435: }
 6436: 
 6437: =pod
 6438: 
 6439: =item scantron_form_start
 6440: 
 6441:     html hidden input for remembering all selected grading options
 6442: 
 6443: =cut
 6444: 
 6445: sub scantron_form_start {
 6446:     my ($max_bubble)=@_;
 6447:     my $result= <<SCANTRONFORM;
 6448: <form method="post" enctype="multipart/form-data" action="/adm/grades" name="scantronupload">
 6449:   <input type="hidden" name="selectpage" value="$env{'form.selectpage'}" />
 6450:   <input type="hidden" name="scantron_format" value="$env{'form.scantron_format'}" />
 6451:   <input type="hidden" name="scantron_selectfile" value="$env{'form.scantron_selectfile'}" />
 6452:   <input type="hidden" name="scantron_maxbubble" value="$max_bubble" />
 6453:   <input type="hidden" name="scantron_CODElist" value="$env{'form.scantron_CODElist'}" />
 6454:   <input type="hidden" name="scantron_CODEunique" value="$env{'form.scantron_CODEunique'}" />
 6455:   <input type="hidden" name="scantron_options_redo" value="$env{'form.scantron_options_redo'}" />
 6456:   <input type="hidden" name="scantron_options_ignore" value="$env{'form.scantron_options_ignore'}" />
 6457:   <input type="hidden" name="scantron_options_hidden" value="$env{'form.scantron_options_hidden'}" />
 6458: SCANTRONFORM
 6459: 
 6460:   my $line = 0;
 6461:     while (defined($env{"form.scantron.bubblelines.$line"})) {
 6462:        my $chunk =
 6463: 	   '<input type="hidden" name="scantron.bubblelines.'.$line.'" value="'.$env{"form.scantron.bubblelines.$line"}.'" />'."\n";
 6464:        $chunk .=
 6465: 	   '<input type="hidden" name="scantron.first_bubble_line.'.$line.'" value="'.$env{"form.scantron.first_bubble_line.$line"}.'" />'."\n";
 6466:        $chunk .= 
 6467:            '<input type="hidden" name="scantron.sub_bubblelines.'.$line.'" value="'.$env{"form.scantron.sub_bubblelines.$line"}.'" />'."\n";
 6468:        $chunk .=
 6469:            '<input type="hidden" name="scantron.responsetype.'.$line.'" value="'.$env{"form.scantron.responsetype.$line"}.'" />'."\n";
 6470:        $result .= $chunk;
 6471:        $line++;
 6472:    }
 6473:     return $result;
 6474: }
 6475: 
 6476: =pod
 6477: 
 6478: =item scantron_validate_file
 6479: 
 6480:     Dispatch routine for doing validation of a bubble sheet data file.
 6481: 
 6482:     Also processes any necessary information resets that need to
 6483:     occur before validation begins (ignore previous corrections,
 6484:     restarting the skipped records processing)
 6485: 
 6486: =cut
 6487: 
 6488: sub scantron_validate_file {
 6489:     my ($r) = @_;
 6490:     my ($symb)=&get_symb($r);
 6491:     if (!$symb) {return '';}
 6492:     my $default_form_data=&defaultFormData($symb);
 6493:     
 6494:     # do the detection of only doing skipped records first befroe we delete
 6495:     # them when doing the corrections reset
 6496:     if ($env{'form.scantron_options_redo'} ne 'redo_skipped_ready') {
 6497: 	&reset_skipping_status();
 6498:     }
 6499:     if ($env{'form.scantron_options_redo'} eq 'redo_skipped') {
 6500: 	&remember_current_skipped();
 6501: 	$env{'form.scantron_options_redo'}='redo_skipped_ready';
 6502:     }
 6503: 
 6504:     if ($env{'form.scantron_options_ignore'} eq 'ignore_corrections') {
 6505: 	&check_for_error($r,&scantron_remove_file('corrected'));
 6506: 	&check_for_error($r,&scantron_remove_file('skipped'));
 6507: 	&check_for_error($r,&scantron_remove_scan_data());
 6508: 	$env{'form.scantron_options_ignore'}='done';
 6509:     }
 6510: 
 6511:     if ($env{'form.scantron_corrections'}) {
 6512: 	&scantron_process_corrections($r);
 6513:     }
 6514:     $r->print('<p>'.&mt('Gathering necessary information.').'</p>');$r->rflush();
 6515:     #get the student pick code ready
 6516:     $r->print(&Apache::loncommon::studentbrowser_javascript());
 6517:     my $nav_error;
 6518:     my $max_bubble=&scantron_get_maxbubble(\$nav_error);
 6519:     if ($nav_error) {
 6520:         $r->print(&navmap_errormsg());
 6521:         return '';
 6522:     }
 6523:     my $result=&scantron_form_start($max_bubble).$default_form_data;
 6524:     $r->print($result);
 6525:     
 6526:     my @validate_phases=( 'sequence',
 6527: 			  'ID',
 6528: 			  'CODE',
 6529: 			  'doublebubble',
 6530: 			  'missingbubbles');
 6531:     if (!$env{'form.validatepass'}) {
 6532: 	$env{'form.validatepass'} = 0;
 6533:     }
 6534:     my $currentphase=$env{'form.validatepass'};
 6535: 
 6536: 
 6537:     my $stop=0;
 6538:     while (!$stop && $currentphase < scalar(@validate_phases)) {
 6539: 	$r->print(&mt('Validating '.$validate_phases[$currentphase]).'<br />');
 6540: 	$r->rflush();
 6541: 	my $which="scantron_validate_".$validate_phases[$currentphase];
 6542: 	{
 6543: 	    no strict 'refs';
 6544: 	    ($stop,$currentphase)=&$which($r,$currentphase);
 6545: 	}
 6546:     }
 6547:     if (!$stop) {
 6548: 	my $warning=&scantron_warning_screen('Start Grading');
 6549: 	$r->print(&mt('Validation process complete.').'<br />'.
 6550:                   $warning.
 6551:                   &mt('Perform verification for each student after storage of submissions?').
 6552:                   '&nbsp;<span class="LC_nobreak"><label>'.
 6553:                   '<input type="radio" name="verifyrecord" value="1" />'.&mt('Yes').'</label>'.
 6554:                   ('&nbsp;'x3).'<label>'.
 6555:                   '<input type="radio" name="verifyrecord" value="0" checked="checked" />'.&mt('No').
 6556:                   '</label></span><br />'.
 6557:                   &mt('Grading will take longer if you use verification.').'<br />'.
 6558:                   &mt("Alternatively, the 'Review bubblesheet data' utility (see grading menu) can be used for all students after grading is complete.").'<br /><br />'.
 6559:                   '<input type="submit" name="submit" value="'.&mt('Start Grading').'" />'.
 6560:                   '<input type="hidden" name="command" value="scantron_process" />'."\n");
 6561:     } else {
 6562: 	$r->print('<input type="hidden" name="command" value="scantron_validate" />');
 6563: 	$r->print("<input type='hidden' name='validatepass' value='".$currentphase."' />");
 6564:     }
 6565:     if ($stop) {
 6566: 	if ($validate_phases[$currentphase] eq 'sequence') {
 6567: 	    $r->print('<input type="submit" name="submit" value="'.&mt('Ignore').' &rarr; " />');
 6568: 	    $r->print(' '.&mt('this error').' <br />');
 6569: 
 6570: 	    $r->print(" <p>".&mt("Or click the 'Grading Menu' button to start over.")."</p>");
 6571: 	} else {
 6572:             if ($validate_phases[$currentphase] eq 'doublebubble' || $validate_phases[$currentphase] eq 'missingbubbles') {
 6573: 	        $r->print('<input type="button" name="submitbutton" value="'.&mt('Continue').' &rarr;" onclick="javascript:verify_bubble_radio(this.form)" />');
 6574:             } else {
 6575:                 $r->print('<input type="submit" name="submit" value="'.&mt('Continue').' &rarr;" />');
 6576:             }
 6577: 	    $r->print(' '.&mt('using corrected info').' <br />');
 6578: 	    $r->print("<input type='submit' value='".&mt("Skip")."' name='scantron_skip_record' />");
 6579: 	    $r->print(" ".&mt("this scanline saving it for later."));
 6580: 	}
 6581:     }
 6582:     $r->print(" </form><br />".&show_grading_menu_form($symb));
 6583:     return '';
 6584: }
 6585: 
 6586: 
 6587: =pod
 6588: 
 6589: =item scantron_remove_file
 6590: 
 6591:    Removes the requested bubble sheet data file, makes sure that
 6592:    scantron_original_<filename> is never removed
 6593: 
 6594: 
 6595: =cut
 6596: 
 6597: sub scantron_remove_file {
 6598:     my ($which)=@_;
 6599:     my $cname=$env{'course.'.$env{'request.course.id'}.'.num'};
 6600:     my $cdom=$env{'course.'.$env{'request.course.id'}.'.domain'};
 6601:     my $file='scantron_';
 6602:     if ($which eq 'corrected' || $which eq 'skipped') {
 6603: 	$file.=$which.'_';
 6604:     } else {
 6605: 	return 'refused';
 6606:     }
 6607:     $file.=$env{'form.scantron_selectfile'};
 6608:     return &Apache::lonnet::removeuserfile($cname,$cdom,$file);
 6609: }
 6610: 
 6611: 
 6612: =pod
 6613: 
 6614: =item scantron_remove_scan_data
 6615: 
 6616:    Removes all scan_data correction for the requested bubble sheet
 6617:    data file.  (In the case that both the are doing skipped records we need
 6618:    to remember the old skipped lines for the time being so that element
 6619:    persists for a while.)
 6620: 
 6621: =cut
 6622: 
 6623: sub scantron_remove_scan_data {
 6624:     my $cname=$env{'course.'.$env{'request.course.id'}.'.num'};
 6625:     my $cdom=$env{'course.'.$env{'request.course.id'}.'.domain'};
 6626:     my @keys=&Apache::lonnet::getkeys('nohist_scantrondata',$cdom,$cname);
 6627:     my @todelete;
 6628:     my $filename=$env{'form.scantron_selectfile'};
 6629:     foreach my $key (@keys) {
 6630: 	if ($key=~/^\Q$filename\E_/) {
 6631: 	    if ($env{'form.scantron_options_redo'} eq 'redo_skipped_ready' &&
 6632: 		$key=~/remember_skipping/) {
 6633: 		next;
 6634: 	    }
 6635: 	    push(@todelete,$key);
 6636: 	}
 6637:     }
 6638:     my $result;
 6639:     if (@todelete) {
 6640: 	$result = &Apache::lonnet::del('nohist_scantrondata',
 6641: 				       \@todelete,$cdom,$cname);
 6642:     } else {
 6643: 	$result = 'ok';
 6644:     }
 6645:     return $result;
 6646: }
 6647: 
 6648: 
 6649: =pod
 6650: 
 6651: =item scantron_getfile
 6652: 
 6653:     Fetches the requested bubble sheet data file (all 3 versions), and
 6654:     the scan_data hash
 6655:   
 6656:   Arguments:
 6657:     None
 6658: 
 6659:   Returns:
 6660:     2 hash references
 6661: 
 6662:      - first one has 
 6663:          orig      -
 6664:          corrected -
 6665:          skipped   -  each of which points to an array ref of the specified
 6666:                       file broken up into individual lines
 6667:          count     - number of scanlines
 6668:  
 6669:      - second is the scan_data hash possible keys are
 6670:        ($number refers to scanline numbered $number and thus the key affects
 6671:         only that scanline
 6672:         $bubline refers to the specific bubble line element and the aspects
 6673:         refers to that specific bubble line element)
 6674: 
 6675:        $number.user - username:domain to use
 6676:        $number.CODE_ignore_dup 
 6677:                     - ignore the duplicate CODE error 
 6678:        $number.useCODE
 6679:                     - use the CODE in the scanline as is
 6680:        $number.no_bubble.$bubline
 6681:                     - it is valid that there is no bubbled in bubble
 6682:                       at $number $bubline
 6683:        remember_skipping
 6684:                     - a frozen hash containing keys of $number and values
 6685:                       of either 
 6686:                         1 - we are on a 'do skipped records pass' and plan
 6687:                             on processing this line
 6688:                         2 - we are on a 'do skipped records pass' and this
 6689:                             scanline has been marked to skip yet again
 6690: 
 6691: =cut
 6692: 
 6693: sub scantron_getfile {
 6694:     #FIXME really would prefer a scantron directory
 6695:     my $cname=$env{'course.'.$env{'request.course.id'}.'.num'};
 6696:     my $cdom=$env{'course.'.$env{'request.course.id'}.'.domain'};
 6697:     my $lines;
 6698:     $lines=&Apache::lonnet::getfile('/uploaded/'.$cdom.'/'.$cname.'/'.
 6699: 		       'scantron_orig_'.$env{'form.scantron_selectfile'});
 6700:     my %scanlines;
 6701:     $scanlines{'orig'}=[(split("\n",$lines,-1))];
 6702:     my $temp=$scanlines{'orig'};
 6703:     $scanlines{'count'}=$#$temp;
 6704: 
 6705:     $lines=&Apache::lonnet::getfile('/uploaded/'.$cdom.'/'.$cname.'/'.
 6706: 		       'scantron_corrected_'.$env{'form.scantron_selectfile'});
 6707:     if ($lines eq '-1') {
 6708: 	$scanlines{'corrected'}=[];
 6709:     } else {
 6710: 	$scanlines{'corrected'}=[(split("\n",$lines,-1))];
 6711:     }
 6712:     $lines=&Apache::lonnet::getfile('/uploaded/'.$cdom.'/'.$cname.'/'.
 6713: 		       'scantron_skipped_'.$env{'form.scantron_selectfile'});
 6714:     if ($lines eq '-1') {
 6715: 	$scanlines{'skipped'}=[];
 6716:     } else {
 6717: 	$scanlines{'skipped'}=[(split("\n",$lines,-1))];
 6718:     }
 6719:     my @tmp=&Apache::lonnet::dump('nohist_scantrondata',$cdom,$cname);
 6720:     if ($tmp[0] =~ /^(error:|no_such_host)/) { @tmp=(); }
 6721:     my %scan_data = @tmp;
 6722:     return (\%scanlines,\%scan_data);
 6723: }
 6724: 
 6725: =pod
 6726: 
 6727: =item lonnet_putfile
 6728: 
 6729:    Wrapper routine to call &Apache::lonnet::finishuserfileupload
 6730: 
 6731:  Arguments:
 6732:    $contents - data to store
 6733:    $filename - filename to store $contents into
 6734: 
 6735:  Returns:
 6736:    result value from &Apache::lonnet::finishuserfileupload
 6737: 
 6738: =cut
 6739: 
 6740: sub lonnet_putfile {
 6741:     my ($contents,$filename)=@_;
 6742:     my $docuname=$env{'course.'.$env{'request.course.id'}.'.num'};
 6743:     my $docudom=$env{'course.'.$env{'request.course.id'}.'.domain'};
 6744:     $env{'form.sillywaytopassafilearound'}=$contents;
 6745:     &Apache::lonnet::finishuserfileupload($docuname,$docudom,'sillywaytopassafilearound',$filename);
 6746: 
 6747: }
 6748: 
 6749: =pod
 6750: 
 6751: =item scantron_putfile
 6752: 
 6753:     Stores the current version of the bubble sheet data files, and the
 6754:     scan_data hash. (Does not modify the original version only the
 6755:     corrected and skipped versions.
 6756: 
 6757:  Arguments:
 6758:     $scanlines - hash ref that looks like the first return value from
 6759:                  &scantron_getfile()
 6760:     $scan_data - hash ref that looks like the second return value from
 6761:                  &scantron_getfile()
 6762: 
 6763: =cut
 6764: 
 6765: sub scantron_putfile {
 6766:     my ($scanlines,$scan_data) = @_;
 6767:     #FIXME really would prefer a scantron directory
 6768:     my $cname=$env{'course.'.$env{'request.course.id'}.'.num'};
 6769:     my $cdom=$env{'course.'.$env{'request.course.id'}.'.domain'};
 6770:     if ($scanlines) {
 6771: 	my $prefix='scantron_';
 6772: # no need to update orig, shouldn't change
 6773: #   &lonnet_putfile(join("\n",@{$scanlines->{'orig'}}),$prefix.'orig_'.
 6774: #		    $env{'form.scantron_selectfile'});
 6775: 	&lonnet_putfile(join("\n",@{$scanlines->{'corrected'}}),
 6776: 			$prefix.'corrected_'.
 6777: 			$env{'form.scantron_selectfile'});
 6778: 	&lonnet_putfile(join("\n",@{$scanlines->{'skipped'}}),
 6779: 			$prefix.'skipped_'.
 6780: 			$env{'form.scantron_selectfile'});
 6781:     }
 6782:     &Apache::lonnet::put('nohist_scantrondata',$scan_data,$cdom,$cname);
 6783: }
 6784: 
 6785: =pod
 6786: 
 6787: =item scantron_get_line
 6788: 
 6789:    Returns the correct version of the scanline
 6790: 
 6791:  Arguments:
 6792:     $scanlines - hash ref that looks like the first return value from
 6793:                  &scantron_getfile()
 6794:     $scan_data - hash ref that looks like the second return value from
 6795:                  &scantron_getfile()
 6796:     $i         - number of the requested line (starts at 0)
 6797: 
 6798:  Returns:
 6799:    A scanline, (either the original or the corrected one if it
 6800:    exists), or undef if the requested scanline should be
 6801:    skipped. (Either because it's an skipped scanline, or it's an
 6802:    unskipped scanline and we are not doing a 'do skipped scanlines'
 6803:    pass.
 6804: 
 6805: =cut
 6806: 
 6807: sub scantron_get_line {
 6808:     my ($scanlines,$scan_data,$i)=@_;
 6809:     if (&should_be_skipped($scanlines,$scan_data,$i)) { return undef; }
 6810:     #if ($scanlines->{'skipped'}[$i]) { return undef; }
 6811:     if ($scanlines->{'corrected'}[$i]) {return $scanlines->{'corrected'}[$i];}
 6812:     return $scanlines->{'orig'}[$i]; 
 6813: }
 6814: 
 6815: =pod
 6816: 
 6817: =item scantron_todo_count
 6818: 
 6819:     Counts the number of scanlines that need processing.
 6820: 
 6821:  Arguments:
 6822:     $scanlines - hash ref that looks like the first return value from
 6823:                  &scantron_getfile()
 6824:     $scan_data - hash ref that looks like the second return value from
 6825:                  &scantron_getfile()
 6826: 
 6827:  Returns:
 6828:     $count - number of scanlines to process
 6829: 
 6830: =cut
 6831: 
 6832: sub get_todo_count {
 6833:     my ($scanlines,$scan_data)=@_;
 6834:     my $count=0;
 6835:     for (my $i=0;$i<=$scanlines->{'count'};$i++) {
 6836: 	my $line=&scantron_get_line($scanlines,$scan_data,$i);
 6837: 	if ($line=~/^[\s\cz]*$/) { next; }
 6838: 	$count++;
 6839:     }
 6840:     return $count;
 6841: }
 6842: 
 6843: =pod
 6844: 
 6845: =item scantron_put_line
 6846: 
 6847:     Updates the 'corrected' or 'skipped' versions of the bubble sheet
 6848:     data file.
 6849: 
 6850:  Arguments:
 6851:     $scanlines - hash ref that looks like the first return value from
 6852:                  &scantron_getfile()
 6853:     $scan_data - hash ref that looks like the second return value from
 6854:                  &scantron_getfile()
 6855:     $i         - line number to update
 6856:     $newline   - contents of the updated scanline
 6857:     $skip      - if true make the line for skipping and update the
 6858:                  'skipped' file
 6859: 
 6860: =cut
 6861: 
 6862: sub scantron_put_line {
 6863:     my ($scanlines,$scan_data,$i,$newline,$skip)=@_;
 6864:     if ($skip) {
 6865: 	$scanlines->{'skipped'}[$i]=$newline;
 6866: 	&start_skipping($scan_data,$i);
 6867: 	return;
 6868:     }
 6869:     $scanlines->{'corrected'}[$i]=$newline;
 6870: }
 6871: 
 6872: =pod
 6873: 
 6874: =item scantron_clear_skip
 6875: 
 6876:    Remove a line from the 'skipped' file
 6877: 
 6878:  Arguments:
 6879:     $scanlines - hash ref that looks like the first return value from
 6880:                  &scantron_getfile()
 6881:     $scan_data - hash ref that looks like the second return value from
 6882:                  &scantron_getfile()
 6883:     $i         - line number to update
 6884: 
 6885: =cut
 6886: 
 6887: sub scantron_clear_skip {
 6888:     my ($scanlines,$scan_data,$i)=@_;
 6889:     if (exists($scanlines->{'skipped'}[$i])) {
 6890: 	undef($scanlines->{'skipped'}[$i]);
 6891: 	return 1;
 6892:     }
 6893:     return 0;
 6894: }
 6895: 
 6896: =pod
 6897: 
 6898: =item scantron_filter_not_exam
 6899: 
 6900:    Filter routine used by &Apache::lonnavmaps::retrieveResources(), to
 6901:    filter out resources that are not marked as 'exam' mode
 6902: 
 6903: =cut
 6904: 
 6905: sub scantron_filter_not_exam {
 6906:     my ($curres)=@_;
 6907:     
 6908:     if (ref($curres) && $curres->is_problem() && !$curres->is_exam()) {
 6909: 	# if the user has asked to not have either hidden
 6910: 	# or 'randomout' controlled resources to be graded
 6911: 	# don't include them
 6912: 	if ($env{'form.scantron_options_hidden'} eq 'ignore_hidden'
 6913: 	    && $curres->randomout) {
 6914: 	    return 0;
 6915: 	}
 6916: 	return 1;
 6917:     }
 6918:     return 0;
 6919: }
 6920: 
 6921: =pod
 6922: 
 6923: =item scantron_validate_sequence
 6924: 
 6925:     Validates the selected sequence, checking for resource that are
 6926:     not set to exam mode.
 6927: 
 6928: =cut
 6929: 
 6930: sub scantron_validate_sequence {
 6931:     my ($r,$currentphase) = @_;
 6932: 
 6933:     my $navmap=Apache::lonnavmaps::navmap->new();
 6934:     unless (ref($navmap)) {
 6935:         $r->print(&navmap_errormsg());
 6936:         return (1,$currentphase);
 6937:     }
 6938:     my (undef,undef,$sequence)=
 6939: 	&Apache::lonnet::decode_symb($env{'form.selectpage'});
 6940: 
 6941:     my $map=$navmap->getResourceByUrl($sequence);
 6942: 
 6943:     $r->print('<input type="hidden" name="validate_sequence_exam"
 6944:                                     value="ignore" />');
 6945:     if ($env{'form.validate_sequence_exam'} ne 'ignore') {
 6946: 	my @resources=
 6947: 	    $navmap->retrieveResources($map,\&scantron_filter_not_exam,1,0);
 6948: 	if (@resources) {
 6949: 	    $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>");
 6950: 	    return (1,$currentphase);
 6951: 	}
 6952:     }
 6953: 
 6954:     return (0,$currentphase+1);
 6955: }
 6956: 
 6957: 
 6958: 
 6959: sub scantron_validate_ID {
 6960:     my ($r,$currentphase) = @_;
 6961:     
 6962:     #get student info
 6963:     my $classlist=&Apache::loncoursedata::get_classlist();
 6964:     my %idmap=&username_to_idmap($classlist);
 6965: 
 6966:     #get scantron line setup
 6967:     my %scantron_config=&get_scantron_config($env{'form.scantron_format'});
 6968:     my ($scanlines,$scan_data)=&scantron_getfile();
 6969: 
 6970:     my $nav_error;
 6971:     &scantron_get_maxbubble(\$nav_error); # parse needs the bubble_lines.. array.
 6972:     if ($nav_error) {
 6973:         $r->print(&navmap_errormsg());
 6974:         return(1,$currentphase);
 6975:     }
 6976: 
 6977:     my %found=('ids'=>{},'usernames'=>{});
 6978:     for (my $i=0;$i<=$scanlines->{'count'};$i++) {
 6979: 	my $line=&scantron_get_line($scanlines,$scan_data,$i);
 6980: 	if ($line=~/^[\s\cz]*$/) { next; }
 6981: 	my $scan_record=&scantron_parse_scanline($line,$i,\%scantron_config,
 6982: 						 $scan_data);
 6983: 	my $id=$$scan_record{'scantron.ID'};
 6984: 	my $found;
 6985: 	foreach my $checkid (keys(%idmap)) {
 6986: 	    if (lc($checkid) eq lc($id)) { $found=$checkid;last; }
 6987: 	}
 6988: 	if ($found) {
 6989: 	    my $username=$idmap{$found};
 6990: 	    if ($found{'ids'}{$found}) {
 6991: 		&scantron_get_correction($r,$i,$scan_record,\%scantron_config,
 6992: 					 $line,'duplicateID',$found);
 6993: 		return(1,$currentphase);
 6994: 	    } elsif ($found{'usernames'}{$username}) {
 6995: 		&scantron_get_correction($r,$i,$scan_record,\%scantron_config,
 6996: 					 $line,'duplicateID',$username);
 6997: 		return(1,$currentphase);
 6998: 	    }
 6999: 	    #FIXME store away line we previously saw the ID on to use above
 7000: 	    $found{'ids'}{$found}++;
 7001: 	    $found{'usernames'}{$username}++;
 7002: 	} else {
 7003: 	    if ($id =~ /^\s*$/) {
 7004: 		my $username=&scan_data($scan_data,"$i.user");
 7005: 		if (defined($username) && $found{'usernames'}{$username}) {
 7006: 		    &scantron_get_correction($r,$i,$scan_record,
 7007: 					     \%scantron_config,
 7008: 					     $line,'duplicateID',$username);
 7009: 		    return(1,$currentphase);
 7010: 		} elsif (!defined($username)) {
 7011: 		    &scantron_get_correction($r,$i,$scan_record,
 7012: 					     \%scantron_config,
 7013: 					     $line,'incorrectID');
 7014: 		    return(1,$currentphase);
 7015: 		}
 7016: 		$found{'usernames'}{$username}++;
 7017: 	    } else {
 7018: 		&scantron_get_correction($r,$i,$scan_record,\%scantron_config,
 7019: 					 $line,'incorrectID');
 7020: 		return(1,$currentphase);
 7021: 	    }
 7022: 	}
 7023:     }
 7024: 
 7025:     return (0,$currentphase+1);
 7026: }
 7027: 
 7028: 
 7029: sub scantron_get_correction {
 7030:     my ($r,$i,$scan_record,$scan_config,$line,$error,$arg)=@_;
 7031: #FIXME in the case of a duplicated ID the previous line, probably need
 7032: #to show both the current line and the previous one and allow skipping
 7033: #the previous one or the current one
 7034: 
 7035:     if ( $$scan_record{'scantron.PaperID'} =~ /\S/) {
 7036: 	$r->print("<p>".&mt("<b>An error was detected ($error)</b>".
 7037: 			    " for PaperID <tt>[_1]</tt>",
 7038: 			    $$scan_record{'scantron.PaperID'})."</p> \n");
 7039:     } else {
 7040: 	$r->print("<p>".&mt("<b>An error was detected ($error)</b>".
 7041: 			    " in scanline [_1] <pre>[_2]</pre>",
 7042: 			    $i,$line)."</p> \n");
 7043:     }
 7044:     my $message="<p>".&mt("The ID on the form is  <tt>[_1]</tt><br />".
 7045: 			  "The name on the paper is [_2],[_3]",
 7046: 			  $$scan_record{'scantron.ID'},
 7047: 			  $$scan_record{'scantron.LastName'},
 7048: 			  $$scan_record{'scantron.FirstName'})."</p>";
 7049: 
 7050:     $r->print('<input type="hidden" name="scantron_corrections" value="'.$error.'" />'."\n");
 7051:     $r->print('<input type="hidden" name="scantron_line" value="'.$i.'" />'."\n");
 7052:                            # Array populated for doublebubble or
 7053:     my @lines_to_correct;  # missingbubble errors to build javascript
 7054:                            # to validate radio button checking   
 7055: 
 7056:     if ($error =~ /ID$/) {
 7057: 	if ($error eq 'incorrectID') {
 7058: 	    $r->print("<p>".&mt("The encoded ID is not in the classlist").
 7059: 		      "</p>\n");
 7060: 	} elsif ($error eq 'duplicateID') {
 7061: 	    $r->print("<p>".&mt("The encoded ID has also been used by a previous paper [_1]",$arg)."</p>\n");
 7062: 	}
 7063: 	$r->print($message);
 7064: 	$r->print("<p>".&mt("How should I handle this?")." <br /> \n");
 7065: 	$r->print("\n<ul><li> ");
 7066: 	#FIXME it would be nice if this sent back the user ID and
 7067: 	#could do partial userID matches
 7068: 	$r->print(&Apache::loncommon::selectstudent_link('scantronupload',
 7069: 				       'scantron_username','scantron_domain'));
 7070: 	$r->print(": <input type='text' name='scantron_username' value='' />");
 7071: 	$r->print("\n@".
 7072: 		 &Apache::loncommon::select_dom_form($env{'request.role.domain'},'scantron_domain'));
 7073: 
 7074: 	$r->print('</li>');
 7075:     } elsif ($error =~ /CODE$/) {
 7076: 	if ($error eq 'incorrectCODE') {
 7077: 	    $r->print("<p>".&mt("The encoded CODE is not in the list of possible CODEs.")."</p>\n");
 7078: 	} elsif ($error eq 'duplicateCODE') {
 7079: 	    $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");
 7080: 	}
 7081: 	$r->print("<p>".&mt("The CODE on the form is  <tt>'[_1]'</tt>",
 7082: 			    $$scan_record{'scantron.CODE'})."<br />\n");
 7083: 	$r->print($message);
 7084: 	$r->print("<p>".&mt("How should I handle this?")." <br /> \n");
 7085: 	$r->print("\n<br /> ");
 7086: 	my $i=0;
 7087: 	if ($error eq 'incorrectCODE' 
 7088: 	    && $$scan_record{'scantron.CODE'}=~/\S/ ) {
 7089: 	    my ($max,$closest)=&scantron_get_closely_matching_CODEs($arg,$$scan_record{'scantron.CODE'});
 7090: 	    if ($closest > 0) {
 7091: 		foreach my $testcode (@{$closest}) {
 7092: 		    my $checked='';
 7093: 		    if (!$i) { $checked=' checked="checked"'; }
 7094: 		    $r->print("
 7095:    <label>
 7096:        <input type='radio' name='scantron_CODE_resolution' value='use_closest_$i'$checked />
 7097:        ".&mt("Use the similar CODE [_1] instead.",
 7098: 	    "<b><tt>".$testcode."</tt></b>")."
 7099:     </label>
 7100:     <input type='hidden' name='scantron_CODE_closest_$i' value='$testcode' />");
 7101: 		    $r->print("\n<br />");
 7102: 		    $i++;
 7103: 		}
 7104: 	    }
 7105: 	}
 7106: 	if ($$scan_record{'scantron.CODE'}=~/\S/ ) {
 7107: 	    my $checked; if (!$i) { $checked=' checked="checked"'; }
 7108: 	    $r->print("
 7109:     <label>
 7110:         <input type='radio' name='scantron_CODE_resolution' value='use_unfound'$checked />
 7111:        ".&mt("Use the CODE [_1] that is was on the paper, ignoring the error.",
 7112: 	     "<b><tt>".$$scan_record{'scantron.CODE'}."</tt></b>")."
 7113:     </label>");
 7114: 	    $r->print("\n<br />");
 7115: 	}
 7116: 
 7117: 	$r->print(<<ENDSCRIPT);
 7118: <script type="text/javascript">
 7119: function change_radio(field) {
 7120:     var slct=document.scantronupload.scantron_CODE_resolution;
 7121:     var i;
 7122:     for (i=0;i<slct.length;i++) {
 7123:         if (slct[i].value==field) { slct[i].checked=true; }
 7124:     }
 7125: }
 7126: </script>
 7127: ENDSCRIPT
 7128: 	my $href="/adm/pickcode?".
 7129: 	   "form=".&escape("scantronupload").
 7130: 	   "&scantron_format=".&escape($env{'form.scantron_format'}).
 7131: 	   "&scantron_CODElist=".&escape($env{'form.scantron_CODElist'}).
 7132: 	   "&curCODE=".&escape($$scan_record{'scantron.CODE'}).
 7133: 	   "&scantron_selectfile=".&escape($env{'form.scantron_selectfile'});
 7134: 	if ($env{'form.scantron_CODElist'} =~ /\S/) { 
 7135: 	    $r->print("
 7136:     <label>
 7137:        <input type='radio' name='scantron_CODE_resolution' value='use_found' />
 7138:        ".&mt("[_1]Select[_2] a CODE from the list of all CODEs and use it.",
 7139: 	     "<a target='_blank' href='$href'>","</a>")."
 7140:     </label> 
 7141:     ".&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\')" />'));
 7142: 	    $r->print("\n<br />");
 7143: 	}
 7144: 	$r->print("
 7145:     <label>
 7146:        <input type='radio' name='scantron_CODE_resolution' value='use_typed' />
 7147:        ".&mt("Use [_1] as the CODE.",
 7148: 	     "</label><input type='text' size='8' name='scantron_CODE_newvalue' onfocus=\"javascript:change_radio('use_typed')\" onkeypress=\"javascript:change_radio('use_typed')\" />"));
 7149: 	$r->print("\n<br /><br />");
 7150:     } elsif ($error eq 'doublebubble') {
 7151: 	$r->print("<p>".&mt("There have been multiple bubbles scanned for some question(s)")."</p>\n");
 7152: 
 7153: 	# The form field scantron_questions is acutally a list of line numbers.
 7154: 	# represented by this form so:
 7155: 
 7156: 	my $line_list = &questions_to_line_list($arg);
 7157: 
 7158: 	$r->print('<input type="hidden" name="scantron_questions" value="'.
 7159: 		  $line_list.'" />');
 7160: 	$r->print($message);
 7161: 	$r->print("<p>".&mt("Please indicate which bubble should be used for grading")."</p>");
 7162: 	foreach my $question (@{$arg}) {
 7163: 	    my @linenums = &prompt_for_corrections($r,$question,$scan_config,
 7164:                                                    $scan_record, $error);
 7165:             push(@lines_to_correct,@linenums);
 7166: 	}
 7167:         $r->print(&verify_bubbles_checked(@lines_to_correct));
 7168:     } elsif ($error eq 'missingbubble') {
 7169: 	$r->print("<p>".&mt("There have been <b>no</b> bubbles scanned for some question(s)")."</p>\n");
 7170: 	$r->print($message);
 7171: 	$r->print("<p>".&mt("Please indicate which bubble should be used for grading.")."</p>");
 7172: 	$r->print(&mt("Some questions have no scanned bubbles.")."\n");
 7173: 
 7174: 	# The form field scantron_questions is actually a list of line numbers not
 7175: 	# a list of question numbers. Therefore:
 7176: 	#
 7177: 	
 7178: 	my $line_list = &questions_to_line_list($arg);
 7179: 
 7180: 	$r->print('<input type="hidden" name="scantron_questions" value="'.
 7181: 		  $line_list.'" />');
 7182: 	foreach my $question (@{$arg}) {
 7183: 	    my @linenums = &prompt_for_corrections($r,$question,$scan_config,
 7184:                                                    $scan_record, $error);
 7185:             push(@lines_to_correct,@linenums);
 7186: 	}
 7187:         $r->print(&verify_bubbles_checked(@lines_to_correct));
 7188:     } else {
 7189: 	$r->print("\n<ul>");
 7190:     }
 7191:     $r->print("\n</li></ul>");
 7192: }
 7193: 
 7194: sub verify_bubbles_checked {
 7195:     my (@ansnums) = @_;
 7196:     my $ansnumstr = join('","',@ansnums);
 7197:     my $warning = &mt("A bubble or 'No bubble' selection has not been made for one or more lines.");
 7198:     my $output = (<<ENDSCRIPT);
 7199: <script type="text/javascript">
 7200: function verify_bubble_radio(form) {
 7201:     var ansnumArray = new Array ("$ansnumstr");
 7202:     var need_bubble_count = 0;
 7203:     for (var i=0; i<ansnumArray.length; i++) {
 7204:         if (form.elements["scantron_correct_Q_"+ansnumArray[i]].length > 1) {
 7205:             var bubble_picked = 0; 
 7206:             for (var j=0; j<form.elements["scantron_correct_Q_"+ansnumArray[i]].length; j++) {
 7207:                 if (form.elements["scantron_correct_Q_"+ansnumArray[i]][j].checked == true) {
 7208:                     bubble_picked = 1;
 7209:                 }
 7210:             }
 7211:             if (bubble_picked == 0) {
 7212:                 need_bubble_count ++;
 7213:             }
 7214:         }
 7215:     }
 7216:     if (need_bubble_count) {
 7217:         alert("$warning");
 7218:         return;
 7219:     }
 7220:     form.submit(); 
 7221: }
 7222: </script>
 7223: ENDSCRIPT
 7224:     return $output;
 7225: }
 7226: 
 7227: =pod
 7228: 
 7229: =item  questions_to_line_list
 7230: 
 7231: Converts a list of questions into a string of comma separated
 7232: line numbers in the answer sheet used by the questions.  This is
 7233: used to fill in the scantron_questions form field.
 7234: 
 7235:   Arguments:
 7236:      questions    - Reference to an array of questions.
 7237: 
 7238: =cut
 7239: 
 7240: 
 7241: sub questions_to_line_list {
 7242:     my ($questions) = @_;
 7243:     my @lines;
 7244: 
 7245:     foreach my $item (@{$questions}) {
 7246:         my $question = $item;
 7247:         my ($first,$count,$last);
 7248:         if ($item =~ /^(\d+)\.(\d+)$/) {
 7249:             $question = $1;
 7250:             my $subquestion = $2;
 7251:             $first = $first_bubble_line{$question-1} + 1;
 7252:             my @subans = split(/,/,$subdivided_bubble_lines{$question-1});
 7253:             my $subcount = 1;
 7254:             while ($subcount<$subquestion) {
 7255:                 $first += $subans[$subcount-1];
 7256:                 $subcount ++;
 7257:             }
 7258:             $count = $subans[$subquestion-1];
 7259:         } else {
 7260: 	    $first   = $first_bubble_line{$question-1} + 1;
 7261: 	    $count   = $bubble_lines_per_response{$question-1};
 7262:         }
 7263:         $last = $first+$count-1;
 7264:         push(@lines, ($first..$last));
 7265:     }
 7266:     return join(',', @lines);
 7267: }
 7268: 
 7269: =pod 
 7270: 
 7271: =item prompt_for_corrections
 7272: 
 7273: Prompts for a potentially multiline correction to the
 7274: user's bubbling (factors out common code from scantron_get_correction
 7275: for multi and missing bubble cases).
 7276: 
 7277:  Arguments:
 7278:    $r           - Apache request object.
 7279:    $question    - The question number to prompt for.
 7280:    $scan_config - The scantron file configuration hash.
 7281:    $scan_record - Reference to the hash that has the the parsed scanlines.
 7282:    $error       - Type of error
 7283: 
 7284:  Implicit inputs:
 7285:    %bubble_lines_per_response   - Starting line numbers for each question.
 7286:                                   Numbered from 0 (but question numbers are from
 7287:                                   1.
 7288:    %first_bubble_line           - Starting bubble line for each question.
 7289:    %subdivided_bubble_lines     - optionresponse, matchresponse and rankresponse 
 7290:                                   type problems render as separate sub-questions, 
 7291:                                   in exam mode. This hash contains a 
 7292:                                   comma-separated list of the lines per 
 7293:                                   sub-question.
 7294:    %responsetype_per_response   - essayresponse, formularesponse,
 7295:                                   stringresponse, imageresponse, reactionresponse,
 7296:                                   and organicresponse type problem parts can have
 7297:                                   multiple lines per response if the weight
 7298:                                   assigned exceeds 10.  In this case, only
 7299:                                   one bubble per line is permitted, but more 
 7300:                                   than one line might contain bubbles, e.g.
 7301:                                   bubbling of: line 1 - J, line 2 - J, 
 7302:                                   line 3 - B would assign 22 points.  
 7303: 
 7304: =cut
 7305: 
 7306: sub prompt_for_corrections {
 7307:     my ($r, $question, $scan_config, $scan_record, $error) = @_;
 7308:     my ($current_line,$lines);
 7309:     my @linenums;
 7310:     my $questionnum = $question;
 7311:     if ($question =~ /^(\d+)\.(\d+)$/) {
 7312:         $question = $1;
 7313:         $current_line = $first_bubble_line{$question-1} + 1 ;
 7314:         my $subquestion = $2;
 7315:         my @subans = split(/,/,$subdivided_bubble_lines{$question-1});
 7316:         my $subcount = 1;
 7317:         while ($subcount<$subquestion) {
 7318:             $current_line += $subans[$subcount-1];
 7319:             $subcount ++;
 7320:         }
 7321:         $lines = $subans[$subquestion-1];
 7322:     } else {
 7323:         $current_line = $first_bubble_line{$question-1} + 1 ;
 7324:         $lines        = $bubble_lines_per_response{$question-1};
 7325:     }
 7326:     if ($lines > 1) {
 7327:         $r->print(&mt('The group of bubble lines below responds to a single question.').'<br />');
 7328:         if (($responsetype_per_response{$question-1} eq 'essayresponse') ||
 7329:             ($responsetype_per_response{$question-1} eq 'formularesponse') ||
 7330:             ($responsetype_per_response{$question-1} eq 'stringresponse') ||
 7331:             ($responsetype_per_response{$question-1} eq 'imageresponse') ||
 7332:             ($responsetype_per_response{$question-1} eq 'reactionresponse') ||
 7333:             ($responsetype_per_response{$question-1} eq 'organicresponse')) {
 7334:             $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 />');
 7335:         } else {
 7336:             $r->print(&mt("Select at most one bubble in a single line and select 'No Bubble' in all the other lines. ")."<br />");
 7337:         }
 7338:     }
 7339:     for (my $i =0; $i < $lines; $i++) {
 7340:         my $selected = $$scan_record{"scantron.$current_line.answer"};
 7341: 	&scantron_bubble_selector($r,$scan_config,$current_line, 
 7342: 	        		  $questionnum,$error,split('', $selected));
 7343:         push(@linenums,$current_line);
 7344: 	$current_line++;
 7345:     }
 7346:     if ($lines > 1) {
 7347: 	$r->print("<hr /><br />");
 7348:     }
 7349:     return @linenums;
 7350: }
 7351: 
 7352: =pod
 7353: 
 7354: =item scantron_bubble_selector
 7355:   
 7356:    Generates the html radiobuttons to correct a single bubble line
 7357:    possibly showing the existing the selected bubbles if known
 7358: 
 7359:  Arguments:
 7360:     $r           - Apache request object
 7361:     $scan_config - hash from &get_scantron_config()
 7362:     $line        - Number of the line being displayed.
 7363:     $questionnum - Question number (may include subquestion)
 7364:     $error       - Type of error.
 7365:     @selected    - Array of bubbles picked on this line.
 7366: 
 7367: =cut
 7368: 
 7369: sub scantron_bubble_selector {
 7370:     my ($r,$scan_config,$line,$questionnum,$error,@selected)=@_;
 7371:     my $max=$$scan_config{'Qlength'};
 7372: 
 7373:     my $scmode=$$scan_config{'Qon'};
 7374:     if ($scmode eq 'number' || $scmode eq 'letter') { $max=10; }	     
 7375: 
 7376:     my @alphabet=('A'..'Z');
 7377:     $r->print(&Apache::loncommon::start_data_table().
 7378:               &Apache::loncommon::start_data_table_row());
 7379:     $r->print('<td rowspan="2" class="LC_leftcol_header">'.$line.'</td>');
 7380:     for (my $i=0;$i<$max+1;$i++) {
 7381: 	$r->print("\n".'<td align="center">');
 7382: 	if ($selected[0] eq $alphabet[$i]) { $r->print('X'); shift(@selected) }
 7383: 	else { $r->print('&nbsp;'); }
 7384: 	$r->print('</td>');
 7385:     }
 7386:     $r->print(&Apache::loncommon::end_data_table_row().
 7387:               &Apache::loncommon::start_data_table_row());
 7388:     for (my $i=0;$i<$max;$i++) {
 7389: 	$r->print("\n".
 7390: 		  '<td><label><input type="radio" name="scantron_correct_Q_'.
 7391: 		  $line.'" value="'.$i.'" />'.$alphabet[$i]."</label></td>");
 7392:     }
 7393:     my $nobub_checked = ' ';
 7394:     if ($error eq 'missingbubble') {
 7395:         $nobub_checked = ' checked = "checked" ';
 7396:     }
 7397:     $r->print("\n".'<td><label><input type="radio" name="scantron_correct_Q_'.
 7398: 	      $line.'" value="none"'.$nobub_checked.'/>'.&mt('No bubble').
 7399:               '</label>'."\n".'<input type="hidden" name="scantron_questionnum_Q_'.
 7400:               $line.'" value="'.$questionnum.'" /></td>');
 7401:     $r->print(&Apache::loncommon::end_data_table_row().
 7402:               &Apache::loncommon::end_data_table());
 7403: }
 7404: 
 7405: =pod
 7406: 
 7407: =item num_matches
 7408: 
 7409:    Counts the number of characters that are the same between the two arguments.
 7410: 
 7411:  Arguments:
 7412:    $orig - CODE from the scanline
 7413:    $code - CODE to match against
 7414: 
 7415:  Returns:
 7416:    $count - integer count of the number of same characters between the
 7417:             two arguments
 7418: 
 7419: =cut
 7420: 
 7421: sub num_matches {
 7422:     my ($orig,$code) = @_;
 7423:     my @code=split(//,$code);
 7424:     my @orig=split(//,$orig);
 7425:     my $same=0;
 7426:     for (my $i=0;$i<scalar(@code);$i++) {
 7427: 	if ($code[$i] eq $orig[$i]) { $same++; }
 7428:     }
 7429:     return $same;
 7430: }
 7431: 
 7432: =pod
 7433: 
 7434: =item scantron_get_closely_matching_CODEs
 7435: 
 7436:    Cycles through all CODEs and finds the set that has the greatest
 7437:    number of same characters as the provided CODE
 7438: 
 7439:  Arguments:
 7440:    $allcodes - hash ref returned by &get_codes()
 7441:    $CODE     - CODE from the current scanline
 7442: 
 7443:  Returns:
 7444:    2 element list
 7445:     - first elements is number of how closely matching the best fit is 
 7446:       (5 means best set has 5 matching characters)
 7447:     - second element is an arrary ref containing the set of valid CODEs
 7448:       that best fit the passed in CODE
 7449: 
 7450: =cut
 7451: 
 7452: sub scantron_get_closely_matching_CODEs {
 7453:     my ($allcodes,$CODE)=@_;
 7454:     my @CODEs;
 7455:     foreach my $testcode (sort(keys(%{$allcodes}))) {
 7456: 	push(@{$CODEs[&num_matches($CODE,$testcode)]},$testcode);
 7457:     }
 7458: 
 7459:     return ($#CODEs,$CODEs[-1]);
 7460: }
 7461: 
 7462: =pod
 7463: 
 7464: =item get_codes
 7465: 
 7466:    Builds a hash which has keys of all of the valid CODEs from the selected
 7467:    set of remembered CODEs.
 7468: 
 7469:  Arguments:
 7470:   $old_name - name of the set of remembered CODEs
 7471:   $cdom     - domain of the course
 7472:   $cnum     - internal course name
 7473: 
 7474:  Returns:
 7475:   %allcodes - keys are the valid CODEs, values are all 1
 7476: 
 7477: =cut
 7478: 
 7479: sub get_codes {
 7480:     my ($old_name, $cdom, $cnum) = @_;
 7481:     if (!$old_name) {
 7482: 	$old_name=$env{'form.scantron_CODElist'};
 7483:     }
 7484:     if (!$cdom) {
 7485: 	$cdom =$env{'course.'.$env{'request.course.id'}.'.domain'};
 7486:     }
 7487:     if (!$cnum) {
 7488: 	$cnum =$env{'course.'.$env{'request.course.id'}.'.num'};
 7489:     }
 7490:     my %result=&Apache::lonnet::get('CODEs',[$old_name,"type\0$old_name"],
 7491: 				    $cdom,$cnum);
 7492:     my %allcodes;
 7493:     if ($result{"type\0$old_name"} eq 'number') {
 7494: 	%allcodes=map {($_,1)} split(',',$result{$old_name});
 7495:     } else {
 7496: 	%allcodes=map {(&Apache::lonprintout::num_to_letters($_),1)} split(',',$result{$old_name});
 7497:     }
 7498:     return %allcodes;
 7499: }
 7500: 
 7501: =pod
 7502: 
 7503: =item scantron_validate_CODE
 7504: 
 7505:    Validates all scanlines in the selected file to not have any
 7506:    invalid or underspecified CODEs and that none of the codes are
 7507:    duplicated if this was requested.
 7508: 
 7509: =cut
 7510: 
 7511: sub scantron_validate_CODE {
 7512:     my ($r,$currentphase) = @_;
 7513:     my %scantron_config=&get_scantron_config($env{'form.scantron_format'});
 7514:     if ($scantron_config{'CODElocation'} &&
 7515: 	$scantron_config{'CODEstart'} &&
 7516: 	$scantron_config{'CODElength'}) {
 7517: 	if (!defined($env{'form.scantron_CODElist'})) {
 7518: 	    &FIXME_blow_up()
 7519: 	}
 7520:     } else {
 7521: 	return (0,$currentphase+1);
 7522:     }
 7523:     
 7524:     my %usedCODEs;
 7525: 
 7526:     my %allcodes=&get_codes();
 7527: 
 7528:     my $nav_error;
 7529:     &scantron_get_maxbubble(\$nav_error); # parse needs the lines per response array.
 7530:     if ($nav_error) {
 7531:         $r->print(&navmap_errormsg());
 7532:         return(1,$currentphase);
 7533:     }
 7534: 
 7535:     my ($scanlines,$scan_data)=&scantron_getfile();
 7536:     for (my $i=0;$i<=$scanlines->{'count'};$i++) {
 7537: 	my $line=&scantron_get_line($scanlines,$scan_data,$i);
 7538: 	if ($line=~/^[\s\cz]*$/) { next; }
 7539: 	my $scan_record=&scantron_parse_scanline($line,$i,\%scantron_config,
 7540: 						 $scan_data);
 7541: 	my $CODE=$$scan_record{'scantron.CODE'};
 7542: 	my $error=0;
 7543: 	if (!&Apache::lonnet::validCODE($CODE)) {
 7544: 	    &scantron_get_correction($r,$i,$scan_record,
 7545: 				     \%scantron_config,
 7546: 				     $line,'incorrectCODE',\%allcodes);
 7547: 	    return(1,$currentphase);
 7548: 	}
 7549: 	if (%allcodes && !exists($allcodes{$CODE}) 
 7550: 	    && !$$scan_record{'scantron.useCODE'}) {
 7551: 	    &scantron_get_correction($r,$i,$scan_record,
 7552: 				     \%scantron_config,
 7553: 				     $line,'incorrectCODE',\%allcodes);
 7554: 	    return(1,$currentphase);
 7555: 	}
 7556: 	if (exists($usedCODEs{$CODE}) 
 7557: 	    && $env{'form.scantron_CODEunique'} eq 'yes'
 7558: 	    && !$$scan_record{'scantron.CODE_ignore_dup'}) {
 7559: 	    &scantron_get_correction($r,$i,$scan_record,
 7560: 				     \%scantron_config,
 7561: 				     $line,'duplicateCODE',$usedCODEs{$CODE});
 7562: 	    return(1,$currentphase);
 7563: 	}
 7564: 	push(@{$usedCODEs{$CODE}},$$scan_record{'scantron.PaperID'});
 7565:     }
 7566:     return (0,$currentphase+1);
 7567: }
 7568: 
 7569: =pod
 7570: 
 7571: =item scantron_validate_doublebubble
 7572: 
 7573:    Validates all scanlines in the selected file to not have any
 7574:    bubble lines with multiple bubbles marked.
 7575: 
 7576: =cut
 7577: 
 7578: sub scantron_validate_doublebubble {
 7579:     my ($r,$currentphase) = @_;
 7580:     #get student info
 7581:     my $classlist=&Apache::loncoursedata::get_classlist();
 7582:     my %idmap=&username_to_idmap($classlist);
 7583: 
 7584:     #get scantron line setup
 7585:     my %scantron_config=&get_scantron_config($env{'form.scantron_format'});
 7586:     my ($scanlines,$scan_data)=&scantron_getfile();
 7587:     my $nav_error;
 7588:     &scantron_get_maxbubble(\$nav_error); # parse needs the bubble line array.
 7589:     if ($nav_error) {
 7590:         $r->print(&navmap_errormsg());
 7591:         return(1,$currentphase);
 7592:     }
 7593: 
 7594:     for (my $i=0;$i<=$scanlines->{'count'};$i++) {
 7595: 	my $line=&scantron_get_line($scanlines,$scan_data,$i);
 7596: 	if ($line=~/^[\s\cz]*$/) { next; }
 7597: 	my $scan_record=&scantron_parse_scanline($line,$i,\%scantron_config,
 7598: 						 $scan_data);
 7599: 	if (!defined($$scan_record{'scantron.doubleerror'})) { next; }
 7600: 	&scantron_get_correction($r,$i,$scan_record,\%scantron_config,$line,
 7601: 				 'doublebubble',
 7602: 				 $$scan_record{'scantron.doubleerror'});
 7603:     	return (1,$currentphase);
 7604:     }
 7605:     return (0,$currentphase+1);
 7606: }
 7607: 
 7608: 
 7609: sub scantron_get_maxbubble {
 7610:     my ($nav_error) = @_;
 7611:     if (defined($env{'form.scantron_maxbubble'}) &&
 7612: 	$env{'form.scantron_maxbubble'}) {
 7613: 	&restore_bubble_lines();
 7614: 	return $env{'form.scantron_maxbubble'};
 7615:     }
 7616: 
 7617:     my (undef, undef, $sequence) =
 7618: 	&Apache::lonnet::decode_symb($env{'form.selectpage'});
 7619: 
 7620:     my $navmap=Apache::lonnavmaps::navmap->new();
 7621:     unless (ref($navmap)) {
 7622:         if (ref($nav_error)) {
 7623:             $$nav_error = 1;
 7624:         }
 7625:         return;
 7626:     }
 7627:     my $map=$navmap->getResourceByUrl($sequence);
 7628:     my @resources=$navmap->retrieveResources($map,\&scantron_filter,1,0);
 7629: 
 7630:     &Apache::lonxml::clear_problem_counter();
 7631: 
 7632:     my $uname       = $env{'user.name'};
 7633:     my $udom        = $env{'user.domain'};
 7634:     my $cid         = $env{'request.course.id'};
 7635:     my $total_lines = 0;
 7636:     %bubble_lines_per_response = ();
 7637:     %first_bubble_line         = ();
 7638:     %subdivided_bubble_lines   = ();
 7639:     %responsetype_per_response = ();
 7640: 
 7641:     my $response_number = 0;
 7642:     my $bubble_line     = 0;
 7643:     foreach my $resource (@resources) {
 7644:         my ($analysis,$parts) = &scantron_partids_tograde($resource,$cid,$uname,$udom);
 7645:         if ((ref($analysis) eq 'HASH') && (ref($parts) eq 'ARRAY')) {
 7646: 	    foreach my $part_id (@{$parts}) {
 7647:                 my $lines;
 7648: 
 7649: 	        # TODO - make this a persistent hash not an array.
 7650: 
 7651:                 # optionresponse, matchresponse and rankresponse type items 
 7652:                 # render as separate sub-questions in exam mode.
 7653:                 if (($analysis->{$part_id.'.type'} eq 'optionresponse') ||
 7654:                     ($analysis->{$part_id.'.type'} eq 'matchresponse') ||
 7655:                     ($analysis->{$part_id.'.type'} eq 'rankresponse')) {
 7656:                     my ($numbub,$numshown);
 7657:                     if ($analysis->{$part_id.'.type'} eq 'optionresponse') {
 7658:                         if (ref($analysis->{$part_id.'.options'}) eq 'ARRAY') {
 7659:                             $numbub = scalar(@{$analysis->{$part_id.'.options'}});
 7660:                         }
 7661:                     } elsif ($analysis->{$part_id.'.type'} eq 'matchresponse') {
 7662:                         if (ref($analysis->{$part_id.'.items'}) eq 'ARRAY') {
 7663:                             $numbub = scalar(@{$analysis->{$part_id.'.items'}});
 7664:                         }
 7665:                     } elsif ($analysis->{$part_id.'.type'} eq 'rankresponse') {
 7666:                         if (ref($analysis->{$part_id.'.foils'}) eq 'ARRAY') {
 7667:                             $numbub = scalar(@{$analysis->{$part_id.'.foils'}});
 7668:                         }
 7669:                     }
 7670:                     if (ref($analysis->{$part_id.'.shown'}) eq 'ARRAY') {
 7671:                         $numshown = scalar(@{$analysis->{$part_id.'.shown'}});
 7672:                     }
 7673:                     my $bubbles_per_line = 10;
 7674:                     my $inner_bubble_lines = int($numbub/$bubbles_per_line);
 7675:                     if (($numbub % $bubbles_per_line) != 0) {
 7676:                         $inner_bubble_lines++;
 7677:                     }
 7678:                     for (my $i=0; $i<$numshown; $i++) {
 7679:                         $subdivided_bubble_lines{$response_number} .= 
 7680:                             $inner_bubble_lines.',';
 7681:                     }
 7682:                     $subdivided_bubble_lines{$response_number} =~ s/,$//;
 7683:                     $lines = $numshown * $inner_bubble_lines;
 7684:                 } else {
 7685:                     $lines = $analysis->{"$part_id.bubble_lines"};
 7686:                 } 
 7687: 
 7688:                 $first_bubble_line{$response_number} = $bubble_line;
 7689: 	        $bubble_lines_per_response{$response_number} = $lines;
 7690:                 $responsetype_per_response{$response_number} = 
 7691:                     $analysis->{$part_id.'.type'};
 7692: 	        $response_number++;
 7693: 
 7694: 	        $bubble_line +=  $lines;
 7695: 	        $total_lines +=  $lines;
 7696: 	    }
 7697:         }
 7698:     }
 7699:     &Apache::lonnet::delenv('scantron.');
 7700: 
 7701:     &save_bubble_lines();
 7702:     $env{'form.scantron_maxbubble'} =
 7703: 	$total_lines;
 7704:     return $env{'form.scantron_maxbubble'};
 7705: }
 7706: 
 7707: sub scantron_validate_missingbubbles {
 7708:     my ($r,$currentphase) = @_;
 7709:     #get student info
 7710:     my $classlist=&Apache::loncoursedata::get_classlist();
 7711:     my %idmap=&username_to_idmap($classlist);
 7712: 
 7713:     #get scantron line setup
 7714:     my %scantron_config=&get_scantron_config($env{'form.scantron_format'});
 7715:     my ($scanlines,$scan_data)=&scantron_getfile();
 7716:     my $nav_error;
 7717:     my $max_bubble=&scantron_get_maxbubble(\$nav_error);
 7718:     if ($nav_error) {
 7719:         return(1,$currentphase);
 7720:     }
 7721:     if (!$max_bubble) { $max_bubble=2**31; }
 7722:     for (my $i=0;$i<=$scanlines->{'count'};$i++) {
 7723: 	my $line=&scantron_get_line($scanlines,$scan_data,$i);
 7724: 	if ($line=~/^[\s\cz]*$/) { next; }
 7725: 	my $scan_record=&scantron_parse_scanline($line,$i,\%scantron_config,
 7726: 						 $scan_data);
 7727: 	if (!defined($$scan_record{'scantron.missingerror'})) { next; }
 7728: 	my @to_correct;
 7729: 	
 7730: 	# Probably here's where the error is...
 7731: 
 7732: 	foreach my $missing (@{$$scan_record{'scantron.missingerror'}}) {
 7733:             my $lastbubble;
 7734:             if ($missing =~ /^(\d+)\.(\d+)$/) {
 7735:                my $question = $1;
 7736:                my $subquestion = $2;
 7737:                if (!defined($first_bubble_line{$question -1})) { next; }
 7738:                my $first = $first_bubble_line{$question-1};
 7739:                my @subans = split(/,/,$subdivided_bubble_lines{$question-1});
 7740:                my $subcount = 1;
 7741:                while ($subcount<$subquestion) {
 7742:                    $first += $subans[$subcount-1];
 7743:                    $subcount ++;
 7744:                }
 7745:                my $count = $subans[$subquestion-1];
 7746:                $lastbubble = $first + $count;
 7747:             } else {
 7748:                 if (!defined($first_bubble_line{$missing - 1})) { next; }
 7749:                 $lastbubble = $first_bubble_line{$missing - 1} + $bubble_lines_per_response{$missing - 1};
 7750:             }
 7751:             if ($lastbubble > $max_bubble) { next; }
 7752: 	    push(@to_correct,$missing);
 7753: 	}
 7754: 	if (@to_correct) {
 7755: 	    &scantron_get_correction($r,$i,$scan_record,\%scantron_config,
 7756: 				     $line,'missingbubble',\@to_correct);
 7757: 	    return (1,$currentphase);
 7758: 	}
 7759: 
 7760:     }
 7761:     return (0,$currentphase+1);
 7762: }
 7763: 
 7764: 
 7765: sub scantron_process_students {
 7766:     my ($r) = @_;
 7767: 
 7768:     my (undef,undef,$sequence)=&Apache::lonnet::decode_symb($env{'form.selectpage'});
 7769:     my ($symb)=&get_symb($r);
 7770:     if (!$symb) {
 7771: 	return '';
 7772:     }
 7773:     my $default_form_data=&defaultFormData($symb);
 7774: 
 7775:     my %scantron_config=&get_scantron_config($env{'form.scantron_format'});
 7776:     my ($scanlines,$scan_data)=&scantron_getfile();
 7777:     my $classlist=&Apache::loncoursedata::get_classlist();
 7778:     my %idmap=&username_to_idmap($classlist);
 7779:     my $navmap=Apache::lonnavmaps::navmap->new();
 7780:     unless (ref($navmap)) {
 7781:         $r->print(&navmap_errormsg());
 7782:         return '';
 7783:     }  
 7784:     my $map=$navmap->getResourceByUrl($sequence);
 7785:     my @resources=$navmap->retrieveResources($map,\&scantron_filter,1,0);
 7786:     my (%grader_partids_by_symb,%grader_randomlists_by_symb);
 7787:     &graders_resources_pass(\@resources,\%grader_partids_by_symb,
 7788:                             \%grader_randomlists_by_symb);
 7789:     my $resource_error;
 7790:     foreach my $resource (@resources) {
 7791:         my $ressymb;
 7792:         if (ref($resource)) {
 7793:             $ressymb = $resource->symb();
 7794:         } else {
 7795:             $resource_error = 1;
 7796:             last;
 7797:         }
 7798:         my ($analysis,$parts) =
 7799:             &scantron_partids_tograde($resource,$env{'request.course.id'},
 7800:                                       $env{'user.name'},$env{'user.domain'},1);
 7801:         $grader_partids_by_symb{$ressymb} = $parts;
 7802:         if (ref($analysis) eq 'HASH') {
 7803:             if (ref($analysis->{'parts_withrandomlist'}) eq 'ARRAY') {
 7804:                 $grader_randomlists_by_symb{$ressymb} = 
 7805:                     $analysis->{'parts_withrandomlist'};
 7806:             }
 7807:         }
 7808:     }
 7809:     if ($resource_error) {
 7810:         $r->print(&navmap_errormsg());
 7811:         return '';
 7812:     }
 7813: 
 7814:     my ($uname,$udom);
 7815:     my $result= <<SCANTRONFORM;
 7816: <form method="post" enctype="multipart/form-data" action="/adm/grades" name="scantronupload">
 7817:   <input type="hidden" name="command" value="scantron_configphase" />
 7818:   $default_form_data
 7819: SCANTRONFORM
 7820:     $r->print($result);
 7821: 
 7822:     my @delayqueue;
 7823:     my (%completedstudents,%scandata);
 7824:     
 7825:     my $lock=&Apache::lonnet::set_lock(&mt('Grading bubblesheet exam'));
 7826:     my $count=&get_todo_count($scanlines,$scan_data);
 7827:     my %prog_state=&Apache::lonhtmlcommon::Create_PrgWin($r,'Bubblesheet Status',
 7828:  				    'Bubblesheet Progress',$count,
 7829: 				    'inline',undef,'scantronupload');
 7830:     &Apache::lonhtmlcommon::Update_PrgWin($r,\%prog_state,
 7831: 					  'Processing first student');
 7832:     $r->print('<br />');
 7833:     my $start=&Time::HiRes::time();
 7834:     my $i=-1;
 7835:     my $started;
 7836: 
 7837:     my $nav_error;
 7838:     &scantron_get_maxbubble(\$nav_error); # Need the bubble lines array to parse.
 7839:     if ($nav_error) {
 7840:         $r->print(&navmap_errormsg());
 7841:         return '';
 7842:     }
 7843: 
 7844:     # If an ssi failed in scantron_get_maxbubble, put an error message out to
 7845:     # the user and return.
 7846: 
 7847:     if ($ssi_error) {
 7848: 	$r->print("</form>");
 7849: 	&ssi_print_error($r);
 7850: 	$r->print(&show_grading_menu_form($symb));
 7851:         &Apache::lonnet::remove_lock($lock);
 7852: 	return '';		# Dunno why the other returns return '' rather than just returning.
 7853:     }
 7854: 
 7855:     my %lettdig = &letter_to_digits();
 7856:     my $numletts = scalar(keys(%lettdig));
 7857: 
 7858:     while ($i<$scanlines->{'count'}) {
 7859:  	($uname,$udom)=('','');
 7860:  	$i++;
 7861:  	my $line=&scantron_get_line($scanlines,$scan_data,$i);
 7862:  	if ($line=~/^[\s\cz]*$/) { next; }
 7863: 	if ($started) {
 7864: 	    &Apache::lonhtmlcommon::Increment_PrgWin($r,\%prog_state,
 7865: 						     'last student');
 7866: 	}
 7867: 	$started=1;
 7868:  	my $scan_record=&scantron_parse_scanline($line,$i,\%scantron_config,
 7869:  						 $scan_data);
 7870:  	unless ($uname=&scantron_find_student($scan_record,$scan_data,
 7871:  					      \%idmap,$i)) {
 7872:   	    &scantron_add_delay(\@delayqueue,$line,
 7873:  				'Unable to find a student that matches',1);
 7874:  	    next;
 7875:   	}
 7876:  	if (exists $completedstudents{$uname}) {
 7877:  	    &scantron_add_delay(\@delayqueue,$line,
 7878:  				'Student '.$uname.' has multiple sheets',2);
 7879:  	    next;
 7880:  	}
 7881:   	($uname,$udom)=split(/:/,$uname);
 7882: 
 7883:         my (%partids_by_symb,$res_error);
 7884:         foreach my $resource (@resources) {
 7885:             my $ressymb;
 7886:             if (ref($resource)) {
 7887:                 $ressymb = $resource->symb();
 7888:             } else {
 7889:                 $res_error = 1;
 7890:                 last;
 7891:             }
 7892:             if ((exists($grader_randomlists_by_symb{$ressymb})) ||
 7893:                 (ref($grader_partids_by_symb{$ressymb}) ne 'ARRAY')) {
 7894:                 my ($analysis,$parts) =
 7895:                     &scantron_partids_tograde($resource,$env{'request.course.id'},$uname,$udom);
 7896:                 $partids_by_symb{$ressymb} = $parts;
 7897:             } else {
 7898:                 $partids_by_symb{$ressymb} = $grader_partids_by_symb{$ressymb};
 7899:             }
 7900:         }
 7901: 
 7902:         if ($res_error) {
 7903:             &scantron_add_delay(\@delayqueue,$line,
 7904:                                 'An error occurred while grading student '.$uname,2);
 7905:             next;
 7906:         }
 7907: 
 7908: 	&Apache::lonxml::clear_problem_counter();
 7909:   	&Apache::lonnet::appenv($scan_record);
 7910: 
 7911: 	if (&scantron_clear_skip($scanlines,$scan_data,$i)) {
 7912: 	    &scantron_putfile($scanlines,$scan_data);
 7913: 	}
 7914: 	
 7915:         my $scancode;
 7916:         if ((exists($scan_record->{'scantron.CODE'})) &&
 7917:             (&Apache::lonnet::validCODE($scan_record->{'scantron.CODE'}))) {
 7918:             $scancode = $scan_record->{'scantron.CODE'};
 7919:         } else {
 7920:             $scancode = '';
 7921:         }
 7922: 
 7923:         if (&grade_student_bubbles($r,$uname,$udom,$scan_record,$scancode,
 7924:                                    \@resources,\%partids_by_symb) eq 'ssi_error') {
 7925:             $ssi_error = 0; # So end of handler error message does not trigger.
 7926:             $r->print("</form>");
 7927:             &ssi_print_error($r);
 7928:             $r->print(&show_grading_menu_form($symb));
 7929:             &Apache::lonnet::remove_lock($lock);
 7930:             return '';      # Why return ''?  Beats me.
 7931:         }
 7932: 
 7933: 	$completedstudents{$uname}={'line'=>$line};
 7934:         if ($env{'form.verifyrecord'}) {
 7935:             my $lastpos = $env{'form.scantron_maxbubble'}*$scantron_config{'Qlength'};
 7936:             my $studentdata = substr($line,$scantron_config{'Qstart'}-1,$lastpos);
 7937:             chomp($studentdata);
 7938:             $studentdata =~ s/\r$//;
 7939:             my $studentrecord = '';
 7940:             my $counter = -1;
 7941:             foreach my $resource (@resources) {
 7942:                 my $ressymb = $resource->symb();
 7943:                 ($counter,my $recording) =
 7944:                     &verify_scantron_grading($resource,$udom,$uname,$env{'request.course.id'},
 7945:                                              $counter,$studentdata,$partids_by_symb{$ressymb},
 7946:                                              \%scantron_config,\%lettdig,$numletts);
 7947:                 $studentrecord .= $recording;
 7948:             }
 7949:             if ($studentrecord ne $studentdata) {
 7950:                 &Apache::lonxml::clear_problem_counter();
 7951:                 if (&grade_student_bubbles($r,$uname,$udom,$scan_record,$scancode,
 7952:                                            \@resources,\%partids_by_symb) eq 'ssi_error') {
 7953:                     $ssi_error = 0; # So end of handler error message does not trigger.
 7954:                     $r->print("</form>");
 7955:                     &ssi_print_error($r);
 7956:                     $r->print(&show_grading_menu_form($symb));
 7957:                     &Apache::lonnet::remove_lock($lock);
 7958:                     delete($completedstudents{$uname});
 7959:                     return '';
 7960:                 }
 7961:                 $counter = -1;
 7962:                 $studentrecord = '';
 7963:                 foreach my $resource (@resources) {
 7964:                     my $ressymb = $resource->symb();
 7965:                     ($counter,my $recording) =
 7966:                         &verify_scantron_grading($resource,$udom,$uname,$env{'request.course.id'},
 7967:                                                  $counter,$studentdata,$partids_by_symb{$ressymb},
 7968:                                                  \%scantron_config,\%lettdig,$numletts);
 7969:                     $studentrecord .= $recording;
 7970:                 }
 7971:                 if ($studentrecord ne $studentdata) {
 7972:                     $r->print('<p><span class="LC_error">');
 7973:                     if ($scancode eq '') {
 7974:                         $r->print(&mt('Mismatch grading bubble sheet for user: [_1] with ID: [_2].',
 7975:                                   $uname.':'.$udom,$scan_record->{'scantron.ID'}));
 7976:                     } else {
 7977:                         $r->print(&mt('Mismatch grading bubble sheet for user: [_1] with ID: [_2] and CODE: [_3].',
 7978:                                   $uname.':'.$udom,$scan_record->{'scantron.ID'},$scancode));
 7979:                     }
 7980:                     $r->print('</span><br />'.&Apache::loncommon::start_data_table()."\n".
 7981:                               &Apache::loncommon::start_data_table_header_row()."\n".
 7982:                               '<th>'.&mt('Source').'</th><th>'.&mt('Bubbled responses').'</th>'.
 7983:                               &Apache::loncommon::end_data_table_header_row()."\n".
 7984:                               &Apache::loncommon::start_data_table_row().
 7985:                               '<td>'.&mt('Bubble Sheet').'</td>'.
 7986:                               '<td><span class="LC_nobreak">'.$studentdata.'</span></td>'.
 7987:                               &Apache::loncommon::end_data_table_row().
 7988:                               &Apache::loncommon::start_data_table_row().
 7989:                               '<td>Stored submissions</td>'.
 7990:                               '<td><span class="LC_nobreak">'.$studentrecord.'</span></td>'."\n".
 7991:                               &Apache::loncommon::end_data_table_row().
 7992:                               &Apache::loncommon::end_data_table().'</p>');
 7993:                 } else {
 7994:                     $r->print('<br /><span class="LC_warning">'.
 7995:                              &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 />'.
 7996:                              &mt("As a consequence, this user's submission history records two tries.").
 7997:                                  '</span><br />');
 7998:                 }
 7999:             }
 8000:         }
 8001:         if (&Apache::loncommon::connection_aborted($r)) { last; }
 8002:     } continue {
 8003: 	&Apache::lonxml::clear_problem_counter();
 8004: 	&Apache::lonnet::delenv('scantron.');
 8005:     }
 8006:     &Apache::lonhtmlcommon::Close_PrgWin($r,\%prog_state);
 8007:     &Apache::lonnet::remove_lock($lock);
 8008: #    my $lasttime = &Time::HiRes::time()-$start;
 8009: #    $r->print("<p>took $lasttime</p>");
 8010: 
 8011:     $r->print("</form>");
 8012:     $r->print(&show_grading_menu_form($symb));
 8013:     return '';
 8014: }
 8015: 
 8016: sub graders_resources_pass {
 8017:     my ($resources,$grader_partids_by_symb,$grader_randomlists_by_symb) = @_;
 8018:     if ((ref($resources) eq 'ARRAY') && (ref($grader_partids_by_symb)) && 
 8019:         (ref($grader_randomlists_by_symb) eq 'HASH')) {
 8020:         foreach my $resource (@{$resources}) {
 8021:             my $ressymb = $resource->symb();
 8022:             my ($analysis,$parts) =
 8023:                 &scantron_partids_tograde($resource,$env{'request.course.id'},
 8024:                                           $env{'user.name'},$env{'user.domain'},1);
 8025:             $grader_partids_by_symb->{$ressymb} = $parts;
 8026:             if (ref($analysis) eq 'HASH') {
 8027:                 if (ref($analysis->{'parts_withrandomlist'}) eq 'ARRAY') {
 8028:                     $grader_randomlists_by_symb->{$ressymb} =
 8029:                         $analysis->{'parts_withrandomlist'};
 8030:                 }
 8031:             }
 8032:         }
 8033:     }
 8034:     return;
 8035: }
 8036: 
 8037: sub grade_student_bubbles {
 8038:     my ($r,$uname,$udom,$scan_record,$scancode,$resources,$parts) = @_;
 8039:     if (ref($resources) eq 'ARRAY') {
 8040:         my $count = 0;
 8041:         foreach my $resource (@{$resources}) {
 8042:             my $ressymb = $resource->symb();
 8043:             my %form = ('submitted'      => 'scantron',
 8044:                         'grade_target'   => 'grade',
 8045:                         'grade_username' => $uname,
 8046:                         'grade_domain'   => $udom,
 8047:                         'grade_courseid' => $env{'request.course.id'},
 8048:                         'grade_symb'     => $ressymb,
 8049:                         'CODE'           => $scancode
 8050:                        );
 8051:             if (ref($parts) eq 'HASH') {
 8052:                 if (ref($parts->{$ressymb}) eq 'ARRAY') {
 8053:                     foreach my $part (@{$parts->{$ressymb}}) {
 8054:                         $form{'scantron_questnum_start.'.$part} =
 8055:                             1+$env{'form.scantron.first_bubble_line.'.$count};
 8056:                         $count++;
 8057:                     }
 8058:                 }
 8059:             }
 8060:             my $result=&ssi_with_retries($resource->src(),$ssi_retries,%form);
 8061:             return 'ssi_error' if ($ssi_error);
 8062:             last if (&Apache::loncommon::connection_aborted($r));
 8063:         }
 8064:     }
 8065:     return;
 8066: }
 8067: 
 8068: sub scantron_upload_scantron_data {
 8069:     my ($r)=@_;
 8070:     my $dom = $env{'request.role.domain'};
 8071:     my $domdesc = &Apache::lonnet::domain($dom,'description');
 8072:     $r->print(&Apache::loncommon::coursebrowser_javascript($dom));
 8073:     my $select_link=&Apache::loncommon::selectcourse_link('rules','courseid',
 8074: 							  'domainid',
 8075: 							  'coursename',$dom);
 8076:     my $syllabuslink = '<a href="javascript:ToSyllabus();">'.&mt('Syllabus').'</a>'.
 8077:                        ('&nbsp'x2).&mt('(shows course personnel)'); 
 8078:     my $default_form_data=&defaultFormData(&get_symb($r,1));
 8079:     my $nofile_alert = &mt('Please use the browse button to select a file from your local directory.');
 8080:     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.");
 8081:     $r->print('
 8082: <script type="text/javascript" language="javascript">
 8083:     function checkUpload(formname) {
 8084: 	if (formname.upfile.value == "") {
 8085: 	    alert("'.$nofile_alert.'");
 8086: 	    return false;
 8087: 	}
 8088:         if (formname.courseid.value == "") {
 8089:             alert("'.$nocourseid_alert.'");
 8090:             return false;
 8091:         }
 8092: 	formname.submit();
 8093:     }
 8094: 
 8095:     function ToSyllabus() {
 8096:         var cdom = '."'$dom'".';
 8097:         var cnum = document.rules.courseid.value;
 8098:         if (cdom == "" || cdom == null) {
 8099:             return;
 8100:         }
 8101:         if (cnum == "" || cnum == null) {
 8102:            return;
 8103:         }
 8104:         syllwin=window.open("/public/"+cdom+"/"+cnum+"/syllabus","LONCAPASyllabus",
 8105:                             "height=350,width=350,scrollbars=yes,menubar=no");
 8106:         return;
 8107:     }
 8108: 
 8109: </script>
 8110: 
 8111: <h3>'.&mt('Send bubblesheet data to a course').'</h3>
 8112: 
 8113: <form enctype="multipart/form-data" action="/adm/grades" name="rules" method="post">
 8114: '.$default_form_data.
 8115:   &Apache::lonhtmlcommon::start_pick_box().
 8116:   &Apache::lonhtmlcommon::row_title(&mt('Course ID')).
 8117:   '<input name="courseid" type="text" size="30" />'.$select_link.
 8118:   &Apache::lonhtmlcommon::row_closure().
 8119:   &Apache::lonhtmlcommon::row_title(&mt('Course Name')).
 8120:   '<input name="coursename" type="text" size="30" />'.$syllabuslink.
 8121:   &Apache::lonhtmlcommon::row_closure().
 8122:   &Apache::lonhtmlcommon::row_title(&mt('Domain')).
 8123:   '<input name="domainid" type="hidden" />'.$domdesc.
 8124:   &Apache::lonhtmlcommon::row_closure().
 8125:   &Apache::lonhtmlcommon::row_title(&mt('File to upload')).
 8126:   '<input type="file" name="upfile" size="50" />'.
 8127:   &Apache::lonhtmlcommon::row_closure(1).
 8128:   &Apache::lonhtmlcommon::end_pick_box().'<br />
 8129: 
 8130: <input name="command" value="scantronupload_save" type="hidden" />
 8131: <input type="button" onclick="javascript:checkUpload(this.form);" value="'.&mt('Upload Bubblesheet Data').'" />
 8132: </form>
 8133: ');
 8134:     return '';
 8135: }
 8136: 
 8137: 
 8138: sub scantron_upload_scantron_data_save {
 8139:     my($r)=@_;
 8140:     my ($symb)=&get_symb($r,1);
 8141:     my $doanotherupload=
 8142: 	'<br /><form action="/adm/grades" method="post">'."\n".
 8143: 	'<input type="hidden" name="command" value="scantronupload" />'."\n".
 8144: 	'<input type="submit" name="submit" value="'.&mt('Do Another Upload').'" />'."\n".
 8145: 	'</form>'."\n";
 8146:     if (!&Apache::lonnet::allowed('usc',$env{'form.domainid'}) &&
 8147: 	!&Apache::lonnet::allowed('usc',
 8148: 			    $env{'form.domainid'}.'_'.$env{'form.courseid'})) {
 8149: 	$r->print(&mt("You are not allowed to upload bubblesheet data to the requested course.")."<br />");
 8150: 	if ($symb) {
 8151: 	    $r->print(&show_grading_menu_form($symb));
 8152: 	} else {
 8153: 	    $r->print($doanotherupload);
 8154: 	}
 8155: 	return '';
 8156:     }
 8157:     my %coursedata=&Apache::lonnet::coursedescription($env{'form.domainid'}.'_'.$env{'form.courseid'});
 8158:     my $uploadedfile;
 8159:     $r->print('<h3>'.&mt("Uploading file to [_1]",$coursedata{'description'}).'</h3>');
 8160:     if (length($env{'form.upfile'}) < 2) {
 8161:         $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>'));
 8162:     } else {
 8163:         my $result = 
 8164:             &Apache::lonnet::userfileupload('upfile','','scantron','','','',
 8165:                                             $env{'form.courseid'},$env{'form.domainid'});
 8166: 	if ($result =~ m{^/uploaded/}) {
 8167: 	    $r->print(&mt('[_1]Success:[_2] Successfully uploaded [_3] bytes of data into location: [_4]',
 8168:                           '<span class="LC_success">','</span>',(length($env{'form.upfile'})-1),
 8169: 			  '<span class="LC_filename">'.$result.'</span>'));
 8170:             ($uploadedfile) = ($result =~ m{/([^/]+)$});
 8171:             $r->print(&validate_uploaded_scantron_file($env{'form.domainid'},
 8172:                                                        $env{'form.courseid'},$uploadedfile));
 8173: 	} else {
 8174: 	    $r->print(&mt('[_1]Error:[_2] An error ([_3]) occurred when attempting to upload the file, [_4]',
 8175:                           '<span class="LC_error">','</span>',$result,
 8176: 			  '<span class="LC_filename">'.&HTML::Entities::encode($env{'form.upfile.filename'},'<>&"').'</span>'));
 8177: 	}
 8178:     }
 8179:     if ($symb) {
 8180: 	$r->print(&scantron_selectphase($r,$uploadedfile));
 8181:     } else {
 8182: 	$r->print($doanotherupload);
 8183:     }
 8184:     return '';
 8185: }
 8186: 
 8187: sub validate_uploaded_scantron_file {
 8188:     my ($cdom,$cname,$fname) = @_;
 8189:     my $scanlines=&Apache::lonnet::getfile('/uploaded/'.$cdom.'/'.$cname.'/'.$fname);
 8190:     my @lines;
 8191:     if ($scanlines ne '-1') {
 8192:         @lines=split("\n",$scanlines,-1);
 8193:     }
 8194:     my $output;
 8195:     if (@lines) {
 8196:         my (%counts,$max_match_format);
 8197:         my ($max_match_count,$max_match_pct) = (0,0);
 8198:         my $classlist = &Apache::loncoursedata::get_classlist($cdom,$cname);
 8199:         my %idmap = &username_to_idmap($classlist);
 8200:         foreach my $key (keys(%idmap)) {
 8201:             my $lckey = lc($key);
 8202:             $idmap{$lckey} = $idmap{$key};
 8203:         }
 8204:         my %unique_formats;
 8205:         my @formatlines = &get_scantronformat_file();
 8206:         foreach my $line (@formatlines) {
 8207:             chomp($line);
 8208:             my @config = split(/:/,$line);
 8209:             my $idstart = $config[5];
 8210:             my $idlength = $config[6];
 8211:             if (($idstart ne '') && ($idlength > 0)) {
 8212:                 if (ref($unique_formats{$idstart.':'.$idlength}) eq 'ARRAY') {
 8213:                     push(@{$unique_formats{$idstart.':'.$idlength}},$config[0].':'.$config[1]); 
 8214:                 } else {
 8215:                     $unique_formats{$idstart.':'.$idlength} = [$config[0].':'.$config[1]];
 8216:                 }
 8217:             }
 8218:         }
 8219:         foreach my $key (keys(%unique_formats)) {
 8220:             my ($idstart,$idlength) = split(':',$key);
 8221:             %{$counts{$key}} = (
 8222:                                'found'   => 0,
 8223:                                'total'   => 0,
 8224:                               );
 8225:             foreach my $line (@lines) {
 8226:                 next if ($line =~ /^#/);
 8227:                 next if ($line =~ /^[\s\cz]*$/);
 8228:                 my $id = substr($line,$idstart-1,$idlength);
 8229:                 $id = lc($id);
 8230:                 if (exists($idmap{$id})) {
 8231:                     $counts{$key}{'found'} ++;
 8232:                 }
 8233:                 $counts{$key}{'total'} ++;
 8234:             }
 8235:             if ($counts{$key}{'total'}) {
 8236:                 my $percent_match = (100*$counts{$key}{'found'})/($counts{$key}{'total'});
 8237:                 if (($max_match_format eq '') || ($percent_match > $max_match_pct)) {
 8238:                     $max_match_pct = $percent_match;
 8239:                     $max_match_format = $key;
 8240:                     $max_match_count = $counts{$key}{'total'};
 8241:                 }
 8242:             }
 8243:         }
 8244:         if (ref($unique_formats{$max_match_format}) eq 'ARRAY') {
 8245:             my $format_descs;
 8246:             my $numwithformat = @{$unique_formats{$max_match_format}};
 8247:             for (my $i=0; $i<$numwithformat; $i++) {
 8248:                 my ($name,$desc) = split(':',$unique_formats{$max_match_format}[$i]);
 8249:                 if ($i<$numwithformat-2) {
 8250:                     $format_descs .= '"<i>'.$desc.'</i>", ';
 8251:                 } elsif ($i==$numwithformat-2) {
 8252:                     $format_descs .= '"<i>'.$desc.'</i>" '.&mt('and').' ';
 8253:                 } elsif ($i==$numwithformat-1) {
 8254:                     $format_descs .= '"<i>'.$desc.'</i>"';
 8255:                 }
 8256:             }
 8257:             my $showpct = sprintf("%.0f",$max_match_pct).'%';
 8258:             $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).
 8259:                        '<br />'.&mt('A low percentage of matches results from one of the following:').'<ul>'.
 8260:                        '<li>'.&mt('The file was uploaded to the wrong course').'</li>'.
 8261:                        '<li>'.&mt('The data are not in the format expected for the domain: [_1]',
 8262:                                   '<i>'.$cdom.'</i>').'</li>'.
 8263:                        '<li>'.&mt('Students did not bubble their IDs, or mis-bubbled them').'</li>'.
 8264:                        '<li>'.&mt('The course roster is not up to date').'</li>'.
 8265:                        '</ul>';
 8266:         }
 8267:     } else {
 8268:         $output = '<span class="LC_warning">'.&mt('Uploaded file contained no data').'</span>';
 8269:     }
 8270:     return $output;
 8271: }
 8272: 
 8273: sub valid_file {
 8274:     my ($requested_file)=@_;
 8275:     foreach my $filename (sort(&scantron_filenames())) {
 8276: 	if ($requested_file eq $filename) { return 1; }
 8277:     }
 8278:     return 0;
 8279: }
 8280: 
 8281: sub scantron_download_scantron_data {
 8282:     my ($r)=@_;
 8283:     my $default_form_data=&defaultFormData(&get_symb($r,1));
 8284:     my $cname=$env{'course.'.$env{'request.course.id'}.'.num'};
 8285:     my $cdom=$env{'course.'.$env{'request.course.id'}.'.domain'};
 8286:     my $file=$env{'form.scantron_selectfile'};
 8287:     if (! &valid_file($file)) {
 8288: 	$r->print('
 8289: 	<p>
 8290: 	    '.&mt('The requested file name was invalid.').'
 8291:         </p>
 8292: ');
 8293: 	$r->print(&show_grading_menu_form(&get_symb($r,1)));
 8294: 	return;
 8295:     }
 8296:     my $orig='/uploaded/'.$cdom.'/'.$cname.'/scantron_orig_'.$file;
 8297:     my $corrected='/uploaded/'.$cdom.'/'.$cname.'/scantron_corrected_'.$file;
 8298:     my $skipped='/uploaded/'.$cdom.'/'.$cname.'/scantron_skipped_'.$file;
 8299:     &Apache::lonnet::allowuploaded('/adm/grades',$orig);
 8300:     &Apache::lonnet::allowuploaded('/adm/grades',$corrected);
 8301:     &Apache::lonnet::allowuploaded('/adm/grades',$skipped);
 8302:     $r->print('
 8303:     <p>
 8304: 	'.&mt('[_1]Original[_2] file as uploaded by the scantron office.',
 8305: 	      '<a href="'.$orig.'">','</a>').'
 8306:     </p>
 8307:     <p>
 8308: 	'.&mt('[_1]Corrections[_2], a file of corrected records that were used in grading.',
 8309: 	      '<a href="'.$corrected.'">','</a>').'
 8310:     </p>
 8311:     <p>
 8312: 	'.&mt('[_1]Skipped[_2], a file of records that were skipped.',
 8313: 	      '<a href="'.$skipped.'">','</a>').'
 8314:     </p>
 8315: ');
 8316:     $r->print(&show_grading_menu_form(&get_symb($r,1)));
 8317:     return '';
 8318: }
 8319: 
 8320: sub checkscantron_results {
 8321:     my ($r) = @_;
 8322:     my ($symb)=&get_symb($r);
 8323:     if (!$symb) {return '';}
 8324:     my $grading_menu_button=&show_grading_menu_form($symb);
 8325:     my $cid = $env{'request.course.id'};
 8326:     my %lettdig = &letter_to_digits();
 8327:     my $numletts = scalar(keys(%lettdig));
 8328:     my $cnum = $env{'course.'.$cid.'.num'};
 8329:     my $cdom = $env{'course.'.$cid.'.domain'};
 8330:     my (undef, undef, $sequence) = &Apache::lonnet::decode_symb($env{'form.selectpage'});
 8331:     my %record;
 8332:     my %scantron_config =
 8333:         &Apache::grades::get_scantron_config($env{'form.scantron_format'});
 8334:     my ($scanlines,$scan_data)=&Apache::grades::scantron_getfile();
 8335:     my $classlist=&Apache::loncoursedata::get_classlist();
 8336:     my %idmap=&Apache::grades::username_to_idmap($classlist);
 8337:     my $navmap=Apache::lonnavmaps::navmap->new();
 8338:     unless (ref($navmap)) {
 8339:         $r->print(&navmap_errormsg());
 8340:         return '';
 8341:     }
 8342:     my $map=$navmap->getResourceByUrl($sequence);
 8343:     my @resources=$navmap->retrieveResources($map,\&scantron_filter,1,0);
 8344:     my (%grader_partids_by_symb,%grader_randomlists_by_symb);
 8345:     &graders_resources_pass(\@resources,\%grader_partids_by_symb,                             \%grader_randomlists_by_symb);
 8346: 
 8347:     my ($uname,$udom);
 8348:     my (%scandata,%lastname,%bylast);
 8349:     $r->print('
 8350: <form method="post" enctype="multipart/form-data" action="/adm/grades" name="checkscantron">'."\n");
 8351: 
 8352:     my @delayqueue;
 8353:     my %completedstudents;
 8354: 
 8355:     my $count=&Apache::grades::get_todo_count($scanlines,$scan_data);
 8356:     my %prog_state=&Apache::lonhtmlcommon::Create_PrgWin($r,'Bubblesheet/Submissions Comparison Status',
 8357:                                     'Progress of Bubblesheet Data/Submission Records Comparison',$count,
 8358:                                     'inline',undef,'checkscantron');
 8359:     my ($username,$domain,$started);
 8360:     my $nav_error;
 8361:     &scantron_get_maxbubble(\$nav_error); # Need the bubble lines array to parse.
 8362:     if ($nav_error) {
 8363:         $r->print(&navmap_errormsg());
 8364:         return '';
 8365:     }
 8366: 
 8367:     &Apache::lonhtmlcommon::Update_PrgWin($r,\%prog_state,
 8368:                                           'Processing first student');
 8369:     my $start=&Time::HiRes::time();
 8370:     my $i=-1;
 8371: 
 8372:     while ($i<$scanlines->{'count'}) {
 8373:         ($username,$domain,$uname)=('','','');
 8374:         $i++;
 8375:         my $line=&Apache::grades::scantron_get_line($scanlines,$scan_data,$i);
 8376:         if ($line=~/^[\s\cz]*$/) { next; }
 8377:         if ($started) {
 8378:             &Apache::lonhtmlcommon::Increment_PrgWin($r,\%prog_state,
 8379:                                                      'last student');
 8380:         }
 8381:         $started=1;
 8382:         my $scan_record=
 8383:             &Apache::grades::scantron_parse_scanline($line,$i,\%scantron_config,
 8384:                                                      $scan_data);
 8385:         unless ($uname=&Apache::grades::scantron_find_student($scan_record,$scan_data,
 8386:                                                               \%idmap,$i)) {
 8387:             &Apache::grades::scantron_add_delay(\@delayqueue,$line,
 8388:                                 'Unable to find a student that matches',1);
 8389:             next;
 8390:         }
 8391:         if (exists $completedstudents{$uname}) {
 8392:             &Apache::grades::scantron_add_delay(\@delayqueue,$line,
 8393:                                 'Student '.$uname.' has multiple sheets',2);
 8394:             next;
 8395:         }
 8396:         my $pid = $scan_record->{'scantron.ID'};
 8397:         $lastname{$pid} = $scan_record->{'scantron.LastName'};
 8398:         push(@{$bylast{$lastname{$pid}}},$pid);
 8399:         my $lastpos = $env{'form.scantron_maxbubble'}*$scantron_config{'Qlength'};
 8400:         $scandata{$pid} = substr($line,$scantron_config{'Qstart'}-1,$lastpos);
 8401:         chomp($scandata{$pid});
 8402:         $scandata{$pid} =~ s/\r$//;
 8403:         ($username,$domain)=split(/:/,$uname);
 8404:         my $counter = -1;
 8405:         foreach my $resource (@resources) {
 8406:             my $parts;
 8407:             my $ressymb = $resource->symb();
 8408:             if ((exists($grader_randomlists_by_symb{$ressymb})) ||
 8409:                 (ref($grader_partids_by_symb{$ressymb}) ne 'ARRAY')) {
 8410:                 (my $analysis,$parts) =
 8411:                     &scantron_partids_tograde($resource,$env{'request.course.id'},$username,$domain);
 8412:             } else {
 8413:                 $parts = $grader_partids_by_symb{$ressymb};
 8414:             }
 8415:             ($counter,my $recording) =
 8416:                 &verify_scantron_grading($resource,$domain,$username,$cid,$counter,
 8417:                                          $scandata{$pid},$parts,
 8418:                                          \%scantron_config,\%lettdig,$numletts);
 8419:             $record{$pid} .= $recording;
 8420:         }
 8421:     }
 8422:     &Apache::lonhtmlcommon::Close_PrgWin($r,\%prog_state);
 8423:     $r->print('<br />');
 8424:     my ($okstudents,$badstudents,$numstudents,$passed,$failed);
 8425:     $passed = 0;
 8426:     $failed = 0;
 8427:     $numstudents = 0;
 8428:     foreach my $last (sort(keys(%bylast))) {
 8429:         if (ref($bylast{$last}) eq 'ARRAY') {
 8430:             foreach my $pid (sort(@{$bylast{$last}})) {
 8431:                 my $showscandata = $scandata{$pid};
 8432:                 my $showrecord = $record{$pid};
 8433:                 $showscandata =~ s/\s/&nbsp;/g;
 8434:                 $showrecord =~ s/\s/&nbsp;/g;
 8435:                 if ($scandata{$pid} eq $record{$pid}) {
 8436:                     my $css_class = ($passed % 2)?'LC_odd_row':'LC_even_row';
 8437:                     $okstudents .= '<tr class="'.$css_class.'">'.
 8438: '<td>'.&mt('Bubblesheet').'</td><td>'.$showscandata.'</td><td rowspan="2">'.$last.'</td><td rowspan="2">'.$pid.'</td>'."\n".
 8439: '</tr>'."\n".
 8440: '<tr class="'.$css_class.'">'."\n".
 8441: '<td>Submissions</td><td>'.$showrecord.'</td></tr>'."\n";
 8442:                     $passed ++;
 8443:                 } else {
 8444:                     my $css_class = ($failed % 2)?'LC_odd_row':'LC_even_row';
 8445:                     $badstudents .= '<tr class="'.$css_class.'"><td>'.&mt('Bubblesheet').'</td><td><span class="LC_nobreak">'.$scandata{$pid}.'</span></td><td rowspan="2">'.$last.'</td><td rowspan="2">'.$pid.'</td>'."\n".
 8446: '</tr>'."\n".
 8447: '<tr class="'.$css_class.'">'."\n".
 8448: '<td>Submissions</td><td><span class="LC_nobreak">'.$record{$pid}.'</span></td>'."\n".
 8449: '</tr>'."\n";
 8450:                     $failed ++;
 8451:                 }
 8452:                 $numstudents ++;
 8453:             }
 8454:         }
 8455:     }
 8456:     $r->print('<p>'.
 8457:               &mt('Comparison of bubblesheet data (including corrections) with corresponding submission records (most recent submission) for [_1][quant,_2,student][_3]  ([_4] bubblesheet lines/student).',
 8458:                   '<b>',
 8459:                   $numstudents,
 8460:                   '</b>',
 8461:                   $env{'form.scantron_maxbubble'}).
 8462:               '</p>'
 8463:     );
 8464:     $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>');
 8465:     if ($passed) {
 8466:         $r->print(&mt('Students with exact correspondence between bubblesheet data and submissions are as follows:').'<br /><br />');
 8467:         $r->print(&Apache::loncommon::start_data_table()."\n".
 8468:                  &Apache::loncommon::start_data_table_header_row()."\n".
 8469:                  '<th>'.&mt('Source').'</th><th>'.&mt('Bubble records').'</th><th>'.&mt('Name').'</th><th>'.&mt('ID').'</th>'.
 8470:                  &Apache::loncommon::end_data_table_header_row()."\n".
 8471:                  $okstudents."\n".
 8472:                  &Apache::loncommon::end_data_table().'<br />');
 8473:     }
 8474:     if ($failed) {
 8475:         $r->print(&mt('Students with differences between bubblesheet data and submissions are as follows:').'<br /><br />');
 8476:         $r->print(&Apache::loncommon::start_data_table()."\n".
 8477:                  &Apache::loncommon::start_data_table_header_row()."\n".
 8478:                  '<th>'.&mt('Source').'</th><th>'.&mt('Bubble records').'</th><th>'.&mt('Name').'</th><th>'.&mt('ID').'</th>'.
 8479:                  &Apache::loncommon::end_data_table_header_row()."\n".
 8480:                  $badstudents."\n".
 8481:                  &Apache::loncommon::end_data_table()).'<br />'.
 8482:                  &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.');  
 8483:     }
 8484:     $r->print('</form><br />'.$grading_menu_button);
 8485:     return;
 8486: }
 8487: 
 8488: sub verify_scantron_grading {
 8489:     my ($resource,$domain,$username,$cid,$counter,$scandata,$partids,
 8490:         $scantron_config,$lettdig,$numletts) = @_;
 8491:     my ($record,%expected,%startpos);
 8492:     return ($counter,$record) if (!ref($resource));
 8493:     return ($counter,$record) if (!$resource->is_problem());
 8494:     my $symb = $resource->symb();
 8495:     return ($counter,$record) if (ref($partids) ne 'ARRAY');
 8496:     foreach my $part_id (@{$partids}) {
 8497:         $counter ++;
 8498:         $expected{$part_id} = 0;
 8499:         if ($env{"form.scantron.sub_bubblelines.$counter"}) {
 8500:             my @sub_lines = split(/,/,$env{"form.scantron.sub_bubblelines.$counter"});
 8501:             foreach my $item (@sub_lines) {
 8502:                 $expected{$part_id} += $item;
 8503:             }
 8504:         } else {
 8505:             $expected{$part_id} = $env{"form.scantron.bubblelines.$counter"};
 8506:         }
 8507:         $startpos{$part_id} = $env{"form.scantron.first_bubble_line.$counter"};
 8508:     }
 8509:     if ($symb) {
 8510:         my %recorded;
 8511:         my (%returnhash) = &Apache::lonnet::restore($symb,$cid,$domain,$username);
 8512:         if ($returnhash{'version'}) {
 8513:             my %lasthash=();
 8514:             my $version;
 8515:             for ($version=1;$version<=$returnhash{'version'};$version++) {
 8516:                 foreach my $key (sort(split(/\:/,$returnhash{$version.':keys'}))) {
 8517:                     $lasthash{$key}=$returnhash{$version.':'.$key};
 8518:                 }
 8519:             }
 8520:             foreach my $key (keys(%lasthash)) {
 8521:                 if ($key =~ /\.scantron$/) {
 8522:                     my $value = &unescape($lasthash{$key});
 8523:                     my ($part_id) = ($key =~ /^resource\.(.+)\.scantron$/);
 8524:                     if ($value eq '') {
 8525:                         for (my $i=0; $i<$expected{$part_id}; $i++) {
 8526:                             for (my $j=0; $j<$scantron_config->{'length'}; $j++) {
 8527:                                 $recorded{$part_id} .= $scantron_config->{'Qoff'};
 8528:                             }
 8529:                         }
 8530:                     } else {
 8531:                         my @tocheck;
 8532:                         my @items = split(//,$value);
 8533:                         if (($scantron_config->{'Qon'} eq 'letter') ||
 8534:                             ($scantron_config->{'Qon'} eq 'number')) {
 8535:                             if (@items < $expected{$part_id}) {
 8536:                                 my $fragment = substr($scandata,$startpos{$part_id},$expected{$part_id});
 8537:                                 my @singles = split(//,$fragment);
 8538:                                 foreach my $pos (@singles) {
 8539:                                     if ($pos eq ' ') {
 8540:                                         push(@tocheck,$pos);
 8541:                                     } else {
 8542:                                         my $next = shift(@items);
 8543:                                         push(@tocheck,$next);
 8544:                                     }
 8545:                                 }
 8546:                             } else {
 8547:                                 @tocheck = @items;
 8548:                             }
 8549:                             foreach my $letter (@tocheck) {
 8550:                                 if ($scantron_config->{'Qon'} eq 'letter') {
 8551:                                     if ($letter !~ /^[A-J]$/) {
 8552:                                         $letter = $scantron_config->{'Qoff'};
 8553:                                     }
 8554:                                     $recorded{$part_id} .= $letter;
 8555:                                 } elsif ($scantron_config->{'Qon'} eq 'number') {
 8556:                                     my $digit;
 8557:                                     if ($letter !~ /^[A-J]$/) {
 8558:                                         $digit = $scantron_config->{'Qoff'};
 8559:                                     } else {
 8560:                                         $digit = $lettdig->{$letter};
 8561:                                     }
 8562:                                     $recorded{$part_id} .= $digit;
 8563:                                 }
 8564:                             }
 8565:                         } else {
 8566:                             @tocheck = @items;
 8567:                             for (my $i=0; $i<$expected{$part_id}; $i++) {
 8568:                                 my $curr_sub = shift(@tocheck);
 8569:                                 my $digit;
 8570:                                 if ($curr_sub =~ /^[A-J]$/) {
 8571:                                     $digit = $lettdig->{$curr_sub}-1;
 8572:                                 }
 8573:                                 if ($curr_sub eq 'J') {
 8574:                                     $digit += scalar($numletts);
 8575:                                 }
 8576:                                 for (my $j=0; $j<$scantron_config->{'Qlength'}; $j++) {
 8577:                                     if ($j == $digit) {
 8578:                                         $recorded{$part_id} .= $scantron_config->{'Qon'};
 8579:                                     } else {
 8580:                                         $recorded{$part_id} .= $scantron_config->{'Qoff'};
 8581:                                     }
 8582:                                 }
 8583:                             }
 8584:                         }
 8585:                     }
 8586:                 }
 8587:             }
 8588:         }
 8589:         foreach my $part_id (@{$partids}) {
 8590:             if ($recorded{$part_id} eq '') {
 8591:                 for (my $i=0; $i<$expected{$part_id}; $i++) {
 8592:                     for (my $j=0; $j<$scantron_config->{'Qlength'}; $j++) {
 8593:                         $recorded{$part_id} .= $scantron_config->{'Qoff'};
 8594:                     }
 8595:                 }
 8596:             }
 8597:             $record .= $recorded{$part_id};
 8598:         }
 8599:     }
 8600:     return ($counter,$record);
 8601: }
 8602: 
 8603: sub letter_to_digits { 
 8604:     my %lettdig = (
 8605:                     A => 1,
 8606:                     B => 2,
 8607:                     C => 3,
 8608:                     D => 4,
 8609:                     E => 5,
 8610:                     F => 6,
 8611:                     G => 7,
 8612:                     H => 8,
 8613:                     I => 9,
 8614:                     J => 0,
 8615:                   );
 8616:     return %lettdig;
 8617: }
 8618: 
 8619: 
 8620: #-------- end of section for handling grading scantron forms -------
 8621: #
 8622: #-------------------------------------------------------------------
 8623: 
 8624: #-------------------------- Menu interface -------------------------
 8625: #
 8626: #--- Show a Grading Menu button - Calls the next routine ---
 8627: sub show_grading_menu_form {
 8628:     my ($symb)=@_;
 8629:     my $result.='<br /><form action="/adm/grades" method="post">'."\n".
 8630: 	'<input type="hidden" name="symb" value="'.&Apache::lonenc::check_encrypt($symb).'" />'."\n".
 8631: 	'<input type="hidden" name="saveState"  value="'.$env{'form.saveState'}.'" />'."\n".
 8632: 	'<input type="hidden" name="command" value="gradingmenu" />'."\n".
 8633: 	'<input type="submit" name="submit" value="'.&mt('Grading Menu').'" />'."\n".
 8634: 	'</form>'."\n";
 8635:     return $result;
 8636: }
 8637: 
 8638: # -- Retrieve choices for grading form
 8639: sub savedState {
 8640:     my %savedState = ();
 8641:     if ($env{'form.saveState'}) {
 8642: 	foreach (split(/:/,$env{'form.saveState'})) {
 8643: 	    my ($key,$value) = split(/=/,$_,2);
 8644: 	    $savedState{$key} = $value;
 8645: 	}
 8646:     }
 8647:     return \%savedState;
 8648: }
 8649: 
 8650: sub grading_menu {
 8651:     my ($request) = @_;
 8652:     my ($symb)=&get_symb($request);
 8653:     if (!$symb) {return '';}
 8654:     my $probTitle = &Apache::lonnet::gettitle($symb);
 8655:     my ($table,undef,$hdgrade) = &showResourceInfo($symb,$probTitle);
 8656: 
 8657:     $request->print($table);
 8658:     my %fields = ('symb'=>&Apache::lonenc::check_encrypt($symb),
 8659:                   'handgrade'=>$hdgrade,
 8660:                   'probTitle'=>$probTitle,
 8661:                   'command'=>'submit_options',
 8662:                   'saveState'=>"",
 8663:                   'gradingMenu'=>1,
 8664:                   'showgrading'=>"yes");
 8665:     
 8666:     my $url1 = &Apache::lonhtmlcommon::build_url('grades/',\%fields);
 8667:     
 8668:     $fields{'command'} = 'csvform';
 8669:     my $url2 = &Apache::lonhtmlcommon::build_url('grades/',\%fields);
 8670:     
 8671:     $fields{'command'} = 'processclicker';
 8672:     my $url3 = &Apache::lonhtmlcommon::build_url('grades/',\%fields);
 8673:     
 8674:     $fields{'command'} = 'scantron_selectphase';
 8675:     my $url4 = &Apache::lonhtmlcommon::build_url('grades/',\%fields);
 8676:     
 8677:     my @menu = ({	categorytitle=>'Course Grading',
 8678:             items =>[
 8679:                         {	linktext => 'Manual Grading/View Submissions',
 8680:                     		url => $url1,
 8681:                     		permission => 'F',
 8682:                     		icon => 'edit-find-replace.png',
 8683:                     		linktitle => 'Start the process of hand grading submissions.'
 8684:                         },
 8685:                 	    {	linktext => 'Upload Scores',
 8686:                     		url => $url2,
 8687:                     		permission => 'F',
 8688:                     		icon => 'uploadscores.png',
 8689:                     		linktitle => 'Specify a file containing the class scores for current resource.'
 8690:                 	    },
 8691:                 	    {	linktext => 'Process Clicker',
 8692:                     		url => $url3,
 8693:                     		permission => 'F',
 8694:                     		icon => 'addClickerInfoFile.png',
 8695:                     		linktitle => 'Specify a file containing the clicker information for this resource.'
 8696:                 	    },
 8697:                 	    {	linktext => 'Grade/Manage/Review Bubblesheets',
 8698:                     		url => $url4,
 8699:                     		permission => 'F',
 8700:                     		icon => 'stat.png',
 8701:                     		linktitle => 'Grade bubblesheet exams, upload/download bubblesheet data files, and review previously graded bubblesheet exams.'
 8702:                 	    }
 8703:                     ]
 8704:             });
 8705: 
 8706:     #$fields{'command'} = 'verify';
 8707:     #$url = &Apache::lonhtmlcommon::build_url('grades/',\%fields);
 8708:     #
 8709:     # Create the menu
 8710:     my $Str;
 8711:     # $Str .= '<h2>'.&mt('Please select a grading task').'</h2>';
 8712:     $Str .= '<form method="post" action="" name="gradingMenu">';
 8713:     $Str .= '<input type="hidden" name="command" value="" />'.
 8714:     	'<input type="hidden" name="symb"        value="'.&Apache::lonenc::check_encrypt($symb).'" />'."\n".
 8715: 	'<input type="hidden" name="handgrade"   value="'.$hdgrade.'" />'."\n".
 8716: 	'<input type="hidden" name="probTitle"   value="'.$probTitle.'" />'."\n".
 8717: 	'<input type="hidden" name="saveState"   value="" />'."\n".
 8718: 	'<input type="hidden" name="gradingMenu" value="1" />'."\n".
 8719: 	'<input type="hidden" name="showgrading" value="yes" />'."\n";
 8720: 
 8721:     $Str .= Apache::lonhtmlcommon::generate_menu(@menu);
 8722:     #$menudata->{'jscript'}
 8723:     $Str .='<hr /><input type="button" value="'.&mt('Verify Receipt No.').'" '.
 8724:         ' onclick="javascript:checkChoice(document.forms.gradingMenu,\'5\',\'verify\')" '.
 8725:         ' /> '.
 8726:         &Apache::lonnet::recprefix($env{'request.course.id'}).
 8727:         '-<input type="text" name="receipt" size="4" onchange="javascript:checkReceiptNo(this.form,\'OK\')" />';
 8728: 
 8729:     $Str .="</form>\n";
 8730:     my $receiptalert = &mt("Please enter a receipt number given by a student in the receipt box.");
 8731:     $request->print(<<GRADINGMENUJS);
 8732: <script type="text/javascript" language="javascript">
 8733:     function checkChoice(formname,val,cmdx) {
 8734: 	if (val <= 2) {
 8735: 	    var cmd = radioSelection(formname.radioChoice);
 8736: 	    var cmdsave = cmd;
 8737: 	} else {
 8738: 	    cmd = cmdx;
 8739: 	    cmdsave = 'submission';
 8740: 	}
 8741: 	formname.command.value = cmd;
 8742: 	if (val < 5) formname.submit();
 8743: 	if (val == 5) {
 8744: 	    if (!checkReceiptNo(formname,'notOK')) { 
 8745: 	        return false;
 8746: 	    } else {
 8747: 	        formname.submit();
 8748: 	    }
 8749: 	}
 8750:     }
 8751: 
 8752:     function checkReceiptNo(formname,nospace) {
 8753: 	var receiptNo = formname.receipt.value;
 8754: 	var checkOpt = false;
 8755: 	if (nospace == "OK" && isNaN(receiptNo)) {checkOpt = true;}
 8756: 	if (nospace == "notOK" && (isNaN(receiptNo) || receiptNo == "")) {checkOpt = true;}
 8757: 	if (checkOpt) {
 8758: 	    alert("$receiptalert");
 8759: 	    formname.receipt.value = "";
 8760: 	    formname.receipt.focus();
 8761: 	    return false;
 8762: 	}
 8763: 	return true;
 8764:     }
 8765: </script>
 8766: GRADINGMENUJS
 8767:     &commonJSfunctions($request);
 8768:     return $Str;    
 8769: }
 8770: 
 8771: 
 8772: #--- Displays the submissions first page -------
 8773: sub submit_options {
 8774:     my ($request) = @_;
 8775:     my ($symb)=&get_symb($request);
 8776:     if (!$symb) {return '';}
 8777:     my $probTitle = &Apache::lonnet::gettitle($symb);
 8778: 
 8779:     my $receiptalert = &mt("Please enter a receipt number given by a student in the receipt box."); 
 8780:     $request->print(<<GRADINGMENUJS);
 8781: <script type="text/javascript" language="javascript">
 8782:     function checkChoice(formname,val,cmdx) {
 8783: 	if (val <= 2) {
 8784: 	    var cmd = radioSelection(formname.radioChoice);
 8785: 	    var cmdsave = cmd;
 8786: 	} else {
 8787: 	    cmd = cmdx;
 8788: 	    cmdsave = 'submission';
 8789: 	}
 8790: 	formname.command.value = cmd;
 8791: 	formname.saveState.value = "saveCmd="+cmdsave+":saveSec="+pullDownSelection(formname.section)+
 8792: 	    ":saveSub="+pullDownSelection(formname.submitonly)+":saveStatus="+pullDownSelection(formname.Status);
 8793: 	if (val < 5) formname.submit();
 8794: 	if (val == 5) {
 8795: 	    if (!checkReceiptNo(formname,'notOK')) { return false;}
 8796: 	    formname.submit();
 8797: 	}
 8798: 	if (val < 7) formname.submit();
 8799:     }
 8800: 
 8801:     function checkReceiptNo(formname,nospace) {
 8802: 	var receiptNo = formname.receipt.value;
 8803: 	var checkOpt = false;
 8804: 	if (nospace == "OK" && isNaN(receiptNo)) {checkOpt = true;}
 8805: 	if (nospace == "notOK" && (isNaN(receiptNo) || receiptNo == "")) {checkOpt = true;}
 8806: 	if (checkOpt) {
 8807: 	    alert("$receiptalert");
 8808: 	    formname.receipt.value = "";
 8809: 	    formname.receipt.focus();
 8810: 	    return false;
 8811: 	}
 8812: 	return true;
 8813:     }
 8814: </script>
 8815: GRADINGMENUJS
 8816:     &commonJSfunctions($request);
 8817:     my ($table,undef,$hdgrade) = &showResourceInfo($symb,$probTitle);
 8818:     my $result;
 8819:     my (undef,$sections) = &getclasslist('all','0');
 8820:     my $savedState = &savedState();
 8821:     my $saveCmd = ($$savedState{'saveCmd'} eq '' ? 'submission' : $$savedState{'saveCmd'});
 8822:     my $saveSec = ($$savedState{'saveSec'} eq '' ? 'all' : $$savedState{'saveSec'});
 8823:     my $saveSub = ($$savedState{'saveSub'} eq '' ? 'all' : $$savedState{'saveSub'});
 8824:     my $saveStatus = ($$savedState{'saveStatus'} eq '' ? 'Active' : $$savedState{'saveStatus'});
 8825: 
 8826:     # Preselect sections
 8827:     my $selsec="";
 8828:     if (ref($sections)) {
 8829:         foreach my $section (sort(@$sections)) {
 8830:             $selsec.='<option value="'.$section.'" '.
 8831:                 ($saveSec eq $section ? 'selected="selected"':'').'>'.$section.'</option>'."\n";
 8832:         }
 8833:     }
 8834: 
 8835:     $result.='<form action="/adm/grades" method="post" name="gradingMenu">'."\n".
 8836: 	'<input type="hidden" name="symb"        value="'.&Apache::lonenc::check_encrypt($symb).'" />'."\n".
 8837: 	'<input type="hidden" name="handgrade"   value="'.$hdgrade.'" />'."\n".
 8838: 	'<input type="hidden" name="probTitle"   value="'.$probTitle.'" />'."\n".
 8839: 	'<input type="hidden" name="command"     value="" />'."\n".
 8840: 	'<input type="hidden" name="saveState"   value="" />'."\n".
 8841: 	'<input type="hidden" name="gradingMenu" value="1" />'."\n".
 8842: 	'<input type="hidden" name="showgrading" value="yes" />'."\n";
 8843: 
 8844:     $result.='
 8845: <h2>
 8846:   '.&mt('Grade Current Resource').'
 8847: </h2>
 8848: <div>
 8849:   '.$table.'
 8850: </div>
 8851: 
 8852: <div class="LC_columnSection">
 8853:   
 8854:     <fieldset>
 8855:       <legend>
 8856:        '.&mt('Sections').'
 8857:       </legend>
 8858:       <select name="section" multiple="multiple" size="5">'."\n";
 8859:     $result.= $selsec;
 8860:     $result.= '<option value="all" '.($saveSec eq 'all' ? 'selected="selected"' : ''). '>all</option></select> &nbsp; ';
 8861:     $result.='
 8862:     </fieldset>
 8863:   
 8864:     <fieldset>
 8865:       <legend>
 8866:         '.&mt('Groups').'
 8867:       </legend>
 8868:       '.&Apache::lonstatistics::GroupSelect('group','multiple',5).'
 8869:     </fieldset>
 8870:   
 8871:     <fieldset>
 8872:       <legend>
 8873:         '.&mt('Access Status').'
 8874:       </legend>
 8875:       '.&Apache::lonhtmlcommon::StatusOptions($saveStatus,undef,5,undef,'mult').'
 8876:     </fieldset>
 8877:   
 8878:     <fieldset>
 8879:       <legend>
 8880:         '.&mt('Submission Status').'
 8881:       </legend>
 8882:       <select name="submitonly" size="5">
 8883: 	         <option value="yes" '.      ($saveSub eq 'yes'       ? 'selected="selected"' : '').'>'.&mt('with submissions').'</option>
 8884: 	         <option value="queued" '.   ($saveSub eq 'queued'    ? 'selected="selected"' : '').'>'.&mt('in grading queue').'</option>
 8885: 	         <option value="graded" '.   ($saveSub eq 'graded'    ? 'selected="selected"' : '').'>'.&mt('with ungraded submissions').'</option>
 8886: 	         <option value="incorrect" '.($saveSub eq 'incorrect' ? 'selected="selected"' : '').'>'.&mt('with incorrect submissions').'</option>
 8887:                  <option value="all" '.      ($saveSub eq 'all'       ? 'selected="selected"' : '').'>'.&mt('with any status').'</option>
 8888:       </select>
 8889:     </fieldset>
 8890:   
 8891: </div>
 8892: 
 8893: <br />
 8894:           <div>
 8895:             <div>
 8896:               <label>
 8897:                 <input type="radio" name="radioChoice" value="submission" '.
 8898:                   ($saveCmd eq 'submission' ? 'checked="checked"' : '').' /> '.
 8899:              &mt('Select individual students to grade and view submissions.').'
 8900: 	      </label> 
 8901:             </div>
 8902:             <div>
 8903: 	      <label>
 8904:                 <input type="radio" name="radioChoice" value="viewgrades" '.
 8905:                   ($saveCmd eq 'viewgrades' ? 'checked="checked"' : '').' /> '.
 8906:                     &mt('Grade all selected students in a grading table.').'
 8907:               </label>
 8908:             </div>
 8909:             <div>
 8910: 	      <input type="button" onclick="javascript:checkChoice(this.form,\'2\');" value="'.&mt('Next').' &rarr;" />
 8911:             </div>
 8912:           </div>
 8913: 
 8914: 
 8915:         <h2>
 8916:          '.&mt('Grade Complete Folder for One Student').'
 8917:         </h2>
 8918:         <div>
 8919:             <div>
 8920:               <label>
 8921:                 <input type="radio" name="radioChoice" value="pickStudentPage" '.
 8922: 	  ($saveCmd eq 'pickStudentPage' ? 'checked="checked"' : '').' /> '.
 8923:   &mt('The <b>complete</b> page/sequence/folder: For one student').'
 8924:               </label>
 8925:             </div>
 8926:             <div>
 8927: 	      <input type="button" onclick="javascript:checkChoice(this.form,\'2\');" value="'.&mt('Next').' &rarr;" />
 8928:             </div>
 8929:         </div>
 8930:   </form>';
 8931:     $result .= &show_grading_menu_form($symb);
 8932:     return $result;
 8933: }
 8934: 
 8935: sub reset_perm {
 8936:     undef(%perm);
 8937: }
 8938: 
 8939: sub init_perm {
 8940:     &reset_perm();
 8941:     foreach my $test_perm ('vgr','mgr','opa') {
 8942: 
 8943: 	my $scope = $env{'request.course.id'};
 8944: 	if (!($perm{$test_perm}=&Apache::lonnet::allowed($test_perm,$scope))) {
 8945: 
 8946: 	    $scope .= '/'.$env{'request.course.sec'};
 8947: 	    if ( $perm{$test_perm}=
 8948: 		 &Apache::lonnet::allowed($test_perm,$scope)) {
 8949: 		$perm{$test_perm.'_section'}=$env{'request.course.sec'};
 8950: 	    } else {
 8951: 		delete($perm{$test_perm});
 8952: 	    }
 8953: 	}
 8954:     }
 8955: }
 8956: 
 8957: sub gather_clicker_ids {
 8958:     my %clicker_ids;
 8959: 
 8960:     my $classlist = &Apache::loncoursedata::get_classlist();
 8961: 
 8962:     # Set up a couple variables.
 8963:     my $username_idx = &Apache::loncoursedata::CL_SNAME();
 8964:     my $domain_idx   = &Apache::loncoursedata::CL_SDOM();
 8965:     my $status_idx   = &Apache::loncoursedata::CL_STATUS();
 8966: 
 8967:     foreach my $student (keys(%$classlist)) {
 8968:         if ($classlist->{$student}->[$status_idx] ne 'Active') { next; }
 8969:         my $username = $classlist->{$student}->[$username_idx];
 8970:         my $domain   = $classlist->{$student}->[$domain_idx];
 8971:         my $clickers =
 8972: 	    (&Apache::lonnet::userenvironment($domain,$username,'clickers'))[1];
 8973:         foreach my $id (split(/\,/,$clickers)) {
 8974:             $id=~s/^[\#0]+//;
 8975:             $id=~s/[\-\:]//g;
 8976:             if (exists($clicker_ids{$id})) {
 8977: 		$clicker_ids{$id}.=','.$username.':'.$domain;
 8978:             } else {
 8979: 		$clicker_ids{$id}=$username.':'.$domain;
 8980:             }
 8981:         }
 8982:     }
 8983:     return %clicker_ids;
 8984: }
 8985: 
 8986: sub gather_adv_clicker_ids {
 8987:     my %clicker_ids;
 8988:     my $cnum=$env{'course.'.$env{'request.course.id'}.'.num'};
 8989:     my $cdom=$env{'course.'.$env{'request.course.id'}.'.domain'};
 8990:     my %coursepersonnel=&Apache::lonnet::get_course_adv_roles($cdom.'/'.$cnum);
 8991:     foreach my $element (sort(keys(%coursepersonnel))) {
 8992:         foreach my $person (split(/\,/,$coursepersonnel{$element})) {
 8993:             my ($puname,$pudom)=split(/\:/,$person);
 8994:             my $clickers =
 8995: 		(&Apache::lonnet::userenvironment($pudom,$puname,'clickers'))[1];
 8996:             foreach my $id (split(/\,/,$clickers)) {
 8997: 		$id=~s/^[\#0]+//;
 8998:                 $id=~s/[\-\:]//g;
 8999: 		if (exists($clicker_ids{$id})) {
 9000: 		    $clicker_ids{$id}.=','.$puname.':'.$pudom;
 9001: 		} else {
 9002: 		    $clicker_ids{$id}=$puname.':'.$pudom;
 9003: 		}
 9004:             }
 9005:         }
 9006:     }
 9007:     return %clicker_ids;
 9008: }
 9009: 
 9010: sub clicker_grading_parameters {
 9011:     return ('gradingmechanism' => 'scalar',
 9012:             'upfiletype' => 'scalar',
 9013:             'specificid' => 'scalar',
 9014:             'pcorrect' => 'scalar',
 9015:             'pincorrect' => 'scalar');
 9016: }
 9017: 
 9018: sub process_clicker {
 9019:     my ($r)=@_;
 9020:     my ($symb)=&get_symb($r);
 9021:     if (!$symb) {return '';}
 9022:     my $result=&checkforfile_js();
 9023:     $env{'form.probTitle'} = &Apache::lonnet::gettitle($symb);
 9024:     my ($table) = &showResourceInfo($symb,$env{'form.probTitle'});
 9025:     $result.=$table;
 9026:     $result.='<br /><table width="100%" border="0"><tr><td bgcolor="#777777">'."\n";
 9027:     $result.='<table width="100%" border="0"><tr bgcolor="#e6ffff"><td>'."\n";
 9028:     $result.='&nbsp;<b>'.&mt('Specify a file containing the clicker information for this resource.').
 9029:         '</b></td></tr>'."\n";
 9030:     $result.='<tr bgcolor="#ffffe6"><td>'."\n";
 9031: # Attempt to restore parameters from last session, set defaults if not present
 9032:     my %Saveable_Parameters=&clicker_grading_parameters();
 9033:     &Apache::loncommon::restore_course_settings('grades_clicker',
 9034:                                                  \%Saveable_Parameters);
 9035:     if (!$env{'form.pcorrect'}) { $env{'form.pcorrect'}=100; }
 9036:     if (!$env{'form.pincorrect'}) { $env{'form.pincorrect'}=100; }
 9037:     if (!$env{'form.gradingmechanism'}) { $env{'form.gradingmechanism'}='attendance'; }
 9038:     if (!$env{'form.upfiletype'}) { $env{'form.upfiletype'}='iclicker'; }
 9039: 
 9040:     my %checked;
 9041:     foreach my $gradingmechanism ('attendance','personnel','specific','given') {
 9042:        if ($env{'form.gradingmechanism'} eq $gradingmechanism) {
 9043:           $checked{$gradingmechanism}=' checked="checked"';
 9044:        }
 9045:     }
 9046: 
 9047:     my $upload=&mt("Upload File");
 9048:     my $type=&mt("Type");
 9049:     my $attendance=&mt("Award points just for participation");
 9050:     my $personnel=&mt("Correctness determined from response by course personnel");
 9051:     my $specific=&mt("Correctness determined from response with clicker ID(s)"); 
 9052:     my $given=&mt("Correctness determined from given list of answers").' '.
 9053:               '<font size="-2"><tt>('.&mt("Provide comma-separated list. Use '*' for any answer correct, '-' for skip").')</tt></font>';
 9054:     my $pcorrect=&mt("Percentage points for correct solution");
 9055:     my $pincorrect=&mt("Percentage points for incorrect solution");
 9056:     my $selectform=&Apache::loncommon::select_form($env{'form.upfiletype'},'upfiletype',
 9057:                                                    {'iclicker' => 'i>clicker',
 9058:                                                     'interwrite' => 'interwrite PRS'});
 9059:     $symb = &Apache::lonenc::check_encrypt($symb);
 9060:     $result.=<<ENDUPFORM;
 9061: <script type="text/javascript">
 9062: function sanitycheck() {
 9063: // Accept only integer percentages
 9064:    document.forms.gradesupload.pcorrect.value=Math.round(document.forms.gradesupload.pcorrect.value);
 9065:    document.forms.gradesupload.pincorrect.value=Math.round(document.forms.gradesupload.pincorrect.value);
 9066: // Find out grading choice
 9067:    for (i=0; i<document.forms.gradesupload.gradingmechanism.length; i++) {
 9068:       if (document.forms.gradesupload.gradingmechanism[i].checked) {
 9069:          gradingchoice=document.forms.gradesupload.gradingmechanism[i].value;
 9070:       }
 9071:    }
 9072: // By default, new choice equals user selection
 9073:    newgradingchoice=gradingchoice;
 9074: // Not good to give more points for false answers than correct ones
 9075:    if (Math.round(document.forms.gradesupload.pcorrect.value)<Math.round(document.forms.gradesupload.pincorrect.value)) {
 9076:       document.forms.gradesupload.pcorrect.value=document.forms.gradesupload.pincorrect.value;
 9077:    }
 9078: // If new choice is attendance only, and old choice was correctness-based, restore defaults
 9079:    if ((gradingchoice=='attendance') && (document.forms.gradesupload.waschecked.value!='attendance')) {
 9080:       document.forms.gradesupload.pcorrect.value=100;
 9081:       document.forms.gradesupload.pincorrect.value=100;
 9082:    }
 9083: // If the values are different, cannot be attendance only
 9084:    if ((Math.round(document.forms.gradesupload.pcorrect.value)!=Math.round(document.forms.gradesupload.pincorrect.value)) &&
 9085:        (gradingchoice=='attendance')) {
 9086:        newgradingchoice='personnel';
 9087:    }
 9088: // Change grading choice to new one
 9089:    for (i=0; i<document.forms.gradesupload.gradingmechanism.length; i++) {
 9090:       if (document.forms.gradesupload.gradingmechanism[i].value==newgradingchoice) {
 9091:          document.forms.gradesupload.gradingmechanism[i].checked=true;
 9092:       } else {
 9093:          document.forms.gradesupload.gradingmechanism[i].checked=false;
 9094:       }
 9095:    }
 9096: // Remember the old state
 9097:    document.forms.gradesupload.waschecked.value=newgradingchoice;
 9098: }
 9099: </script>
 9100: <form method="post" enctype="multipart/form-data" action="/adm/grades" name="gradesupload">
 9101: <input type="hidden" name="symb" value="$symb" />
 9102: <input type="hidden" name="command" value="processclickerfile" />
 9103: <input type="hidden" name="probTitle" value="$env{'form.probTitle'}" />
 9104: <input type="hidden" name="saveState"  value="$env{'form.saveState'}" />
 9105: <input type="file" name="upfile" size="50" />
 9106: <br /><label>$type: $selectform</label>
 9107: <br /><label><input type="radio" name="gradingmechanism" value="attendance"$checked{'attendance'} onclick="sanitycheck()" />$attendance </label>
 9108: <br /><label><input type="radio" name="gradingmechanism" value="personnel"$checked{'personnel'} onclick="sanitycheck()" />$personnel</label>
 9109: <br /><label><input type="radio" name="gradingmechanism" value="specific"$checked{'specific'} onclick="sanitycheck()" />$specific </label>
 9110: <input type="text" name="specificid" value="$env{'form.specificid'}" size="20" />
 9111: <br /><label><input type="radio" name="gradingmechanism" value="given"$checked{'given'} onclick="sanitycheck()" />$given </label>
 9112: <br />&nbsp;&nbsp;&nbsp;
 9113: <input type="text" name="givenanswer" size="50" />
 9114: <input type="hidden" name="waschecked" value="$env{'form.gradingmechanism'}" />
 9115: <br /><label>$pcorrect: <input type="text" name="pcorrect" size="4" value="$env{'form.pcorrect'}" onchange="sanitycheck()" /></label>
 9116: <br /><label>$pincorrect: <input type="text" name="pincorrect" size="4" value="$env{'form.pincorrect'}" onchange="sanitycheck()" /></label>
 9117: <br /><input type="button" onclick="javascript:checkUpload(this.form);" value="$upload" />
 9118: </form>
 9119: ENDUPFORM
 9120:     $result.='</td></tr></table>'."\n".
 9121:              '</td></tr></table><br /><br />'."\n";
 9122:     $result.=&show_grading_menu_form($symb);
 9123:     return $result;
 9124: }
 9125: 
 9126: sub process_clicker_file {
 9127:     my ($r)=@_;
 9128:     my ($symb)=&get_symb($r);
 9129:     if (!$symb) {return '';}
 9130: 
 9131:     my %Saveable_Parameters=&clicker_grading_parameters();
 9132:     &Apache::loncommon::store_course_settings('grades_clicker',
 9133:                                               \%Saveable_Parameters);
 9134: 
 9135:     my ($result) = &showResourceInfo($symb,$env{'form.probTitle'});
 9136:     if (($env{'form.gradingmechanism'} eq 'specific') && ($env{'form.specificid'}!~/\w/)) {
 9137: 	$result.='<span class="LC_error">'.&mt('You need to specify a clicker ID for the correct answer').'</span>';
 9138: 	return $result.&show_grading_menu_form($symb);
 9139:     }
 9140:     if (($env{'form.gradingmechanism'} eq 'given') && ($env{'form.givenanswer'}!~/\S/)) {
 9141:         $result.='<span class="LC_error">'.&mt('You need to specify the correct answer').'</span>';
 9142:         return $result.&show_grading_menu_form($symb);
 9143:     }
 9144:     my $foundgiven=0;
 9145:     if ($env{'form.gradingmechanism'} eq 'given') {
 9146:         $env{'form.givenanswer'}=~s/^\s*//gs;
 9147:         $env{'form.givenanswer'}=~s/\s*$//gs;
 9148:         $env{'form.givenanswer'}=~s/[^a-zA-Z0-9\.\*\-\+]+/\,/g;
 9149:         $env{'form.givenanswer'}=uc($env{'form.givenanswer'});
 9150:         my @answers=split(/\,/,$env{'form.givenanswer'});
 9151:         $foundgiven=$#answers+1;
 9152:     }
 9153:     my %clicker_ids=&gather_clicker_ids();
 9154:     my %correct_ids;
 9155:     if ($env{'form.gradingmechanism'} eq 'personnel') {
 9156: 	%correct_ids=&gather_adv_clicker_ids();
 9157:     }
 9158:     if ($env{'form.gradingmechanism'} eq 'specific') {
 9159: 	foreach my $correct_id (split(/[\s\,]/,$env{'form.specificid'})) {;
 9160: 	   $correct_id=~tr/a-z/A-Z/;
 9161: 	   $correct_id=~s/\s//gs;
 9162: 	   $correct_id=~s/^[\#0]+//;
 9163:            $correct_id=~s/[\-\:]//g;
 9164:            if ($correct_id) {
 9165: 	      $correct_ids{$correct_id}='specified';
 9166:            }
 9167:         }
 9168:     }
 9169:     if ($env{'form.gradingmechanism'} eq 'attendance') {
 9170: 	$result.=&mt('Score based on attendance only');
 9171:     } elsif ($env{'form.gradingmechanism'} eq 'given') {
 9172:         $result.=&mt('Score based on [_1] ([_2] answers)','<tt>'.$env{'form.givenanswer'}.'</tt>',$foundgiven);
 9173:     } else {
 9174: 	my $number=0;
 9175: 	$result.='<p><b>'.&mt('Correctness determined by the following IDs').'</b>';
 9176: 	foreach my $id (sort(keys(%correct_ids))) {
 9177: 	    $result.='<br /><tt>'.$id.'</tt> - ';
 9178: 	    if ($correct_ids{$id} eq 'specified') {
 9179: 		$result.=&mt('specified');
 9180: 	    } else {
 9181: 		my ($uname,$udom)=split(/\:/,$correct_ids{$id});
 9182: 		$result.=&Apache::loncommon::plainname($uname,$udom);
 9183: 	    }
 9184: 	    $number++;
 9185: 	}
 9186:         $result.="</p>\n";
 9187: 	if ($number==0) {
 9188: 	    $result.='<span class="LC_error">'.&mt('No IDs found to determine correct answer').'</span>';
 9189: 	    return $result.&show_grading_menu_form($symb);
 9190: 	}
 9191:     }
 9192:     if (length($env{'form.upfile'}) < 2) {
 9193:         $result.=&mt('[_1] Error: [_2] The file you attempted to upload, [_3] contained no information. Please check that you entered the correct filename.',
 9194: 		     '<span class="LC_error">',
 9195: 		     '</span>',
 9196: 		     '<span class="LC_filename">'.&HTML::Entities::encode($env{'form.upfile.filename'},'<>&"').'</span>');
 9197:         return $result.&show_grading_menu_form($symb);
 9198:     }
 9199: 
 9200: # Were able to get all the info needed, now analyze the file
 9201: 
 9202:     $result.=&Apache::loncommon::studentbrowser_javascript();
 9203:     $symb = &Apache::lonenc::check_encrypt($symb);
 9204:     my $heading=&mt('Scanning clicker file');
 9205:     $result.=(<<ENDHEADER);
 9206: <br /><table width="100%" border="0"><tr><td bgcolor="#777777">
 9207: <table width="100%" border="0"><tr bgcolor="#e6ffff"><td>
 9208: <b>$heading</b></td></tr><tr bgcolor="#ffffe6"><td>
 9209: <form method="post" action="/adm/grades" name="clickeranalysis">
 9210: <input type="hidden" name="symb" value="$symb" />
 9211: <input type="hidden" name="command" value="assignclickergrades" />
 9212: <input type="hidden" name="probTitle" value="$env{'form.probTitle'}" />
 9213: <input type="hidden" name="saveState"  value="$env{'form.saveState'}" />
 9214: <input type="hidden" name="gradingmechanism" value="$env{'form.gradingmechanism'}" />
 9215: <input type="hidden" name="pcorrect" value="$env{'form.pcorrect'}" />
 9216: <input type="hidden" name="pincorrect" value="$env{'form.pincorrect'}" />
 9217: ENDHEADER
 9218:     if ($env{'form.gradingmechanism'} eq 'given') {
 9219:        $result.='<input type="hidden" name="correct:given" value="'.$env{'form.givenanswer'}.'" />';
 9220:     } 
 9221:     my %responses;
 9222:     my @questiontitles;
 9223:     my $errormsg='';
 9224:     my $number=0;
 9225:     if ($env{'form.upfiletype'} eq 'iclicker') {
 9226: 	($errormsg,$number)=&iclicker_eval(\@questiontitles,\%responses);
 9227:     }
 9228:     if ($env{'form.upfiletype'} eq 'interwrite') {
 9229:         ($errormsg,$number)=&interwrite_eval(\@questiontitles,\%responses);
 9230:     }
 9231:     $result.='<br />'.&mt('Found [_1] question(s)',$number).'<br />'.
 9232:              '<input type="hidden" name="number" value="'.$number.'" />'.
 9233:              &mt('Awarding [_1] percent for correct and [_2] percent for incorrect responses',
 9234:                  $env{'form.pcorrect'},$env{'form.pincorrect'}).
 9235:              '<br />';
 9236:     if (($env{'form.gradingmechanism'} eq 'given') && ($number!=$foundgiven)) {
 9237:        $result.='<span class="LC_error">'.&mt('Number of given answers does not agree with number of questions in file.').'</span>';
 9238:        return $result.&show_grading_menu_form($symb);
 9239:     } 
 9240: # Remember Question Titles
 9241: # FIXME: Possibly need delimiter other than ":"
 9242:     for (my $i=0;$i<$number;$i++) {
 9243:         $result.='<input type="hidden" name="question:'.$i.'" value="'.
 9244:                  &HTML::Entities::encode($questiontitles[$i],'"&<>').'" />';
 9245:     }
 9246:     my $correct_count=0;
 9247:     my $student_count=0;
 9248:     my $unknown_count=0;
 9249: # Match answers with usernames
 9250: # FIXME: Possibly need delimiter other than ":"
 9251:     foreach my $id (keys(%responses)) {
 9252:        if ($correct_ids{$id}) {
 9253:           $result.="\n".'<input type="hidden" name="correct:'.$correct_count.':'.$correct_ids{$id}.'" value="'.$responses{$id}.'" />';
 9254:           $correct_count++;
 9255:        } elsif ($clicker_ids{$id}) {
 9256:           if ($clicker_ids{$id}=~/\,/) {
 9257: # More than one user with the same clicker!
 9258:              $result.="\n<hr />".&mt('Clicker registered more than once').": <tt>".$id."</tt><br />";
 9259:              $result.="\n".'<input type="hidden" name="unknown:'.$id.'" value="'.$responses{$id}.'" />'.
 9260:                            "<select name='multi".$id."'>";
 9261:              foreach my $reguser (sort(split(/\,/,$clicker_ids{$id}))) {
 9262:                  $result.="<option value='".$reguser."'>".&Apache::loncommon::plainname(split(/\:/,$reguser)).' ('.$reguser.')</option>';
 9263:              }
 9264:              $result.='</select>';
 9265:              $unknown_count++;
 9266:           } else {
 9267: # Good: found one and only one user with the right clicker
 9268:              $result.="\n".'<input type="hidden" name="student:'.$clicker_ids{$id}.'" value="'.$responses{$id}.'" />';
 9269:              $student_count++;
 9270:           }
 9271:        } else {
 9272:           $result.="\n<hr />".&mt('Unregistered Clicker')." <tt>".$id."</tt><br />";
 9273:           $result.="\n".'<input type="hidden" name="unknown:'.$id.'" value="'.$responses{$id}.'" />'.
 9274:                    "\n".&mt("Username").": <input type='text' name='uname".$id."' />&nbsp;".
 9275:                    "\n".&mt("Domain").": ".
 9276:                    &Apache::loncommon::select_dom_form($env{'course.'.$env{'request.course.id'}.'.domain'},'udom'.$id).'&nbsp;'.
 9277:                    &Apache::loncommon::selectstudent_link('clickeranalysis','uname'.$id,'udom'.$id,0,$id);
 9278:           $unknown_count++;
 9279:        }
 9280:     }
 9281:     $result.='<hr />'.
 9282:              &mt('Found [_1] registered and [_2] unregistered clickers.',$student_count,$unknown_count);
 9283:     if (($env{'form.gradingmechanism'} ne 'attendance') && ($env{'form.gradingmechanism'} ne 'given')) {
 9284:        if ($correct_count==0) {
 9285:           $errormsg.="Found no correct answers answers for grading!";
 9286:        } elsif ($correct_count>1) {
 9287:           $result.='<br /><span class="LC_warning">'.&mt("Found [_1] entries for grading!",$correct_count).'</span>';
 9288:        }
 9289:     }
 9290:     if ($number<1) {
 9291:        $errormsg.="Found no questions.";
 9292:     }
 9293:     if ($errormsg) {
 9294:        $result.='<br /><span class="LC_error">'.&mt($errormsg).'</span>';
 9295:     } else {
 9296:        $result.='<br /><input type="submit" name="finalize" value="'.&mt('Finalize Grading').'" />';
 9297:     }
 9298:     $result.='</form></td></tr></table>'."\n".
 9299:              '</td></tr></table><br /><br />'."\n";
 9300:     return $result.&show_grading_menu_form($symb);
 9301: }
 9302: 
 9303: sub iclicker_eval {
 9304:     my ($questiontitles,$responses)=@_;
 9305:     my $number=0;
 9306:     my $errormsg='';
 9307:     foreach my $line (split(/[\n\r]/,$env{'form.upfile'})) {
 9308:         my %components=&Apache::loncommon::record_sep($line);
 9309:         my @entries=map {$components{$_}} (sort(keys(%components)));
 9310: 	if ($entries[0] eq 'Question') {
 9311: 	    for (my $i=3;$i<$#entries;$i+=6) {
 9312: 		$$questiontitles[$number]=$entries[$i];
 9313: 		$number++;
 9314: 	    }
 9315: 	}
 9316: 	if ($entries[0]=~/^\#/) {
 9317: 	    my $id=$entries[0];
 9318: 	    my @idresponses;
 9319: 	    $id=~s/^[\#0]+//;
 9320: 	    for (my $i=0;$i<$number;$i++) {
 9321: 		my $idx=3+$i*6;
 9322:                 $entries[$idx]=~s/[^a-zA-Z0-9\.\*\-\+]+//g;
 9323: 		push(@idresponses,$entries[$idx]);
 9324: 	    }
 9325: 	    $$responses{$id}=join(',',@idresponses);
 9326: 	}
 9327:     }
 9328:     return ($errormsg,$number);
 9329: }
 9330: 
 9331: sub interwrite_eval {
 9332:     my ($questiontitles,$responses)=@_;
 9333:     my $number=0;
 9334:     my $errormsg='';
 9335:     my $skipline=1;
 9336:     my $questionnumber=0;
 9337:     my %idresponses=();
 9338:     foreach my $line (split(/[\n\r]/,$env{'form.upfile'})) {
 9339:         my %components=&Apache::loncommon::record_sep($line);
 9340:         my @entries=map {$components{$_}} (sort(keys(%components)));
 9341:         if ($entries[1] eq 'Time') { $skipline=0; next; }
 9342:         if ($entries[1] eq 'Response') { $skipline=1; }
 9343:         next if $skipline;
 9344:         if ($entries[0]!=$questionnumber) {
 9345:            $questionnumber=$entries[0];
 9346:            $$questiontitles[$number]=&mt('Question [_1]',$questionnumber);
 9347:            $number++;
 9348:         }
 9349:         my $id=$entries[4];
 9350:         $id=~s/^[\#0]+//;
 9351:         $id=~s/^v\d*\://i;
 9352:         $id=~s/[\-\:]//g;
 9353:         $idresponses{$id}[$number]=$entries[6];
 9354:     }
 9355:     foreach my $id (keys(%idresponses)) {
 9356:        $$responses{$id}=join(',',@{$idresponses{$id}});
 9357:        $$responses{$id}=~s/^\s*\,//;
 9358:     }
 9359:     return ($errormsg,$number);
 9360: }
 9361: 
 9362: sub assign_clicker_grades {
 9363:     my ($r)=@_;
 9364:     my ($symb)=&get_symb($r);
 9365:     if (!$symb) {return '';}
 9366: # See which part we are saving to
 9367:     my $res_error;
 9368:     my ($partlist,$handgrade,$responseType) = &response_type($symb,\$res_error);
 9369:     if ($res_error) {
 9370:         return &navmap_errormsg();
 9371:     }
 9372: # FIXME: This should probably look for the first handgradeable part
 9373:     my $part=$$partlist[0];
 9374: # Start screen output
 9375:     my ($result) = &showResourceInfo($symb,$env{'form.probTitle'}).'<br />';
 9376: 
 9377:     $result .= &Apache::loncommon::start_data_table().
 9378:                &Apache::loncommon::start_data_table_header_row().
 9379:                '<th>'.&mt('Assigning grades based on clicker file').'</th>'.
 9380:                &Apache::loncommon::end_data_table_header_row().
 9381:                &Apache::loncommon::start_data_table_row().'<td>';
 9382: 
 9383: # Get correct result
 9384: # FIXME: Possibly need delimiter other than ":"
 9385:     my @correct=();
 9386:     my $gradingmechanism=$env{'form.gradingmechanism'};
 9387:     my $number=$env{'form.number'};
 9388:     if ($gradingmechanism ne 'attendance') {
 9389:        foreach my $key (keys(%env)) {
 9390:           if ($key=~/^form\.correct\:/) {
 9391:              my @input=split(/\,/,$env{$key});
 9392:              for (my $i=0;$i<=$#input;$i++) {
 9393:                  if (($correct[$i]) && ($input[$i]) &&
 9394:                      ($correct[$i] ne $input[$i])) {
 9395:                     $result.='<br /><span class="LC_warning">'.
 9396:                              &mt('More than one correct result given for question "[_1]": [_2] versus [_3].',
 9397:                                  $env{'form.question:'.$i},$correct[$i],$input[$i]).'</span>';
 9398:                  } elsif (($input[$i]) || ($input[$i] eq '0')) {
 9399:                     $correct[$i]=$input[$i];
 9400:                  }
 9401:              }
 9402:           }
 9403:        }
 9404:        for (my $i=0;$i<$number;$i++) {
 9405:           if ((!$correct[$i]) && ($correct[$i] ne '0')) {
 9406:              $result.='<br /><span class="LC_error">'.
 9407:                       &mt('No correct result given for question "[_1]"!',
 9408:                           $env{'form.question:'.$i}).'</span>';
 9409:           }
 9410:        }
 9411:        $result.='<br />'.&mt("Correct answer: [_1]",join(', ',map { ((($_) || ($_ eq '0'))?$_:'-') } @correct));
 9412:     }
 9413: # Start grading
 9414:     my $pcorrect=$env{'form.pcorrect'};
 9415:     my $pincorrect=$env{'form.pincorrect'};
 9416:     my $storecount=0;
 9417:     my %users=();
 9418:     foreach my $key (keys(%env)) {
 9419:        my $user='';
 9420:        if ($key=~/^form\.student\:(.*)$/) {
 9421:           $user=$1;
 9422:        }
 9423:        if ($key=~/^form\.unknown\:(.*)$/) {
 9424:           my $id=$1;
 9425:           if (($env{'form.uname'.$id}) && ($env{'form.udom'.$id})) {
 9426:              $user=$env{'form.uname'.$id}.':'.$env{'form.udom'.$id};
 9427:           } elsif ($env{'form.multi'.$id}) {
 9428:              $user=$env{'form.multi'.$id};
 9429:           }
 9430:        }
 9431:        if ($user) {
 9432:           if ($users{$user}) {
 9433:              $result.='<br /><span class="LC_warning">'.
 9434:                       &mt("More than one entry found for <tt>[_1]</tt>!",$user).
 9435:                       '</span><br />';
 9436:           }
 9437:           $users{$user}=1;
 9438:           my @answer=split(/\,/,$env{$key});
 9439:           my $sum=0;
 9440:           my $realnumber=$number;
 9441:           for (my $i=0;$i<$number;$i++) {
 9442:              if  ($correct[$i] eq '-') {
 9443:                 $realnumber--;
 9444:              } elsif ($answer[$i]) {
 9445:                 if ($gradingmechanism eq 'attendance') {
 9446:                    $sum+=$pcorrect;
 9447:                 } elsif ($correct[$i] eq '*') {
 9448:                    $sum+=$pcorrect;
 9449:                 } else {
 9450: # We actually grade if correct or not
 9451:                    my $increment=$pincorrect;
 9452: # Special case: numerical answer "0"
 9453:                    if ($correct[$i] eq '0') {
 9454:                       if ($answer[$i]=~/^[0\.]+$/) {
 9455:                          $increment=$pcorrect;
 9456:                       }
 9457: # General numerical answer, both evaluate to something non-zero
 9458:                    } elsif ((1.0*$correct[$i]!=0) && (1.0*$answer[$i]!=0)) {
 9459:                       if (1.0*$correct[$i]==1.0*$answer[$i]) {
 9460:                          $increment=$pcorrect;
 9461:                       }
 9462: # Must be just alphanumeric
 9463:                    } elsif ($answer[$i] eq $correct[$i]) {
 9464:                       $increment=$pcorrect;
 9465:                    }
 9466:                    $sum+=$increment;
 9467:                 }
 9468:              }
 9469:           }
 9470:           my $ave=$sum/(100*$realnumber);
 9471: # Store
 9472:           my ($username,$domain)=split(/\:/,$user);
 9473:           my %grades=();
 9474:           $grades{"resource.$part.solved"}='correct_by_override';
 9475:           $grades{"resource.$part.awarded"}=$ave;
 9476:           $grades{"resource.regrader"}="$env{'user.name'}:$env{'user.domain'}";
 9477:           my $returncode=&Apache::lonnet::cstore(\%grades,$symb,
 9478:                                                  $env{'request.course.id'},
 9479:                                                  $domain,$username);
 9480:           if ($returncode ne 'ok') {
 9481:              $result.="<br /><span class=\"LC_error\">Failed to save student $username:$domain. Message when trying to save was ($returncode)</span>";
 9482:           } else {
 9483:              $storecount++;
 9484:           }
 9485:        }
 9486:     }
 9487: # We are done
 9488:     $result.='<br />'.&mt('Successfully stored grades for [quant,_1,student].',$storecount).
 9489:              '</td>'.
 9490:              &Apache::loncommon::end_data_table_row().
 9491:              &Apache::loncommon::end_data_table()."<br /><br />\n";
 9492:     return $result.&show_grading_menu_form($symb);
 9493: }
 9494: 
 9495: sub navmap_errormsg {
 9496:     return '<div class="LC_error">'.
 9497:            &mt('An error occurred retrieving information about resources in the course.').'<br />'.
 9498:            &mt('It is recommended that you [_1]re-initialize the course[_2] and then return to this grading page.','<a href="/adm/roles?selectrole=1&newrole='.$env{'request.role'}.'">','</a>').
 9499:            '</div>';
 9500: }
 9501: 
 9502: sub handler {
 9503:     my $request=$_[0];
 9504:     &reset_caches();
 9505:     if ($request->header_only) {
 9506:         &Apache::loncommon::content_type($request,'text/html');
 9507:         $request->send_http_header;
 9508:         return OK;
 9509:     }
 9510:     &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'});
 9511: 
 9512:     my $symb=&get_symb($request,1);
 9513:     my @commands=&Apache::loncommon::get_env_multiple('form.command');
 9514:     my $command=$commands[0];
 9515: 
 9516:     if ($#commands > 0) {
 9517: 	&Apache::lonnet::logthis("grades got multiple commands ".join(':',@commands));
 9518:     }
 9519: 
 9520:     $ssi_error = 0;
 9521:     my $brcrum = [{href=>"/adm/grades",text=>"Grading"}];
 9522:     my $start_page = &Apache::loncommon::start_page('Grading',undef,
 9523:                                           {'bread_crumbs' => $brcrum}));
 9524:     if ($symb eq '' && $command eq '') {
 9525: 	if ($env{'user.adv'}) {
 9526:             &Apache::loncommon::content_type($request,'text/html');
 9527:             $request->send_http_header;
 9528:             $request->print($start_page);
 9529: 	    if (($env{'form.codeone'}) && ($env{'form.codetwo'}) &&
 9530: 		($env{'form.codethree'})) {
 9531: 		my $token=$env{'form.codeone'}.'*'.$env{'form.codetwo'}.'*'.
 9532: 		    $env{'form.codethree'};
 9533: 		my ($tsymb,$tuname,$tudom,$tcrsid)=
 9534: 		    &Apache::lonnet::checkin($token);
 9535: 		if ($tsymb) {
 9536: 		    my ($map,$id,$url)=&Apache::lonnet::decode_symb($tsymb);
 9537: 		    if (&Apache::lonnet::allowed('mgr',$tcrsid)) {
 9538: 			$request->print(&ssi_with_retries('/res/'.$url, $ssi_retries,
 9539: 					  ('grade_username' => $tuname,
 9540: 					   'grade_domain' => $tudom,
 9541: 					   'grade_courseid' => $tcrsid,
 9542: 					   'grade_symb' => $tsymb)));
 9543: 		    } else {
 9544: 			$request->print('<h3>Not authorized: '.$token.'</h3>');
 9545: 		    }
 9546: 		} else {
 9547: 		    $request->print('<h3>Not a valid DocID: '.$token.'</h3>');
 9548: 		}
 9549: 	    } else {
 9550: 		$request->print(&Apache::lonxml::tokeninputfield());
 9551: 	    }
 9552:         } elsif ($env{'request.course.id'}) {
 9553:             &init_perm(); 
 9554:             if (!%perm) {
 9555:                 $request->internal_redirect('/adm/quickgrades');
 9556:             } else {
 9557:                 &Apache::loncommon::content_type($request,'text/html');
 9558:                 $request->send_http_header;
 9559:                 $request->print($start_page);
 9560:             }
 9561:         }
 9562:     } else {
 9563:         &init_perm();
 9564:         if (!$env{'request.course.id'}) {
 9565:             # Not in a course.
 9566:             $env{'user.error.msg'}="/adm/grades::vgr:0:0:Cannot display grades page outside course context";
 9567:             return HTTP_NOT_ACCEPTABLE;
 9568:         } elsif (!%perm) {
 9569:             $request->internal_redirect('/adm/quickgrades');
 9570:         }
 9571:         &Apache::loncommon::content_type($request,'text/html');
 9572:         $request->send_http_header;
 9573:         $request->print($start_page);
 9574: 	if ($command eq 'submission' && $perm{'vgr'}) {
 9575: 	    ($env{'form.student'} eq '' ? &listStudents($request) : &submission($request,0,0));
 9576: 	} elsif ($command eq 'pickStudentPage' && $perm{'vgr'}) {
 9577: 	    &pickStudentPage($request);
 9578: 	} elsif ($command eq 'displayPage' && $perm{'vgr'}) {
 9579: 	    &displayPage($request);
 9580: 	} elsif ($command eq 'gradeByPage' && $perm{'mgr'}) {
 9581: 	    &updateGradeByPage($request);
 9582: 	} elsif ($command eq 'processGroup' && $perm{'vgr'}) {
 9583: 	    &processGroup($request);
 9584: 	} elsif ($command eq 'gradingmenu' && $perm{'vgr'}) {
 9585: 	    $request->print(&grading_menu($request));
 9586: 	} elsif ($command eq 'submit_options' && $perm{'vgr'}) {
 9587: 	    $request->print(&submit_options($request));
 9588: 	} elsif ($command eq 'viewgrades' && $perm{'vgr'}) {
 9589: 	    $request->print(&viewgrades($request));
 9590: 	} elsif ($command eq 'handgrade' && $perm{'mgr'}) {
 9591: 	    $request->print(&processHandGrade($request));
 9592: 	} elsif ($command eq 'editgrades' && $perm{'mgr'}) {
 9593: 	    $request->print(&editgrades($request));
 9594: 	} elsif ($command eq 'verify' && $perm{'vgr'}) {
 9595: 	    $request->print(&verifyreceipt($request));
 9596:         } elsif ($command eq 'processclicker' && $perm{'mgr'}) {
 9597:             $request->print(&process_clicker($request));
 9598:         } elsif ($command eq 'processclickerfile' && $perm{'mgr'}) {
 9599:             $request->print(&process_clicker_file($request));
 9600:         } elsif ($command eq 'assignclickergrades' && $perm{'mgr'}) {
 9601:             $request->print(&assign_clicker_grades($request));
 9602: 	} elsif ($command eq 'csvform' && $perm{'mgr'}) {
 9603: 	    $request->print(&upcsvScores_form($request));
 9604: 	} elsif ($command eq 'csvupload' && $perm{'mgr'}) {
 9605: 	    $request->print(&csvupload($request));
 9606: 	} elsif ($command eq 'csvuploadmap' && $perm{'mgr'} ) {
 9607: 	    $request->print(&csvuploadmap($request));
 9608: 	} elsif ($command eq 'csvuploadoptions' && $perm{'mgr'}) {
 9609: 	    if ($env{'form.associate'} ne 'Reverse Association') {
 9610: 		$request->print(&csvuploadoptions($request));
 9611: 	    } else {
 9612: 		if ( $env{'form.upfile_associate'} ne 'reverse' ) {
 9613: 		    $env{'form.upfile_associate'} = 'reverse';
 9614: 		} else {
 9615: 		    $env{'form.upfile_associate'} = 'forward';
 9616: 		}
 9617: 		$request->print(&csvuploadmap($request));
 9618: 	    }
 9619: 	} elsif ($command eq 'csvuploadassign' && $perm{'mgr'} ) {
 9620: 	    $request->print(&csvuploadassign($request));
 9621: 	} elsif ($command eq 'scantron_selectphase' && $perm{'mgr'}) {
 9622: 	    $request->print(&scantron_selectphase($request));
 9623:  	} elsif ($command eq 'scantron_warning' && $perm{'mgr'}) {
 9624:  	    $request->print(&scantron_do_warning($request));
 9625: 	} elsif ($command eq 'scantron_validate' && $perm{'mgr'}) {
 9626: 	    $request->print(&scantron_validate_file($request));
 9627: 	} elsif ($command eq 'scantron_process' && $perm{'mgr'}) {
 9628: 	    $request->print(&scantron_process_students($request));
 9629:  	} elsif ($command eq 'scantronupload' && 
 9630:  		 (&Apache::lonnet::allowed('usc',$env{'request.role.domain'})||
 9631: 		  &Apache::lonnet::allowed('usc',$env{'request.course.id'}))) {
 9632:  	    $request->print(&scantron_upload_scantron_data($request)); 
 9633:  	} elsif ($command eq 'scantronupload_save' &&
 9634:  		 (&Apache::lonnet::allowed('usc',$env{'request.role.domain'})||
 9635: 		  &Apache::lonnet::allowed('usc',$env{'request.course.id'}))) {
 9636:  	    $request->print(&scantron_upload_scantron_data_save($request));
 9637:  	} elsif ($command eq 'scantron_download' &&
 9638: 		 &Apache::lonnet::allowed('usc',$env{'request.course.id'})) {
 9639:  	    $request->print(&scantron_download_scantron_data($request));
 9640:         } elsif ($command eq 'checksubmissions' && $perm{'vgr'}) {
 9641:             $request->print(&checkscantron_results($request));     
 9642: 	} elsif ($command) {
 9643: 	    $request->print('<p class="LC_error">'.&mt('Access Denied ([_1])',$command).'</p>');
 9644: 	}
 9645:     }
 9646:     if ($ssi_error) {
 9647: 	&ssi_print_error($request);
 9648:     }
 9649:     $request->print(&Apache::loncommon::end_page());
 9650:     &reset_caches();
 9651:     return OK;
 9652: }
 9653: 
 9654: 1;
 9655: 
 9656: __END__;
 9657: 
 9658: 
 9659: =head1 NAME
 9660: 
 9661: Apache::grades
 9662: 
 9663: =head1 SYNOPSIS
 9664: 
 9665: Handles the viewing of grades.
 9666: 
 9667: This is part of the LearningOnline Network with CAPA project
 9668: described at http://www.lon-capa.org.
 9669: 
 9670: =head1 OVERVIEW
 9671: 
 9672: Do an ssi with retries:
 9673: While I'd love to factor out this with the vesrion in lonprintout,
 9674: 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
 9675: I'm not quite ready to invent (e.g. an ssi_with_retry object).
 9676: 
 9677: At least the logic that drives this has been pulled out into loncommon.
 9678: 
 9679: 
 9680: 
 9681: ssi_with_retries - Does the server side include of a resource.
 9682:                      if the ssi call returns an error we'll retry it up to
 9683:                      the number of times requested by the caller.
 9684:                      If we still have a proble, no text is appended to the
 9685:                      output and we set some global variables.
 9686:                      to indicate to the caller an SSI error occurred.  
 9687:                      All of this is supposed to deal with the issues described
 9688:                      in LonCAPA BZ 5631 see:
 9689:                      http://bugs.lon-capa.org/show_bug.cgi?id=5631
 9690:                      by informing the user that this happened.
 9691: 
 9692: Parameters:
 9693:   resource   - The resource to include.  This is passed directly, without
 9694:                interpretation to lonnet::ssi.
 9695:   form       - The form hash parameters that guide the interpretation of the resource
 9696:                
 9697:   retries    - Number of retries allowed before giving up completely.
 9698: Returns:
 9699:   On success, returns the rendered resource identified by the resource parameter.
 9700: Side Effects:
 9701:   The following global variables can be set:
 9702:    ssi_error                - If an unrecoverable error occurred this becomes true.
 9703:                               It is up to the caller to initialize this to false
 9704:                               if desired.
 9705:    ssi_error_resource  - If an unrecoverable error occurred, this is the value
 9706:                               of the resource that could not be rendered by the ssi
 9707:                               call.
 9708:    ssi_error_message   - The error string fetched from the ssi response
 9709:                               in the event of an error.
 9710: 
 9711: 
 9712: =head1 HANDLER SUBROUTINE
 9713: 
 9714: ssi_with_retries()
 9715: 
 9716: =head1 SUBROUTINES
 9717: 
 9718: =over
 9719: 
 9720: =item scantron_get_correction() : 
 9721: 
 9722:    Builds the interface screen to interact with the operator to fix a
 9723:    specific error condition in a specific scanline
 9724: 
 9725:  Arguments:
 9726:     $r           - Apache request object
 9727:     $i           - number of the current scanline
 9728:     $scan_record - hash ref as returned from &scantron_parse_scanline()
 9729:     $scan_config - hash ref as returned from &get_scantron_config()
 9730:     $line        - full contents of the current scanline
 9731:     $error       - error condition, valid values are
 9732:                    'incorrectCODE', 'duplicateCODE',
 9733:                    'doublebubble', 'missingbubble',
 9734:                    'duplicateID', 'incorrectID'
 9735:     $arg         - extra information needed
 9736:        For errors:
 9737:          - duplicateID   - paper number that this studentID was seen before on
 9738:          - duplicateCODE - array ref of the paper numbers this CODE was
 9739:                            seen on before
 9740:          - incorrectCODE - current incorrect CODE 
 9741:          - doublebubble  - array ref of the bubble lines that have double
 9742:                            bubble errors
 9743:          - missingbubble - array ref of the bubble lines that have missing
 9744:                            bubble errors
 9745: 
 9746: =item  scantron_get_maxbubble() : 
 9747: 
 9748:    Arguments:
 9749:        $nav_error  - Reference to scalar which is a flag to indicate a
 9750:                       failure to retrieve a navmap object.
 9751:        if $nav_error is set to 1 by scantron_get_maxbubble(), the 
 9752:        calling routine should trap the error condition and display the warning
 9753:        found in &navmap_errormsg().
 9754: 
 9755:    Returns the maximum number of bubble lines that are expected to
 9756:    occur. Does this by walking the selected sequence rendering the
 9757:    resource and then checking &Apache::lonxml::get_problem_counter()
 9758:    for what the current value of the problem counter is.
 9759: 
 9760:    Caches the results to $env{'form.scantron_maxbubble'},
 9761:    $env{'form.scantron.bubble_lines.n'}, 
 9762:    $env{'form.scantron.first_bubble_line.n'} and
 9763:    $env{"form.scantron.sub_bubblelines.n"}
 9764:    which are the total number of bubble, lines, the number of bubble
 9765:    lines for response n and number of the first bubble line for response n,
 9766:    and a comma separated list of numbers of bubble lines for sub-questions
 9767:    (for optionresponse, matchresponse, and rankresponse items), for response n.  
 9768: 
 9769: 
 9770: =item  scantron_validate_missingbubbles() : 
 9771: 
 9772:    Validates all scanlines in the selected file to not have any
 9773:     answers that don't have bubbles that have not been verified
 9774:     to be bubble free.
 9775: 
 9776: =item  scantron_process_students() : 
 9777: 
 9778:    Routine that does the actual grading of the bubble sheet information.
 9779: 
 9780:    The parsed scanline hash is added to %env 
 9781: 
 9782:    Then foreach unskipped scanline it does an &Apache::lonnet::ssi()
 9783:    foreach resource , with the form data of
 9784: 
 9785: 	'submitted'     =>'scantron' 
 9786: 	'grade_target'  =>'grade',
 9787: 	'grade_username'=> username of student
 9788: 	'grade_domain'  => domain of student
 9789: 	'grade_courseid'=> of course
 9790: 	'grade_symb'    => symb of resource to grade
 9791: 
 9792:     This triggers a grading pass. The problem grading code takes care
 9793:     of converting the bubbled letter information (now in %env) into a
 9794:     valid submission.
 9795: 
 9796: =item  scantron_upload_scantron_data() :
 9797: 
 9798:     Creates the screen for adding a new bubble sheet data file to a course.
 9799: 
 9800: =item  scantron_upload_scantron_data_save() : 
 9801: 
 9802:    Adds a provided bubble information data file to the course if user
 9803:    has the correct privileges to do so. 
 9804: 
 9805: =item  valid_file() :
 9806: 
 9807:    Validates that the requested bubble data file exists in the course.
 9808: 
 9809: =item  scantron_download_scantron_data() : 
 9810: 
 9811:    Shows a list of the three internal files (original, corrected,
 9812:    skipped) for a specific bubble sheet data file that exists in the
 9813:    course.
 9814: 
 9815: =item  scantron_validate_ID() : 
 9816: 
 9817:    Validates all scanlines in the selected file to not have any
 9818:    invalid or underspecified student/employee IDs
 9819: 
 9820: =item navmap_errormsg() :
 9821: 
 9822:    Returns HTML mark-up inside a <div></div> with a link to re-initialize the course.
 9823:    Should be called whenever the request to instantiate a navmap object fails.  
 9824: 
 9825: =back
 9826: 
 9827: =cut

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