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

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

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