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

1.17      albertel    1: # The LearningOnline Network with CAPA
1.13      albertel    2: # The LON-CAPA Grading handler
1.17      albertel    3: #
1.41    ! ng          4: # $Id: grades.pm,v 1.40 2002/07/19 20:42:18 ng Exp $
1.17      albertel    5: #
                      6: # Copyright Michigan State University Board of Trustees
                      7: #
                      8: # This file is part of the LearningOnline Network with CAPA (LON-CAPA).
                      9: #
                     10: # LON-CAPA is free software; you can redistribute it and/or modify
                     11: # it under the terms of the GNU General Public License as published by
                     12: # the Free Software Foundation; either version 2 of the License, or
                     13: # (at your option) any later version.
                     14: #
                     15: # LON-CAPA is distributed in the hope that it will be useful,
                     16: # but WITHOUT ANY WARRANTY; without even the implied warranty of
                     17: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
                     18: # GNU General Public License for more details.
                     19: #
                     20: # You should have received a copy of the GNU General Public License
                     21: # along with LON-CAPA; if not, write to the Free Software
                     22: # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
                     23: #
                     24: # /home/httpd/html/adm/gpl.txt
                     25: #
                     26: # http://www.lon-capa.org/
                     27: #
1.13      albertel   28: # 2/9,2/13 Guy Albertelli
1.8       www        29: # 6/8 Gerd Kortemeyer
1.13      albertel   30: # 7/26 H.K. Ng
1.14      www        31: # 8/20 Gerd Kortemeyer
1.30      ng         32: # Year 2002
1.35      ng         33: # June, July 2002 H.K. Ng
1.30      ng         34: #
1.1       albertel   35: 
                     36: package Apache::grades;
                     37: use strict;
                     38: use Apache::style;
                     39: use Apache::lonxml;
                     40: use Apache::lonnet;
1.3       albertel   41: use Apache::loncommon;
1.1       albertel   42: use Apache::lonhomework;
1.38      ng         43: use Apache::lonmsg qw(:user_normal_msg);
1.1       albertel   44: use Apache::Constants qw(:common);
1.39      ng         45: #use Time::HiRes qw( gettimeofday tv_interval );
1.1       albertel   46: 
1.2       albertel   47: sub moreinfo {
1.41    ! ng         48:     my ($request,$reason) = @_;
        !            49:     $request->print("Unable to process request: $reason");
        !            50:     if ( $Apache::grades::viewgrades eq 'F' ) {
        !            51: 	$request->print('<form action="/adm/grades" method="post">'."\n");
        !            52: 	if ($ENV{'form.url'}) {
        !            53: 	    $request->print('<input type="hidden" name="url" value="'.$ENV{'form.url'}.'" />'."\n");
        !            54: 	}
        !            55: 	if ($ENV{'form.symb'}) {
        !            56: 	    $request->print('<input type="hidden" name="symb" value="'.$ENV{'form.symb'}.'" />'."\n");
        !            57: 	}
        !            58: #    $request->print('<input type="hidden" name="command" value="submission" />'."\n");
        !            59: 	$request->print('<input type="hidden" name="command" value="'.$ENV{'form.command'}.'" />'."\n");
        !            60: 	$request->print("Student:".'<input type="text" name="student" value="'.$ENV{'form.student'}.'" />'."<br />\n");
        !            61: 	$request->print("Domain:".'<input type="text" name="domain" value="'.$ENV{'user.domain'}.'" />'."<br />\n");
        !            62: 	$request->print('<input type="submit" name="submit" value="ReSubmit" />'."<br />\n");
        !            63: 	$request->print('</form>');
1.16      albertel   64:     }
1.41    ! ng         65:     return '';
1.2       albertel   66: }
                     67: 
1.23      www        68: sub verifyreceipt {
                     69:     my $request=shift;
                     70:     my $courseid=$ENV{'request.course.id'};
1.34      ng         71: #    my $cdom=$ENV{"course.$courseid.domain"};
                     72: #    my $cnum=$ENV{"course.$courseid.num"};
1.23      www        73:     my $receipt=unpack("%32C*",$Apache::lonnet::perlvar{'lonHostID'}).'-'.
1.41    ! ng         74: 	$ENV{'form.receipt'};
1.23      www        75:     $receipt=~s/[^\-\d]//g;
                     76:     my $symb=$ENV{'form.symb'};
                     77:     unless ($symb) {
                     78: 	$symb=&Apache::lonnet::symbread($ENV{'form.url'});
                     79:     }
                     80:     if ((&Apache::lonnet::allowed('mgr',$courseid)) && ($symb)) {
                     81:         $request->print('<h1>Verifying Submission Receipt '.$receipt.'</h1>');
                     82:         my $matches=0;
1.34      ng         83:         my ($classlist) = &getclasslist('all','0');
                     84:         foreach my $student ( sort(@{ $$classlist{'all'} }) ) {
1.23      www        85:             my ($uname,$udom)=split(/\:/,$student);
                     86:             if ($receipt eq 
1.41    ! ng         87: 		&Apache::lonnet::ireceipt($uname,$udom,$courseid,$symb)) {
        !            88: 		$request->print('Matching '.$student.'<br>');
        !            89: 		$matches++;
1.23      www        90: 	   }
                     91:         }
1.30      ng         92:         $request->printf('<p>'.$matches." match%s</p>",$matches <= 1 ? '' : 'es');
1.33      ng         93: # needs to print who is matched
1.23      www        94:     }
                     95:     return '';
                     96: }
1.13      albertel   97: 
1.32      ng         98: sub student_gradeStatus {
1.41    ! ng         99:     my ($url,$udom,$uname,$partlist) = @_;
        !           100:     my $symb=($ENV{'form.symb'} ne '' ? $ENV{'form.symb'} : (&Apache::lonnet::symbread($url)));
        !           101:     my %record= &Apache::lonnet::restore($symb,$ENV{'request.course.id'},$udom,$uname);
        !           102:     my %partstatus = ();
        !           103:     foreach (@$partlist) {
        !           104: 	my ($status,$foo)=split(/_/,$record{"resource.$_.solved"},2);
        !           105: 	$status = 'nothing' if ($status eq '');
        !           106: 	$partstatus{$_} = $status;
        !           107: 	$partstatus{"resource.$_.submitted_by"} = $record{"resource.$_.submitted_by"} 
        !           108: 	if ($record{"resource.$_.submitted_by"} ne '');
        !           109:     }
        !           110:     return %partstatus;
1.32      ng        111: }
                    112: 
1.34      ng        113: sub get_fullname {
1.39      ng        114:     my ($uname,$udom) = @_;
1.34      ng        115:     my %name=&Apache::lonnet::get('environment', ['lastname','generation',
1.41    ! ng        116: 						  'firstname','middlename'],$udom,$uname);
1.34      ng        117:     my $fullname;
                    118:     my ($tmp) = keys(%name);
                    119:     if ($tmp !~ /^(con_lost|error|no_such_host)/i) {
                    120: 	$fullname=$name{'lastname'}.$name{'generation'};
                    121: 	if ($fullname =~ /[^\s]+/) { $fullname.=', '; }
                    122: 	$fullname.=$name{'firstname'}.' '.$name{'middlename'};
                    123:     }
                    124:     return $fullname;
                    125: }
                    126: 
1.39      ng        127: sub response_type {
1.41    ! ng        128:     my ($url) = shift;
        !           129:     my $allkeys = &Apache::lonnet::metadata($url,'keys');
        !           130: #    print "allkeys=>$allkeys<br>";
        !           131:     my %seen = ();
        !           132:     my (@partlist,%handgrade);
        !           133:     foreach (split(/,/,&Apache::lonnet::metadata($url,'packages'))) {
        !           134: 	if (/^\w+response_\d{1,2}.*/) {
        !           135: 	    my ($responsetype,$part) = split(/_/,$_,2);
        !           136: 	    my ($partid,$respid) = split(/_/,$part);
        !           137: 	    $handgrade{$part} = $responsetype.':'.($allkeys =~ /parameter_$part\_handgrade/ ? 'yes' : 'no');
        !           138: 	    next if ($seen{$partid} > 0);
        !           139: 	    $seen{$partid}++;
        !           140: 	    push @partlist,$partid;
        !           141: 	}
        !           142:     }
        !           143:     return \@partlist,\%handgrade;
1.39      ng        144: }
                    145: 
                    146: 
1.30      ng        147: sub listStudents {
1.41    ! ng        148:     my ($request) = shift;
        !           149:     my $cdom      =$ENV{"course.$ENV{'request.course.id'}.domain"};
        !           150:     my $cnum      =$ENV{"course.$ENV{'request.course.id'}.num"};
        !           151:     my $getsec    =$ENV{'form.section'} eq '' ? 'all' : $ENV{'form.section'};
        !           152:     my $submitonly=$ENV{'form.submitonly'} eq '' ? 'all' : $ENV{'form.submitonly'};
        !           153: 
        !           154:     my $result='<h2><font color="#339933">&nbsp;View Submissions for a Student or a Group of Students</font></h2>';
        !           155:     $result.='<table border="0">';
        !           156:     $result.='<tr><td colspan=3><font size=+1><b>Resource: </b>'.$ENV{'form.url'}.'</font></td></tr>';
        !           157:     my ($partlist,$handgrade) = &response_type($ENV{'form.url'});
        !           158:     for (sort keys(%$handgrade)) {
        !           159: 	my ($responsetype,$handgrade)=split(/:/,$$handgrade{$_});
        !           160: 	$ENV{'form.handgrade'} = 'yes' if ($handgrade eq 'yes');
        !           161: 	$result.='<tr><td><b>Part id: </b>'.$_.'</td>'.
        !           162: 	    '<td><b>Type: </b>'.$responsetype.'</td>'.
        !           163: 	    '<td><b>Handgrade: </b>'.$handgrade.'</font></td></tr>';
        !           164:     }
        !           165:     $result.='</table>';
        !           166:     $request->print($result);
1.39      ng        167: 
1.41    ! ng        168:     $request->print(<<ENDTABLEST);
1.39      ng        169: <form action="/adm/grades" method="post">
1.34      ng        170: &nbsp;<b>View Problem: </b><input type="radio" name="vProb" value="no" checked> no 
1.37      ng        171: <input type="radio" name="vProb" value="yes"> yes <br />
                    172: &nbsp;<b>Submissions: </b>
1.40      ng        173: <input type="radio" name="lastSub" value="hdgrade" checked /> handgrade only
                    174: <input type="radio" name="lastSub" value="lastonly" /> last sub only
1.39      ng        175: <input type="radio" name="lastSub" value="last" /> last sub & parts info
                    176: <input type="radio" name="lastSub" value="all" /> all details
                    177: <input type="hidden" name="section" value="$getsec" />
                    178: <input type="hidden" name="submitonly" value="$submitonly" />
                    179: <input type="hidden" name="response" value="$ENV{'form.response'}" />
1.41    ! ng        180: <input type="hidden" name="handgrade" value="$ENV{'form.handgrade'}" /><br />
        !           181: <input type="hidden" name="showgrading" value="$ENV{'form.showgrading'}" /><br />
1.39      ng        182: <input type="submit" name="submit" value="View/Grade" />
1.23      www       183: ENDTABLEST
1.41    ! ng        184:     if ($ENV{'form.url'}) {
        !           185: 	$request->print('<input type="hidden" name="url" value="'.$ENV{'form.url'}.'" />'."\n");
        !           186:     }
        !           187:     if ($ENV{'form.symb'}) {
        !           188: 	$request->print('<input type="hidden" name="symb" value="'.$ENV{'form.symb'}.'" />'."\n");
        !           189:     }
        !           190:     $request->print('<input type="hidden" name="command" value="processGroup" />'."\n");
        !           191:     
        !           192:     my ($classlist,$seclist,$ids,$stusec,$fullname) = &getclasslist($getsec,'0');
        !           193:     
        !           194:     $result='<table border="0"><tr><td bgcolor="#777777">'.
        !           195: 	'<table border="0"><tr bgcolor="#e6ffff">'.
        !           196: 	'<td><b>&nbsp;Select&nbsp;</b></td><td><b>&nbsp;Username&nbsp;</b></td>'.
        !           197: 	'<td><b>&nbsp;Fullname&nbsp;</b></td><td><b>&nbsp;Domain&nbsp;</b></td>';
        !           198:     foreach (sort(@$partlist)) {
        !           199: 	$result.='<td><b>&nbsp;Part ID '.$_.' Status&nbsp;</b></td>';
        !           200:     }
        !           201:     $request->print($result.'</tr>'."\n");
        !           202: 
        !           203:     foreach my $student (sort(@{ $$classlist{$getsec} }) ) {
        !           204: 	my ($uname,$udom) = split(/:/,$student);
        !           205: 	my (%status) = &student_gradeStatus($ENV{'form.url'},$udom,$uname,$partlist);
        !           206: 	my $statusflg = '';
        !           207: 	foreach (keys(%status)) {
        !           208: 	    $statusflg = 1 if ($status{$_} ne 'nothing');
        !           209: 	    my ($foo,$partid,$foo) = split(/\./,$_);
        !           210: 	    if ($status{'resource.'.$partid.'.submitted_by'} ne '') {
        !           211: 		$statusflg = '';
        !           212: 		$request->print('<input type="hidden" name="'.
        !           213: 				$student.':submitted_by" value="'.
        !           214: 				$status{'resource.'.$partid.'.submitted_by'}.'" />');
        !           215: 	    }
        !           216: 	}
        !           217: 	next if ($statusflg eq '' && $submitonly eq 'yes');
1.34      ng        218: 
1.41    ! ng        219: 	if ( $Apache::grades::viewgrades eq 'F' ) {
        !           220: 	    $result='<tr bgcolor="#ffffe6">'.
        !           221: 		'<td align="center"><input type=checkbox name="stuinfo" value="'.
        !           222: 		$student.':'.$$fullname{$student}.'"></td>'."\n".
        !           223: 		'<td>&nbsp;'.$uname.'&nbsp;</td>'."\n".
        !           224: 		'<td>&nbsp;'.$$fullname{$student}.'&nbsp;</td>'."\n".
        !           225: 		'<td align="middle">&nbsp;'.$udom.'&nbsp;</td>'."\n";
        !           226: 	    
        !           227: 	    foreach (sort keys(%status)) {
        !           228: 		next if (/^resource.*?submitted_by$/);
        !           229: 		$result.='<td align="middle">&nbsp;'.$status{$_}.'&nbsp;</td>'."\n";
        !           230: 	    }
        !           231: 	    $request->print($result.'</tr>'."\n");
        !           232: 	}
        !           233:     }
        !           234:     $request->print('</table></td></tr></table>');
        !           235:     $request->print('<input type="submit" name="submit" value="View/Grade" /><form />');
1.10      ng        236: }
                    237: 
