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

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

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