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

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

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