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

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

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