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

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

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