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

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

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