File:  [LON-CAPA] / loncom / homework / grades.pm
Revision 1.134: download - view: text, annotated - select for diffs
Mon Aug 18 18:24:32 2003 UTC (20 years, 8 months ago) by www
Branches: MAIN
CVS tags: HEAD
Ray's logo for SFU
Logo for Bear Creek

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

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