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

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

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