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

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

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