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

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

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