File:  [LON-CAPA] / loncom / homework / grades.pm
Revision 1.123: download - view: text, annotated - select for diffs
Wed Jul 23 17:33:59 2003 UTC (20 years, 10 months ago) by ng
Branches: MAIN
CVS tags: HEAD
fix bug 1315 - encode the following characters (<,>,&,") in message box (essay grading)
Test the script on following browsers: IE 5.5 on windows, IE ?? on mac, mozilla 5,
netscape 4.76 and konqueror on linux, and netscape 4.7 on windows.

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

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