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

1.17      albertel    1: # The LearningOnline Network with CAPA
1.13      albertel    2: # The LON-CAPA Grading handler
1.17      albertel    3: #
1.50    ! albertel    4: # $Id: grades.pm,v 1.49 2002/09/20 23:35:30 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.30      ng         34: #
1.1       albertel   35: 
                     36: package Apache::grades;
                     37: use strict;
                     38: use Apache::style;
                     39: use Apache::lonxml;
                     40: use Apache::lonnet;
1.3       albertel   41: use Apache::loncommon;
1.1       albertel   42: use Apache::lonhomework;
1.38      ng         43: use Apache::lonmsg qw(:user_normal_msg);
1.1       albertel   44: use Apache::Constants qw(:common);
                     45: 
1.44      ng         46: # ----- These first few routines are general use routines.-----
                     47: #
                     48: # --- Retrieve the parts that matches stores_\d+ from the metadata file.---
                     49: sub getpartlist {
                     50:     my ($url) = @_;
                     51:     my @parts =();
                     52:     my (@metakeys) = split(/,/,&Apache::lonnet::metadata($url,'keys'));
                     53:     foreach my $key (@metakeys) {
                     54: 	if ( $key =~ m/stores_([0-9]+)_.*/) {
                     55: 	    push(@parts,$key);
1.41      ng         56: 	}
1.16      albertel   57:     }
1.44      ng         58:     return @parts;
1.2       albertel   59: }
                     60: 
1.44      ng         61: # --- Get the symbolic name of a problem and the url
                     62: sub get_symb_and_url {
                     63:     my ($request) = @_;
                     64:     (my $url=$ENV{'form.url'}) =~ s-^http://($ENV{'SERVER_NAME'}|$ENV{'HTTP_HOST'})--;
1.41      ng         65:     my $symb=($ENV{'form.symb'} ne '' ? $ENV{'form.symb'} : (&Apache::lonnet::symbread($url)));
1.44      ng         66:     if ($symb eq '') { $request->print("Unable to handle ambiguous references:$url:."); return ''; }
                     67:     return ($symb,$url);
1.32      ng         68: }
                     69: 
1.44      ng         70: # --- Retrieve the fullname for a user. Return lastname, first middle ---
                     71: # --- Generation is attached next to the lastname if it exists. ---
1.34      ng         72: sub get_fullname {
1.39      ng         73:     my ($uname,$udom) = @_;
1.34      ng         74:     my %name=&Apache::lonnet::get('environment', ['lastname','generation',
1.41      ng         75: 						  'firstname','middlename'],$udom,$uname);
1.34      ng         76:     my $fullname;
                     77:     my ($tmp) = keys(%name);
                     78:     if ($tmp !~ /^(con_lost|error|no_such_host)/i) {
                     79: 	$fullname=$name{'lastname'}.$name{'generation'};
                     80: 	if ($fullname =~ /[^\s]+/) { $fullname.=', '; }
                     81: 	$fullname.=$name{'firstname'}.' '.$name{'middlename'};
                     82:     }
                     83:     return $fullname;
                     84: }
                     85: 
1.44      ng         86: #--- Get the partlist and the response type for a given problem. ---
                     87: #--- Indicate if a response type is coded handgraded or not. ---
1.39      ng         88: sub response_type {
1.41      ng         89:     my ($url) = shift;
                     90:     my $allkeys = &Apache::lonnet::metadata($url,'keys');
                     91:     my %seen = ();
                     92:     my (@partlist,%handgrade);
                     93:     foreach (split(/,/,&Apache::lonnet::metadata($url,'packages'))) {
1.43      ng         94: 	if (/^\w+response_\d+.*/) {
1.41      ng         95: 	    my ($responsetype,$part) = split(/_/,$_,2);
                     96: 	    my ($partid,$respid) = split(/_/,$part);
                     97: 	    $handgrade{$part} = $responsetype.':'.($allkeys =~ /parameter_$part\_handgrade/ ? 'yes' : 'no');
                     98: 	    next if ($seen{$partid} > 0);
                     99: 	    $seen{$partid}++;
                    100: 	    push @partlist,$partid;
                    101: 	}
                    102:     }
                    103:     return \@partlist,\%handgrade;
1.39      ng        104: }
                    105: 
1.44      ng        106: #--- Dumps the class list with usernames,list of sections,
                    107: #--- section, ids and fullnames for each user.
                    108: sub getclasslist {
                    109:     my ($getsec,$hideexpired) = @_;
                    110:     my $now = time;
                    111:     my %classlist=&Apache::lonnet::dump('classlist',
                    112: 					$ENV{'course.'.$ENV{'request.course.id'}.'.domain'},
                    113: 					$ENV{'course.'.$ENV{'request.course.id'}.'.num'});
1.49      albertel  114:     my ($tmp) = keys(%classlist);
                    115:     # Bail out if we were unable to get the classlist
                    116:     return if ($tmp =~ /^(con_lost|error|no_such_host)/i);
                    117: 
1.44      ng        118:     # codes to check for fields in the classlist
                    119:     # should contain end:start:id:section:fullname
                    120:     for (keys %classlist) {
                    121: 	my (@fields) = split(/:/,$classlist{$_});
                    122: 	%classlist   = &reformat_classlist(\%classlist) if (scalar(@fields) <= 2);
                    123: 	last;
                    124:     }
                    125: 
                    126:     my (@holdsec,@sections,%allids,%stusec,%fullname);
                    127:     foreach (keys(%classlist)) {
                    128: 	my ($end,$start,$id,$section,$fullname)=split(/:/,$classlist{$_});
                    129: 	# still a student?
                    130: 	if (($hideexpired) && ($end) && ($end < $now)) {
                    131: 	    next;
                    132: 	}
                    133: 	$section = ($section ne '' ? $section : 'no');
                    134: 	push @holdsec,$section;
                    135: 	if ($getsec eq 'all' || $getsec eq $section) {
                    136: 	    push (@{ $classlist{$getsec} }, $_);
                    137: 	    $allids{$_}  =$id;
                    138: 	    $stusec{$_}  =$section;
                    139: 	    $fullname{$_}=$fullname;
                    140: 	}
                    141:     }
                    142:     my %seen = ();
                    143:     foreach my $item (@holdsec) {
                    144: 	push (@sections, $item) unless $seen{$item}++;
                    145:     }
                    146:     return (\%classlist,\@sections,\%allids,\%stusec,\%fullname);
                    147: }
                    148: 
                    149: # add id, section and fullname to the classlist.db
                    150: # done to maintain backward compatibility with older versions
                    151: sub reformat_classlist {
                    152:     my ($classlist) = shift;
                    153:     foreach (sort keys(%$classlist)) {
                    154: 	my ($unam,$udom) = split(/:/);
                    155: 	my $section      = &Apache::lonnet::usection($udom,$unam,$ENV{'request.course.id'});
                    156: 	my $fullname     = &get_fullname ($unam,$udom);
                    157: 	my %userid       = &Apache::lonnet::idrget($udom,($unam));
                    158: 	$$classlist{$_}  = $$classlist{$_}.':'.$userid{$unam}.':'.$section.':'.$fullname;
                    159:     }
                    160:     my $putresult = &Apache::lonnet::put
                    161: 	('classlist',\%$classlist,
                    162: 	 $ENV{'course.'.$ENV{'request.course.id'}.'.domain'},
                    163: 	 $ENV{'course.'.$ENV{'request.course.id'}.'.num'});
                    164: 
                    165:     return %$classlist;
                    166: }
                    167: 
                    168: #find user domain
                    169: sub finduser {
                    170:     my ($name) = @_;
                    171:     my $domain = '';
                    172:     if ( $Apache::grades::viewgrades eq 'F' ) {
                    173: 	my %classlist=&Apache::lonnet::dump('classlist',
                    174: 					    $ENV{'course.'.$ENV{'request.course.id'}.'.domain'},
                    175: 					    $ENV{'course.'.$ENV{'request.course.id'}.'.num'});
                    176: 	my (@fields) = grep /^$name:/, keys %classlist;
                    177: 	($name, $domain) = split(/:/,$fields[0]);
                    178: 	return ($name,$domain);
                    179:     } else {
                    180: 	return ($ENV{'user.name'},$ENV{'user.domain'});
                    181:     }
                    182: }
                    183: 
                    184: #--- Prompts a user to enter a username.
                    185: sub moreinfo {
                    186:     my ($request,$reason) = @_;
                    187:     $request->print("Unable to process request: $reason");
                    188:     if ( $Apache::grades::viewgrades eq 'F' ) {
                    189: 	$request->print('<form action="/adm/grades" method="post">'."\n");
                    190: 	if ($ENV{'form.url'}) {
                    191: 	    $request->print('<input type="hidden" name="url" value="'.$ENV{'form.url'}.'" />'."\n");
                    192: 	}
                    193: 	if ($ENV{'form.symb'}) {
                    194: 	    $request->print('<input type="hidden" name="symb" value="'.$ENV{'form.symb'}.'" />'."\n");
                    195: 	}
                    196: 	$request->print('<input type="hidden" name="command" value="'.$ENV{'form.command'}.'" />'."\n");
                    197: 	$request->print("Student:".'<input type="text" name="student" value="'.$ENV{'form.student'}.'" />'."<br />\n");
                    198: 	$request->print("Domain:".'<input type="text" name="domain" value="'.$ENV{'user.domain'}.'" />'."<br />\n");
                    199: 	$request->print('<input type="submit" name="submit" value="ReSubmit" />'."<br />\n");
                    200: 	$request->print('</form>');
                    201:     }
                    202:     return '';
                    203: }
                    204: 
                    205: #--- Retrieve the grade status of a student for all the parts
                    206: sub student_gradeStatus {
                    207:     my ($url,$symb,$udom,$uname,$partlist) = @_;
                    208:     my %record     = &Apache::lonnet::restore($symb,$ENV{'request.course.id'},$udom,$uname);
                    209:     my %partstatus = ();
                    210:     foreach (@$partlist) {
                    211: 	my ($status,$foo)    = split(/_/,$record{"resource.$_.solved"},2);
                    212: 	$status              = 'nothing' if ($status eq '');
                    213: 	$partstatus{$_}      = $status;
                    214: 	my $subkey           = "resource.$_.submitted_by";
                    215: 	$partstatus{$subkey} = $record{$subkey} if ($record{$subkey} ne '');
                    216:     }
                    217:     return %partstatus;
                    218: }
                    219: 
1.45      ng        220: # hidden form and javascript that calls the form
                    221: # Use by verifyscript and viewgrades
                    222: # Shows a student's view of problem and submission
                    223: sub jscriptNform {
                    224:     my ($url,$symb) = @_;
                    225:     my $jscript='<script type="text/javascript" language="javascript">'."\n".
                    226: 	'    function viewOneStudent(user,domain) {'."\n".
                    227: 	'	document.onestudent.student.value = user;'."\n".
                    228: 	'	document.onestudent.userdom.value = domain;'."\n".
                    229: 	'	document.onestudent.submit();'."\n".
                    230: 	'    }'."\n".
                    231: 	'</script>'."\n";
                    232:     $jscript.= '<form action="/adm/grades" method="post" name="onestudent">'."\n".
                    233: 	'<input type="hidden" name="symb"    value="'.$symb.'" />'."\n".
                    234: 	'<input type="hidden" name="url"     value="'.$url.'" />'."\n".
                    235: 	'<input type="hidden" name="command" value="submission" />'."\n".
                    236: 	'<input type="hidden" name="student" value="" />'."\n".
                    237: 	'<input type="hidden" name="userdom" value="" />'."\n".
                    238: 	'</form>'."\n";
                    239:     return $jscript;
                    240: }
1.39      ng        241: 
1.44      ng        242: #------------------ End of general use routines --------------------
                    243: #-------------------------------------------------------------------
                    244: 
                    245: #------------------------------------ Receipt Verification Routines
1.45      ng        246: #
1.44      ng        247: #--- Check whether a receipt number is valid.---
                    248: sub verifyreceipt {
                    249:     my $request  = shift;
                    250: 
                    251:     my $courseid = $ENV{'request.course.id'};
                    252:     my $receipt  = unpack("%32C*",$Apache::lonnet::perlvar{'lonHostID'}).'-'.
                    253: 	$ENV{'form.receipt'};
                    254:     $receipt     =~ s/[^\-\d]//g;
                    255:     my $url      = $ENV{'form.url'};
                    256:     my $symb     = $ENV{'form.symb'};
                    257:     unless ($symb) {
                    258: 	$symb    = &Apache::lonnet::symbread($url);
                    259:     }
                    260: 
1.45      ng        261:     my $title.='<h3><font color="#339933">Verifying Submission Receipt '.
                    262: 	$receipt.'</h3></font>'."\n".
1.44      ng        263: 	'<font size=+1><b>Resource: </b>'.$ENV{'form.url'}.'</font><br><br>'."\n";
                    264: 
                    265:     my ($string,$contents,$matches) = ('','',0);
                    266:     my ($classlist,$seclist,$ids,$stusec,$fullname) = &getclasslist('all','0');
                    267:     
                    268:     foreach (sort {$$fullname{$a} cmp $$fullname{$b} } keys %$fullname) {
                    269: 	my ($uname,$udom)=split(/\:/);
                    270: 	if ($receipt eq 
                    271: 	    &Apache::lonnet::ireceipt($uname,$udom,$courseid,$symb)) {
                    272: 	    $contents.='<tr bgcolor="#ffffe6"><td>&nbsp;'."\n".
                    273: 		'<a href="javascript:viewOneStudent(\''.$uname.'\',\''.$udom.
                    274: 		'\')"; TARGET=_self>'.$$fullname{$_}.'</a>&nbsp;</td>'."\n".
                    275: 		'<td>&nbsp;'.$uname.'&nbsp;</td>'.
                    276: 		'<td>&nbsp;'.$udom.'&nbsp;</td></tr>'."\n";
                    277: 	    
                    278: 	    $matches++;
                    279: 	}
                    280:     }
                    281:     if ($matches == 0) {
                    282: 	$string = $title.'No match found for the above receipt.';
                    283:     } else {
1.45      ng        284: 	$string = &jscriptNform($url,$symb).$title.
1.44      ng        285: 	    'The above receipt matches the following student'.
                    286: 	    ($matches <= 1 ? '.' : 's.')."\n".
                    287: 	    '<table border="0"><tr><td bgcolor="#777777">'."\n".
                    288: 	    '<table border="0"><tr bgcolor="#e6ffff">'."\n".
                    289: 	    '<td><b>&nbsp;Fullname&nbsp;</b></td>'."\n".
                    290: 	    '<td><b>&nbsp;Username&nbsp;</b></td>'."\n".
                    291: 	    '<td><b>&nbsp;Domain&nbsp;</b></td></tr>'."\n".
                    292: 	    $contents.
                    293: 	    '</table></td></tr></table>'."\n";
                    294:     }
1.50    ! albertel  295:     return $string.&show_grading_menu_form($symb,$url);
1.44      ng        296: }
                    297: 
                    298: #--- This is called by a number of programs.
                    299: #--- Called from the Grading Menu - View/Grade an individual student
                    300: #--- Also called directly when one clicks on the subm button 
                    301: #    on the problem page.