1.34      ng        238: sub processGroup {
1.41    ! ng        239:     my ($request)  = shift;
        !           240:     my $ctr        = 0;
        !           241:     my @stuchecked = (ref($ENV{'form.stuinfo'}) ? @{$ENV{'form.stuinfo'}}
        !           242: 		      : ($ENV{'form.stuinfo'}));
        !           243:     my $total      = scalar(@stuchecked)-1;
        !           244:     if ($stuchecked[0] eq '') {
        !           245: 	&userError($request,'No student was selected for viewing/grading.');
        !           246: 	return;
        !           247:     }
        !           248:     foreach (@stuchecked) {
        !           249: 	my ($uname,$udom,$fullname) = split(/:/);
        !           250: 	$ENV{'form.student'} = $uname;
        !           251: 	$ENV{'form.fullname'} = $fullname;
        !           252: 	&submission($request,$ctr,$total);
        !           253: 	$ctr++;
        !           254:     }
        !           255:     return '';
1.35      ng        256: }
1.34      ng        257: 
1.35      ng        258: sub userError {
                    259:     my ($request, $reason, $step) = @_;
                    260:     $request->print('<h3><font color="red">LON-CAPA User Error</font></h3><br />'."\n");
                    261:     $request->print('<b>Reason: </b>'.$reason.'<br /><br />'."\n");
                    262:     $request->print('<b>Step: </b>'.($step ne '' ? $step : 'Use your browser back button to correct')
                    263: 		    .'<br /><br />'."\n");
                    264:     return '';
1.34      ng        265: }
1.13      albertel  266: 
1.7       albertel  267: #FIXME - needs to handle multiple matches
1.2       albertel  268: sub finduser {
1.41    ! ng        269:     my ($name) = @_;
        !           270:     my $domain = '';
        !           271:     if ( $Apache::grades::viewgrades eq 'F' ) {
        !           272: 	my ($classlist) = &getclasslist('all','0');
        !           273: 	foreach ( sort(@{ $$classlist{'all'} }) ) {
        !           274: 	    my ($posname,$posdomain) = split(/:/);
        !           275: 	    if ($posname =~ $name) { $name=$posname; $domain=$posdomain; last; }
        !           276: 	}
        !           277: 	return ($name,$domain);
        !           278:     } else {
        !           279: 	return ($ENV{'user.name'},$ENV{'user.domain'});
        !           280:     }
1.5       albertel  281: }
                    282: 
                    283: sub getclasslist {
1.41    ! ng        284:     my ($getsec,$hideexpired) = @_;
        !           285:     my $now = time;
        !           286:     my %classlist=&Apache::lonnet::dump('classlist',
        !           287: 					$ENV{'course.'.$ENV{'request.course.id'}.'.domain'},
        !           288: 					$ENV{'course.'.$ENV{'request.course.id'}.'.num'});
        !           289:     my (@holdsec,@sections,%allids,%stusec,%fullname);
        !           290:     foreach (keys(%classlist)) {
        !           291: 	my ($end,$start,$id,$section,$fullname)=split(/:/,$classlist{$_});
1.39      ng        292:        # still a student?
1.41    ! ng        293: 	if (($hideexpired) && ($end) && ($end < $now)) {
        !           294: 	    next;
        !           295: 	}
        !           296: 	$section = ($section ne '' ? $section : 'no');
        !           297: 	push @holdsec,$section;
        !           298: 	if ($getsec eq 'all' || $getsec eq $section) {
        !           299: 	    push (@{ $classlist{$getsec} }, $_);
        !           300: 	    $allids{$_}=$id;
        !           301: 	    $stusec{$_}=$section;
        !           302: 	    $fullname{$_}=$fullname;
        !           303: 	}
        !           304:     }
        !           305:     my %seen = ();
        !           306:     foreach my $item (@holdsec) {
        !           307: 	push (@sections, $item) unless $seen{$item}++;
        !           308:     }
        !           309:     return (\%classlist,\@sections,\%allids,\%stusec,\%fullname);
1.5       albertel  310: }
                    311: 
                    312: sub getpartlist {
1.41    ! ng        313:     my ($url) = @_;
        !           314:     my @parts =();
        !           315:     my (@metakeys) = split(/,/,&Apache::lonnet::metadata($url,'keys'));
        !           316:     foreach my $key (@metakeys) {
        !           317: 	if ( $key =~ m/stores_([0-9]+)_.*/) {
        !           318: 	    push(@parts,$key);
        !           319: 	}
1.13      albertel  320:     }
1.41    ! ng        321:     return @parts;
1.5       albertel  322: }
1.31      ng        323: 
                    324: #FIXME need to look at the metadata <stores> spec on what type of data to accept and provide an
1.6       albertel  325: #interface based on that, also do that to above function.
1.5       albertel  326: sub setstudentgrade {
1.41    ! ng        327:     my ($url,$symb,$courseid,$student,@parts) = @_;
        !           328:     my $result ='';
        !           329:     my ($stuname,$domain) = split(/:/,$student);
        !           330:     my %record=&Apache::lonnet::restore($symb,$courseid,$domain,$stuname);
        !           331:     my %newrecord;
        !           332: 
        !           333:     foreach my $part (@parts) {
        !           334: 	my ($temp,$part,$type)=split(/_/,$part);
        !           335: 	my $oldscore=$record{"resource.$part.$type"};
        !           336: 	my $newscore=$ENV{"form.GRADE.$student.$part.$type"};
        !           337: 	if ($type eq 'solved') {
        !           338: 	    my $update=0;
        !           339: 	    if ($newscore eq 'nothing' ) {
        !           340: 		if ($oldscore ne '') {
        !           341: 		    $update=1;
        !           342: 		    $newscore = '';
        !           343: 		}
        !           344: 	    } elsif ($oldscore !~ m/^$newscore/) {
        !           345: 		$update=1;
        !           346: 		$result.="Updating $stuname to $newscore<br />\n";
        !           347: 		if ($newscore eq 'correct')   { $newscore = 'correct_by_override'; }
        !           348: 		if ($newscore eq 'incorrect') { $newscore = 'incorrect_by_override'; }
        !           349: 		if ($newscore eq 'excused')   { $newscore = 'excused'; }
        !           350: 		if ($newscore eq 'ungraded')  { $newscore = 'ungraded_attempted'; }
        !           351: 	    } else {
        !           352: 		#$result.="$stuname:$part:$type:unchanged  $oldscore to $newscore:<br />\n";
        !           353: 	    }
        !           354: 	    if ($update) { $newrecord{"resource.$part.$type"}=$newscore; }
        !           355: 	} else {
        !           356: 	    if ($oldscore ne $newscore) {
        !           357: 		$newrecord{"resource.$part.$type"}=$newscore;
        !           358: 		$result.="Updating $student"."'s status for $part.$type to $newscore<br />\n";
        !           359: 	    } else {
        !           360: 		#$result.="$stuname:$part:$type:unchanged  $oldscore to $newscore:<br />\n";
        !           361: 	    }
        !           362: 	}
1.13      albertel  363:     }
1.41    ! ng        364:     if ( scalar(keys(%newrecord)) > 0 ) {
        !           365: 	$newrecord{'resource.regrader'}="$ENV{'user.name'}:$ENV{'user.domain'}";
        !           366: #       &Apache::lonnet::cstore(\%newrecord,$symb,$courseid,$domain,$stuname);
1.13      albertel  367: 
1.41    ! ng        368: 	$result.="Stored away ".scalar(keys(%newrecord))." elements.<br />\n";
        !           369:     }
        !           370:     return $result;
1.2       albertel  371: }
                    372: 
1.41    ! ng        373: sub print_hash {
        !           374:     my ($request, $hash) = @_;
        !           375:     $request->print('<table border=1><tr><td>Key</td><td>Value</td></tr>');
        !           376:     for (sort keys (%$hash)) {
        !           377: 	$request->print('<tr><td>'.$_.'</td><td>'.$$hash{$_}.'&nbsp;</td></tr>');
        !           378:     }
        !           379:     $request->print('</table>');
        !           380:     return '';
        !           381: }
