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

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

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