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

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

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