1.33      ng        382: #
1.32      ng        383: # --------------------------- show submissions of a student, option to grade --------
1.2       albertel  384: sub submission {
1.41    ! ng        385:     my ($request,$counter,$total) = @_;
1.33      ng        386: 
1.41    ! ng        387:     (my $url=$ENV{'form.url'})=~s-^http://($ENV{'SERVER_NAME'}|$ENV{'HTTP_HOST'})--;
        !           388:     if ($ENV{'form.student'} eq '') { &moreinfo($request,'Need student login id'); return ''; }
        !           389:     my ($uname,$udom) = &finduser($ENV{'form.student'});
        !           390:     if ($uname eq '') { &moreinfo($request,'Unable to find student'); return ''; }
        !           391: 
        !           392:     my $symb=($ENV{'form.symb'} ne '' ? $ENV{'form.symb'} : (&Apache::lonnet::symbread($url)));
        !           393:     if ($symb eq '') { $request->print("Unable to handle ambiguous references:$url:."); return ''; }
        !           394:     my $last = ($ENV{'form.lastSub'} eq 'last' ? 'last' : '');
        !           395:     $ENV{'form.vProb'} = $ENV{'form.vProb'} ne '' ? $ENV{'form.vProb'} : 'yes';
        !           396:     my ($classlist,$seclist,$ids,$stusec,$fullname);
        !           397: 
        !           398:     # header info
        !           399:     if ($counter == 0) {
        !           400: 	&sub_page_js($request);
        !           401: 	$request->print('<h2>&nbsp;<font color="#339933">Submission Record</font></h2>'.
        !           402: 			'<font size=+1>&nbsp;<b>Resource: </b>'.$url.'</font>');
        !           403: 
        !           404: 	# option to display problem, only once else it cause problems with the form later 
        !           405: 	# since the problem has a form.
        !           406: 	if ($ENV{'form.vProb'} eq 'yes') {
        !           407: 	    my $rendered=&Apache::loncommon::get_student_view($symb,$uname,$udom,
        !           408: 							      $ENV{'request.course.id'});
        !           409: 	    my $companswer=&Apache::loncommon::get_student_answers($symb,$uname,$udom,
        !           410: 								   $ENV{'request.course.id'});
        !           411: 	    my $result.='<table border="0" width="100%"><tr><td bgcolor="#777777">';
        !           412: 	    $result.='<table border="0" width="100%"><tr><td bgcolor="#e6ffff">';
        !           413: 	    $result.='<b>Student\'s view of the problem</b></td></tr><tr><td bgcolor="#ffffff">'.$rendered.'<br />';
        !           414: 	    $result.='<b>Correct answer:</b><br />'.$companswer;
        !           415: 	    $result.='</td></tr></table>';
        !           416: 	    $result.='</td></tr></table><br />';
        !           417: 	    $request->print($result);
        !           418: 	}
        !           419: 	
        !           420: 	# kwclr is the only variable that is guaranteed to be non blank if this subroutine has been called once.
        !           421: 	my %keyhash = ();
        !           422: 	if ($ENV{'form.kwclr'} eq '') {
        !           423: 	    %keyhash = &Apache::lonnet::dump('nohist_handgrade',
        !           424: 					     $ENV{'course.'.$ENV{'request.course.id'}.'.domain'},
        !           425: 					     $ENV{'course.'.$ENV{'request.course.id'}.'.num'});
        !           426: 
        !           427: 	    my $loginuser = $ENV{'user.name'}.':'.$ENV{'user.domain'};
        !           428: 	    $ENV{'form.keywords'} = $keyhash{$symb.'_keywords'} ne '' ? $keyhash{$symb.'_keywords'} : '';
        !           429: 	    $ENV{'form.kwclr'}    = $keyhash{$loginuser.'_kwclr'} ne '' ? $keyhash{$loginuser.'_kwclr'} : 'red';
        !           430: 	    $ENV{'form.kwsize'}   = $keyhash{$loginuser.'_kwsize'} ne '' ? $keyhash{$loginuser.'_kwsize'} : '0';
        !           431: 	    $ENV{'form.kwstyle'}  = $keyhash{$loginuser.'_kwstyle'} ne '' ? $keyhash{$loginuser.'_kwstyle'} : '';
        !           432: 	    $ENV{'form.msgsub'}   = $keyhash{$symb.'_subject'} ne '' ? 
        !           433: 		$keyhash{$symb.'_subject'} : &Apache::lonnet::metadata($url,'title');
        !           434: 	    $ENV{'form.savemsgN'} = $keyhash{$symb.'_savemsgN'} ne '' ? $keyhash{$symb.'_savemsgN'} : '0';
1.38      ng        435: 
1.41    ! ng        436: 	}
        !           437: 	$request->print('<form action="/adm/grades" method="post" name="SCORE">'."\n".
        !           438: 			'<input type="hidden" name="command"    value="handgrade" />'."\n".
        !           439: 			'<input type="hidden" name="refresh"    value="off" />'."\n".
        !           440: 			'<input type="hidden" name="symb"       value="'.$symb.'" />'."\n".
        !           441: 			'<input type="hidden" name="url"        value="'.$url.'" />'."\n".
        !           442: 			'<input type="hidden" name="showgrading" value="'.$ENV{'form.showgrading'}.'" />'."\n".
        !           443: 			'<input type="hidden" name="vProb"      value="'.$ENV{'form.vProb'}.'" />'."\n".
        !           444: 			'<input type="hidden" name="lastSub"    value="'.$ENV{'form.lastSub'}.'" />'."\n".
        !           445: 			'<input type="hidden" name="section"    value="'.$ENV{'form.section'}.'">'."\n".
        !           446: 			'<input type="hidden" name="submitonly" value="'.$ENV{'form.submitonly'}.'">'."\n".
        !           447: 			'<input type="hidden" name="response"   value="'.$ENV{'form.response'}.'">'."\n".
        !           448: 			'<input type="hidden" name="handgrade"  value="'.$ENV{'form.handgrade'}.'">'."\n".
        !           449: 			'<input type="hidden" name="keywords"   value="'.$ENV{'form.keywords'}.'" />'."\n".
        !           450: 			'<input type="hidden" name="kwclr"      value="'.$ENV{'form.kwclr'}.'" />'."\n".
        !           451: 			'<input type="hidden" name="kwsize"     value="'.$ENV{'form.kwsize'}.'" />'."\n".
        !           452: 			'<input type="hidden" name="kwstyle"    value="'.$ENV{'form.kwstyle'}.'" />'."\n".
        !           453: 			'<input type="hidden" name="msgsub"     value="'.$ENV{'form.msgsub'}.'" />'."\n".
        !           454: 			'<input type="hidden" name="savemsgN"   value="'.$ENV{'form.savemsgN'}.'" />'."\n".
        !           455: 			'<input type="hidden" name="NCT"'.
        !           456: 			' value="'.($ENV{'form.NTSTU'} ne '' ? $ENV{'form.NTSTU'} : $total+1).'" />'."\n");
        !           457: 	
        !           458: 	my ($cts,$prnmsg) = (1,'');
        !           459: 	while ($cts <= $ENV{'form.savemsgN'}) {
        !           460: 	    $prnmsg.='<input type="hidden" name="savemsg'.$cts.'" value="'.
        !           461: 		($keyhash{$symb.'_savemsg'.$cts} eq '' ? $ENV{'form.savemsg'.$cts} : $keyhash{$symb.'_savemsg'.$cts}).
        !           462: 		'" />'."\n";
        !           463: 	    $cts++;
        !           464: 	}
        !           465: 	$request->print($prnmsg);
1.32      ng        466: 
1.41    ! ng        467: 	if ($ENV{'form.handgrade'} eq 'yes' && $ENV{'form.showgrading'} eq 'yes') {
        !           468: 	    $request->print(<<KEYWORDS);
1.38      ng        469: &nbsp;<b>Keyword Options:</b>&nbsp;
                    470: <a href="javascript:keywords(document.SCORE.keywords)"; TARGET=_self>List</a>&nbsp; &nbsp;
                    471: <a href="#" onMouseDown="javascript:getSel(); return false"
                    472:  CLASS="page">Paste Selection to List</a>&nbsp; &nbsp;
                    473: <a href="javascript:kwhighlight()"; TARGET=_self>Highlight Attribute</a><br /><br />
                    474: KEYWORDS
1.41    ! ng        475:         }
        !           476:     }
        !           477:     my %record = &Apache::lonnet::restore($symb,$ENV{'request.course.id'},$udom,$uname);
        !           478:     my ($partlist,$handgrade) = &response_type($url);
        !           479: #    &print_hash($request,\%record);
        !           480: 
        !           481:     # Student info
        !           482:     $request->print(($counter == 0 ? '' : '<br />'));
        !           483:     my $fullname = ($ENV{'form.fullname'} ne '' ? $ENV{'form.fullname'} : &get_fullname($uname,$udom));
        !           484:     my $result='<table border="0" width=100%><tr><td bgcolor="#777777">'.
        !           485: 	'<table border="0" width=100%><tr bgcolor="#ffffff"><td>';
        !           486: 
        !           487:     $result.='<table border="0"><tr bgcolor="#ffffff"><td><b>Fullname: </b>'.$fullname.
        !           488: 	'</td><td>&nbsp; &nbsp;<b>Username: </b>'.$uname.
        !           489: 	'</td><td>&nbsp; &nbsp;<b>Domain: </b>'.$udom.'</td></tr>';
        !           490:     if ($ENV{'form.handgrade'} eq 'yes') {
        !           491: 	my @col_list;
        !           492: 	($classlist,$seclist,$ids,$stusec,$fullname) = &getclasslist('all','0');
        !           493: 	for (keys (%$handgrade)) {
        !           494: 	    my $ncol = &Apache::lonnet::EXT('resource.'.$_.'.maxcollaborators',$symb,$udom,$uname);
        !           495: 	    if ($ncol > 0) {
        !           496: 		s/\_/\./g;
        !           497: 		if ($record{'resource.'.$_.'.collaborators'} ne '') {
        !           498: 		    my (@collaborators) = split(/,?\s+/,$record{'resource.'.$_.'.collaborators'});
        !           499: 		    my (@badcollaborators);
        !           500: 		    if (scalar(@collaborators) != 0) {
        !           501: 			$result.='<tr bgcolor="#ffffff"><td colspan=3><b>Collaborators: </b>';
        !           502: 			foreach my $collaborator (@collaborators) {
        !           503: 			    $collaborator = $collaborator =~ /\@|:/ ? 
        !           504: 				(split(/@|:/,$collaborator))[0] : $collaborator;
        !           505: 			    next if ($collaborator eq $uname);
        !           506: 			    if (!grep /^$collaborator:/i,keys %$classlist) {
        !           507: 				push @badcollaborators,$collaborator;
        !           508: 				next;
        !           509: 			    }
        !           510: 			    push @col_list, $collaborator;
        !           511: 			    $result.=$$fullname{$collaborator.':'.$udom}.' ('.$collaborator.') &nbsp; &nbsp;';
        !           512: 			}
        !           513: 			$result.='</td></tr>'."\n";
        !           514: 			$result.='<tr bgcolor="#ffbbbb"><td colspan=2>'.
        !           515: 			    'This student has submitted '.(scalar (@badcollaborators) > 1 ? '' : 'an').
        !           516: 			    ' invalid collaborator'.(scalar (@badcollaborators) > 1 ? 's. ' : '. ').
        !           517: 			    (join ', ',@badcollaborators).'</td></tr>' 
        !           518: 			    if (scalar(@badcollaborators) > 0);
        !           519: 
        !           520: 			$result.='<tr bgcolor="#ffbbbb"><td colspan=2>'.
        !           521: 			    'This student has submitted too many collaborators. Maximum is '.
        !           522: 			    $ncol.'.</td></tr>' if (scalar(@collaborators) > $ncol);
        !           523: 			$result.='<input type="hidden" name="collaborator'.$counter.
        !           524: 			    '" value="'.(join ':',@col_list).'" />'."\n";
        !           525: 		    }
        !           526: 		}
        !           527: 	    }
        !           528: 	}
        !           529:     }
        !           530:     $request->print($result.'</table>'."\n");
1.33      ng        531: 
1.41    ! ng        532:     # print student answer
        !           533:     if ($ENV{'form.lastSub'} =~ /^(lastonly|hdgrade)$/) {
        !           534: 	if ($ENV{'form.'.$uname.':'.$udom.':submitted_by'}) {
        !           535: 	    my $submitby='</td></tr><tr><td bgcolor="#e6ffff">'.
        !           536: 		'<b>Collaborative submission by: </b>'.
        !           537: 		'<a href="javascript:viewSubmitter(\''.$ENV{'form.'.$uname.':'.$udom.':submitted_by'}.
        !           538: 		'\')"; TARGET=_self>'.
        !           539: 		$$fullname{$ENV{'form.'.$uname.':'.$udom.':submitted_by'}}.'</a>';
        !           540: 	    $submitby.='</td></tr><tr><td bgcolor="#ffffff">'."\n";
        !           541: 	    $request->print($submitby);
        !           542: 	} else {
        !           543: 	    my ($string,$timestamp)=&get_last_submission ($symb,$uname,$udom,$ENV{'request.course.id'});
        !           544: 	    my $lastsubonly.='</td></tr><tr><td bgcolor="#e6ffff"><b>Last Submission Only</b>'.
        !           545: 		($$timestamp eq '' ? '' : '&nbsp; &nbsp; <b>Date Submitted:</b> '.$$timestamp).'</td></tr>';
        !           546: 	    if ($$timestamp eq '') {
        !           547: 		$lastsubonly.='<tr><td bgcolor="#ffffe6">'.$$string[0].'</td></tr>';
        !           548: 	    } else {
        !           549: 		for my $part (sort keys(%$handgrade)) {
        !           550: 		    foreach (@$string) {
        !           551: 			my ($partid,$respid) = /^resource\.(\d+)\.(\d+)\.submission/;
        !           552: 			if ($part eq ($partid.'_'.$respid)) {
        !           553: 			    my ($ressub,$subval) = split(/:/,$_,2);
        !           554: 			    $lastsubonly.='<tr><td bgcolor="#ffffe6"><b>Part ID</b> '.
        !           555: 				$partid.' <b>Response ID</b> '.$respid.
        !           556: 				' <b>Submission</b> '.&keywords_highlight($subval).'</td></tr>'
        !           557: 				if ($ENV{'form.lastSub'} eq 'lastonly' || 
        !           558: 				    ($ENV{'form.lastSub'} eq 'hdgrade' && $$handgrade{$part} =~ /:yes$/));
        !           559: 			}
        !           560: 		    }
        !           561: 		}
        !           562: 	    }
        !           563: 	    $lastsubonly.='</td></tr><tr><td bgcolor="#ffffff">'."\n";
        !           564: 	    $request->print($lastsubonly);
        !           565: 	}
        !           566:     } else {
        !           567: 	$request->print(&Apache::loncommon::get_previous_attempt($symb,$uname,$udom,
        !           568: 								 $ENV{'request.course.id'},$last,
        !           569: 								 '.submission','Apache::grades::keywords_highlight'));
        !           570:     }
        !           571:     
        !           572:     # view submission with no grading option
        !           573:     if ($ENV{'form.showgrading'} eq '') {
        !           574: 	$request->print('</td></tr></table></td></tr></table></form>');
        !           575: 	return;
        !           576:     }
1.33      ng        577: 
1.41    ! ng        578:     $result='<input type="hidden" name="newmsg'.$counter.'" value="" />'."\n".
        !           579: 	'<input type="hidden" name="includemsg'.$counter.'" value="" />'."\n".
        !           580: 	'<input type="hidden" name="unamedom'.$counter.'" value="'.$uname.':'.$udom.'" />'."\n";
        !           581:     $result.='&nbsp;<a href="javascript:msgCenter(document.SCORE,'.$counter.
        !           582: 	',\''.$fullname.'\')"; TARGET=_self>Compose Message</a><br />'."\n" if ($ENV{'form.handgrade'} eq 'yes');
        !           583:     $request->print($result);
        !           584: 
        !           585:     my %seen = ();
        !           586:     my @partlist;
        !           587:     for (sort keys(%$handgrade)) {
        !           588: 	my ($partid,$respid) = split(/_/);
        !           589: 	next if ($seen{$partid} > 0);
        !           590: 	$seen{$partid}++;
        !           591: 	next if ($$handgrade{$_} =~ /:no$/);
        !           592: 	push @partlist,$partid;
        !           593: 	my $wgt    = &Apache::lonnet::EXT('resource.'.$partid.'.weight',$symb,$udom,$uname);
        !           594: 	my $wgtmsg = ($wgt > 0 ? '(problem weight)' : '<font color="red">problem weight assigned by computer</font>');
        !           595: 	$wgt       = ($wgt > 0 ? $wgt : '1');
        !           596: 	my $score  = ($record{'resource.'.$partid.'.awarded'} eq '' ?
        !           597: 		      '' : $record{'resource.'.$partid.'.awarded'}*$wgt);
        !           598: 
        !           599: 	# display grading options
        !           600: 	$result='<input type="hidden" name="WGT'.$counter.'_'.$partid.'" value="'.$wgt.'" />';
        !           601: 	$result.='<table border="0"><tr><td><b>Part </b>'.$partid.' <b>Points</b></td><td>';
        !           602: 
        !           603: 	my $ctr = 0;
        !           604: 	$result.='<table border="0"><tr>';  # display radio buttons in a nice table 10 across
        !           605: 	while ($ctr<=$wgt) {
        !           606: 	    $result.= '<td><input type="radio" name="RADVAL'.$counter.'_'.$partid.'" '.
        !           607: 		'onclick="javascript:writeBox(this.form.GRADE_BOX'.$counter.'_'.$partid.
        !           608: 		',this.form.GRADE_SEL'.$counter.'_'.$partid.','.$ctr.
        !           609: 		',this.form.stores'.$counter.'_'.$partid.')" '.
        !           610: 		($score eq $ctr ? 'checked':'').' /> '.$ctr."</td>\n";
        !           611: 	    $result.=(($ctr+1)%10 == 0 ? '</tr><tr>' : '');
        !           612: 	    $ctr++;
        !           613: 	}
        !           614: 	$result.='</tr></table>';
1.39      ng        615: 
1.41    ! ng        616: 	$result.='</td><td>&nbsp;<b>or</b>&nbsp;</td>';
        !           617: 	$result.='<td><input type="text" name="GRADE_BOX'.$counter.'_'.$partid.'"'.
        !           618: 	    ($score ne ''? ' value = "'.$score.'"':'').' size="4" '.
        !           619: 	    'onChange="javascript:updateRadio(this.form.RADVAL'.$counter.'_'.$partid.
        !           620: 	    ',this.form.GRADE_BOX'.$counter.'_'.$partid.
        !           621: 	    ',this.form.GRADE_SEL'.$counter.'_'.$partid.
        !           622: 	    ',this.form.stores'.$counter.'_'.$partid.')" /></td>'."\n";
        !           623: 	$result.='<td>/'.$wgt.' '.$wgtmsg.' </td><td>';
        !           624: 
        !           625: 	$result.='<select name="GRADE_SEL'.$counter.'_'.$partid.'" '.
        !           626: 	    'onChange="javascript:clearRadBox(this.form.RADVAL'.$counter.'_'.$partid.
        !           627: 	    ',this.form.GRADE_BOX'.$counter.'_'.$partid.
        !           628: 	    ',this.form.GRADE_SEL'.$counter.'_'.$partid.
        !           629: 	    ',this.form.stores'.$counter.'_'.$partid.')" />'."\n".
        !           630: 	    '<option selected="on"> </option>'.
        !           631: 	    '<option>excused</option></select>'."&nbsp&nbsp\n";
        !           632: 	$result.='<input type="hidden" name="stores'.$counter.'_'.$partid.'" value="0" />';
        !           633: 	$result.='</td></tr></table>';
        !           634: 	$request->print($result);
        !           635:     }
        !           636:     $request->print('<input type="hidden" name="partlist'.$counter.'" value="'.(join ":",@partlist).'" />'."\n");
        !           637:     $request->print('</td></tr></table></td></tr></table>'."\n");
        !           638: 
        !           639:     # print end of form
        !           640:     if ($counter == $total) {
        !           641: 	my $endform.='<table border="0"><tr><td><input type="submit" name="gradeOpt" value="Save & Next" />';
        !           642: 	my $ntstu ='<select name="NTSTU">'.
        !           643: 	    '<option>1</option><option>2</option>'.
        !           644: 	    '<option>3</option><option>5</option>'.
        !           645: 	    '<option>7</option><option>10</option></select>'."\n";
        !           646: 	my $nsel = ($ENV{'form.NTSTU'} ne '' ? $ENV{'form.NTSTU'} : '1');
        !           647: 	$ntstu =~ s/<option>$nsel</<option selected="on">$nsel</;
        !           648: 	$endform.=$ntstu.'student(s) &nbsp;&nbsp;'.
        !           649: 	    '<input type="submit" name="gradeOpt" value="Next" />&nbsp'.
        !           650: 	    '<input type="submit" name="gradeOpt" value="Previous" />&nbsp'.
        !           651: 	    '(Next and Previous do not save the scores.)'.
        !           652: 	    '</td><tr></table></form>';
        !           653: 	$request->print($endform);
        !           654:     }
        !           655:     return '';
1.38      ng        656: }
                    657: 
                    658: sub get_last_submission {
1.41    ! ng        659:     my ($symb,$username,$domain,$course)=@_;
        !           660:     if ($symb) {
        !           661: 	my (@string,$timestamp);
        !           662: 	my (%returnhash)=&Apache::lonnet::restore($symb,$course,$domain,$username);
        !           663: 	if ($returnhash{'version'}) {
        !           664: 	    my %lasthash=();
        !           665: 	    my ($version);
        !           666: 	    for ($version=1;$version<=$returnhash{'version'};$version++) {
        !           667: 		foreach (sort(split(/\:/,$returnhash{$version.':keys'}))) {
        !           668: 		    $lasthash{$_}=$returnhash{$version.':'.$_};
        !           669: 		}
        !           670: 	    }
        !           671: 	    foreach ((keys %lasthash)) {
        !           672: 		if ($_ =~ /\.submission$/) {push @string, (join(':',$_,$lasthash{$_}))}
        !           673: 		if ($_ =~ /timestamp/) {$timestamp = scalar(localtime($lasthash{$_}))};
        !           674: 	    }
        !           675: 	}
        !           676: 	@string = $string[0] eq '' ? 'Nothing submitted - no attempts.' : @string;
        !           677: 	return \@string,\$timestamp;
        !           678:     }
1.38      ng        679: }
1.35      ng        680: 
1.38      ng        681: sub keywords_highlight {
1.41    ! ng        682:     my $string  = shift;
        !           683:     my $size    = $ENV{'form.kwsize'} eq '0' ? '' : 'size='.$ENV{'form.kwsize'};
        !           684:     my $styleon = $ENV{'form.kwstyle'} eq ''  ? '' : $ENV{'form.kwstyle'};
        !           685:     (my $styleoff = $styleon) =~ s/\</\<\//;
        !           686:     my @keylist = split(/[,\s+]/,$ENV{'form.keywords'});
        !           687:     foreach (@keylist) {
        !           688: 	$string =~ s/\b$_(\b|\.)/\<font color\=$ENV{'form.kwclr'} $size\>$styleon$_$styleoff\<\/font\>/gi;
        !           689:     }
        !           690:     return $string;
1.38      ng        691: }
1.36      ng        692: 
1.38      ng        693: sub processHandGrade {
1.41    ! ng        694:     my ($request) = shift;
        !           695:     my $url    = $ENV{'form.url'};
        !           696:     my $symb   = $ENV{'form.symb'};
        !           697:     my $button = $ENV{'form.gradeOpt'};
        !           698:     my $ngrade = $ENV{'form.NCT'};
        !           699:     my $ntstu  = $ENV{'form.NTSTU'};
        !           700: 
        !           701:     my $loginuser = $ENV{'user.name'}.':'.$ENV{'user.domain'};
        !           702:     my %keyhash = ();
        !           703:     $ENV{'form.keywords'}           =~ s/,\s{0,}|\s+/ /g;
        !           704:     $ENV{'form.keywords'}           =~ s/^\s+|\s+$//;
        !           705:     $keyhash{$symb.'_keywords'}     = $ENV{'form.keywords'};
        !           706:     $keyhash{$symb.'_subject'}      = $ENV{'form.msgsub'};
        !           707:     $keyhash{$loginuser.'_kwclr'}   = $ENV{'form.kwclr'};
        !           708:     $keyhash{$loginuser.'_kwsize'}  = $ENV{'form.kwsize'};
        !           709:     $keyhash{$loginuser.'_kwstyle'} = $ENV{'form.kwstyle'};
        !           710: 
        !           711:     my ($ctr,$idx) = (1,1);
        !           712:     while ($ctr <= $ENV{'form.savemsgN'}) {
        !           713: 	if ($ENV{'form.savemsg'.$ctr} ne '') {
        !           714: 	    $keyhash{$symb.'_savemsg'.$idx} = $ENV{'form.savemsg'.$ctr};
        !           715: 	    $idx++;
        !           716: 	}
        !           717: 	$ctr++;
        !           718:     }
        !           719:     $ctr = 0;
        !           720:     while ($ctr < $ngrade) {
        !           721: 	if ($ENV{'form.newmsg'.$ctr} ne '') {
        !           722: 	    $keyhash{$symb.'_savemsg'.$idx} = $ENV{'form.newmsg'.$ctr};
        !           723: 	    $ENV{'form.savemsg'.$idx} = $ENV{'form.newmsg'.$ctr};
        !           724: 	    $idx++;
        !           725: 	}
        !           726: 	$ctr++;
        !           727:     }
        !           728:     $ENV{'form.savemsgN'} = --$idx;
        !           729:     $keyhash{$symb.'_savemsgN'} = $ENV{'form.savemsgN'};
        !           730:     my $putresult = &Apache::lonnet::put
        !           731: 	('nohist_handgrade',\%keyhash,
        !           732: 	 $ENV{'course.'.$ENV{'request.course.id'}.'.domain'},
        !           733: 	 $ENV{'course.'.$ENV{'request.course.id'}.'.num'});
        !           734: 
        !           735:     if ($ENV{'form.refresh'} eq 'on') {
        !           736: 	my $ctr = 0;
        !           737: 	$ENV{'form.NTSTU'}=$ngrade;
        !           738: 	while ($ctr < $ngrade) {
        !           739: 	    ($ENV{'form.student'},my $udom) = split(/:/,$ENV{'form.unamedom'.$ctr});
        !           740: 	    &submission($request,$ctr,$ngrade-1);
        !           741: 	    $ctr++;
        !           742: 	}
        !           743: 	return '';
        !           744:     }
1.36      ng        745: 
1.41    ! ng        746:     if ($button eq 'Save & Next') {
        !           747: 	my $ctr = 0;
        !           748: 	while ($ctr < $ngrade) {
        !           749: 	    my ($uname,$udom) = split(/:/,$ENV{'form.unamedom'.$ctr});
        !           750: 	    my ($errorflg) = &saveHandGrade($request,$url,$symb,$uname,$udom,$ctr);
        !           751: 	    return '' if ($errorflg eq 'error');
        !           752: 
        !           753: 	    my $includemsg = $ENV{'form.includemsg'.$ctr};
        !           754: 	    my ($subject,$message,$msgstatus) = ('','','');
        !           755: 	    if ($includemsg =~ /savemsg|new$ctr/) {
        !           756: 		$subject = $ENV{'form.msgsub'} if ($includemsg =~ /^msgsub/);
        !           757: 		my (@msgnum) = split(/,/,$includemsg);
        !           758: 		foreach (@msgnum) {
        !           759: 		    $message.=$ENV{'form.'.$_} if ($_ =~ /savemsg|newmsg/ && $_ ne '');
        !           760: 		}
        !           761: 		$message =~ s/\s+/ /g;
        !           762: 		$msgstatus = &Apache::lonmsg::user_normal_msg ($uname,$udom,
        !           763: 							       $ENV{'form.msgsub'},$message);
        !           764: 	    }
        !           765: 	    if ($ENV{'form.collaborator'.$ctr}) {
        !           766: 		my (@collaborators) = split(/:/,$ENV{'form.collaborator'.$ctr});
        !           767: 		foreach (@collaborators) {
        !           768: 		    &saveHandGrade($request,$url,$symb,$_,$udom,$ctr,
        !           769: 				   $ENV{'form.unamedom'.$ctr});
        !           770: 		    if ($message ne '') {
        !           771: 			$msgstatus = &Apache::lonmsg::user_normal_msg ($_,$udom,
        !           772: 								       $ENV{'form.msgsub'},
        !           773: 								       $message);
        !           774: 		    }
        !           775: 		}
        !           776: 	    }
        !           777: 	    $ctr++;
        !           778: 	}
        !           779:     }
        !           780:     my $firststu = $ENV{'form.unamedom0'};
        !           781:     my $laststu = $ENV{'form.unamedom'.($ngrade-1)};
        !           782:     $ctr = 2;
        !           783:     while ($laststu eq '') {
        !           784: 	$laststu  = $ENV{'form.unamedom'.($ngrade-$ctr)};
        !           785: 	$ctr++;
        !           786: 	$laststu = $firststu if ($ctr > $ngrade);
        !           787:     }
        !           788:     my ($classlist,$seclist,$ids,$stusec,$fullname) = &getclasslist($ENV{'form.section'},'0');
        !           789: 
        !           790:     my (@parsedlist,@nextlist);
        !           791:     my ($nextflg) = 0;
        !           792:     foreach ( sort(@{ $$classlist{$ENV{'form.section'}} }) ) {
        !           793: 	if ($nextflg == 1 && $button =~ /Next$/) {
        !           794: 	    push @parsedlist,$_;
        !           795: 	}
        !           796: 	$nextflg = 1 if ($_ eq $laststu);
        !           797: 	if ($button eq 'Previous') {
        !           798: 	    last if ($_ eq $firststu);
        !           799: 	    push @parsedlist,$_;
        !           800: 	}
        !           801:     }
        !           802:     $ctr = 0;
        !           803:     my ($partlist,$handgrade) = &response_type($ENV{'form.url'});
        !           804:     @parsedlist = reverse @parsedlist if ($button eq 'Previous');
        !           805:     foreach my $student (@parsedlist) {
        !           806: 	my ($uname,$udom) = split(/:/,$student);
        !           807: 	if ($ENV{'form.submitonly'} eq 'yes') {
        !           808: 	    my (%status) = &student_gradeStatus($ENV{'form.url'},$udom,$uname,$partlist) ;
        !           809: 	    my $statusflg = '';
        !           810: 	    foreach (keys(%status)) {
        !           811: 		$statusflg = 1 if ($status{$_} ne 'nothing');
        !           812: 		my ($foo,$partid,$foo) = split(/\./,$_);
        !           813: 		$statusflg = '' if ($status{'resource.'.$partid.'.submitted_by'} ne '');
        !           814: 	    }
        !           815: 	    next if ($statusflg eq '');
        !           816: 	}
        !           817: 	push @nextlist,$student if ($ctr < $ntstu);
        !           818: 	$ctr++;
        !           819:     }
1.36      ng        820: 
1.41    ! ng        821:     $ctr = 0;
        !           822:     my $total = scalar(@nextlist)-1;
1.39      ng        823: 
1.41    ! ng        824:     foreach (sort @nextlist) {
        !           825: 	my ($uname,$udom,$submitter) = split(/:/);
        !           826: 	$ENV{'form.student'} = $uname;
        !           827: 	$ENV{'form.fullname'} = $$fullname{$_};
        !           828: #	$ENV{'form.'.$_.':submitted_by'} = $submitter;
        !           829: #	print "submitter=$ENV{'form.'.$_.':submitted_by'}= $submitter:<br>";
        !           830: 	&submission($request,$ctr,$total);
        !           831: 	$ctr++;
        !           832:     }
        !           833:     if ($total < 0) {
        !           834: 	my $the_end = '<h3><font color="red">LON-CAPA User Message</font></h3><br />'."\n";
        !           835: 	$the_end.='<b>Message: </b> No more students for this section or class.<br /><br />'."\n";
        !           836: 	$the_end.='Click on the button below to return to the grading menu.<br /><br />'."\n";
        !           837: 	$the_end.=&show_grading_menu_form ($symb,$url);
        !           838: 	$request->print($the_end);
        !           839:     }
        !           840:     return '';
1.38      ng        841: }
1.36      ng        842: 
1.38      ng        843: sub saveHandGrade {
1.41    ! ng        844:     my ($request,$url,$symb,$stuname,$domain,$newflg,$submitter) = @_;
        !           845: #   my %record=&Apache::lonnet::restore($symb,$ENV{'request.course.id'},$domain,$stuname);
        !           846:     my %newrecord;
        !           847:     foreach (split(/:/,$ENV{'form.partlist'.$newflg})) {
        !           848: 	if ($ENV{'form.GRADE_SEL'.$newflg.'_'.$_} eq 'excused') {
        !           849: 	    $newrecord{'resource.'.$_.'.solved'} = 'excused';
        !           850: 	} else {
        !           851: 	    my $pts    = ($ENV{'form.GRADE_BOX'.$newflg.'_'.$_} ne '' ? 
        !           852: 			  $ENV{'form.GRADE_BOX'.$newflg.'_'.$_} : $ENV{'form.RADVAL'.$newflg.'_'.$_});
        !           853: 	    if ($pts eq '') {
        !           854: 		&userError($request,'No point was assigned for part id '.$_.' and for username '.$stuname.'.');
        !           855: 		return 'error';
        !           856: 	    }
        !           857: 	    my $wgt    = $ENV{'form.WGT'.$newflg.'_'.$_} eq '' ? 1 : $ENV{'form.WGT'.$newflg.'_'.$_};
        !           858: 	    my $partial= $pts/$wgt;
        !           859: 	    $newrecord{'resource.'.$_.'.awarded'}  = $partial;
        !           860: 	    if ($partial == 0) {
        !           861: 		$newrecord{'resource.'.$_.'.solved'} = 'incorrect_by_override';
        !           862: 	    } else {
        !           863: 		$newrecord{'resource.'.$_.'.solved'} = 'correct_by_override';
        !           864: 	    }
        !           865: 	    $newrecord{'resource.'.$_.'.submitted_by'} = $submitter if ($submitter);
        !           866: 	}
        !           867:     }
        !           868:     
        !           869:     if ( scalar(keys(%newrecord)) > 0 ) {
        !           870: 	$newrecord{'resource.regrader'}="$ENV{'user.name'}:$ENV{'user.domain'}";
        !           871: #	&print_hash($request,\%newrecord);
        !           872: 	&Apache::lonnet::cstore(\%newrecord,$symb,$ENV{'request.course.id'},$domain,$stuname);
        !           873:     }
        !           874:     return '';
1.36      ng        875: }
1.38      ng        876: 
                    877: sub get_symb_and_url {
1.41    ! ng        878:     my ($request) = @_;
        !           879:     (my $url=$ENV{'form.url'}) =~ s-^http://($ENV{'SERVER_NAME'}|$ENV{'HTTP_HOST'})--;
        !           880:     my $symb=($ENV{'form.symb'} ne '' ? $ENV{'form.symb'} : (&Apache::lonnet::symbread($url)));
        !           881:     if ($symb eq '') { $request->print("Unable to handle ambiguous references:$url:."); return ''; }
        !           882:     return ($symb,$url);
1.36      ng        883: }
                    884: 
