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

1.17      albertel    1: # The LearningOnline Network with CAPA
1.13      albertel    2: # The LON-CAPA Grading handler
1.17      albertel    3: #
1.150   ! albertel    4: # $Id: grades.pm,v 1.149 2003/11/07 18:05:33 albertel Exp $
1.17      albertel    5: #
                      6: # Copyright Michigan State University Board of Trustees
                      7: #
                      8: # This file is part of the LearningOnline Network with CAPA (LON-CAPA).
                      9: #
                     10: # LON-CAPA is free software; you can redistribute it and/or modify
                     11: # it under the terms of the GNU General Public License as published by
                     12: # the Free Software Foundation; either version 2 of the License, or
                     13: # (at your option) any later version.
                     14: #
                     15: # LON-CAPA is distributed in the hope that it will be useful,
                     16: # but WITHOUT ANY WARRANTY; without even the implied warranty of
                     17: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
                     18: # GNU General Public License for more details.
                     19: #
                     20: # You should have received a copy of the GNU General Public License
                     21: # along with LON-CAPA; if not, write to the Free Software
                     22: # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
                     23: #
                     24: # /home/httpd/html/adm/gpl.txt
                     25: #
                     26: # http://www.lon-capa.org/
                     27: #
1.13      albertel   28: # 2/9,2/13 Guy Albertelli
1.8       www        29: # 6/8 Gerd Kortemeyer
1.13      albertel   30: # 7/26 H.K. Ng
1.14      www        31: # 8/20 Gerd Kortemeyer
1.30      ng         32: # Year 2002
1.44      ng         33: # June-August H.K. Ng
1.68      ng         34: # Year 2003
1.71      ng         35: # February, March H.K. Ng
1.125     ng         36: # July, H. K. Ng
1.30      ng         37: #
1.1       albertel   38: 
                     39: package Apache::grades;
                     40: use strict;
                     41: use Apache::style;
                     42: use Apache::lonxml;
                     43: use Apache::lonnet;
1.3       albertel   44: use Apache::loncommon;
1.112     ng         45: use Apache::lonhtmlcommon;
1.68      ng         46: use Apache::lonnavmaps;
1.1       albertel   47: use Apache::lonhomework;
1.55      matthew    48: use Apache::loncoursedata;
1.38      ng         49: use Apache::lonmsg qw(:user_normal_msg);
1.1       albertel   50: use Apache::Constants qw(:common);
1.87      www        51: use String::Similarity;
                     52: 
                     53: my %oldessays=();
1.103     albertel   54: my %perm=();
1.1       albertel   55: 
1.68      ng         56: # ----- These first few routines are general use routines.----
1.44      ng         57: #
1.146     albertel   58: # --- Retrieve the parts from the metadata file.---
1.44      ng         59: sub getpartlist {
1.146     albertel   60:     my ($url,$symb) = @_;
                     61:     my $partorder = &Apache::lonnet::metadata($url, 'partorder');
                     62:     my @parts;
                     63:     if ($partorder) {
                     64: 	for my $part (split (/,/,$partorder)) {
                     65: 	    if (!&Apache::loncommon::check_if_partid_hidden($part,$symb)) {
                     66: 		push(@parts, $part);
                     67: 	    }
                     68: 	}	    
                     69:     } else {
                     70: 	my $metadata = &Apache::lonnet::metadata($url, 'packages');
                     71: 	foreach (split(/\,/,$metadata)) {
                     72: 	    if ($_ =~ /^part_(.*)$/) {
                     73: 		if (!&Apache::loncommon::check_if_partid_hidden($1,$symb)) {
                     74: 		    push(@parts, $1);
                     75: 		}
                     76: 	    }
1.41      ng         77: 	}
1.16      albertel   78:     }
1.146     albertel   79:     my @stores;
                     80:     foreach my $part (@parts) {
                     81: 	my (@metakeys) = split(/,/,&Apache::lonnet::metadata($url,'keys'));
                     82: 	foreach my $key (@metakeys) {
                     83: 	    if ($key =~ m/^stores_\Q$part\E_/) { push(@stores,$key); }
                     84: 	}
                     85:     }
                     86:     return @stores;
1.2       albertel   87: }
                     88: 
1.44      ng         89: # --- Get the symbolic name of a problem and the url
                     90: sub get_symb_and_url {
                     91:     my ($request) = @_;
                     92:     (my $url=$ENV{'form.url'}) =~ s-^http://($ENV{'SERVER_NAME'}|$ENV{'HTTP_HOST'})--;
1.41      ng         93:     my $symb=($ENV{'form.symb'} ne '' ? $ENV{'form.symb'} : (&Apache::lonnet::symbread($url)));
1.44      ng         94:     if ($symb eq '') { $request->print("Unable to handle ambiguous references:$url:."); return ''; }
                     95:     return ($symb,$url);
1.32      ng         96: }
                     97: 
1.44      ng         98: # --- Retrieve the fullname for a user. Return lastname, first middle ---
                     99: # --- Generation is attached next to the lastname if it exists. ---
1.34      ng        100: sub get_fullname {
1.39      ng        101:     my ($uname,$udom) = @_;
1.34      ng        102:     my %name=&Apache::lonnet::get('environment', ['lastname','generation',
1.55      matthew   103: 						  'firstname','middlename'],
                    104:                                   $udom,$uname);
1.34      ng        105:     my $fullname;
                    106:     my ($tmp) = keys(%name);
                    107:     if ($tmp !~ /^(con_lost|error|no_such_host)/i) {
1.55      matthew   108:         $fullname = &Apache::loncoursedata::ProcessFullName
                    109:             (@name{qw/lastname generation firstname middlename/});
                    110:     } else {
                    111:         &Apache::lonnet::logthis('grades.pm: no name data for '.$uname.
                    112:                                  '@'.$udom.':'.$tmp);
1.34      ng        113:     }
                    114:     return $fullname;
                    115: }
                    116: 
1.129     ng        117: #--- Format fullname, username:domain if different for display
                    118: #--- Use anywhere where the student names are listed
                    119: sub nameUserString {
                    120:     my ($type,$fullname,$uname,$udom) = @_;
                    121:     if ($type eq 'header') {
                    122: 	return '<b>&nbsp;Fullname&nbsp;</b><font color="#999999">(Username)</font>&nbsp;';
                    123:     } else {
                    124: 	return '&nbsp;'.$fullname.'<font color="#999999">&nbsp;('.$uname.
                    125: 	    ($ENV{'user.domain'} eq $udom ? '' : ' ('.$udom.')').')</font>';
                    126:     }
                    127: }
                    128: 
1.44      ng        129: #--- Get the partlist and the response type for a given problem. ---
                    130: #--- Indicate if a response type is coded handgraded or not. ---
1.39      ng        131: sub response_type {
1.125     ng        132:     my ($url,$symb) = shift;
                    133:     $symb=($ENV{'form.symb'} ne '' ? $ENV{'form.symb'} : (&Apache::lonnet::symbread($url))) if ($symb eq '');
1.41      ng        134:     my $allkeys = &Apache::lonnet::metadata($url,'keys');
                    135:     my %seen = ();
1.147     albertel  136:     my (@partlist,%handgrade,%responseType);
1.41      ng        137:     foreach (split(/,/,&Apache::lonnet::metadata($url,'packages'))) {
1.147     albertel  138: 	if (/^\w+response_.*/) {
1.41      ng        139: 	    my ($responsetype,$part) = split(/_/,$_,2);
                    140: 	    my ($partid,$respid) = split(/_/,$part);
1.146     albertel  141: 	    if (&Apache::loncommon::check_if_partid_hidden($partid,$symb)) {
                    142: 		next;
                    143: 	    }
1.118     ng        144: 	    $responsetype =~ s/response$//; # make it compatible w/ navmaps - should move to that!!
1.127     ng        145: 	    my ($value) = &Apache::lonnet::EXT('resource.'.$part.'.handgrade',$symb);
1.147     albertel  146: 	    $handgrade{$part} = ($value eq 'yes' ? 'yes' : 'no'); 
                    147: 	    if (!exists($responseType{$partid})) { $responseType{$partid}={}; }
                    148: 	    $responseType{$partid}->{$respid}=$responsetype;
1.41      ng        149: 	    next if ($seen{$partid} > 0);
                    150: 	    $seen{$partid}++;
                    151: 	    push @partlist,$partid;
                    152: 	}
                    153:     }
1.147     albertel  154:     return \@partlist,\%handgrade,\%responseType;
1.39      ng        155: }
                    156: 
1.118     ng        157: #--- Show resource title
                    158: #--- and parts and response type
                    159: sub showResourceInfo {
                    160:     my ($url,$probTitle) = @_;
                    161:     my $result ='<table border="0">'.
                    162: 	'<tr><td colspan=3><font size=+1><b>Current Resource: </b>'.$probTitle.'</font></td></tr>'."\n";
1.147     albertel  163:     my ($partlist,$handgrade,$responseType) = &response_type($url);
1.126     ng        164:     my %resptype = ();
1.122     ng        165:     my $hdgrade='no';
1.147     albertel  166:     for my $part_resID (sort keys(%$handgrade)) {
                    167: 	my $handgrade=$$handgrade{$part_resID};
                    168: 	my ($partID,$resID) = split(/_/,$part_resID);
                    169: 	my $responsetype = $responseType->{$partID}->{$resID};
1.118     ng        170: 	$hdgrade = $handgrade if ($handgrade eq 'yes');
1.147     albertel  171: 	$result.='<tr><td><b>Part </b>'.$partID.' <font color="#999999">'.
                    172: 	    $resID.'</font></td>'.
1.118     ng        173: 	    '<td><b>Type: </b>'.$responsetype.'</td></tr>';
                    174: #	    '<td><b>Handgrade: </b>'.$handgrade.'</td></tr>';
                    175:     }
                    176:     $result.='</table>'."\n";
1.147     albertel  177:     return $result,$responseType,$hdgrade,$partlist,$handgrade;
1.118     ng        178: }
                    179: 
1.148     albertel  180: 
                    181: sub get_order {
                    182:     my ($partid,$respid,$symb,$uname,$udom)=@_;
                    183:     my (undef,undef,$url)=&Apache::lonnet::decode_symb($symb);
                    184:     $url=&Apache::lonnet::clutter($url);
                    185:     my $subresult=&Apache::lonnet::ssi($url,
                    186: 				       ('grade_target' => 'analyze'),
                    187: 				       ('grade_domain' => $udom),
                    188: 				       ('grade_symb' => $symb),
                    189: 				       ('grade_courseid' => 
                    190: 					        $ENV{'request.course.id'}),
                    191: 				       ('grade_username' => $uname));
1.149     albertel  192:     (undef,$subresult)=split(/_HASH_REF__/,$subresult,2);
1.148     albertel  193:     my %analyze=&Apache::lonnet::str2hash($subresult);
                    194:     return ($analyze{"$partid.$respid.shown"});
                    195: }
1.118     ng        196: #--- Clean response type for display
1.148     albertel  197: #--- Currently filters option/rank/radiobutton/match/essay response types only.
1.118     ng        198: sub cleanRecord {
1.148     albertel  199:     my ($answer,$response,$symb,$partid,$respid,$record,$order,$version) = @_;
                    200:     my $grayFont = '<font color="#999999">';
                    201:     if ($response =~ /^(option|rank)$/) {
                    202: 	my %answer=&Apache::lonnet::str2hash($answer);
                    203: 	my %grading=&Apache::lonnet::str2hash($record->{$version."resource.$partid.$respid.submissiongrading"});
                    204: 	my ($toprow,$bottomrow);
                    205: 	foreach my $foil (@$order) {
                    206: 	    if ($grading{$foil} == 1) {
                    207: 		$toprow.='<td><b>'.$answer{$foil}.'&nbsp;</b></td>';
                    208: 	    } else {
                    209: 		$toprow.='<td><i>'.$answer{$foil}.'&nbsp;</i></td>';
                    210: 	    }
                    211: 	    $bottomrow.='<td>'.$grayFont.$foil.'</font>&nbsp;</td>';
                    212: 	}
                    213: 	return '<blockquote><table border="1">'.
                    214: 	    '<tr valign="top"><td>Answer</td>'.$toprow.'</tr>'.
                    215: 	    '<tr valign="top"><td>'.$grayFont.'Option ID</font></td>'.
                    216: 	    $grayFont.$bottomrow.'</tr>'.'</table></blockquote>';
                    217:     } elsif ($response eq 'match') {
                    218: 	my %answer=&Apache::lonnet::str2hash($answer);
                    219: 	my %grading=&Apache::lonnet::str2hash($record->{$version."resource.$partid.$respid.submissiongrading"});
                    220: 	my @items=&Apache::lonnet::str2array($record->{$version."resource.$partid.$respid.submissionitems"});
                    221: 	my ($toprow,$middlerow,$bottomrow);
                    222: 	foreach my $foil (@$order) {
                    223: 	    my $item=shift(@items);
                    224: 	    if ($grading{$foil} == 1) {
                    225: 		$toprow.='<td><b>'.$item.'&nbsp;</b></td>';
                    226: 		$middlerow.='<td><b>'.$grayFont.$answer{$foil}.'&nbsp;</font></b></td>';
                    227: 	    } else {
                    228: 		$toprow.='<td><i>'.$item.'&nbsp;</i></td>';
                    229: 		$middlerow.='<td><i>'.$grayFont.$answer{$foil}.'&nbsp;</font></i></td>';
                    230: 	    }
                    231: 	    $bottomrow.='<td>'.$grayFont.$foil.'</font>&nbsp;</td>';
1.118     ng        232: 	}
1.126     ng        233: 	return '<blockquote><table border="1">'.
1.148     albertel  234: 	    '<tr valign="top"><td>Answer</td>'.$toprow.'</tr>'.
                    235: 	    '<tr valign="top"><td>'.$grayFont.'Item ID</font></td>'.
                    236: 	    $middlerow.'</tr>'.
                    237: 	    '<tr valign="top"><td>'.$grayFont.'Option ID</font></td>'.
                    238: 	    $bottomrow.'</tr>'.'</table></blockquote>';
                    239:     } elsif ($response eq 'radiobutton') {
                    240: 	my %answer=&Apache::lonnet::str2hash($answer);
                    241: 	my ($toprow,$bottomrow);
                    242: 	my $correct=($order->[0])+1;
                    243: 	for (my $i=1;$i<=$#$order;$i++) {
                    244: 	    my $foil=$order->[$i];
                    245: 	    if (exists($answer{$foil})) {
                    246: 		if ($i == $correct) {
                    247: 		    $toprow.='<td><b>true</b></td>';
                    248: 		} else {
                    249: 		    $toprow.='<td><i>true</i></td>';
                    250: 		}
                    251: 	    } else {
                    252: 		$toprow.='<td>false</td>';
                    253: 	    }
                    254: 	    $bottomrow.='<td>'.$grayFont.$foil.'</font>&nbsp;</td>';
                    255: 	}
                    256: 	return '<blockquote><table border="1">'.
                    257: 	    '<tr valign="top"><td>Answer</td>'.$toprow.'</tr>'.
                    258: 	    '<tr valign="top"><td>'.$grayFont.'Option ID</font></td>'.
                    259: 	    $grayFont.$bottomrow.'</tr>'.'</table></blockquote>';
                    260:     } elsif ($response eq 'essay') {
1.122     ng        261: 	if (! exists ($ENV{'form.'.$symb})) {
                    262: 	    my (%keyhash) = &Apache::lonnet::dump('nohist_handgrade',
                    263: 						  $ENV{'course.'.$ENV{'request.course.id'}.'.domain'},
                    264: 						  $ENV{'course.'.$ENV{'request.course.id'}.'.num'});
                    265: 
                    266: 	    my $loginuser = $ENV{'user.name'}.':'.$ENV{'user.domain'};
                    267: 	    $ENV{'form.keywords'} = $keyhash{$symb.'_keywords'} ne '' ? $keyhash{$symb.'_keywords'} : '';
                    268: 	    $ENV{'form.kwclr'}    = $keyhash{$loginuser.'_kwclr'} ne '' ? $keyhash{$loginuser.'_kwclr'} : 'red';
                    269: 	    $ENV{'form.kwsize'}   = $keyhash{$loginuser.'_kwsize'} ne '' ? $keyhash{$loginuser.'_kwsize'} : '0';
                    270: 	    $ENV{'form.kwstyle'}  = $keyhash{$loginuser.'_kwstyle'} ne '' ? $keyhash{$loginuser.'_kwstyle'} : '';
                    271: 	    $ENV{'form.'.$symb} = 1; # so that we don't have to read it from disk for multiple sub of the same prob.
                    272: 	}
1.143     albertel  273: 	return '<br /><br /><blockquote><pre>'.&keywords_highlight($answer).'</pre></blockquote>';
1.122     ng        274:     }
1.118     ng        275:     return $answer;
                    276: }
                    277: 
                    278: #-- A couple of common js functions
                    279: sub commonJSfunctions {
                    280:     my $request = shift;
                    281:     $request->print(<<COMMONJSFUNCTIONS);
                    282: <script type="text/javascript" language="javascript">
                    283:     function radioSelection(radioButton) {
                    284: 	var selection=null;
                    285: 	if (radioButton.length > 1) {
                    286: 	    for (var i=0; i<radioButton.length; i++) {
                    287: 		if (radioButton[i].checked) {
                    288: 		    return radioButton[i].value;
                    289: 		}
                    290: 	    }
                    291: 	} else {
                    292: 	    if (radioButton.checked) return radioButton.value;
                    293: 	}
                    294: 	return selection;
                    295:     }
                    296: 
                    297:     function pullDownSelection(selectOne) {
                    298: 	var selection="";
                    299: 	if (selectOne.length > 1) {
                    300: 	    for (var i=0; i<selectOne.length; i++) {
                    301: 		if (selectOne[i].selected) {
                    302: 		    return selectOne[i].value;
                    303: 		}
                    304: 	    }
                    305: 	} else {
1.138     albertel  306:             // only one value it must be the selected one
                    307: 	    return selectOne.value;
1.118     ng        308: 	}
                    309:     }
                    310: </script>
                    311: COMMONJSFUNCTIONS
                    312: }
                    313: 
1.44      ng        314: #--- Dumps the class list with usernames,list of sections,
                    315: #--- section, ids and fullnames for each user.
                    316: sub getclasslist {
1.76      ng        317:     my ($getsec,$filterlist) = @_;
1.121     ng        318:     $getsec = $getsec eq '' ? 'all' : $getsec;
1.56      matthew   319:     my $classlist=&Apache::loncoursedata::get_classlist();
1.49      albertel  320:     # Bail out if we were unable to get the classlist
1.56      matthew   321:     return if (! defined($classlist));
                    322:     #
                    323:     my %sections;
                    324:     my %fullnames;
                    325:     foreach (keys(%$classlist)) {
                    326:         # the following undefs are for 'domain', and 'username' respectively.
                    327: 	my (undef,undef,$end,$start,$id,$section,$fullname,$status)=
                    328:             @{$classlist->{$_}};
1.76      ng        329: 	# filter students according to status selected
1.112     ng        330: 	if ($filterlist && $ENV{'form.Status'} ne 'Any') {
                    331: 	    if ($ENV{'form.Status'} ne $status) {
1.76      ng        332: 		delete ($classlist->{$_});
                    333: 		next;
                    334: 	    }
                    335: 	}
1.44      ng        336: 	$section = ($section ne '' ? $section : 'no');
1.106     albertel  337: 	if (&canview($section)) {
1.103     albertel  338: 	    if ($getsec eq 'all' || $getsec eq $section) {
                    339: 		$sections{$section}++;
                    340: 		$fullnames{$_}=$fullname;
                    341: 	    } else {
                    342: 		delete($classlist->{$_});
                    343: 	    }
                    344: 	} else {
                    345: 	    delete($classlist->{$_});
                    346: 	}
1.44      ng        347:     }
                    348:     my %seen = ();
1.56      matthew   349:     my @sections = sort(keys(%sections));
                    350:     return ($classlist,\@sections,\%fullnames);
1.44      ng        351: }
                    352: 
1.103     albertel  353: sub canmodify {
                    354:     my ($sec)=@_;
                    355:     if ($perm{'mgr'}) {
                    356: 	if (!defined($perm{'mgr_section'})) {
                    357: 	    # can modify whole class
                    358: 	    return 1;
                    359: 	} else {
                    360: 	    if ($sec eq $perm{'mgr_section'}) {
                    361: 		#can modify the requested section
                    362: 		return 1;
                    363: 	    } else {
                    364: 		# can't modify the request section
                    365: 		return 0;
                    366: 	    }
                    367: 	}
                    368:     }
                    369:     #can't modify
                    370:     return 0;
                    371: }
                    372: 
                    373: sub canview {
                    374:     my ($sec)=@_;
                    375:     if ($perm{'vgr'}) {
                    376: 	if (!defined($perm{'vgr_section'})) {
                    377: 	    # can modify whole class
                    378: 	    return 1;
                    379: 	} else {
                    380: 	    if ($sec eq $perm{'vgr_section'}) {
                    381: 		#can modify the requested section
                    382: 		return 1;
                    383: 	    } else {
                    384: 		# can't modify the request section
                    385: 		return 0;
                    386: 	    }
                    387: 	}
                    388:     }
                    389:     #can't modify
                    390:     return 0;
                    391: }
                    392: 
1.44      ng        393: #--- Retrieve the grade status of a student for all the parts
                    394: sub student_gradeStatus {
                    395:     my ($url,$symb,$udom,$uname,$partlist) = @_;
                    396:     my %record     = &Apache::lonnet::restore($symb,$ENV{'request.course.id'},$udom,$uname);
                    397:     my %partstatus = ();
                    398:     foreach (@$partlist) {
1.128     ng        399: 	my ($status,undef)   = split(/_/,$record{"resource.$_.solved"},2);
1.44      ng        400: 	$status              = 'nothing' if ($status eq '');
                    401: 	$partstatus{$_}      = $status;
                    402: 	my $subkey           = "resource.$_.submitted_by";
                    403: 	$partstatus{$subkey} = $record{$subkey} if ($record{$subkey} ne '');
                    404:     }
                    405:     return %partstatus;
                    406: }
                    407: 
1.45      ng        408: # hidden form and javascript that calls the form
                    409: # Use by verifyscript and viewgrades
                    410: # Shows a student's view of problem and submission
                    411: sub jscriptNform {
                    412:     my ($url,$symb) = @_;
                    413:     my $jscript='<script type="text/javascript" language="javascript">'."\n".
                    414: 	'    function viewOneStudent(user,domain) {'."\n".
                    415: 	'	document.onestudent.student.value = user;'."\n".
                    416: 	'	document.onestudent.userdom.value = domain;'."\n".
                    417: 	'	document.onestudent.submit();'."\n".
                    418: 	'    }'."\n".
                    419: 	'</script>'."\n";
                    420:     $jscript.= '<form action="/adm/grades" method="post" name="onestudent">'."\n".
                    421: 	'<input type="hidden" name="symb"    value="'.$symb.'" />'."\n".
                    422: 	'<input type="hidden" name="url"     value="'.$url.'" />'."\n".
1.77      ng        423: 	'<input type="hidden" name="saveState" value="'.$ENV{'form.saveState'}.'" />'."\n".
1.72      ng        424: 	'<input type="hidden" name="probTitle" value="'.$ENV{'form.probTitle'}.'" />'."\n".
1.125     ng        425: 	'<input type="hidden" name="Status"  value="'.$ENV{'form.Status'}.'" />'."\n".
1.45      ng        426: 	'<input type="hidden" name="command" value="submission" />'."\n".
                    427: 	'<input type="hidden" name="student" value="" />'."\n".
                    428: 	'<input type="hidden" name="userdom" value="" />'."\n".
                    429: 	'</form>'."\n";
                    430:     return $jscript;
                    431: }
1.39      ng        432: 
1.44      ng        433: #------------------ End of general use routines --------------------
1.87      www       434: 
                    435: #
                    436: # Find most similar essay
                    437: #
                    438: 
                    439: sub most_similar {
                    440:     my ($uname,$udom,$uessay)=@_;
                    441: 
                    442: # ignore spaces and punctuation
                    443: 
                    444:     $uessay=~s/\W+/ /gs;
                    445: 
                    446: # these will be returned. Do not care if not at least 50 percent similar
1.88      www       447:     my $limit=0.6;
1.87      www       448:     my $sname='';
                    449:     my $sdom='';
                    450:     my $scrsid='';
                    451:     my $sessay='';
                    452: # go through all essays ...
                    453:     foreach my $tkey (keys %oldessays) {
                    454: 	my ($tname,$tdom,$tcrsid)=split(/\./,$tkey);
                    455: # ... except the same student
1.88      www       456:         if (($tname ne $uname) || ($tdom ne $udom)) {
1.87      www       457: 	    my $tessay=$oldessays{$tkey};
                    458:             $tessay=~s/\W+/ /gs;
                    459: # String similarity gives up if not even limit
1.88      www       460:             my $tsimilar=&String::Similarity::similarity($uessay,$tessay,$limit);
1.87      www       461: # Found one
                    462:             if ($tsimilar>$limit) {
                    463: 		$limit=$tsimilar;
                    464:                 $sname=$tname;
1.88      www       465:                 $sdom=$tdom;
1.87      www       466:                 $scrsid=$tcrsid;
                    467:                 $sessay=$oldessays{$tkey};
                    468:             }
                    469:         } 
                    470:     }
1.88      www       471:     if ($limit>0.6) {
1.87      www       472:        return ($sname,$sdom,$scrsid,$sessay,$limit);
                    473:     } else {
                    474:        return ('','','','',0);
                    475:     }
                    476: }
                    477: 
1.44      ng        478: #-------------------------------------------------------------------
                    479: 
                    480: #------------------------------------ Receipt Verification Routines
1.45      ng        481: #
1.44      ng        482: #--- Check whether a receipt number is valid.---
                    483: sub verifyreceipt {
                    484:     my $request  = shift;
                    485: 
                    486:     my $courseid = $ENV{'request.course.id'};
                    487:     my $receipt  = unpack("%32C*",$Apache::lonnet::perlvar{'lonHostID'}).'-'.
                    488: 	$ENV{'form.receipt'};
                    489:     $receipt     =~ s/[^\-\d]//g;
                    490:     my $url      = $ENV{'form.url'};
                    491:     my $symb     = $ENV{'form.symb'};
                    492:     unless ($symb) {
                    493: 	$symb    = &Apache::lonnet::symbread($url);
                    494:     }
                    495: 
1.45      ng        496:     my $title.='<h3><font color="#339933">Verifying Submission Receipt '.
                    497: 	$receipt.'</h3></font>'."\n".
1.118     ng        498: 	'<font size=+1><b>Resource: </b>'.$ENV{'form.probTitle'}.'</font><br><br>'."\n";
1.44      ng        499: 
                    500:     my ($string,$contents,$matches) = ('','',0);
1.56      matthew   501:     my (undef,undef,$fullname) = &getclasslist('all','0');
                    502: 
1.53      albertel  503:     foreach (sort {lc($$fullname{$a}) cmp lc($$fullname{$b}) } keys %$fullname) {
1.44      ng        504: 	my ($uname,$udom)=split(/\:/);
                    505: 	if ($receipt eq 
                    506: 	    &Apache::lonnet::ireceipt($uname,$udom,$courseid,$symb)) {
                    507: 	    $contents.='<tr bgcolor="#ffffe6"><td>&nbsp;'."\n".
                    508: 		'<a href="javascript:viewOneStudent(\''.$uname.'\',\''.$udom.
                    509: 		'\')"; TARGET=_self>'.$$fullname{$_}.'</a>&nbsp;</td>'."\n".
                    510: 		'<td>&nbsp;'.$uname.'&nbsp;</td>'.
                    511: 		'<td>&nbsp;'.$udom.'&nbsp;</td></tr>'."\n";
                    512: 	    
                    513: 	    $matches++;
                    514: 	}
                    515:     }
                    516:     if ($matches == 0) {
                    517: 	$string = $title.'No match found for the above receipt.';
                    518:     } else {
1.45      ng        519: 	$string = &jscriptNform($url,$symb).$title.
1.44      ng        520: 	    'The above receipt matches the following student'.
                    521: 	    ($matches <= 1 ? '.' : 's.')."\n".
                    522: 	    '<table border="0"><tr><td bgcolor="#777777">'."\n".
                    523: 	    '<table border="0"><tr bgcolor="#e6ffff">'."\n".
                    524: 	    '<td><b>&nbsp;Fullname&nbsp;</b></td>'."\n".
                    525: 	    '<td><b>&nbsp;Username&nbsp;</b></td>'."\n".
                    526: 	    '<td><b>&nbsp;Domain&nbsp;</b></td></tr>'."\n".
                    527: 	    $contents.
                    528: 	    '</table></td></tr></table>'."\n";
                    529:     }
1.50      albertel  530:     return $string.&show_grading_menu_form($symb,$url);
1.44      ng        531: }
                    532: 
                    533: #--- This is called by a number of programs.
                    534: #--- Called from the Grading Menu - View/Grade an individual student
                    535: #--- Also called directly when one clicks on the subm button 
                    536: #    on the problem page.
