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

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

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