1.38      ng        885: sub show_grading_menu_form {
1.41    ! ng        886:     my ($symb,$url)=@_;
        !           887:     my $result.='<form action="/adm/grades" method="post">'."\n".
        !           888: 	'<input type="hidden" name="symb" value="'.$symb.'" />'."\n".
        !           889: 	'<input type="hidden" name="url" value="'.$url.'" />'."\n".
1.38      ng        890: 	'<input type="hidden" name="command" value="gradingmenu" />'."\n".
1.41    ! ng        891: 	'<input type="submit" name="submit" value="Grading Menu" />'."\n".
        !           892: 	'</form>'."\n";
        !           893:     return $result;
1.36      ng        894: }
                    895: 
1.38      ng        896: sub gradingmenu {
1.41    ! ng        897:     my ($request) = @_;
        !           898:     my ($symb,$url)=&get_symb_and_url($request);
        !           899:     if (!$symb) {return '';}
        !           900:     my $result='<h2>&nbsp;<font color="#339933">Select a Grading Method</font></h2>';
        !           901:     $result.='<table border="0">';
        !           902:     $result.='<tr><td colspan=3><font size=+1><b>Resource: </b>'.$url.'</font></td></tr>';
        !           903:     my ($partlist,$handgrade) = &response_type($url);
        !           904:     my ($resptype,$hdgrade)=('','no');
        !           905:     for (sort keys(%$handgrade)) {
        !           906: 	my ($responsetype,$handgrade)=split(/:/,$$handgrade{$_});
        !           907: 	$resptype = $responsetype;
        !           908: 	$hdgrade = $handgrade if ($handgrade eq 'yes');
        !           909: 	$result.='<tr><td><b>Part id: </b>'.$_.'</td>'.
        !           910: 	    '<td><b>Type: </b>'.$responsetype.'</td>'.
        !           911: 	    '<td><b>Handgrade: </b>'.$handgrade.'</font></td></tr>';
        !           912:     }
        !           913:     $result.='</table>';
        !           914:     $result.=&view_edit_entire_class_form($symb,$url).'<br />';
        !           915:     $result.=&upcsvScores_form($symb,$url).'<br />';
        !           916:     $result.=&viewGradeaStu_form($symb,$url,$resptype,$hdgrade).'<br />';
        !           917:     $result.=&verifyReceipt_form($symb,$url).'<br />';
        !           918:     $result.=&view_classlist_form($symb,$url);
1.39      ng        919: 
1.41    ! ng        920:     return $result;
1.39      ng        921: }
                    922: 
                    923: sub view_classlist_form {
1.41    ! ng        924:     my ($symb,$url)=@_;
        !           925:     my $result.='<table width=100% border=0><tr><td bgcolor=#777777>'."\n";
        !           926:     $result.='<table width=100% border=0><tr bgcolor="#e6ffff"><td>'."\n";
        !           927:     $result.='&nbsp;<b>View Class List</b></td></tr>'."\n";
        !           928:     $result.='<tr bgcolor=#ffffe6><td>'."\n";
        !           929:     $result.='<form action="/adm/grades" method="post">'."\n".
        !           930: 	'<input type="hidden" name="symb" value="'.$symb.'" />'."\n".
        !           931: 	'<input type="hidden" name="url" value="'.$url.'" />'."\n".
        !           932: 	'<input type="hidden" name="command" value="viewclasslist" />'."\n";
        !           933:     $result.='&nbsp;<input type="submit" name="submit" value="View Class" /></form>'."\n";
        !           934:     $result.='</td></tr></table>'."\n";
        !           935:     $result.='</td></tr></table>'."\n";
        !           936:     return $result;
1.36      ng        937: }
                    938: 
