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

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

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