1.30      ng        302: sub listStudents {
1.41      ng        303:     my ($request) = shift;
1.49      albertel  304: 
                    305:     my ($symb,$url) = &get_symb_and_url();
                    306:     my $cdom      = $ENV{"course.$ENV{'request.course.id'}.domain"};
                    307:     my $cnum      = $ENV{"course.$ENV{'request.course.id'}.num"};
                    308:     my $getsec    = $ENV{'form.section'} eq '' ? 'all' : $ENV{'form.section'};
                    309:     my $submitonly= $ENV{'form.submitonly'} eq '' ? 'all' : $ENV{'form.submitonly'};
                    310: 
                    311:     my $result;
                    312:     my ($partlist,$handgrade) = &response_type($url);
                    313:     for (sort keys(%$handgrade)) {
                    314: 	my ($responsetype,$handgrade)=split(/:/,$$handgrade{$_});
                    315: 	$ENV{'form.handgrade'} = 'yes' if ($handgrade eq 'yes');
                    316: 	$result.='<tr><td><b>Part </b>'.(split(/_/))[0].'</td>'.
                    317: 	    '<td><b>Type: </b>'.$responsetype.'</td>'.
                    318: 	    '<td><b>Handgrade: </b>'.$handgrade.'</font></td></tr>';
                    319:     }
                    320:     $result.='</table>';
                    321: 
                    322:     my $viewgrade;
                    323:     if ($ENV{'form.handgrade'} eq 'yes') {
                    324: 	$viewgrade = 'View/Grade';
                    325:     } else {
                    326: 	$viewgrade = 'View';
                    327:     }
                    328: 
                    329:     $result='<h3><font color="#339933">&nbsp;'.
                    330: 	$viewgrade.
                    331: 	    ' Submissions for a Student or a Group of Students</font></h3>'.
                    332: 		'<table border="0"><tr><td colspan=3><font size=+1>'.
                    333: 		    '<b>Resource: </b>'.$url.'</font></td></tr>'.$result;
                    334: 
1.45      ng        335:     $request->print(<<LISTJAVASCRIPT);
                    336: <script type="text/javascript" language="javascript">
                    337:   function checkSelect(checkBox) {
                    338:     var ctr=0;
1.46      ng        339:     var sense="";
1.45      ng        340:     if (checkBox.length > 1) {
                    341:        for (var i=0; i<checkBox.length; i++) {
                    342: 	  if (checkBox[i].checked) {
                    343: 	     ctr++;
                    344: 	  }
                    345:        }
1.46      ng        346:        sense = "a student or group of students";
1.45      ng        347:     } else {
                    348:        if (checkBox.checked) {
                    349: 	   ctr = 1;
                    350:        }
1.46      ng        351:        sense = "the student";
1.45      ng        352:     }
                    353:     if (ctr == 0) {
1.49      albertel  354:        alert("Please select "+sense+" before clicking on the $viewgrade button.");
1.45      ng        355:        return false;
                    356:     }
                    357:     document.gradesub.submit();
                    358:   }
                    359: </script>
                    360: LISTJAVASCRIPT
                    361: 
1.41      ng        362:     $request->print($result);
1.39      ng        363: 
1.45      ng        364:     my $checkhdgrade = $ENV{'form.handgrade'} eq 'yes' ? 'checked' : '';
                    365:     my $checklastsub = $ENV{'form.handgrade'} eq 'yes' ? '' : 'checked';
1.44      ng        366: 
1.45      ng        367:     my $gradeTable='<form action="/adm/grades" method="post" name="gradesub">'."\n".
                    368: 	'&nbsp;<b>View Problem: </b><input type="radio" name="vProb" value="no" checked> no '."\n".
                    369: 	'<input type="radio" name="vProb" value="yes"> yes <br />'."\n".
1.49      albertel  370: 	'&nbsp;<b>Submissions: </b>'."\n";
                    371:     if ($ENV{'form.handgrade'} eq 'yes') {
                    372: 	$gradeTable.='<input type="radio" name="lastSub" value="hdgrade" '.$checkhdgrade.' /> handgrade only'."\n";
                    373:     }
                    374:     $gradeTable.='<input type="radio" name="lastSub" value="lastonly" '.$checklastsub.' /> last sub only'."\n".
1.45      ng        375: 	'<input type="radio" name="lastSub" value="last" /> last sub & parts info'."\n".
                    376: 	'<input type="radio" name="lastSub" value="all" /> all details'."\n".
                    377: 	'<input type="hidden" name="section"     value="'.$getsec.'" />'."\n".
                    378: 	'<input type="hidden" name="submitonly"  value="'.$submitonly.'" />'."\n".
                    379: 	'<input type="hidden" name="response"    value="'.$ENV{'form.response'}.'" />'."\n".
                    380: 	'<input type="hidden" name="handgrade"   value="'.$ENV{'form.handgrade'}.'" /><br />'."\n".
                    381: 	'<input type="hidden" name="showgrading" value="'.$ENV{'form.showgrading'}.'" /><br />'."\n".
1.48      albertel  382: 	'<input type="hidden" name="url"  value="'.$url.'" />'."\n".
                    383: 	'<input type="hidden" name="symb" value="'.$symb.'" />'."\n".
1.49      albertel  384: 	'To '.lc($viewgrade).' a submission, click on the check box next to the student\'s name. Then '."\n".
                    385: 	'click on the '.$viewgrade.' button. To view the submissions for a group of students, click'."\n".
1.45      ng        386: 	' on the check boxes for the group of students.<br />'."\n".
                    387: 	'<input type="hidden" name="command" value="processGroup" />'."\n".
                    388: 	'<input type="button" '."\n".
                    389: 	'onClick="javascript:checkSelect(this.form.stuinfo);" '."\n".
1.49      albertel  390: 	'value="'.$viewgrade.'" />'."\n";
1.45      ng        391:  
1.41      ng        392:     my ($classlist,$seclist,$ids,$stusec,$fullname) = &getclasslist($getsec,'0');
                    393:     
1.45      ng        394:     $gradeTable.='<table border="0"><tr><td bgcolor="#777777">'.
1.41      ng        395: 	'<table border="0"><tr bgcolor="#e6ffff">'.
1.44      ng        396: 	'<td><b>&nbsp;Select&nbsp;</b></td><td><b>&nbsp;Fullname&nbsp;</b></td>'.
                    397: 	'<td><b>&nbsp;Username&nbsp;</b></td><td><b>&nbsp;Domain&nbsp;</b></td>';
1.41      ng        398:     foreach (sort(@$partlist)) {
1.45      ng        399: 	$gradeTable.='<td><b>&nbsp;Part '.(split(/_/))[0].' Status&nbsp;</b></td>';
1.41      ng        400:     }
1.45      ng        401:     $gradeTable.='</tr>'."\n";
1.41      ng        402: 
1.45      ng        403:     my $ctr = 0;
1.44      ng        404:     foreach my $student (sort {$$fullname{$a} cmp $$fullname{$b} } keys %$fullname) {
1.41      ng        405: 	my ($uname,$udom) = split(/:/,$student);
1.48      albertel  406: 	my (%status) =&student_gradeStatus($url,$symb,$udom,$uname,$partlist);
1.41      ng        407: 	my $statusflg = '';
                    408: 	foreach (keys(%status)) {
                    409: 	    $statusflg = 1 if ($status{$_} ne 'nothing');
1.43      ng        410: 	    my ($foo,$partid,$foo1) = split(/\./,$_);
1.41      ng        411: 	    if ($status{'resource.'.$partid.'.submitted_by'} ne '') {
                    412: 		$statusflg = '';
1.45      ng        413: 		$gradeTable.='<input type="hidden" name="'.
                    414: 		    $student.':submitted_by" value="'.
                    415: 		    $status{'resource.'.$partid.'.submitted_by'}.'" />';
1.41      ng        416: 	    }
                    417: 	}
                    418: 	next if ($statusflg eq '' && $submitonly eq 'yes');
1.34      ng        419: 
1.45      ng        420: 	$ctr++;
1.41      ng        421: 	if ( $Apache::grades::viewgrades eq 'F' ) {
1.45      ng        422: 	    $gradeTable.='<tr bgcolor="#ffffe6">'.
1.41      ng        423: 		'<td align="center"><input type=checkbox name="stuinfo" value="'.
                    424: 		$student.':'.$$fullname{$student}.'"></td>'."\n".
1.44      ng        425: 		'<td>&nbsp;'.$$fullname{$student}.'&nbsp;</td>'."\n".
1.41      ng        426: 		'<td>&nbsp;'.$uname.'&nbsp;</td>'."\n".
                    427: 		'<td align="middle">&nbsp;'.$udom.'&nbsp;</td>'."\n";
                    428: 	    
                    429: 	    foreach (sort keys(%status)) {
                    430: 		next if (/^resource.*?submitted_by$/);
1.45      ng        431: 		$gradeTable.='<td align="middle">&nbsp;'.$status{$_}.'&nbsp;</td>'."\n";
1.41      ng        432: 	    }
1.45      ng        433: 	    $gradeTable.='</tr>'."\n";
1.41      ng        434: 	}
                    435:     }
1.45      ng        436:     $gradeTable.='</table></td></tr></table>'.
                    437: 	'<input type="button" '.
                    438: 	'onClick="javascript:checkSelect(this.form.stuinfo);" '.
1.50    ! albertel  439: 	'value="'.$viewgrade.'" /></form>'."\n";
1.45      ng        440:     if ($ctr == 0) {
                    441: 	$gradeTable='<br />&nbsp;<font color="red">'.
                    442: 	    'No submission found for this resource.</font><br />';
1.46      ng        443:     } elsif ($ctr == 1) {
                    444: 	$gradeTable =~ s/type=checkbox/type=checkbox checked/;
1.45      ng        445:     }
1.50    ! albertel  446:     $gradeTable.=&show_grading_menu_form($symb,$url);
1.45      ng        447:     $request->print($gradeTable);
1.44      ng        448:     return '';
1.10      ng        449: }
                    450: 
1.44      ng        451: #---- Called from the listStudents routine
                    452: #     Displays the submissions for one student or a group of students