1.39      ng        939: sub viewclasslist {
1.41    ! ng        940:     my ($request) = shift;
        !           941:     my ($coursedomain,$coursenum) = split(/_/,$ENV{'request.course.id'});
        !           942:     my %classlist=&Apache::lonnet::dump('classlist',$coursedomain,$coursenum);
        !           943:     $request->print('<table border=1>');
        !           944:     foreach (sort keys(%classlist)) {
1.39      ng        945: #    my ($unam,$udom) = split(/:/,$_,2);
                    946: #    my $section = &Apache::lonnet::usection($udom,$unam,$ENV{'request.course.id'});
                    947: #    my $fullname = &get_fullname ($unam,$udom);
                    948: #    my @uname;
                    949: #    $uname[0]=$unam;
                    950: #    my %userid=&Apache::lonnet::idrget($udom,@uname);
                    951: #    my $value=$classlist{$_}.':'.$userid{$unam}.':'.$section.':'.$fullname;
                    952: #    $classlist{$_}=$value;
1.41    ! ng        953: 	$request->print('<tr><td>'.$_.' </td><td><pre> '.$classlist{$_}.'</pre></td></tr>');
        !           954:     }
        !           955:     $request->print('</table>');
1.39      ng        956: #  my $putresult = &Apache::lonnet::put
                    957: #      ('classlist',\%classlist,
                    958: #       $ENV{'course.'.$ENV{'request.course.id'}.'.domain'},
                    959: #       $ENV{'course.'.$ENV{'request.course.id'}.'.num'});
                    960: 
1.41    ! ng        961:     return '';
1.39      ng        962: }
                    963: 
1.38      ng        964: sub view_edit_entire_class_form {
1.41    ! ng        965:     my ($symb,$url)=@_;
        !           966:     my ($classlist,$sections) = &getclasslist('all','0');
        !           967:     my $result.='<table width=100% border=0><tr><td bgcolor=#777777>'."\n";
        !           968:     $result.='<table width=100% border=0><tr bgcolor="#e6ffff"><td>'."\n";
        !           969:     $result.='&nbsp;<b>View/Grade Entire Section/Class</b></td></tr>'."\n";
        !           970:     $result.='<tr bgcolor=#ffffe6><td>'."\n";
        !           971:     $result.='<form action="/adm/grades" method="post">'."\n".
        !           972: 	'<input type="hidden" name="symb" value="'.$symb.'" />'."\n".
        !           973: 	'<input type="hidden" name="url" value="'.$url.'" />'."\n".
        !           974: 	'<input type="hidden" name="command" value="viewgrades" />'."\n";
        !           975:     $result.='&nbsp;<b>Select section:</b> <select name="section">'."\n";
        !           976:     foreach (sort (@$sections)) {
        !           977: 	$result.= '<option>'.$_.'</option>'."\n";
        !           978:     }
        !           979:     $result.= '<option selected="on">all</select>'."<br />\n";
        !           980: #    $result.='&nbsp;<b>Display students who has: </b>'.
        !           981: #	'<input type="radio" name="submitonly" value="yes" checked> submitted'.
        !           982: #	'<input type="radio" name="submitonly" value="all"> everybody <br /><br />';
        !           983:     $result.='&nbsp;<input type="submit" name="submit" value="View/Grade" /></form>'."\n";
        !           984:     $result.='</td></tr></table>'."\n";
        !           985:     $result.='</td></tr></table>'."\n";
        !           986:     return $result;
1.36      ng        987: }
                    988: 
1.38      ng        989: sub upcsvScores_form {
1.41    ! ng        990:     my ($symb,$url) = @_;
        !           991:     if (!$symb) {return '';}
        !           992:     my $result.='<table width=100% border=0><tr><td bgcolor=#777777>'."\n";
        !           993:     $result.='<table width=100% border=0><tr bgcolor="#e6ffff"><td>'."\n";
        !           994:     $result.='&nbsp;<b>Specify a file containing the class scores for above resource</b></td></tr>'."\n";
        !           995:     $result.='<tr bgcolor=#ffffe6><td>'."\n";
        !           996:     my $upfile_select=&Apache::loncommon::upfile_select_html();
1.38      ng        997:   $result.=<<ENDUPFORM;
                    998: <form method="post" enctype="multipart/form-data" action="/adm/grades" name="gradesupload">
                    999: <input type="hidden" name="symb" value="$symb" />
                   1000: <input type="hidden" name="url" value="$url" />
                   1001: <input type="hidden" name="command" value="csvuploadmap" />
                   1002: $upfile_select
                   1003: <br />&nbsp;<input type="submit" name="submit" value="Upload Grades" />
                   1004: </form>
                   1005: ENDUPFORM
1.41    ! ng       1006:     $result.='</td></tr></table>'."\n";
        !          1007:     $result.='</td></tr></table>'."\n";
        !          1008:     return $result;
1.38      ng       1009: }
                   1010: 
                   1011: sub viewGradeaStu_form {
1.41    ! ng       1012:     my ($symb,$url,$response,$handgrade) = @_;
        !          1013:     my ($classlist,$sections) = &getclasslist('all','0');
        !          1014:     my $result.='<table width=100% border=0><tr><td bgcolor=#777777>'."\n";
        !          1015:     $result.='<table width=100% border=0><tr bgcolor="#e6ffff"><td>'."\n";
        !          1016:     $result.='&nbsp;<b>View/Grade an Individual Student\'s Submission</b></td></tr>'."\n";
        !          1017:     $result.='<tr bgcolor=#ffffe6><td>'."\n";
        !          1018:     $result.='<form action="/adm/grades" method="post">'."\n".
        !          1019: 	'<input type="hidden" name="symb" value="'.$symb.'" />'."\n".
        !          1020: 	'<input type="hidden" name="url" value="'.$url.'" />'."\n".
        !          1021: 	'<input type="hidden" name="response" value="'.$response.'" />'."\n".
        !          1022: 	'<input type="hidden" name="handgrade" value="'.$handgrade.'" />'."\n".
        !          1023: 	'<input type="hidden" name="showgrading" value="yes" />'."\n".
        !          1024: 	'<input type="hidden" name="command" value="submission" />'."\n";
        !          1025: 
        !          1026:     $result.='&nbsp;<b>Select section:</b> <select name="section">'."\n";
        !          1027:     foreach (sort (@$sections)) {
        !          1028: 	$result.= '<option>'.$_.'</option>'."\n";
        !          1029:     }
        !          1030:     $result.= '<option selected="on">all</select>'."\n";
        !          1031:     $result.='&nbsp;&nbsp;<b>Display students who has: </b>'.
        !          1032: 	'<input type="radio" name="submitonly" value="yes" checked> submitted'.
        !          1033: 	'<input type="radio" name="submitonly" value="all"> everybody <br />';
        !          1034:     $result.='&nbsp;(Section "no" implies the students were not assigned a section.)<br />' 
        !          1035: 	if (grep /no/,@$sections);
        !          1036:     
        !          1037:     $result.='<br />&nbsp;<input type="submit" name="submit" value="View/Grade" />'."\n".
        !          1038: 	'</form>'."\n";
        !          1039:     $result.='</td></tr></table>'."\n";
        !          1040:     $result.='</td></tr></table>'."\n";
        !          1041:     return $result;
1.36      ng       1042: }
                   1043: 
1.38      ng       1044: sub verifyReceipt_form {
1.41    ! ng       1045:     my ($symb,$url) = @_;
        !          1046:     my $cdom=$ENV{"course.$ENV{'request.course.id'}.domain"};
        !          1047:     my $cnum=$ENV{"course.$ENV{'request.course.id'}.num"};
        !          1048:     my $hostver=unpack("%32C*",$Apache::lonnet::perlvar{'lonHostID'});
        !          1049: 
        !          1050:     my $result.='<table width=100% border=0><tr><td bgcolor=#777777>'."\n";
        !          1051:     $result.='<table width=100% border=0><tr><td bgcolor=#e6ffff>'."\n";
        !          1052:     $result.='&nbsp;<b>Verify a Submission Receipt Issued by this Server</td></tr>'."\n";
        !          1053:     $result.='<tr bgcolor=#ffffe6><td>'."\n";
        !          1054:     $result.='<form action="/adm/grades" method="post">'."\n";
        !          1055:     $result.='&nbsp;<tt>'.$hostver.'-<input type="text" name="receipt" size="4"></tt><br />'."\n";
        !          1056:     $result.='&nbsp;<input type="submit" name="submit" value="Verify Receipt">'."\n";
        !          1057:     $result.='<input type="hidden" name="command" value="verify">'."\n";
        !          1058:     if ($ENV{'form.url'}) {
        !          1059: 	$result.='<input type="hidden" name="url" value="'.$ENV{'form.url'}.'" />';
        !          1060:     }
        !          1061:     if ($ENV{'form.symb'}) {
        !          1062: 	$result.='<input type="hidden" name="symb" value="'.$ENV{'form.symb'}.'" />';
        !          1063:     }
        !          1064:     $result.='</form>';
        !          1065:     $result.='</td></tr></table>'."\n";
        !          1066:     $result.='</td></tr></table>'."\n";
        !          1067:     return $result;
1.36      ng       1068: }
                   1069: 