1.30      ng        537: sub listStudents {
1.41      ng        538:     my ($request) = shift;
1.49      albertel  539: 
1.72      ng        540:     my ($symb,$url) = &get_symb_and_url($request);
1.49      albertel  541:     my $cdom      = $ENV{"course.$ENV{'request.course.id'}.domain"};
                    542:     my $cnum      = $ENV{"course.$ENV{'request.course.id'}.num"};
                    543:     my $getsec    = $ENV{'form.section'} eq '' ? 'all' : $ENV{'form.section'};
                    544:     my $submitonly= $ENV{'form.submitonly'} eq '' ? 'all' : $ENV{'form.submitonly'};
                    545: 
1.118     ng        546:     my $viewgrade = $ENV{'form.showgrading'} eq 'yes' ? 'View/Grade/Regrade' : 'View';
1.76      ng        547:     $ENV{'form.probTitle'} = $ENV{'form.probTitle'} eq '' ? 
                    548: 	&Apache::lonnet::gettitle($symb) : $ENV{'form.probTitle'};
1.49      albertel  549: 
1.118     ng        550:     my $result='<h3><font color="#339933">&nbsp;'.$viewgrade.
                    551: 	' Submissions for a Student or a Group of Students</font></h3>';
                    552: 
1.122     ng        553:     my ($table,undef,$hdgrade,$partlist,$handgrade) = &showResourceInfo($url,$ENV{'form.probTitle'});
1.118     ng        554:     $result.=$table;
1.49      albertel  555: 
1.45      ng        556:     $request->print(<<LISTJAVASCRIPT);
                    557: <script type="text/javascript" language="javascript">
1.110     ng        558:     function checkSelect(checkBox) {
                    559: 	var ctr=0;
                    560: 	var sense="";
                    561: 	if (checkBox.length > 1) {
                    562: 	    for (var i=0; i<checkBox.length; i++) {
                    563: 		if (checkBox[i].checked) {
                    564: 		    ctr++;
                    565: 		}
                    566: 	    }
                    567: 	    sense = "a student or group of students";
                    568: 	} else {
                    569: 	    if (checkBox.checked) {
                    570: 		ctr = 1;
                    571: 	    }
                    572: 	    sense = "the student";
                    573: 	}
                    574: 	if (ctr == 0) {
1.126     ng        575: 	    alert("Please select "+sense+" before clicking on the Next button.");
1.110     ng        576: 	    return false;
                    577: 	}
                    578: 	document.gradesub.submit();
                    579:     }
                    580: 
                    581:     function reLoadList(formname) {
1.112     ng        582: 	if (formname.saveStatusOld.value == pullDownSelection(formname.Status)) {return;}
1.110     ng        583: 	formname.command.value = 'submission';
                    584: 	formname.submit();
                    585:     }
1.45      ng        586: </script>
                    587: LISTJAVASCRIPT
                    588: 
1.118     ng        589:     &commonJSfunctions($request);
1.41      ng        590:     $request->print($result);
1.39      ng        591: 
1.118     ng        592:     my $checkhdgrade = ($ENV{'form.handgrade'} eq 'yes' && scalar(@$partlist) > 1 ) ? 'checked' : '';
1.119     ng        593:     my $checklastsub = $checkhdgrade eq '' ? 'checked' : '';
1.45      ng        594:     my $gradeTable='<form action="/adm/grades" method="post" name="gradesub">'."\n".
1.144     albertel  595: 	'&nbsp;<b>View Problem Text: </b><input type="radio" name="vProb" value="no" checked="on" /> no '."\n".
1.80      ng        596: 	'<input type="radio" name="vProb" value="yes" /> one student '."\n".
1.58      albertel  597: 	'<input type="radio" name="vProb" value="all" /> all students <br />'."\n".
1.144     albertel  598: 	'&nbsp;<b>View Answer: </b><input type="radio" name="vAns" value="no"  /> no '."\n".
                    599: 	'<input type="radio" name="vAns" value="yes" /> one student '."\n".
                    600: 	'<input type="radio" name="vAns" value="all" checked="on" /> all students <br />'."\n".
1.49      albertel  601: 	'&nbsp;<b>Submissions: </b>'."\n";
1.118     ng        602:     if ($ENV{'form.handgrade'} eq 'yes' && scalar(@$partlist) > 1) {
                    603: 	$gradeTable.='<input type="radio" name="lastSub" value="hdgrade" '.$checkhdgrade.' /> essay part only'."\n";
1.49      albertel  604:     }
1.110     ng        605: 
1.112     ng        606:     my $saveStatus = $ENV{'form.Status'} eq '' ? 'Active' : $ENV{'form.Status'};
                    607:     $ENV{'form.Status'} = $saveStatus;
1.110     ng        608: 
1.135     bowersj2  609:     $gradeTable.='<input type="radio" name="lastSub" value="lastonly" '.$checklastsub.' /> last submission only'."\n".
                    610: 	'<input type="radio" name="lastSub" value="last" /> last submission & parts info'."\n".
1.122     ng        611: 	'<input type="radio" name="lastSub" value="datesub" /> by dates and submissions'."\n".
1.45      ng        612: 	'<input type="radio" name="lastSub" value="all" /> all details'."\n".
                    613: 	'<input type="hidden" name="section"     value="'.$getsec.'" />'."\n".
                    614: 	'<input type="hidden" name="submitonly"  value="'.$submitonly.'" />'."\n".
1.65      albertel  615: 	'<input type="hidden" name="handgrade"   value="'.$ENV{'form.handgrade'}.'" /><br />'."\n".
1.64      albertel  616: 	'<input type="hidden" name="showgrading" value="'.$ENV{'form.showgrading'}.'" /><br />'."\n".
1.77      ng        617: 	'<input type="hidden" name="saveState"   value="'.$ENV{'form.saveState'}.'" />'."\n".
1.72      ng        618: 	'<input type="hidden" name="probTitle"   value="'.$ENV{'form.probTitle'}.'" />'."\n".
1.48      albertel  619: 	'<input type="hidden" name="url"  value="'.$url.'" />'."\n".
                    620: 	'<input type="hidden" name="symb" value="'.$symb.'" />'."\n".
1.110     ng        621: 	'<input type="hidden" name="saveStatusOld" value="'.$saveStatus.'" />'."\n";
                    622: 
1.124     ng        623:     if (exists($ENV{'form.gradingMenu'}) && exists($ENV{'form.Status'})) {
                    624: 	$gradeTable.='<input type="hidden" name="Status"   value="'.$ENV{'form.Status'}.'" />'."\n";
                    625:     } else {
                    626: 	$gradeTable.='<b>Student Status:</b> '.
                    627: 	    &Apache::lonhtmlcommon::StatusOptions($saveStatus,undef,1,'javascript:reLoadList(this.form);').'<br />';
                    628:     }
1.112     ng        629: 
1.126     ng        630:     $gradeTable.='To '.lc($viewgrade).' a submission or a group of submissions, click on the check box(es) '.
                    631: 	'next to the student\'s name(s). Then click on the Next button.<br />'."\n".
1.110     ng        632: 	'<input type="hidden" name="command" value="processGroup" />'."\n";
                    633:     $gradeTable.='<input type="button" '."\n".
1.45      ng        634: 	'onClick="javascript:checkSelect(this.form.stuinfo);" '."\n".
1.126     ng        635: 	'value="Next->" />'."\n";
1.134     www       636:     $gradeTable.='<input type="checkbox" name="checkPlag" checked="on">Check For Plagiarism</input>';
1.110     ng        637:     my (undef, undef, $fullname) = &getclasslist($getsec,'1');  
1.45      ng        638:     $gradeTable.='<table border="0"><tr><td bgcolor="#777777">'.
1.110     ng        639: 	'<table border="0"><tr bgcolor="#e6ffff">';
                    640:     my $loop = 0;
                    641:     while ($loop < 2) {
1.126     ng        642: 	$gradeTable.='<td><b>&nbsp;No.</b>&nbsp;</td><td><b>&nbsp;Select&nbsp;</b></td>'.
1.129     ng        643: 	    '<td>'.&nameUserString('header').'</td>';
1.110     ng        644: 	if ($ENV{'form.showgrading'} eq 'yes' && $submitonly ne 'all') {
                    645: 	    foreach (sort(@$partlist)) {
                    646: 		$gradeTable.='<td><b>&nbsp;Part '.(split(/_/))[0].' Status&nbsp;</b></td>';
                    647: 	    }
                    648: 	}
                    649: 	$loop++;
1.126     ng        650: #	$gradeTable.='<td></td>' if ($loop%2 ==1);
1.41      ng        651:     }
1.45      ng        652:     $gradeTable.='</tr>'."\n";
1.41      ng        653: 
1.45      ng        654:     my $ctr = 0;
1.53      albertel  655:     foreach my $student (sort {lc($$fullname{$a}) cmp lc($$fullname{$b}) } keys %$fullname) {
1.41      ng        656: 	my ($uname,$udom) = split(/:/,$student);
1.110     ng        657: 	my %status = ();
                    658: 	if ($ENV{'form.showgrading'} eq 'yes' && $submitonly ne 'all') {
                    659: 	    (%status) =&student_gradeStatus($url,$symb,$udom,$uname,$partlist);
1.145     albertel  660: 	    my $submitted = 0;
                    661: 	    my $graded = 1;
1.110     ng        662: 	    foreach (keys(%status)) {
1.145     albertel  663: 		$submitted = 1 if ($status{$_} ne 'nothing');
                    664: 		$graded = 0 if ($status{$_} =~ /^correct/);
1.110     ng        665: 		my ($foo,$partid,$foo1) = split(/\./,$_);
                    666: 		if ($status{'resource.'.$partid.'.submitted_by'} ne '') {
1.145     albertel  667: 		    $submitted = 0;
1.150   ! albertel  668: 		    my ($part)=split(/\./,$partid);
1.110     ng        669: 		    $gradeTable.='<input type="hidden" name="'.
1.150   ! albertel  670: 			$student.':'.$part.':submitted_by" value="'.
1.110     ng        671: 			$status{'resource.'.$partid.'.submitted_by'}.'" />';
                    672: 		}
1.41      ng        673: 	    }
1.145     albertel  674: 	    next if (!$submitted && ($submitonly eq 'yes' || $submitonly eq 'graded'));
                    675: 	    next if (!$graded && $submitonly eq 'graded');
1.41      ng        676: 	}
1.34      ng        677: 
1.45      ng        678: 	$ctr++;
1.104     albertel  679: 	if ( $perm{'vgr'} eq 'F' ) {
1.110     ng        680: 	    $gradeTable.='<tr bgcolor="#ffffe6">' if ($ctr%2 ==1);
1.126     ng        681: 	    $gradeTable.='<td align="right">'.$ctr.'&nbsp;</td>'.
                    682: 		'<td align="center"><input type=checkbox name="stuinfo" value="'.
1.110     ng        683: 		$student.':'.$$fullname{$student}.'&nbsp;"></td>'."\n".
1.129     ng        684: 		'<td>'.&nameUserString(undef,$$fullname{$student},$uname,$udom).'</td>'."\n";
1.110     ng        685: 
                    686: 	    if ($ENV{'form.showgrading'} eq 'yes' && $submitonly ne 'all') {
                    687: 		foreach (sort keys(%status)) {
                    688: 		    next if (/^resource.*?submitted_by$/);
                    689: 		    $gradeTable.='<td align="middle">&nbsp;'.$status{$_}.'&nbsp;</td>'."\n";
                    690: 		}
1.41      ng        691: 	    }
1.126     ng        692: #	    $gradeTable.='<td></td>' if ($ctr%2 ==1);
1.110     ng        693: 	    $gradeTable.='</tr>'."\n" if ($ctr%2 ==0);
1.41      ng        694: 	}
                    695:     }
1.110     ng        696:     if ($ctr%2 ==1) {
1.126     ng        697: 	$gradeTable.='<td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td>';
1.110     ng        698: 	    if ($ENV{'form.showgrading'} eq 'yes' && $submitonly ne 'all') {
                    699: 		foreach (@$partlist) {
                    700: 		    $gradeTable.='<td>&nbsp;</td>';
                    701: 		}
                    702: 	    }
                    703: 	$gradeTable.='</tr>';
                    704:     }
                    705: 
1.45      ng        706:     $gradeTable.='</table></td></tr></table>'.
                    707: 	'<input type="button" '.
                    708: 	'onClick="javascript:checkSelect(this.form.stuinfo);" '.
1.126     ng        709: 	'value="Next->" /></form>'."\n";
1.45      ng        710:     if ($ctr == 0) {
1.96      albertel  711: 	my $num_students=(scalar(keys(%$fullname)));
                    712: 	if ($num_students eq 0) {
                    713: 	    $gradeTable='<br />&nbsp;<font color="red">There are no students currently enrolled.</font>';
                    714: 	} else {
                    715: 	    $gradeTable='<br />&nbsp;<font color="red">'.
1.110     ng        716: 		'No submissions found for this resource for any students. ('.$num_students.
1.145     albertel  717: 		' checked for submissions)</font><br />';
1.96      albertel  718: 	}
1.46      ng        719:     } elsif ($ctr == 1) {
                    720: 	$gradeTable =~ s/type=checkbox/type=checkbox checked/;
1.45      ng        721:     }
1.50      albertel  722:     $gradeTable.=&show_grading_menu_form($symb,$url);
1.45      ng        723:     $request->print($gradeTable);
1.44      ng        724:     return '';
1.10      ng        725: }
                    726: 
1.44      ng        727: #---- Called from the listStudents routine
                    728: #     Displays the submissions for one student or a group of students
1.34      ng        729: sub processGroup {
1.41      ng        730:     my ($request)  = shift;
                    731:     my $ctr        = 0;
                    732:     my @stuchecked = (ref($ENV{'form.stuinfo'}) ? @{$ENV{'form.stuinfo'}}
                    733: 		      : ($ENV{'form.stuinfo'}));
                    734:     my $total      = scalar(@stuchecked)-1;
1.45      ng        735: 
1.41      ng        736:     foreach (@stuchecked) {
                    737: 	my ($uname,$udom,$fullname) = split(/:/);
1.44      ng        738: 	$ENV{'form.student'}        = $uname;
                    739: 	$ENV{'form.userdom'}        = $udom;
                    740: 	$ENV{'form.fullname'}       = $fullname;
1.41      ng        741: 	&submission($request,$ctr,$total);
                    742: 	$ctr++;
                    743:     }
                    744:     return '';
1.35      ng        745: }
1.34      ng        746: 
1.44      ng        747: #------------------------------------------------------------------------------------
                    748: #
                    749: #-------------------------- Next few routines handles grading by student, essentially
                    750: #                           handles essay response type problem/part
                    751: #
                    752: #--- Javascript to handle the submission page functionality ---
                    753: sub sub_page_js {
                    754:     my $request = shift;
                    755:     $request->print(<<SUBJAVASCRIPT);
                    756: <script type="text/javascript" language="javascript">
1.71      ng        757:     function updateRadio(formname,id,weight) {
1.125     ng        758: 	var gradeBox = formname["GD_BOX"+id];
                    759: 	var radioButton = formname["RADVAL"+id];
                    760: 	var oldpts = formname["oldpts"+id].value;
1.72      ng        761: 	var pts = checkSolved(formname,id) == 'update' ? gradeBox.value : oldpts;
1.71      ng        762: 	gradeBox.value = pts;
                    763: 	var resetbox = false;
                    764: 	if (isNaN(pts) || pts < 0) {
                    765: 	    alert("A number equal or greater than 0 is expected. Entered value = "+pts);
                    766: 	    for (var i=0; i<radioButton.length; i++) {
                    767: 		if (radioButton[i].checked) {
                    768: 		    gradeBox.value = i;
                    769: 		    resetbox = true;
                    770: 		}
                    771: 	    }
                    772: 	    if (!resetbox) {
                    773: 		formtextbox.value = "";
                    774: 	    }
                    775: 	    return;
1.44      ng        776: 	}
1.71      ng        777: 
                    778: 	if (pts > weight) {
                    779: 	    var resp = confirm("You entered a value ("+pts+
                    780: 			       ") greater than the weight for the part. Accept?");
                    781: 	    if (resp == false) {
1.125     ng        782: 		gradeBox.value = oldpts;
1.71      ng        783: 		return;
                    784: 	    }
1.44      ng        785: 	}
1.13      albertel  786: 
1.71      ng        787: 	for (var i=0; i<radioButton.length; i++) {
                    788: 	    radioButton[i].checked=false;
                    789: 	    if (pts == i && pts != "") {
                    790: 		radioButton[i].checked=true;
                    791: 	    }
                    792: 	}
                    793: 	updateSelect(formname,id);
1.125     ng        794: 	formname["stores"+id].value = "0";
1.41      ng        795:     }
1.5       albertel  796: 
1.72      ng        797:     function writeBox(formname,id,pts) {
1.125     ng        798: 	var gradeBox = formname["GD_BOX"+id];
1.71      ng        799: 	if (checkSolved(formname,id) == 'update') {
                    800: 	    gradeBox.value = pts;
                    801: 	} else {
1.125     ng        802: 	    var oldpts = formname["oldpts"+id].value;
1.72      ng        803: 	    gradeBox.value = oldpts;
1.125     ng        804: 	    var radioButton = formname["RADVAL"+id];
1.71      ng        805: 	    for (var i=0; i<radioButton.length; i++) {
                    806: 		radioButton[i].checked=false;
1.72      ng        807: 		if (i == oldpts) {
1.71      ng        808: 		    radioButton[i].checked=true;
                    809: 		}
                    810: 	    }
1.41      ng        811: 	}
1.125     ng        812: 	formname["stores"+id].value = "0";
1.71      ng        813: 	updateSelect(formname,id);
                    814: 	return;
1.41      ng        815:     }
1.44      ng        816: 
1.71      ng        817:     function clearRadBox(formname,id) {
                    818: 	if (checkSolved(formname,id) == 'noupdate') {
                    819: 	    updateSelect(formname,id);
                    820: 	    return;
                    821: 	}
1.125     ng        822: 	gradeSelect = formname["GD_SEL"+id];
1.71      ng        823: 	for (var i=0; i<gradeSelect.length; i++) {
                    824: 	    if (gradeSelect[i].selected) {
                    825: 		var selectx=i;
                    826: 	    }
                    827: 	}
1.125     ng        828: 	var stores = formname["stores"+id];
1.71      ng        829: 	if (selectx == stores.value) { return };
1.125     ng        830: 	var gradeBox = formname["GD_BOX"+id];
1.71      ng        831: 	gradeBox.value = "";
1.125     ng        832: 	var radioButton = formname["RADVAL"+id];
1.71      ng        833: 	for (var i=0; i<radioButton.length; i++) {
                    834: 	    radioButton[i].checked=false;
                    835: 	}
                    836: 	stores.value = selectx;
                    837:     }
1.5       albertel  838: 
1.71      ng        839:     function checkSolved(formname,id) {
1.125     ng        840: 	if (formname["solved"+id].value == "correct_by_student" && formname.overRideScore.value == 'no') {
1.118     ng        841: 	    var reply = confirm("This problem has been graded correct by the computer. Do you want to change the score?");
                    842: 	    if (!reply) {return "noupdate";}
1.120     ng        843: 	    formname.overRideScore.value = 'yes';
1.41      ng        844: 	}
1.71      ng        845: 	return "update";
1.13      albertel  846:     }
1.71      ng        847: 
                    848:     function updateSelect(formname,id) {
1.125     ng        849: 	formname["GD_SEL"+id][0].selected = true;
1.71      ng        850: 	return;
1.41      ng        851:     }
1.33      ng        852: 
1.121     ng        853: //=========== Check that a point is assigned for all the parts  ============
1.71      ng        854:     function checksubmit(formname,val,total,parttot) {
1.121     ng        855: 	formname.gradeOpt.value = val;
1.71      ng        856: 	if (val == "Save & Next") {
                    857: 	    for (i=0;i<=total;i++) {
                    858: 		for (j=0;j<parttot;j++) {
1.125     ng        859: 		    var partid = formname["partid"+i+"_"+j].value;
1.127     ng        860: 		    if (formname["GD_SEL"+i+"_"+partid][0].selected) {
1.125     ng        861: 			var points = formname["GD_BOX"+i+"_"+partid].value;
1.71      ng        862: 			if (points == "") {
1.125     ng        863: 			    var name = formname["name"+i].value;
1.129     ng        864: 			    var studentID = (name != '' ? name : formname["unamedom"+i].value);
                    865: 			    var resp = confirm("You did not assign a score for "+studentID+
                    866: 					       ", part "+partid+". Continue?");
1.71      ng        867: 			    if (resp == false) {
1.125     ng        868: 				formname["GD_BOX"+i+"_"+partid].focus();
1.71      ng        869: 				return false;
                    870: 			    }
                    871: 			}
                    872: 		    }
                    873: 		    
                    874: 		}
                    875: 	    }
                    876: 	    
                    877: 	}
1.121     ng        878: 	if (val == "Grade Student") {
                    879: 	    formname.showgrading.value = "yes";
                    880: 	    if (formname.Status.value == "") {
                    881: 		formname.Status.value = "Active";
                    882: 	    }
                    883: 	    formname.studentNo.value = total;
                    884: 	}
1.120     ng        885: 	formname.submit();
                    886:     }
                    887: 
1.71      ng        888: //======= Check that a score is assigned for all the problems (page/sequence grading only) =========
                    889:     function checkSubmitPage(formname,total) {
                    890: 	noscore = new Array(100);
                    891: 	var ptr = 0;
                    892: 	for (i=1;i<total;i++) {
1.125     ng        893: 	    var partid = formname["q_"+i].value;
1.127     ng        894: 	    if (formname["GD_SEL"+i+"_"+partid][0].selected) {
1.125     ng        895: 		var points = formname["GD_BOX"+i+"_"+partid].value;
                    896: 		var status = formname["solved"+i+"_"+partid].value;
1.71      ng        897: 		if (points == "" && status != "correct_by_student") {
                    898: 		    noscore[ptr] = i;
                    899: 		    ptr++;
                    900: 		}
                    901: 	    }
                    902: 	}
                    903: 	if (ptr != 0) {
                    904: 	    var sense = ptr == 1 ? ": " : "s: ";
                    905: 	    var prolist = "";
                    906: 	    if (ptr == 1) {
                    907: 		prolist = noscore[0];
                    908: 	    } else {
                    909: 		var i = 0;
                    910: 		while (i < ptr-1) {
                    911: 		    prolist += noscore[i]+", ";
                    912: 		    i++;
                    913: 		}
                    914: 		prolist += "and "+noscore[i];
                    915: 	    }
                    916: 	    var resp = confirm("You did not assign any score for the following problem"+sense+prolist+". Continue?");
                    917: 	    if (resp == false) {
                    918: 		return false;
                    919: 	    }
                    920: 	}
1.45      ng        921: 
1.71      ng        922: 	formname.submit();
                    923:     }
                    924: </script>
                    925: SUBJAVASCRIPT
                    926: }
