File:  [LON-CAPA] / loncom / homework / grades.pm
Revision 1.122: download - view: text, annotated - select for diffs
Tue Jul 22 18:59:57 2003 UTC (20 years, 10 months ago) by ng
Branches: MAIN
CVS tags: HEAD
restore number of tries column for grading by section/course
Where is the can of raid? manual grading record - graded by grader has missing period ...
Add highlight of keywords when displaying by page/sequence for essay response part.

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

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