1.38      ng       1070: sub viewgrades {
1.41    ! ng       1071:     my ($request) = @_;
        !          1072:     $request->print(<<VIEWJAVASCRIPT);
        !          1073: <script type="text/javascript" language="javascript">
        !          1074:     function viewOneStudent(user) {
        !          1075: 	document.onestudent.student.value = user;
        !          1076: 	document.onestudent.submit();
        !          1077:     }
        !          1078: 
        !          1079: 
        !          1080:     function writePoint(partid,weight,point) {
        !          1081: 	for (i=0;i<document.classgrade.total.value;i++) {
        !          1082: 	    var user = eval("document.classgrade.counter"+i+".value");
        !          1083: 	    var scorename = eval("document.classgrade.GRADE_"+user+"_"+partid+"_awarded");
        !          1084: 	    scorename.value = point;
        !          1085: 	}
        !          1086: }
        !          1087: </script>
        !          1088: VIEWJAVASCRIPT
        !          1089: 
        !          1090:     my ($symb,$url) = ($ENV{'form.symb'},$ENV{'form.url'}); 
        !          1091:     $request->print ('<h2><font color="#339933">Manual Grading</font></h2>');
1.38      ng       1092: 
1.41    ! ng       1093:     my $result='<table border="0">';
        !          1094:     $result.='<tr><td colspan=3><font size=+1><b>Resource: </b>'.$ENV{'form.url'}.
        !          1095: 	'</font></td></tr>'."\n";
        !          1096:     my ($partlist,$handgrade) = &response_type($ENV{'form.url'});
        !          1097:     my %weight = ();
        !          1098:     for (sort keys(%$handgrade)) {
        !          1099: 	my ($responsetype,$handgrade)=split(/:/,$$handgrade{$_});
        !          1100: 	my ($partid,$respid) = split (/_/);
        !          1101: 	my $wgt = &Apache::lonnet::EXT('resource.'.$partid.'.weight',$symb);
        !          1102: 	$weight{$partid} = $wgt eq '' ? '1' : $wgt;
        !          1103: 	$result.='<tr><td><b>Part id: </b>'.$partid.'</td>'.
        !          1104: 	    '<td><b>Type: </b>'.$responsetype.'</td>'.
        !          1105: 	    '<td><b>Handgrade: </b>'.$handgrade.'</font></td></tr>'."\n";
        !          1106:     }
        !          1107:     $request->print($result.'</table>'."\n");
        !          1108: 
        !          1109:     #view individual student submission form - called using Javascript viewOneStudent
        !          1110:     $result = '<form action="/adm/grades" method="post" name="onestudent">'."\n".
        !          1111: 	'<input type="hidden" name="symb"    value="'.$symb.'" />'."\n".
        !          1112: 	'<input type="hidden" name="url"     value="'.$url.'" />'."\n".
        !          1113: 	'<input type="hidden" name="command" value="submission" />'."\n".
        !          1114: 	'<input type="hidden" name="student" value="" />'."\n".
        !          1115: 	'</form>'."\n";
        !          1116: 
        !          1117:     #start the form
        !          1118:     $result.= '<form action="/adm/grades" method="post" name="classgrade">'."\n".
        !          1119: 	'<input type="hidden" name="symb"    value="'.$symb.'" />'."\n".
        !          1120: 	'<input type="hidden" name="url"     value="'.$url.'" />'."\n".
1.38      ng       1121: 	'<input type="hidden" name="command" value="editgrades" />'."\n".
1.41    ! ng       1122: 	'<input type="hidden" name="section" value="'.$ENV{'form.section'}.'" />'."\n";
        !          1123:     $result.='To assign the same score for all the students use the radio buttons or box below. '.
        !          1124: 	'To assign individual score fill in the score for each student in the table below.<br />';
        !          1125:     $result.='<table border="0">';
        !          1126:     for (sort keys (%weight)) {
        !          1127: 	my $ctr = 0;
        !          1128: 	$result.='<tr><td><b>Part</b> '.$_.'</td><td>';
        !          1129: 	$result.='<table border="0"><tr>';  # display radio buttons in a nice table 10 across
        !          1130: 	while ($ctr<=$weight{$_}) {
        !          1131: 	    $result.= '<td><input type="radio" name="RADVAL_'.$_.'" '.
        !          1132: 		'onclick="javascript:writePoint('.$_.','.$weight{$_}.
        !          1133: 		','.$ctr.')" />'.$ctr."</td>\n";
        !          1134: 	    $result.=(($ctr+1)%10 == 0 ? '</tr><tr>' : '');
        !          1135: 	    $ctr++;
        !          1136: 	}
        !          1137: 	$result.='</tr></table>';
        !          1138:     }
        !          1139:     $result.='</tr></table>';
        !          1140: 
        !          1141:     $result.= '<input type="submit" name="submit"  value="Submit Changes" />'."\n".
        !          1142: 	'<table border=0><tr><td bgcolor="#777777">'."\n".
        !          1143: 	'<table border=0><tr bgcolor="#deffff">'.
        !          1144: 	'<td><b>Username</b></td><td><b>Fullname</b></td><td><b>Domain</b></td>'."\n";
        !          1145:     #get list of parts for this problem
        !          1146:     my (@parts) = sort(&getpartlist($url));
        !          1147:     foreach my $part (@parts) {
        !          1148: 	my $display=&Apache::lonnet::metadata($url,$part.'.display');
        !          1149: 	next if ($display =~ /^Number of Attempts/);
        !          1150: 	if  (!$display) { $display = &Apache::lonnet::metadata($url,$part.'.name'); }
        !          1151: 	if ($display =~ /^Partial Credit Factor/) {
        !          1152: 	    $_ = $display;
        !          1153: 	    my ($partid) = /.*?(\d+).*/;
        !          1154: 	    $result.='<td><b>Score Part '.$partid.'<br>(weight = '.$weight{$partid}.')</b></td>'."\n";
        !          1155: 	    next;
        !          1156: 	}
        !          1157: 	$display =~ s/Problem Status/Grade Status/;
        !          1158: 	$result.='<td><b>'.$display.'</b></td>'."\n";
        !          1159:     }
        !          1160:     $result.='</tr>';
        !          1161:     #get info for each student
        !          1162:     my ($classlist,$seclist,$ids,$stusec,$fullname) = &getclasslist($ENV{'form.section'},'0');
        !          1163:     my $ctr = 0;
        !          1164:     foreach ( sort(@{ $$classlist{$ENV{'form.section'}} }) ) {
        !          1165: 	(my $username = $_) = split(/:/);
        !          1166: 	$result.='<input type="hidden" name="counter'.$ctr.'" value="'.$username.'" />'."\n";
        !          1167: 	$result.=&viewstudentgrade($url,$symb,$ENV{'request.course.id'},
        !          1168: 				   $_,$$fullname{$_},\@parts,\%weight);
        !          1169: 	$ctr++;
        !          1170:     }
        !          1171:     $result.='</table></td></tr></table>';
        !          1172:     $result.='<input type="hidden" name="total" value="'.$ctr.'" />'."\n";
        !          1173:     $result.='<input type="submit" name="submit" value="Submit Changes" /></form>';
        !          1174:     $result.=&show_grading_menu_form($symb,$url);
        !          1175:     return $result;
        !          1176: }
        !          1177: 
        !          1178: sub viewstudentgrade {
        !          1179:     my ($url,$symb,$courseid,$student,$fullname,$parts,$weight) = @_;
        !          1180:     my ($username,$domain) = split(/:/,$student);
        !          1181:     my %record=&Apache::lonnet::restore($symb,$courseid,$domain,$username);
        !          1182:     my $result='<tr bgcolor="#ffffdd"><td>'.
        !          1183: 	'<a href="javascript:viewOneStudent(\''.$username.'\')"; TARGET=_self>'.$username.'</a>'.
        !          1184: 	'</td><td>'.$fullname.'</td><td align="middle">'.$domain.'</td>'."\n";
        !          1185:     foreach my $part (@$parts) {
        !          1186: 	my ($temp,$part,$type)=split(/_/,$part);
        !          1187: 	my $score=$record{"resource.$part.$type"};
        !          1188: 	next if $type eq 'tries';
        !          1189: 	if ($type eq 'awarded') {
        !          1190: 	    my $pts = $score*$$weight{$part};
        !          1191: 	    $result.='<td align="middle"><input type="text" name="GRADE_'.$username.'_'.$part.'_'.$type.
        !          1192: 		'" value="'.$pts.'" size="4" /></td>'."\n";
        !          1193: #	    $result.='<td align="middle"><input type="text" name="GRADE.'.$student.'.'.$part.'.'.$type.
        !          1194: #		'" value="'.$pts.'" size="4" /></td>'."\n";
        !          1195: #	} elsif ($type eq 'tries') {
        !          1196: #	    $result.='<td align="middle">'.$score.'&nbsp;</td>'."\n";
        !          1197: 	} elsif ($type eq 'solved') {
        !          1198: 	    my ($status,$foo)=split(/_/,$score,2);
        !          1199: 	    $result.="<td align=\"middle\"><select name=\"GRADE.$student.$part.$type\">\n";
        !          1200: 	    my $optsel = '<option>correct</option><option>incorrect</option><option>excused</option>'.
        !          1201: 		'<option>ungraded</option><option>nothing</option>'."\n";
        !          1202: 	    $status = 'nothing' if ($status eq '');
        !          1203: 	    $optsel =~ s/<option>$status/<option selected="on">$status/;
        !          1204: 	    $result.=$optsel;
        !          1205: 	    $result.="</select></td>\n";
        !          1206: 	}
        !          1207:     }
        !          1208:     $result.='</tr>';
        !          1209:     return $result;
1.38      ng       1210: }
                   1211: 