1.34      ng        453: sub processGroup {
1.41      ng        454:     my ($request)  = shift;
                    455:     my $ctr        = 0;
                    456:     my @stuchecked = (ref($ENV{'form.stuinfo'}) ? @{$ENV{'form.stuinfo'}}
                    457: 		      : ($ENV{'form.stuinfo'}));
                    458:     my $total      = scalar(@stuchecked)-1;
1.45      ng        459: 
1.41      ng        460:     foreach (@stuchecked) {
                    461: 	my ($uname,$udom,$fullname) = split(/:/);
1.44      ng        462: 	$ENV{'form.student'}        = $uname;
                    463: 	$ENV{'form.userdom'}        = $udom;
                    464: 	$ENV{'form.fullname'}       = $fullname;
1.41      ng        465: 	&submission($request,$ctr,$total);
                    466: 	$ctr++;
                    467:     }
                    468:     return '';
1.35      ng        469: }
1.34      ng        470: 
1.44      ng        471: #------------------------------------------------------------------------------------
                    472: #
                    473: #-------------------------- Next few routines handles grading by student, essentially
                    474: #                           handles essay response type problem/part
                    475: #
                    476: #--- Javascript to handle the submission page functionality ---
                    477: sub sub_page_js {
                    478:     my $request = shift;
                    479:     $request->print(<<SUBJAVASCRIPT);
                    480: <script type="text/javascript" language="javascript">
                    481:   function updateRadio(radioButton,formtextbox,formsel,scores,weight) {
                    482:      var pts = formtextbox.value;
                    483:      var resetbox =false;
                    484:      if (isNaN(pts) || pts < 0) {
                    485:         alert("A number equal or greater than 0 is expected. Entered value = "+pts);
                    486:         for (var i=0; i<radioButton.length; i++) {
                    487:            if (radioButton[i].checked) {
                    488: 	      formtextbox.value = i;
                    489: 	      resetbox = true;
                    490: 	   }
                    491: 	}
                    492: 	if (!resetbox) {
                    493: 	   formtextbox.value = "";
                    494: 	}
                    495: 	return;
                    496:      }
1.13      albertel  497: 
1.44      ng        498:      if (pts > weight) {
                    499:         var resp = confirm("You entered a value ("+pts+
                    500:       		     ") greater than the weight for the part. Accept?");
                    501:         if (resp == false) {
                    502:            formtextbox.value = "";
                    503:            return;
                    504:        }
1.41      ng        505:     }
1.5       albertel  506: 
1.44      ng        507:     for (var i=0; i<radioButton.length; i++) {
                    508: 	radioButton[i].checked=false;
                    509: 	if (pts == i) {
                    510: 	   radioButton[i].checked=true;
1.41      ng        511: 	}
                    512:     }
1.44      ng        513:     updateSelect(formsel);
                    514:     scores.value = "0";
                    515:   }
                    516: 
                    517:   function writeBox(formrad,formsel,pts,scores) {
                    518:     formrad.value = pts;
                    519:     scores.value = "0";
                    520:     updateSelect(formsel,pts);
                    521:     return;
                    522:   }
1.5       albertel  523: 
1.44      ng        524:   function clearRadBox(radioButton,formbox,formsel,scores) {
                    525:     for (var i=0; i<formsel.length; i++) {
                    526: 	if (formsel[i].selected) {
                    527: 	    var selectx=i;
1.41      ng        528: 	}
1.13      albertel  529:     }
1.44      ng        530:     if (selectx == scores.value) { return };
                    531:     formbox.value = "";
                    532:     for (var i=0; i<radioButton.length; i++) {
                    533: 	radioButton[i].checked=false;
1.41      ng        534:     }
1.44      ng        535:     scores.value = selectx;
                    536:   }
1.33      ng        537: 
1.44      ng        538:   function updateSelect(formsel) {
                    539:     formsel[0].selected = true;
                    540:     return;
                    541:   }
                    542: 
1.45      ng        543: //=================== Check that a point is assigned for all the parts  ==============
                    544:   function checksubmit(val,total,parttot) {
                    545:      document.SCORE.gradeOpt.value = val;
                    546:      if (val == "Save & Next") {
                    547: 	for (i=0;i<=total;i++) {
                    548: 	   for (j=0;j<parttot;j++) {
                    549: 	      var partid = eval("document.SCORE.partid"+i+"_"+j+".value");
                    550: 	      var selopt = eval("document.SCORE.GD_SEL"+i+"_"+partid);
                    551: 	      if (selopt[0].selected) {
                    552: 		 var points = eval("document.SCORE.GD_BOX"+i+"_"+partid+".value");
                    553: 		 if (points == "") {
                    554: 		     var name = eval("document.SCORE.name"+i+".value");
1.46      ng        555: 		     alert("Please assign a score for "+name+", part "+partid+".");
1.45      ng        556: 		     return false;
                    557: 		 }
                    558: 	      }
                    559: 
                    560: 	  }
                    561:        }
                    562: 
                    563:      }
                    564:      document.SCORE.submit();
                    565:  }
                    566: 
1.44      ng        567: //===================== Show list of keywords ====================
                    568:   function keywords(keyform) {
                    569:     var keywds = keyform.value;
                    570:     var nret = prompt("Keywords list, separated by a space. Add/delete to list if desired.",keywds);
                    571:     if (nret==null) return;
                    572:     keyform.value = nret;
                    573: 
                    574:     document.SCORE.refresh.value = "on";
                    575:     if (document.SCORE.keywords.value != "") {
                    576: 	document.SCORE.submit();
                    577:     }
                    578:     return;
                    579:   }
                    580: 
                    581: //===================== Script to view submitted by ==================
                    582:   function viewSubmitter(submitter) {
                    583:     document.SCORE.refresh.value = "on";
                    584:     document.SCORE.NCT.value = "1";
                    585:     document.SCORE.unamedom0.value = submitter;
                    586:     document.SCORE.submit();
                    587:     return;
                    588:   }
                    589: 
                    590: //===================== Script to add keyword(s) ==================
                    591:   function getSel() {
                    592:     if (document.getSelection) txt = document.getSelection();
                    593:     else if (document.selection) txt = document.selection.createRange().text;
                    594:     else return;
                    595:     var cleantxt = txt.replace(new RegExp('([\\f\\n\\r\\t\\v ])+', 'g')," ");
                    596:     if (cleantxt=="") {
1.46      ng        597: 	alert("Please select a word or group of words from document and then click this link.");
1.44      ng        598: 	return;
                    599:     }
                    600:     var nret = prompt("Add selection to keyword list? Edit if desired.",cleantxt);
                    601:     if (nret==null) return;
                    602:     var curlist = document.SCORE.keywords.value;
                    603:     document.SCORE.keywords.value = curlist+" "+nret;
                    604:     document.SCORE.refresh.value = "on";
                    605:     if (document.SCORE.keywords.value != "") {
                    606: 	document.SCORE.submit();
                    607:     }
                    608:     return;
                    609:   }
                    610: 
                    611: //====================== Script for composing message ==============
                    612:   function msgCenter(msgform,usrctr,fullname) {
                    613:     var Nmsg  = msgform.savemsgN.value;
                    614:     savedMsgHeader(Nmsg,usrctr,fullname);
                    615:     var subject = msgform.msgsub.value;
                    616:     var rtrchk  = eval("document.SCORE.includemsg"+usrctr);
                    617:     var msgchk = rtrchk.value;
                    618:     re = /msgsub/;
                    619:     var shwsel = "";
                    620:     if (re.test(msgchk)) { shwsel = "checked" }
                    621:     displaySubject(subject,shwsel);
                    622:     for (var i=1; i<=Nmsg; i++) {
                    623: 	var testpt = "savemsg"+i+",";
                    624: 	re = /testpt/;
                    625: 	shwsel = "";
                    626: 	if (re.test(msgchk)) { shwsel = "checked" }
                    627: 	var message = eval("document.SCORE.savemsg"+i+".value");
                    628: 	displaySavedMsg(i,message,shwsel);
                    629:     }
                    630:     newmsg = eval("document.SCORE.newmsg"+usrctr+".value");
                    631:     shwsel = "";
                    632:     re = /newmsg/;
                    633:     if (re.test(msgchk)) { shwsel = "checked" }
                    634:     newMsg(newmsg,shwsel);
                    635:     msgTail(); 
                    636:     return;
                    637:   }
                    638: 
                    639:   function savedMsgHeader(Nmsg,usrctr,fullname) {
                    640:     var height = 30*Nmsg+250;
                    641:     var scrollbar = "no";
                    642:     if (height > 600) {
                    643: 	height = 600;
                    644: 	scrollbar = "yes";
                    645:     }
                    646: /*    if (window.pWin)
                    647: 	window.pWin.close(); */
                    648:     pWin = window.open('', 'MessageCenter', 'toolbar=no,location=no,scrollbars='+scrollbar+',screenx=70,screeny=75,width=600,height='+height);
                    649:     pWin.document.write("<html><head>");
                    650:     pWin.document.write("<title>Message Central</title>");
                    651: 
                    652:     pWin.document.write("<script language=javascript>");
                    653:     pWin.document.write("function checkInput() {");
                    654:     pWin.document.write("  opener.document.SCORE.msgsub.value = document.msgcenter.msgsub.value;");
                    655:     pWin.document.write("  var nmsg   = opener.document.SCORE.savemsgN.value;");
                    656:     pWin.document.write("  var usrctr = document.msgcenter.usrctr.value;");
                    657:     pWin.document.write("  var newval = eval(\\"opener.document.SCORE.newmsg\\"+usrctr);");
                    658:     pWin.document.write("  newval.value = document.msgcenter.newmsg.value;");
                    659: 
                    660:     pWin.document.write("  var msgchk = \\"\\";");
                    661:     pWin.document.write("  if (document.msgcenter.subchk.checked) {");
                    662:     pWin.document.write("     msgchk = \\"msgsub,\\";");
                    663:     pWin.document.write("  }");
                    664:     pWin.document.write(   "for (var i=1; i<=nmsg; i++) {");
                    665:     pWin.document.write("      var opnmsg = eval(\\"opener.document.SCORE.savemsg\\"+i);");
                    666:     pWin.document.write("      var frmmsg = eval(\\"document.msgcenter.msg\\"+i);");
                    667:     pWin.document.write("      opnmsg.value = frmmsg.value;");
                    668:     pWin.document.write("      var chkbox = eval(\\"document.msgcenter.msgn\\"+i);");
                    669:     pWin.document.write("      if (chkbox.checked) {");
                    670:     pWin.document.write("         msgchk += \\"savemsg\\"+i+\\",\\";");
                    671:     pWin.document.write("      }");
                    672:     pWin.document.write("  }");
                    673:     pWin.document.write("  if (document.msgcenter.newmsgchk.checked) {");
                    674:     pWin.document.write("     msgchk += \\"newmsg\\"+usrctr;");
                    675:     pWin.document.write("  }");
                    676:     pWin.document.write("  var includemsg = eval(\\"opener.document.SCORE.includemsg\\"+usrctr);");
                    677:     pWin.document.write("  includemsg.value = msgchk;");
                    678: 
                    679:     pWin.document.write("  self.close()");
                    680: 
                    681:     pWin.document.write("}");
                    682: 
                    683:     pWin.document.write("<");
                    684:     pWin.document.write("/script>");
                    685: 
                    686:     pWin.document.write("</head><body bgcolor=white>");
                    687: 
                    688:     pWin.document.write("<form action=\\"inactive\\" name=\\"msgcenter\\">");
                    689:     pWin.document.write("<input value=\\""+usrctr+"\\" name=\\"usrctr\\" type=\\"hidden\\">");
                    690:     pWin.document.write("<font color=\\"green\\" size=+1>&nbsp;Compose Message for \"+fullname+\"</font><br><br>");
                    691: 
                    692:     pWin.document.write("<table border=0 width=100%><tr><td bgcolor=\\"#777777\\">");
                    693:     pWin.document.write("<table border=0 width=100%><tr bgcolor=\\"#ddffff\\">");
                    694:     pWin.document.write("<td><b>Type</b></td><td><b>Include</b></td><td><b>Message</td></tr>");
                    695: }
                    696:     function displaySubject(msg,shwsel) {
                    697:     pWin.document.write("<tr bgcolor=\\"#ffffdd\\">");
                    698:     pWin.document.write("<td>Subject</td>");
                    699:     pWin.document.write("<td align=\\"center\\"><input name=\\"subchk\\" type=\\"checkbox\\"" +shwsel+"></td>");
                    700:     pWin.document.write("<td><input name=\\"msgsub\\" type=\\"text\\" value=\\""+msg+" \\"size=\\"60\\" maxlength=\\"80\\"></td></tr>");
                    701: }
                    702: 
                    703: function displaySavedMsg(ctr,msg,shwsel) {
                    704:     pWin.document.write("<tr bgcolor=\\"#ffffdd\\">");
                    705:     pWin.document.write("<td align=\\"center\\">"+ctr+"</td>");
                    706:     pWin.document.write("<td align=\\"center\\"><input name=\\"msgn"+ctr+"\\" type=\\"checkbox\\"" +shwsel+"></td>");
                    707:     pWin.document.write("<td><input name=\\"msg"+ctr+"\\" type=\\"text\\" value=\\""+msg+" \\" size=\\"60\\" maxlength=\\"80\\"></td></tr>");
                    708: }
                    709: 
                    710:   function newMsg(newmsg,shwsel) {
                    711:     pWin.document.write("<tr bgcolor=\\"#ffffdd\\">");
                    712:     pWin.document.write("<td align=\\"center\\">New</td>");
                    713:     pWin.document.write("<td align=\\"center\\"><input name=\\"newmsgchk\\" type=\\"checkbox\\"" +shwsel+"></td>");
                    714:     pWin.document.write("<td><input name=\\"newmsg\\" type=\\"text\\" onchange=\\"javascript:this.form.newmsgchk.checked=true\\" value=\\""+newmsg+" \\" size=\\"60\\" maxlength=\\"80\\"></td></tr>");
                    715: }
                    716: 
                    717:   function msgTail() {
                    718:     pWin.document.write("</table>");
                    719:     pWin.document.write("</td></tr></table>&nbsp;");
                    720:     pWin.document.write("<input type=\\"button\\" value=\\"Save\\" onClick=\\"javascript:checkInput()\\">&nbsp;&nbsp;");
                    721:     pWin.document.write("<input type=\\"button\\" value=\\"Cancel\\" onClick=\\"self.close()\\"><br><br>");
                    722:     pWin.document.write("</form>");
                    723:     pWin.document.write("</body></html>");
                    724: }
                    725: 
                    726: //====================== Script for keyword highlight options ==============
                    727:   function kwhighlight() {
                    728:     var kwclr    = document.SCORE.kwclr.value;
                    729:     var kwsize   = document.SCORE.kwsize.value;
                    730:     var kwstyle  = document.SCORE.kwstyle.value;
                    731:     var redsel = "";
                    732:     var grnsel = "";
                    733:     var blusel = "";
                    734:     if (kwclr=="red")   {var redsel="checked"};
                    735:     if (kwclr=="green") {var grnsel="checked"};
                    736:     if (kwclr=="blue")  {var blusel="checked"};
                    737:     var sznsel = "";
                    738:     var sz1sel = "";
                    739:     var sz2sel = "";
                    740:     if (kwsize=="0")  {var sznsel="checked"};
                    741:     if (kwsize=="+1") {var sz1sel="checked"};
                    742:     if (kwsize=="+2") {var sz2sel="checked"};
                    743:     var synsel = "";
                    744:     var syisel = "";
                    745:     var sybsel = "";
                    746:     if (kwstyle=="")    {var synsel="checked"};
                    747:     if (kwstyle=="<i>") {var syisel="checked"};
                    748:     if (kwstyle=="<b>") {var sybsel="checked"};
                    749:     highlightCentral();
                    750:     highlightbody('red','red',redsel,'0','normal',sznsel,'','normal',synsel);
                    751:     highlightbody('green','green',grnsel,'+1','+1',sz1sel,'<i>','italic',syisel);
                    752:     highlightbody('blue','blue',blusel,'+2','+2',sz2sel,'<b>','bold',sybsel);
                    753:     highlightend();
                    754:     return;
                    755:   }
                    756: 
                    757: 
                    758:   function highlightCentral() {
                    759:     hwdWin = window.open('', 'KeywordHighlightCentral', 'toolbar=no,location=no,scrollbars=no,width=400,height=300,screenx=100,screeny=75');
                    760:     hwdWin.document.write("<html><head>");
                    761:     hwdWin.document.write("<title>Highlight Central</title>");
                    762: 
                    763:     hwdWin.document.write("<script language=javascript>");
                    764:     hwdWin.document.write("function updateChoice(flag) {");
                    765:     hwdWin.document.write("  opener.document.SCORE.kwclr.value = radioSelection(document.hlCenter.kwdclr);");
                    766:     hwdWin.document.write("  opener.document.SCORE.kwsize.value = radioSelection(document.hlCenter.kwdsize);");
                    767:     hwdWin.document.write("  opener.document.SCORE.kwstyle.value = radioSelection(document.hlCenter.kwdstyle);");
                    768:     hwdWin.document.write("  opener.document.SCORE.refresh.value = \\"on\\";");
                    769:     hwdWin.document.write("  if (opener.document.SCORE.keywords.value!=\\"\\"){");
                    770:     hwdWin.document.write("     opener.document.SCORE.submit();");
                    771:     hwdWin.document.write("  }");
                    772:     hwdWin.document.write("  self.close()");
                    773:     hwdWin.document.write("}");
                    774: 
                    775:     hwdWin.document.write("function radioSelection(radioButton) {");
                    776:     hwdWin.document.write("    var selection=null;");
                    777:     hwdWin.document.write("    for (var i=0; i<radioButton.length; i++) {");
                    778:     hwdWin.document.write("        if (radioButton[i].checked) {");
                    779:     hwdWin.document.write("            selection=radioButton[i].value;");
                    780:     hwdWin.document.write("            return selection;");
                    781:     hwdWin.document.write("        }");
                    782:     hwdWin.document.write("    }");
                    783:     hwdWin.document.write("}");
                    784: 
                    785:     hwdWin.document.write("<");
                    786:     hwdWin.document.write("/script>");
                    787: 
                    788:     hwdWin.document.write("</head><body bgcolor=white>");
                    789: 
                    790:     hwdWin.document.write("<form action=\\"inactive\\" name=\\"hlCenter\\">");
                    791:     hwdWin.document.write("<font color=\\"green\\" size=+1>&nbsp;Keyword Highlight Options</font><br><br>");
                    792: 
                    793:     hwdWin.document.write("<table border=0 width=100%><tr><td bgcolor=\\"#777777\\">");
                    794:     hwdWin.document.write("<table border=0 width=100%><tr bgcolor=\\"#ddffff\\">");
                    795:     hwdWin.document.write("<td><b>Text Color</b></td><td><b>Font Size</b></td><td><b>Font Style</td></tr>");
                    796:   }
                    797: 
                    798:   function highlightbody(clrval,clrtxt,clrsel,szval,sztxt,szsel,syval,sytxt,sysel) { 
                    799:     hwdWin.document.write("<tr bgcolor=\\"#ffffdd\\">");
                    800:     hwdWin.document.write("<td align=\\"left\\">");
                    801:     hwdWin.document.write("<input name=\\"kwdclr\\" type=\\"radio\\" value=\\""+clrval+"\\" "+clrsel+">&nbsp;"+clrtxt+"</td>");
                    802:     hwdWin.document.write("<td align=\\"left\\">");
                    803:     hwdWin.document.write("<input name=\\"kwdsize\\" type=\\"radio\\" value=\\""+szval+"\\" "+szsel+">&nbsp;"+sztxt+"</td>");
                    804:     hwdWin.document.write("<td align=\\"left\\">");
                    805:     hwdWin.document.write("<input name=\\"kwdstyle\\" type=\\"radio\\" value=\\""+syval+"\\" "+sysel+">&nbsp;"+sytxt+"</td>");
                    806:     hwdWin.document.write("</tr>");
                    807:   }
                    808: 
                    809:   function highlightend() { 
                    810:     hwdWin.document.write("</table>");
                    811:     hwdWin.document.write("</td></tr></table>&nbsp;");
                    812: //    hwdWin.document.write("<input type=\\"button\\" value=\\"Save\\" onClick=\\"javascript:updateChoice(0)\\">&nbsp;&nbsp;");
                    813:     hwdWin.document.write("<input type=\\"button\\" value=\\"Save\\" onClick=\\"javascript:updateChoice(1)\\">&nbsp;&nbsp;");
                    814:     hwdWin.document.write("<input type=\\"button\\" value=\\"Cancel\\" onClick=\\"self.close()\\"><br><br>");
                    815:     hwdWin.document.write("</form>");
                    816:     hwdWin.document.write("</body></html>");
                    817:   }
                    818: 
                    819: </script>
                    820: SUBJAVASCRIPT
                    821: }
                    822: 
                    823: 
                    824: # --------------------------- show submissions of a student, option to grade 
                    825: sub submission {
                    826:     my ($request,$counter,$total) = @_;
                    827: 
                    828:     (my $url=$ENV{'form.url'})=~s-^http://($ENV{'SERVER_NAME'}|$ENV{'HTTP_HOST'})--;
                    829: #    if ($ENV{'form.student'} eq '') { &moreinfo($request,'Need student login id'); return ''; }
                    830:     my ($uname,$udom)     = ($ENV{'form.student'},$ENV{'form.userdom'});
                    831:     ($uname,$udom)        = &finduser($uname) if $udom eq '';
                    832:     $ENV{'form.fullname'} = &get_fullname ($uname,$udom) if $ENV{'form.fullname'} eq '';
                    833: #    if ($uname eq '') { &moreinfo($request,'Unable to find student'); return ''; }
1.41      ng        834: 
                    835:     my $symb=($ENV{'form.symb'} ne '' ? $ENV{'form.symb'} : (&Apache::lonnet::symbread($url)));
                    836:     if ($symb eq '') { $request->print("Unable to handle ambiguous references:$url:."); return ''; }
                    837:     my $last = ($ENV{'form.lastSub'} eq 'last' ? 'last' : '');
                    838:     $ENV{'form.vProb'} = $ENV{'form.vProb'} ne '' ? $ENV{'form.vProb'} : 'yes';
                    839:     my ($classlist,$seclist,$ids,$stusec,$fullname);
                    840: 
                    841:     # header info
                    842:     if ($counter == 0) {
                    843: 	&sub_page_js($request);
1.45      ng        844: 	$request->print('<h3>&nbsp;<font color="#339933">Submission Record</font></h3>'."\n".
                    845: 			'<font size=+1>&nbsp;<b>Resource: </b>'.$url.'</font>'."\n");
1.41      ng        846: 
1.44      ng        847: 	# option to display problem, only once else it cause problems 
                    848:         # with the form later since the problem has a form.
1.41      ng        849: 	if ($ENV{'form.vProb'} eq 'yes') {
                    850: 	    my $rendered=&Apache::loncommon::get_student_view($symb,$uname,$udom,
                    851: 							      $ENV{'request.course.id'});
                    852: 	    my $companswer=&Apache::loncommon::get_student_answers($symb,$uname,$udom,
                    853: 								   $ENV{'request.course.id'});
                    854: 	    my $result.='<table border="0" width="100%"><tr><td bgcolor="#777777">';
                    855: 	    $result.='<table border="0" width="100%"><tr><td bgcolor="#e6ffff">';
1.45      ng        856: 	    $result.='<b> View of the problem - '.$ENV{'form.fullname'}.
1.44      ng        857: 		'</b></td></tr><tr><td bgcolor="#ffffff">'.$rendered.'<br />';
1.41      ng        858: 	    $result.='<b>Correct answer:</b><br />'.$companswer;
                    859: 	    $result.='</td></tr></table>';
                    860: 	    $result.='</td></tr></table><br />';
                    861: 	    $request->print($result);
                    862: 	}
                    863: 	
1.44      ng        864: 	# kwclr is the only variable that is guaranteed to be non blank 
                    865:         # if this subroutine has been called once.
1.41      ng        866: 	my %keyhash = ();
                    867: 	if ($ENV{'form.kwclr'} eq '') {
                    868: 	    %keyhash = &Apache::lonnet::dump('nohist_handgrade',
                    869: 					     $ENV{'course.'.$ENV{'request.course.id'}.'.domain'},
                    870: 					     $ENV{'course.'.$ENV{'request.course.id'}.'.num'});
                    871: 
                    872: 	    my $loginuser = $ENV{'user.name'}.':'.$ENV{'user.domain'};
                    873: 	    $ENV{'form.keywords'} = $keyhash{$symb.'_keywords'} ne '' ? $keyhash{$symb.'_keywords'} : '';
                    874: 	    $ENV{'form.kwclr'}    = $keyhash{$loginuser.'_kwclr'} ne '' ? $keyhash{$loginuser.'_kwclr'} : 'red';
                    875: 	    $ENV{'form.kwsize'}   = $keyhash{$loginuser.'_kwsize'} ne '' ? $keyhash{$loginuser.'_kwsize'} : '0';
                    876: 	    $ENV{'form.kwstyle'}  = $keyhash{$loginuser.'_kwstyle'} ne '' ? $keyhash{$loginuser.'_kwstyle'} : '';
                    877: 	    $ENV{'form.msgsub'}   = $keyhash{$symb.'_subject'} ne '' ? 
                    878: 		$keyhash{$symb.'_subject'} : &Apache::lonnet::metadata($url,'title');
                    879: 	    $ENV{'form.savemsgN'} = $keyhash{$symb.'_savemsgN'} ne '' ? $keyhash{$symb.'_savemsgN'} : '0';
1.38      ng        880: 
1.41      ng        881: 	}
1.44      ng        882: 
1.41      ng        883: 	$request->print('<form action="/adm/grades" method="post" name="SCORE">'."\n".
                    884: 			'<input type="hidden" name="command"    value="handgrade" />'."\n".
                    885: 			'<input type="hidden" name="refresh"    value="off" />'."\n".
                    886: 			'<input type="hidden" name="symb"       value="'.$symb.'" />'."\n".
                    887: 			'<input type="hidden" name="url"        value="'.$url.'" />'."\n".
                    888: 			'<input type="hidden" name="showgrading" value="'.$ENV{'form.showgrading'}.'" />'."\n".
                    889: 			'<input type="hidden" name="vProb"      value="'.$ENV{'form.vProb'}.'" />'."\n".
                    890: 			'<input type="hidden" name="lastSub"    value="'.$ENV{'form.lastSub'}.'" />'."\n".
                    891: 			'<input type="hidden" name="section"    value="'.$ENV{'form.section'}.'">'."\n".
                    892: 			'<input type="hidden" name="submitonly" value="'.$ENV{'form.submitonly'}.'">'."\n".
                    893: 			'<input type="hidden" name="response"   value="'.$ENV{'form.response'}.'">'."\n".
                    894: 			'<input type="hidden" name="handgrade"  value="'.$ENV{'form.handgrade'}.'">'."\n".
                    895: 			'<input type="hidden" name="keywords"   value="'.$ENV{'form.keywords'}.'" />'."\n".
                    896: 			'<input type="hidden" name="kwclr"      value="'.$ENV{'form.kwclr'}.'" />'."\n".
                    897: 			'<input type="hidden" name="kwsize"     value="'.$ENV{'form.kwsize'}.'" />'."\n".
                    898: 			'<input type="hidden" name="kwstyle"    value="'.$ENV{'form.kwstyle'}.'" />'."\n".
                    899: 			'<input type="hidden" name="msgsub"     value="'.$ENV{'form.msgsub'}.'" />'."\n".
                    900: 			'<input type="hidden" name="savemsgN"   value="'.$ENV{'form.savemsgN'}.'" />'."\n".
                    901: 			'<input type="hidden" name="NCT"'.
                    902: 			' value="'.($ENV{'form.NTSTU'} ne '' ? $ENV{'form.NTSTU'} : $total+1).'" />'."\n");
                    903: 	
                    904: 	my ($cts,$prnmsg) = (1,'');
                    905: 	while ($cts <= $ENV{'form.savemsgN'}) {
                    906: 	    $prnmsg.='<input type="hidden" name="savemsg'.$cts.'" value="'.
                    907: 		($keyhash{$symb.'_savemsg'.$cts} eq '' ? $ENV{'form.savemsg'.$cts} : $keyhash{$symb.'_savemsg'.$cts}).
                    908: 		'" />'."\n";
                    909: 	    $cts++;
                    910: 	}
                    911: 	$request->print($prnmsg);
1.32      ng        912: 
1.41      ng        913: 	if ($ENV{'form.handgrade'} eq 'yes' && $ENV{'form.showgrading'} eq 'yes') {
                    914: 	    $request->print(<<KEYWORDS);
1.38      ng        915: &nbsp;<b>Keyword Options:</b>&nbsp;
                    916: <a href="javascript:keywords(document.SCORE.keywords)"; TARGET=_self>List</a>&nbsp; &nbsp;
                    917: <a href="#" onMouseDown="javascript:getSel(); return false"
                    918:  CLASS="page">Paste Selection to List</a>&nbsp; &nbsp;
                    919: <a href="javascript:kwhighlight()"; TARGET=_self>Highlight Attribute</a><br /><br />
                    920: KEYWORDS
1.41      ng        921:         }
                    922:     }
1.44      ng        923: 
1.41      ng        924:     my %record = &Apache::lonnet::restore($symb,$ENV{'request.course.id'},$udom,$uname);
                    925:     my ($partlist,$handgrade) = &response_type($url);
                    926: 
1.44      ng        927:     # Display student info
1.41      ng        928:     $request->print(($counter == 0 ? '' : '<br />'));
1.45      ng        929:     my $result='<table border="0" width=100%><tr><td bgcolor="#777777">'."\n".
                    930: 	'<table border="0" width=100%><tr bgcolor="#edffff"><td>'."\n";
1.44      ng        931: 
                    932:     $result.='<b>Fullname: </b>'.$ENV{'form.fullname'}.
                    933: 	'<font color="#999999">&nbsp; &nbsp;Username: '.$uname.'</font>'.
1.45      ng        934: 	'<font color="#999999">&nbsp; &nbsp;Domain: '.$udom.'</font><br />'."\n";
                    935:     $result.='<input type="hidden" name="name'.$counter.
                    936: 	'" value="'.$ENV{'form.fullname'}.'" />'."\n";
1.41      ng        937: 
1.44      ng        938:     # If this is handgraded, then check for collaborators
1.45      ng        939:     my @col_fullnames;
1.41      ng        940:     if ($ENV{'form.handgrade'} eq 'yes') {
                    941: 	my @col_list;
                    942: 	($classlist,$seclist,$ids,$stusec,$fullname) = &getclasslist('all','0');
                    943: 	for (keys (%$handgrade)) {
1.44      ng        944: 	    my $ncol = &Apache::lonnet::EXT('resource.'.$_.
                    945: 					    '.maxcollaborators',$symb,$udom,$uname);
1.41      ng        946: 	    if ($ncol > 0) {
                    947: 		s/\_/\./g;
                    948: 		if ($record{'resource.'.$_.'.collaborators'} ne '') {
1.44      ng        949: 		    my (@collaborators) = split(/,?\s+/,
                    950: 						$record{'resource.'.$_.'.collaborators'});
1.41      ng        951: 		    my (@badcollaborators);
                    952: 		    if (scalar(@collaborators) != 0) {
1.44      ng        953: 			$result.='<b>Collaborators: </b>';
1.41      ng        954: 			foreach my $collaborator (@collaborators) {
                    955: 			    $collaborator = $collaborator =~ /\@|:/ ? 
                    956: 				(split(/@|:/,$collaborator))[0] : $collaborator;
                    957: 			    next if ($collaborator eq $uname);
                    958: 			    if (!grep /^$collaborator:/i,keys %$classlist) {
                    959: 				push @badcollaborators,$collaborator;
                    960: 				next;
                    961: 			    }
                    962: 			    push @col_list, $collaborator;
1.45      ng        963: 			    my ($lastname,$givenn) = split(/,/,$$fullname{$collaborator.':'.$udom});
                    964: 			    push @col_fullnames, $givenn.' '.$lastname;
1.44      ng        965: 			    $result.=$$fullname{$collaborator.':'.$udom}.'&nbsp; &nbsp; &nbsp;';
1.41      ng        966: 			}
1.44      ng        967: 			$result.='<br />'."\n";
                    968: 			$result.='<table border="0"><tr bgcolor="#ffbbbb"><td>'.
                    969: 			    'This student has submitted '.
                    970: 			    (scalar (@badcollaborators) > 1 ? '' : 'an').
1.41      ng        971: 			    ' invalid collaborator'.(scalar (@badcollaborators) > 1 ? 's. ' : '. ').
1.44      ng        972: 			    (join ', ',@badcollaborators).'</td></tr></table>' 
1.41      ng        973: 			    if (scalar(@badcollaborators) > 0);
                    974: 
1.44      ng        975: 			$result.='<table border="0"><tr bgcolor="#ffbbbb"><td>'.
1.41      ng        976: 			    'This student has submitted too many collaborators. Maximum is '.
1.44      ng        977: 			    $ncol.'.</td></tr></table>' if (scalar(@collaborators) > $ncol);
1.41      ng        978: 			$result.='<input type="hidden" name="collaborator'.$counter.
                    979: 			    '" value="'.(join ':',@col_list).'" />'."\n";
                    980: 		    }
                    981: 		}
                    982: 	    }
                    983: 	}
                    984:     }
1.44      ng        985:     $request->print($result."\n");
1.33      ng        986: 
1.44      ng        987:     # print student answer/submission
                    988:     # Options are (1) Handgaded submission only
                    989:     #             (2) Last submission, includes submission that is not handgraded 
                    990:     #                  (for multi-response type part)
                    991:     #             (3) Last submission plus the parts info
                    992:     #             (4) The whole record for this student
1.41      ng        993:     if ($ENV{'form.lastSub'} =~ /^(lastonly|hdgrade)$/) {
                    994: 	if ($ENV{'form.'.$uname.':'.$udom.':submitted_by'}) {
1.44      ng        995: 	    my $submitby=''.
1.41      ng        996: 		'<b>Collaborative submission by: </b>'.
1.44      ng        997: 		'<a href="javascript:viewSubmitter(\''.
                    998: 		$ENV{'form.'.$uname.':'.$udom.':submitted_by'}.
1.41      ng        999: 		'\')"; TARGET=_self>'.
                   1000: 		$$fullname{$ENV{'form.'.$uname.':'.$udom.':submitted_by'}}.'</a>';
                   1001: 	    $request->print($submitby);
                   1002: 	} else {
1.44      ng       1003: 	    my ($string,$timestamp)=
1.46      ng       1004: 		&get_last_submission (%record);
1.44      ng       1005: 	    my $lastsubonly.=''.
                   1006: 		($$timestamp eq '' ? '' : '<b>Date Submitted:</b> '.
                   1007: 		 $$timestamp).'';
1.41      ng       1008: 	    if ($$timestamp eq '') {
1.45      ng       1009: 		$lastsubonly.='<tr><td bgcolor="#ffffe6">'.$$string[0].'</td></tr>'."\n";
1.41      ng       1010: 	    } else {
                   1011: 		for my $part (sort keys(%$handgrade)) {
                   1012: 		    foreach (@$string) {
                   1013: 			my ($partid,$respid) = /^resource\.(\d+)\.(\d+)\.submission/;
                   1014: 			if ($part eq ($partid.'_'.$respid)) {
                   1015: 			    my ($ressub,$subval) = split(/:/,$_,2);
1.44      ng       1016: 			    $lastsubonly.='<tr><td bgcolor="#ffffe6"><b>Part '.
                   1017: 				$partid.'</b> <font color="#999999">( ID '.$respid.
                   1018: 				' )</font>&nbsp; &nbsp;<b>Answer: </b>'.
1.45      ng       1019: 				&keywords_highlight($subval).'</td></tr>'."\n"
1.41      ng       1020: 				if ($ENV{'form.lastSub'} eq 'lastonly' || 
1.44      ng       1021: 				    ($ENV{'form.lastSub'} eq 'hdgrade' && 
                   1022: 				     $$handgrade{$part} =~ /:yes$/));
1.41      ng       1023: 			}
                   1024: 		    }
                   1025: 		}
                   1026: 	    }
1.45      ng       1027: 	    $lastsubonly.='</td></tr>'."\n";
1.41      ng       1028: 	    $request->print($lastsubonly);
                   1029: 	}
                   1030:     } else {
                   1031: 	$request->print(&Apache::loncommon::get_previous_attempt($symb,$uname,$udom,
1.44      ng       1032: 								 $ENV{'request.course.id'},
                   1033: 								 $last,'.submission',
                   1034: 								 'Apache::grades::keywords_highlight'));
1.41      ng       1035:     }
                   1036:     
1.44      ng       1037:     # return if view submission with no grading option
1.41      ng       1038:     if ($ENV{'form.showgrading'} eq '') {
1.45      ng       1039: 	$request->print('</td></tr></table></td></tr></table></form>'."\n");
1.41      ng       1040: 	return;
                   1041:     }
1.33      ng       1042: 
1.44      ng       1043:     # Grading options
1.41      ng       1044:     $result='<input type="hidden" name="newmsg'.$counter.'" value="" />'."\n".
                   1045: 	'<input type="hidden" name="includemsg'.$counter.'" value="" />'."\n".
1.45      ng       1046: 	'<input type="hidden" name="unamedom'.$counter.'" value="'.$uname.':'
                   1047: 	.$udom.'" />'."\n";
                   1048:     my ($lastname,$givenn) = split(/,/,$ENV{'form.fullname'});
                   1049:     my $msgfor = $givenn.' '.$lastname;
                   1050:     if (scalar(@col_fullnames) > 0) {
                   1051: 	my $lastone = pop @col_fullnames;
                   1052: 	$msgfor .= ', '.(join ', ',@col_fullnames).' and '.$lastone.'.';
                   1053:     }
                   1054:     $result.='<tr><td bgcolor="#ffffff">'."\n".
                   1055: 	'&nbsp;<a href="javascript:msgCenter(document.SCORE,'.$counter.
                   1056: 	',\''.$msgfor.'\')"; TARGET=_self>'.
                   1057: 	'Compose Message to student'.(scalar(@col_fullnames) >= 1 ? 's' : '').'</a>'.
1.44      ng       1058: 	'<br />&nbsp;(Message will be sent when you click on Save & Next below.)'."\n" 
                   1059: 	if ($ENV{'form.handgrade'} eq 'yes');
1.41      ng       1060:     $request->print($result);
                   1061: 
                   1062:     my %seen = ();
                   1063:     my @partlist;
                   1064:     for (sort keys(%$handgrade)) {
                   1065: 	my ($partid,$respid) = split(/_/);
                   1066: 	next if ($seen{$partid} > 0);
                   1067: 	$seen{$partid}++;
                   1068: 	next if ($$handgrade{$_} =~ /:no$/);
                   1069: 	push @partlist,$partid;
                   1070: 	my $wgt    = &Apache::lonnet::EXT('resource.'.$partid.'.weight',$symb,$udom,$uname);
1.44      ng       1071: 	my $wgtmsg = ($wgt > 0 ? '(problem weight)' : 
                   1072: 		      '<font color="red">problem weight assigned by computer</font>');
1.41      ng       1073: 	$wgt       = ($wgt > 0 ? $wgt : '1');
                   1074: 	my $score  = ($record{'resource.'.$partid.'.awarded'} eq '' ?
                   1075: 		      '' : $record{'resource.'.$partid.'.awarded'}*$wgt);
                   1076: 	$result='<input type="hidden" name="WGT'.$counter.'_'.$partid.'" value="'.$wgt.'" />';
1.44      ng       1077: 	$result.='<table border="0"><tr><td><b>Part </b>'.$partid.' <b>Points: </b></td><td>';
1.41      ng       1078: 
                   1079: 	my $ctr = 0;
                   1080: 	$result.='<table border="0"><tr>';  # display radio buttons in a nice table 10 across
                   1081: 	while ($ctr<=$wgt) {
                   1082: 	    $result.= '<td><input type="radio" name="RADVAL'.$counter.'_'.$partid.'" '.
1.43      ng       1083: 		'onclick="javascript:writeBox(this.form.GD_BOX'.$counter.'_'.$partid.
                   1084: 		',this.form.GD_SEL'.$counter.'_'.$partid.','.$ctr.
1.41      ng       1085: 		',this.form.stores'.$counter.'_'.$partid.')" '.
                   1086: 		($score eq $ctr ? 'checked':'').' /> '.$ctr."</td>\n";
                   1087: 	    $result.=(($ctr+1)%10 == 0 ? '</tr><tr>' : '');
                   1088: 	    $ctr++;
                   1089: 	}
                   1090: 	$result.='</tr></table>';
1.39      ng       1091: 
1.41      ng       1092: 	$result.='</td><td>&nbsp;<b>or</b>&nbsp;</td>';
1.43      ng       1093: 	$result.='<td><input type="text" name="GD_BOX'.$counter.'_'.$partid.'"'.
1.41      ng       1094: 	    ($score ne ''? ' value = "'.$score.'"':'').' size="4" '.
                   1095: 	    'onChange="javascript:updateRadio(this.form.RADVAL'.$counter.'_'.$partid.
1.43      ng       1096: 	    ',this.form.GD_BOX'.$counter.'_'.$partid.
                   1097: 	    ',this.form.GD_SEL'.$counter.'_'.$partid.
1.44      ng       1098: 	    ',this.form.stores'.$counter.'_'.$partid.
                   1099: 	    ','.$wgt.')" /></td>'."\n";
1.41      ng       1100: 	$result.='<td>/'.$wgt.' '.$wgtmsg.' </td><td>';
                   1101: 
1.43      ng       1102: 	$result.='<select name="GD_SEL'.$counter.'_'.$partid.'" '.
1.41      ng       1103: 	    'onChange="javascript:clearRadBox(this.form.RADVAL'.$counter.'_'.$partid.
1.43      ng       1104: 	    ',this.form.GD_BOX'.$counter.'_'.$partid.
                   1105: 	    ',this.form.GD_SEL'.$counter.'_'.$partid.
1.41      ng       1106: 	    ',this.form.stores'.$counter.'_'.$partid.')" />'."\n".
                   1107: 	    '<option selected="on"> </option>'.
                   1108: 	    '<option>excused</option></select>'."&nbsp&nbsp\n";
                   1109: 	$result.='<input type="hidden" name="stores'.$counter.'_'.$partid.'" value="0" />';
1.45      ng       1110: 	$result.='</td></tr></table>'."\n";
1.41      ng       1111: 	$request->print($result);
                   1112:     }
1.45      ng       1113:     $result='<input type="hidden" name="partlist'.$counter.
                   1114: 	'" value="'.(join ":",@partlist).'" />'."\n";
                   1115:     my $ctr = 0;
                   1116:     while ($ctr < scalar(@partlist)) {
                   1117: 	$result.='<input type="hidden" name="partid'.$counter.'_'.$ctr.'" value="'.
                   1118: 	    $partlist[$ctr].'" />'."\n";
                   1119: 	$ctr++;
                   1120:     }
                   1121:     $request->print($result.'</td></tr></table></td></tr></table>'."\n");
1.41      ng       1122: 
                   1123:     # print end of form
                   1124:     if ($counter == $total) {
1.45      ng       1125: 	my $endform='<table border="0"><tr><td>'.
                   1126: 	    '<input type="hidden" name="gradeOpt" value="" />'."\n";
                   1127: 	if ($ENV{'form.handgrade'} eq 'yes') {
                   1128: 	    $endform.='<input type="button" value="Save & Next" '.
                   1129: 		'onClick="javascript:checksubmit(\'Save & Next\','.
                   1130: 		$total.','.scalar(@partlist).');" TARGET=_self> &nbsp;'."\n";
                   1131: 	    my $ntstu ='<select name="NTSTU">'.
                   1132: 		'<option>1</option><option>2</option>'.
                   1133: 		'<option>3</option><option>5</option>'.
                   1134: 		'<option>7</option><option>10</option></select>'."\n";
                   1135: 	    my $nsel = ($ENV{'form.NTSTU'} ne '' ? $ENV{'form.NTSTU'} : '1');
                   1136: 	    $ntstu =~ s/<option>$nsel</<option selected="on">$nsel</;
                   1137: 	    $endform.=$ntstu.'student(s) &nbsp;&nbsp;';
                   1138: 	} else {
                   1139: 	    $endform.='<input type="hidden" name="NTSTU" value="1" />'."\n";
                   1140: 	}
                   1141: 	$endform.='<input type="button" value="Next" '.
                   1142: 	    'onClick="javascript:checksubmit(\'Next\');" TARGET=_self> &nbsp;'."\n".
                   1143: 	    '<input type="button" value="Previous" '.
                   1144: 	    'onClick="javascript:checksubmit(\'Previous\');" TARGET=_self> &nbsp;';
                   1145: 	$endform.='(Next and Previous do not save the scores.)'."\n" 
                   1146: 	    if ($ENV{'form.handgrade'} eq 'yes');
                   1147: 	$endform.='</td><tr></table></form>';
1.50    ! albertel 1148: 	$endform.=&show_grading_menu_form($symb,$url);
1.41      ng       1149: 	$request->print($endform);
                   1150:     }
                   1151:     return '';
1.38      ng       1152: }
                   1153: 
1.44      ng       1154: #--- Retrieve the last submission for all the parts
1.38      ng       1155: sub get_last_submission {
1.46      ng       1156:     my (%returnhash)=@_;
                   1157:     my (@string,$timestamp);
                   1158:     if ($returnhash{'version'}) {
                   1159: 	my %lasthash=();
                   1160: 	my ($version);
                   1161: 	for ($version=1;$version<=$returnhash{'version'};$version++) {
                   1162: 	    foreach (sort(split(/\:/,$returnhash{$version.':keys'}))) {
                   1163: 		$lasthash{$_}=$returnhash{$version.':'.$_};
                   1164: 		if ($returnhash{$version.':'.$_} =~ /(SUBMITTED|DRAFT)$/) {
                   1165: 		   $timestamp = scalar(localtime($returnhash{$version.':timestamp'}));
                   1166: 	       } 
                   1167: 	    }
                   1168: 	}
                   1169: 	foreach ((keys %lasthash)) {
                   1170: 	    if ($_ =~ /\.submission$/) {
                   1171: 		my ($partid,$foo) = split(/submission$/,$_);
                   1172: 		my $draft  = $lasthash{$partid.'awarddetail'} eq 'DRAFT' ?
                   1173: 		    '<font color="red">Draft Copy</font> ' : '';
                   1174: 		push @string, (join(':',$_,$draft.$lasthash{$_}));
1.41      ng       1175: 	    }
                   1176: 	}
                   1177:     }
1.46      ng       1178:     @string = $string[0] eq '' ? 'Nothing submitted - no attempts.' : @string;
                   1179:     return \@string,\$timestamp;
1.38      ng       1180: }
1.35      ng       1181: 
1.44      ng       1182: #--- High light keywords, with style choosen by user.
1.38      ng       1183: sub keywords_highlight {
1.44      ng       1184:     my $string    = shift;
                   1185:     my $size      = $ENV{'form.kwsize'} eq '0' ? '' : 'size='.$ENV{'form.kwsize'};
                   1186:     my $styleon   = $ENV{'form.kwstyle'} eq ''  ? '' : $ENV{'form.kwstyle'};
1.41      ng       1187:     (my $styleoff = $styleon) =~ s/\</\<\//;
1.44      ng       1188:     my @keylist   = split(/[,\s+]/,$ENV{'form.keywords'});
1.41      ng       1189:     foreach (@keylist) {
                   1190: 	$string =~ s/\b$_(\b|\.)/\<font color\=$ENV{'form.kwclr'} $size\>$styleon$_$styleoff\<\/font\>/gi;
                   1191:     }
                   1192:     return $string;
1.38      ng       1193: }
1.36      ng       1194: 
1.44      ng       1195: #--- Called from submission routine
1.38      ng       1196: sub processHandGrade {
1.41      ng       1197:     my ($request) = shift;
                   1198:     my $url    = $ENV{'form.url'};
                   1199:     my $symb   = $ENV{'form.symb'};
                   1200:     my $button = $ENV{'form.gradeOpt'};
                   1201:     my $ngrade = $ENV{'form.NCT'};
                   1202:     my $ntstu  = $ENV{'form.NTSTU'};
                   1203: 
1.44      ng       1204:     if ($button eq 'Save & Next') {
                   1205: 	my $ctr = 0;
                   1206: 	while ($ctr < $ngrade) {
                   1207: 	    my ($uname,$udom) = split(/:/,$ENV{'form.unamedom'.$ctr});
1.45      ng       1208: 	    my ($errorflag) = &saveHandGrade($request,$url,$symb,$uname,$udom,$ctr);
1.44      ng       1209: 
                   1210: 	    my $includemsg = $ENV{'form.includemsg'.$ctr};
                   1211: 	    my ($subject,$message,$msgstatus) = ('','','');
                   1212: 	    if ($includemsg =~ /savemsg|new$ctr/) {
                   1213: 		$subject = $ENV{'form.msgsub'} if ($includemsg =~ /^msgsub/);
                   1214: 		my (@msgnum) = split(/,/,$includemsg);
                   1215: 		foreach (@msgnum) {
                   1216: 		    $message.=$ENV{'form.'.$_} if ($_ =~ /savemsg|newmsg/ && $_ ne '');
                   1217: 		}
                   1218: 		$message =~ s/\s+/ /g;
                   1219: 		$msgstatus = &Apache::lonmsg::user_normal_msg ($uname,$udom,
                   1220: 							       $ENV{'form.msgsub'},$message);
                   1221: 	    }
                   1222: 	    if ($ENV{'form.collaborator'.$ctr}) {
                   1223: 		my (@collaborators) = split(/:/,$ENV{'form.collaborator'.$ctr});
                   1224: 		foreach (@collaborators) {
                   1225: 		    &saveHandGrade($request,$url,$symb,$_,$udom,$ctr,
                   1226: 				   $ENV{'form.unamedom'.$ctr});
                   1227: 		    if ($message ne '') {
                   1228: 			$msgstatus = &Apache::lonmsg::user_normal_msg ($_,$udom,
                   1229: 								       $ENV{'form.msgsub'},
                   1230: 								       $message);
                   1231: 		    }
                   1232: 		}
                   1233: 	    }
                   1234: 	    $ctr++;
                   1235: 	}
                   1236:     }
                   1237: 
                   1238:     # Keywords sorted in alphabatical order
1.41      ng       1239:     my $loginuser = $ENV{'user.name'}.':'.$ENV{'user.domain'};
                   1240:     my %keyhash = ();
                   1241:     $ENV{'form.keywords'}           =~ s/,\s{0,}|\s+/ /g;
                   1242:     $ENV{'form.keywords'}           =~ s/^\s+|\s+$//;
1.44      ng       1243:     my (@keywords) = sort(split(/\s+/,$ENV{'form.keywords'}));
                   1244:     $ENV{'form.keywords'} = join(' ',@keywords);
1.41      ng       1245:     $keyhash{$symb.'_keywords'}     = $ENV{'form.keywords'};
                   1246:     $keyhash{$symb.'_subject'}      = $ENV{'form.msgsub'};
                   1247:     $keyhash{$loginuser.'_kwclr'}   = $ENV{'form.kwclr'};
                   1248:     $keyhash{$loginuser.'_kwsize'}  = $ENV{'form.kwsize'};
                   1249:     $keyhash{$loginuser.'_kwstyle'} = $ENV{'form.kwstyle'};
                   1250: 
1.44      ng       1251:     # message center - Order of message gets changed. Blank line is eliminated.
                   1252:     # New messages are saved in ENV for the next student.
                   1253:     # All messages are saved in nohist_handgrade.db
1.41      ng       1254:     my ($ctr,$idx) = (1,1);
                   1255:     while ($ctr <= $ENV{'form.savemsgN'}) {
                   1256: 	if ($ENV{'form.savemsg'.$ctr} ne '') {
                   1257: 	    $keyhash{$symb.'_savemsg'.$idx} = $ENV{'form.savemsg'.$ctr};
                   1258: 	    $idx++;
                   1259: 	}
                   1260: 	$ctr++;
                   1261:     }
                   1262:     $ctr = 0;
                   1263:     while ($ctr < $ngrade) {
                   1264: 	if ($ENV{'form.newmsg'.$ctr} ne '') {
                   1265: 	    $keyhash{$symb.'_savemsg'.$idx} = $ENV{'form.newmsg'.$ctr};
                   1266: 	    $ENV{'form.savemsg'.$idx} = $ENV{'form.newmsg'.$ctr};
                   1267: 	    $idx++;
                   1268: 	}
                   1269: 	$ctr++;
                   1270:     }
                   1271:     $ENV{'form.savemsgN'} = --$idx;
                   1272:     $keyhash{$symb.'_savemsgN'} = $ENV{'form.savemsgN'};
                   1273:     my $putresult = &Apache::lonnet::put
                   1274: 	('nohist_handgrade',\%keyhash,
                   1275: 	 $ENV{'course.'.$ENV{'request.course.id'}.'.domain'},
                   1276: 	 $ENV{'course.'.$ENV{'request.course.id'}.'.num'});
                   1277: 
1.44      ng       1278:     # Called by Save & Refresh from Highlight Attribute Window
1.41      ng       1279:     if ($ENV{'form.refresh'} eq 'on') {
                   1280: 	my $ctr = 0;
                   1281: 	$ENV{'form.NTSTU'}=$ngrade;
                   1282: 	while ($ctr < $ngrade) {
1.44      ng       1283: 	    ($ENV{'form.student'},$ENV{'form.userdom'}) = split(/:/,$ENV{'form.unamedom'.$ctr});
1.41      ng       1284: 	    &submission($request,$ctr,$ngrade-1);
                   1285: 	    $ctr++;
                   1286: 	}
                   1287: 	return '';
                   1288:     }
1.36      ng       1289: 
1.44      ng       1290:     # Get the next/previous one or group of students
1.41      ng       1291:     my $firststu = $ENV{'form.unamedom0'};
                   1292:     my $laststu = $ENV{'form.unamedom'.($ngrade-1)};
                   1293:     $ctr = 2;
                   1294:     while ($laststu eq '') {
                   1295: 	$laststu  = $ENV{'form.unamedom'.($ngrade-$ctr)};
                   1296: 	$ctr++;
                   1297: 	$laststu = $firststu if ($ctr > $ngrade);
                   1298:     }
1.44      ng       1299: 
1.41      ng       1300:     my ($classlist,$seclist,$ids,$stusec,$fullname) = &getclasslist($ENV{'form.section'},'0');
                   1301:     my (@parsedlist,@nextlist);
                   1302:     my ($nextflg) = 0;
1.44      ng       1303:     foreach (sort {$$fullname{$a} cmp $$fullname{$b} } keys %$fullname) {
1.41      ng       1304: 	if ($nextflg == 1 && $button =~ /Next$/) {
                   1305: 	    push @parsedlist,$_;
                   1306: 	}
                   1307: 	$nextflg = 1 if ($_ eq $laststu);
                   1308: 	if ($button eq 'Previous') {
                   1309: 	    last if ($_ eq $firststu);
                   1310: 	    push @parsedlist,$_;
                   1311: 	}
                   1312:     }
                   1313:     $ctr = 0;
                   1314:     my ($partlist,$handgrade) = &response_type($ENV{'form.url'});
                   1315:     @parsedlist = reverse @parsedlist if ($button eq 'Previous');
                   1316:     foreach my $student (@parsedlist) {
                   1317: 	my ($uname,$udom) = split(/:/,$student);
                   1318: 	if ($ENV{'form.submitonly'} eq 'yes') {
1.44      ng       1319: 	    my (%status) = &student_gradeStatus($ENV{'form.url'},$symb,$udom,$uname,$partlist) ;
1.41      ng       1320: 	    my $statusflg = '';
                   1321: 	    foreach (keys(%status)) {
                   1322: 		$statusflg = 1 if ($status{$_} ne 'nothing');
1.44      ng       1323: 		my ($foo,$partid,$foo1) = split(/\./);
1.41      ng       1324: 		$statusflg = '' if ($status{'resource.'.$partid.'.submitted_by'} ne '');
                   1325: 	    }
                   1326: 	    next if ($statusflg eq '');
                   1327: 	}
                   1328: 	push @nextlist,$student if ($ctr < $ntstu);
                   1329: 	$ctr++;
                   1330:     }
1.36      ng       1331: 
1.41      ng       1332:     $ctr = 0;
                   1333:     my $total = scalar(@nextlist)-1;
1.39      ng       1334: 
1.41      ng       1335:     foreach (sort @nextlist) {
                   1336: 	my ($uname,$udom,$submitter) = split(/:/);
1.44      ng       1337: 	$ENV{'form.student'}  = $uname;
                   1338: 	$ENV{'form.userdom'}  = $udom;
1.41      ng       1339: 	$ENV{'form.fullname'} = $$fullname{$_};
                   1340: #	$ENV{'form.'.$_.':submitted_by'} = $submitter;
                   1341: #	print "submitter=$ENV{'form.'.$_.':submitted_by'}= $submitter:<br>";
                   1342: 	&submission($request,$ctr,$total);
                   1343: 	$ctr++;
                   1344:     }
                   1345:     if ($total < 0) {
                   1346: 	my $the_end = '<h3><font color="red">LON-CAPA User Message</font></h3><br />'."\n";
                   1347: 	$the_end.='<b>Message: </b> No more students for this section or class.<br /><br />'."\n";
                   1348: 	$the_end.='Click on the button below to return to the grading menu.<br /><br />'."\n";
                   1349: 	$the_end.=&show_grading_menu_form ($symb,$url);
                   1350: 	$request->print($the_end);
                   1351:     }
                   1352:     return '';
1.38      ng       1353: }
1.36      ng       1354: 
1.44      ng       1355: #---- Save the score and award for each student, if changed
1.38      ng       1356: sub saveHandGrade {
1.41      ng       1357:     my ($request,$url,$symb,$stuname,$domain,$newflg,$submitter) = @_;
1.44      ng       1358:     my %record=&Apache::lonnet::restore($symb,$ENV{'request.course.id'},$domain,$stuname);
1.41      ng       1359:     my %newrecord;
                   1360:     foreach (split(/:/,$ENV{'form.partlist'.$newflg})) {
1.43      ng       1361: 	if ($ENV{'form.GD_SEL'.$newflg.'_'.$_} eq 'excused') {
1.44      ng       1362: 	    $newrecord{'resource.'.$_.'.solved'} = 'excused' 
                   1363: 		if ($record{'resource.'.$_.'.solved'} ne 'excused');
1.41      ng       1364: 	} else {
1.44      ng       1365: 	    my $pts = ($ENV{'form.GD_BOX'.$newflg.'_'.$_} ne '' ? 
                   1366: 		       $ENV{'form.GD_BOX'.$newflg.'_'.$_} : 
                   1367: 		       $ENV{'form.RADVAL'.$newflg.'_'.$_});
                   1368: 	    my $wgt = $ENV{'form.WGT'.$newflg.'_'.$_} eq '' ? 1 : 
                   1369: 		$ENV{'form.WGT'.$newflg.'_'.$_};
1.41      ng       1370: 	    my $partial= $pts/$wgt;
1.44      ng       1371: 	    $newrecord{'resource.'.$_.'.awarded'}  = $partial 
                   1372: 		if ($record{'resource.'.$_.'.awarded'} ne $partial);
                   1373: 	    my $reckey = 'resource.'.$_.'.solved';
1.41      ng       1374: 	    if ($partial == 0) {
1.44      ng       1375: 		$newrecord{$reckey} = 'incorrect_by_override' 
                   1376: 		    if ($record{$reckey} ne 'incorrect_by_override');
1.41      ng       1377: 	    } else {
1.44      ng       1378: 		$newrecord{$reckey} = 'correct_by_override' 
                   1379: 		    if ($record{$reckey} ne 'correct_by_override');
1.41      ng       1380: 	    }
1.44      ng       1381: 	    $newrecord{'resource.'.$_.'.submitted_by'} = $submitter 
                   1382: 		if ($submitter && ($record{'resource.'.$_.'.submitted_by'} ne $submitter));
1.41      ng       1383: 	}
                   1384:     }
1.44      ng       1385: 
                   1386:     if (scalar(keys(%newrecord)) > 0) {
1.41      ng       1387: 	$newrecord{'resource.regrader'}="$ENV{'user.name'}:$ENV{'user.domain'}";
1.44      ng       1388: 	&Apache::lonnet::cstore(\%newrecord,$symb,
                   1389: 				$ENV{'request.course.id'},$domain,$stuname);
1.41      ng       1390:     }
                   1391:     return '';
1.36      ng       1392: }
1.38      ng       1393: 
1.44      ng       1394: #--------------------------------------------------------------------------------------
                   1395: #
                   1396: #-------------------------- Next few routines handles grading by section or whole class
                   1397: #
                   1398: #--- Javascript to handle grading by section or whole class
1.42      ng       1399: sub viewgrades_js {
                   1400:     my ($request) = shift;
                   1401: 
1.41      ng       1402:     $request->print(<<VIEWJAVASCRIPT);
                   1403: <script type="text/javascript" language="javascript">
1.45      ng       1404:    function writePoint(partid,weight,point) {
1.42      ng       1405: 	var radioButton = eval("document.classgrade.RADVAL_"+partid);
                   1406: 	var textbox = eval("document.classgrade.TEXTVAL_"+partid);
                   1407: 	if (point == "textval") {
                   1408: 	    var point = eval("document.classgrade.TEXTVAL_"+partid+".value");
                   1409: 	    if (isNaN(point) || point < 0) {
                   1410: 		alert("A number equal or greater than 0 is expected. Entered value = "+point);
                   1411: 		var resetbox = false;
                   1412: 		for (var i=0; i<radioButton.length; i++) {
                   1413: 		    if (radioButton[i].checked) {
                   1414: 			textbox.value = i;
                   1415: 			resetbox = true;
                   1416: 		    }
                   1417: 		}
                   1418: 		if (!resetbox) {
                   1419: 		    textbox.value = "";
                   1420: 		}
                   1421: 		return;
                   1422: 	    }
1.44      ng       1423: 	    if (point > weight) {
                   1424: 		var resp = confirm("You entered a value ("+point+
                   1425: 				   ") greater than the weight for the part. Accept?");
                   1426: 		if (resp == false) {
                   1427: 		    textbox.value = "";
                   1428: 		    return;
                   1429: 		}
                   1430: 	    }
1.42      ng       1431: 	    for (var i=0; i<radioButton.length; i++) {
                   1432: 		radioButton[i].checked=false;
                   1433: 		if (point == i) {
                   1434: 		    radioButton[i].checked=true;
                   1435: 		}
                   1436: 	    }
1.41      ng       1437: 
1.42      ng       1438: 	} else {
                   1439: 	    textbox.value = point;
                   1440: 	}
1.41      ng       1441: 	for (i=0;i<document.classgrade.total.value;i++) {
1.43      ng       1442: 	    var user = eval("document.classgrade.ctr"+i+".value");
                   1443: 	    var scorename = eval("document.classgrade.GD_"+user+
                   1444: 				 "_"+partid+"_aw");
                   1445: 	    var saveval   = eval("document.classgrade.GD_"+user+
                   1446: 				 "_"+partid+"_sv_s.value");
                   1447: 	    var selname   = eval("document.classgrade.GD_"+user+"_"+partid+"_sv");
1.42      ng       1448: 	    if (saveval != "correct") {
                   1449: 		scorename.value = point;
1.43      ng       1450: 		if (selname[0].selected != true) {
                   1451: 		    selname[0].selected = true;
                   1452: 		}
1.42      ng       1453: 	    }
                   1454: 	}
                   1455: 	var selval   = eval("document.classgrade.SELVAL_"+partid);
                   1456: 	selval[0].selected = true;
                   1457:     }
                   1458: 
                   1459:     function writeRadText(partid,weight) {
                   1460: 	var selval   = eval("document.classgrade.SELVAL_"+partid);
1.43      ng       1461: 	var radioButton = eval("document.classgrade.RADVAL_"+partid);
                   1462: 	var textbox = eval("document.classgrade.TEXTVAL_"+partid);
1.42      ng       1463: 	if (selval[1].selected) {
                   1464: 	    for (var i=0; i<radioButton.length; i++) {
                   1465: 		radioButton[i].checked=false;
                   1466: 
                   1467: 	    }
                   1468: 	    textbox.value = "";
                   1469: 
                   1470: 	    for (i=0;i<document.classgrade.total.value;i++) {
1.43      ng       1471: 		var user = eval("document.classgrade.ctr"+i+".value");
                   1472: 		var scorename = eval("document.classgrade.GD_"+user+
                   1473: 				     "_"+partid+"_aw");
                   1474: 		var saveval   = eval("document.classgrade.GD_"+user+
                   1475: 				     "_"+partid+"_sv_s.value");
                   1476: 		var selname   = eval("document.classgrade.GD_"+user+
                   1477: 				     "_"+partid+"_sv");
1.42      ng       1478: 		if (saveval != "correct") {
                   1479: 		    scorename.value = "";
                   1480: 		    selname[1].selected = true;
                   1481: 		}
                   1482: 	    }
1.43      ng       1483: 	} else {
                   1484: 	    for (i=0;i<document.classgrade.total.value;i++) {
                   1485: 		var user = eval("document.classgrade.ctr"+i+".value");
                   1486: 		var scorename = eval("document.classgrade.GD_"+user+
                   1487: 				     "_"+partid+"_aw");
                   1488: 		var saveval   = eval("document.classgrade.GD_"+user+
                   1489: 				     "_"+partid+"_sv_s.value");
                   1490: 		var selname   = eval("document.classgrade.GD_"+user+
                   1491: 				     "_"+partid+"_sv");
                   1492: 		if (saveval != "correct") {
                   1493: 		    scorename.value = eval("document.classgrade.GD_"+user+
                   1494: 				     "_"+partid+"_aw_s.value");;
                   1495: 		    selname[0].selected = true;
                   1496: 		}
                   1497: 	    }
                   1498: 	}	    
1.42      ng       1499:     }
                   1500: 
                   1501:     function changeSelect(partid,user) {
1.43      ng       1502: 	var selval = eval("document.classgrade.GD_"+user+'_'+partid+"_sv");
1.44      ng       1503: 	var textbox = eval("document.classgrade.GD_"+user+'_'+partid+"_aw");
                   1504: 	var point  = textbox.value;
                   1505: 	var weight = eval("document.classgrade.weight_"+partid+".value");
                   1506: 
                   1507: 	if (isNaN(point) || point < 0) {
                   1508: 	    alert("A number equal or greater than 0 is expected. Entered value = "+point);
                   1509: 	    textbox.value = "";
                   1510: 	    return;
                   1511: 	}
                   1512: 	if (point > weight) {
                   1513: 	    var resp = confirm("You entered a value ("+point+
                   1514: 			       ") greater than the weight of the part. Accept?");
                   1515: 	    if (resp == false) {
                   1516: 		textbox.value = "";
                   1517: 		return;
                   1518: 	    }
                   1519: 	}
1.42      ng       1520: 	selval[0].selected = true;
                   1521:     }
                   1522: 
                   1523:     function changeOneScore(partid,user) {
1.43      ng       1524: 	var selval = eval("document.classgrade.GD_"+user+'_'+partid+"_sv");
1.42      ng       1525: 	if (selval[1].selected) {
1.43      ng       1526: 	    var boxval = eval("document.classgrade.GD_"+user+'_'+partid+"_aw");
1.42      ng       1527: 	    boxval.value = "";
                   1528: 	}
                   1529:     }
                   1530: 
                   1531:     function resetEntry(numpart) {
                   1532: 	for (ctpart=0;ctpart<numpart;ctpart++) {
                   1533: 	    var partid = eval("document.classgrade.partid_"+ctpart+".value");
                   1534: 	    var radioButton = eval("document.classgrade.RADVAL_"+partid);
                   1535: 	    var textbox = eval("document.classgrade.TEXTVAL_"+partid);
                   1536: 	    var selval  = eval("document.classgrade.SELVAL_"+partid);
                   1537: 	    for (var i=0; i<radioButton.length; i++) {
                   1538: 		radioButton[i].checked=false;
                   1539: 
                   1540: 	    }
                   1541: 	    textbox.value = "";
                   1542: 	    selval[0].selected = true;
                   1543: 
                   1544: 	    for (i=0;i<document.classgrade.total.value;i++) {
1.43      ng       1545: 		var user = eval("document.classgrade.ctr"+i+".value");
                   1546: 		var resetscore = eval("document.classgrade.GD_"+user+
                   1547: 				      "_"+partid+"_aw");
                   1548: 		resetscore.value = eval("document.classgrade.GD_"+user+
                   1549: 					"_"+partid+"_aw_s.value");
1.42      ng       1550: 
1.43      ng       1551: 		var saveselval   = eval("document.classgrade.GD_"+user+
                   1552: 				     "_"+partid+"_sv_s.value");
1.42      ng       1553: 
1.43      ng       1554: 		var selname   = eval("document.classgrade.GD_"+user+"_"+partid+"_sv");
1.42      ng       1555: 		if (saveselval == "excused") {
1.43      ng       1556: 		    if (selname[1].selected == false) { selname[1].selected = true;}
1.42      ng       1557: 		} else {
1.43      ng       1558: 		    if (selname[0].selected == false) {selname[0].selected = true};
1.42      ng       1559: 		}
                   1560: 	    }
1.41      ng       1561: 	}
1.42      ng       1562:     }
                   1563: 
1.41      ng       1564: </script>
                   1565: VIEWJAVASCRIPT
1.42      ng       1566: }
                   1567: 
1.44      ng       1568: #--- show scores for a section or whole class w/ option to change/update a score
1.42      ng       1569: sub viewgrades {
                   1570:     my ($request) = shift;
                   1571:     &viewgrades_js($request);
1.41      ng       1572: 
                   1573:     my ($symb,$url) = ($ENV{'form.symb'},$ENV{'form.url'}); 
1.45      ng       1574:     my $result='<h3><font color="#339933">Manual Grading</font></h3>';
1.38      ng       1575: 
1.43      ng       1576:     $result.='<font size=+1><b>Resource: </b>'.$ENV{'form.url'}.'</font>'."\n";
1.41      ng       1577: 
                   1578:     #view individual student submission form - called using Javascript viewOneStudent
1.45      ng       1579:     $result.=&jscriptNform($url,$symb);
1.41      ng       1580: 
1.44      ng       1581:     #beginning of class grading form
1.41      ng       1582:     $result.= '<form action="/adm/grades" method="post" name="classgrade">'."\n".
                   1583: 	'<input type="hidden" name="symb"    value="'.$symb.'" />'."\n".
                   1584: 	'<input type="hidden" name="url"     value="'.$url.'" />'."\n".
1.38      ng       1585: 	'<input type="hidden" name="command" value="editgrades" />'."\n".
1.41      ng       1586: 	'<input type="hidden" name="section" value="'.$ENV{'form.section'}.'" />'."\n";
1.42      ng       1587:     $result.='To assign the same score for all the students use the radio buttons or '.
                   1588: 	'text box below. To assign scores individually fill in the score boxes for '.
1.44      ng       1589: 	'each student in the table below. <font color="red">A part that has already '.
1.42      ng       1590: 	'been graded does not get changed using the radio buttons or text box. '.
                   1591: 	'If needed, it has to be changed individually.</font>';
                   1592: 
1.44      ng       1593:     #radio buttons/text box for assigning points for a section or class.
                   1594:     #handles different parts of a problem
1.42      ng       1595:     my ($partlist,$handgrade) = &response_type($ENV{'form.url'});
                   1596:     my %weight = ();
                   1597:     my $ctsparts = 0;
1.41      ng       1598:     $result.='<table border="0">';
1.45      ng       1599:     my %seen = ();
1.42      ng       1600:     for (sort keys(%$handgrade)) {
1.45      ng       1601: 	my ($partid,$respid) = split (/_/);
                   1602: 	next if $seen{$partid};
                   1603: 	$seen{$partid}++;
1.42      ng       1604: 	my ($responsetype,$handgrade)=split(/:/,$$handgrade{$_});
                   1605: 	my $wgt = &Apache::lonnet::EXT('resource.'.$partid.'.weight',$symb);
                   1606: 	$weight{$partid} = $wgt eq '' ? '1' : $wgt;
                   1607: 
1.44      ng       1608: 	$result.='<input type="hidden" name="partid_'.
                   1609: 	    $ctsparts.'" value="'.$partid.'" />'."\n";
                   1610: 	$result.='<input type="hidden" name="weight_'.
                   1611: 	    $partid.'" value="'.$weight{$partid}.'" />'."\n";
                   1612: 	$result.='<tr><td><b>Part  '.$partid.'&nbsp; &nbsp;Point:</b> </td><td>';
1.42      ng       1613: 	$result.='<table border="0"><tr>';  
1.41      ng       1614: 	my $ctr = 0;
1.42      ng       1615: 	while ($ctr<=$weight{$partid}) { # display radio buttons in a nice table 10 across
                   1616: 	    $result.= '<td><input type="radio" name="RADVAL_'.$partid.'" '.
                   1617: 		'onclick="javascript:writePoint('.$partid.','.$weight{$partid}.
1.41      ng       1618: 		','.$ctr.')" />'.$ctr."</td>\n";
                   1619: 	    $result.=(($ctr+1)%10 == 0 ? '</tr><tr>' : '');
                   1620: 	    $ctr++;
                   1621: 	}
                   1622: 	$result.='</tr></table>';
1.44      ng       1623: 	$result.= '</td><td><b> or </b><input type="text" name="TEXTVAL_'.
                   1624: 	    $partid.'" size="4" '.
                   1625: 	    'onChange="javascript:writePoint('.$partid.','.$weight{$partid}.
                   1626: 	    ',\'textval\')" /> /'.
1.42      ng       1627: 	    $weight{$partid}.' (problem weight)</td>'."\n";
                   1628: 	$result.= '</td><td><select name="SELVAL_'.$partid.'"'.
                   1629: 	    'onChange="javascript:writeRadText('.$partid.','.$weight{$partid}.')" /> '.
                   1630: 	    '<option selected="on"> </option>'.
                   1631: 	    '<option>excused</option></select></td></tr>'."\n";
                   1632: 	$ctsparts++;
1.41      ng       1633:     }
1.42      ng       1634:     $result.='</table><input type="hidden" name="totalparts" value="'.$ctsparts.'" />';
                   1635:     $result.='<input type="button" value="Reset" '.
1.43      ng       1636: 	'onClick="javascript:resetEntry('.$ctsparts.');" TARGET=_self> &nbsp; &nbsp;';
1.45      ng       1637:     $result.='<input type="button" value="Submit Changes" '.
                   1638: 	'onClick="javascript:submit();" TARGET=_self />'."\n";
1.41      ng       1639: 
1.44      ng       1640:     #table listing all the students in a section/class
                   1641:     #header of table
1.42      ng       1642:     $result.= '<table border=0><tr><td bgcolor="#777777">'."\n".
1.41      ng       1643: 	'<table border=0><tr bgcolor="#deffff">'.
1.44      ng       1644: 	'<td><b>Fullname</b></td><td><b>Username</b></td><td><b>Domain</b></td>'."\n";
1.41      ng       1645:     my (@parts) = sort(&getpartlist($url));
                   1646:     foreach my $part (@parts) {
                   1647: 	my $display=&Apache::lonnet::metadata($url,$part.'.display');
                   1648: 	next if ($display =~ /^Number of Attempts/);
                   1649: 	if  (!$display) { $display = &Apache::lonnet::metadata($url,$part.'.name'); }
                   1650: 	if ($display =~ /^Partial Credit Factor/) {
                   1651: 	    $_ = $display;
                   1652: 	    my ($partid) = /.*?(\d+).*/;
1.42      ng       1653: 	    $result.='<td><b>Score Part '.$partid.'<br>(weight = '.
                   1654: 		$weight{$partid}.')</b></td>'."\n";
1.41      ng       1655: 	    next;
                   1656: 	}
1.42      ng       1657: 	$display =~ s/Problem Status/Grade Status<br>/;
1.41      ng       1658: 	$result.='<td><b>'.$display.'</b></td>'."\n";
                   1659:     }
                   1660:     $result.='</tr>';
1.44      ng       1661: 
1.41      ng       1662:     #get info for each student
1.44      ng       1663:     #list all the students - with points and grade status
1.41      ng       1664:     my ($classlist,$seclist,$ids,$stusec,$fullname) = &getclasslist($ENV{'form.section'},'0');
                   1665:     my $ctr = 0;
1.44      ng       1666:     foreach (sort {$$fullname{$a} cmp $$fullname{$b} } keys %$fullname) {
                   1667: 	my ($uname,$udom) = split(/:/);
                   1668: 	$result.='<input type="hidden" name="ctr'.$ctr.'" value="'.$uname.'" />'."\n";
1.41      ng       1669: 	$result.=&viewstudentgrade($url,$symb,$ENV{'request.course.id'},
                   1670: 				   $_,$$fullname{$_},\@parts,\%weight);
                   1671: 	$ctr++;
                   1672:     }
                   1673:     $result.='</table></td></tr></table>';
                   1674:     $result.='<input type="hidden" name="total" value="'.$ctr.'" />'."\n";
1.45      ng       1675:     $result.='<input type="button" value="Submit Changes" '.
                   1676: 	'onClick="javascript:submit();" TARGET=_self /></form>'."\n";
1.41      ng       1677:     $result.=&show_grading_menu_form($symb,$url);
                   1678:     return $result;
                   1679: }
                   1680: 
1.44      ng       1681: #--- call by previous routine to display each student
1.41      ng       1682: sub viewstudentgrade {
                   1683:     my ($url,$symb,$courseid,$student,$fullname,$parts,$weight) = @_;
1.44      ng       1684:     my ($uname,$udom) = split(/:/,$student);
                   1685:     my %record=&Apache::lonnet::restore($symb,$courseid,$udom,$uname);
1.41      ng       1686:     my $result='<tr bgcolor="#ffffdd"><td>'.
1.44      ng       1687: 	'<a href="javascript:viewOneStudent(\''.$uname.'\',\''.$udom.
                   1688: 	'\')"; TARGET=_self>'.$fullname.'</a>'.
                   1689: 	'</td><td>'.$uname.'</td><td align="middle">'.$udom.'</td>'."\n";
1.41      ng       1690:     foreach my $part (@$parts) {
                   1691: 	my ($temp,$part,$type)=split(/_/,$part);
                   1692: 	my $score=$record{"resource.$part.$type"};
                   1693: 	next if $type eq 'tries';
                   1694: 	if ($type eq 'awarded') {
1.42      ng       1695: 	    my $pts = $score eq '' ? '' : $score*$$weight{$part};
                   1696: 	    $result.='<input type="hidden" name="'.
1.44      ng       1697: 		'GD_'.$uname.'_'.$part.'_aw_s" value="'.$pts.'" />'."\n";
1.42      ng       1698: 	    $result.='<td align="middle"><input type="text" name="'.
1.44      ng       1699: 		'GD_'.$uname.'_'.$part.'_aw" '.
                   1700: 		'onChange="javascript:changeSelect('.$part.',\''.$uname.
                   1701: 		'\')" value="'.$pts.'" size="4" /></td>'."\n";
1.41      ng       1702: 	} elsif ($type eq 'solved') {
                   1703: 	    my ($status,$foo)=split(/_/,$score,2);
                   1704: 	    $status = 'nothing' if ($status eq '');
1.42      ng       1705: 	    $result.='<input type="hidden" name="'.
1.44      ng       1706: 		'GD_'.$uname.'_'.$part.'_sv_s" value="'.$status.'" />'."\n";
1.42      ng       1707: 	    $result.='<td align="middle"><select name="'.
1.44      ng       1708: 		'GD_'.$uname.'_'.$part.'_sv" '.
                   1709: 		'onChange="javascript:changeOneScore('.$part.',\''.$uname.'\')" >'."\n";
1.42      ng       1710: 	    my $optsel = '<option selected="on"> </option><option>excused</option>'."\n";
                   1711: 	    $optsel = '<option> </option><option selected="on">excused</option>'."\n"
                   1712: 		if ($status eq 'excused');
1.41      ng       1713: 	    $result.=$optsel;
                   1714: 	    $result.="</select></td>\n";
                   1715: 	}
                   1716:     }
                   1717:     $result.='</tr>';
                   1718:     return $result;
1.38      ng       1719: }
                   1720: 
1.44      ng       1721: #--- change scores for all the students in a section/class
                   1722: #    record does not get update if unchanged
1.38      ng       1723: sub editgrades {
1.41      ng       1724:     my ($request) = @_;
                   1725: 
                   1726:     my $symb=$ENV{'form.symb'};
1.43      ng       1727:     my $url =$ENV{'form.url'};
1.45      ng       1728:     my $title='<h3><font color="#339933">Current Grade Status</font></h3>';
1.44      ng       1729:     $title.='<font size=+1><b>Resource: </b>'.$ENV{'form.url'}.'</font><br />'."\n";
                   1730:     $title.='<font size=+1><b>Section: </b>'.$ENV{'form.section'}.'</font>'."\n";
                   1731:     $title.= &show_grading_menu_form ($symb,$url);
                   1732:     my $result= '<table border="0"><tr><td bgcolor="#777777">'."\n";
1.43      ng       1733:     $result.= '<table border="0"><tr bgcolor="#deffff">'.
                   1734: 	'<td rowspan=2><b>Username</b></td><td rowspan=2><b>Fullname</b></td>'."\n";
                   1735: 
                   1736:     my %scoreptr = (
                   1737: 		    'correct'  =>'correct_by_override',
                   1738: 		    'incorrect'=>'incorrect_by_override',
                   1739: 		    'excused'  =>'excused',
                   1740: 		    'ungraded' =>'ungraded_attempted',
                   1741: 		    'nothing'  => '',
                   1742: 		    );
                   1743:     my ($classlist,$seclist,$ids,$stusec,$fullname) = &getclasslist($ENV{'form.section'},'0');
1.34      ng       1744: 
1.44      ng       1745:     my (@partid);
                   1746:     my %weight = ();
                   1747:     my ($i,$ctr,$count,$rec_update) = (0,0,0,0);
                   1748:     while ($ctr < $ENV{'form.totalparts'}) {
                   1749: 	my $partid = $ENV{'form.partid_'.$ctr};
                   1750: 	push @partid,$partid;
                   1751: 	$weight{$partid} = $ENV{'form.weight_'.$partid};
                   1752: 	$ctr++;
                   1753: 	$result .= '<td colspan = 2 align="center"><b>Part '.$partid.
                   1754: 	    '</b> (Weight = '.$weight{$partid}.')</td>';
                   1755:     }
                   1756:     $result .= '</tr><tr bgcolor="#deffff">';
                   1757:     foreach (@partid) {
                   1758: 	$result .= '<td align="center">&nbsp;<b>Old Score</b>&nbsp;</td>'.
                   1759: 	    '<td align="center">&nbsp;<b>New Score</b>&nbsp;</td>';
                   1760:     }
                   1761:     $result .= '</tr>'."\n";
1.13      albertel 1762: 
1.44      ng       1763:     for ($i=0; $i<$ENV{'form.total'}; $i++) {
                   1764: 	my $user = $ENV{'form.ctr'.$i};
                   1765: 	my %newrecord;
                   1766: 	my $updateflag = 0;
                   1767: 	my @userdom = grep /^$user:/,keys %$classlist;
                   1768: 	my ($foo,$udom) = split(/:/,$userdom[0]);
1.13      albertel 1769: 
1.44      ng       1770: 	$result .= '<tr bgcolor="#ffffde"><td>'.$user.'&nbsp;</td><td>'.
                   1771: 	    $$fullname{$userdom[0]}.'&nbsp;</td>';
1.13      albertel 1772: 
1.44      ng       1773: 	foreach (@partid) {
                   1774: 	    my $old_aw    = $ENV{'form.GD_'.$user.'_'.$_.'_aw_s'};
                   1775: 	    my $old_part  = $old_aw eq '' ? '' : $old_aw/$weight{$_};
                   1776: 	    my $old_score = $scoreptr{$ENV{'form.GD_'.$user.'_'.$_.'_sv_s'}};
1.38      ng       1777: 
1.44      ng       1778: 	    my $awarded   = $ENV{'form.GD_'.$user.'_'.$_.'_aw'};
                   1779: 	    my $partial   = $awarded eq '' ? '' : $awarded/$weight{$_};
                   1780: 	    my $score;
                   1781: 	    if ($partial eq '') {
                   1782: 		$score = $scoreptr{$ENV{'form.GD_'.$user.'_'.$_.'_sv_s'}};
                   1783: 	    } elsif ($partial > 0) {
                   1784: 		$score = 'correct_by_override';
                   1785: 	    } elsif ($partial == 0) {
                   1786: 		$score = 'incorrect_by_override';
                   1787: 	    }
                   1788: 	    $score = 'excused' if (($ENV{'form.GD_'.$user.'_'.$_.'_sv'} eq 'excused') &&
                   1789: 				   ($score ne 'excused'));
                   1790: 	    $result .= '<td align="center">'.$old_aw.'&nbsp;</td>'.
                   1791: 		'<td align="center">'.$awarded.
                   1792: 		($score eq 'excused' ? $score : '').'&nbsp;</td>';
1.5       albertel 1793: 
1.44      ng       1794: 	    next if ($old_part eq $partial && $old_score eq $score);
1.5       albertel 1795: 
1.44      ng       1796: 	    $updateflag = 1;
                   1797: 	    $newrecord{'resource.'.$_.'.awarded'}  = $partial if $partial ne '';
                   1798: 	    $newrecord{'resource.'.$_.'.solved'}   = $score;
                   1799: 	    $rec_update++;
                   1800: 	}
                   1801: 	$result .= '</tr>'."\n";
                   1802: 	if ($updateflag) {
                   1803: 	    $count++;
                   1804: 	    $newrecord{'resource.regrader'}="$ENV{'user.name'}:$ENV{'user.domain'}";
                   1805: 	    &Apache::lonnet::cstore(\%newrecord,$symb,$ENV{'request.course.id'},
                   1806: 				    $udom,$user);
                   1807: 	}
                   1808:     }
                   1809:     $result .= '</table></td></tr></table>'."\n";
                   1810:     my $msg = '<b>Number of records updated = '.$rec_update.
                   1811: 	' for '.$count.' student'.($count <= 1 ? '' : 's').'.</b><br />'.
                   1812: 	'<b>Total number of students = '.$ENV{'form.total'}.'</b><br />';
                   1813:     return $title.$msg.$result;
1.5       albertel 1814: }
1.44      ng       1815: #------------- end of section for handling grading by section/class ---------
                   1816: #
                   1817: #----------------------------------------------------------------------------
                   1818: 
1.5       albertel 1819: 
1.44      ng       1820: #----------------------------------------------------------------------------
                   1821: #
                   1822: #-------------------------- Next few routines handles grading by csv upload
                   1823: #
                   1824: #--- Javascript to handle csv upload
1.27      albertel 1825: sub csvupload_javascript_reverse_associate {
                   1826:   return(<<ENDPICK);
                   1827:   function verify(vf) {
                   1828:     var foundsomething=0;
                   1829:     var founduname=0;
                   1830:     var founddomain=0;
                   1831:     for (i=0;i<=vf.nfields.value;i++) {
                   1832:       tw=eval('vf.f'+i+'.selectedIndex');
                   1833:       if (i==0 && tw!=0) { founduname=1; }
                   1834:       if (i==1 && tw!=0) { founddomain=1; }
                   1835:       if (i!=0 && i!=1 && tw!=0) { foundsomething=1; }
                   1836:     }
                   1837:     if (founduname==0 || founddomain==0) {
                   1838:       alert('You need to specify at both the username and domain');
                   1839:       return;
                   1840:     }
                   1841:     if (foundsomething==0) {
                   1842:       alert('You need to specify at least one grading field');
                   1843:       return;
                   1844:     }
                   1845:     vf.submit();
                   1846:   }
                   1847:   function flip(vf,tf) {
                   1848:     var nw=eval('vf.f'+tf+'.selectedIndex');
                   1849:     var i;
                   1850:     for (i=0;i<=vf.nfields.value;i++) {
                   1851:       //can not pick the same destination field for both name and domain
                   1852:       if (((i ==0)||(i ==1)) && 
                   1853:           ((tf==0)||(tf==1)) && 
                   1854:           (i!=tf) &&
                   1855:           (eval('vf.f'+i+'.selectedIndex')==nw)) {
                   1856:         eval('vf.f'+i+'.selectedIndex=0;')
                   1857:       }
                   1858:     }
                   1859:   }
                   1860: ENDPICK
                   1861: }
                   1862: 
                   1863: sub csvupload_javascript_forward_associate {
                   1864:   return(<<ENDPICK);
                   1865:   function verify(vf) {
                   1866:     var foundsomething=0;
                   1867:     var founduname=0;
                   1868:     var founddomain=0;
                   1869:     for (i=0;i<=vf.nfields.value;i++) {
                   1870:       tw=eval('vf.f'+i+'.selectedIndex');
                   1871:       if (tw==1) { founduname=1; }
                   1872:       if (tw==2) { founddomain=1; }
                   1873:       if (tw>2) { foundsomething=1; }
                   1874:     }
                   1875:     if (founduname==0 || founddomain==0) {
                   1876:       alert('You need to specify at both the username and domain');
                   1877:       return;
                   1878:     }
                   1879:     if (foundsomething==0) {
                   1880:       alert('You need to specify at least one grading field');
                   1881:       return;
                   1882:     }
                   1883:     vf.submit();
                   1884:   }
                   1885:   function flip(vf,tf) {
                   1886:     var nw=eval('vf.f'+tf+'.selectedIndex');
                   1887:     var i;
                   1888:     //can not pick the same destination field twice
                   1889:     for (i=0;i<=vf.nfields.value;i++) {
                   1890:       if ((i!=tf) && (eval('vf.f'+i+'.selectedIndex')==nw)) {
                   1891:         eval('vf.f'+i+'.selectedIndex=0;')
                   1892:       }
                   1893:     }
                   1894:   }
                   1895: ENDPICK
                   1896: }
                   1897: 
1.26      albertel 1898: sub csvuploadmap_header {
1.41      ng       1899:     my ($request,$symb,$url,$datatoken,$distotal)= @_;
                   1900:     my $javascript;
                   1901:     if ($ENV{'form.upfile_associate'} eq 'reverse') {
                   1902: 	$javascript=&csvupload_javascript_reverse_associate();
                   1903:     } else {
                   1904: 	$javascript=&csvupload_javascript_forward_associate();
                   1905:     }
1.45      ng       1906: 
                   1907:     my $result='<table border="0">';
                   1908:     $result.='<tr><td colspan=3><font size=+1><b>Resource: </b>'.$url.'</font></td></tr>';
                   1909:     my ($partlist,$handgrade) = &response_type($url);
                   1910:     my ($resptype,$hdgrade)=('','no');
                   1911:     for (sort keys(%$handgrade)) {
                   1912: 	my ($responsetype,$handgrade)=split(/:/,$$handgrade{$_});
                   1913: 	$resptype = $responsetype;
                   1914: 	$hdgrade = $handgrade if ($handgrade eq 'yes');
                   1915: 	$result.='<tr><td><b>Part </b>'.(split(/_/))[0].'</td>'.
                   1916: 	    '<td><b>Type: </b>'.$responsetype.'</td>'.
                   1917: 	    '<td><b>Handgrade: </b>'.$handgrade.'</font></td></tr>';
                   1918:     }
                   1919:     $result.='</table>';
1.41      ng       1920:     $request->print(<<ENDPICK);
1.26      albertel 1921: <form method="post" enctype="multipart/form-data" action="/adm/grades" name="gradesupload">
1.45      ng       1922: <h3><font color="#339933">Uploading Class Grades</font></h3>
                   1923: $result
1.26      albertel 1924: <hr>
                   1925: <h3>Identify fields</h3>
                   1926: Total number of records found in file: $distotal <hr />
                   1927: Enter as many fields as you can. The system will inform you and bring you back
                   1928: to this page if the data selected is insufficient to run your class.<hr />
                   1929: <input type="button" value="Reverse Association" onClick="javascript:this.form.associate.value='Reverse Association';submit(this.form);" />
                   1930: <input type="hidden" name="associate"  value="" />
                   1931: <input type="hidden" name="phase"      value="three" />
                   1932: <input type="hidden" name="datatoken"  value="$datatoken" />
                   1933: <input type="hidden" name="fileupload" value="$ENV{'form.fileupload'}" />
                   1934: <input type="hidden" name="upfiletype" value="$ENV{'form.upfiletype'}" />
                   1935: <input type="hidden" name="upfile_associate" 
                   1936:                                        value="$ENV{'form.upfile_associate'}" />
                   1937: <input type="hidden" name="symb"       value="$symb" />
                   1938: <input type="hidden" name="url"        value="$url" />
                   1939: <input type="hidden" name="command"    value="csvuploadassign" />
                   1940: <hr />
                   1941: <script type="text/javascript" language="Javascript">
                   1942: $javascript
                   1943: </script>
                   1944: ENDPICK
1.41      ng       1945: return '';
1.26      albertel 1946: 
                   1947: }
                   1948: 
                   1949: sub csvupload_fields {
1.41      ng       1950:     my ($url) = @_;
                   1951:     my (@parts) = &getpartlist($url);
                   1952:     my @fields=(['username','Student Username'],['domain','Student Domain']);
                   1953:     foreach my $part (sort(@parts)) {
                   1954: 	my @datum;
                   1955: 	my $display=&Apache::lonnet::metadata($url,$part.'.display');
                   1956: 	my $name=$part;
                   1957: 	if  (!$display) { $display = $name; }
                   1958: 	@datum=($name,$display);
                   1959: 	push(@fields,\@datum);
                   1960:     }
                   1961:     return (@fields);
1.26      albertel 1962: }
                   1963: 
                   1964: sub csvuploadmap_footer {
1.41      ng       1965:     my ($request,$i,$keyfields) =@_;
                   1966:     $request->print(<<ENDPICK);
1.26      albertel 1967: </table>
                   1968: <input type="hidden" name="nfields" value="$i" />
                   1969: <input type="hidden" name="keyfields" value="$keyfields" />
                   1970: <input type="button" onClick="javascript:verify(this.form)" value="Assign Grades" /><br />
                   1971: </form>
                   1972: ENDPICK
                   1973: }
                   1974: 
                   1975: sub csvuploadmap {
1.41      ng       1976:     my ($request)= @_;
                   1977:     my ($symb,$url)=&get_symb_and_url($request);
                   1978:     if (!$symb) {return '';}
                   1979:     my $datatoken;
                   1980:     if (!$ENV{'form.datatoken'}) {
                   1981: 	$datatoken=&Apache::loncommon::upfile_store($request);
1.26      albertel 1982:     } else {
1.41      ng       1983: 	$datatoken=$ENV{'form.datatoken'};
                   1984: 	&Apache::loncommon::load_tmp_file($request);
1.26      albertel 1985:     }
1.41      ng       1986:     my @records=&Apache::loncommon::upfile_record_sep();
                   1987:     &csvuploadmap_header($request,$symb,$url,$datatoken,$#records+1);
                   1988:     my ($i,$keyfields);
                   1989:     if (@records) {
                   1990: 	my @fields=&csvupload_fields($url);
1.45      ng       1991: 
1.41      ng       1992: 	if ($ENV{'form.upfile_associate'} eq 'reverse') {	
                   1993: 	    &Apache::loncommon::csv_print_samples($request,\@records);
                   1994: 	    $i=&Apache::loncommon::csv_print_select_table($request,\@records,
                   1995: 							  \@fields);
                   1996: 	    foreach (@fields) { $keyfields.=$_->[0].','; }
                   1997: 	    chop($keyfields);
                   1998: 	} else {
                   1999: 	    unshift(@fields,['none','']);
                   2000: 	    $i=&Apache::loncommon::csv_samples_select_table($request,\@records,
                   2001: 							    \@fields);
                   2002: 	    my %sone=&Apache::loncommon::record_sep($records[0]);
                   2003: 	    $keyfields=join(',',sort(keys(%sone)));
                   2004: 	}
                   2005:     }
                   2006:     &csvuploadmap_footer($request,$i,$keyfields);
                   2007:     return '';
1.27      albertel 2008: }
                   2009: 
                   2010: sub csvuploadassign {
1.41      ng       2011:     my ($request)= @_;
                   2012:     my ($symb,$url)=&get_symb_and_url($request);
                   2013:     if (!$symb) {return '';}
                   2014:     &Apache::loncommon::load_tmp_file($request);
1.44      ng       2015:     my @gradedata = &Apache::loncommon::upfile_record_sep();
1.41      ng       2016:     my @keyfields = split(/\,/,$ENV{'form.keyfields'});
                   2017:     my %fields=();
                   2018:     for (my $i=0; $i<=$ENV{'form.nfields'}; $i++) {
                   2019: 	if ($ENV{'form.upfile_associate'} eq 'reverse') {
                   2020: 	    if ($ENV{'form.f'.$i} ne 'none') {
                   2021: 		$fields{$keyfields[$i]}=$ENV{'form.f'.$i};
                   2022: 	    }
                   2023: 	} else {
                   2024: 	    if ($ENV{'form.f'.$i} ne 'none') {
                   2025: 		$fields{$ENV{'form.f'.$i}}=$keyfields[$i];
                   2026: 	    }
                   2027: 	}
1.27      albertel 2028:     }
1.41      ng       2029:     $request->print('<h3>Assigning Grades</h3>');
                   2030:     my $courseid=$ENV{'request.course.id'};
                   2031:     my ($classlist) = &getclasslist('all','1');
                   2032:     my @skipped;
                   2033:     my $countdone=0;
                   2034:     foreach my $grade (@gradedata) {
                   2035: 	my %entries=&Apache::loncommon::record_sep($grade);
                   2036: 	my $username=$entries{$fields{'username'}};
                   2037: 	my $domain=$entries{$fields{'domain'}};
                   2038: 	if (!exists($$classlist{"$username:$domain"})) {
                   2039: 	    push(@skipped,"$username:$domain");
                   2040: 	    next;
                   2041: 	}
                   2042: 	my %grades;
                   2043: 	foreach my $dest (keys(%fields)) {
                   2044: 	    if ($dest eq 'username' || $dest eq 'domain') { next; }
                   2045: 	    if ($entries{$fields{$dest}} eq '') { next; }
                   2046: 	    my $store_key=$dest;
                   2047: 	    $store_key=~s/^stores/resource/;
                   2048: 	    $store_key=~s/_/\./g;
                   2049: 	    $grades{$store_key}=$entries{$fields{$dest}};
                   2050: 	}
                   2051: 	$grades{"resource.regrader"}="$ENV{'user.name'}:$ENV{'user.domain'}";
                   2052: 	&Apache::lonnet::cstore(\%grades,$symb,$ENV{'request.course.id'},
                   2053: 				$domain,$username);
                   2054: 	$request->print('.');
                   2055: 	$request->rflush();
                   2056: 	$countdone++;
                   2057:     }
                   2058:     $request->print("<br />Stored $countdone students\n");
                   2059:     if (@skipped) {
                   2060: 	$request->print('<br /><font size="+1"><b>Skipped Students</b></font><br />');
                   2061: 	foreach my $student (@skipped) { $request->print("<br />$student"); }
                   2062:     }
                   2063:     $request->print(&view_edit_entire_class_form($symb,$url));
                   2064:     $request->print(&show_grading_menu_form($symb,$url));
                   2065:     return '';
1.26      albertel 2066: }
1.44      ng       2067: #------------- end of section for handling csv file upload ---------
                   2068: #
                   2069: #-------------------------------------------------------------------
                   2070: 
                   2071: #-------------------------- Menu interface -------------------------
                   2072: #
                   2073: #--- Show a Grading Menu button - Calls the next routine ---
                   2074: sub show_grading_menu_form {
                   2075:     my ($symb,$url)=@_;
                   2076:     my $result.='<form action="/adm/grades" method="post">'."\n".
                   2077: 	'<input type="hidden" name="symb" value="'.$symb.'" />'."\n".
                   2078: 	'<input type="hidden" name="url" value="'.$url.'" />'."\n".
                   2079: 	'<input type="hidden" name="command" value="gradingmenu" />'."\n".
                   2080: 	'<input type="submit" name="submit" value="Grading Menu" />'."\n".
                   2081: 	'</form>'."\n";
                   2082:     return $result;
                   2083: }
                   2084: 
                   2085: #--- Displays the main menu page -------
                   2086: sub gradingmenu {
                   2087:     my ($request) = @_;
                   2088:     my ($symb,$url)=&get_symb_and_url($request);
                   2089:     if (!$symb) {return '';}
1.45      ng       2090:     my $result='<h3>&nbsp;<font color="#339933">Select a Grading Method</font></h3>';
1.44      ng       2091:     $result.='<table border="0">';
                   2092:     $result.='<tr><td colspan=3><font size=+1><b>Resource: </b>'.$url.'</font></td></tr>';
                   2093:     my ($partlist,$handgrade) = &response_type($url);
                   2094:     my ($resptype,$hdgrade)=('','no');
                   2095:     for (sort keys(%$handgrade)) {
                   2096: 	my ($responsetype,$handgrade)=split(/:/,$$handgrade{$_});
                   2097: 	$resptype = $responsetype;
                   2098: 	$hdgrade = $handgrade if ($handgrade eq 'yes');
                   2099: 	$result.='<tr><td><b>Part </b>'.(split(/_/))[0].'</td>'.
                   2100: 	    '<td><b>Type: </b>'.$responsetype.'</td>'.
                   2101: 	    '<td><b>Handgrade: </b>'.$handgrade.'</font></td></tr>';
                   2102:     }
                   2103:     $result.='</table>';
                   2104:     $result.=&view_edit_entire_class_form($symb,$url).'<br />';
                   2105:     $result.=&upcsvScores_form($symb,$url).'<br />';
                   2106:     $result.=&viewGradeaStu_form($symb,$url,$resptype,$hdgrade).'<br />';
                   2107:     $result.=&verifyReceipt_form($symb,$url) 
                   2108: 	if ((&Apache::lonnet::allowed('mgr',$ENV{'request.course.id'})) && ($symb));
                   2109:  
                   2110:     return $result;
                   2111: }
                   2112: 
                   2113: #--- Menu for grading a section or the whole class ---
                   2114: sub view_edit_entire_class_form {
                   2115:     my ($symb,$url)=@_;
                   2116:     my ($classlist,$sections) = &getclasslist('all','0');
                   2117:     my $result.='<table width=100% border=0><tr><td bgcolor=#777777>'."\n";
                   2118:     $result.='<table width=100% border=0><tr bgcolor="#e6ffff"><td>'."\n";
                   2119:     $result.='&nbsp;<b>View/Grade Entire Section/Class</b></td></tr>'."\n";
                   2120:     $result.='<tr bgcolor=#ffffe6><td>'."\n";
                   2121:     $result.='<form action="/adm/grades" method="post">'."\n".
                   2122: 	'<input type="hidden" name="symb" value="'.$symb.'" />'."\n".
                   2123: 	'<input type="hidden" name="url" value="'.$url.'" />'."\n".
                   2124: 	'<input type="hidden" name="command" value="viewgrades" />'."\n";
                   2125:     $result.='&nbsp;<b>Select section:</b> <select name="section">'."\n";
1.49      albertel 2126:     if (ref($sections)) {
                   2127: 	foreach (sort (@$sections)) {
                   2128: 	    $result.= '<option>'.$_.'</option>'."\n";
                   2129: 	}
1.44      ng       2130:     }
                   2131:     $result.='<option selected="on">all</select>'."<br />\n";
1.46      ng       2132:     $result.='&nbsp;<input type="button" onClick="submit();" value="View/Grade" /></form>'."\n";
1.44      ng       2133:     $result.='</td></tr></table>'."\n";
                   2134:     $result.='</td></tr></table>'."\n";
                   2135:     return $result;
                   2136: }
                   2137: 
                   2138: #--- Menu to upload a csv scores ---
                   2139: sub upcsvScores_form {
                   2140:     my ($symb,$url) = @_;
                   2141:     if (!$symb) {return '';}
1.46      ng       2142:     my $result = '<script type="text/javascript" language="javascript">'."\n".
                   2143: 	'  function checkUpload(formname) {'."\n".
                   2144: 	'    if (formname.upfile.value == "") {'."\n".
                   2145: 	'       alert("Please use the browse button to select a file from your local directory.");'."\n".
                   2146: 	'       return false;'."\n".
                   2147: 	'    }'."\n".
                   2148: 	'    formname.submit();'."\n".
                   2149: 	'  }'."\n".
                   2150: 	'</script>'."\n";
                   2151: 
                   2152:     $result.='<table width=100% border=0><tr><td bgcolor=#777777>'."\n";
1.44      ng       2153:     $result.='<table width=100% border=0><tr bgcolor="#e6ffff"><td>'."\n";
                   2154:     $result.='&nbsp;<b>Specify a file containing the class scores for above resource</b></td></tr>'."\n";
                   2155:     $result.='<tr bgcolor=#ffffe6><td>'."\n";
                   2156:     my $upfile_select=&Apache::loncommon::upfile_select_html();
                   2157:   $result.=<<ENDUPFORM;
                   2158: <form method="post" enctype="multipart/form-data" action="/adm/grades" name="gradesupload">
                   2159: <input type="hidden" name="symb" value="$symb" />
                   2160: <input type="hidden" name="url" value="$url" />
                   2161: <input type="hidden" name="command" value="csvuploadmap" />
                   2162: $upfile_select
1.46      ng       2163: <br />&nbsp;<input type="button" onClick="javascript:checkUpload(this.form);" value="Upload Grades" />
1.44      ng       2164: </form>
                   2165: ENDUPFORM
                   2166:     $result.='</td></tr></table>'."\n";
                   2167:     $result.='</td></tr></table>'."\n";
                   2168:     return $result;
                   2169: }
                   2170: 
                   2171: #--- Handgrading problems ---
                   2172: sub viewGradeaStu_form {
                   2173:     my ($symb,$url,$response,$handgrade) = @_;
                   2174:     my ($classlist,$sections) = &getclasslist('all','0');
                   2175:     my $result.='<table width=100% border=0><tr><td bgcolor=#777777>'."\n";
                   2176:     $result.='<table width=100% border=0><tr bgcolor="#e6ffff"><td>'."\n";
1.49      albertel 2177:     $result.='&nbsp;<b>';
                   2178:     if ($handgrade eq 'yes') {
                   2179: 	$result.="View/Grade ";
                   2180:     } else {
                   2181: 	$result.="View ";
                   2182:     }
                   2183:     $result.='an Individual Student\'s Submission</b></td></tr>'."\n";
1.44      ng       2184:     $result.='<tr bgcolor=#ffffe6><td>'."\n";
                   2185:     $result.='<form action="/adm/grades" method="post">'."\n".
                   2186: 	'<input type="hidden" name="symb" value="'.$symb.'" />'."\n".
                   2187: 	'<input type="hidden" name="url" value="'.$url.'" />'."\n".
                   2188: 	'<input type="hidden" name="response" value="'.$response.'" />'."\n".
                   2189: 	'<input type="hidden" name="handgrade" value="'.$handgrade.'" />'."\n".
                   2190: 	'<input type="hidden" name="showgrading" value="yes" />'."\n".
                   2191: 	'<input type="hidden" name="command" value="submission" />'."\n";
1.26      albertel 2192: 
1.44      ng       2193:     $result.='&nbsp;<b>Select section:</b> <select name="section">'."\n";
1.49      albertel 2194:     if (ref($sections)) {
                   2195: 	foreach (sort (@$sections)) {$result.='<option>'.$_.'</option>'."\n";}
1.44      ng       2196:     }
                   2197:     $result.= '<option selected="on">all</select>'."\n";
                   2198:     $result.='&nbsp;&nbsp;<b>Display students who has: </b>'.
                   2199: 	'<input type="radio" name="submitonly" value="yes" checked> submitted'.
                   2200: 	'<input type="radio" name="submitonly" value="all"> everybody <br />';
1.49      albertel 2201:     if (ref($sections)) {
                   2202: 	$result.='&nbsp;(Section "no" implies the students were not assigned a section.)<br />' 
                   2203: 	    if (grep /no/,@$sections);
                   2204:     }
                   2205: 
                   2206: 
                   2207:     $result.='<br />&nbsp;<input type="button" onClick="submit();" value="';
                   2208:     if ($handgrade eq 'yes') {
                   2209: 	$result.="View/Grade";
                   2210:     } else {
                   2211: 	$result.="View";
                   2212:     }
                   2213:     $result.='" />'."\n".'</form>'."\n";
1.44      ng       2214:     $result.='</td></tr></table>'."\n";
                   2215:     $result.='</td></tr></table>'."\n";
                   2216:     return $result;
1.2       albertel 2217: }
                   2218: 
1.44      ng       2219: #--- Form to input a receipt number ---
                   2220: sub verifyReceipt_form {
                   2221:     my ($symb,$url) = @_;
1.46      ng       2222:     my $result = '<script type="text/javascript" language="javascript">'."\n".
                   2223: 	'  function checkEntry(formname) {'."\n".
                   2224: 	'    var receipt = formname.receipt.value;'."\n".
                   2225: 	'    if (isNaN(receipt) || receipt == "") {'."\n".
                   2226: 	'       alert("Please enter a receipt number given by a student in the box.");'."\n".
                   2227: 	'       return false;'."\n".
                   2228: 	'    }'."\n".
                   2229: 	'    formname.submit();'."\n".
                   2230: 	'  }'."\n".
                   2231: 	'</script>'."\n";
                   2232: 
1.44      ng       2233:     my $hostver=unpack("%32C*",$Apache::lonnet::perlvar{'lonHostID'});
                   2234: 
1.46      ng       2235:     $result.='<table width=100% border=0><tr><td bgcolor=#777777>'."\n";
1.44      ng       2236:     $result.='<table width=100% border=0><tr><td bgcolor=#e6ffff>'."\n";
                   2237:     $result.='&nbsp;<b>Verify a Submission Receipt Issued by this Server</td></tr>'."\n";
                   2238:     $result.='<tr bgcolor=#ffffe6><td>'."\n";
1.46      ng       2239:     $result.='<form action="/adm/grades" method="post" name="verifyform">'."\n";
1.44      ng       2240:     $result.='&nbsp;<tt>'.$hostver.'-<input type="text" name="receipt" size="4"></tt><br />'."\n";
1.46      ng       2241:     $result.='&nbsp;<input type="button" onClick="javascript:checkEntry(this.form);"'.
                   2242: 	' value="Verify Receipt">'."\n";
1.44      ng       2243:     $result.='<input type="hidden" name="command" value="verify">'."\n";
                   2244:     if ($ENV{'form.url'}) {
                   2245: 	$result.='<input type="hidden" name="url" value="'.$ENV{'form.url'}.'" />';
                   2246:     }
                   2247:     if ($ENV{'form.symb'}) {
                   2248: 	$result.='<input type="hidden" name="symb" value="'.$ENV{'form.symb'}.'" />';
                   2249:     }
                   2250:     $result.='</form>';
                   2251:     $result.='</td></tr></table>'."\n";
                   2252:     $result.='</td></tr></table>'."\n";
                   2253:     return $result;
1.2       albertel 2254: }
                   2255: 
1.1       albertel 2256: sub handler {
1.41      ng       2257:     my $request=$_[0];
                   2258:     
                   2259:     if ($ENV{'browser.mathml'}) {
                   2260: 	$request->content_type('text/xml');
                   2261:     } else {
                   2262: 	$request->content_type('text/html');
                   2263:     }
                   2264:     $request->send_http_header;
1.44      ng       2265:     return '' if $request->header_only;
1.41      ng       2266:     &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'});
                   2267:     my $url=$ENV{'form.url'};
                   2268:     my $symb=$ENV{'form.symb'};
                   2269:     my $command=$ENV{'form.command'};
                   2270:     if (!$url) {
                   2271: 	my ($temp1,$temp2);
                   2272: 	($temp1,$temp2,$ENV{'form.url'})=split(/___/,$symb);
                   2273: 	$url = $ENV{'form.url'};
                   2274:     }
                   2275:     &send_header($request);
                   2276:     if ($url eq '' && $symb eq '') {
                   2277: 	if ($ENV{'user.adv'}) {
                   2278: 	    if (($ENV{'form.codeone'}) && ($ENV{'form.codetwo'}) &&
                   2279: 		($ENV{'form.codethree'})) {
                   2280: 		my $token=$ENV{'form.codeone'}.'*'.$ENV{'form.codetwo'}.'*'.
                   2281: 		    $ENV{'form.codethree'};
                   2282: 		my ($tsymb,$tuname,$tudom,$tcrsid)=
                   2283: 		    &Apache::lonnet::checkin($token);
                   2284: 		if ($tsymb) {
                   2285: 		    my ($map,$id,$url)=split(/\_\_\_/,$tsymb);
                   2286: 		    if (&Apache::lonnet::allowed('mgr',$tcrsid)) {
                   2287: 			$request->print(
                   2288: 					&Apache::lonnet::ssi('/res/'.$url,
                   2289: 							     ('grade_username' => $tuname,
                   2290: 							      'grade_domain' => $tudom,
                   2291: 							      'grade_courseid' => $tcrsid,
                   2292: 							      'grade_symb' => $tsymb)));
                   2293: 		    } else {
1.45      ng       2294: 			$request->print('<h3>Not authorized: '.$token.'</h3>');
1.41      ng       2295: 		    }           
                   2296: 		} else {
1.45      ng       2297: 		    $request->print('<h3>Not a valid DocID: '.$token.'</h3>');
1.41      ng       2298: 		}
1.14      www      2299: 	    } else {
1.41      ng       2300: 		$request->print(&Apache::lonxml::tokeninputfield());
                   2301: 	    }
                   2302: 	}
                   2303:     } else {
                   2304: 	$Apache::grades::viewgrades=&Apache::lonnet::allowed('vgr',$ENV{'request.course.id'});
                   2305: 	if ($command eq 'submission') {
                   2306: 	    &listStudents($request) if ($ENV{'form.student'} eq '');
                   2307: 	    &submission($request,0,0) if ($ENV{'form.student'} ne '');
                   2308: 	} elsif ($command eq 'processGroup') {
                   2309: 	    &processGroup($request);
                   2310: 	} elsif ($command eq 'gradingmenu') {
                   2311: 	    $request->print(&gradingmenu($request));
                   2312: 	} elsif ($command eq 'viewgrades') {
                   2313: 	    $request->print(&viewgrades($request));
                   2314: 	} elsif ($command eq 'handgrade') {
                   2315: 	    $request->print(&processHandGrade($request));
                   2316: 	} elsif ($command eq 'editgrades') {
                   2317: 	    $request->print(&editgrades($request));
                   2318: 	} elsif ($command eq 'verify') {
                   2319: 	    $request->print(&verifyreceipt($request));
                   2320: 	} elsif ($command eq 'csvupload') {
                   2321: 	    $request->print(&csvupload($request));
                   2322: 	} elsif ($command eq 'viewclasslist') {
                   2323: 	    $request->print(&viewclasslist($request));
                   2324: 	} elsif ($command eq 'csvuploadmap') {
                   2325: 	    $request->print(&csvuploadmap($request));
                   2326: 	} elsif ($command eq 'csvuploadassign') {
                   2327: 	    if ($ENV{'form.associate'} ne 'Reverse Association') {
                   2328: 		$request->print(&csvuploadassign($request));
                   2329: 	    } else {
                   2330: 		if ( $ENV{'form.upfile_associate'} ne 'reverse' ) {
                   2331: 		    $ENV{'form.upfile_associate'} = 'reverse';
                   2332: 		} else {
                   2333: 		    $ENV{'form.upfile_associate'} = 'forward';
                   2334: 		}
                   2335: 		$request->print(&csvuploadmap($request));
                   2336: 	    }
1.26      albertel 2337: 	} else {
1.41      ng       2338: 	    $request->print("Unknown action: $command:");
1.26      albertel 2339: 	}
1.2       albertel 2340:     }
1.41      ng       2341:     &send_footer($request);
1.44      ng       2342:     return '';
                   2343: }
                   2344: 
                   2345: sub send_header {
                   2346:     my ($request)= @_;
                   2347:     $request->print(&Apache::lontexconvert::header());
                   2348: #  $request->print("
                   2349: #<script>
                   2350: #remotewindow=open('','homeworkremote');
                   2351: #remotewindow.close();
                   2352: #</script>"); 
1.47      www      2353:     $request->print(&Apache::loncommon::bodytag('Grading'));
1.44      ng       2354: }
                   2355: 
                   2356: sub send_footer {
                   2357:     my ($request)= @_;
                   2358:     $request->print('</body>');
                   2359:     $request->print(&Apache::lontexconvert::footer());
1.1       albertel 2360: }
                   2361: 
                   2362: 1;
                   2363: 
1.13      albertel 2364: __END__;

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