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

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

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