1.41    ! ng       1212: 
1.38      ng       1213: sub editgrades {
1.41    ! ng       1214:     my ($request) = @_;
        !          1215: 
        !          1216:     my $symb=$ENV{'form.symb'};
        !          1217:     if ($symb eq '') { $request->print("Unable to handle ambiguous references:$symb:$ENV{'form.url'}"); return ''; }
        !          1218:     my $url=$ENV{'form.url'};
1.34      ng       1219: 
1.41    ! ng       1220:     my $result.='<form action="/adm/grades" method="post">'."\n".
        !          1221: 	'<input type="hidden" name="symb" value="'.$symb.'" />'."\n".
        !          1222: 	'<input type="hidden" name="url" value="'.$url.'" />'."\n".
1.38      ng       1223: 	'<input type="hidden" name="command" value="viewgrades" />'."\n".
1.41    ! ng       1224: 	'<input type="submit" name="submit" value="See Grades" /> <br />'."\n";
        !          1225:     
        !          1226:     my (@parts) = &getpartlist($url);
        !          1227:     my ($classlist) = &getclasslist($ENV{'form.section'},'0');
        !          1228:     foreach my $student ( sort(@{ $$classlist{$ENV{'form.section'}} }) ) {
        !          1229: 	$result.=&setstudentgrade($url,$symb,$ENV{'request.course.id'},$student,@parts);
        !          1230:     }
1.35      ng       1231: 
1.41    ! ng       1232:     $result.='<input type="submit" name="submit" value="See Grades" /></table></form>';
        !          1233:     return $result;
1.38      ng       1234: }
                   1235: 
                   1236: sub sub_page_js {
1.41    ! ng       1237:     my $request = shift;
        !          1238:     $request->print(<<SUBJAVASCRIPT);
1.38      ng       1239: <script type="text/javascript" language="javascript">
1.39      ng       1240:   function updateRadio(radioButton,formtextbox,formsel,scores) {
1.38      ng       1241:      var pts = formtextbox.value;
                   1242:      var resetbox =false;
                   1243:      if (isNaN(pts) || pts < 0) {
                   1244: 	alert("A number equal or greater than 0 is expected. Entered value = "+pts);
                   1245: 	for (var i=0; i<radioButton.length; i++) {
                   1246: 	   if (radioButton[i].checked) {
                   1247: 	      formtextbox.value = i;
                   1248: 	      resetbox = true;
                   1249: 	   }
                   1250: 	}
                   1251: 	if (!resetbox) {
                   1252: 	   formtextbox.value = "";
                   1253: 	}
                   1254: 	return;
                   1255:     }
1.37      ng       1256: 
1.38      ng       1257:     for (var i=0; i<radioButton.length; i++) {
                   1258: 	radioButton[i].checked=false;
                   1259: 	if (pts == i) {
                   1260: 	   radioButton[i].checked=true;
                   1261: 	}
                   1262:     }
1.39      ng       1263:     updateSelect(formsel);
                   1264:     scores.value = "0";
1.37      ng       1265:   }
                   1266: 
1.39      ng       1267:   function writeBox(formrad,formsel,pts,scores) {
1.38      ng       1268:     formrad.value = pts;
1.39      ng       1269:     scores.value = "0";
                   1270:     updateSelect(formsel,pts);
1.38      ng       1271:     return;
1.31      ng       1272:   }
1.34      ng       1273: 
1.39      ng       1274:   function clearRadBox(radioButton,formbox,formsel,scores) {
                   1275:     for (var i=0; i<formsel.length; i++) {
                   1276: 	if (formsel[i].selected) {
                   1277: 	    var selectx=i;
                   1278: 	}
1.38      ng       1279:     }
1.39      ng       1280:     if (selectx == scores.value) { return };
                   1281:     formbox.value = "";
                   1282:     for (var i=0; i<radioButton.length; i++) {
                   1283: 	radioButton[i].checked=false;
1.33      ng       1284:     }
1.39      ng       1285:     scores.value = selectx;
                   1286:   }
                   1287: 
                   1288:   function updateSelect(formsel) {
                   1289:     formsel[0].selected = true;
1.38      ng       1290:     return;
1.33      ng       1291:   }
1.37      ng       1292: 
1.39      ng       1293: //===================== Show list of keywords ====================
1.38      ng       1294:   function keywords(keyform) {
                   1295:     var keywds = keyform.value;
                   1296:     var nret = prompt("Keywords list, separated by a space. Add/delete to list if desired.",keywds);
                   1297:     if (nret==null) return;
                   1298:     keyform.value = nret;
                   1299:     return;
1.34      ng       1300:   }
1.30      ng       1301: 
1.41    ! ng       1302: //===================== Script to view submitted by ==================
        !          1303:   function viewSubmitter(submitter) {
        !          1304:     document.SCORE.refresh.value = "on";
        !          1305:     document.SCORE.NCT.value = "1";
        !          1306:     document.SCORE.unamedom0.value = submitter;
        !          1307:     document.SCORE.submit();
        !          1308:     return;
        !          1309:   }
        !          1310: 
1.38      ng       1311: //===================== Script to add keyword(s) ==================
                   1312:   function getSel() {
                   1313:     if (document.getSelection) txt = document.getSelection();
                   1314:     else if (document.selection) txt = document.selection.createRange().text;
                   1315:     else return;
                   1316:     var cleantxt = txt.replace(new RegExp('([\\f\\n\\r\\t\\v ])+', 'g')," ");
                   1317:     if (cleantxt=="") {
                   1318: 	alert("Select a word or group of words from document and then click this link.");
                   1319: 	return;
1.35      ng       1320:     }
1.38      ng       1321:     var nret = prompt("Add selection to keyword list? Edit if desired.",cleantxt);
                   1322:     if (nret==null) return;
                   1323:     var curlist = document.SCORE.keywords.value;
                   1324:     document.SCORE.keywords.value = curlist+" "+nret;
                   1325:     return;
1.35      ng       1326:   }
                   1327: 
1.38      ng       1328: //====================== Script for composing message ==============
                   1329:   function msgCenter(msgform,usrctr,fullname) {
                   1330:     var Nmsg  = msgform.savemsgN.value;
                   1331:     savedMsgHeader(Nmsg,usrctr,fullname);
                   1332:     var subject = msgform.msgsub.value;
                   1333:     var rtrchk  = eval("document.SCORE.includemsg"+usrctr);
                   1334:     var msgchk = rtrchk.value;
                   1335: //    alert("checked=>"+msgchk);
                   1336:     re = /msgsub/;
                   1337:     var shwsel = "";
                   1338:     if (re.test(msgchk)) { shwsel = "checked" }
                   1339:     displaySubject(subject,shwsel);
                   1340:     for (var i=1; i<=Nmsg; i++) {
                   1341: 	var testpt = "savemsg"+i+",";
                   1342: 	re = /testpt/;
                   1343: 	shwsel = "";
                   1344: 	if (re.test(msgchk)) { shwsel = "checked" }
                   1345: 	var message = eval("document.SCORE.savemsg"+i+".value");
                   1346: 	displaySavedMsg(i,message,shwsel);
                   1347:     }
                   1348:     newmsg = eval("document.SCORE.newmsg"+usrctr+".value");
                   1349:     shwsel = "";
                   1350:     re = /newmsg/;
                   1351:     if (re.test(msgchk)) { shwsel = "checked" }
                   1352:     newMsg(newmsg,shwsel);
                   1353:     msgTail(); 
                   1354:     return;
1.35      ng       1355:   }
                   1356: 
1.38      ng       1357:   function savedMsgHeader(Nmsg,usrctr,fullname) {
                   1358:     var height = 30*Nmsg+250;
                   1359:     var scrollbar = "no";
                   1360:     if (height > 600) {
                   1361: 	height = 600;
                   1362: 	scrollbar = "yes";
                   1363:     }
                   1364: /*    if (window.pWin)
                   1365: 	window.pWin.close(); */
                   1366:     pWin = window.open('', 'MessageCenter', 'toolbar=no,location=no,scrollbars='+scrollbar+',screenx=70,screeny=75,width=600,height='+height);
                   1367:     pWin.document.write("<html><head>");
                   1368:     pWin.document.write("<title>Message Central</title>");
1.37      ng       1369: 
1.38      ng       1370:     pWin.document.write("<script language=javascript>");
                   1371:     pWin.document.write("function checkInput() {");
                   1372:     pWin.document.write("  opener.document.SCORE.msgsub.value = document.msgcenter.msgsub.value;");
                   1373:     pWin.document.write("  var nmsg   = opener.document.SCORE.savemsgN.value;");
                   1374:     pWin.document.write("  var usrctr = document.msgcenter.usrctr.value;");
                   1375:     pWin.document.write("  var newval = eval(\\"opener.document.SCORE.newmsg\\"+usrctr);");
                   1376:     pWin.document.write("  newval.value = document.msgcenter.newmsg.value;");
1.37      ng       1377: 
1.38      ng       1378:     pWin.document.write("  var msgchk = \\"\\";");
                   1379:     pWin.document.write("  if (document.msgcenter.subchk.checked) {");
                   1380:     pWin.document.write("     msgchk = \\"msgsub,\\";");
                   1381:     pWin.document.write("  }");
                   1382:     pWin.document.write(   "for (var i=1; i<=nmsg; i++) {");
                   1383:     pWin.document.write("      var opnmsg = eval(\\"opener.document.SCORE.savemsg\\"+i);");
                   1384:     pWin.document.write("      var frmmsg = eval(\\"document.msgcenter.msg\\"+i);");
                   1385:     pWin.document.write("      opnmsg.value = frmmsg.value;");
                   1386:     pWin.document.write("      var chkbox = eval(\\"document.msgcenter.msgn\\"+i);");
                   1387:     pWin.document.write("      if (chkbox.checked) {");
                   1388:     pWin.document.write("         msgchk += \\"savemsg\\"+i+\\",\\";");
                   1389:     pWin.document.write("      }");
                   1390:     pWin.document.write("  }");
                   1391:     pWin.document.write("  if (document.msgcenter.newmsgchk.checked) {");
                   1392:     pWin.document.write("     msgchk += \\"newmsg\\"+usrctr;");
                   1393:     pWin.document.write("  }");
                   1394:     pWin.document.write("  var includemsg = eval(\\"opener.document.SCORE.includemsg\\"+usrctr);");
                   1395:     pWin.document.write("  includemsg.value = msgchk;");
1.37      ng       1396: 
1.38      ng       1397: //    pWin.document.write("  alert(\\"slected=\\"+msgchk)");
                   1398:     pWin.document.write("  self.close()");
1.37      ng       1399: 
1.38      ng       1400:     pWin.document.write("}");
1.34      ng       1401: 
1.38      ng       1402:     pWin.document.write("<");
                   1403:     pWin.document.write("/script>");
1.33      ng       1404: 
1.38      ng       1405:     pWin.document.write("</head><body bgcolor=white>");
1.37      ng       1406: 
1.38      ng       1407:     pWin.document.write("<form action=\\"inactive\\" name=\\"msgcenter\\">");
                   1408:     pWin.document.write("<input value=\\""+usrctr+"\\" name=\\"usrctr\\" type=\\"hidden\\">");
                   1409:     pWin.document.write("<font color=\\"green\\" size=+1>&nbsp;Compose Message for \"+fullname+\"</font><br><br>");
1.34      ng       1410: 
1.38      ng       1411:     pWin.document.write("<table border=0 width=100%><tr><td bgcolor=\\"#777777\\">");
                   1412:     pWin.document.write("<table border=0 width=100%><tr bgcolor=\\"#ddffff\\">");
                   1413:     pWin.document.write("<td><b>Type</b></td><td><b>Include</b></td><td><b>Message</td></tr>");
                   1414: }
                   1415:     function displaySubject(msg,shwsel) {
                   1416:     pWin.document.write("<tr bgcolor=\\"#ffffdd\\">");
                   1417:     pWin.document.write("<td>Subject</td>");
                   1418:     pWin.document.write("<td align=\\"center\\"><input name=\\"subchk\\" type=\\"checkbox\\"" +shwsel+"></td>");
                   1419:     pWin.document.write("<td><input name=\\"msgsub\\" type=\\"text\\" value=\\""+msg+" \\"size=\\"60\\" maxlength=\\"80\\"></td></tr>");
1.34      ng       1420: }
                   1421: 
1.38      ng       1422: function displaySavedMsg(ctr,msg,shwsel) {
                   1423:     pWin.document.write("<tr bgcolor=\\"#ffffdd\\">");
                   1424:     pWin.document.write("<td align=\\"center\\">"+ctr+"</td>");
                   1425:     pWin.document.write("<td align=\\"center\\"><input name=\\"msgn"+ctr+"\\" type=\\"checkbox\\"" +shwsel+"></td>");
                   1426:     pWin.document.write("<td><input name=\\"msg"+ctr+"\\" type=\\"text\\" value=\\""+msg+" \\" size=\\"60\\" maxlength=\\"80\\"></td></tr>");
1.34      ng       1427: }
                   1428: 
1.38      ng       1429:   function newMsg(newmsg,shwsel) {
                   1430:     pWin.document.write("<tr bgcolor=\\"#ffffdd\\">");
                   1431:     pWin.document.write("<td align=\\"center\\">New</td>");
                   1432:     pWin.document.write("<td align=\\"center\\"><input name=\\"newmsgchk\\" type=\\"checkbox\\"" +shwsel+"></td>");
1.39      ng       1433:     pWin.document.write("<td><input name=\\"newmsg\\" type=\\"text\\" onchange=\\"javascript:this.form.newmsgchk.checked=true\\" value=\\""+newmsg+" \\" size=\\"60\\" maxlength=\\"80\\"></td></tr>");
1.34      ng       1434: }
                   1435: 
1.38      ng       1436:   function msgTail() {
                   1437:     pWin.document.write("</table>");
                   1438:     pWin.document.write("</td></tr></table>&nbsp;");
                   1439:     pWin.document.write("<input type=\\"button\\" value=\\"Save\\" onClick=\\"javascript:checkInput()\\">&nbsp;&nbsp;");
                   1440:     pWin.document.write("<input type=\\"button\\" value=\\"Cancel\\" onClick=\\"self.close()\\"><br><br>");
                   1441:     pWin.document.write("</form>");
                   1442:     pWin.document.write("</body></html>");
                   1443: }
1.34      ng       1444: 
1.38      ng       1445: //====================== Script for keyword highlight options ==============
                   1446:   function kwhighlight() {
                   1447:     var kwclr    = document.SCORE.kwclr.value;
                   1448:     var kwsize   = document.SCORE.kwsize.value;
                   1449:     var kwstyle  = document.SCORE.kwstyle.value;
                   1450:     var redsel = "";
                   1451:     var grnsel = "";
                   1452:     var blusel = "";
                   1453:     if (kwclr=="red")   {var redsel="checked"};
                   1454:     if (kwclr=="green") {var grnsel="checked"};
                   1455:     if (kwclr=="blue")  {var blusel="checked"};
                   1456:     var sznsel = "";
                   1457:     var sz1sel = "";
                   1458:     var sz2sel = "";
                   1459:     if (kwsize=="0")  {var sznsel="checked"};
                   1460:     if (kwsize=="+1") {var sz1sel="checked"};
                   1461:     if (kwsize=="+2") {var sz2sel="checked"};
                   1462:     var synsel = "";
                   1463:     var syisel = "";
                   1464:     var sybsel = "";
                   1465:     if (kwstyle=="")    {var synsel="checked"};
                   1466:     if (kwstyle=="<i>") {var syisel="checked"};
                   1467:     if (kwstyle=="<b>") {var sybsel="checked"};
                   1468:     highlightCentral();
                   1469:     highlightbody('red','red',redsel,'0','normal',sznsel,'','normal',synsel);
                   1470:     highlightbody('green','green',grnsel,'+1','+1',sz1sel,'<i>','italic',syisel);
                   1471:     highlightbody('blue','blue',blusel,'+2','+2',sz2sel,'<b>','bold',sybsel);
                   1472:     highlightend();
                   1473:     return;
1.34      ng       1474:   }
                   1475: 
                   1476: 
1.38      ng       1477:   function highlightCentral() {
                   1478:     hwdWin = window.open('', 'KeywordHighlightCentral', 'toolbar=no,location=no,scrollbars=no,width=400,height=300,screenx=100,screeny=75');
                   1479:     hwdWin.document.write("<html><head>");
                   1480:     hwdWin.document.write("<title>Highlight Central</title>");
1.34      ng       1481: 
1.38      ng       1482:     hwdWin.document.write("<script language=javascript>");
1.39      ng       1483:     hwdWin.document.write("function updateChoice(flag) {");
1.38      ng       1484:     hwdWin.document.write("  opener.document.SCORE.kwclr.value = radioSelection(document.hlCenter.kwdclr);");
                   1485:     hwdWin.document.write("  opener.document.SCORE.kwsize.value = radioSelection(document.hlCenter.kwdsize);");
                   1486:     hwdWin.document.write("  opener.document.SCORE.kwstyle.value = radioSelection(document.hlCenter.kwdstyle);");
1.40      ng       1487: //    hwdWin.document.write("     var kwords=opener.document.SCORE.keywords.value;");
                   1488: //    hwdWin.document.write("     alert(\\"keywords=\\"+opener.document.SCORE.keywords.value);");
                   1489: //    hwdWin.document.write("     return;");
                   1490: 
1.39      ng       1491:     hwdWin.document.write("  if (flag==1){");
                   1492:     hwdWin.document.write("     opener.document.SCORE.refresh.value = \\"on\\";");
1.40      ng       1493:     hwdWin.document.write("     if (opener.document.SCORE.keywords.value!=\\"\\"){");
                   1494:     hwdWin.document.write("        opener.document.SCORE.submit();");
                   1495:     hwdWin.document.write("     }");
1.39      ng       1496:     hwdWin.document.write("   }");
1.38      ng       1497:     hwdWin.document.write("  self.close()");
                   1498:     hwdWin.document.write("}");
1.26      albertel 1499: 
1.38      ng       1500:     hwdWin.document.write("function radioSelection(radioButton) {");
                   1501:     hwdWin.document.write("    var selection=null;");
                   1502:     hwdWin.document.write("    for (var i=0; i<radioButton.length; i++) {");
                   1503:     hwdWin.document.write("        if (radioButton[i].checked) {");
                   1504:     hwdWin.document.write("            selection=radioButton[i].value;");
                   1505:     hwdWin.document.write("            return selection;");
                   1506:     hwdWin.document.write("        }");
                   1507:     hwdWin.document.write("    }");
                   1508:     hwdWin.document.write("}");
1.26      albertel 1509: 
1.38      ng       1510:     hwdWin.document.write("<");
                   1511:     hwdWin.document.write("/script>");
1.13      albertel 1512: 
1.38      ng       1513:     hwdWin.document.write("</head><body bgcolor=white>");
1.13      albertel 1514: 
1.38      ng       1515:     hwdWin.document.write("<form action=\\"inactive\\" name=\\"hlCenter\\">");
                   1516:     hwdWin.document.write("<font color=\\"green\\" size=+1>&nbsp;Keyword Highlight Options</font><br><br>");
1.13      albertel 1517: 
1.38      ng       1518:     hwdWin.document.write("<table border=0 width=100%><tr><td bgcolor=\\"#777777\\">");
                   1519:     hwdWin.document.write("<table border=0 width=100%><tr bgcolor=\\"#ddffff\\">");
                   1520:     hwdWin.document.write("<td><b>Text Color</b></td><td><b>Font Size</b></td><td><b>Font Style</td></tr>");
1.30      ng       1521:   }
1.38      ng       1522: 
                   1523:   function highlightbody(clrval,clrtxt,clrsel,szval,sztxt,szsel,syval,sytxt,sysel) { 
                   1524:     hwdWin.document.write("<tr bgcolor=\\"#ffffdd\\">");
                   1525:     hwdWin.document.write("<td align=\\"left\\">");
                   1526:     hwdWin.document.write("<input name=\\"kwdclr\\" type=\\"radio\\" value=\\""+clrval+"\\" "+clrsel+">&nbsp;"+clrtxt+"</td>");
                   1527:     hwdWin.document.write("<td align=\\"left\\">");
                   1528:     hwdWin.document.write("<input name=\\"kwdsize\\" type=\\"radio\\" value=\\""+szval+"\\" "+szsel+">&nbsp;"+sztxt+"</td>");
                   1529:     hwdWin.document.write("<td align=\\"left\\">");
                   1530:     hwdWin.document.write("<input name=\\"kwdstyle\\" type=\\"radio\\" value=\\""+syval+"\\" "+sysel+">&nbsp;"+sytxt+"</td>");
                   1531:     hwdWin.document.write("</tr>");
1.13      albertel 1532:   }
1.5       albertel 1533: 
1.38      ng       1534:   function highlightend() { 
                   1535:     hwdWin.document.write("</table>");
                   1536:     hwdWin.document.write("</td></tr></table>&nbsp;");
1.40      ng       1537:     hwdWin.document.write("<input type=\\"button\\" value=\\"Save\\" onClick=\\"javascript:updateChoice(0)\\">&nbsp;&nbsp;");
                   1538:     hwdWin.document.write("<input type=\\"button\\" value=\\"Save & Refresh\\" onClick=\\"javascript:updateChoice(1)\\">&nbsp;&nbsp;");
1.38      ng       1539:     hwdWin.document.write("<input type=\\"button\\" value=\\"Cancel\\" onClick=\\"self.close()\\"><br><br>");
                   1540:     hwdWin.document.write("</form>");
                   1541:     hwdWin.document.write("</body></html>");
1.13      albertel 1542:   }
1.5       albertel 1543: 
1.38      ng       1544: </script>
                   1545: SUBJAVASCRIPT
1.5       albertel 1546: }
                   1547: 
1.27      albertel 1548: sub csvupload_javascript_reverse_associate {
                   1549:   return(<<ENDPICK);
                   1550:   function verify(vf) {
                   1551:     var foundsomething=0;
                   1552:     var founduname=0;
                   1553:     var founddomain=0;
                   1554:     for (i=0;i<=vf.nfields.value;i++) {
                   1555:       tw=eval('vf.f'+i+'.selectedIndex');
                   1556:       if (i==0 && tw!=0) { founduname=1; }
                   1557:       if (i==1 && tw!=0) { founddomain=1; }
                   1558:       if (i!=0 && i!=1 && tw!=0) { foundsomething=1; }
                   1559:     }
                   1560:     if (founduname==0 || founddomain==0) {
                   1561:       alert('You need to specify at both the username and domain');
                   1562:       return;
                   1563:     }
                   1564:     if (foundsomething==0) {
                   1565:       alert('You need to specify at least one grading field');
                   1566:       return;
                   1567:     }
                   1568:     vf.submit();
                   1569:   }
                   1570:   function flip(vf,tf) {
                   1571:     var nw=eval('vf.f'+tf+'.selectedIndex');
                   1572:     var i;
                   1573:     for (i=0;i<=vf.nfields.value;i++) {
                   1574:       //can not pick the same destination field for both name and domain
                   1575:       if (((i ==0)||(i ==1)) && 
                   1576:           ((tf==0)||(tf==1)) && 
                   1577:           (i!=tf) &&
                   1578:           (eval('vf.f'+i+'.selectedIndex')==nw)) {
                   1579:         eval('vf.f'+i+'.selectedIndex=0;')
                   1580:       }
                   1581:     }
                   1582:   }
                   1583: ENDPICK
                   1584: }
                   1585: 
                   1586: sub csvupload_javascript_forward_associate {
                   1587:   return(<<ENDPICK);
                   1588:   function verify(vf) {
                   1589:     var foundsomething=0;
                   1590:     var founduname=0;
                   1591:     var founddomain=0;
                   1592:     for (i=0;i<=vf.nfields.value;i++) {
                   1593:       tw=eval('vf.f'+i+'.selectedIndex');
                   1594:       if (tw==1) { founduname=1; }
                   1595:       if (tw==2) { founddomain=1; }
                   1596:       if (tw>2) { foundsomething=1; }
                   1597:     }
                   1598:     if (founduname==0 || founddomain==0) {
                   1599:       alert('You need to specify at both the username and domain');
                   1600:       return;
                   1601:     }
                   1602:     if (foundsomething==0) {
                   1603:       alert('You need to specify at least one grading field');
                   1604:       return;
                   1605:     }
                   1606:     vf.submit();
                   1607:   }
                   1608:   function flip(vf,tf) {
                   1609:     var nw=eval('vf.f'+tf+'.selectedIndex');
                   1610:     var i;
                   1611:     //can not pick the same destination field twice
                   1612:     for (i=0;i<=vf.nfields.value;i++) {
                   1613:       if ((i!=tf) && (eval('vf.f'+i+'.selectedIndex')==nw)) {
                   1614:         eval('vf.f'+i+'.selectedIndex=0;')
                   1615:       }
                   1616:     }
                   1617:   }
                   1618: ENDPICK
                   1619: }
                   1620: 
1.26      albertel 1621: sub csvuploadmap_header {
1.41    ! ng       1622:     my ($request,$symb,$url,$datatoken,$distotal)= @_;
        !          1623:     my $result;
        !          1624:     my $javascript;
        !          1625:     if ($ENV{'form.upfile_associate'} eq 'reverse') {
        !          1626: 	$javascript=&csvupload_javascript_reverse_associate();
        !          1627:     } else {
        !          1628: 	$javascript=&csvupload_javascript_forward_associate();
        !          1629:     }
        !          1630:     $request->print(<<ENDPICK);
1.26      albertel 1631: <form method="post" enctype="multipart/form-data" action="/adm/grades" name="gradesupload">
                   1632: <h3>Uploading Class Grades for resource $url</h3>
                   1633: <hr>
                   1634: <h3>Identify fields</h3>
                   1635: Total number of records found in file: $distotal <hr />
                   1636: Enter as many fields as you can. The system will inform you and bring you back
                   1637: to this page if the data selected is insufficient to run your class.<hr />
                   1638: <input type="button" value="Reverse Association" onClick="javascript:this.form.associate.value='Reverse Association';submit(this.form);" />
                   1639: <input type="hidden" name="associate"  value="" />
                   1640: <input type="hidden" name="phase"      value="three" />
                   1641: <input type="hidden" name="datatoken"  value="$datatoken" />
                   1642: <input type="hidden" name="fileupload" value="$ENV{'form.fileupload'}" />
                   1643: <input type="hidden" name="upfiletype" value="$ENV{'form.upfiletype'}" />
                   1644: <input type="hidden" name="upfile_associate" 
                   1645:                                        value="$ENV{'form.upfile_associate'}" />
                   1646: <input type="hidden" name="symb"       value="$symb" />
                   1647: <input type="hidden" name="url"        value="$url" />
                   1648: <input type="hidden" name="command"    value="csvuploadassign" />
                   1649: <hr />
                   1650: <script type="text/javascript" language="Javascript">
                   1651: $javascript
                   1652: </script>
                   1653: ENDPICK
1.41    ! ng       1654: return '';
1.26      albertel 1655: 
                   1656: }
                   1657: 
                   1658: sub csvupload_fields {
1.41    ! ng       1659:     my ($url) = @_;
        !          1660:     my (@parts) = &getpartlist($url);
        !          1661:     my @fields=(['username','Student Username'],['domain','Student Domain']);
        !          1662:     foreach my $part (sort(@parts)) {
        !          1663: 	my @datum;
        !          1664: 	my $display=&Apache::lonnet::metadata($url,$part.'.display');
        !          1665: 	my $name=$part;
        !          1666: 	if  (!$display) { $display = $name; }
        !          1667: 	@datum=($name,$display);
        !          1668: 	push(@fields,\@datum);
        !          1669:     }
        !          1670:     return (@fields);
1.26      albertel 1671: }
                   1672: 
                   1673: sub csvuploadmap_footer {
1.41    ! ng       1674:     my ($request,$i,$keyfields) =@_;
        !          1675:     $request->print(<<ENDPICK);
1.26      albertel 1676: </table>
                   1677: <input type="hidden" name="nfields" value="$i" />
                   1678: <input type="hidden" name="keyfields" value="$keyfields" />
                   1679: <input type="button" onClick="javascript:verify(this.form)" value="Assign Grades" /><br />
                   1680: </form>
                   1681: ENDPICK
                   1682: }
                   1683: 
                   1684: sub csvuploadmap {
1.41    ! ng       1685:     my ($request)= @_;
        !          1686:     my ($symb,$url)=&get_symb_and_url($request);
        !          1687:     if (!$symb) {return '';}
        !          1688:     my $datatoken;
        !          1689:     if (!$ENV{'form.datatoken'}) {
        !          1690: 	$datatoken=&Apache::loncommon::upfile_store($request);
1.26      albertel 1691:     } else {
1.41    ! ng       1692: 	$datatoken=$ENV{'form.datatoken'};
        !          1693: 	&Apache::loncommon::load_tmp_file($request);
1.26      albertel 1694:     }
1.41    ! ng       1695:     my @records=&Apache::loncommon::upfile_record_sep();
        !          1696:     &csvuploadmap_header($request,$symb,$url,$datatoken,$#records+1);
        !          1697:     my ($i,$keyfields);
        !          1698:     if (@records) {
        !          1699: 	my @fields=&csvupload_fields($url);
        !          1700: 	if ($ENV{'form.upfile_associate'} eq 'reverse') {	
        !          1701: 	    &Apache::loncommon::csv_print_samples($request,\@records);
        !          1702: 	    $i=&Apache::loncommon::csv_print_select_table($request,\@records,
        !          1703: 							  \@fields);
        !          1704: 	    foreach (@fields) { $keyfields.=$_->[0].','; }
        !          1705: 	    chop($keyfields);
        !          1706: 	} else {
        !          1707: 	    unshift(@fields,['none','']);
        !          1708: 	    $i=&Apache::loncommon::csv_samples_select_table($request,\@records,
        !          1709: 							    \@fields);
        !          1710: 	    my %sone=&Apache::loncommon::record_sep($records[0]);
        !          1711: 	    $keyfields=join(',',sort(keys(%sone)));
        !          1712: 	}
        !          1713:     }
        !          1714:     &csvuploadmap_footer($request,$i,$keyfields);
        !          1715:     return '';
1.27      albertel 1716: }
                   1717: 
                   1718: sub csvuploadassign {
1.41    ! ng       1719:     my ($request)= @_;
        !          1720:     my ($symb,$url)=&get_symb_and_url($request);
        !          1721:     if (!$symb) {return '';}
        !          1722:     &Apache::loncommon::load_tmp_file($request);
        !          1723:     my @gradedata=&Apache::loncommon::upfile_record_sep();
        !          1724:     my @keyfields = split(/\,/,$ENV{'form.keyfields'});
        !          1725:     my %fields=();
        !          1726:     for (my $i=0; $i<=$ENV{'form.nfields'}; $i++) {
        !          1727: 	if ($ENV{'form.upfile_associate'} eq 'reverse') {
        !          1728: 	    if ($ENV{'form.f'.$i} ne 'none') {
        !          1729: 		$fields{$keyfields[$i]}=$ENV{'form.f'.$i};
        !          1730: 	    }
        !          1731: 	} else {
        !          1732: 	    if ($ENV{'form.f'.$i} ne 'none') {
        !          1733: 		$fields{$ENV{'form.f'.$i}}=$keyfields[$i];
        !          1734: 	    }
        !          1735: 	}
1.27      albertel 1736:     }
1.41    ! ng       1737:     $request->print('<h3>Assigning Grades</h3>');
        !          1738:     my $courseid=$ENV{'request.course.id'};
1.34      ng       1739: #  my $cdom=$ENV{"course.$courseid.domain"};
                   1740: #  my $cnum=$ENV{"course.$courseid.num"};
1.41    ! ng       1741:     my ($classlist) = &getclasslist('all','1');
        !          1742:     my @skipped;
        !          1743:     my $countdone=0;
        !          1744:     foreach my $grade (@gradedata) {
        !          1745: 	my %entries=&Apache::loncommon::record_sep($grade);
        !          1746: 	my $username=$entries{$fields{'username'}};
        !          1747: 	my $domain=$entries{$fields{'domain'}};
        !          1748: 	if (!exists($$classlist{"$username:$domain"})) {
        !          1749: 	    push(@skipped,"$username:$domain");
        !          1750: 	    next;
        !          1751: 	}
        !          1752: 	my %grades;
        !          1753: 	foreach my $dest (keys(%fields)) {
        !          1754: 	    if ($dest eq 'username' || $dest eq 'domain') { next; }
        !          1755: 	    if ($entries{$fields{$dest}} eq '') { next; }
        !          1756: 	    my $store_key=$dest;
        !          1757: 	    $store_key=~s/^stores/resource/;
        !          1758: 	    $store_key=~s/_/\./g;
        !          1759: 	    $grades{$store_key}=$entries{$fields{$dest}};
        !          1760: 	}
        !          1761: 	$grades{"resource.regrader"}="$ENV{'user.name'}:$ENV{'user.domain'}";
        !          1762: 	&Apache::lonnet::cstore(\%grades,$symb,$ENV{'request.course.id'},
        !          1763: 				$domain,$username);
        !          1764: 	$request->print('.');
        !          1765: 	$request->rflush();
        !          1766: 	$countdone++;
        !          1767:     }
        !          1768:     $request->print("<br />Stored $countdone students\n");
        !          1769:     if (@skipped) {
        !          1770: 	$request->print('<br /><font size="+1"><b>Skipped Students</b></font><br />');
        !          1771: 	foreach my $student (@skipped) { $request->print("<br />$student"); }
        !          1772:     }
        !          1773:     $request->print(&view_edit_entire_class_form($symb,$url));
        !          1774:     $request->print(&show_grading_menu_form($symb,$url));
        !          1775:     return '';
1.26      albertel 1776: }
                   1777: 
1.2       albertel 1778: sub send_header {
1.41    ! ng       1779:     my ($request)= @_;
        !          1780:     $request->print(&Apache::lontexconvert::header());
1.6       albertel 1781: #  $request->print("
                   1782: #<script>
                   1783: #remotewindow=open('','homeworkremote');
                   1784: #remotewindow.close();
                   1785: #</script>"); 
1.41    ! ng       1786:     $request->print('<body bgcolor="#FFFFFF">');
1.2       albertel 1787: }
                   1788: 
                   1789: sub send_footer {
1.41    ! ng       1790:     my ($request)= @_;
        !          1791:     $request->print('</body>');
        !          1792:     $request->print(&Apache::lontexconvert::footer());
1.2       albertel 1793: }
                   1794: 
1.1       albertel 1795: sub handler {
1.41    ! ng       1796:     my $request=$_[0];
        !          1797:     
        !          1798:     if ($ENV{'browser.mathml'}) {
        !          1799: 	$request->content_type('text/xml');
        !          1800:     } else {
        !          1801: 	$request->content_type('text/html');
        !          1802:     }
        !          1803:     $request->send_http_header;
        !          1804:     return OK if $request->header_only;
        !          1805:     &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'});
        !          1806:     my $url=$ENV{'form.url'};
        !          1807:     my $symb=$ENV{'form.symb'};
        !          1808:     my $command=$ENV{'form.command'};
        !          1809:     if (!$url) {
        !          1810: 	my ($temp1,$temp2);
        !          1811: 	($temp1,$temp2,$ENV{'form.url'})=split(/___/,$symb);
        !          1812: 	$url = $ENV{'form.url'};
        !          1813:     }
        !          1814:     &send_header($request);
        !          1815:     if ($url eq '' && $symb eq '') {
        !          1816: 	if ($ENV{'user.adv'}) {
        !          1817: 	    if (($ENV{'form.codeone'}) && ($ENV{'form.codetwo'}) &&
        !          1818: 		($ENV{'form.codethree'})) {
        !          1819: 		my $token=$ENV{'form.codeone'}.'*'.$ENV{'form.codetwo'}.'*'.
        !          1820: 		    $ENV{'form.codethree'};
        !          1821: 		my ($tsymb,$tuname,$tudom,$tcrsid)=
        !          1822: 		    &Apache::lonnet::checkin($token);
        !          1823: 		if ($tsymb) {
        !          1824: 		    my ($map,$id,$url)=split(/\_\_\_/,$tsymb);
        !          1825: 		    if (&Apache::lonnet::allowed('mgr',$tcrsid)) {
        !          1826: 			$request->print(
        !          1827: 					&Apache::lonnet::ssi('/res/'.$url,
        !          1828: 							     ('grade_username' => $tuname,
        !          1829: 							      'grade_domain' => $tudom,
        !          1830: 							      'grade_courseid' => $tcrsid,
        !          1831: 							      'grade_symb' => $tsymb)));
        !          1832: 		    } else {
        !          1833: 			$request->print('<h1>Not authorized: '.$token.'</h1>');
        !          1834: 		    }           
        !          1835: 		} else {
        !          1836: 		    $request->print('<h1>Not a valid DocID: '.$token.'</h1>');
        !          1837: 		}
1.14      www      1838: 	    } else {
1.41    ! ng       1839: 		$request->print(&Apache::lonxml::tokeninputfield());
        !          1840: 	    }
        !          1841: 	}
        !          1842:     } else {
        !          1843: 	#&Apache::lonhomework::showhashsubset(\%ENV,'^form');
        !          1844: 	$Apache::grades::viewgrades=&Apache::lonnet::allowed('vgr',$ENV{'request.course.id'});
        !          1845: 	if ($command eq 'submission') {
        !          1846: 	    &listStudents($request) if ($ENV{'form.student'} eq '');
        !          1847: 	    &submission($request,0,0) if ($ENV{'form.student'} ne '');
        !          1848: 	} elsif ($command eq 'processGroup') {
        !          1849: 	    &processGroup($request);
        !          1850: 	} elsif ($command eq 'gradingmenu') {
        !          1851: 	    $request->print(&gradingmenu($request));
        !          1852: 	} elsif ($command eq 'viewgrades') {
        !          1853: 	    $request->print(&viewgrades($request));
        !          1854: 	} elsif ($command eq 'handgrade') {
        !          1855: 	    $request->print(&processHandGrade($request));
        !          1856: 	} elsif ($command eq 'editgrades') {
        !          1857: 	    $request->print(&editgrades($request));
        !          1858: 	} elsif ($command eq 'verify') {
        !          1859: 	    $request->print(&verifyreceipt($request));
        !          1860: 	} elsif ($command eq 'csvupload') {
        !          1861: 	    $request->print(&csvupload($request));
        !          1862: 	} elsif ($command eq 'viewclasslist') {
        !          1863: 	    $request->print(&viewclasslist($request));
        !          1864: 	} elsif ($command eq 'csvuploadmap') {
        !          1865: 	    $request->print(&csvuploadmap($request));
1.34      ng       1866: #    } elsif ($command eq 'receiptInput') {
                   1867: #      &receiptInput($request);
1.41    ! ng       1868: 	} elsif ($command eq 'csvuploadassign') {
        !          1869: 	    if ($ENV{'form.associate'} ne 'Reverse Association') {
        !          1870: 		$request->print(&csvuploadassign($request));
        !          1871: 	    } else {
        !          1872: 		if ( $ENV{'form.upfile_associate'} ne 'reverse' ) {
        !          1873: 		    $ENV{'form.upfile_associate'} = 'reverse';
        !          1874: 		} else {
        !          1875: 		    $ENV{'form.upfile_associate'} = 'forward';
        !          1876: 		}
        !          1877: 		$request->print(&csvuploadmap($request));
        !          1878: 	    }
1.26      albertel 1879: 	} else {
1.41    ! ng       1880: 	    $request->print("Unknown action: $command:");
1.26      albertel 1881: 	}
1.2       albertel 1882:     }
1.41    ! ng       1883:     &send_footer($request);
        !          1884:     return OK;
1.1       albertel 1885: }
                   1886: 
                   1887: 1;
                   1888: 
1.13      albertel 1889: __END__;

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