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

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

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