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

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

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