1.45      ng        927: 
1.71      ng        928: #--- javascript for essay type problem --
                    929: sub sub_page_kw_js {
                    930:     my $request = shift;
1.80      ng        931:     my $iconpath = $request->dir_config('lonIconsURL');
1.118     ng        932:     &commonJSfunctions($request);
1.71      ng        933:     $request->print(<<SUBJAVASCRIPT);
                    934: <script type="text/javascript" language="javascript">
1.45      ng        935: 
1.44      ng        936: //===================== Show list of keywords ====================
1.122     ng        937:   function keywords(formname) {
                    938:     var nret = prompt("Keywords list, separated by a space. Add/delete to list if desired.",formname.keywords.value);
1.44      ng        939:     if (nret==null) return;
1.122     ng        940:     formname.keywords.value = nret;
1.44      ng        941: 
1.122     ng        942:     if (formname.keywords.value != "") {
1.128     ng        943: 	formname.refresh.value = "on";
1.122     ng        944: 	formname.submit();
1.44      ng        945:     }
                    946:     return;
                    947:   }
                    948: 
                    949: //===================== Script to view submitted by ==================
                    950:   function viewSubmitter(submitter) {
                    951:     document.SCORE.refresh.value = "on";
                    952:     document.SCORE.NCT.value = "1";
                    953:     document.SCORE.unamedom0.value = submitter;
                    954:     document.SCORE.submit();
                    955:     return;
                    956:   }
                    957: 
                    958: //===================== Script to add keyword(s) ==================
                    959:   function getSel() {
                    960:     if (document.getSelection) txt = document.getSelection();
                    961:     else if (document.selection) txt = document.selection.createRange().text;
                    962:     else return;
                    963:     var cleantxt = txt.replace(new RegExp('([\\f\\n\\r\\t\\v ])+', 'g')," ");
                    964:     if (cleantxt=="") {
1.46      ng        965: 	alert("Please select a word or group of words from document and then click this link.");
1.44      ng        966: 	return;
                    967:     }
                    968:     var nret = prompt("Add selection to keyword list? Edit if desired.",cleantxt);
                    969:     if (nret==null) return;
1.127     ng        970:     document.SCORE.keywords.value = document.SCORE.keywords.value+" "+nret;
1.44      ng        971:     if (document.SCORE.keywords.value != "") {
1.127     ng        972: 	document.SCORE.refresh.value = "on";
1.44      ng        973: 	document.SCORE.submit();
                    974:     }
                    975:     return;
                    976:   }
                    977: 
                    978: //====================== Script for composing message ==============
1.80      ng        979:    // preload images
                    980:    img1 = new Image();
                    981:    img1.src = "$iconpath/mailbkgrd.gif";
                    982:    img2 = new Image();
                    983:    img2.src = "$iconpath/mailto.gif";
                    984: 
1.44      ng        985:   function msgCenter(msgform,usrctr,fullname) {
                    986:     var Nmsg  = msgform.savemsgN.value;
                    987:     savedMsgHeader(Nmsg,usrctr,fullname);
                    988:     var subject = msgform.msgsub.value;
1.127     ng        989:     var msgchk = document.SCORE["includemsg"+usrctr].value;
1.44      ng        990:     re = /msgsub/;
                    991:     var shwsel = "";
                    992:     if (re.test(msgchk)) { shwsel = "checked" }
1.123     ng        993:     subject = (document.SCORE.shownSub.value == 0 ? checkEntities(subject) : subject);
                    994:     displaySubject(checkEntities(subject),shwsel);
1.44      ng        995:     for (var i=1; i<=Nmsg; i++) {
1.123     ng        996: 	var testmsg = "savemsg"+i+",";
                    997: 	re = new RegExp(testmsg,"g");
1.44      ng        998: 	shwsel = "";
                    999: 	if (re.test(msgchk)) { shwsel = "checked" }
1.125     ng       1000: 	var message = document.SCORE["savemsg"+i].value;
1.126     ng       1001: 	message = (document.SCORE["shownOnce"+i].value == 0 ? checkEntities(message) : message);
1.123     ng       1002: 	displaySavedMsg(i,message,shwsel); //I do not get it. w/o checkEntities on saved messages,
                   1003: 	                                   //any &lt; is already converted to <, etc. However, only once!!
1.44      ng       1004:     }
1.125     ng       1005:     newmsg = document.SCORE["newmsg"+usrctr].value;
1.44      ng       1006:     shwsel = "";
                   1007:     re = /newmsg/;
                   1008:     if (re.test(msgchk)) { shwsel = "checked" }
                   1009:     newMsg(newmsg,shwsel);
                   1010:     msgTail(); 
                   1011:     return;
                   1012:   }
                   1013: 
1.123     ng       1014:   function checkEntities(strx) {
                   1015:     if (strx.length == 0) return strx;
                   1016:     var orgStr = ["&", "<", ">", '"']; 
                   1017:     var newStr = ["&amp;", "&lt;", "&gt;", "&quot;"];
                   1018:     var counter = 0;
                   1019:     while (counter < 4) {
                   1020: 	strx = strReplace(strx,orgStr[counter],newStr[counter]);
                   1021: 	counter++;
                   1022:     }
                   1023:     return strx;
                   1024:   }
                   1025: 
                   1026:   function strReplace(strx, orgStr, newStr) {
                   1027:     return strx.split(orgStr).join(newStr);
                   1028:   }
                   1029: 
1.44      ng       1030:   function savedMsgHeader(Nmsg,usrctr,fullname) {
1.76      ng       1031:     var height = 70*Nmsg+250;
1.44      ng       1032:     var scrollbar = "no";
                   1033:     if (height > 600) {
                   1034: 	height = 600;
                   1035: 	scrollbar = "yes";
                   1036:     }
1.118     ng       1037:     var xpos = (screen.width-600)/2;
                   1038:     xpos = (xpos < 0) ? '0' : xpos;
                   1039:     var ypos = (screen.height-height)/2-30;
                   1040:     ypos = (ypos < 0) ? '0' : ypos;
                   1041: 
                   1042:     pWin = window.open('', 'MessageCenter', 'toolbar=no,location=no,scrollbars='+scrollbar+',screenx='+xpos+',screeny='+ypos+',width=600,height='+height);
1.76      ng       1043:     pWin.focus();
                   1044:     pDoc = pWin.document;
1.128     ng       1045:     pDoc.open('text/html','replace');
1.76      ng       1046:     pDoc.write("<html><head>");
                   1047:     pDoc.write("<title>Message Central</title>");
                   1048: 
                   1049:     pDoc.write("<script language=javascript>");
                   1050:     pDoc.write("function checkInput() {");
1.123     ng       1051:     pDoc.write("  opener.document.SCORE.msgsub.value = opener.checkEntities(document.msgcenter.msgsub.value);");
1.76      ng       1052:     pDoc.write("  var nmsg   = opener.document.SCORE.savemsgN.value;");
                   1053:     pDoc.write("  var usrctr = document.msgcenter.usrctr.value;");
1.125     ng       1054:     pDoc.write("  var newval = opener.document.SCORE[\\"newmsg\\"+usrctr];");
1.123     ng       1055:     pDoc.write("  newval.value = opener.checkEntities(document.msgcenter.newmsg.value);");
1.76      ng       1056: 
                   1057:     pDoc.write("  var msgchk = \\"\\";");
                   1058:     pDoc.write("  if (document.msgcenter.subchk.checked) {");
                   1059:     pDoc.write("     msgchk = \\"msgsub,\\";");
                   1060:     pDoc.write("  }");
1.80      ng       1061:     pDoc.write("  var includemsg = 0;");
                   1062:     pDoc.write("  for (var i=1; i<=nmsg; i++) {");
1.125     ng       1063:     pDoc.write("      var opnmsg = opener.document.SCORE[\\"savemsg\\"+i];");
                   1064:     pDoc.write("      var frmmsg = document.msgcenter[\\"msg\\"+i];");
1.123     ng       1065:     pDoc.write("      opnmsg.value = opener.checkEntities(frmmsg.value);");
1.125     ng       1066:     pDoc.write("      var showflg = opener.document.SCORE[\\"shownOnce\\"+i];");
1.123     ng       1067:     pDoc.write("      showflg.value = \\"1\\";");
1.125     ng       1068:     pDoc.write("      var chkbox = document.msgcenter[\\"msgn\\"+i];");
1.76      ng       1069:     pDoc.write("      if (chkbox.checked) {");
                   1070:     pDoc.write("         msgchk += \\"savemsg\\"+i+\\",\\";");
1.80      ng       1071:     pDoc.write("         includemsg = 1;");
1.76      ng       1072:     pDoc.write("      }");
                   1073:     pDoc.write("  }");
                   1074:     pDoc.write("  if (document.msgcenter.newmsgchk.checked) {");
                   1075:     pDoc.write("     msgchk += \\"newmsg\\"+usrctr;");
1.80      ng       1076:     pDoc.write("     includemsg = 1;");
                   1077:     pDoc.write("  }");
1.125     ng       1078:     pDoc.write("  imgformname = opener.document.SCORE[\\"mailicon\\"+usrctr];");
1.84      ng       1079:     pDoc.write("  imgformname.src = \\"$iconpath/\\"+((includemsg) ? \\"mailto.gif\\" : \\"mailbkgrd.gif\\");");
1.125     ng       1080:     pDoc.write("  var includemsg = opener.document.SCORE[\\"includemsg\\"+usrctr];");
1.76      ng       1081:     pDoc.write("  includemsg.value = msgchk;");
                   1082: 
                   1083:     pDoc.write("  self.close()");
                   1084: 
                   1085:     pDoc.write("}");
                   1086: 
                   1087:     pDoc.write("<");
                   1088:     pDoc.write("/script>");
                   1089: 
                   1090:     pDoc.write("</head><body bgcolor=white>");
                   1091: 
                   1092:     pDoc.write("<form action=\\"inactive\\" name=\\"msgcenter\\">");
                   1093:     pDoc.write("<input value=\\""+usrctr+"\\" name=\\"usrctr\\" type=\\"hidden\\">");
                   1094:     pDoc.write("<font color=\\"green\\" size=+1>&nbsp;Compose Message for \"+fullname+\"</font><br><br>");
                   1095: 
                   1096:     pDoc.write("<table border=0 width=100%><tr><td bgcolor=\\"#777777\\">");
                   1097:     pDoc.write("<table border=0 width=100%><tr bgcolor=\\"#ddffff\\">");
                   1098:     pDoc.write("<td><b>Type</b></td><td><b>Include</b></td><td><b>Message</td></tr>");
1.44      ng       1099: }
                   1100:     function displaySubject(msg,shwsel) {
1.76      ng       1101:     pDoc = pWin.document;
                   1102:     pDoc.write("<tr bgcolor=\\"#ffffdd\\">");
                   1103:     pDoc.write("<td>Subject</td>");
                   1104:     pDoc.write("<td align=\\"center\\"><input name=\\"subchk\\" type=\\"checkbox\\"" +shwsel+"></td>");
                   1105:     pDoc.write("<td><input name=\\"msgsub\\" type=\\"text\\" value=\\""+msg+"\\"size=\\"60\\" maxlength=\\"80\\"></td></tr>");
1.44      ng       1106: }
                   1107: 
1.72      ng       1108:   function displaySavedMsg(ctr,msg,shwsel) {
1.76      ng       1109:     pDoc = pWin.document;
                   1110:     pDoc.write("<tr bgcolor=\\"#ffffdd\\">");
                   1111:     pDoc.write("<td align=\\"center\\">"+ctr+"</td>");
                   1112:     pDoc.write("<td align=\\"center\\"><input name=\\"msgn"+ctr+"\\" type=\\"checkbox\\"" +shwsel+"></td>");
                   1113:     pDoc.write("<td><textarea name=\\"msg"+ctr+"\\" cols=\\"60\\" rows=\\"3\\">"+msg+"</textarea></td></tr>");
1.44      ng       1114: }
                   1115: 
                   1116:   function newMsg(newmsg,shwsel) {
1.76      ng       1117:     pDoc = pWin.document;
                   1118:     pDoc.write("<tr bgcolor=\\"#ffffdd\\">");
                   1119:     pDoc.write("<td align=\\"center\\">New</td>");
                   1120:     pDoc.write("<td align=\\"center\\"><input name=\\"newmsgchk\\" type=\\"checkbox\\"" +shwsel+"></td>");
                   1121:     pDoc.write("<td><textarea name=\\"newmsg\\" cols=\\"60\\" rows=\\"3\\" onchange=\\"javascript:this.form.newmsgchk.checked=true\\" >"+newmsg+"</textarea></td></tr>");
1.44      ng       1122: }
                   1123: 
                   1124:   function msgTail() {
1.76      ng       1125:     pDoc = pWin.document;
                   1126:     pDoc.write("</table>");
                   1127:     pDoc.write("</td></tr></table>&nbsp;");
                   1128:     pDoc.write("<input type=\\"button\\" value=\\"Save\\" onClick=\\"javascript:checkInput()\\">&nbsp;&nbsp;");
                   1129:     pDoc.write("<input type=\\"button\\" value=\\"Cancel\\" onClick=\\"self.close()\\"><br><br>");
                   1130:     pDoc.write("</form>");
                   1131:     pDoc.write("</body></html>");
1.128     ng       1132:     pDoc.close();
1.44      ng       1133: }
                   1134: 
                   1135: //====================== Script for keyword highlight options ==============
                   1136:   function kwhighlight() {
                   1137:     var kwclr    = document.SCORE.kwclr.value;
                   1138:     var kwsize   = document.SCORE.kwsize.value;
                   1139:     var kwstyle  = document.SCORE.kwstyle.value;
                   1140:     var redsel = "";
                   1141:     var grnsel = "";
                   1142:     var blusel = "";
                   1143:     if (kwclr=="red")   {var redsel="checked"};
                   1144:     if (kwclr=="green") {var grnsel="checked"};
                   1145:     if (kwclr=="blue")  {var blusel="checked"};
                   1146:     var sznsel = "";
                   1147:     var sz1sel = "";
                   1148:     var sz2sel = "";
                   1149:     if (kwsize=="0")  {var sznsel="checked"};
                   1150:     if (kwsize=="+1") {var sz1sel="checked"};
                   1151:     if (kwsize=="+2") {var sz2sel="checked"};
                   1152:     var synsel = "";
                   1153:     var syisel = "";
                   1154:     var sybsel = "";
                   1155:     if (kwstyle=="")    {var synsel="checked"};
                   1156:     if (kwstyle=="<i>") {var syisel="checked"};
                   1157:     if (kwstyle=="<b>") {var sybsel="checked"};
                   1158:     highlightCentral();
                   1159:     highlightbody('red','red',redsel,'0','normal',sznsel,'','normal',synsel);
                   1160:     highlightbody('green','green',grnsel,'+1','+1',sz1sel,'<i>','italic',syisel);
                   1161:     highlightbody('blue','blue',blusel,'+2','+2',sz2sel,'<b>','bold',sybsel);
                   1162:     highlightend();
                   1163:     return;
                   1164:   }
                   1165: 
                   1166:   function highlightCentral() {
1.76      ng       1167: //    if (window.hwdWin) window.hwdWin.close();
1.118     ng       1168:     var xpos = (screen.width-400)/2;
                   1169:     xpos = (xpos < 0) ? '0' : xpos;
                   1170:     var ypos = (screen.height-330)/2-30;
                   1171:     ypos = (ypos < 0) ? '0' : ypos;
                   1172: 
                   1173:     hwdWin = window.open('', 'KeywordHighlightCentral', 'toolbar=no,location=no,scrollbars=no,width=400,height=300,screenx='+xpos+',screeny='+ypos);
1.76      ng       1174:     hwdWin.focus();
                   1175:     var hDoc = hwdWin.document;
1.128     ng       1176:     hDoc.open('text/html','replace');
1.76      ng       1177:     hDoc.write("<html><head>");
                   1178:     hDoc.write("<title>Highlight Central</title>");
                   1179: 
                   1180:     hDoc.write("<script language=javascript>");
                   1181:     hDoc.write("function updateChoice(flag) {");
1.118     ng       1182:     hDoc.write("  opener.document.SCORE.kwclr.value = opener.radioSelection(document.hlCenter.kwdclr);");
                   1183:     hDoc.write("  opener.document.SCORE.kwsize.value = opener.radioSelection(document.hlCenter.kwdsize);");
                   1184:     hDoc.write("  opener.document.SCORE.kwstyle.value = opener.radioSelection(document.hlCenter.kwdstyle);");
1.76      ng       1185:     hDoc.write("  opener.document.SCORE.refresh.value = \\"on\\";");
                   1186:     hDoc.write("  if (opener.document.SCORE.keywords.value!=\\"\\"){");
                   1187:     hDoc.write("     opener.document.SCORE.submit();");
                   1188:     hDoc.write("  }");
                   1189:     hDoc.write("  self.close()");
                   1190:     hDoc.write("}");
                   1191: 
                   1192:     hDoc.write("<");
                   1193:     hDoc.write("/script>");
                   1194: 
                   1195:     hDoc.write("</head><body bgcolor=white>");
                   1196: 
                   1197:     hDoc.write("<form action=\\"inactive\\" name=\\"hlCenter\\">");
                   1198:     hDoc.write("<font color=\\"green\\" size=+1>&nbsp;Keyword Highlight Options</font><br><br>");
                   1199: 
                   1200:     hDoc.write("<table border=0 width=100%><tr><td bgcolor=\\"#777777\\">");
                   1201:     hDoc.write("<table border=0 width=100%><tr bgcolor=\\"#ddffff\\">");
                   1202:     hDoc.write("<td><b>Text Color</b></td><td><b>Font Size</b></td><td><b>Font Style</td></tr>");
1.44      ng       1203:   }
                   1204: 
                   1205:   function highlightbody(clrval,clrtxt,clrsel,szval,sztxt,szsel,syval,sytxt,sysel) { 
1.76      ng       1206:     var hDoc = hwdWin.document;
                   1207:     hDoc.write("<tr bgcolor=\\"#ffffdd\\">");
                   1208:     hDoc.write("<td align=\\"left\\">");
                   1209:     hDoc.write("<input name=\\"kwdclr\\" type=\\"radio\\" value=\\""+clrval+"\\" "+clrsel+">&nbsp;"+clrtxt+"</td>");
                   1210:     hDoc.write("<td align=\\"left\\">");
                   1211:     hDoc.write("<input name=\\"kwdsize\\" type=\\"radio\\" value=\\""+szval+"\\" "+szsel+">&nbsp;"+sztxt+"</td>");
                   1212:     hDoc.write("<td align=\\"left\\">");
                   1213:     hDoc.write("<input name=\\"kwdstyle\\" type=\\"radio\\" value=\\""+syval+"\\" "+sysel+">&nbsp;"+sytxt+"</td>");
                   1214:     hDoc.write("</tr>");
1.44      ng       1215:   }
                   1216: 
                   1217:   function highlightend() { 
1.76      ng       1218:     var hDoc = hwdWin.document;
                   1219:     hDoc.write("</table>");
                   1220:     hDoc.write("</td></tr></table>&nbsp;");
                   1221:     hDoc.write("<input type=\\"button\\" value=\\"Save\\" onClick=\\"javascript:updateChoice(1)\\">&nbsp;&nbsp;");
                   1222:     hDoc.write("<input type=\\"button\\" value=\\"Cancel\\" onClick=\\"self.close()\\"><br><br>");
                   1223:     hDoc.write("</form>");
                   1224:     hDoc.write("</body></html>");
1.128     ng       1225:     hDoc.close();
1.44      ng       1226:   }
                   1227: 
                   1228: </script>
                   1229: SUBJAVASCRIPT
                   1230: }
                   1231: 
1.71      ng       1232: #--- displays the grading box, used in essay type problem and grading by page/sequence
                   1233: sub gradeBox {
                   1234:     my ($request,$symb,$uname,$udom,$counter,$partid,$record) = @_;
                   1235: 
                   1236:     my $checkIcon = '<img src="'.$request->dir_config('lonIconsURL').
                   1237: 	'/check.gif" height="16" border="0" />';
                   1238: 
                   1239:     my $wgt    = &Apache::lonnet::EXT('resource.'.$partid.'.weight',$symb,$udom,$uname);
                   1240:     my $wgtmsg = ($wgt > 0 ? '(problem weight)' : 
                   1241: 		  '<font color="red">problem weight assigned by computer</font>');
                   1242:     $wgt       = ($wgt > 0 ? $wgt : '1');
                   1243:     my $score  = ($$record{'resource.'.$partid.'.awarded'} eq '' ?
                   1244: 		  '' : $$record{'resource.'.$partid.'.awarded'}*$wgt);
                   1245:     my $result='<input type="hidden" name="WGT'.$counter.'_'.$partid.'" value="'.$wgt.'" />'."\n";
                   1246: 
                   1247:     $result.='<table border="0"><tr><td>'.
                   1248: 	'<b>Part </b>'.$partid.' <b>Points: </b></td><td>'."\n";
                   1249: 
                   1250:     my $ctr = 0;
                   1251:     $result.='<table border="0"><tr>'."\n";  # display radio buttons in a nice table 10 across
                   1252:     while ($ctr<=$wgt) {
                   1253: 	$result.= '<td><input type="radio" name="RADVAL'.$counter.'_'.$partid.'" '.
                   1254: 	    'onclick="javascript:writeBox(this.form,\''.$counter.'_'.$partid.'\','.
1.72      ng       1255: 	    $ctr.')" value="'.$ctr.'" '.
1.71      ng       1256: 	    ($score eq $ctr ? 'checked':'').' /> '.$ctr."</td>\n";
                   1257: 	$result.=(($ctr+1)%10 == 0 ? '</tr><tr>' : '');
                   1258: 	$ctr++;
                   1259:     }
                   1260:     $result.='</tr></table>';
                   1261: 
                   1262:     $result.='</td><td>&nbsp;<b>or</b>&nbsp;</td>'."\n";
                   1263:     $result.='<td><input type="text" name="GD_BOX'.$counter.'_'.$partid.'"'.
                   1264: 	($score ne ''? ' value = "'.$score.'"':'').' size="4" '.
                   1265: 	'onChange="javascript:updateRadio(this.form,\''.$counter.'_'.$partid.'\','.
                   1266: 	$wgt.')" /></td>'."\n";
                   1267:     $result.='<td>/'.$wgt.' '.$wgtmsg.
                   1268: 	($$record{'resource.'.$partid.'.solved'} eq 'correct_by_student' ? '&nbsp;'.$checkIcon : '').
                   1269: 	' </td><td>'."\n";
                   1270: 
                   1271:     $result.='<select name="GD_SEL'.$counter.'_'.$partid.'" '.
                   1272: 	'onChange="javascript:clearRadBox(this.form,\''.$counter.'_'.$partid.'\')" >'."\n";
                   1273:     if ($$record{'resource.'.$partid.'.solved'} eq 'excused') {
                   1274: 	$result.='<option> </option>'.
1.125     ng       1275: 	    '<option selected="on">excused</option>';
1.71      ng       1276:     } else {
                   1277: 	$result.='<option selected="on"> </option>'.
1.125     ng       1278: 	    '<option>excused</option>';
1.71      ng       1279:     }
1.125     ng       1280:     $result.='<option>reset status</option></select>'."\n";
1.71      ng       1281:     $result.="&nbsp&nbsp\n";
                   1282:     $result.='<input type="hidden" name="stores'.$counter.'_'.$partid.'" value="" />'."\n".
                   1283: 	'<input type="hidden" name="oldpts'.$counter.'_'.$partid.'" value="'.$score.'" />'."\n".
                   1284: 	'<input type="hidden" name="solved'.$counter.'_'.$partid.'" value="'.
                   1285: 	$$record{'resource.'.$partid.'.solved'}.'" />'."\n";
                   1286:     $result.='</td></tr></table>'."\n";
                   1287:     return $result;
                   1288: }
1.44      ng       1289: 
1.58      albertel 1290: sub show_problem {
1.144     albertel 1291:     my ($request,$symb,$uname,$udom,$removeform,$viewon,$mode) = @_;
                   1292:     my $rendered;
                   1293:     if ($mode eq 'both' or $mode eq 'text') {
                   1294: 	$rendered=&Apache::loncommon::get_student_view($symb,$uname,$udom,
                   1295: 					     $ENV{'request.course.id'});
                   1296:     }
1.58      albertel 1297:     if ($removeform) {
                   1298: 	$rendered=~s|<form(.*?)>||g;
                   1299: 	$rendered=~s|</form>||g;
                   1300: 	$rendered=~s|name="submit"|name="would_have_been_submit"|g;
                   1301:     }
1.144     albertel 1302:     my $companswer;
                   1303:     if ($mode eq 'both' or $mode eq 'answer') {
                   1304: 	$companswer=&Apache::loncommon::get_student_answers($symb,$uname,$udom,
                   1305: 						    $ENV{'request.course.id'});
                   1306:     }
1.58      albertel 1307:     if ($removeform) {
                   1308: 	$companswer=~s|<form(.*?)>||g;
                   1309: 	$companswer=~s|</form>||g;
1.144     albertel 1310: 	$companswer=~s|name="submit"|name="would_have_been_submit"|g;
1.58      albertel 1311:     }
                   1312:     my $result.='<table border="0" width="100%"><tr><td bgcolor="#777777">';
1.71      ng       1313:     $result.='<table border="0" width="100%">';
1.144     albertel 1314:     if ($viewon) {
                   1315: 	$result.='<tr><td bgcolor="#e6ffff"><b> ';
                   1316: 	if ($mode eq 'both' or $mode eq 'text') {
                   1317: 	    $result.='View of the problem - ';
                   1318: 	} else {
                   1319: 	    $result.='Correct answer: ';
                   1320: 	}
                   1321: 	$result.=$ENV{'form.fullname'}.'</b></td></tr>';
                   1322:     }
                   1323:     if ($mode eq 'both') {
                   1324: 	$result.='<tr><td bgcolor="#ffffff">'.$rendered.'<br />';
                   1325: 	$result.='<b>Correct answer:</b><br />'.$companswer;
                   1326:     } elsif ($mode eq 'text') {
                   1327: 	$result.='<tr><td bgcolor="#ffffff">'.$rendered;
                   1328:     } elsif ($mode eq 'answer') {
                   1329: 	$result.='<tr><td bgcolor="#ffffff">'.$companswer;
                   1330:     }
1.58      albertel 1331:     $result.='</td></tr></table>';
                   1332:     $result.='</td></tr></table><br />';
1.71      ng       1333:     return $result;
1.58      albertel 1334: }
                   1335: 
1.44      ng       1336: # --------------------------- show submissions of a student, option to grade 
                   1337: sub submission {
                   1338:     my ($request,$counter,$total) = @_;
                   1339: 
                   1340:     (my $url=$ENV{'form.url'})=~s-^http://($ENV{'SERVER_NAME'}|$ENV{'HTTP_HOST'})--;
                   1341:     my ($uname,$udom)     = ($ENV{'form.student'},$ENV{'form.userdom'});
1.120     ng       1342:     $udom = ($udom eq '' ? $ENV{'user.domain'} : $udom); #has form.userdom changed for a student?
1.104     albertel 1343:     my $usec = &Apache::lonnet::getsection($udom,$uname,$ENV{'request.course.id'});
1.44      ng       1344:     $ENV{'form.fullname'} = &get_fullname ($uname,$udom) if $ENV{'form.fullname'} eq '';
1.41      ng       1345: 
                   1346:     my $symb=($ENV{'form.symb'} ne '' ? $ENV{'form.symb'} : (&Apache::lonnet::symbread($url)));
                   1347:     if ($symb eq '') { $request->print("Unable to handle ambiguous references:$url:."); return ''; }
1.104     albertel 1348: 
                   1349:     if (!&canview($usec)) {
1.116     ng       1350: 	$request->print('<font color="red">Unable to view requested student.('.
                   1351: 			$uname.$udom.$usec.$ENV{'request.course.id'}.')</font>');
1.104     albertel 1352: 	$request->print(&show_grading_menu_form($symb,$url));
                   1353: 	return;
                   1354:     }
                   1355: 
1.122     ng       1356:     $ENV{'form.lastSub'} = ($ENV{'form.lastSub'} eq '' ? 'datesub' : $ENV{'form.lastSub'});
1.41      ng       1357:     my $last = ($ENV{'form.lastSub'} eq 'last' ? 'last' : '');
1.122     ng       1358:     my $checkIcon = '<img src="'.$request->dir_config('lonIconsURL').
                   1359: 	'/check.gif" height="16" border="0" />';
1.41      ng       1360: 
                   1361:     # header info
                   1362:     if ($counter == 0) {
                   1363: 	&sub_page_js($request);
1.118     ng       1364: 	&sub_page_kw_js($request) if ($ENV{'form.handgrade'} eq 'yes');
1.76      ng       1365: 	$ENV{'form.probTitle'} = $ENV{'form.probTitle'} eq '' ? 
                   1366: 	    &Apache::lonnet::gettitle($symb) : $ENV{'form.probTitle'};
                   1367: 
1.45      ng       1368: 	$request->print('<h3>&nbsp;<font color="#339933">Submission Record</font></h3>'."\n".
1.118     ng       1369: 			'<font size=+1>&nbsp;<b>Resource: </b>'.$ENV{'form.probTitle'}.'</font>'."\n");
                   1370: 
                   1371: 	if ($ENV{'form.handgrade'} eq 'no') {
                   1372: 	    my $checkMark='<br /><br />&nbsp;<b>Note:</b> Part(s) graded correct by the computer is marked with a '.
                   1373: 		$checkIcon.' symbol.'."\n";
                   1374: 	    $request->print($checkMark);
                   1375: 	}
1.41      ng       1376: 
1.44      ng       1377: 	# option to display problem, only once else it cause problems 
                   1378:         # with the form later since the problem has a form.
1.144     albertel 1379: 	if ($ENV{'form.vProb'} eq 'yes' or $ENV{'form.vAns'} eq 'yes') {
                   1380: 	    my $mode;
                   1381: 	    if ($ENV{'form.vProb'} eq 'yes' && $ENV{'form.vAns'} eq 'yes') {
                   1382: 		$mode='both';
                   1383: 	    } elsif ($ENV{'form.vProb'} eq 'yes') {
                   1384: 		$mode='text';
                   1385: 	    } elsif ($ENV{'form.vAns'} eq 'yes') {
                   1386: 		$mode='answer';
                   1387: 	    }
                   1388: 	    $request->print(&show_problem($request,$symb,$uname,$udom,0,1,$mode));
1.41      ng       1389: 	}
                   1390: 	
1.44      ng       1391: 	# kwclr is the only variable that is guaranteed to be non blank 
                   1392:         # if this subroutine has been called once.
1.41      ng       1393: 	my %keyhash = ();
1.118     ng       1394: 	if ($ENV{'form.kwclr'} eq '' && $ENV{'form.handgrade'} eq 'yes') {
1.41      ng       1395: 	    %keyhash = &Apache::lonnet::dump('nohist_handgrade',
                   1396: 					     $ENV{'course.'.$ENV{'request.course.id'}.'.domain'},
                   1397: 					     $ENV{'course.'.$ENV{'request.course.id'}.'.num'});
                   1398: 
                   1399: 	    my $loginuser = $ENV{'user.name'}.':'.$ENV{'user.domain'};
                   1400: 	    $ENV{'form.keywords'} = $keyhash{$symb.'_keywords'} ne '' ? $keyhash{$symb.'_keywords'} : '';
                   1401: 	    $ENV{'form.kwclr'}    = $keyhash{$loginuser.'_kwclr'} ne '' ? $keyhash{$loginuser.'_kwclr'} : 'red';
                   1402: 	    $ENV{'form.kwsize'}   = $keyhash{$loginuser.'_kwsize'} ne '' ? $keyhash{$loginuser.'_kwsize'} : '0';
                   1403: 	    $ENV{'form.kwstyle'}  = $keyhash{$loginuser.'_kwstyle'} ne '' ? $keyhash{$loginuser.'_kwstyle'} : '';
                   1404: 	    $ENV{'form.msgsub'}   = $keyhash{$symb.'_subject'} ne '' ? 
1.72      ng       1405: 		$keyhash{$symb.'_subject'} : $ENV{'form.probTitle'};
1.41      ng       1406: 	    $ENV{'form.savemsgN'} = $keyhash{$symb.'_savemsgN'} ne '' ? $keyhash{$symb.'_savemsgN'} : '0';
                   1407: 	}
1.120     ng       1408: 	my $overRideScore = $ENV{'form.overRideScore'} eq '' ? 'no' : $ENV{'form.overRideScore'};
1.44      ng       1409: 
1.41      ng       1410: 	$request->print('<form action="/adm/grades" method="post" name="SCORE">'."\n".
                   1411: 			'<input type="hidden" name="command"    value="handgrade" />'."\n".
1.80      ng       1412: 			'<input type="hidden" name="saveState"  value="'.$ENV{'form.saveState'}.'" />'."\n".
1.119     ng       1413: 			'<input type="hidden" name="Status"     value="'.$ENV{'form.Status'}.'" />'."\n".
1.120     ng       1414: 			'<input type="hidden" name="overRideScore" value="'.$overRideScore.'" />'."\n".
1.72      ng       1415: 			'<input type="hidden" name="probTitle"  value="'.$ENV{'form.probTitle'}.'" />'."\n".
1.41      ng       1416: 			'<input type="hidden" name="refresh"    value="off" />'."\n".
1.120     ng       1417: 			'<input type="hidden" name="studentNo"  value="" />'."\n".
                   1418: 			'<input type="hidden" name="gradeOpt"   value="" />'."\n".
1.41      ng       1419: 			'<input type="hidden" name="symb"       value="'.$symb.'" />'."\n".
                   1420: 			'<input type="hidden" name="url"        value="'.$url.'" />'."\n".
                   1421: 			'<input type="hidden" name="showgrading" value="'.$ENV{'form.showgrading'}.'" />'."\n".
                   1422: 			'<input type="hidden" name="vProb"      value="'.$ENV{'form.vProb'}.'" />'."\n".
1.144     albertel 1423: 			'<input type="hidden" name="vAns"       value="'.$ENV{'form.vAns'}.'" />'."\n".
1.41      ng       1424: 			'<input type="hidden" name="lastSub"    value="'.$ENV{'form.lastSub'}.'" />'."\n".
                   1425: 			'<input type="hidden" name="section"    value="'.$ENV{'form.section'}.'">'."\n".
                   1426: 			'<input type="hidden" name="submitonly" value="'.$ENV{'form.submitonly'}.'">'."\n".
                   1427: 			'<input type="hidden" name="handgrade"  value="'.$ENV{'form.handgrade'}.'">'."\n".
                   1428: 			'<input type="hidden" name="NCT"'.
                   1429: 			' value="'.($ENV{'form.NTSTU'} ne '' ? $ENV{'form.NTSTU'} : $total+1).'" />'."\n");
1.123     ng       1430: 	if ($ENV{'form.handgrade'} eq 'yes') {
                   1431: 	    $request->print('<input type="hidden" name="keywords" value="'.$ENV{'form.keywords'}.'" />'."\n".
                   1432: 			    '<input type="hidden" name="kwclr"    value="'.$ENV{'form.kwclr'}.'" />'."\n".
                   1433: 			    '<input type="hidden" name="kwsize"   value="'.$ENV{'form.kwsize'}.'" />'."\n".
                   1434: 			    '<input type="hidden" name="kwstyle"  value="'.$ENV{'form.kwstyle'}.'" />'."\n".
                   1435: 			    '<input type="hidden" name="msgsub"   value="'.$ENV{'form.msgsub'}.'" />'."\n".
                   1436: 			    '<input type="hidden" name="shownSub" value="0" />'."\n".
                   1437: 			    '<input type="hidden" name="savemsgN" value="'.$ENV{'form.savemsgN'}.'" />'."\n");
                   1438: 	}
1.41      ng       1439: 	
                   1440: 	my ($cts,$prnmsg) = (1,'');
                   1441: 	while ($cts <= $ENV{'form.savemsgN'}) {
                   1442: 	    $prnmsg.='<input type="hidden" name="savemsg'.$cts.'" value="'.
1.123     ng       1443: 		(!exists($keyhash{$symb.'_savemsg'.$cts}) ? 
1.80      ng       1444: 		 &Apache::lonfeedback::clear_out_html($ENV{'form.savemsg'.$cts}) :
                   1445: 		 &Apache::lonfeedback::clear_out_html($keyhash{$symb.'_savemsg'.$cts})).
1.123     ng       1446: 		'" />'."\n".
                   1447: 		'<input type="hidden" name="shownOnce'.$cts.'" value="0" />'."\n";
1.41      ng       1448: 	    $cts++;
                   1449: 	}
                   1450: 	$request->print($prnmsg);
1.32      ng       1451: 
1.41      ng       1452: 	if ($ENV{'form.handgrade'} eq 'yes' && $ENV{'form.showgrading'} eq 'yes') {
1.88      www      1453: #
                   1454: # Print out the keyword options line
                   1455: #
1.41      ng       1456: 	    $request->print(<<KEYWORDS);
1.38      ng       1457: &nbsp;<b>Keyword Options:</b>&nbsp;
1.122     ng       1458: <a href="javascript:keywords(document.SCORE)"; TARGET=_self>List</a>&nbsp; &nbsp;
1.38      ng       1459: <a href="#" onMouseDown="javascript:getSel(); return false"
                   1460:  CLASS="page">Paste Selection to List</a>&nbsp; &nbsp;
                   1461: <a href="javascript:kwhighlight()"; TARGET=_self>Highlight Attribute</a><br /><br />
                   1462: KEYWORDS
1.88      www      1463: #
                   1464: # Load the other essays for similarity check
                   1465: #
                   1466:             my $essayurl=&Apache::lonnet::declutter($url);
                   1467: 	    my ($adom,$aname,$apath)=($essayurl=~/^(\w+)\/(\w+)\/(.*)$/);
                   1468: 	    $apath=&Apache::lonnet::escape($apath);
                   1469: 	    $apath=~s/\W/\_/gs;
                   1470: 	    %oldessays=&Apache::lonnet::dump('nohist_essay_'.$apath,$adom,$aname);
1.41      ng       1471:         }
                   1472:     }
1.44      ng       1473: 
1.144     albertel 1474:     if ($ENV{'form.vProb'} eq 'all' or $ENV{'form.vAns'} eq 'all') {
1.71      ng       1475: 	$request->print('<br /><br /><br />') if ($counter > 0);
1.144     albertel 1476: 	my $mode;
                   1477: 	if ($ENV{'form.vProb'} eq 'all' && $ENV{'form.vAns'} eq 'all') {
                   1478: 	    $mode='both';
                   1479: 	} elsif ($ENV{'form.vProb'} eq 'all' ) {
                   1480: 	    $mode='text';
                   1481: 	} elsif ($ENV{'form.vAns'} eq 'all') {
                   1482: 	    $mode='answer';
                   1483: 	}
                   1484: 	$request->print(&show_problem($request,$symb,$uname,$udom,1,1,$mode));
1.58      albertel 1485:     }
1.144     albertel 1486: 
1.41      ng       1487:     my %record = &Apache::lonnet::restore($symb,$ENV{'request.course.id'},$udom,$uname);
1.125     ng       1488: 
1.147     albertel 1489:     my ($partlist,$handgrade,$responseType) = &response_type($url,$symb);
1.41      ng       1490: 
1.44      ng       1491:     # Display student info
1.41      ng       1492:     $request->print(($counter == 0 ? '' : '<br />'));
1.45      ng       1493:     my $result='<table border="0" width=100%><tr><td bgcolor="#777777">'."\n".
                   1494: 	'<table border="0" width=100%><tr bgcolor="#edffff"><td>'."\n";
1.44      ng       1495: 
1.129     ng       1496:     $result.='<b>Fullname: </b>'.&nameUserString(undef,$ENV{'form.fullname'},$uname,$udom).'<br />'."\n";
1.45      ng       1497:     $result.='<input type="hidden" name="name'.$counter.
                   1498: 	'" value="'.$ENV{'form.fullname'}.'" />'."\n";
1.41      ng       1499: 
1.118     ng       1500:     # If any part of the problem is an essay-response (handgraded), then check for collaborators
1.45      ng       1501:     my @col_fullnames;
1.56      matthew  1502:     my ($classlist,$fullname);
1.41      ng       1503:     if ($ENV{'form.handgrade'} eq 'yes') {
1.80      ng       1504: 	($classlist,undef,$fullname) = &getclasslist('all','0');
1.41      ng       1505: 	for (keys (%$handgrade)) {
1.44      ng       1506: 	    my $ncol = &Apache::lonnet::EXT('resource.'.$_.
1.57      matthew  1507: 					    '.maxcollaborators',
                   1508:                                             $symb,$udom,$uname);
                   1509: 	    next if ($ncol <= 0);
                   1510:             s/\_/\./g;
                   1511:             next if ($record{'resource.'.$_.'.collaborators'} eq '');
1.86      ng       1512:             my @goodcollaborators = ();
                   1513:             my @badcollaborators  = ();
                   1514: 	    foreach (split(/,?\s+/,$record{'resource.'.$_.'.collaborators'})) { 
                   1515: 		$_ =~ s/[\$\^\(\)]//g;
                   1516: 		next if ($_ eq '');
1.80      ng       1517: 		my ($co_name,$co_dom) = split /\@|:/,$_;
1.86      ng       1518: 		$co_dom = $udom if (! defined($co_dom) || $co_dom =~ /^domain$/i);
1.80      ng       1519: 		next if ($co_name eq $uname && $co_dom eq $udom);
1.86      ng       1520: 		# Doing this grep allows 'fuzzy' specification
                   1521: 		my @Matches = grep /^$co_name:$co_dom$/i,keys %$classlist;
                   1522: 		if (! scalar(@Matches)) {
                   1523: 		    push @badcollaborators,$_;
                   1524: 		} else {
                   1525: 		    push @goodcollaborators, @Matches;
                   1526: 		}
1.80      ng       1527: 	    }
1.86      ng       1528:             if (scalar(@goodcollaborators) != 0) {
1.57      matthew  1529:                 $result.='<b>Collaborators: </b>';
1.86      ng       1530:                 foreach (@goodcollaborators) {
                   1531: 		    my ($lastname,$givenn) = split(/,/,$$fullname{$_});
                   1532: 		    push @col_fullnames, $givenn.' '.$lastname;
                   1533: 		    $result.=$$fullname{$_}.'&nbsp; &nbsp; &nbsp;';
                   1534: 		}
1.57      matthew  1535:                 $result.='<br />'."\n";
1.150   ! albertel 1536: 		my ($part)=split(/\./,$_);
1.86      ng       1537: 		$result.='<input type="hidden" name="collaborator'.$counter.
1.150   ! albertel 1538: 		    '" value="'.$part.':'.(join ':',@goodcollaborators).'" />'.
        !          1539: 		    "\n";
1.86      ng       1540: 	    }
                   1541: 	    if (scalar(@badcollaborators) > 0) {
                   1542: 		$result.='<table border="0"><tr bgcolor="#ffbbbb"><td>';
                   1543: 		$result.='This student has submitted ';
                   1544: 		$result.=(scalar(@badcollaborators) == 1) ? 'an invalid collaborator' : 'invalid collaborators';
                   1545: 		$result .= ': '.join(', ',@badcollaborators);
                   1546: 		$result .= '</td></tr></table>';
                   1547: 	    }         
                   1548: 	    if (scalar(@badcollaborators > $ncol)) {
                   1549: 		$result .= '<table border="0"><tr bgcolor="#ffbbbb"><td>';
                   1550: 		$result .= 'This student has submitted too many '.
                   1551: 		    'collaborators.  Maximum is '.$ncol.'.';
                   1552: 		$result .= '</td></tr></table>';
                   1553: 	    }
1.41      ng       1554: 	}
                   1555:     }
1.44      ng       1556:     $request->print($result."\n");
1.33      ng       1557: 
1.44      ng       1558:     # print student answer/submission
                   1559:     # Options are (1) Handgaded submission only
                   1560:     #             (2) Last submission, includes submission that is not handgraded 
                   1561:     #                  (for multi-response type part)
                   1562:     #             (3) Last submission plus the parts info
                   1563:     #             (4) The whole record for this student
1.41      ng       1564:     if ($ENV{'form.lastSub'} =~ /^(lastonly|hdgrade)$/) {
1.147     albertel 1565: 	    my ($string,$timestamp)= &get_last_submission(\%record);
1.71      ng       1566: 	    my $lastsubonly=''.
1.44      ng       1567: 		($$timestamp eq '' ? '' : '<b>Date Submitted:</b> '.
1.118     ng       1568: 		 $$timestamp)."</td></tr>\n";
1.41      ng       1569: 	    if ($$timestamp eq '') {
1.118     ng       1570: 		$lastsubonly.='<tr><td bgcolor="#ffffe6">'.$$string[0]; 
1.41      ng       1571: 	    } else {
1.150   ! albertel 1572: 		my %seenparts;
1.41      ng       1573: 		for my $part (sort keys(%$handgrade)) {
1.118     ng       1574: 		    my ($partid,$respid) = split(/_/,$part);
1.150   ! albertel 1575: 		    if ($ENV{"form.$uname:$udom:$partid:submitted_by"}) {
        !          1576: 			if (exists($seenparts{$partid})) { next; }
        !          1577: 			$seenparts{$partid}=1;
        !          1578: 			my $submitby='<b>Part '.$partid.
        !          1579: 			    ' Collaborative submission by: </b>'.
        !          1580: 			    '<a href="javascript:viewSubmitter(\''.
        !          1581: 			    $ENV{"form.$uname:$udom:$partid:submitted_by"}.
        !          1582: 			    '\')"; TARGET=_self>'.
        !          1583: 			    $$fullname{$ENV{"form.$uname:$udom:$partid:submitted_by"}}.'</a><br />';
        !          1584: 			$request->print($submitby);
        !          1585: 			next;
        !          1586: 		    }
1.147     albertel 1587: 		    my $responsetype = $responseType->{$partid}->{$respid};
1.118     ng       1588: 		    if (!exists($record{'resource.'.$partid.'.'.$respid.'.submission'})) {
                   1589: 			$lastsubonly.='<tr><td bgcolor="#ffffe6"><b>Part '.
                   1590: 			    $partid.'</b> <font color="#999999">( ID '.$respid.
1.125     ng       1591: 			    ' )</font>&nbsp; &nbsp;'.
                   1592: 			    '<font color="red">Nothing submitted - no attempts</font><br /><br />';
1.118     ng       1593:  		    } else {
                   1594: 			foreach (@$string) {
1.148     albertel 1595: 			    my ($partid,$respid) = /^resource\.([^\.]*)\.([^\.]*)\.submission/;
1.118     ng       1596: 			    if ($part eq ($partid.'_'.$respid)) {
                   1597: 				my ($ressub,$subval) = split(/:/,$_,2);
                   1598:                             # Similarity check
                   1599: 				my $similar='';
1.134     www      1600: 				my $oname;
                   1601: 				my $odom;
                   1602: 				my $ocrsid;
                   1603: 				my $oessay;
                   1604: 				my $osim;
                   1605: 				if($ENV{'form.checkPlag'}){
                   1606: 				    ($oname,$odom,$ocrsid,$oessay,$osim)=&most_similar($uname,$udom,$subval);
                   1607: 				    if ($osim) {
                   1608: 					$osim=int($osim*100.0);
                   1609: 					$similar='<hr /><h3><font color="#FF0000">Essay is '.$osim.
                   1610: 					    '% similar to an essay by '.&Apache::loncommon::plainname($oname,$odom).
                   1611: 					    '</font></h3><blockquote><i>'.
                   1612: 					    &keywords_highlight($oessay).'</i></blockquote><hr />';
                   1613: 				    }
1.118     ng       1614: 				}
1.148     albertel 1615: 				my $order=&get_order($partid,$respid,$symb,$uname,$udom);
1.118     ng       1616: 				$lastsubonly.='<tr><td bgcolor="#ffffe6"><b>Part '.
                   1617: 				    $partid.'</b> <font color="#999999">( ID '.$respid.
                   1618: 				    ' )</font>&nbsp; &nbsp;'.
                   1619: 				    ($record{"resource.$partid.$respid.uploadedurl"}?
                   1620: 				     '<a href="'.
                   1621: 				     &Apache::lonnet::tokenwrapper($record{"resource.$partid.$respid.uploadedurl"}).
                   1622: 				     '"><img src="/adm/lonIcons/unknown.gif" border=0"> File uploaded by student</a> '.
                   1623: 				     '<font color="red" size="1">Like all files provided by users, '.
                   1624: 				     'this file may contain virusses</font><br />':'').
1.126     ng       1625: 				     '<b>Submitted Answer: </b>'.
1.148     albertel 1626: 				     &cleanRecord($subval,$responsetype,$symb,$partid,$respid,\%record,$order).
1.134     www      1627: 				     '<br /><br />'.$similar."\n"
1.118     ng       1628: 				     if ($ENV{'form.lastSub'} eq 'lastonly' || 
                   1629: 					 ($ENV{'form.lastSub'} eq 'hdgrade' && 
1.149     albertel 1630: 					  $$handgrade{$part} eq 'yes'));
1.118     ng       1631: 			    }
1.41      ng       1632: 			}
                   1633: 		    }
                   1634: 		}
                   1635: 	    }
1.118     ng       1636: 	    $lastsubonly.='</td></tr><tr bgcolor="#ffffff"><td>'."\n";
1.41      ng       1637: 	    $request->print($lastsubonly);
1.122     ng       1638:     } elsif ($ENV{'form.lastSub'} eq 'datesub') {
                   1639: 	my (undef,$responseType,undef,$parts) = &showResourceInfo($url);
1.148     albertel 1640: 	$request->print(&displaySubByDates($symb,\%record,$parts,$responseType,$checkIcon,$uname,$udom));
1.122     ng       1641:     } elsif ($ENV{'form.lastSub'} =~ /^(last|all)$/) {
1.41      ng       1642: 	$request->print(&Apache::loncommon::get_previous_attempt($symb,$uname,$udom,
1.44      ng       1643: 								 $ENV{'request.course.id'},
                   1644: 								 $last,'.submission',
                   1645: 								 'Apache::grades::keywords_highlight'));
1.41      ng       1646:     }
1.120     ng       1647: 
1.121     ng       1648:     $request->print('<input type="hidden" name="unamedom'.$counter.'" value="'.$uname.':'
                   1649: 	.$udom.'" />'."\n");
1.41      ng       1650:     
1.44      ng       1651:     # return if view submission with no grading option
1.118     ng       1652:     if ($ENV{'form.showgrading'} eq '' || (!&canmodify($usec))) {
1.120     ng       1653: 	my $toGrade.='<input type="button" value="Grade Student" '.
1.121     ng       1654: 	    'onClick="javascript:checksubmit(this.form,\'Grade Student\',\''
                   1655: 	    .$counter.'\');" TARGET=_self> &nbsp;'."\n" if (&canmodify($usec));
1.120     ng       1656: 	$toGrade.='</td></tr></table></td></tr></table></form>'."\n";
                   1657: 	$toGrade.=&show_grading_menu_form($symb,$url) 
1.72      ng       1658: 	    if (($ENV{'form.command'} eq 'submission') || 
                   1659: 		($ENV{'form.command'} eq 'processGroup' && $counter == $total));
1.120     ng       1660: 	$request = print($toGrade);
1.41      ng       1661: 	return;
                   1662:     }
1.33      ng       1663: 
1.121     ng       1664:     # essay grading message center
1.118     ng       1665:     if ($ENV{'form.handgrade'} eq 'yes') {
                   1666: 	my ($lastname,$givenn) = split(/,/,$ENV{'form.fullname'});
                   1667: 	my $msgfor = $givenn.' '.$lastname;
                   1668: 	if (scalar(@col_fullnames) > 0) {
                   1669: 	    my $lastone = pop @col_fullnames;
                   1670: 	    $msgfor .= ', '.(join ', ',@col_fullnames).' and '.$lastone.'.';
                   1671: 	}
                   1672: 	$msgfor =~ s/\'/\\'/g; #' stupid emacs - no! javascript
1.121     ng       1673: 	$result='<input type="hidden" name="includemsg'.$counter.'" value="" />'."\n".
                   1674: 	    '<input type="hidden" name="newmsg'.$counter.'" value="" />'."\n";
                   1675: 	$result.='&nbsp;<a href="javascript:msgCenter(document.SCORE,'.$counter.
1.118     ng       1676: 	    ',\''.$msgfor.'\')"; TARGET=_self>'.
                   1677: 	    'Compose Message to student'.(scalar(@col_fullnames) >= 1 ? 's' : '').'</a> &nbsp;'.
                   1678: 	    '<img src="'.$request->dir_config('lonIconsURL').
                   1679: 	    '/mailbkgrd.gif" width="14" height="10" name="mailicon'.$counter.'" />'."\n".
                   1680: 	    '<br />&nbsp;(Message will be sent when you click on Save & Next below.)'."\n" 
                   1681: 	    if ($ENV{'form.handgrade'} eq 'yes');
1.121     ng       1682: 	$request->print($result);
1.118     ng       1683:     }
1.41      ng       1684: 
                   1685:     my %seen = ();
                   1686:     my @partlist;
1.129     ng       1687:     my @gradePartRespid;
1.41      ng       1688:     for (sort keys(%$handgrade)) {
                   1689: 	my ($partid,$respid) = split(/_/);
                   1690: 	next if ($seen{$partid} > 0);
                   1691: 	$seen{$partid}++;
1.118     ng       1692: 	next if ($$handgrade{$_} =~ /:no$/ && $ENV{'form.lastSub'} =~ /^(hdgrade)$/);
1.41      ng       1693: 	push @partlist,$partid;
1.129     ng       1694: 	push @gradePartRespid,$partid.'.'.$respid;
1.41      ng       1695: 
1.71      ng       1696: 	$request->print(&gradeBox($request,$symb,$uname,$udom,$counter,$partid,\%record));
1.41      ng       1697:     }
1.45      ng       1698:     $result='<input type="hidden" name="partlist'.$counter.
                   1699: 	'" value="'.(join ":",@partlist).'" />'."\n";
1.129     ng       1700:     $result.='<input type="hidden" name="gradePartRespid'.
                   1701: 	'" value="'.(join ":",@gradePartRespid).'" />'."\n" if ($counter == 0);
1.45      ng       1702:     my $ctr = 0;
                   1703:     while ($ctr < scalar(@partlist)) {
                   1704: 	$result.='<input type="hidden" name="partid'.$counter.'_'.$ctr.'" value="'.
                   1705: 	    $partlist[$ctr].'" />'."\n";
                   1706: 	$ctr++;
                   1707:     }
                   1708:     $request->print($result.'</td></tr></table></td></tr></table>'."\n");
1.41      ng       1709: 
                   1710:     # print end of form
                   1711:     if ($counter == $total) {
1.120     ng       1712: 	my $endform='<table border="0"><tr><td>'."\n";
1.119     ng       1713: 	$endform.='<input type="button" value="Save & Next" '.
                   1714: 	    'onClick="javascript:checksubmit(this.form,\'Save & Next\','.
                   1715: 	    $total.','.scalar(@partlist).');" TARGET=_self> &nbsp;'."\n";
                   1716: 	my $ntstu ='<select name="NTSTU">'.
                   1717: 	    '<option>1</option><option>2</option>'.
                   1718: 	    '<option>3</option><option>5</option>'.
                   1719: 	    '<option>7</option><option>10</option></select>'."\n";
                   1720: 	my $nsel = ($ENV{'form.NTSTU'} ne '' ? $ENV{'form.NTSTU'} : '1');
                   1721: 	$ntstu =~ s/<option>$nsel</<option selected="on">$nsel</;
                   1722: 	$endform.=$ntstu.'student(s) &nbsp;&nbsp;';
1.126     ng       1723: 	$endform.='<input type="button" value="Previous" '.
                   1724: 	    'onClick="javascript:checksubmit(this.form,\'Previous\');" TARGET=_self> &nbsp;'."\n".
                   1725: 	    '<input type="button" value="Next" '.
                   1726: 	    'onClick="javascript:checksubmit(this.form,\'Next\');" TARGET=_self> &nbsp;';
                   1727: 	$endform.='(Next and Previous (student) do not save the scores.)'."\n" ;
1.45      ng       1728: 	$endform.='</td><tr></table></form>';
1.50      albertel 1729: 	$endform.=&show_grading_menu_form($symb,$url);
1.41      ng       1730: 	$request->print($endform);
                   1731:     }
                   1732:     return '';
1.38      ng       1733: }
                   1734: 
1.44      ng       1735: #--- Retrieve the last submission for all the parts
1.38      ng       1736: sub get_last_submission {
1.119     ng       1737:     my ($returnhash)=@_;
1.46      ng       1738:     my (@string,$timestamp);
1.119     ng       1739:     if ($$returnhash{'version'}) {
1.46      ng       1740: 	my %lasthash=();
                   1741: 	my ($version);
1.119     ng       1742: 	for ($version=1;$version<=$$returnhash{'version'};$version++) {
                   1743: 	    foreach (sort(split(/\:/,$$returnhash{$version.':keys'}))) {
                   1744: 		$lasthash{$_}=$$returnhash{$version.':'.$_};
                   1745: 		   $timestamp = scalar(localtime($$returnhash{$version.':timestamp'}));
1.46      ng       1746: 	    }
                   1747: 	}
                   1748: 	foreach ((keys %lasthash)) {
                   1749: 	    if ($_ =~ /\.submission$/) {
                   1750: 		my ($partid,$foo) = split(/submission$/,$_);
                   1751: 		my $draft  = $lasthash{$partid.'awarddetail'} eq 'DRAFT' ?
                   1752: 		    '<font color="red">Draft Copy</font> ' : '';
                   1753: 		push @string, (join(':',$_,$draft.$lasthash{$_}));
1.41      ng       1754: 	    }
                   1755: 	}
                   1756:     }
1.125     ng       1757:     @string = $string[0] eq '' ? '<font color="red">Nothing submitted - no attempts.</font>' : @string;
1.46      ng       1758:     return \@string,\$timestamp;
1.38      ng       1759: }
1.35      ng       1760: 
1.44      ng       1761: #--- High light keywords, with style choosen by user.
1.38      ng       1762: sub keywords_highlight {
1.44      ng       1763:     my $string    = shift;
                   1764:     my $size      = $ENV{'form.kwsize'} eq '0' ? '' : 'size='.$ENV{'form.kwsize'};
                   1765:     my $styleon   = $ENV{'form.kwstyle'} eq ''  ? '' : $ENV{'form.kwstyle'};
1.41      ng       1766:     (my $styleoff = $styleon) =~ s/\</\<\//;
1.44      ng       1767:     my @keylist   = split(/[,\s+]/,$ENV{'form.keywords'});
1.41      ng       1768:     foreach (@keylist) {
1.119     ng       1769: 	$string =~ s/\b\Q$_\E(\b|\.)/<font color\=$ENV{'form.kwclr'} $size\>$styleon$_$styleoff<\/font>/gi;
1.41      ng       1770:     }
                   1771:     return $string;
1.38      ng       1772: }
1.36      ng       1773: 
1.44      ng       1774: #--- Called from submission routine
1.38      ng       1775: sub processHandGrade {
1.41      ng       1776:     my ($request) = shift;
                   1777:     my $url    = $ENV{'form.url'};
                   1778:     my $symb   = $ENV{'form.symb'};
                   1779:     my $button = $ENV{'form.gradeOpt'};
                   1780:     my $ngrade = $ENV{'form.NCT'};
                   1781:     my $ntstu  = $ENV{'form.NTSTU'};
1.44      ng       1782:     if ($button eq 'Save & Next') {
                   1783: 	my $ctr = 0;
                   1784: 	while ($ctr < $ngrade) {
                   1785: 	    my ($uname,$udom) = split(/:/,$ENV{'form.unamedom'.$ctr});
1.77      ng       1786: 	    my ($errorflag,$pts,$wgt) = &saveHandGrade($request,$url,$symb,$uname,$udom,$ctr);
1.71      ng       1787: 	    if ($errorflag eq 'no_score') {
                   1788: 		$ctr++;
                   1789: 		next;
                   1790: 	    }
1.104     albertel 1791: 	    if ($errorflag eq 'not_allowed') {
                   1792: 		$request->print("<font color=\"red\">Not allowed to modify grades for $uname:$udom</font>");
                   1793: 		$ctr++;
                   1794: 		next;
                   1795: 	    }
1.44      ng       1796: 	    my $includemsg = $ENV{'form.includemsg'.$ctr};
                   1797: 	    my ($subject,$message,$msgstatus) = ('','','');
1.62      albertel 1798: 	    if ($includemsg =~ /savemsg|newmsg\Q$ctr\E/) {
1.44      ng       1799: 		$subject = $ENV{'form.msgsub'} if ($includemsg =~ /^msgsub/);
                   1800: 		my (@msgnum) = split(/,/,$includemsg);
                   1801: 		foreach (@msgnum) {
                   1802: 		    $message.=$ENV{'form.'.$_} if ($_ =~ /savemsg|newmsg/ && $_ ne '');
                   1803: 		}
1.80      ng       1804: 		$message =&Apache::lonfeedback::clear_out_html($message);
1.77      ng       1805: 		$message.="\n\nPoint".($pts > 1 ? 's':'').' awarded = '.$pts.' out of '.$wgt;
1.80      ng       1806: 		$message.=" for <a href=\"".
                   1807: 		    &Apache::lonnet::clutter($url).
                   1808: 		    "?symb=$symb\">$ENV{'form.probTitle'}</a>";
1.44      ng       1809: 		$msgstatus = &Apache::lonmsg::user_normal_msg ($uname,$udom,
                   1810: 							       $ENV{'form.msgsub'},$message);
                   1811: 	    }
                   1812: 	    if ($ENV{'form.collaborator'.$ctr}) {
1.150   ! albertel 1813: 		&Apache::lonnet::logthis('collab '.(join(':',@{ $ENV{'form.collaborator'.$ctr} })));
        !          1814: 		my @collabstrs;
        !          1815: 		if (ref($ENV{'form.collaborator'.$ctr}) eq 'ARRAY') {
        !          1816: 		    @collabstrs=@{$ENV{'form.collaborator'.$ctr}};
        !          1817: 		} else {
        !          1818: 		    @collabstrs=$ENV{'form.collaborator'.$ctr};
        !          1819: 		}
        !          1820: 		foreach my $collabstr (@collabstrs) {
        !          1821: 		    my ($part,@collaborators) = split(/:/,$collabstr);
        !          1822: 		    foreach (@collaborators) {
        !          1823: 			my ($errorflag,$pts,$wgt) = 
        !          1824: 			    &saveHandGrade($request,$url,$symb,$_,$udom,$ctr,
        !          1825: 					   $ENV{'form.unamedom'.$ctr},$part);
        !          1826: 			if ($errorflag eq 'not_allowed') {
        !          1827: 			    $request->print("<font color=\"red\">Not allowed to modify grades for $_:$udom</font>");
        !          1828: 			    next;
        !          1829: 			} else {
        !          1830: 			    if ($message ne '') {
        !          1831: 				$msgstatus = &Apache::lonmsg::user_normal_msg($_,$udom,$ENV{'form.msgsub'},$message);
        !          1832: 			    }
        !          1833: 			    
1.104     albertel 1834: 			}
1.44      ng       1835: 		    }
                   1836: 		}
                   1837: 	    }
                   1838: 	    $ctr++;
                   1839: 	}
                   1840:     }
                   1841: 
1.119     ng       1842:     if ($ENV{'form.handgrade'} eq 'yes') {
                   1843: 	# Keywords sorted in alphabatical order
                   1844: 	my $loginuser = $ENV{'user.name'}.':'.$ENV{'user.domain'};
                   1845: 	my %keyhash = ();
                   1846: 	$ENV{'form.keywords'}           =~ s/,\s{0,}|\s+/ /g;
                   1847: 	$ENV{'form.keywords'}           =~ s/^\s+|\s+$//;
                   1848: 	my (@keywords) = sort(split(/\s+/,$ENV{'form.keywords'}));
                   1849: 	$ENV{'form.keywords'} = join(' ',@keywords);
                   1850: 	$keyhash{$symb.'_keywords'}     = $ENV{'form.keywords'};
                   1851: 	$keyhash{$symb.'_subject'}      = $ENV{'form.msgsub'};
                   1852: 	$keyhash{$loginuser.'_kwclr'}   = $ENV{'form.kwclr'};
                   1853: 	$keyhash{$loginuser.'_kwsize'}  = $ENV{'form.kwsize'};
                   1854: 	$keyhash{$loginuser.'_kwstyle'} = $ENV{'form.kwstyle'};
                   1855: 
                   1856: 	# message center - Order of message gets changed. Blank line is eliminated.
                   1857: 	# New messages are saved in ENV for the next student.
                   1858: 	# All messages are saved in nohist_handgrade.db
                   1859: 	my ($ctr,$idx) = (1,1);
                   1860: 	while ($ctr <= $ENV{'form.savemsgN'}) {
                   1861: 	    if ($ENV{'form.savemsg'.$ctr} ne '') {
                   1862: 		$keyhash{$symb.'_savemsg'.$idx} = $ENV{'form.savemsg'.$ctr};
                   1863: 		$idx++;
                   1864: 	    }
                   1865: 	    $ctr++;
1.41      ng       1866: 	}
1.119     ng       1867: 	$ctr = 0;
                   1868: 	while ($ctr < $ngrade) {
                   1869: 	    if ($ENV{'form.newmsg'.$ctr} ne '') {
                   1870: 		$keyhash{$symb.'_savemsg'.$idx} = $ENV{'form.newmsg'.$ctr};
                   1871: 		$ENV{'form.savemsg'.$idx} = $ENV{'form.newmsg'.$ctr};
                   1872: 		$idx++;
                   1873: 	    }
                   1874: 	    $ctr++;
1.41      ng       1875: 	}
1.119     ng       1876: 	$ENV{'form.savemsgN'} = --$idx;
                   1877: 	$keyhash{$symb.'_savemsgN'} = $ENV{'form.savemsgN'};
                   1878: 	my $putresult = &Apache::lonnet::put
                   1879: 	    ('nohist_handgrade',\%keyhash,
                   1880: 	     $ENV{'course.'.$ENV{'request.course.id'}.'.domain'},
                   1881: 	     $ENV{'course.'.$ENV{'request.course.id'}.'.num'});
1.41      ng       1882:     }
1.44      ng       1883:     # Called by Save & Refresh from Highlight Attribute Window
1.119     ng       1884:     my (undef,undef,$fullname) = &getclasslist($ENV{'form.section'},'1');
1.41      ng       1885:     if ($ENV{'form.refresh'} eq 'on') {
1.86      ng       1886: 	my ($ctr,$total) = (0,0);
                   1887: 	while ($ctr < $ngrade) {
                   1888: 	    $total++ if  $ENV{'form.unamedom'.$ctr} ne '';
                   1889: 	    $ctr++;
                   1890: 	}
1.41      ng       1891: 	$ENV{'form.NTSTU'}=$ngrade;
1.86      ng       1892: 	$ctr = 0;
                   1893: 	while ($ctr < $total) {
                   1894: 	    my $processUser = $ENV{'form.unamedom'.$ctr};
                   1895: 	    ($ENV{'form.student'},$ENV{'form.userdom'}) = split(/:/,$processUser);
                   1896: 	    $ENV{'form.fullname'} = $$fullname{$processUser};
                   1897: 	    &submission($request,$ctr,$total-1);
1.41      ng       1898: 	    $ctr++;
                   1899: 	}
                   1900: 	return '';
                   1901:     }
1.36      ng       1902: 
1.121     ng       1903: # Go directly to grade student - from submission or link from chart page
1.120     ng       1904:     if ($button eq 'Grade Student') {
1.121     ng       1905: 	(undef,undef,$ENV{'form.handgrade'},undef,undef) = &showResourceInfo($url);
1.120     ng       1906: 	my $processUser = $ENV{'form.unamedom'.$ENV{'form.studentNo'}};
                   1907: 	($ENV{'form.student'},$ENV{'form.userdom'}) = split(/:/,$processUser);
                   1908: 	$ENV{'form.fullname'} = $$fullname{$processUser};
                   1909: 	&submission($request,0,0);
                   1910: 	return '';
                   1911:     }
                   1912: 
1.44      ng       1913:     # Get the next/previous one or group of students
1.41      ng       1914:     my $firststu = $ENV{'form.unamedom0'};
                   1915:     my $laststu = $ENV{'form.unamedom'.($ngrade-1)};
1.119     ng       1916:     my $ctr = 2;
1.41      ng       1917:     while ($laststu eq '') {
                   1918: 	$laststu  = $ENV{'form.unamedom'.($ngrade-$ctr)};
                   1919: 	$ctr++;
                   1920: 	$laststu = $firststu if ($ctr > $ngrade);
                   1921:     }
1.44      ng       1922: 
1.41      ng       1923:     my (@parsedlist,@nextlist);
                   1924:     my ($nextflg) = 0;
1.53      albertel 1925:     foreach (sort {lc($$fullname{$a}) cmp lc($$fullname{$b}) } keys %$fullname) {
1.41      ng       1926: 	if ($nextflg == 1 && $button =~ /Next$/) {
                   1927: 	    push @parsedlist,$_;
                   1928: 	}
                   1929: 	$nextflg = 1 if ($_ eq $laststu);
                   1930: 	if ($button eq 'Previous') {
                   1931: 	    last if ($_ eq $firststu);
                   1932: 	    push @parsedlist,$_;
                   1933: 	}
                   1934:     }
                   1935:     $ctr = 0;
                   1936:     @parsedlist = reverse @parsedlist if ($button eq 'Previous');
1.145     albertel 1937:     my ($partlist) = &response_type($url);
1.41      ng       1938:     foreach my $student (@parsedlist) {
1.145     albertel 1939: 	my $submitonly=$ENV{'form.submitonly'};
1.41      ng       1940: 	my ($uname,$udom) = split(/:/,$student);
1.145     albertel 1941: 	if ($submitonly =~ /^(yes|graded)$/) {
                   1942: #	    my %record = &Apache::lonnet::restore($symb,$ENV{'request.course.id'},$udom,$uname);
                   1943: 	    my %status=&student_gradeStatus($url,$symb,$udom,$uname,$partlist);
                   1944: 	    my $submitted = 0;
                   1945: 	    my $graded = 1;
                   1946: 	    foreach (keys(%status)) {
                   1947: 		$submitted = 1 if ($status{$_} ne 'nothing');
                   1948: 		$graded = 0 if ($status{$_} =~ /^correct/);
                   1949: 		my ($foo,$partid,$foo1) = split(/\./,$_);
                   1950: 		if ($status{'resource.'.$partid.'.submitted_by'} ne '') {
                   1951: 		    $submitted = 0;
                   1952: 		}
1.41      ng       1953: 	    }
1.145     albertel 1954: 	    next if (!$submitted && ($submitonly eq 'yes' || $submitonly eq 'graded'));
                   1955: 	    next if (!$graded && $submitonly eq 'graded');
1.41      ng       1956: 	}
                   1957: 	push @nextlist,$student if ($ctr < $ntstu);
1.129     ng       1958: 	last if ($ctr == $ntstu);
1.41      ng       1959: 	$ctr++;
                   1960:     }
1.36      ng       1961: 
1.41      ng       1962:     $ctr = 0;
                   1963:     my $total = scalar(@nextlist)-1;
1.39      ng       1964: 
1.41      ng       1965:     foreach (sort @nextlist) {
                   1966: 	my ($uname,$udom,$submitter) = split(/:/);
1.44      ng       1967: 	$ENV{'form.student'}  = $uname;
                   1968: 	$ENV{'form.userdom'}  = $udom;
1.41      ng       1969: 	$ENV{'form.fullname'} = $$fullname{$_};
                   1970: 	&submission($request,$ctr,$total);
                   1971: 	$ctr++;
                   1972:     }
                   1973:     if ($total < 0) {
                   1974: 	my $the_end = '<h3><font color="red">LON-CAPA User Message</font></h3><br />'."\n";
                   1975: 	$the_end.='<b>Message: </b> No more students for this section or class.<br /><br />'."\n";
                   1976: 	$the_end.='Click on the button below to return to the grading menu.<br /><br />'."\n";
                   1977: 	$the_end.=&show_grading_menu_form ($symb,$url);
                   1978: 	$request->print($the_end);
                   1979:     }
                   1980:     return '';
1.38      ng       1981: }
1.36      ng       1982: 
1.44      ng       1983: #---- Save the score and award for each student, if changed
1.38      ng       1984: sub saveHandGrade {
1.150   ! albertel 1985:     my ($request,$url,$symb,$stuname,$domain,$newflg,$submitter,$part) = @_;
1.104     albertel 1986:     my $usec = &Apache::lonnet::getsection($domain,$stuname,
                   1987: 					   $ENV{'request.course.id'});
                   1988:     if (!&canmodify($usec)) { return('not_allowed'); }
1.77      ng       1989:     my %record     = &Apache::lonnet::restore($symb,$ENV{'request.course.id'},$domain,$stuname);
                   1990:     my %newrecord  = ();
                   1991:     my ($pts,$wgt) = ('','');
1.41      ng       1992:     foreach (split(/:/,$ENV{'form.partlist'.$newflg})) {
1.150   ! albertel 1993: 	&Apache::lonnet::logthis("-$submitter-$stuname-$part-$_");
        !          1994: 	#collaborator may vary for different parts
        !          1995: 	if ($submitter && $_ ne $part) { next; }
1.125     ng       1996: 	my $dropMenu = $ENV{'form.GD_SEL'.$newflg.'_'.$_};
                   1997: 	if ($dropMenu eq 'excused') {
1.58      albertel 1998: 	    if ($record{'resource.'.$_.'.solved'} ne 'excused') {
                   1999: 		$newrecord{'resource.'.$_.'.solved'} = 'excused';
                   2000: 		if (exists($record{'resource.'.$_.'.awarded'})) {
                   2001: 		    $newrecord{'resource.'.$_.'.awarded'} = '';
                   2002: 		}
1.125     ng       2003: 	    $newrecord{'resource.'.$_.'.regrader'}="$ENV{'user.name'}:$ENV{'user.domain'}";
1.58      albertel 2004: 	    }
1.125     ng       2005: 	} elsif ($dropMenu eq 'reset status'
                   2006: 		 && exists($record{'resource.'.$_.'.solved'})) { #don't bother if no old records -> no attempts
                   2007: 	    $newrecord{'resource.'.$_.'.tries'} = 0;
                   2008: 	    $newrecord{'resource.'.$_.'.solved'} = '';
                   2009: 	    $newrecord{'resource.'.$_.'.award'} = '';
                   2010: 	    $newrecord{'resource.'.$_.'.awarded'} = 0;
                   2011: 	    $newrecord{'resource.'.$_.'.regrader'}="$ENV{'user.name'}:$ENV{'user.domain'}";
                   2012: 	} elsif ($dropMenu eq '') {
1.77      ng       2013: 	    $pts = ($ENV{'form.GD_BOX'.$newflg.'_'.$_} ne '' ? 
                   2014: 		    $ENV{'form.GD_BOX'.$newflg.'_'.$_} : 
                   2015: 		    $ENV{'form.RADVAL'.$newflg.'_'.$_});
1.71      ng       2016: 	    return 'no_score' if ($pts eq '' && $ENV{'form.GD_SEL'.$newflg.'_'.$_} eq '');
1.77      ng       2017: 	    $wgt = $ENV{'form.WGT'.$newflg.'_'.$_} eq '' ? 1 : 
1.44      ng       2018: 		$ENV{'form.WGT'.$newflg.'_'.$_};
1.41      ng       2019: 	    my $partial= $pts/$wgt;
1.119     ng       2020: 	    next if ($partial eq $record{'resource.'.$_.'.awarded'}); #do not update score for part if not changed.
1.44      ng       2021: 	    $newrecord{'resource.'.$_.'.awarded'}  = $partial 
                   2022: 		if ($record{'resource.'.$_.'.awarded'} ne $partial);
                   2023: 	    my $reckey = 'resource.'.$_.'.solved';
1.41      ng       2024: 	    if ($partial == 0) {
1.44      ng       2025: 		$newrecord{$reckey} = 'incorrect_by_override' 
                   2026: 		    if ($record{$reckey} ne 'incorrect_by_override');
1.41      ng       2027: 	    } else {
1.44      ng       2028: 		$newrecord{$reckey} = 'correct_by_override' 
                   2029: 		    if ($record{$reckey} ne 'correct_by_override');
1.41      ng       2030: 	    }
1.150   ! albertel 2031: 	    
1.44      ng       2032: 	    $newrecord{'resource.'.$_.'.submitted_by'} = $submitter 
                   2033: 		if ($submitter && ($record{'resource.'.$_.'.submitted_by'} ne $submitter));
1.122     ng       2034: 	    $newrecord{'resource.'.$_.'.regrader'}="$ENV{'user.name'}:$ENV{'user.domain'}";
1.41      ng       2035: 	}
                   2036:     }
1.44      ng       2037: 
                   2038:     if (scalar(keys(%newrecord)) > 0) {
                   2039: 	&Apache::lonnet::cstore(\%newrecord,$symb,
                   2040: 				$ENV{'request.course.id'},$domain,$stuname);
1.41      ng       2041:     }
1.77      ng       2042:     return '',$pts,$wgt;
1.36      ng       2043: }
1.38      ng       2044: 
1.44      ng       2045: #--------------------------------------------------------------------------------------
                   2046: #
                   2047: #-------------------------- Next few routines handles grading by section or whole class
                   2048: #
                   2049: #--- Javascript to handle grading by section or whole class
1.42      ng       2050: sub viewgrades_js {
                   2051:     my ($request) = shift;
                   2052: 
1.41      ng       2053:     $request->print(<<VIEWJAVASCRIPT);
                   2054: <script type="text/javascript" language="javascript">
1.45      ng       2055:    function writePoint(partid,weight,point) {
1.125     ng       2056: 	var radioButton = document.classgrade["RADVAL_"+partid];
                   2057: 	var textbox = document.classgrade["TEXTVAL_"+partid];
1.42      ng       2058: 	if (point == "textval") {
1.125     ng       2059: 	    point = document.classgrade["TEXTVAL_"+partid].value;
1.109     matthew  2060: 	    if (isNaN(point) || parseFloat(point) < 0) {
                   2061: 		alert("A number equal or greater than 0 is expected. Entered value = "+parseFloat(point));
1.42      ng       2062: 		var resetbox = false;
                   2063: 		for (var i=0; i<radioButton.length; i++) {
                   2064: 		    if (radioButton[i].checked) {
                   2065: 			textbox.value = i;
                   2066: 			resetbox = true;
                   2067: 		    }
                   2068: 		}
                   2069: 		if (!resetbox) {
                   2070: 		    textbox.value = "";
                   2071: 		}
                   2072: 		return;
                   2073: 	    }
1.109     matthew  2074: 	    if (parseFloat(point) > parseFloat(weight)) {
                   2075: 		var resp = confirm("You entered a value ("+parseFloat(point)+
1.44      ng       2076: 				   ") greater than the weight for the part. Accept?");
                   2077: 		if (resp == false) {
                   2078: 		    textbox.value = "";
                   2079: 		    return;
                   2080: 		}
                   2081: 	    }
1.42      ng       2082: 	    for (var i=0; i<radioButton.length; i++) {
                   2083: 		radioButton[i].checked=false;
1.109     matthew  2084: 		if (parseFloat(point) == i) {
1.42      ng       2085: 		    radioButton[i].checked=true;
                   2086: 		}
                   2087: 	    }
1.41      ng       2088: 
1.42      ng       2089: 	} else {
1.125     ng       2090: 	    textbox.value = parseFloat(point);
1.42      ng       2091: 	}
1.41      ng       2092: 	for (i=0;i<document.classgrade.total.value;i++) {
1.125     ng       2093: 	    var user = document.classgrade["ctr"+i].value;
                   2094: 	    var scorename = document.classgrade["GD_"+user+"_"+partid+"_awarded"];
                   2095: 	    var saveval   = document.classgrade["GD_"+user+"_"+partid+"_solved_s"].value;
                   2096: 	    var selname   = document.classgrade["GD_"+user+"_"+partid+"_solved"];
1.42      ng       2097: 	    if (saveval != "correct") {
                   2098: 		scorename.value = point;
1.43      ng       2099: 		if (selname[0].selected != true) {
                   2100: 		    selname[0].selected = true;
                   2101: 		}
1.42      ng       2102: 	    }
                   2103: 	}
1.125     ng       2104: 	document.classgrade["SELVAL_"+partid][0].selected = true;
1.42      ng       2105:     }
                   2106: 
                   2107:     function writeRadText(partid,weight) {
1.125     ng       2108: 	var selval   = document.classgrade["SELVAL_"+partid];
                   2109: 	var radioButton = document.classgrade["RADVAL_"+partid];
                   2110: 	var textbox = document.classgrade["TEXTVAL_"+partid];
                   2111: 	if (selval[1].selected || selval[2].selected) {
1.42      ng       2112: 	    for (var i=0; i<radioButton.length; i++) {
                   2113: 		radioButton[i].checked=false;
                   2114: 
                   2115: 	    }
                   2116: 	    textbox.value = "";
                   2117: 
                   2118: 	    for (i=0;i<document.classgrade.total.value;i++) {
1.125     ng       2119: 		var user = document.classgrade["ctr"+i].value;
                   2120: 		var scorename = document.classgrade["GD_"+user+"_"+partid+"_awarded"];
                   2121: 		var saveval   = document.classgrade["GD_"+user+"_"+partid+"_solved_s"].value;
                   2122: 		var selname   = document.classgrade["GD_"+user+"_"+partid+"_solved"];
1.42      ng       2123: 		if (saveval != "correct") {
                   2124: 		    scorename.value = "";
1.125     ng       2125: 		    if (selval[1].selected) {
                   2126: 			selname[1].selected = true;
                   2127: 		    } else {
                   2128: 			selname[2].selected = true;
                   2129: 			if (Number(document.classgrade["GD_"+user+"_"+partid+"_tries"].value)) 
                   2130: 			{document.classgrade["GD_"+user+"_"+partid+"_tries"].value = '0';}
                   2131: 		    }
1.42      ng       2132: 		}
                   2133: 	    }
1.43      ng       2134: 	} else {
                   2135: 	    for (i=0;i<document.classgrade.total.value;i++) {
1.125     ng       2136: 		var user = document.classgrade["ctr"+i].value;
                   2137: 		var scorename = document.classgrade["GD_"+user+"_"+partid+"_awarded"];
                   2138: 		var saveval   = document.classgrade["GD_"+user+"_"+partid+"_solved_s"].value;
                   2139: 		var selname   = document.classgrade["GD_"+user+"_"+partid+"_solved"];
1.43      ng       2140: 		if (saveval != "correct") {
1.125     ng       2141: 		    scorename.value = document.classgrade["GD_"+user+"_"+partid+"_awarded_s"].value;
1.43      ng       2142: 		    selname[0].selected = true;
                   2143: 		}
                   2144: 	    }
                   2145: 	}	    
1.42      ng       2146:     }
                   2147: 
                   2148:     function changeSelect(partid,user) {
1.125     ng       2149: 	var selval = document.classgrade["GD_"+user+'_'+partid+"_solved"];
                   2150: 	var textbox = document.classgrade["GD_"+user+'_'+partid+"_awarded"];
1.44      ng       2151: 	var point  = textbox.value;
1.125     ng       2152: 	var weight = document.classgrade["weight_"+partid].value;
1.44      ng       2153: 
1.109     matthew  2154: 	if (isNaN(point) || parseFloat(point) < 0) {
                   2155: 	    alert("A number equal or greater than 0 is expected. Entered value = "+parseFloat(point));
1.44      ng       2156: 	    textbox.value = "";
                   2157: 	    return;
                   2158: 	}
1.109     matthew  2159: 	if (parseFloat(point) > parseFloat(weight)) {
                   2160: 	    var resp = confirm("You entered a value ("+parseFloat(point)+
1.44      ng       2161: 			       ") greater than the weight of the part. Accept?");
                   2162: 	    if (resp == false) {
                   2163: 		textbox.value = "";
                   2164: 		return;
                   2165: 	    }
                   2166: 	}
1.42      ng       2167: 	selval[0].selected = true;
                   2168:     }
                   2169: 
                   2170:     function changeOneScore(partid,user) {
1.125     ng       2171: 	var selval = document.classgrade["GD_"+user+'_'+partid+"_solved"];
                   2172: 	if (selval[1].selected || selval[2].selected) {
                   2173: 	    document.classgrade["GD_"+user+'_'+partid+"_awarded"].value = "";
                   2174: 	    if (selval[2].selected) {
                   2175: 		document.classgrade["GD_"+user+'_'+partid+"_tries"].value = "0";
                   2176: 	    }
1.42      ng       2177: 	}
                   2178:     }
                   2179: 
                   2180:     function resetEntry(numpart) {
                   2181: 	for (ctpart=0;ctpart<numpart;ctpart++) {
1.125     ng       2182: 	    var partid = document.classgrade["partid_"+ctpart].value;
                   2183: 	    var radioButton = document.classgrade["RADVAL_"+partid];
                   2184: 	    var textbox = document.classgrade["TEXTVAL_"+partid];
                   2185: 	    var selval  = document.classgrade["SELVAL_"+partid];
1.42      ng       2186: 	    for (var i=0; i<radioButton.length; i++) {
                   2187: 		radioButton[i].checked=false;
                   2188: 
                   2189: 	    }
                   2190: 	    textbox.value = "";
                   2191: 	    selval[0].selected = true;
                   2192: 
                   2193: 	    for (i=0;i<document.classgrade.total.value;i++) {
1.125     ng       2194: 		var user = document.classgrade["ctr"+i].value;
                   2195: 		var resetscore = document.classgrade["GD_"+user+"_"+partid+"_awarded"];
                   2196: 		resetscore.value = document.classgrade["GD_"+user+"_"+partid+"_awarded_s"].value;
                   2197: 		var resettries = document.classgrade["GD_"+user+"_"+partid+"_tries"];
                   2198: 		resettries.value = document.classgrade["GD_"+user+"_"+partid+"_tries_s"].value;
                   2199: 		var saveselval   = document.classgrade["GD_"+user+"_"+partid+"_solved_s"].value;
                   2200: 		var selname   = document.classgrade["GD_"+user+"_"+partid+"_solved"];
1.42      ng       2201: 		if (saveselval == "excused") {
1.43      ng       2202: 		    if (selname[1].selected == false) { selname[1].selected = true;}
1.42      ng       2203: 		} else {
1.43      ng       2204: 		    if (selname[0].selected == false) {selname[0].selected = true};
1.42      ng       2205: 		}
                   2206: 	    }
1.41      ng       2207: 	}
1.42      ng       2208:     }
                   2209: 
1.41      ng       2210: </script>
                   2211: VIEWJAVASCRIPT
1.42      ng       2212: }
                   2213: 
1.44      ng       2214: #--- show scores for a section or whole class w/ option to change/update a score
1.42      ng       2215: sub viewgrades {
                   2216:     my ($request) = shift;
                   2217:     &viewgrades_js($request);
1.41      ng       2218: 
                   2219:     my ($symb,$url) = ($ENV{'form.symb'},$ENV{'form.url'}); 
1.45      ng       2220:     my $result='<h3><font color="#339933">Manual Grading</font></h3>';
1.38      ng       2221: 
1.118     ng       2222:     $result.='<font size=+1><b>Current Resource: </b>'.$ENV{'form.probTitle'}.'</font>'."\n";
1.41      ng       2223: 
                   2224:     #view individual student submission form - called using Javascript viewOneStudent
1.45      ng       2225:     $result.=&jscriptNform($url,$symb);
1.41      ng       2226: 
1.44      ng       2227:     #beginning of class grading form
1.41      ng       2228:     $result.= '<form action="/adm/grades" method="post" name="classgrade">'."\n".
1.106     albertel 2229: 	'<input type="hidden" name="symb"    value="'.$symb.'" />'."\n".
1.41      ng       2230: 	'<input type="hidden" name="url"     value="'.$url.'" />'."\n".
1.38      ng       2231: 	'<input type="hidden" name="command" value="editgrades" />'."\n".
1.72      ng       2232: 	'<input type="hidden" name="section" value="'.$ENV{'form.section'}.'" />'."\n".
1.77      ng       2233: 	'<input type="hidden" name="saveState" value="'.$ENV{'form.saveState'}.'" />'."\n".
1.125     ng       2234: 	'<input type="hidden" name="Status" value="'.$ENV{'form.Status'}.'" />'."\n".
1.72      ng       2235: 	'<input type="hidden" name="probTitle" value="'.$ENV{'form.probTitle'}.'" />'."\n";
                   2236: 
1.126     ng       2237:     my $sectionClass;
1.52      albertel 2238:     if ($ENV{'form.section'} eq 'all') {
1.126     ng       2239: 	$sectionClass='Class </h3>';
1.52      albertel 2240:     } elsif ($ENV{'form.section'} eq 'no') {
1.126     ng       2241: 	$sectionClass='Students in no Section </h3>';
1.52      albertel 2242:     } else {
1.126     ng       2243: 	$sectionClass='Students in Section '.$ENV{'form.section'}.'</h3>';
1.52      albertel 2244:     }
1.126     ng       2245:     $result.='<h3>Assign Common Grade To '.$sectionClass;
1.52      albertel 2246:     $result.= '<table border=0><tr><td bgcolor="#777777">'."\n".
                   2247: 	'<table border=0><tr bgcolor="#ffffdd"><td>';
1.44      ng       2248:     #radio buttons/text box for assigning points for a section or class.
                   2249:     #handles different parts of a problem
1.125     ng       2250:     my ($partlist,$handgrade) = &response_type($url,$symb);
1.42      ng       2251:     my %weight = ();
                   2252:     my $ctsparts = 0;
1.41      ng       2253:     $result.='<table border="0">';
1.45      ng       2254:     my %seen = ();
1.42      ng       2255:     for (sort keys(%$handgrade)) {
1.54      albertel 2256: 	my ($partid,$respid) = split (/_/,$_,2);
1.45      ng       2257: 	next if $seen{$partid};
                   2258: 	$seen{$partid}++;
1.147     albertel 2259: 	my $handgrade=$$handgrade{$_};
1.42      ng       2260: 	my $wgt = &Apache::lonnet::EXT('resource.'.$partid.'.weight',$symb);
                   2261: 	$weight{$partid} = $wgt eq '' ? '1' : $wgt;
                   2262: 
1.44      ng       2263: 	$result.='<input type="hidden" name="partid_'.
                   2264: 	    $ctsparts.'" value="'.$partid.'" />'."\n";
                   2265: 	$result.='<input type="hidden" name="weight_'.
                   2266: 	    $partid.'" value="'.$weight{$partid}.'" />'."\n";
                   2267: 	$result.='<tr><td><b>Part  '.$partid.'&nbsp; &nbsp;Point:</b> </td><td>';
1.42      ng       2268: 	$result.='<table border="0"><tr>';  
1.41      ng       2269: 	my $ctr = 0;
1.42      ng       2270: 	while ($ctr<=$weight{$partid}) { # display radio buttons in a nice table 10 across
                   2271: 	    $result.= '<td><input type="radio" name="RADVAL_'.$partid.'" '.
1.54      albertel 2272: 		'onclick="javascript:writePoint(\''.$partid.'\','.$weight{$partid}.
1.41      ng       2273: 		','.$ctr.')" />'.$ctr."</td>\n";
                   2274: 	    $result.=(($ctr+1)%10 == 0 ? '</tr><tr>' : '');
                   2275: 	    $ctr++;
                   2276: 	}
                   2277: 	$result.='</tr></table>';
1.44      ng       2278: 	$result.= '</td><td><b> or </b><input type="text" name="TEXTVAL_'.
1.54      albertel 2279: 	    $partid.'" size="4" '.'onChange="javascript:writePoint(\''.
                   2280: 		$partid.'\','.$weight{$partid}.',\'textval\')" /> /'.
1.42      ng       2281: 	    $weight{$partid}.' (problem weight)</td>'."\n";
                   2282: 	$result.= '</td><td><select name="SELVAL_'.$partid.'"'.
1.54      albertel 2283: 	    'onChange="javascript:writeRadText(\''.$partid.'\','.
1.59      albertel 2284: 		$weight{$partid}.')"> '.
1.42      ng       2285: 	    '<option selected="on"> </option>'.
1.125     ng       2286: 	    '<option>excused</option>'.
                   2287: 	    '<option>reset status</option></select></td></tr>'."\n";
1.42      ng       2288: 	$ctsparts++;
1.41      ng       2289:     }
1.52      albertel 2290:     $result.='</table>'.'</td></tr></table>'.'</td></tr></table>'."\n".
                   2291: 	'<input type="hidden" name="totalparts" value="'.$ctsparts.'" />';
1.42      ng       2292:     $result.='<input type="button" value="Reset" '.
1.111     ng       2293: 	'onClick="javascript:resetEntry('.$ctsparts.');" TARGET=_self>';
1.41      ng       2294: 
1.44      ng       2295:     #table listing all the students in a section/class
                   2296:     #header of table
1.126     ng       2297:     $result.= '<h3>Assign Grade to Specific Students in '.$sectionClass;
1.42      ng       2298:     $result.= '<table border=0><tr><td bgcolor="#777777">'."\n".
1.126     ng       2299: 	'<table border=0><tr bgcolor="#deffff"><td>&nbsp;<b>No.</b>&nbsp;</td>'.
1.129     ng       2300: 	'<td>'.&nameUserString('header')."</td>\n";
1.146     albertel 2301:     my (@parts) = sort(&getpartlist($url,$symb));
1.41      ng       2302:     foreach my $part (@parts) {
                   2303: 	my $display=&Apache::lonnet::metadata($url,$part.'.display');
1.126     ng       2304: 	$display =~ s|^Number of Attempts|Tries<br />|; # makes the column narrower
1.41      ng       2305: 	if  (!$display) { $display = &Apache::lonnet::metadata($url,$part.'.name'); }
                   2306: 	if ($display =~ /^Partial Credit Factor/) {
1.54      albertel 2307: 	    my ($partid) = &split_part_type($part);
1.53      albertel 2308: 	    $result.='<td><b>Score Part '.$partid.'<br />(weight = '.
1.42      ng       2309: 		$weight{$partid}.')</b></td>'."\n";
1.41      ng       2310: 	    next;
                   2311: 	}
1.53      albertel 2312: 	$display =~ s|Problem Status|Grade Status<br />|;
1.41      ng       2313: 	$result.='<td><b>'.$display.'</b></td>'."\n";
                   2314:     }
                   2315:     $result.='</tr>';
1.44      ng       2316: 
1.41      ng       2317:     #get info for each student
1.44      ng       2318:     #list all the students - with points and grade status
1.76      ng       2319:     my (undef,undef,$fullname) = &getclasslist($ENV{'form.section'},'1');
1.41      ng       2320:     my $ctr = 0;
1.53      albertel 2321:     foreach (sort {lc($$fullname{$a}) cmp lc($$fullname{$b}) } keys %$fullname) {
1.90      albertel 2322: 	my $uname = $_;
                   2323: 	$uname=~s/:/_/;
                   2324: 	$result.='<input type="hidden" name="ctr'.$ctr.'" value="'.$uname.'" />'."\n";
1.126     ng       2325: 	$ctr++;
1.41      ng       2326: 	$result.=&viewstudentgrade($url,$symb,$ENV{'request.course.id'},
1.126     ng       2327: 				   $_,$$fullname{$_},\@parts,\%weight,$ctr);
1.41      ng       2328:     }
                   2329:     $result.='</table></td></tr></table>';
                   2330:     $result.='<input type="hidden" name="total" value="'.$ctr.'" />'."\n";
1.126     ng       2331:     $result.='<input type="button" value="Save" '.
1.45      ng       2332: 	'onClick="javascript:submit();" TARGET=_self /></form>'."\n";
1.96      albertel 2333:     if (scalar(%$fullname) eq 0) {
                   2334: 	my $colspan=3+scalar(@parts);
1.116     ng       2335: 	$result='<font color="red">There are no students in section "'.$ENV{'form.section'}.
                   2336: 	    '" with enrollment status "'.$ENV{'form.Status'}.'" to modify or grade.</font>';
1.96      albertel 2337:     }
1.41      ng       2338:     $result.=&show_grading_menu_form($symb,$url);
                   2339:     return $result;
                   2340: }
                   2341: 
1.44      ng       2342: #--- call by previous routine to display each student
1.41      ng       2343: sub viewstudentgrade {
1.130     albertel 2344:     my ($url,$symb,$courseid,$student,$fullname,$parts,$weight,$ctr) = @_;
1.44      ng       2345:     my ($uname,$udom) = split(/:/,$student);
1.90      albertel 2346:     $student=~s/:/_/;
1.44      ng       2347:     my %record=&Apache::lonnet::restore($symb,$courseid,$udom,$uname);
1.126     ng       2348:     my $result='<tr bgcolor="#ffffdd"><td align="right">'.$ctr.'&nbsp;</td><td>&nbsp;'.
1.44      ng       2349: 	'<a href="javascript:viewOneStudent(\''.$uname.'\',\''.$udom.
1.112     ng       2350: 	'\')"; TARGET=_self>'.$fullname.'</a> '.
                   2351: 	'<font color="#999999">('.$uname.($ENV{'user.domain'} eq $udom ? '' : ':'.$udom).')</font></td>'."\n";
1.63      albertel 2352:     foreach my $apart (@$parts) {
                   2353: 	my ($part,$type) = &split_part_type($apart);
1.41      ng       2354: 	my $score=$record{"resource.$part.$type"};
                   2355: 	if ($type eq 'awarded') {
1.42      ng       2356: 	    my $pts = $score eq '' ? '' : $score*$$weight{$part};
                   2357: 	    $result.='<input type="hidden" name="'.
1.89      albertel 2358: 		'GD_'.$student.'_'.$part.'_awarded_s" value="'.$pts.'" />'."\n";
1.42      ng       2359: 	    $result.='<td align="middle"><input type="text" name="'.
1.89      albertel 2360: 		'GD_'.$student.'_'.$part.'_awarded" '.
                   2361: 		'onChange="javascript:changeSelect(\''.$part.'\',\''.$student.
1.44      ng       2362: 		'\')" value="'.$pts.'" size="4" /></td>'."\n";
1.41      ng       2363: 	} elsif ($type eq 'solved') {
                   2364: 	    my ($status,$foo)=split(/_/,$score,2);
                   2365: 	    $status = 'nothing' if ($status eq '');
1.89      albertel 2366: 	    $result.='<input type="hidden" name="'.'GD_'.$student.'_'.
1.54      albertel 2367: 		$part.'_solved_s" value="'.$status.'" />'."\n";
1.126     ng       2368: 	    $result.='<td align="middle">&nbsp;<select name="'.
1.89      albertel 2369: 		'GD_'.$student.'_'.$part.'_solved" '.
                   2370: 		'onChange="javascript:changeOneScore(\''.$part.'\',\''.$student.'\')" >'."\n";
1.125     ng       2371: 	    $result.= (($status eq 'excused') ? '<option> </option><option selected="on">excused</option>' 
                   2372: 		: '<option selected="on"> </option><option>excused</option>')."\n";
                   2373: 	    $result.='<option>reset status</option>';
1.126     ng       2374: 	    $result.="</select>&nbsp;</td>\n";
1.122     ng       2375: 	} else {
                   2376: 	    $result.='<input type="hidden" name="'.
                   2377: 		'GD_'.$student.'_'.$part.'_'.$type.'_s" value="'.$score.'" />'.
                   2378: 		    "\n";
                   2379: 	    $result.='<td align="middle"><input type="text" name="'.
                   2380: 		'GD_'.$student.'_'.$part.'_'.$type.'" '.
                   2381: 		'value="'.$score.'" size="4" /></td>'."\n";
1.41      ng       2382: 	}
                   2383:     }
                   2384:     $result.='</tr>';
                   2385:     return $result;
1.38      ng       2386: }
                   2387: 
1.44      ng       2388: #--- change scores for all the students in a section/class
                   2389: #    record does not get update if unchanged
1.38      ng       2390: sub editgrades {
1.41      ng       2391:     my ($request) = @_;
                   2392: 
                   2393:     my $symb=$ENV{'form.symb'};
1.43      ng       2394:     my $url =$ENV{'form.url'};
1.45      ng       2395:     my $title='<h3><font color="#339933">Current Grade Status</font></h3>';
1.118     ng       2396:     $title.='<font size=+1><b>Current Resource: </b>'.$ENV{'form.probTitle'}.'</font><br />'."\n";
1.44      ng       2397:     $title.='<font size=+1><b>Section: </b>'.$ENV{'form.section'}.'</font>'."\n";
1.126     ng       2398: 
1.44      ng       2399:     my $result= '<table border="0"><tr><td bgcolor="#777777">'."\n";
1.129     ng       2400:     $result.= '<table border="0"><tr bgcolor="#deffff">'.
                   2401: 	'<td rowspan=2 valign="center">&nbsp;<b>No.</b>&nbsp;</td>'.
                   2402: 	'<td rowspan=2 valign="center">'.&nameUserString('header')."</td>\n";
1.43      ng       2403: 
                   2404:     my %scoreptr = (
                   2405: 		    'correct'  =>'correct_by_override',
                   2406: 		    'incorrect'=>'incorrect_by_override',
                   2407: 		    'excused'  =>'excused',
                   2408: 		    'ungraded' =>'ungraded_attempted',
                   2409: 		    'nothing'  => '',
                   2410: 		    );
1.56      matthew  2411:     my ($classlist,undef,$fullname) = &getclasslist($ENV{'form.section'},'0');
1.34      ng       2412: 
1.44      ng       2413:     my (@partid);
                   2414:     my %weight = ();
1.54      albertel 2415:     my %columns = ();
1.44      ng       2416:     my ($i,$ctr,$count,$rec_update) = (0,0,0,0);
1.54      albertel 2417: 
1.146     albertel 2418:     my (@parts) = sort(&getpartlist($url,$symb));
1.54      albertel 2419:     my $header;
1.44      ng       2420:     while ($ctr < $ENV{'form.totalparts'}) {
                   2421: 	my $partid = $ENV{'form.partid_'.$ctr};
                   2422: 	push @partid,$partid;
                   2423: 	$weight{$partid} = $ENV{'form.weight_'.$partid};
                   2424: 	$ctr++;
1.54      albertel 2425:     }
                   2426:     foreach my $partid (@partid) {
                   2427: 	$header .= '<td align="center">&nbsp;<b>Old Score</b>&nbsp;</td>'.
                   2428: 	    '<td align="center">&nbsp;<b>New Score</b>&nbsp;</td>';
                   2429: 	$columns{$partid}=2;
                   2430: 	foreach my $stores (@parts) {
                   2431: 	    my ($part,$type) = &split_part_type($stores);
                   2432: 	    if ($part !~ m/^\Q$partid\E/) { next;}
                   2433: 	    if ($type eq 'awarded' || $type eq 'solved') { next; }
                   2434: 	    my $display=&Apache::lonnet::metadata($url,$stores.'.display');
                   2435: 	    $display =~ s/\[Part: (\w)+\]//;
1.125     ng       2436: 	    $display =~ s/Number of Attempts/Tries/;
                   2437: 	    $header .= '<td align="center">&nbsp;<b>Old '.$display.'</b>&nbsp;</td>'.
                   2438: 		'<td align="center">&nbsp;<b>New '.$display.'</b>&nbsp;</td>';
1.54      albertel 2439: 	    $columns{$partid}+=2;
                   2440: 	}
                   2441:     }
                   2442:     foreach my $partid (@partid) {
                   2443: 	$result .= '<td colspan="'.$columns{$partid}.
                   2444: 	    '" align="center"><b>Part '.$partid.
1.44      ng       2445: 	    '</b> (Weight = '.$weight{$partid}.')</td>';
1.54      albertel 2446: 
1.44      ng       2447:     }
                   2448:     $result .= '</tr><tr bgcolor="#deffff">';
1.54      albertel 2449:     $result .= $header;
1.44      ng       2450:     $result .= '</tr>'."\n";
1.93      albertel 2451:     my $noupdate;
1.126     ng       2452:     my ($updateCtr,$noupdateCtr) = (1,1);
1.44      ng       2453:     for ($i=0; $i<$ENV{'form.total'}; $i++) {
1.93      albertel 2454: 	my $line;
1.44      ng       2455: 	my $user = $ENV{'form.ctr'.$i};
1.92      albertel 2456: 	my $usercolon = $user;
                   2457: 	$usercolon =~s/_/:/;
                   2458: 	my ($uname,$udom)=split(/_/,$user);
1.44      ng       2459: 	my %newrecord;
                   2460: 	my $updateflag = 0;
1.129     ng       2461: 	$line .= '<td>'.&nameUserString(undef,$$fullname{$usercolon},$uname,$udom).'</td>';
1.108     albertel 2462: 	my $usec=$classlist->{"$uname:$udom"}[5];
1.105     albertel 2463: 	if (!&canmodify($usec)) {
1.126     ng       2464: 	    my $numcols=scalar(@partid)*4+2;
1.105     albertel 2465: 	    $noupdate.=$line."<td colspan=\"$numcols\"><font color=\"red\">Not allowed to modify student</font></td></tr>";
                   2466: 	    next;
                   2467: 	}
1.44      ng       2468: 	foreach (@partid) {
1.54      albertel 2469: 	    my $old_aw    = $ENV{'form.GD_'.$user.'_'.$_.'_awarded_s'};
                   2470: 	    my $old_part_pcr = $old_aw/($weight{$_} ne '0' ? $weight{$_}:1);
                   2471: 	    my $old_part  = $old_aw eq '' ? '' : $old_part_pcr;
                   2472: 	    my $old_score = $scoreptr{$ENV{'form.GD_'.$user.'_'.$_.'_solved_s'}};
                   2473: 
                   2474: 	    my $awarded   = $ENV{'form.GD_'.$user.'_'.$_.'_awarded'};
                   2475: 	    my $pcr       = $awarded/($weight{$_} ne '0' ? $weight{$_} : 1);
                   2476: 	    my $partial   = $awarded eq '' ? '' : $pcr;
1.44      ng       2477: 	    my $score;
                   2478: 	    if ($partial eq '') {
1.54      albertel 2479: 		$score = $scoreptr{$ENV{'form.GD_'.$user.'_'.$_.'_solved_s'}};
1.44      ng       2480: 	    } elsif ($partial > 0) {
                   2481: 		$score = 'correct_by_override';
                   2482: 	    } elsif ($partial == 0) {
                   2483: 		$score = 'incorrect_by_override';
                   2484: 	    }
1.125     ng       2485: 	    my $dropMenu = $ENV{'form.GD_'.$user.'_'.$_.'_solved'};
                   2486: 	    $score = 'excused' if (($dropMenu eq 'excused') && ($score ne 'excused'));
                   2487: 
                   2488: 	    if ($dropMenu eq 'reset status' &&
                   2489: 		$old_score ne '') { # ignore if no previous attempts => nothing to reset
                   2490: 		$newrecord{'resource.'.$_.'.tries'} = 0;
                   2491: 		$newrecord{'resource.'.$_.'.solved'} = '';
                   2492: 		$newrecord{'resource.'.$_.'.award'} = '';
                   2493: 		$newrecord{'resource.'.$_.'.awarded'} = 0;
                   2494: 		$newrecord{'resource.'.$_.'.regrader'}="$ENV{'user.name'}:$ENV{'user.domain'}";
                   2495: 		$updateflag = 1;
1.139     albertel 2496: 	    } elsif (!($old_part eq $partial && $old_score eq $score)) {
                   2497: 		$updateflag = 1;
                   2498: 		$newrecord{'resource.'.$_.'.awarded'}  = $partial if $partial ne '';
                   2499: 		$newrecord{'resource.'.$_.'.solved'}   = $score;
                   2500: 		$rec_update++;
1.125     ng       2501: 	    }
                   2502: 
1.93      albertel 2503: 	    $line .= '<td align="center">'.$old_aw.'&nbsp;</td>'.
1.44      ng       2504: 		'<td align="center">'.$awarded.
                   2505: 		($score eq 'excused' ? $score : '').'&nbsp;</td>';
1.5       albertel 2506: 
1.54      albertel 2507: 
                   2508: 	    my $partid=$_;
                   2509: 	    foreach my $stores (@parts) {
                   2510: 		my ($part,$type) = &split_part_type($stores);
                   2511: 		if ($part !~ m/^\Q$partid\E/) { next;}
                   2512: 		if ($type eq 'awarded' || $type eq 'solved') { next; }
                   2513: 		my $old_aw    = $ENV{'form.GD_'.$user.'_'.$part.'_'.$type.'_s'};
                   2514: 		my $awarded   = $ENV{'form.GD_'.$user.'_'.$part.'_'.$type};
                   2515: 		if ($awarded ne '' && $awarded ne $old_aw) {
                   2516: 		    $newrecord{'resource.'.$part.'.'.$type}= $awarded;
1.122     ng       2517: 		    $newrecord{'resource.'.$part.'.regrader'}="$ENV{'user.name'}:$ENV{'user.domain'}";
1.54      albertel 2518: 		    $updateflag=1;
                   2519: 		}
1.93      albertel 2520: 		$line .= '<td align="center">'.$old_aw.'&nbsp;</td>'.
1.54      albertel 2521: 		    '<td align="center">'.$awarded.'&nbsp;</td>';
                   2522: 	    }
1.44      ng       2523: 	}
1.93      albertel 2524: 	$line.='</tr>'."\n";
1.44      ng       2525: 	if ($updateflag) {
                   2526: 	    $count++;
                   2527: 	    &Apache::lonnet::cstore(\%newrecord,$symb,$ENV{'request.course.id'},
1.89      albertel 2528: 				    $udom,$uname);
1.126     ng       2529: 	    $result.='<tr bgcolor="#ffffde"><td align="right">&nbsp;'.$updateCtr.'&nbsp;</td>'.$line;
                   2530: 	    $updateCtr++;
1.93      albertel 2531: 	} else {
1.126     ng       2532: 	    $noupdate.='<tr bgcolor="#ffffde"><td align="right">&nbsp;'.$noupdateCtr.'&nbsp;</td>'.$line;
                   2533: 	    $noupdateCtr++;
1.44      ng       2534: 	}
1.93      albertel 2535:     }
                   2536:     if ($noupdate) {
1.126     ng       2537: #	my $numcols=(scalar(@partid)*(scalar(@parts)-1)*2)+3;
                   2538: 	my $numcols=scalar(@partid)*4+2;
                   2539: 	$result .= '<tr bgcolor="#ffffff"><td align="center" colspan="'.$numcols.'">No Changes Occurred For the Students Below</td></tr>'.$noupdate;
1.44      ng       2540:     }
1.72      ng       2541:     $result .= '</table></td></tr></table>'."\n".
                   2542: 	&show_grading_menu_form ($symb,$url);
1.125     ng       2543:     my $msg = '<br /><b>Number of records updated = '.$rec_update.
1.44      ng       2544: 	' for '.$count.' student'.($count <= 1 ? '' : 's').'.</b><br />'.
                   2545: 	'<b>Total number of students = '.$ENV{'form.total'}.'</b><br />';
                   2546:     return $title.$msg.$result;
1.5       albertel 2547: }
1.54      albertel 2548: 
                   2549: sub split_part_type {
                   2550:     my ($partstr) = @_;
                   2551:     my ($temp,@allparts)=split(/_/,$partstr);
                   2552:     my $type=pop(@allparts);
                   2553:     my $part=join('.',@allparts);
                   2554:     return ($part,$type);
                   2555: }
                   2556: 
1.44      ng       2557: #------------- end of section for handling grading by section/class ---------
                   2558: #
                   2559: #----------------------------------------------------------------------------
                   2560: 
1.5       albertel 2561: 
1.44      ng       2562: #----------------------------------------------------------------------------
                   2563: #
                   2564: #-------------------------- Next few routines handles grading by csv upload
                   2565: #
                   2566: #--- Javascript to handle csv upload
1.27      albertel 2567: sub csvupload_javascript_reverse_associate {
                   2568:   return(<<ENDPICK);
                   2569:   function verify(vf) {
                   2570:     var foundsomething=0;
                   2571:     var founduname=0;
                   2572:     var founddomain=0;
                   2573:     for (i=0;i<=vf.nfields.value;i++) {
                   2574:       tw=eval('vf.f'+i+'.selectedIndex');
                   2575:       if (i==0 && tw!=0) { founduname=1; }
                   2576:       if (i==1 && tw!=0) { founddomain=1; }
                   2577:       if (i!=0 && i!=1 && tw!=0) { foundsomething=1; }
                   2578:     }
                   2579:     if (founduname==0 || founddomain==0) {
                   2580:       alert('You need to specify at both the username and domain');
                   2581:       return;
                   2582:     }
                   2583:     if (foundsomething==0) {
                   2584:       alert('You need to specify at least one grading field');
                   2585:       return;
                   2586:     }
                   2587:     vf.submit();
                   2588:   }
                   2589:   function flip(vf,tf) {
                   2590:     var nw=eval('vf.f'+tf+'.selectedIndex');
                   2591:     var i;
                   2592:     for (i=0;i<=vf.nfields.value;i++) {
                   2593:       //can not pick the same destination field for both name and domain
                   2594:       if (((i ==0)||(i ==1)) && 
                   2595:           ((tf==0)||(tf==1)) && 
                   2596:           (i!=tf) &&
                   2597:           (eval('vf.f'+i+'.selectedIndex')==nw)) {
                   2598:         eval('vf.f'+i+'.selectedIndex=0;')
                   2599:       }
                   2600:     }
                   2601:   }
                   2602: ENDPICK
                   2603: }
                   2604: 
                   2605: sub csvupload_javascript_forward_associate {
                   2606:   return(<<ENDPICK);
                   2607:   function verify(vf) {
                   2608:     var foundsomething=0;
                   2609:     var founduname=0;
                   2610:     var founddomain=0;
                   2611:     for (i=0;i<=vf.nfields.value;i++) {
                   2612:       tw=eval('vf.f'+i+'.selectedIndex');
                   2613:       if (tw==1) { founduname=1; }
                   2614:       if (tw==2) { founddomain=1; }
                   2615:       if (tw>2) { foundsomething=1; }
                   2616:     }
                   2617:     if (founduname==0 || founddomain==0) {
                   2618:       alert('You need to specify at both the username and domain');
                   2619:       return;
                   2620:     }
                   2621:     if (foundsomething==0) {
                   2622:       alert('You need to specify at least one grading field');
                   2623:       return;
                   2624:     }
                   2625:     vf.submit();
                   2626:   }
                   2627:   function flip(vf,tf) {
                   2628:     var nw=eval('vf.f'+tf+'.selectedIndex');
                   2629:     var i;
                   2630:     //can not pick the same destination field twice
                   2631:     for (i=0;i<=vf.nfields.value;i++) {
                   2632:       if ((i!=tf) && (eval('vf.f'+i+'.selectedIndex')==nw)) {
                   2633:         eval('vf.f'+i+'.selectedIndex=0;')
                   2634:       }
                   2635:     }
                   2636:   }
                   2637: ENDPICK
                   2638: }
                   2639: 
1.26      albertel 2640: sub csvuploadmap_header {
1.41      ng       2641:     my ($request,$symb,$url,$datatoken,$distotal)= @_;
                   2642:     my $javascript;
                   2643:     if ($ENV{'form.upfile_associate'} eq 'reverse') {
                   2644: 	$javascript=&csvupload_javascript_reverse_associate();
                   2645:     } else {
                   2646: 	$javascript=&csvupload_javascript_forward_associate();
                   2647:     }
1.45      ng       2648: 
1.122     ng       2649:     my ($result) = &showResourceInfo($url,$ENV{'form.probTitle'});
1.118     ng       2650: 
1.41      ng       2651:     $request->print(<<ENDPICK);
1.26      albertel 2652: <form method="post" enctype="multipart/form-data" action="/adm/grades" name="gradesupload">
1.45      ng       2653: <h3><font color="#339933">Uploading Class Grades</font></h3>
                   2654: $result
1.26      albertel 2655: <hr>
                   2656: <h3>Identify fields</h3>
                   2657: Total number of records found in file: $distotal <hr />
                   2658: Enter as many fields as you can. The system will inform you and bring you back
                   2659: to this page if the data selected is insufficient to run your class.<hr />
                   2660: <input type="button" value="Reverse Association" onClick="javascript:this.form.associate.value='Reverse Association';submit(this.form);" />
                   2661: <input type="hidden" name="associate"  value="" />
                   2662: <input type="hidden" name="phase"      value="three" />
                   2663: <input type="hidden" name="datatoken"  value="$datatoken" />
                   2664: <input type="hidden" name="fileupload" value="$ENV{'form.fileupload'}" />
                   2665: <input type="hidden" name="upfiletype" value="$ENV{'form.upfiletype'}" />
                   2666: <input type="hidden" name="upfile_associate" 
                   2667:                                        value="$ENV{'form.upfile_associate'}" />
                   2668: <input type="hidden" name="symb"       value="$symb" />
                   2669: <input type="hidden" name="url"        value="$url" />
1.77      ng       2670: <input type="hidden" name="saveState"  value="$ENV{'form.saveState'}" />
1.72      ng       2671: <input type="hidden" name="probTitle"  value="$ENV{'form.probTitle'}" />
1.26      albertel 2672: <input type="hidden" name="command"    value="csvuploadassign" />
                   2673: <hr />
                   2674: <script type="text/javascript" language="Javascript">
                   2675: $javascript
                   2676: </script>
                   2677: ENDPICK
1.118     ng       2678:     return '';
1.26      albertel 2679: 
                   2680: }
                   2681: 
                   2682: sub csvupload_fields {
1.146     albertel 2683:     my ($url,$symb) = @_;
                   2684:     my (@parts) = &getpartlist($url,$symb);
1.41      ng       2685:     my @fields=(['username','Student Username'],['domain','Student Domain']);
                   2686:     foreach my $part (sort(@parts)) {
                   2687: 	my @datum;
                   2688: 	my $display=&Apache::lonnet::metadata($url,$part.'.display');
                   2689: 	my $name=$part;
                   2690: 	if  (!$display) { $display = $name; }
                   2691: 	@datum=($name,$display);
                   2692: 	push(@fields,\@datum);
                   2693:     }
                   2694:     return (@fields);
1.26      albertel 2695: }
                   2696: 
                   2697: sub csvuploadmap_footer {
1.41      ng       2698:     my ($request,$i,$keyfields) =@_;
                   2699:     $request->print(<<ENDPICK);
1.26      albertel 2700: </table>
                   2701: <input type="hidden" name="nfields" value="$i" />
                   2702: <input type="hidden" name="keyfields" value="$keyfields" />
                   2703: <input type="button" onClick="javascript:verify(this.form)" value="Assign Grades" /><br />
                   2704: </form>
                   2705: ENDPICK
                   2706: }
                   2707: 
1.86      ng       2708: sub upcsvScores_form {
                   2709:     my ($request) = shift;
                   2710:     my ($symb,$url)=&get_symb_and_url($request);
                   2711:     if (!$symb) {return '';}
                   2712:     my $result =<<CSVFORMJS;
                   2713: <script type="text/javascript" language="javascript">
                   2714:     function checkUpload(formname) {
                   2715: 	if (formname.upfile.value == "") {
                   2716: 	    alert("Please use the browse button to select a file from your local directory.");
                   2717: 	    return false;
                   2718: 	}
                   2719: 	formname.submit();
                   2720:     }
                   2721:     </script>
                   2722: CSVFORMJS
                   2723:     $ENV{'form.probTitle'} = &Apache::lonnet::gettitle($symb);
1.118     ng       2724:     my ($table) = &showResourceInfo($url,$ENV{'form.probTitle'});
                   2725:     $result.=$table;
1.86      ng       2726:     $result.='<br /><table width=100% border=0><tr><td bgcolor="#777777">'."\n";
                   2727:     $result.='<table width=100% border=0><tr bgcolor="#e6ffff"><td>'."\n";
1.118     ng       2728:     $result.='&nbsp;<b>Specify a file containing the class scores for current resource'.
1.86      ng       2729: 	'.</b></td></tr>'."\n";
                   2730:     $result.='<tr bgcolor=#ffffe6><td>'."\n";
                   2731:     my $upfile_select=&Apache::loncommon::upfile_select_html();
                   2732:     $result.=<<ENDUPFORM;
1.106     albertel 2733: <form method="post" enctype="multipart/form-data" action="/adm/grades" name="gradesupload">
1.86      ng       2734: <input type="hidden" name="symb" value="$symb" />
                   2735: <input type="hidden" name="url" value="$url" />
                   2736: <input type="hidden" name="command" value="csvuploadmap" />
                   2737: <input type="hidden" name="probTitle" value="$ENV{'form.probTitle'}" />
                   2738: <input type="hidden" name="saveState"  value="$ENV{'form.saveState'}" />
                   2739: $upfile_select
                   2740: <br /><input type="button" onClick="javascript:checkUpload(this.form);" value="Upload Scores" />
                   2741: 
                   2742: </form>
                   2743: ENDUPFORM
                   2744:     $result.='</td></tr></table>'."\n";
                   2745:     $result.='</td></tr></table><br /><br />'."\n";
                   2746:     $result.=&show_grading_menu_form($symb,$url);
                   2747:     return $result;
                   2748: }
                   2749: 
                   2750: 
1.26      albertel 2751: sub csvuploadmap {
1.41      ng       2752:     my ($request)= @_;
                   2753:     my ($symb,$url)=&get_symb_and_url($request);
                   2754:     if (!$symb) {return '';}
1.72      ng       2755: 
1.41      ng       2756:     my $datatoken;
                   2757:     if (!$ENV{'form.datatoken'}) {
                   2758: 	$datatoken=&Apache::loncommon::upfile_store($request);
1.26      albertel 2759:     } else {
1.41      ng       2760: 	$datatoken=$ENV{'form.datatoken'};
                   2761: 	&Apache::loncommon::load_tmp_file($request);
1.26      albertel 2762:     }
1.41      ng       2763:     my @records=&Apache::loncommon::upfile_record_sep();
                   2764:     &csvuploadmap_header($request,$symb,$url,$datatoken,$#records+1);
                   2765:     my ($i,$keyfields);
                   2766:     if (@records) {
1.146     albertel 2767: 	my @fields=&csvupload_fields($url,$symb);
1.45      ng       2768: 
1.41      ng       2769: 	if ($ENV{'form.upfile_associate'} eq 'reverse') {	
                   2770: 	    &Apache::loncommon::csv_print_samples($request,\@records);
                   2771: 	    $i=&Apache::loncommon::csv_print_select_table($request,\@records,
                   2772: 							  \@fields);
                   2773: 	    foreach (@fields) { $keyfields.=$_->[0].','; }
                   2774: 	    chop($keyfields);
                   2775: 	} else {
                   2776: 	    unshift(@fields,['none','']);
                   2777: 	    $i=&Apache::loncommon::csv_samples_select_table($request,\@records,
                   2778: 							    \@fields);
                   2779: 	    my %sone=&Apache::loncommon::record_sep($records[0]);
                   2780: 	    $keyfields=join(',',sort(keys(%sone)));
                   2781: 	}
                   2782:     }
                   2783:     &csvuploadmap_footer($request,$i,$keyfields);
1.72      ng       2784:     $request->print(&show_grading_menu_form($symb,$url));
                   2785: 
1.41      ng       2786:     return '';
1.27      albertel 2787: }
                   2788: 
                   2789: sub csvuploadassign {
1.41      ng       2790:     my ($request)= @_;
                   2791:     my ($symb,$url)=&get_symb_and_url($request);
                   2792:     if (!$symb) {return '';}
                   2793:     &Apache::loncommon::load_tmp_file($request);
1.44      ng       2794:     my @gradedata = &Apache::loncommon::upfile_record_sep();
1.41      ng       2795:     my @keyfields = split(/\,/,$ENV{'form.keyfields'});
                   2796:     my %fields=();
                   2797:     for (my $i=0; $i<=$ENV{'form.nfields'}; $i++) {
                   2798: 	if ($ENV{'form.upfile_associate'} eq 'reverse') {
                   2799: 	    if ($ENV{'form.f'.$i} ne 'none') {
                   2800: 		$fields{$keyfields[$i]}=$ENV{'form.f'.$i};
                   2801: 	    }
                   2802: 	} else {
                   2803: 	    if ($ENV{'form.f'.$i} ne 'none') {
                   2804: 		$fields{$ENV{'form.f'.$i}}=$keyfields[$i];
                   2805: 	    }
                   2806: 	}
1.27      albertel 2807:     }
1.41      ng       2808:     $request->print('<h3>Assigning Grades</h3>');
                   2809:     my $courseid=$ENV{'request.course.id'};
1.97      albertel 2810:     my ($classlist) = &getclasslist('all',0);
1.106     albertel 2811:     my @notallowed;
1.41      ng       2812:     my @skipped;
                   2813:     my $countdone=0;
                   2814:     foreach my $grade (@gradedata) {
                   2815: 	my %entries=&Apache::loncommon::record_sep($grade);
                   2816: 	my $username=$entries{$fields{'username'}};
                   2817: 	my $domain=$entries{$fields{'domain'}};
                   2818: 	if (!exists($$classlist{"$username:$domain"})) {
                   2819: 	    push(@skipped,"$username:$domain");
                   2820: 	    next;
                   2821: 	}
1.108     albertel 2822: 	my $usec=$classlist->{"$username:$domain"}[5];
1.106     albertel 2823: 	if (!&canmodify($usec)) {
                   2824: 	    push(@notallowed,"$username:$domain");
                   2825: 	    next;
                   2826: 	}
1.41      ng       2827: 	my %grades;
                   2828: 	foreach my $dest (keys(%fields)) {
                   2829: 	    if ($dest eq 'username' || $dest eq 'domain') { next; }
                   2830: 	    if ($entries{$fields{$dest}} eq '') { next; }
                   2831: 	    my $store_key=$dest;
                   2832: 	    $store_key=~s/^stores/resource/;
                   2833: 	    $store_key=~s/_/\./g;
                   2834: 	    $grades{$store_key}=$entries{$fields{$dest}};
                   2835: 	}
                   2836: 	$grades{"resource.regrader"}="$ENV{'user.name'}:$ENV{'user.domain'}";
                   2837: 	&Apache::lonnet::cstore(\%grades,$symb,$ENV{'request.course.id'},
                   2838: 				$domain,$username);
                   2839: 	$request->print('.');
                   2840: 	$request->rflush();
                   2841: 	$countdone++;
                   2842:     }
                   2843:     $request->print("<br />Stored $countdone students\n");
                   2844:     if (@skipped) {
1.106     albertel 2845: 	$request->print('<p<font size="+1"><b>Skipped Students</b></font></p>');
                   2846: 	foreach my $student (@skipped) { $request->print("$student<br />\n"); }
                   2847:     }
                   2848:     if (@notallowed) {
                   2849: 	$request->print('<p><font size="+1" color="red"><b>Students Not Allowed to Modify</b></font></p>');
                   2850: 	foreach my $student (@notallowed) { $request->print("$student<br />\n"); }
1.41      ng       2851:     }
1.106     albertel 2852:     $request->print("<br />\n");
1.41      ng       2853:     $request->print(&show_grading_menu_form($symb,$url));
                   2854:     return '';
1.26      albertel 2855: }
1.44      ng       2856: #------------- end of section for handling csv file upload ---------
                   2857: #
                   2858: #-------------------------------------------------------------------
                   2859: #
1.122     ng       2860: #-------------- Next few routines handle grading by page/sequence
1.72      ng       2861: #
                   2862: #--- Select a page/sequence and a student to grade
1.68      ng       2863: sub pickStudentPage {
                   2864:     my ($request) = shift;
                   2865: 
                   2866:     $request->print(<<LISTJAVASCRIPT);
                   2867: <script type="text/javascript" language="javascript">
                   2868: 
                   2869: function checkPickOne(formname) {
1.76      ng       2870:     if (radioSelection(formname.student) == null) {
1.68      ng       2871: 	alert("Please select the student you wish to grade.");
                   2872: 	return;
                   2873:     }
1.125     ng       2874:     ptr = pullDownSelection(formname.selectpage);
                   2875:     formname.page.value = formname["page"+ptr].value;
                   2876:     formname.title.value = formname["title"+ptr].value;
1.68      ng       2877:     formname.submit();
                   2878: }
                   2879: 
                   2880: </script>
                   2881: LISTJAVASCRIPT
1.118     ng       2882:     &commonJSfunctions($request);
1.72      ng       2883:     my ($symb,$url) = &get_symb_and_url($request);
1.68      ng       2884:     my $cdom      = $ENV{"course.$ENV{'request.course.id'}.domain"};
                   2885:     my $cnum      = $ENV{"course.$ENV{'request.course.id'}.num"};
                   2886:     my $getsec    = $ENV{'form.section'} eq '' ? 'all' : $ENV{'form.section'};
                   2887: 
                   2888:     my $result='<h3><font color="#339933">&nbsp;'.
                   2889: 	'Manual Grading by Page or Sequence</font></h3>';
                   2890: 
1.80      ng       2891:     $result.='<form action="/adm/grades" method="post" name="displayPage">'."\n";
1.70      ng       2892:     $result.='&nbsp;<b>Problems from:</b> <select name="selectpage">'."\n";
1.74      albertel 2893:     my ($titles,$symbx) = &getSymbMap($request);
1.137     albertel 2894:     my ($curpage) =&Apache::lonnet::decode_symb($symb); 
                   2895: #    my ($curpage,$mapId) =&Apache::lonnet::decode_symb($symb); 
                   2896: #    my $type=($curpage =~ /\.(page|sequence)/);
1.70      ng       2897:     my $ctr=0;
1.68      ng       2898:     foreach (@$titles) {
                   2899: 	my ($minder,$showtitle) = ($_ =~ /(\d+)\.(.*)/);
1.70      ng       2900: 	$result.='<option value="'.$ctr.'" '.
1.71      ng       2901: 	    ($$symbx{$_} =~ /$curpage$/ ? 'selected="on"' : '').
                   2902: 	    '>'.$showtitle.'</option>'."\n";
1.70      ng       2903: 	$ctr++;
1.68      ng       2904:     }
                   2905:     $result.= '</select>'."<br>\n";
1.70      ng       2906:     $ctr=0;
                   2907:     foreach (@$titles) {
                   2908: 	my ($minder,$showtitle) = ($_ =~ /(\d+)\.(.*)/);
                   2909: 	$result.='<input type="hidden" name="page'.$ctr.'" value="'.$$symbx{$_}.'" />'."\n";
                   2910: 	$result.='<input type="hidden" name="title'.$ctr.'" value="'.$showtitle.'" />'."\n";
                   2911: 	$ctr++;
                   2912:     }
1.72      ng       2913:     $result.='<input type="hidden" name="page" />'."\n".
                   2914: 	'<input type="hidden" name="title" />'."\n";
1.68      ng       2915: 
1.144     albertel 2916:     $result.='&nbsp;<b>View Problems Text: </b><input type="radio" name="vProb" value="no" checked="on" /> no '."\n".
1.71      ng       2917: 	'<input type="radio" name="vProb" value="yes" /> yes '."<br>\n";
1.72      ng       2918: 
1.71      ng       2919:     $result.='&nbsp;<b>Submission Details: </b>'.
                   2920: 	'<input type="radio" name="lastSub" value="none" /> none'."\n".
1.122     ng       2921: 	'<input type="radio" name="lastSub" value="datesub" checked /> by dates and submissions'."\n".
1.71      ng       2922: 	'<input type="radio" name="lastSub" value="all" /> all details'."\n";
1.72      ng       2923: 
1.68      ng       2924:     $result.='<input type="hidden" name="section"     value="'.$getsec.'" />'."\n".
1.118     ng       2925: 	'<input type="hidden" name="Status"  value="'.$ENV{'form.Status'}.'" />'."\n".
1.72      ng       2926: 	'<input type="hidden" name="command" value="displayPage" />'."\n".
                   2927: 	'<input type="hidden" name="url"     value="'.$url.'" />'."\n".
1.80      ng       2928: 	'<input type="hidden" name="symb"    value="'.$symb.'" />'."\n".
                   2929: 	'<input type="hidden" name="saveState" value="'.$ENV{'form.saveState'}.'" />'."<br />\n";
1.72      ng       2930: 
1.80      ng       2931:     $result.='&nbsp;<input type="button" '.
1.126     ng       2932: 	'onClick="javascript:checkPickOne(this.form);"value="Next->" /><br />'."\n";
1.72      ng       2933: 
1.68      ng       2934:     $request->print($result);
                   2935: 
1.126     ng       2936:     my $studentTable.='&nbsp;<b>Select a student you wish to grade and then click on the Next button.</b><br>'.
1.68      ng       2937: 	'<table border="0"><tr><td bgcolor="#777777">'.
                   2938: 	'<table border="0"><tr bgcolor="#e6ffff">'.
1.126     ng       2939: 	'<td align="right">&nbsp;<b>No.</b></td>'.
1.129     ng       2940: 	'<td>'.&nameUserString('header').'</td>'.
1.126     ng       2941: 	'<td align="right">&nbsp;<b>No.</b></td>'.
1.129     ng       2942: 	'<td>'.&nameUserString('header').'</td></tr>';
1.68      ng       2943:  
1.76      ng       2944:     my (undef,undef,$fullname) = &getclasslist($getsec,'1');
1.68      ng       2945:     my $ptr = 1;
                   2946:     foreach my $student (sort {lc($$fullname{$a}) cmp lc($$fullname{$b}) } keys %$fullname) {
                   2947: 	my ($uname,$udom) = split(/:/,$student);
1.126     ng       2948: 	$studentTable.=($ptr%2 == 1 ? '<tr bgcolor="#ffffe6">' : '</td>');
                   2949: 	$studentTable.='<td align="right">'.$ptr.'&nbsp;</td>';
1.129     ng       2950: 	$studentTable.='<td>&nbsp;<input type="radio" name="student" value="'.$student.'" /> '
                   2951: 	    .&nameUserString(undef,$$fullname{$student},$uname,$udom)."\n";
1.126     ng       2952: 	$studentTable.=($ptr%2 == 0 ? '</td></tr>' : '');
1.68      ng       2953: 	$ptr++;
                   2954:     }
1.126     ng       2955:     $studentTable.='</td><td>&nbsp;</td><td>&nbsp;' if ($ptr%2 == 0);
1.68      ng       2956:     $studentTable.='</td></tr></table></td></tr></table>'."\n";
1.126     ng       2957:     $studentTable.='<input type="button" '.
                   2958: 	'onClick="javascript:checkPickOne(this.form);"value="Next->" /></form>'."\n";
1.68      ng       2959: 
                   2960:     $studentTable.=&show_grading_menu_form($symb,$url);
                   2961:     $request->print($studentTable);
                   2962: 
                   2963:     return '';
                   2964: }
                   2965: 
                   2966: sub getSymbMap {
1.74      albertel 2967:     my ($request) = @_;
1.132     bowersj2 2968:     my $navmap = Apache::lonnavmaps::navmap->new();
1.68      ng       2969: 
                   2970:     my %symbx = ();
                   2971:     my @titles = ();
1.117     bowersj2 2972:     my $minder = 0;
                   2973: 
                   2974:     # Gather every sequence that has problems.
                   2975:     my @sequences = $navmap->retrieveResources(undef, sub { shift->is_map(); }, 1);
                   2976:     for my $sequence ($navmap->getById('0.0'), @sequences) {
                   2977: 	if ($navmap->hasResource($sequence, sub { shift->is_problem(); }, 0) ) {
                   2978: 	    my $title = $minder.'.'.$sequence->compTitle();
                   2979: 	    push @titles, $title; # minder in case two titles are identical
                   2980: 	    $symbx{$title} = $sequence->symb();
                   2981: 	    $minder++;
                   2982: 	}
1.68      ng       2983:     }
                   2984: 
                   2985:     $navmap->untieHashes();
                   2986:     return \@titles,\%symbx;
                   2987: }
                   2988: 
1.72      ng       2989: #
                   2990: #--- Displays a page/sequence w/wo problems, w/wo submissions
1.68      ng       2991: sub displayPage {
                   2992:     my ($request) = shift;
                   2993: 
1.72      ng       2994:     my ($symb,$url) = &get_symb_and_url($request);
1.68      ng       2995:     my $cdom      = $ENV{"course.$ENV{'request.course.id'}.domain"};
                   2996:     my $cnum      = $ENV{"course.$ENV{'request.course.id'}.num"};
                   2997:     my $getsec    = $ENV{'form.section'} eq '' ? 'all' : $ENV{'form.section'};
                   2998:     my $pageTitle = $ENV{'form.page'};
1.103     albertel 2999:     my ($classlist,undef,$fullname) = &getclasslist($getsec,'1');
1.70      ng       3000:     my ($uname,$udom) = split(/:/,$ENV{'form.student'});
1.103     albertel 3001:     my $usec=$classlist->{$ENV{'form.student'}}[5];
                   3002:     if (!&canview($usec)) {
                   3003: 	$request->print('<font color="red">Unable to view requested student.('.$ENV{'form.student'}.')</font>');
                   3004: 	$request->print(&show_grading_menu_form($symb,$url));
                   3005: 	return;
                   3006:     }
1.70      ng       3007:     my $result='<h3><font color="#339933">&nbsp;'.$ENV{'form.title'}.'</font></h3>';
1.129     ng       3008:     $result.='<h3>&nbsp;Student: '.&nameUserString(undef,$$fullname{$ENV{'form.student'}},$uname,$udom).
                   3009: 	'</h3>'."\n";
1.71      ng       3010:     &sub_page_js($request);
                   3011:     $request->print($result);
                   3012: 
1.132     bowersj2 3013:     my $navmap = Apache::lonnavmaps::navmap->new();
1.136     www      3014:     my ($mapUrl, $id, $resUrl)=&Apache::lonnet::decode_symb($ENV{'form.page'});
1.68      ng       3015:     my $map = $navmap->getResourceByUrl($resUrl); # add to navmaps
                   3016: 
                   3017:     my $iterator = $navmap->getIterator($map->map_start(),
                   3018: 					$map->map_finish());
                   3019: 
1.71      ng       3020:     my $studentTable='<form action="/adm/grades" method="post" name="gradePage">'."\n".
1.72      ng       3021: 	'<input type="hidden" name="command" value="gradeByPage" />'."\n".
1.125     ng       3022: 	'<input type="hidden" name="fullname" value="'.$$fullname{$ENV{'form.student'}}.'" />'."\n".
1.72      ng       3023: 	'<input type="hidden" name="student" value="'.$ENV{'form.student'}.'" />'."\n".
                   3024: 	'<input type="hidden" name="page"    value="'.$pageTitle.'" />'."\n".
                   3025: 	'<input type="hidden" name="title"   value="'.$ENV{'form.title'}.'" />'."\n".
                   3026: 	'<input type="hidden" name="url"     value="'.$url.'" />'."\n".
                   3027: 	'<input type="hidden" name="symb"    value="'.$symb.'" />'."\n".
1.125     ng       3028: 	'<input type="hidden" name="overRideScore" value="no" />'."\n".
1.77      ng       3029: 	'<input type="hidden" name="saveState" value="'.$ENV{'form.saveState'}.'" />'."\n";
1.71      ng       3030: 
                   3031:     my $checkIcon = '<img src="'.$request->dir_config('lonIconsURL').
                   3032: 	'/check.gif" height="16" border="0" />';
                   3033: 
1.118     ng       3034:     $studentTable.='&nbsp;<b>Note:</b> Problems graded correct by the computer are marked with a '.$checkIcon.
                   3035: 	' symbol.'."\n".
1.71      ng       3036: 	'<table border="0"><tr><td bgcolor="#777777">'.
                   3037: 	'<table border="0"><tr bgcolor="#e6ffff">'.
1.118     ng       3038: 	'<td align="center"><b>&nbsp;Prob.&nbsp;</b></td>'.
                   3039: 	'<td><b>&nbsp;'.($ENV{'form.vProb'} eq 'no' ? 'Title' : 'Problem Text').'/Grade</b></td></tr>';
1.71      ng       3040: 
1.101     albertel 3041:     my ($depth,$question) = (1,1);
1.68      ng       3042:     $iterator->next(); # skip the first BEGIN_MAP
                   3043:     my $curRes = $iterator->next(); # for "current resource"
1.101     albertel 3044:     while ($depth > 0) {
1.68      ng       3045:         if($curRes == $iterator->BEGIN_MAP) { $depth++; }
1.100     bowersj2 3046:         if($curRes == $iterator->END_MAP) { $depth--; }
1.68      ng       3047: 
1.120     ng       3048:         if (ref($curRes) && $curRes->is_problem()) {
1.91      albertel 3049: 	    my $parts = $curRes->parts();
1.68      ng       3050:             my $title = $curRes->compTitle();
1.71      ng       3051: 	    my $symbx = $curRes->symb();
                   3052: 	    $studentTable.='<tr bgcolor="#ffffe6"><td align="center" valign="top" >'.$question.
                   3053: 		(scalar(@{$parts}) == 1 ? '' : '<br>('.scalar(@{$parts}).'&nbsp;parts)').'</td>';
                   3054: 	    $studentTable.='<td valign="top">';
1.144     albertel 3055: 	    if ($ENV{'form.vProb'} eq 'yes' ) {
                   3056: 		$studentTable.=&show_problem($request,$symbx,$uname,$udom,1,
                   3057: 					     undef,'both');
1.71      ng       3058: 	    } else {
1.116     ng       3059: 		my $companswer = &Apache::loncommon::get_student_answers($symbx,$uname,$udom,$ENV{'request.course.id'});
1.80      ng       3060: 		$companswer =~ s|<form(.*?)>||g;
                   3061: 		$companswer =~ s|</form>||g;
1.71      ng       3062: #		while ($companswer =~ /(<a href\=\"javascript:newWindow.*?Script Vars<\/a>)/s) { #<a href="javascript:newWindow</a>
1.116     ng       3063: #		    $companswer =~ s/$1/ /ms;
                   3064: #		    $request->print('match='.$1."<br>\n");
1.71      ng       3065: #		}
1.116     ng       3066: #		$companswer =~ s|<table border=\"1\">|<table border=\"0\">|g;
1.71      ng       3067: 		$studentTable.='&nbsp;<b>'.$title.'</b>&nbsp;<br>&nbsp;<b>Correct answer:</b><br>'.$companswer;
                   3068: 	    }
                   3069: 
                   3070: 	    my %record = &Apache::lonnet::restore($symbx,$ENV{'request.course.id'},$udom,$uname);
1.125     ng       3071: 
1.71      ng       3072: 	    if ($ENV{'form.lastSub'} eq 'datesub') {
                   3073: 		if ($record{'version'} eq '') {
                   3074: 		    $studentTable.='<br />&nbsp;<font color="red">No recorded submission for this problem</font><br />';
                   3075: 		} else {
1.116     ng       3076: 		    my %responseType = ();
                   3077: 		    foreach my $partid (@{$parts}) {
1.147     albertel 3078: 			my @responseIds =$curRes->responseIds($partid);
                   3079: 			my @responseType =$curRes->responseType($partid);
                   3080: 			my %responseIds;
                   3081: 			for (my $i=0;$i<=$#responseIds;$i++) {
                   3082: 			    $responseIds{$responseIds[$i]}=$responseType[$i];
                   3083: 			}
                   3084: 			$responseType{$partid} = \%responseIds;
1.116     ng       3085: 		    }
1.148     albertel 3086: 		    $studentTable.= &displaySubByDates($symbx,\%record,$parts,\%responseType,$checkIcon,$uname,$udom);
1.147     albertel 3087: 
1.71      ng       3088: 		}
                   3089: 	    } elsif ($ENV{'form.lastSub'} eq 'all') {
                   3090: 		my $last = ($ENV{'form.lastSub'} eq 'last' ? 'last' : '');
                   3091: 		$studentTable.=&Apache::loncommon::get_previous_attempt($symbx,$uname,$udom,
                   3092: 									$ENV{'request.course.id'},
                   3093: 									'','.submission');
                   3094:  
                   3095: 	    }
1.103     albertel 3096: 	    if (&canmodify($usec)) {
                   3097: 		foreach my $partid (@{$parts}) {
                   3098: 		    $studentTable.=&gradeBox($request,$symbx,$uname,$udom,$question,$partid,\%record);
                   3099: 		    $studentTable.='<input type="hidden" name="q_'.$question.'" value="'.$partid.'" />'."\n";
                   3100: 		    $question++;
                   3101: 		}
1.71      ng       3102: 	    }
                   3103: 	    $studentTable.='</td></tr>';
1.68      ng       3104: 
1.103     albertel 3105: 	}
1.68      ng       3106:         $curRes = $iterator->next();
                   3107:     }
                   3108: 
1.98      albertel 3109:     $navmap->untieHashes();
                   3110: 
1.71      ng       3111:     $studentTable.='</td></tr></table></td></tr></table>'."\n".
1.125     ng       3112: 	'<input type="button" value="Save" '.
1.71      ng       3113: 	'onClick="javascript:checkSubmitPage(this.form,'.$question.');" TARGET=_self />'.
                   3114: 	'</form>'."\n";
                   3115:     $studentTable.=&show_grading_menu_form($symb,$url);
                   3116:     $request->print($studentTable);
                   3117: 
                   3118:     return '';
1.119     ng       3119: }
                   3120: 
                   3121: sub displaySubByDates {
1.148     albertel 3122:     my ($symb,$record,$parts,$responseType,$checkIcon,$uname,$udom) = @_;
1.119     ng       3123:     my $studentTable='<table border="0" width="100%"><tr><td bgcolor="#777777">'.
                   3124: 	'<table border="0" width="100%"><tr bgcolor="#e6ffff">'.
                   3125: 	'<td><b>Date/Time</b></td>'.
                   3126: 	'<td><b>Submission</b></td>'.
                   3127: 	'<td><b>Status&nbsp;</b></td></tr>';
                   3128:     my ($version);
                   3129:     my %mark;
1.148     albertel 3130:     my %orders;
1.119     ng       3131:     $mark{'correct_by_student'} = $checkIcon;
1.147     albertel 3132:     if (!exists($$record{'1:timestamp'})) {
                   3133: 	return '<br />&nbsp;<font color="red">Nothing submitted - no attempts</font><br />';
                   3134:     }
1.119     ng       3135:     for ($version=1;$version<=$$record{'version'};$version++) {
                   3136: 	my $timestamp = scalar(localtime($$record{$version.':timestamp'}));
                   3137: 	$studentTable.='<tr bgcolor="#ffffff" valign="top"><td>'.$timestamp.'</td>';
                   3138: 	my @versionKeys = split(/\:/,$$record{$version.':keys'});
                   3139: 	my @displaySub = ();
                   3140: 	foreach my $partid (@{$parts}) {
1.147     albertel 3141: 	    my @matchKey = sort(grep /^resource\.\Q$partid\E\..*?\.submission$/,@versionKeys);
1.122     ng       3142: #	    next if ($$record{"$version:resource.$partid.solved"} eq '');
1.147     albertel 3143: 	    foreach my $matchKey (@matchKey) {
                   3144: 		if (exists $$record{$version.':'.$matchKey}) {
                   3145: 		    my ($responseId)=($matchKey=~ /^resource\.\Q$partid\E\.(.*?)\.submission$/);
                   3146: 		    $displaySub[0].='<b>Part&nbsp;'.$partid.'&nbsp;';
                   3147: 		    $displaySub[0].='<font color="#999999">(ID&nbsp;'.
                   3148: 			$responseId.')</font>&nbsp;';
                   3149: 		    if ($$record{"$version:resource.$partid.tries"} eq '') {
                   3150: 			$displaySub[0].='Trial&nbsp;not&nbsp;counted';
                   3151: 		    } else {
                   3152: 			$displaySub[0].='Trial&nbsp;'.
                   3153: 			    $$record{"$version:resource.$partid.tries"};
                   3154: 		    }
                   3155: 		    my $responseType=$responseType->{$partid}->{$responseId};
1.148     albertel 3156: 		    if (!exists($orders{$partid})) { $orders{$partid}={}; }
                   3157: 		    if (!exists($orders{$partid}->{$responseId})) {
                   3158: 			$orders{$partid}->{$responseId}=
                   3159: 			    &get_order($partid,$responseId,$symb,$uname,$udom);
                   3160: 		    }
1.147     albertel 3161: 		    $displaySub[0].='</b>&nbsp; '.
1.148     albertel 3162: 			&cleanRecord($$record{$version.':'.$matchKey},$responseType,$symb,$partid,$responseId,$record,$orders{$partid}->{$responseId},"$version:").'<br />';
1.147     albertel 3163: 		}
                   3164: 	    }
                   3165: 	    if (exists $$record{"$version:resource.$partid.award"}) {
                   3166: 		$displaySub[1].='<b>Part&nbsp;'.$partid.'</b> &nbsp;'.
                   3167: 		    lc($$record{"$version:resource.$partid.award"}).' '.
                   3168: 		    $mark{$$record{"$version:resource.$partid.solved"}}.
                   3169: 		    '<br />';
                   3170: 	    }
                   3171: 	    if (exists $$record{"$version:resource.$partid.regrader"}) {
                   3172: 		$displaySub[2].=$$record{"$version:resource.$partid.regrader"}.
                   3173: 		    ' (<b>Part:</b> '.$partid.')';
                   3174: 	    }
                   3175: 	}
                   3176: 	# needed because old essay regrader has not parts info
                   3177: 	if (exists $$record{"$version:resource.regrader"}) {
                   3178: 	    $displaySub[2].=$$record{"$version:resource.regrader"};
                   3179: 	}
                   3180: 	$studentTable.='<td>'.$displaySub[0].'&nbsp;</td><td>'.$displaySub[1];
                   3181: 	if ($displaySub[2]) {
                   3182: 	    $studentTable.='Manually graded by '.$displaySub[2];
                   3183: 	}
                   3184: 	$studentTable.='&nbsp;</td></tr>';
                   3185:     
1.119     ng       3186:     }
                   3187:     $studentTable.='</table></td></tr></table>';
                   3188:     return $studentTable;
1.71      ng       3189: }
                   3190: 
                   3191: sub updateGradeByPage {
                   3192:     my ($request) = shift;
                   3193: 
                   3194:     my $cdom      = $ENV{"course.$ENV{'request.course.id'}.domain"};
                   3195:     my $cnum      = $ENV{"course.$ENV{'request.course.id'}.num"};
                   3196:     my $getsec    = $ENV{'form.section'} eq '' ? 'all' : $ENV{'form.section'};
                   3197:     my $pageTitle = $ENV{'form.page'};
1.103     albertel 3198:     my ($classlist,undef,$fullname) = &getclasslist($getsec,'1');
1.71      ng       3199:     my ($uname,$udom) = split(/:/,$ENV{'form.student'});
1.103     albertel 3200:     my $usec=$classlist->{$ENV{'form.student'}}[5];
                   3201:     if (!&canmodify($usec)) {
                   3202: 	$request->print('<font color="red">Unable to modify requested student.('.$ENV{'form.student'}.'</font>');
                   3203: 	$request->print(&show_grading_menu_form($ENV{'form.symb'},$ENV{'form.url'}));
                   3204: 	return;
                   3205:     }
1.71      ng       3206:     my $result='<h3><font color="#339933">&nbsp;'.$ENV{'form.title'}.'</font></h3>';
1.129     ng       3207:     $result.='<h3>&nbsp;Student: '.&nameUserString(undef,$ENV{'form.fullname'},$uname,$udom).
                   3208: 	'</h3>'."\n";
1.70      ng       3209: 
1.68      ng       3210:     $request->print($result);
                   3211: 
1.132     bowersj2 3212:     my $navmap = Apache::lonnavmaps::navmap->new();
1.136     www      3213:     my ($mapUrl, $id, $resUrl) = &Apache::lonnet::decode_symb( $ENV{'form.page'});
1.71      ng       3214:     my $map = $navmap->getResourceByUrl($resUrl); # add to navmaps
                   3215: 
                   3216:     my $iterator = $navmap->getIterator($map->map_start(),
                   3217: 					$map->map_finish());
1.70      ng       3218: 
1.71      ng       3219:     my $studentTable='<table border="0"><tr><td bgcolor="#777777">'.
1.68      ng       3220: 	'<table border="0"><tr bgcolor="#e6ffff">'.
1.125     ng       3221: 	'<td align="center"><b>&nbsp;Prob.&nbsp;</b></td>'.
1.71      ng       3222: 	'<td><b>&nbsp;Title&nbsp;</b></td>'.
                   3223: 	'<td><b>&nbsp;Previous Score&nbsp;</b></td>'.
                   3224: 	'<td><b>&nbsp;New Score&nbsp;</b></td></tr>';
                   3225: 
                   3226:     $iterator->next(); # skip the first BEGIN_MAP
                   3227:     my $curRes = $iterator->next(); # for "current resource"
1.101     albertel 3228:     my ($depth,$question,$changeflag)= (1,1,0);
                   3229:     while ($depth > 0) {
1.71      ng       3230:         if($curRes == $iterator->BEGIN_MAP) { $depth++; }
1.100     bowersj2 3231:         if($curRes == $iterator->END_MAP) { $depth--; }
1.71      ng       3232: 
                   3233:         if (ref($curRes) && $curRes->is_problem() && !$curRes->randomout) {
1.91      albertel 3234: 	    my $parts = $curRes->parts();
1.71      ng       3235:             my $title = $curRes->compTitle();
                   3236: 	    my $symbx = $curRes->symb();
                   3237: 	    $studentTable.='<tr bgcolor="#ffffe6"><td align="center" valign="top" >'.$question.
                   3238: 		(scalar(@{$parts}) == 1 ? '' : '<br>('.scalar(@{$parts}).'&nbsp;parts)').'</td>';
                   3239: 	    $studentTable.='<td valign="top">&nbsp;<b>'.$title.'</b>&nbsp;</td>';
                   3240: 
                   3241: 	    my %newrecord=();
                   3242: 	    my @displayPts=();
                   3243: 	    foreach my $partid (@{$parts}) {
                   3244: 		my $newpts = $ENV{'form.GD_BOX'.$question.'_'.$partid};
                   3245: 		my $oldpts = $ENV{'form.oldpts'.$question.'_'.$partid};
                   3246: 
                   3247: 		my $wgt = $ENV{'form.WGT'.$question.'_'.$partid} != 0 ? 
                   3248: 		    $ENV{'form.WGT'.$question.'_'.$partid} : 1;
                   3249: 		my $partial = $newpts/$wgt;
                   3250: 		my $score;
                   3251: 		if ($partial > 0) {
                   3252: 		    $score = 'correct_by_override';
1.125     ng       3253: 		} elsif ($newpts ne '') { #empty is taken as 0
1.71      ng       3254: 		    $score = 'incorrect_by_override';
                   3255: 		}
1.125     ng       3256: 		my $dropMenu = $ENV{'form.GD_SEL'.$question.'_'.$partid};
                   3257: 		if ($dropMenu eq 'excused') {
1.71      ng       3258: 		    $partial = '';
                   3259: 		    $score = 'excused';
1.125     ng       3260: 		} elsif ($dropMenu eq 'reset status'
                   3261: 			 && $ENV{'form.solved'.$question.'_'.$partid} ne '') { #update only if previous record exists
                   3262: 		    $newrecord{'resource.'.$partid.'.tries'} = 0;
                   3263: 		    $newrecord{'resource.'.$partid.'.solved'} = '';
                   3264: 		    $newrecord{'resource.'.$partid.'.award'} = '';
                   3265: 		    $newrecord{'resource.'.$partid.'.awarded'} = 0;
                   3266: 		    $newrecord{'resource.'.$partid.'.regrader'} = "$ENV{'user.name'}:$ENV{'user.domain'}";
                   3267: 		    $changeflag++;
                   3268: 		    $newpts = '';
1.71      ng       3269: 		}
1.125     ng       3270: 
1.71      ng       3271: 		my $oldstatus = $ENV{'form.solved'.$question.'_'.$partid};
                   3272: 		$displayPts[0].='&nbsp;<b>Part</b> '.$partid.' = '.
                   3273: 		    (($oldstatus eq 'excused') ? 'excused' : $oldpts).
                   3274: 		    '&nbsp;<br>';
                   3275: 		$displayPts[1].='&nbsp;<b>Part</b> '.$partid.' = '.
1.125     ng       3276: 		     (($score eq 'excused') ? 'excused' : $newpts).
1.71      ng       3277: 		    '&nbsp;<br>';
                   3278: 
                   3279: 		$question++;
1.125     ng       3280: 		next if ($dropMenu eq 'reset status' || ($newpts == $oldpts && $score ne 'excused'));
                   3281: 
1.71      ng       3282: 		$newrecord{'resource.'.$partid.'.awarded'}  = $partial if $partial ne '';
1.125     ng       3283: 		$newrecord{'resource.'.$partid.'.solved'}   = $score if $score ne '';
                   3284: 		$newrecord{'resource.'.$partid.'.regrader'} = "$ENV{'user.name'}:$ENV{'user.domain'}"
                   3285: 		    if (scalar(keys(%newrecord)) > 0);
1.71      ng       3286: 
                   3287: 		$changeflag++;
                   3288: 	    }
                   3289: 	    if (scalar(keys(%newrecord)) > 0) {
                   3290: 		&Apache::lonnet::cstore(\%newrecord,$symbx,$ENV{'request.course.id'},
                   3291: 					$udom,$uname);
                   3292: 	    }
1.125     ng       3293: 
1.71      ng       3294: 	    $studentTable.='<td valign="top">'.$displayPts[0].'</td>'.
                   3295: 		'<td valign="top">'.$displayPts[1].'</td>'.
                   3296: 		'</tr>';
1.68      ng       3297: 
                   3298: 	}
1.71      ng       3299:         $curRes = $iterator->next();
1.68      ng       3300:     }
1.98      albertel 3301: 
                   3302:     $navmap->untieHashes();
1.68      ng       3303: 
1.71      ng       3304:     $studentTable.='</td></tr></table></td></tr></table>';
                   3305:     $studentTable.=&show_grading_menu_form($ENV{'form.symb'},$ENV{'form.url'});
1.76      ng       3306:     my $grademsg=($changeflag == 0 ? 'No score was changed or updated.' :
                   3307: 		  'The scores were changed for '.
                   3308: 		  $changeflag.' problem'.($changeflag == 1 ? '.' : 's.'));
                   3309:     $request->print($grademsg.$studentTable);
1.68      ng       3310: 
1.70      ng       3311:     return '';
                   3312: }
                   3313: 
1.72      ng       3314: #-------- end of section for handling grading by page/sequence ---------
                   3315: #
                   3316: #-------------------------------------------------------------------
                   3317: 
1.75      albertel 3318: #--------------------Scantron Grading-----------------------------------
                   3319: #
                   3320: #------ start of section for handling grading by page/sequence ---------
                   3321: 
1.81      albertel 3322: sub defaultFormData {
                   3323:     my ($symb,$url)=@_;
                   3324:     return '
                   3325:       <input type="hidden" name="symb"    value="'.$symb.'" />'."\n".
                   3326:      '<input type="hidden" name="url"     value="'.$url.'" />'."\n".
                   3327:      '<input type="hidden" name="saveState" value="'.$ENV{'form.saveState'}.'" />'."\n".
                   3328:      '<input type="hidden" name="probTitle" value="'.$ENV{'form.probTitle'}.'" />'."\n";
                   3329: }
                   3330: 
1.75      albertel 3331: sub getSequenceDropDown {
                   3332:     my ($request,$symb)=@_;
                   3333:     my $result='<select name="selectpage">'."\n";
                   3334:     my ($titles,$symbx) = &getSymbMap($request);
1.137     albertel 3335:     my ($curpage)=&Apache::lonnet::decode_symb($symb); 
1.75      albertel 3336:     my $ctr=0;
                   3337:     foreach (@$titles) {
                   3338: 	my ($minder,$showtitle) = ($_ =~ /(\d+)\.(.*)/);
                   3339: 	$result.='<option value="'.$$symbx{$_}.'" '.
                   3340: 	    ($$symbx{$_} =~ /$curpage$/ ? 'selected="on"' : '').
                   3341: 	    '>'.$showtitle.'</option>'."\n";
                   3342: 	$ctr++;
                   3343:     }
                   3344:     $result.= '</select>';
                   3345:     return $result;
                   3346: }
                   3347: 
1.81      albertel 3348: sub scantron_uploads {
                   3349:     if (!-e $Apache::lonnet::perlvar{'lonScansDir'}) { return ''};
                   3350:     my $result=	'<select name="scantron_selectfile">';
                   3351:     opendir(DIR,$Apache::lonnet::perlvar{'lonScansDir'});
                   3352:     my @files=sort(readdir(DIR));
                   3353:     foreach my $filename (@files) {
                   3354: 	if ($filename eq '.' or $filename eq '..') { next; }
                   3355: 	$result.="<option>$filename</option>\n";
                   3356:     }
                   3357:     closedir(DIR);
                   3358:     $result.="</select>";
                   3359:     return $result;
                   3360: }
                   3361: 
1.82      albertel 3362: sub scantron_scantab {
                   3363:     my $fh=Apache::File->new($Apache::lonnet::perlvar{'lonTabDir'}.'/scantronformat.tab');
                   3364:     my $result='<select name="scantron_format">'."\n";
                   3365:     foreach my $line (<$fh>) {
                   3366: 	my ($name,$descrip)=split(/:/,$line);
                   3367: 	if ($name =~ /^\#/) { next; }
                   3368: 	$result.='<option value="'.$name.'">'.$descrip.'</option>'."\n";
                   3369:     }
                   3370:     $result.='</select>'."\n";
                   3371: 
                   3372:     return $result;
                   3373: }
                   3374: 
1.75      albertel 3375: sub scantron_selectphase {
                   3376:     my ($r) = @_;
                   3377:     my ($symb,$url)=&get_symb_and_url($r);
                   3378:     if (!$symb) {return '';}
                   3379:     my $sequence_selector=&getSequenceDropDown($r,$symb);
1.81      albertel 3380:     my $default_form_data=&defaultFormData($symb,$url);
                   3381:     my $grading_menu_button=&show_grading_menu_form($symb,$url);
                   3382:     my $file_selector=&scantron_uploads();
1.82      albertel 3383:     my $format_selector=&scantron_scantab();
1.75      albertel 3384:     my $result;
                   3385:     $result.= <<SCANTRONFORM;
1.82      albertel 3386: <form method="post" enctype="multipart/form-data" action="/adm/grades" name="scantro_process">
1.142     albertel 3387:   <input type="hidden" name="command" value="scantron_validate" />
1.81      albertel 3388:   $default_form_data
1.75      albertel 3389:   <table width="100%" border="0">
                   3390:     <tr>
                   3391:       <td bgcolor="#777777">
                   3392:         <table width="100%" border="0">
                   3393:           <tr bgcolor="#e6ffff">
                   3394:             <td>
                   3395:               &nbsp;<b>Specify file location and which Folder/Sequence to grade</b>
                   3396:             </td>
                   3397:           </tr>
                   3398:           <tr bgcolor="#ffffe6">
                   3399:             <td>
                   3400:                Sequence to grade: $sequence_selector
                   3401: 	    </td>
                   3402:           </tr>
                   3403:           <tr bgcolor="#ffffe6">
                   3404:             <td>
1.81      albertel 3405: 		Filename of scoring office file: $file_selector
1.75      albertel 3406: 	    </td>
                   3407:           </tr>
1.82      albertel 3408:           <tr bgcolor="#ffffe6">
                   3409:             <td>
                   3410:               Format of data file: $format_selector
                   3411: 	    </td>
                   3412:           </tr>
1.75      albertel 3413:         </table>
                   3414:       </td>
                   3415:     </tr>
                   3416:   </table>
1.142     albertel 3417:   <input type="submit" value="Validate Scantron Records" />
1.75      albertel 3418: </form>
1.81      albertel 3419: $grading_menu_button
1.75      albertel 3420: SCANTRONFORM
                   3421: 
                   3422:     return $result;
                   3423: }
                   3424: 
1.82      albertel 3425: sub get_scantron_config {
                   3426:     my ($which) = @_;
                   3427:     my $fh=Apache::File->new($Apache::lonnet::perlvar{'lonTabDir'}.'/scantronformat.tab');
                   3428:     my %config;
                   3429:     foreach my $line (<$fh>) {
                   3430: 	my ($name,$descrip)=split(/:/,$line);
                   3431: 	if ($name ne $which ) { next; }
                   3432: 	chomp($line);
                   3433: 	my @config=split(/:/,$line);
                   3434: 	$config{'name'}=$config[0];
                   3435: 	$config{'description'}=$config[1];
                   3436: 	$config{'CODElocation'}=$config[2];
                   3437: 	$config{'CODEstart'}=$config[3];
                   3438: 	$config{'CODElength'}=$config[4];
                   3439: 	$config{'IDstart'}=$config[5];
                   3440: 	$config{'IDlength'}=$config[6];
                   3441: 	$config{'Qstart'}=$config[7];
                   3442: 	$config{'Qlength'}=$config[8];
                   3443: 	$config{'Qoff'}=$config[9];
                   3444: 	$config{'Qon'}=$config[10];
                   3445: 	last;
                   3446:     }
                   3447:     return %config;
                   3448: }
                   3449: 
                   3450: sub username_to_idmap {
                   3451:     my ($classlist)= @_;
                   3452:     my %idmap;
                   3453:     foreach my $student (keys(%$classlist)) {
                   3454: 	$idmap{$classlist->{$student}->[&Apache::loncoursedata::CL_ID]}=
                   3455: 	    $student;
                   3456:     }
                   3457:     return %idmap;
                   3458: }
                   3459: 
                   3460: sub scantron_parse_scanline {
                   3461:     my ($line,$scantron_config)=@_;
                   3462:     my %record;
                   3463:     my $questions=substr($line,$$scantron_config{'Qstart'}-1);
                   3464:     my $data=substr($line,0,$$scantron_config{'Qstart'}-1);
                   3465:     if ($$scantron_config{'CODElocation'} ne 0) {
                   3466: 	if ($$scantron_config{'CODElocation'} < 0) {
1.83      albertel 3467: 	    $record{'scantron.CODE'}=substr($data,$$scantron_config{'CODEstart'}-1,
                   3468: 					    $$scantron_config{'CODElength'});
1.82      albertel 3469: 	} else {
                   3470: 	    #FIXME interpret first N questions
                   3471: 	}
                   3472:     }
1.83      albertel 3473:     $record{'scantron.ID'}=substr($data,$$scantron_config{'IDstart'}-1,
                   3474: 				  $$scantron_config{'IDlength'});
1.82      albertel 3475:     my @alphabet=('A'..'Z');
                   3476:     my $questnum=0;
                   3477:     while ($questions) {
                   3478: 	$questnum++;
                   3479: 	my $currentquest=substr($questions,0,$$scantron_config{'Qlength'});
                   3480: 	substr($questions,0,$$scantron_config{'Qlength'})='';
1.83      albertel 3481: 	if (length($currentquest) < $$scantron_config{'Qlength'}) { next; }
1.82      albertel 3482: 	my (@array)=split(/$$scantron_config{'Qon'}/,$currentquest);
                   3483: 	if (scalar(@array) gt 2) {
                   3484: 	    #FIXME do something intelligent with double bubbles
1.83      albertel 3485: 	    Apache->request->print("<br ><b>Wha!!!</b> <pre>".scalar(@array).
                   3486: 				   '-'.$currentquest.'-'.$questnum.'</pre><br />');
1.82      albertel 3487: 	}
                   3488: 	if (length($array[0]) eq $$scantron_config{'Qlength'}) {
1.83      albertel 3489: 	    $record{"scantron.$questnum.answer"}='';
1.82      albertel 3490: 	} else {
1.83      albertel 3491: 	    $record{"scantron.$questnum.answer"}=$alphabet[length($array[0])];
1.82      albertel 3492: 	}
                   3493:     }
1.83      albertel 3494:     $record{'scantron.maxquest'}=$questnum;
                   3495:     return \%record;
1.82      albertel 3496: }
                   3497: 
                   3498: sub scantron_add_delay {
1.140     albertel 3499:     my ($delayqueue,$scanline,$errormessage,$errorcode)=@_;
                   3500:     Apache->request->print('add_delay_error '.$_[2] );
                   3501:     push(@$delayqueue,
                   3502: 	 {'line' => $scanline, 'emsg' => $errormessage,
                   3503: 	  'ecode' => $errorcode }
                   3504: 	 );
1.82      albertel 3505: }
                   3506: 
                   3507: sub scantron_find_student {
1.83      albertel 3508:     my ($scantron_record,$idmap)=@_;
                   3509:     my $scanID=$$scantron_record{'scantron.ID'};
                   3510:     foreach my $id (keys(%$idmap)) {
1.140     albertel 3511: 	#Apache->request->print('<pre>checking studnet -'.$id.'- againt -'.$scanID.'- </pre>');
                   3512: 	if (lc($id) eq lc($scanID)) {
                   3513: 	    #Apache->request->print('success');
                   3514: 	    return $$idmap{$id};
                   3515: 	}
1.83      albertel 3516:     }
                   3517:     return undef;
                   3518: }
                   3519: 
                   3520: sub scantron_filter {
                   3521:     my ($curres)=@_;
                   3522:     if (ref($curres) && $curres->is_problem() && !$curres->randomout) {
                   3523: 	return 1;
                   3524:     }
                   3525:     return 0;
1.82      albertel 3526: }
                   3527: 
1.140     albertel 3528: #FIXME I think I am doing this in the wrong order, I think it would be
                   3529: #better to make a several passes analyzing all of the lines in the
                   3530: #file for common errors wrong/invalid PID/username duplicated
                   3531: #PID/username, missing bubbles, double bubbles, missing/invalid CODE
                   3532: #and then get the instructor to fix all of these errors, then grade
                   3533: #the corrected one, I'll still need to catch error conditions, but
                   3534: #maybe most will taken care even before we start
1.142     albertel 3535: 
                   3536: sub scantron_validate_file {
                   3537:     my ($r) = @_;
                   3538: }
                   3539: 
1.82      albertel 3540: sub scantron_process_students {
1.75      albertel 3541:     my ($r) = @_;
1.136     www      3542:     my (undef,undef,$sequence)=&Apache::lonnet::decode_symb($ENV{'form.selectpage'});
1.81      albertel 3543:     my ($symb,$url)=&get_symb_and_url($r);
                   3544:     if (!$symb) {return '';}
                   3545:     my $default_form_data=&defaultFormData($symb,$url);
1.82      albertel 3546: 
                   3547:     my %scantron_config=&get_scantron_config($ENV{'form.scantron_format'});
                   3548:     my $scanlines=Apache::File->new($Apache::lonnet::perlvar{'lonScansDir'}."/$ENV{'form.scantron_selectfile'}");
1.85      albertel 3549:     my @scanlines=<$scanlines>;
1.82      albertel 3550:     my $classlist=&Apache::loncoursedata::get_classlist();
                   3551:     my %idmap=&username_to_idmap($classlist);
1.132     bowersj2 3552:     my $navmap=Apache::lonnavmaps::navmap->new();
1.83      albertel 3553:     my $map=$navmap->getResourceByUrl($sequence);
                   3554:     my @resources=$navmap->retrieveResources($map,\&scantron_filter,1,0);
1.140     albertel 3555: #    $r->print("geto ".scalar(@resources)."<br />");
1.82      albertel 3556:     my $result= <<SCANTRONFORM;
1.81      albertel 3557: <form method="post" enctype="multipart/form-data" action="/adm/grades" name="scantronupload">
                   3558:   <input type="hidden" name="command" value="scantron_configphase" />
                   3559:   $default_form_data
                   3560: SCANTRONFORM
1.82      albertel 3561:     $r->print($result);
                   3562: 
                   3563:     my @delayqueue;
1.140     albertel 3564:     my %completedstudents;
                   3565:     
1.85      albertel 3566:     my %prog_state=&Apache::lonhtmlcommon::Create_PrgWin($r,
                   3567: 	           'Scantron Status','Scantron Progress',scalar(@scanlines));
1.140     albertel 3568:     &Apache::lonhtmlcommon::Update_PrgWin($r,\%prog_state,
                   3569: 					  'Processing first student');
                   3570:     my $start=&Time::HiRes::time();
1.85      albertel 3571:     foreach my $line (@scanlines) {
1.140     albertel 3572: 	$r->print('<pre>line is'.$line.'</pre>');
1.75      albertel 3573: 
1.83      albertel 3574: 	chomp($line);
1.82      albertel 3575: 	my $scan_record=&scantron_parse_scanline($line,\%scantron_config);
                   3576: 	my ($uname,$udom);
1.140     albertel 3577: 	unless ($uname=&scantron_find_student($scan_record,\%idmap)) {
1.82      albertel 3578: 	    &scantron_add_delay(\@delayqueue,$line,
1.140     albertel 3579: 				'Unable to find a student that matches',1);
                   3580: 	    next;
                   3581: 	}
                   3582: 	if (exists $completedstudents{$uname}) {
                   3583: 	    &scantron_add_delay(\@delayqueue,$line,
                   3584: 				'Student '.$uname.' has multiple sheets',2);
                   3585: 	    next;
1.82      albertel 3586: 	}
1.83      albertel 3587: 	$r->print('<pre>doing studnet'.$uname.'</pre>');
1.82      albertel 3588: 	($uname,$udom)=split(/:/,$uname);
1.85      albertel 3589: 	&Apache::lonnet::delenv('form.counter');
1.83      albertel 3590: 	&Apache::lonnet::appenv(%$scan_record);
1.85      albertel 3591: #    &Apache::lonhomework::showhash(%ENV);
1.140     albertel 3592: #    $Apache::lonxml::debug=1;
                   3593: #	&Apache::lonxml::debug("line is $line");
1.83      albertel 3594: 	
1.85      albertel 3595: 	    my $i=0;
1.83      albertel 3596: 	foreach my $resource (@resources) {
1.85      albertel 3597: 	    $i++;
1.83      albertel 3598: 	    my $result=&Apache::lonnet::ssi($resource->src(),
                   3599: 				 ('submitted'     =>'scantron',
                   3600: 				  'grade_target'  =>'grade',
                   3601: 				  'grade_username'=>$uname,
                   3602: 				  'grade_domain'  =>$udom,
                   3603: 				  'grade_courseid'=>$ENV{'request.course.id'},
                   3604: 				  'grade_symb'    =>$resource->symb()));
1.140     albertel 3605: #	    my %score=&Apache::lonnet::restore($resource->symb(),
                   3606: #					       $ENV{'request.course.id'},
                   3607: #					       $udom,$uname);
                   3608: #	    foreach my $part ($resource->{PARTS}) {
                   3609: #		if ($score{'resource.'.$part.'.solved'} =~ /^correct/) {
                   3610: #		    $studentcorrect++;
                   3611: #		    $totalcorrect++;
                   3612: #		} else {
                   3613: #		    $studentincorrect++;
                   3614: #		    $totalincorrect++;
                   3615: #		}
                   3616: #	    }
                   3617: #	    $r->print('<pre>'.
                   3618: #		      $resource->symb().'-'.
                   3619: #		      $resource->src().'-'.'</pre>result is'.$result);
                   3620: #	    &Apache::lonhomework::showhash(%score);
1.85      albertel 3621: 	#    if ($i eq 3) {last;}
1.83      albertel 3622: 	}
1.140     albertel 3623: 	$completedstudents{$uname}={'line'=>$line};
                   3624:     } continue {
1.85      albertel 3625: 	&Apache::lonnet::delenv('form.counter');
1.83      albertel 3626: 	&Apache::lonnet::delenv('scantron\.');
1.85      albertel 3627: 	&Apache::lonhtmlcommon::Increment_PrgWin($r,\%prog_state,
1.140     albertel 3628: 						 'last student');
                   3629: 	#last;
1.82      albertel 3630: 	#FIXME
                   3631: 	#get iterator for $sequence
                   3632: 	#foreach question 'submit' the students answer to the server
                   3633: 	#   through grade target {
                   3634: 	#   generate data to pass back that includes grade recevied
                   3635: 	#}
                   3636:     }
1.140     albertel 3637:     &Apache::lonhtmlcommon::Close_PrgWin($r,\%prog_state);
                   3638:     my $lasttime = &Time::HiRes::time()-$start;
                   3639:     $r->print("<p>took $lasttime</p>");
                   3640: 
                   3641:     #$Apache::lonxml::debug=0;
1.82      albertel 3642:     foreach my $delay (@delayqueue) {
                   3643: 	#FIXME
                   3644: 	#print out each delayed student with interface to select how
                   3645: 	#  to repair student provided info
                   3646: 	#Expected errors include
                   3647: 	#  1 bad/no stuid/username
                   3648: 	#  2 invalid bubblings
                   3649: 	
                   3650:     }
1.75      albertel 3651:     #FIXME
                   3652:     # if delay queue exists 2 submits one to process delayed students one
                   3653:     #     to ignore delayed students, possibly saving the delay queue for later
1.85      albertel 3654:     
                   3655:     $navmap->untieHashes();
1.75      albertel 3656: }
                   3657: #-------- end of section for handling grading scantron forms -------
                   3658: #
                   3659: #-------------------------------------------------------------------
                   3660: 
                   3661: 
1.72      ng       3662: #-------------------------- Menu interface -------------------------
                   3663: #
                   3664: #--- Show a Grading Menu button - Calls the next routine ---
                   3665: sub show_grading_menu_form {
                   3666:     my ($symb,$url)=@_;
1.125     ng       3667:     my $result.='<br /><form action="/adm/grades" method="post">'."\n".
1.72      ng       3668: 	'<input type="hidden" name="symb" value="'.$symb.'" />'."\n".
                   3669: 	'<input type="hidden" name="url" value="'.$url.'" />'."\n".
1.77      ng       3670: 	'<input type="hidden" name="saveState"  value="'.$ENV{'form.saveState'}.'" />'."\n".
1.72      ng       3671: 	'<input type="hidden" name="command" value="gradingmenu" />'."\n".
                   3672: 	'<input type="submit" name="submit" value="Grading Menu" />'."\n".
                   3673: 	'</form>'."\n";
                   3674:     return $result;
                   3675: }
                   3676: 
1.77      ng       3677: # -- Retrieve choices for grading form
                   3678: sub savedState {
                   3679:     my %savedState = ();
                   3680:     if ($ENV{'form.saveState'}) {
                   3681: 	foreach (split(/:/,$ENV{'form.saveState'})) {
                   3682: 	    my ($key,$value) = split(/=/,$_,2);
                   3683: 	    $savedState{$key} = $value;
                   3684: 	}
                   3685:     }
                   3686:     return \%savedState;
                   3687: }
1.76      ng       3688: 
1.72      ng       3689: #--- Displays the main menu page -------
                   3690: sub gradingmenu {
                   3691:     my ($request) = @_;
                   3692:     my ($symb,$url)=&get_symb_and_url($request);
                   3693:     if (!$symb) {return '';}
1.76      ng       3694:     my $probTitle = &Apache::lonnet::gettitle($symb);
1.72      ng       3695: 
                   3696:     $request->print(<<GRADINGMENUJS);
                   3697: <script type="text/javascript" language="javascript">
1.116     ng       3698:     function checkChoice(formname,val,cmdx) {
                   3699: 	if (val <= 2) {
                   3700: 	    var cmd = radioSelection(formname.radioChoice);
1.118     ng       3701: 	    var cmdsave = cmd;
1.116     ng       3702: 	} else {
                   3703: 	    cmd = cmdx;
1.118     ng       3704: 	    cmdsave = 'submission';
1.116     ng       3705: 	}
                   3706: 	formname.command.value = cmd;
1.118     ng       3707: 	formname.saveState.value = "saveCmd="+cmdsave+":saveSec="+pullDownSelection(formname.section)+
1.145     albertel 3708: 	    ":saveSub="+pullDownSelection(formname.submitonly)+":saveStatus="+pullDownSelection(formname.Status);
1.116     ng       3709: 	if (val < 5) formname.submit();
                   3710: 	if (val == 5) {
1.72      ng       3711: 	    if (!checkReceiptNo(formname,'notOK')) { return false;}
                   3712: 	    formname.submit();
                   3713: 	}
                   3714:     }
                   3715: 
                   3716:     function checkReceiptNo(formname,nospace) {
                   3717: 	var receiptNo = formname.receipt.value;
                   3718: 	var checkOpt = false;
                   3719: 	if (nospace == "OK" && isNaN(receiptNo)) {checkOpt = true;}
                   3720: 	if (nospace == "notOK" && (isNaN(receiptNo) || receiptNo == "")) {checkOpt = true;}
                   3721: 	if (checkOpt) {
                   3722: 	    alert("Please enter a receipt number given by a student in the receipt box.");
                   3723: 	    formname.receipt.value = "";
                   3724: 	    formname.receipt.focus();
                   3725: 	    return false;
                   3726: 	}
                   3727: 	return true;
                   3728:     }
                   3729: </script>
                   3730: GRADINGMENUJS
1.118     ng       3731:     &commonJSfunctions($request);
                   3732:     my $result='<h3>&nbsp;<font color="#339933">Manual Grading/View Submission</font></h3>';
1.122     ng       3733:     my ($table,undef,$hdgrade) = &showResourceInfo($url,$probTitle);
1.118     ng       3734:     $result.=$table;
1.76      ng       3735:     my (undef,$sections) = &getclasslist('all','0');
1.77      ng       3736:     my $savedState = &savedState();
1.118     ng       3737:     my $saveCmd = ($$savedState{'saveCmd'} eq '' ? 'submission' : $$savedState{'saveCmd'});
1.77      ng       3738:     my $saveSec = ($$savedState{'saveSec'} eq '' ? 'all' : $$savedState{'saveSec'});
1.118     ng       3739:     my $saveSub = ($$savedState{'saveSub'} eq '' ? 'all' : $$savedState{'saveSub'});
1.77      ng       3740:     my $saveStatus = ($$savedState{'saveStatus'} eq '' ? 'Active' : $$savedState{'saveStatus'});
1.72      ng       3741: 
                   3742:     $result.='<form action="/adm/grades" method="post" name="gradingMenu">'."\n".
                   3743: 	'<input type="hidden" name="symb"        value="'.$symb.'" />'."\n".
                   3744: 	'<input type="hidden" name="url"         value="'.$url.'" />'."\n".
                   3745: 	'<input type="hidden" name="handgrade"   value="'.$hdgrade.'" />'."\n".
                   3746: 	'<input type="hidden" name="probTitle"   value="'.$probTitle.'" />'."\n".
1.116     ng       3747: 	'<input type="hidden" name="command"     value="" />'."\n".
1.77      ng       3748: 	'<input type="hidden" name="saveState"   value="" />'."\n".
1.124     ng       3749: 	'<input type="hidden" name="gradingMenu" value="1" />'."\n".
1.72      ng       3750: 	'<input type="hidden" name="showgrading" value="yes" />'."\n";
                   3751: 
1.116     ng       3752:     $result.='<table width="100%" border=0><tr><td bgcolor=#777777>'."\n".
                   3753: 	'<table width=100% border=0><tr bgcolor="#e6ffff"><td colspan="2">'."\n".
1.72      ng       3754: 	'&nbsp;<b>Select a Grading/Viewing Option</b></td></tr>'."\n".
1.116     ng       3755: 	'<tr bgcolor="#ffffe6" valign="top"><td>'."\n";
                   3756: 
                   3757:     $result.='<table width="100%" border=0>';
                   3758:     $result.='<tr bgcolor="#ffffe6" valign="top"><td>'."\n".
1.118     ng       3759: 	'&nbsp;Select Section: <select name="section">'."\n";
1.116     ng       3760:     if (ref($sections)) {
                   3761: 	foreach (sort (@$sections)) {$result.='<option value="'.$_.'" '.
                   3762: 					 ($saveSec eq $_ ? 'selected="on"' : '').'>'.$_.'</option>'."\n";}
                   3763:     }
                   3764:     $result.= '<option value="all" '.($saveSec eq 'all' ? 'selected="on"' : ''). '>all</select> &nbsp; ';
                   3765: 
                   3766:     $result.='Student Status:</b>'.&Apache::lonhtmlcommon::StatusOptions($saveStatus,undef,1,undef);
1.72      ng       3767: 
1.116     ng       3768:     if (ref($sections)) {
                   3769: 	$result.='&nbsp;(Section "no" implies the students were not assigned a section.)<br />' 
                   3770: 	    if (grep /no/,@$sections);
                   3771:     }
                   3772:     $result.='</td></tr>';
                   3773: 
1.118     ng       3774:     $result.='<tr bgcolor="#ffffe6"valign="top"><td>'.
                   3775: 	'<input type="radio" name="radioChoice" value="submission" '.
1.145     albertel 3776: 	($saveCmd eq 'submission' ? 'checked' : '').'> '.'<b>Current Resource:</b> For one or more students '.
                   3777: 	'<select name="submitonly">'.
                   3778: 	'<option value="yes" '.
                   3779: 	($saveSub eq 'yes' ? 'selected="on"' : '').'>with submissions</option>'.
                   3780: 	'<option value="graded" '.
                   3781: 	($saveSub eq 'graded' ? 'selected="on"' : '').'>with ungraded submissions</option>'.
                   3782: 	'<option value="all" '.
                   3783: 	($saveSub eq 'all' ? 'selected="on"' : '').'>with any status</option></select></td></tr>'."\n";
1.72      ng       3784: 
1.116     ng       3785:     $result.='<tr bgcolor="#ffffe6"valign="top"><td>'.
                   3786: 	'<input type="radio" name="radioChoice" value="viewgrades" '.
1.76      ng       3787: 	($saveCmd eq 'viewgrades' ? 'checked' : '').'> '.
1.118     ng       3788: 	'<b>Current Resource:</b> For all students in selected section or course</td></tr>'."\n";
1.72      ng       3789: 
1.118     ng       3790:     $result.='<tr bgcolor="#ffffe6" valign="top"><td>'.
                   3791: 	'<input type="radio" name="radioChoice" value="pickStudentPage" '.
                   3792: 	($saveCmd eq 'pickStudentPage' ? 'checked' : '').'> '.
                   3793: 	'The <b>complete</b> set/page/sequence: For one student</td></tr>'."\n";
1.46      ng       3794: 
1.116     ng       3795:     $result.='<tr bgcolor="#ffffe6"><td><br />'.
1.126     ng       3796: 	'<input type="button" onClick="javascript:checkChoice(this.form,\'2\');" value="Next->" />'.
1.116     ng       3797: 	'</td></tr></table>'."\n";
                   3798: 
                   3799:     $result.='</td><td valign="top">';
                   3800: 
                   3801:     $result.='<table width="100%" border=0>';
                   3802:     $result.='<tr bgcolor="#ffffe6"><td>'.
                   3803: 	'<input type="button" onClick="javascript:checkChoice(this.form,\'3\',\'csvform\');" value="Upload" />'.
                   3804: 	' scores from file </td></tr>'."\n";
1.72      ng       3805: 
1.75      albertel 3806:     $result.='<tr bgcolor="#ffffe6"valign="top"><td colspan="2">'.
1.116     ng       3807: 	'<input type="button" onClick="javascript:checkChoice(this.form,\'4\',\'scantron_selectphase\');'.
                   3808: 	'" value="Grade" /> scantron forms</td></tr>'."\n";
1.75      albertel 3809: 
1.72      ng       3810:     if ((&Apache::lonnet::allowed('mgr',$ENV{'request.course.id'})) && ($symb)) {
                   3811: 	$result.='<tr bgcolor="#ffffe6"valign="top"><td>'.
1.116     ng       3812: 	    '<input type="button" onClick="javascript:checkChoice(this.form,\'5\',\'verify\');" value="Verify" />'.
                   3813: 	    ' submission Receipt no: '.unpack("%32C*",$Apache::lonnet::perlvar{'lonHostID'}).
1.72      ng       3814: 	    '-<input type="text" name="receipt" size="4" onChange="javascript:checkReceiptNo(this.form,\'OK\')">'.
                   3815: 	    '</td></tr>'."\n";
                   3816:     } 
1.44      ng       3817: 
1.116     ng       3818:     $result.='</form></td></tr></table>'."\n".
1.72      ng       3819: 	'</td></tr></table>'."\n".
                   3820: 	'</td></tr></table>'."\n";
1.44      ng       3821:     return $result;
1.2       albertel 3822: }
                   3823: 
1.1       albertel 3824: sub handler {
1.41      ng       3825:     my $request=$_[0];
1.102     albertel 3826: 
1.103     albertel 3827:     undef(%perm);
1.41      ng       3828:     if ($ENV{'browser.mathml'}) {
1.141     www      3829: 	&Apache::loncommon::content_type($request,'text/xml');
1.41      ng       3830:     } else {
1.141     www      3831: 	&Apache::loncommon::content_type($request,'text/html');
1.41      ng       3832:     }
                   3833:     $request->send_http_header;
1.44      ng       3834:     return '' if $request->header_only;
1.41      ng       3835:     &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'});
                   3836:     my $url=$ENV{'form.url'};
                   3837:     my $symb=$ENV{'form.symb'};
                   3838:     my $command=$ENV{'form.command'};
                   3839:     if (!$url) {
                   3840: 	my ($temp1,$temp2);
1.136     www      3841: 	($temp1,$temp2,$ENV{'form.url'})=&Apache::lonnet::decode_symb($symb);
1.41      ng       3842: 	$url = $ENV{'form.url'};
                   3843:     }
                   3844:     &send_header($request);
                   3845:     if ($url eq '' && $symb eq '') {
                   3846: 	if ($ENV{'user.adv'}) {
                   3847: 	    if (($ENV{'form.codeone'}) && ($ENV{'form.codetwo'}) &&
                   3848: 		($ENV{'form.codethree'})) {
                   3849: 		my $token=$ENV{'form.codeone'}.'*'.$ENV{'form.codetwo'}.'*'.
                   3850: 		    $ENV{'form.codethree'};
                   3851: 		my ($tsymb,$tuname,$tudom,$tcrsid)=
                   3852: 		    &Apache::lonnet::checkin($token);
                   3853: 		if ($tsymb) {
1.137     albertel 3854: 		    my ($map,$id,$url)=&Apache::lonnet::decode_symb($tsymb);
1.41      ng       3855: 		    if (&Apache::lonnet::allowed('mgr',$tcrsid)) {
1.99      albertel 3856: 			$request->print(&Apache::lonnet::ssi_body('/res/'.$url,
                   3857: 					  ('grade_username' => $tuname,
                   3858: 					   'grade_domain' => $tudom,
                   3859: 					   'grade_courseid' => $tcrsid,
                   3860: 					   'grade_symb' => $tsymb)));
1.41      ng       3861: 		    } else {
1.45      ng       3862: 			$request->print('<h3>Not authorized: '.$token.'</h3>');
1.99      albertel 3863: 		    }
1.41      ng       3864: 		} else {
1.45      ng       3865: 		    $request->print('<h3>Not a valid DocID: '.$token.'</h3>');
1.41      ng       3866: 		}
1.14      www      3867: 	    } else {
1.41      ng       3868: 		$request->print(&Apache::lonxml::tokeninputfield());
                   3869: 	    }
                   3870: 	}
                   3871:     } else {
1.103     albertel 3872: 	if (!($perm{'vgr'}=&Apache::lonnet::allowed('vgr',$ENV{'request.course.id'}))) {
                   3873: 	    if ($perm{'vgr'}=&Apache::lonnet::allowed('vgr',$ENV{'request.course.id'}.'/'.$ENV{'request.course.sec'})) {
                   3874: 		$perm{'vgr_section'}=$ENV{'request.course.sec'};
1.102     albertel 3875: 	    } else {
1.103     albertel 3876: 		delete($perm{'vgr'});
1.102     albertel 3877: 	    }
                   3878: 	}
1.103     albertel 3879: 	if (!($perm{'mgr'}=&Apache::lonnet::allowed('mgr',$ENV{'request.course.id'}))) {
                   3880: 	    if ($perm{'mgr'}=&Apache::lonnet::allowed('mgr',$ENV{'request.course.id'}.'/'.$ENV{'request.course.sec'})) {
                   3881: 		$perm{'mgr_section'}=$ENV{'request.course.sec'};
1.102     albertel 3882: 	    } else {
1.103     albertel 3883: 		delete($perm{'mgr'});
1.102     albertel 3884: 	    }
                   3885: 	}
                   3886: 
1.104     albertel 3887: 	if ($command eq 'submission' && $perm{'vgr'}) {
1.68      ng       3888: 	    ($ENV{'form.student'} eq '' ? &listStudents($request) : &submission($request,0,0));
1.103     albertel 3889: 	} elsif ($command eq 'pickStudentPage' && $perm{'vgr'}) {
1.68      ng       3890: 	    &pickStudentPage($request);
1.103     albertel 3891: 	} elsif ($command eq 'displayPage' && $perm{'vgr'}) {
1.68      ng       3892: 	    &displayPage($request);
1.104     albertel 3893: 	} elsif ($command eq 'gradeByPage' && $perm{'mgr'}) {
1.71      ng       3894: 	    &updateGradeByPage($request);
1.104     albertel 3895: 	} elsif ($command eq 'processGroup' && $perm{'vgr'}) {
1.41      ng       3896: 	    &processGroup($request);
1.104     albertel 3897: 	} elsif ($command eq 'gradingmenu' && $perm{'vgr'}) {
1.41      ng       3898: 	    $request->print(&gradingmenu($request));
1.104     albertel 3899: 	} elsif ($command eq 'viewgrades' && $perm{'vgr'}) {
1.41      ng       3900: 	    $request->print(&viewgrades($request));
1.104     albertel 3901: 	} elsif ($command eq 'handgrade' && $perm{'mgr'}) {
1.41      ng       3902: 	    $request->print(&processHandGrade($request));
1.106     albertel 3903: 	} elsif ($command eq 'editgrades' && $perm{'mgr'}) {
1.41      ng       3904: 	    $request->print(&editgrades($request));
1.106     albertel 3905: 	} elsif ($command eq 'verify' && $perm{'vgr'}) {
1.41      ng       3906: 	    $request->print(&verifyreceipt($request));
1.106     albertel 3907: 	} elsif ($command eq 'csvform' && $perm{'mgr'}) {
1.72      ng       3908: 	    $request->print(&upcsvScores_form($request));
1.106     albertel 3909: 	} elsif ($command eq 'csvupload' && $perm{'mgr'}) {
1.41      ng       3910: 	    $request->print(&csvupload($request));
1.106     albertel 3911: 	} elsif ($command eq 'csvuploadmap' && $perm{'mgr'} ) {
1.41      ng       3912: 	    $request->print(&csvuploadmap($request));
1.106     albertel 3913: 	} elsif ($command eq 'csvuploadassign' && $perm{'mgr'}) {
1.41      ng       3914: 	    if ($ENV{'form.associate'} ne 'Reverse Association') {
                   3915: 		$request->print(&csvuploadassign($request));
                   3916: 	    } else {
                   3917: 		if ( $ENV{'form.upfile_associate'} ne 'reverse' ) {
                   3918: 		    $ENV{'form.upfile_associate'} = 'reverse';
                   3919: 		} else {
                   3920: 		    $ENV{'form.upfile_associate'} = 'forward';
                   3921: 		}
                   3922: 		$request->print(&csvuploadmap($request));
                   3923: 	    }
1.106     albertel 3924: 	} elsif ($command eq 'scantron_selectphase' && $perm{'mgr'}) {
1.75      albertel 3925: 	    $request->print(&scantron_selectphase($request));
1.142     albertel 3926: 	} elsif ($command eq 'scantron_validate' && $perm{'mgr'}) {
                   3927: 	    $request->print(&scantron_validate_file($request));
1.106     albertel 3928: 	} elsif ($command eq 'scantron_process' && $perm{'mgr'}) {
1.82      albertel 3929: 	    $request->print(&scantron_process_students($request));
1.106     albertel 3930: 	} elsif ($command) {
                   3931: 	    $request->print("Access Denied");
1.26      albertel 3932: 	}
1.2       albertel 3933:     }
1.41      ng       3934:     &send_footer($request);
1.44      ng       3935:     return '';
                   3936: }
                   3937: 
                   3938: sub send_header {
                   3939:     my ($request)= @_;
                   3940:     $request->print(&Apache::lontexconvert::header());
                   3941: #  $request->print("
                   3942: #<script>
                   3943: #remotewindow=open('','homeworkremote');
                   3944: #remotewindow.close();
                   3945: #</script>"); 
1.47      www      3946:     $request->print(&Apache::loncommon::bodytag('Grading'));
1.150   ! albertel 3947:     foreach my $key (sort(keys(%ENV))) {
        !          3948: 	if ($key =~ /^form\./) {
        !          3949: 	    Apache->request->print("$key => $ENV{$key} <br />");
        !          3950: 	}
        !          3951:     }
1.44      ng       3952: }
                   3953: 
                   3954: sub send_footer {
                   3955:     my ($request)= @_;
                   3956:     $request->print('</body>');
                   3957:     $request->print(&Apache::lontexconvert::footer());
1.1       albertel 3958: }
                   3959: 
                   3960: 1;
                   3961: 
1.13      albertel 3962: __END__;

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