File:  [LON-CAPA] / loncom / homework / grades.pm
Revision 1.120: download - view: text, annotated - select for diffs
Sat Jul 19 15:11:27 2003 UTC (20 years, 10 months ago) by ng
Branches: MAIN
CVS tags: HEAD
fix bug 763 - when displaying submission page for a student from the chart page gives option to grade that student.

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

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