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

1.17      albertel    1: # The LearningOnline Network with CAPA
1.13      albertel    2: # The LON-CAPA Grading handler
1.17      albertel    3: #
1.36    ! ng          4: # $Id: grades.pm,v 1.35 2002/07/03 21:05:33 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;
                     43: use Apache::Constants qw(:common);
                     44: 
1.2       albertel   45: sub moreinfo {
1.13      albertel   46:   my ($request,$reason) = @_;
                     47:   $request->print("Unable to process request: $reason");
                     48:   if ( $Apache::grades::viewgrades eq 'F' ) {
                     49:     $request->print('<form action="/adm/grades" method="post">'."\n");
1.16      albertel   50:     if ($ENV{'form.url'}) {
                     51:       $request->print('<input type="hidden" name="url" value="'.$ENV{'form.url'}.'" />'."\n");
                     52:     }
                     53:     if ($ENV{'form.symb'}) {
                     54:       $request->print('<input type="hidden" name="symb" value="'.$ENV{'form.symb'}.'" />'."\n");
                     55:     }
1.35      ng         56: #    $request->print('<input type="hidden" name="command" value="submission" />'."\n");
1.16      albertel   57:     $request->print('<input type="hidden" name="command" value="'.$ENV{'form.command'}.'" />'."\n");
                     58:     $request->print("Student:".'<input type="text" name="student" value="'.$ENV{'form.student'}.'" />'."<br />\n");
                     59:     $request->print("Domain:".'<input type="text" name="domain" value="'.$ENV{'user.domain'}.'" />'."<br />\n");
                     60:     $request->print('<input type="submit" name="submit" value="ReSubmit" />'."<br />\n");
1.13      albertel   61:     $request->print('</form>');
                     62:   }
                     63:   return '';
1.2       albertel   64: }
                     65: 
1.23      www        66: sub verifyreceipt {
                     67:     my $request=shift;
                     68:     my $courseid=$ENV{'request.course.id'};
1.34      ng         69: #    my $cdom=$ENV{"course.$courseid.domain"};
                     70: #    my $cnum=$ENV{"course.$courseid.num"};
1.23      www        71:     my $receipt=unpack("%32C*",$Apache::lonnet::perlvar{'lonHostID'}).'-'.
                     72:                 $ENV{'form.receipt'};
                     73:     $receipt=~s/[^\-\d]//g;
                     74:     my $symb=$ENV{'form.symb'};
                     75:     unless ($symb) {
                     76: 	$symb=&Apache::lonnet::symbread($ENV{'form.url'});
                     77:     }
                     78:     if ((&Apache::lonnet::allowed('mgr',$courseid)) && ($symb)) {
                     79:         $request->print('<h1>Verifying Submission Receipt '.$receipt.'</h1>');
                     80:         my $matches=0;
1.34      ng         81:         my ($classlist) = &getclasslist('all','0');
                     82:         foreach my $student ( sort(@{ $$classlist{'all'} }) ) {
1.23      www        83:             my ($uname,$udom)=split(/\:/,$student);
                     84:             if ($receipt eq 
                     85:              &Apache::lonnet::ireceipt($uname,$udom,$courseid,$symb)) {
                     86:                $request->print('Matching '.$student.'<br>');
                     87:                $matches++;
                     88: 	   }
                     89:         }
1.30      ng         90:         $request->printf('<p>'.$matches." match%s</p>",$matches <= 1 ? '' : 'es');
1.33      ng         91: # needs to print who is matched
1.23      www        92:     }
                     93:     return '';
                     94: }
1.13      albertel   95: 
1.32      ng         96: sub student_gradeStatus {
                     97:   my ($url,$udom,$uname) = @_;
                     98:   my $symb=($ENV{'form.symb'} ne '' ? $ENV{'form.symb'} : (&Apache::lonnet::symbread($url)));
                     99:   my %record= &Apache::lonnet::restore($symb,$ENV{'request.course.id'},$udom,$uname);
                    100:   foreach my $part (&getpartlist($url)) {
                    101:     my ($temp,$part,$type)=split(/_/,$part);
                    102:     if ($type eq 'solved') {
                    103:       my ($status,$foo)=split(/_/,$record{"resource.$part.$type"},2);
1.34      ng        104:       $status = 'partial' if ($foo =~ /^partially/);
1.32      ng        105:       $status = 'nothing' if ($status eq '');
                    106:       return $type,$status;
                    107:     }
                    108:   }
                    109:   return '';
                    110: }
                    111: 
1.34      ng        112: sub get_fullname {
                    113:     my ($sname,$sdom) = @_;
                    114:     my %name=&Apache::lonnet::get('environment', ['lastname','generation',
                    115: 						  'firstname','middlename'],
                    116: 				  $sdom,$sname);
                    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.30      ng        127: sub listStudents {
                    128:   my ($request) = shift;
1.34      ng        129:   my $cdom      =$ENV{"course.$ENV{'request.course.id'}.domain"};
                    130:   my $cnum      =$ENV{"course.$ENV{'request.course.id'}.num"};
                    131:   my $getsec    =$ENV{'form.section'};
                    132:   my $submitonly=$ENV{'form.submitonly'};
1.30      ng        133: 
1.23      www       134:   $request->print(<<ENDTABLEST);
1.34      ng        135: <h2><font color="#339933">&nbsp;View Submissions for a Student or a Group of Students</font></h2>
                    136: &nbsp;<font size=+1><b>Resource:</b> $ENV{'form.url'}<br /><br />
                    137: <form action="/adm/grades" method="post">&nbsp;<b>View Options</b></font><br />
                    138: &nbsp;<b>View Problem: </b><input type="radio" name="vProb" value="no" checked> no 
                    139: <input type="radio" name="vProb" value="yes"> yes 
                    140: &nbsp;&nbsp;&nbsp;<b>Submissions: </b><input type="radio" name="lastSub" value="last" checked> last 
                    141: <input type="radio" name="lastSub" value="all"> all 
                    142: <input type="hidden" name="section" value="$getsec">
                    143: <input type="hidden" name="submitonly" value="$submitonly">
1.35      ng        144: <input type="hidden" name="response" value="$ENV{'form.response'}">
                    145: <input type="hidden" name="handgrade" value="$ENV{'form.handgrade'}">
1.32      ng        146: <table border="0"><tr><td bgcolor="#777777">
1.34      ng        147: <table border="0"><tr bgcolor="#e6ffff">
                    148: <td><b>&nbsp;Select&nbsp;</b></td><td><b>&nbsp;Username&nbsp;</b></td>
                    149: <td><b>&nbsp;Fullname&nbsp;</b></td><td><b>&nbsp;Domain&nbsp;</b></td>
                    150: <td><b>&nbsp;Grade Status&nbsp;</b></td></tr>
1.23      www       151: ENDTABLEST
1.34      ng        152:   if ($ENV{'form.url'}) {
                    153:       $request->print('<input type="hidden" name="url" value="'.$ENV{'form.url'}.'" />'."\n");
                    154:   }
                    155:   if ($ENV{'form.symb'}) {
                    156:       $request->print('<input type="hidden" name="symb" value="'.$ENV{'form.symb'}.'" />'."\n");
                    157:   }
                    158:   $request->print('<input type="hidden" name="command" value="processGroup" />'."\n");
                    159: 
                    160:   my ($classlist) = &getclasslist($getsec,'0');
                    161:   foreach my $student ( sort(@{ $$classlist{$getsec} }) ) {
1.13      albertel  162:       my ($sname,$sdom) = split(/:/,$student);
1.34      ng        163:       my ($type,$status) = &student_gradeStatus($ENV{'form.url'},$cdom,$sname);
                    164:       next if ($status eq 'nothing' && $submitonly eq 'yes');
1.13      albertel  165: 
1.34      ng        166:       my $fullname = &get_fullname($sname,$sdom);
1.13      albertel  167:       if ( $Apache::grades::viewgrades eq 'F' ) {
1.34      ng        168: 	  $request->print("\n".'<tr bgcolor="#ffffe6">'.
                    169: 			  '<td align="center"><input type=checkbox name="stuinfo" value="'.
                    170: 			  $student.':'.$fullname.'"></td>'."\n".
                    171: 			  '<td>&nbsp;'.$sname.'&nbsp;</td>'."\n".
                    172: 			  '<td>&nbsp;'.$fullname.'&nbsp;</td>'."\n".
                    173: 			  '<td align="middle">&nbsp;'.$sdom.'&nbsp;</td>'."\n");
                    174: 	  $request->print('<td align="middle">&nbsp;'.$status.'&nbsp;</td>'."\n");
                    175: 
                    176: 	  $request->print('</tr>');
1.13      albertel  177:       }
                    178:   }
1.28      ng        179:   $request->print('</table></td></tr></table>');
1.34      ng        180:   $request->print('<input type="submit" name="submit" value="View/Grade" /><form />');
1.10      ng        181: }
                    182: 
1.34      ng        183: sub processGroup {
                    184:   my ($request)  = shift;
                    185:   my $ctr        = 0;
                    186:   my @stuchecked = (ref($ENV{'form.stuinfo'}) ? @{$ENV{'form.stuinfo'}}
1.35      ng        187:                            : ($ENV{'form.stuinfo'}));
1.34      ng        188:   my $total      = scalar(@stuchecked)-1;
1.35      ng        189:   if ($stuchecked[0] eq '') {
                    190:       &userError($request,'No student was selected for viewing/grading.');
                    191:       return;
                    192:   }
                    193:   foreach (@stuchecked) {
                    194:       my ($sname,$sdom,$fullname) = split(/:/);
1.34      ng        195:       $ENV{'form.student'} = $sname;
                    196:       $ENV{'form.fullname'} = $fullname;
                    197:       &submission($request,$ctr,$total);
                    198:       $ctr++;
                    199:   }
1.35      ng        200:   return 'The End';
                    201: }
1.34      ng        202: 
1.35      ng        203: sub userError {
                    204:     my ($request, $reason, $step) = @_;
                    205:     $request->print('<h3><font color="red">LON-CAPA User Error</font></h3><br />'."\n");
                    206:     $request->print('<b>Reason: </b>'.$reason.'<br /><br />'."\n");
                    207:     $request->print('<b>Step: </b>'.($step ne '' ? $step : 'Use your browser back button to correct')
                    208: 		    .'<br /><br />'."\n");
                    209:     return '';
1.34      ng        210: }
1.13      albertel  211: 
1.7       albertel  212: #FIXME - needs to handle multiple matches
1.2       albertel  213: sub finduser {
1.13      albertel  214:   my ($name) = @_;
                    215:   my $domain = '';
                    216:   if ( $Apache::grades::viewgrades eq 'F' ) {
1.34      ng        217:     my ($classlist) = &getclasslist('all','0');
1.35      ng        218:     foreach ( sort(@{ $$classlist{'all'} }) ) {
                    219:       my ($posname,$posdomain) = split(/:/);
1.13      albertel  220:       if ($posname =~ $name) { $name=$posname; $domain=$posdomain; last; }
1.7       albertel  221:     }
1.13      albertel  222:     return ($name,$domain);
                    223:   } else {
                    224:     return ($ENV{'user.name'},$ENV{'user.domain'});
                    225:   }
1.5       albertel  226: }
                    227: 
                    228: sub getclasslist {
1.34      ng        229:   my ($getsec,$hideexpired) = @_;
                    230:   my ($coursedomain,$coursenum) = split(/_/,$ENV{'request.course.id'});
1.24      albertel  231:   my %classlist=&Apache::lonnet::dump('classlist',$coursedomain,$coursenum);
1.13      albertel  232:   my $now = time;
1.34      ng        233:   my (@holdsec,@sections);
1.24      albertel  234:   foreach my $student (keys(%classlist)) {
                    235:     my ($end,$start)=split(/:/,$classlist{$student});
1.13      albertel  236:     # still a student?
                    237:     if (($hideexpired) && ($end) && ($end < $now)) {
                    238:       next;
                    239:     }
1.34      ng        240:     my ($unam,$udom) = split(/:/,$student,2);
                    241:     my $section = &Apache::lonnet::usection($udom,$unam,$ENV{'request.course.id'});
1.35      ng        242:     $section = ($section ne '-1' ? $section : 'no');
1.34      ng        243:     push @holdsec,$section;
1.35      ng        244:     push (@{ $classlist{$getsec} }, $student) if ($getsec eq 'all' || $getsec eq $section);
1.34      ng        245:   }
                    246:   my %seen = ();
                    247:   foreach my $item (@holdsec) {
                    248:       push (@sections, $item) unless $seen{$item}++;
1.13      albertel  249:   }
1.34      ng        250:   return (\%classlist,\@sections);
1.5       albertel  251: }
                    252: 
                    253: sub getpartlist {
1.13      albertel  254:   my ($url) = @_;
                    255:   my @parts =();
                    256:   my (@metakeys) = split(/,/,&Apache::lonnet::metadata($url,'keys'));
                    257:   foreach my $key (@metakeys) {
1.30      ng        258:     if ( $key =~ m/stores_([0-9]+)_.*/) {
1.13      albertel  259:       push(@parts,$key);
1.6       albertel  260:     }
1.13      albertel  261:   }
                    262:   return @parts;
1.5       albertel  263: }
                    264: 
                    265: sub viewstudentgrade {
1.13      albertel  266:   my ($url,$symb,$courseid,$student,@parts) = @_;
                    267:   my $result ='';
                    268:   my $cellclr = '"#ffffdd"';
1.28      ng        269:   my ($username,$domain) = split(/:/,$student);
1.13      albertel  270: 
1.34      ng        271:   my $fullname = &get_fullname($username,$domain);
1.28      ng        272:   my %record=&Apache::lonnet::restore($symb,$courseid,$domain,$username);
1.32      ng        273: 
1.28      ng        274:   $result.="<tr bgcolor=$cellclr><td>$username</td><td>$fullname</td><td align=\"middle\">$domain</td>\n";
1.13      albertel  275:   foreach my $part (@parts) {
                    276:     my ($temp,$part,$type)=split(/_/,$part);
1.31      ng        277:     my $score=$record{"resource.$part.$type"};
                    278:     if ($type eq 'awarded' || $type eq 'tries') {
                    279:       $result.='<td align="middle"><input type="text" name="GRADE.'.$student.'.'.$part.'.'.$type.
                    280: 	  '" value="'.$score.'" size="4" /></td>'."\n";
1.13      albertel  281:     } elsif ($type eq 'solved') {
1.31      ng        282:       my ($status,$foo)=split(/_/,$score,2);
1.28      ng        283:       $result.="<td align=\"middle\"><select name=\"GRADE.$student.$part.$type\">\n";
1.31      ng        284:       my $optsel = '<option>correct</option><option>incorrect</option><option>excused</option>'.
1.33      ng        285: 	  '<option>ungraded</option><option>partial</option><option>nothing</option>'."\n";
1.31      ng        286:       $status = 'nothing' if ($status eq '');
                    287:       $optsel =~ s/<option>$status/<option selected="on">$status/;
                    288:       $result.=$optsel;
1.13      albertel  289:       $result.="</select></td>\n";
                    290:     }
                    291:   }
1.29      albertel  292:   $result.='<td></td></tr>';
1.13      albertel  293:   return $result;
1.5       albertel  294: }
1.31      ng        295: 
                    296: #FIXME need to look at the metadata <stores> spec on what type of data to accept and provide an
1.6       albertel  297: #interface based on that, also do that to above function.
1.5       albertel  298: sub setstudentgrade {
1.13      albertel  299:   my ($url,$symb,$courseid,$student,@parts) = @_;
1.34      ng        300:   print "set student grade parts=@parts<br>";
1.13      albertel  301:   my $result ='';
                    302:   my ($stuname,$domain) = split(/:/,$student);
                    303:   my %record=&Apache::lonnet::restore($symb,$courseid,$domain,$stuname);
                    304:   my %newrecord;
                    305: 
                    306:   foreach my $part (@parts) {
                    307:     my ($temp,$part,$type)=split(/_/,$part);
                    308:     my $oldscore=$record{"resource.$part.$type"};
                    309:     my $newscore=$ENV{"form.GRADE.$student.$part.$type"};
1.32      ng        310:     print "old=$oldscore:new=$newscore:<br>";
1.13      albertel  311:     if ($type eq 'solved') {
                    312:       my $update=0;
                    313:       if ($newscore eq 'nothing' ) {
                    314: 	if ($oldscore ne '') {
                    315: 	  $update=1;
                    316: 	  $newscore = '';
1.6       albertel  317: 	}
1.13      albertel  318:       } elsif ($oldscore !~ m/^$newscore/) {
                    319: 	$update=1;
                    320: 	$result.="Updating $stuname to $newscore<br />\n";
1.34      ng        321: 	if ($newscore eq 'correct')   { $newscore = 'correct_by_override'; }
1.13      albertel  322: 	if ($newscore eq 'incorrect') { $newscore = 'incorrect_by_override'; }
1.34      ng        323: 	if ($newscore eq 'excused')   { $newscore = 'excused'; }
                    324: 	if ($newscore eq 'ungraded')  { $newscore = 'ungraded_attempted'; }
                    325: 	if ($newscore eq 'partial')   { $newscore = 'correct_partially_by_override'; }
1.13      albertel  326:       } else {
                    327: 	#$result.="$stuname:$part:$type:unchanged  $oldscore to $newscore:<br />\n";
                    328:       }
                    329:       if ($update) { $newrecord{"resource.$part.$type"}=$newscore; }
                    330:     } else {
                    331:       if ($oldscore ne $newscore) {
                    332: 	$newrecord{"resource.$part.$type"}=$newscore;
                    333: 	$result.="Updating $student"."'s status for $part.$type to $newscore<br />\n";
                    334:       } else {
                    335: 	#$result.="$stuname:$part:$type:unchanged  $oldscore to $newscore:<br />\n";
                    336:       }
                    337:     }
                    338:   }
                    339:   if ( scalar(keys(%newrecord)) > 0 ) {
1.32      ng        340:     $newrecord{'resource.regrader'}="$ENV{'user.name'}:$ENV{'user.domain'}";
1.34      ng        341: #    &Apache::lonnet::cstore(\%newrecord,$symb,$courseid,$domain,$stuname);
1.13      albertel  342: 
                    343:     $result.="Stored away ".scalar(keys(%newrecord))." elements.<br />\n";
                    344:   }
                    345:   return $result;
1.2       albertel  346: }
                    347: 
1.33      ng        348: #
1.32      ng        349: # --------------------------- show submissions of a student, option to grade --------
1.2       albertel  350: sub submission {
1.34      ng        351:   my ($request,$counter,$total) = @_;
1.33      ng        352: 
1.34      ng        353:   if ($counter == 0) {
1.36    ! ng        354:       $request->print(<<SUBJAVASCRIPT);
1.32      ng        355: <script type="text/javascript" language="javascript">
1.33      ng        356:   function updateRadio(radioButton,formtextbox,formsel,wgt) {
                    357:      var pts = formtextbox.value;
                    358:      var resetbox =false;
                    359:      if (isNaN(pts) || pts < 0) {
                    360: 	alert("A number equal or greater than 0 is expected. Entered value = "+pts);
                    361: 	for (var i=0; i<radioButton.length; i++) {
                    362: 	   if (radioButton[i].checked) {
                    363: 	      formtextbox.value = i;
                    364: 	      resetbox = true;
                    365: 	   }
1.32      ng        366: 	}
1.33      ng        367: 	if (!resetbox) {
                    368: 	   formtextbox.value = "";
                    369: 	}
                    370: 	return;
                    371:     }
1.32      ng        372: 
1.33      ng        373:     for (var i=0; i<radioButton.length; i++) {
                    374: 	radioButton[i].checked=false;
                    375: 	if (pts == i) {
                    376: 	   radioButton[i].checked=true;
1.32      ng        377: 	}
1.31      ng        378:     }
1.33      ng        379:     updateSelect(formsel,pts,wgt);
                    380:   }
                    381: 
                    382:   function writeBox(formrad,formsel,pts,wgt) {
                    383:     formrad.value = pts;
                    384:     updateSelect(formsel,pts,wgt);
                    385:     return;
                    386:   }
                    387: 
                    388:   function updateSelect(formsel,pts,wgt) {
                    389:     if (pts == 0) {
                    390:       formsel[1].selected = true;
                    391:     }
                    392:     if (pts > 0 && pts < wgt) {
                    393:       formsel[4].selected = true;
                    394:     }
                    395:     if (pts == wgt) {
                    396:       formsel[0].selected = true;
                    397:     }
                    398:     return;
                    399:   }
1.32      ng        400: 
1.35      ng        401:   function keywords(keyform) {
                    402:     var keywds = keyform.value;
1.36    ! ng        403:     var nret = prompt("Keywords list, separated by a space. Add/delete to list if desired.",keywds);
1.35      ng        404:     if (nret==null) return;
                    405:     keyform.value = nret;
                    406:     return;
                    407:   }
                    408: 
1.36    ! ng        409: //===================== Script to add keyword(s) ==================
        !           410:   function getSel() {
        !           411:     if (document.getSelection) txt = document.getSelection();
        !           412:     else if (document.selection) txt = document.selection.createRange().text;
        !           413:     else return;
        !           414:     var cleantxt = txt.replace(new RegExp('([\\f\\n\\r\\t\\v ])+', 'g')," ");
        !           415:     if (cleantxt=="") {
        !           416: 	alert("Select a word or group of words from document and then click this link.");
        !           417: 	return;
        !           418:     }
        !           419:     var nret = prompt("Add selection to keyword list?",cleantxt);
1.35      ng        420:     if (nret==null) return;
1.36    ! ng        421:     var curlist = document.SCORE.keywords.value;
        !           422:     document.SCORE.keywords.value = curlist+" "+nret;
        !           423:     return;
        !           424:   }
        !           425: 
        !           426: //====================== Script for composing message ==============
        !           427:   function msgCenter(msgform,usrctr,fullname) {
        !           428:     var Nmsg  = msgform.savemsgN.value;
        !           429:     savedMsgHeader(Nmsg,usrctr,fullname);
        !           430:     var subject = msgform.msgsub.value;
        !           431:     displaySubject(subject);
        !           432:     for (var i=1; i<=Nmsg; i++) {
        !           433: 	var message = eval("document.SCORE.savemsg"+i+".value");
        !           434: 	displaySavedMsg(i,message);
        !           435:     }
        !           436:     newmsg = eval("document.SCORE.newmsg"+usrctr+".value");
        !           437:     newMsg(newmsg);
        !           438:     msgTail(); 
1.35      ng        439:     return;
                    440:   }
                    441: 
1.36    ! ng        442: function savedMsgHeader(Nmsg,usrctr,fullname) {
        !           443:     var height = 30*Nmsg+250;
        !           444:     var scrollbar = "no";
        !           445:     if (height > 600) {
        !           446: 	height = 600;
        !           447: 	scrollbar = "yes";
        !           448:     }
        !           449:     pWin = window.open('', 'MessageCenter', 'toolbar=no,location=no,scrollbars='+scrollbar+',width=600,height='+height);
        !           450:     pWin.document.write("<html><head>");
        !           451:     pWin.document.write("<title>Message Central</title>");
        !           452: 
        !           453:     pWin.document.write("<script language=javascript>");
        !           454:     pWin.document.write("function checkInput() {");
        !           455:     pWin.document.write("  opener.document.SCORE.msgsub.value = document.msgcenter.msgsub.value;");
        !           456:     pWin.document.write("  var nmsg   = opener.document.SCORE.savemsgN.value;");
        !           457:     pWin.document.write("  var usrctr = document.msgcenter.usrctr.value;");
        !           458:     pWin.document.write("  var newval = eval(\\"opener.document.SCORE.newmsg\\"+usrctr);");
        !           459:     pWin.document.write("  newval.value = document.msgcenter.newmsg.value;");
        !           460: 
        !           461:     pWin.document.write("  var msgchk = \\"\\";");
        !           462:     pWin.document.write("  if (document.msgcenter.subchk.checked) {");
        !           463:     pWin.document.write("     msgchk = \\"subject,\\";");
        !           464:     pWin.document.write("  }");
        !           465:     pWin.document.write(   "for (var i=1; i<=nmsg; i++) {");
        !           466:     pWin.document.write("      var opnmsg = eval(\\"opener.document.SCORE.savemsg\\"+i);");
        !           467:     pWin.document.write("      var frmmsg = eval(\\"document.msgcenter.msg\\"+i);");
        !           468:     pWin.document.write("      opnmsg.value = frmmsg.value;");
        !           469:     pWin.document.write("      var chkbox = eval(\\"document.msgcenter.msgn\\"+i);");
        !           470:     pWin.document.write("      if (chkbox.checked) {");
        !           471:     pWin.document.write("         msgchk += i+\\",\\";");
        !           472:     pWin.document.write("      }");
        !           473:     pWin.document.write("  }");
        !           474:     pWin.document.write("  if (document.msgcenter.newmsgchk.checked) {");
        !           475:     pWin.document.write("     msgchk += \\"new\\";");
        !           476:     pWin.document.write("  }");
        !           477:     pWin.document.write("  var includemsg = eval(\\"opener.document.SCORE.includemsg\\"+usrctr);");
        !           478:     pWin.document.write("  includemsg.value = msgchk;");
        !           479: 
        !           480:     pWin.document.write("  self.close()");
        !           481: 
        !           482:     pWin.document.write("}");
        !           483: 
        !           484:     pWin.document.write("<");
        !           485:     pWin.document.write("/script>");
        !           486: 
        !           487:     pWin.document.write("</head><body bgcolor=white>");
        !           488: 
        !           489:     pWin.document.write("<form action=\\"inactive\\" name=\\"msgcenter\\">");
        !           490:     pWin.document.write("<input value=\\""+usrctr+"\\" name=\\"usrctr\\" type=\\"hidden\\">");
        !           491:     pWin.document.write("<font color=\\"green\\" size=+1>&nbsp;Compose Message for \"+fullname+\"</font><br><br>");
        !           492: 
        !           493:     pWin.document.write("<table border=0 width=100%><tr><td bgcolor=\\"#777777\\">");
        !           494:     pWin.document.write("<table border=0 width=100%><tr bgcolor=\\"#ddffff\\">");
        !           495:     pWin.document.write("<td><b>Type</b></td><td><b>Include</b></td><td><b>Message</td></tr>");
        !           496: }
        !           497:     function displaySubject(msg) {
        !           498:     pWin.document.write("<tr bgcolor=\\"#ffffdd\\">");
        !           499:     pWin.document.write("<td>Subject</td>");
        !           500:     pWin.document.write("<td align=\\"center\\"><input name=\\"subchk\\" type=\\"checkbox\\" checked></td>");
        !           501:     pWin.document.write("<td><input name=\\"msgsub\\" type=\\"text\\" value=\\""+msg+" \\"size=\\"60\\" maxlength=\\"80\\"></td></tr>");
        !           502: }
        !           503: 
        !           504: function displaySavedMsg(ctr,msg) {
        !           505:     pWin.document.write("<tr bgcolor=\\"#ffffdd\\">");
        !           506:     pWin.document.write("<td align=\\"center\\">"+ctr+"</td>");
        !           507:     pWin.document.write("<td align=\\"center\\"><input name=\\"msgn"+ctr+"\\" type=\\"checkbox\\"></td>");
        !           508:     pWin.document.write("<td><input name=\\"msg"+ctr+"\\" type=\\"text\\" value=\\""+msg+" \\" size=\\"60\\" maxlength=\\"80\\"></td></tr>");
        !           509: }
        !           510: 
        !           511: function newMsg(newmsg) {
        !           512:     pWin.document.write("<tr bgcolor=\\"#ffffdd\\">");
        !           513:     pWin.document.write("<td align=\\"center\\">New</td>");
        !           514:     pWin.document.write("<td align=\\"center\\"><input name=\\"newmsgchk\\" type=\\"checkbox\\"></td>");
        !           515:     pWin.document.write("<td><input name=\\"newmsg\\" type=\\"text\\" value=\\""+newmsg+" \\" size=\\"60\\" maxlength=\\"80\\"></td></tr>");
        !           516: }
        !           517: 
        !           518: function msgTail() {
        !           519:     pWin.document.write("</table>");
        !           520:     pWin.document.write("</td></tr></table>&nbsp;");
        !           521:     pWin.document.write("<input type=\\"button\\" value=\\"Save\\" onClick=\\"javascript:checkInput()\\">&nbsp;&nbsp;");
        !           522:     pWin.document.write("<input type=\\"button\\" value=\\"Cancel\\" onClick=\\"self.close()\\"><br><br>");
        !           523:     pWin.document.write("</form>");
        !           524:     pWin.document.write("</body></html>");
        !           525: }
        !           526: 
        !           527: //====================== Script for keyword highlight options ==============
        !           528:   function kwhighlight() {
        !           529:     var kwclr    = document.SCORE.kwclr.value;
        !           530:     var kwsize   = document.SCORE.kwsize.value;
        !           531:     var kwstyle  = document.SCORE.kwstyle.value;
        !           532:     var redsel = "";
        !           533:     var grnsel = "";
        !           534:     var blusel = "";
        !           535:     if (kwclr=="red")   {var redsel="checked"};
        !           536:     if (kwclr=="green") {var grnsel="checked"};
        !           537:     if (kwclr=="blue")  {var blusel="checked"};
        !           538:     var sznsel = "";
        !           539:     var sz1sel = "";
        !           540:     var sz2sel = "";
        !           541:     if (kwsize=="0")  {var sznsel="checked"};
        !           542:     if (kwsize=="+1") {var sz1sel="checked"};
        !           543:     if (kwsize=="+2") {var sz2sel="checked"};
        !           544:     var synsel = "";
        !           545:     var syisel = "";
        !           546:     var sybsel = "";
        !           547:     if (kwstyle=="")    {var synsel="checked"};
        !           548:     if (kwstyle=="<i>") {var syisel="checked"};
        !           549:     if (kwstyle=="<b>") {var sybsel="checked"};
        !           550:     highlightCentral();
        !           551:     highlightbody('red','red',redsel,'0','normal',sznsel,'','normal',synsel);
        !           552:     highlightbody('green','green',grnsel,'+1','+1',sz1sel,'<i>','italic',syisel);
        !           553:     highlightbody('blue','blue',blusel,'+2','+2',sz2sel,'<b>','bold',sybsel);
        !           554:     highlightend();
1.35      ng        555:     return;
                    556:   }
                    557: 
1.36    ! ng        558: 
        !           559: function highlightCentral() {
        !           560:     hwdWin = window.open('', 'KeywordHighlightCentral', 'toolbar=no,location=no,scrollbars=no,width=400,height=300');
        !           561:     hwdWin.document.write("<html><head>");
        !           562:     hwdWin.document.write("<title>Highlight Central</title>");
        !           563: 
        !           564:     hwdWin.document.write("<script language=javascript>");
        !           565:     hwdWin.document.write("function updateChoice() {");
        !           566:     hwdWin.document.write("  opener.document.SCORE.kwclr.value = radioSelection(document.hlCenter.kwdclr);");
        !           567:     hwdWin.document.write("  opener.document.SCORE.kwsize.value = radioSelection(document.hlCenter.kwdsize);");
        !           568:     hwdWin.document.write("  opener.document.SCORE.kwstyle.value = radioSelection(document.hlCenter.kwdstyle);");
        !           569:     hwdWin.document.write("  self.close()");
        !           570:     hwdWin.document.write("}");
        !           571: 
        !           572:     hwdWin.document.write("function radioSelection(radioButton) {");
        !           573:     hwdWin.document.write("    var selection=null;");
        !           574:     hwdWin.document.write("    for (var i=0; i<radioButton.length; i++) {");
        !           575:     hwdWin.document.write("        if (radioButton[i].checked) {");
        !           576:     hwdWin.document.write("            selection=radioButton[i].value;");
        !           577:     hwdWin.document.write("            return selection;");
        !           578:     hwdWin.document.write("        }");
        !           579:     hwdWin.document.write("    }");
        !           580:     hwdWin.document.write("}");
        !           581: 
        !           582:     hwdWin.document.write("<");
        !           583:     hwdWin.document.write("/script>");
        !           584: 
        !           585:     hwdWin.document.write("</head><body bgcolor=white>");
        !           586: 
        !           587:     hwdWin.document.write("<form action=\\"inactive\\" name=\\"hlCenter\\">");
        !           588:     hwdWin.document.write("<font color=\\"green\\" size=+1>&nbsp;Keyword Highlight Options</font><br><br>");
        !           589: 
        !           590:     hwdWin.document.write("<table border=0 width=100%><tr><td bgcolor=\\"#777777\\">");
        !           591:     hwdWin.document.write("<table border=0 width=100%><tr bgcolor=\\"#ddffff\\">");
        !           592:     hwdWin.document.write("<td><b>Text Color</b></td><td><b>Font Size</b></td><td><b>Font Style</td></tr>");
        !           593: }
        !           594: 
        !           595: function highlightbody(clrval,clrtxt,clrsel,szval,sztxt,szsel,syval,sytxt,sysel) { 
        !           596:     hwdWin.document.write("<tr bgcolor=\\"#ffffdd\\">");
        !           597:     hwdWin.document.write("<td align=\\"left\\">");
        !           598:     hwdWin.document.write("<input name=\\"kwdclr\\" type=\\"radio\\" value=\\""+clrval+"\\" "+clrsel+">&nbsp;"+clrtxt+"</td>");
        !           599:     hwdWin.document.write("<td align=\\"left\\">");
        !           600:     hwdWin.document.write("<input name=\\"kwdsize\\" type=\\"radio\\" value=\\""+szval+"\\" "+szsel+">&nbsp;"+sztxt+"</td>");
        !           601:     hwdWin.document.write("<td align=\\"left\\">");
        !           602:     hwdWin.document.write("<input name=\\"kwdstyle\\" type=\\"radio\\" value=\\""+syval+"\\" "+sysel+">&nbsp;"+sytxt+"</td>");
        !           603:     hwdWin.document.write("</tr>");
        !           604:  }
        !           605: 
        !           606: function highlightend() { 
        !           607:     hwdWin.document.write("</table>");
        !           608:     hwdWin.document.write("</td></tr></table>&nbsp;");
        !           609:     hwdWin.document.write("<input type=\\"button\\" value=\\"Set Options\\" onClick=\\"javascript:updateChoice()\\">&nbsp;&nbsp;");
        !           610:     hwdWin.document.write("<input type=\\"button\\" value=\\"Cancel\\" onClick=\\"self.close()\\"><br><br>");
        !           611:     hwdWin.document.write("</form>");
        !           612:     hwdWin.document.write("</body></html>");
        !           613: }
        !           614: 
1.31      ng        615: </script>
1.36    ! ng        616: SUBJAVASCRIPT
1.34      ng        617: }
1.33      ng        618:   (my $url=$ENV{'form.url'})=~s-^http://($ENV{'SERVER_NAME'}|$ENV{'HTTP_HOST'})--;
1.35      ng        619:   if ($ENV{'form.student'} eq '') { &moreinfo($request,'Need student login id'); return ''; }
1.13      albertel  620:   my ($uname,$udom) = &finduser($ENV{'form.student'});
1.35      ng        621:   if ($uname eq '') { &moreinfo($request,'Unable to find student'); return ''; }
1.32      ng        622: 
                    623:   my $symb=($ENV{'form.symb'} ne '' ? $ENV{'form.symb'} : (&Apache::lonnet::symbread($url)));
1.13      albertel  624:   if ($symb eq '') { $request->print("Unable to handle ambiguous references:$url:."); return ''; }
1.34      ng        625:   my $last = ($ENV{'form.lastSub'} eq 'last' ? 'last' : '');
1.31      ng        626: #
                    627: # header info
1.34      ng        628:   if ($counter == 0) {
1.36    ! ng        629:       $request->print('<h2>&nbsp;<font color="#339933">Submission Record</font></h2>'.
        !           630: 		      '<font size=+1>&nbsp;<b>Resource: </b>'.$url.'</font>');
1.34      ng        631:   }
                    632: 
1.31      ng        633: #
1.34      ng        634: # option to display problem, only once else it cause problems with the form later since the problem has a form.
                    635:   if ($ENV{'form.vProb'} eq 'yes' && $counter == 0) {
1.30      ng        636:       my $rendered=&Apache::loncommon::get_student_view($symb,$uname,$udom,
1.17      albertel  637: 						   $ENV{'request.course.id'});
1.30      ng        638:       my $companswer=&Apache::loncommon::get_student_answers($symb,$uname,$udom,
                    639: 							 $ENV{'request.course.id'});
1.34      ng        640:       my $result.='<table border="0" width="100%"><tr><td bgcolor="#777777">';
1.33      ng        641:       $result.='<table border="0" width="100%"><tr><td bgcolor="#e6ffff">';
1.31      ng        642:       $result.='<b>Student\'s view of the problem</b></td></tr><tr><td bgcolor="#ffffff">'.$rendered.'<br />';
1.30      ng        643:       $result.='<b>Correct answer:</b><br />'.$companswer;
                    644:       $result.='</td></tr></table>';
                    645:       $result.='</td></tr></table><br />';
1.34      ng        646:       $request->print($result);
                    647:   }
                    648: #
                    649: # beginning of form
                    650:   if ($counter == 0) {
1.35      ng        651:       my %keyhash = &Apache::lonnet::get
                    652:                 ('nohist_handgrade',[$symb.'_keywords'],
                    653:                  $ENV{'course.'.$ENV{'request.course.id'}.'.domain'},
                    654: 		 $ENV{'course.'.$ENV{'request.course.id'}.'.num'});
                    655: 
1.34      ng        656:       $request->print('<form action="/adm/grades" method="post" name="SCORE">'."\n".
1.35      ng        657: 		      '<input type="hidden" name="symb"       value="'.$symb.'" />'."\n".
                    658: 		      '<input type="hidden" name="url"        value="'.$url.'" />'."\n".
                    659: 		      '<input type="hidden" name="vProb"      value="'.$ENV{'form.vProb'}.'" />'."\n".
                    660: 		      '<input type="hidden" name="lastSub"    value="'.$last.'" />'."\n".
                    661: 		      '<input type="hidden" name="section"    value="'.$ENV{'form.section'}.'">'."\n".
1.34      ng        662: 		      '<input type="hidden" name="submitonly" value="'.$ENV{'form.submitonly'}.'">'."\n".
1.35      ng        663: 		      '<input type="hidden" name="response"   value="'.$ENV{'form.response'}.'">'."\n".
                    664: 		      '<input type="hidden" name="handgrade"  value="'.$ENV{'form.handgrade'}.'">'."\n".
                    665: 		      '<input type="hidden" name="command"    value="handgrade" />'."\n".
                    666: 		      '<input type="hidden" name="keywords"   value="'.$keyhash{$symb.'_keywords'}.'" />'."\n".
1.36    ! ng        667: 		      '<input type="hidden" name="kwclr"      value="blue" />'."\n".
        !           668: 		      '<input type="hidden" name="kwsize"     value="0" />'."\n".
        !           669: 		      '<input type="hidden" name="kwstyle"    value="" />'."\n".
        !           670: 		      '<input type="hidden" name="msgsub"     value="Problem title" />'."\n".
        !           671: 		      '<input type="hidden" name="savemsgN"   value="4" />'."\n".
        !           672: 		      '<input type="hidden" name="savemsg1"   value="Good Job!" />'."\n".
        !           673: 		      '<input type="hidden" name="savemsg2"   value="Needs a better explanation." />'."\n".
        !           674: 		      '<input type="hidden" name="savemsg3"   value="Read the book before submitting such garbagge!" />'."\n".
        !           675: 		      '<input type="hidden" name="savemsg4"   value="You are nowhere close." />'."\n".
1.34      ng        676: 		      '<input type="hidden" name="NCT"'.
                    677: 		      ' value="'.($ENV{'form.NTSTU'} ne '' ? $ENV{'form.NTSTU'} : '1').'" />'."\n");
1.35      ng        678: 
                    679:       if ($ENV{'form.handgrade'} eq 'yes') {
                    680: 	  $request->print(<<KEYWORDS);
1.36    ! ng        681: &nbsp;<b>Keyword Options</b>&nbsp;
        !           682: <a href="javascript:keywords(document.SCORE.keywords)"; TARGET=_self>Keyword List</a>&nbsp; &nbsp;
        !           683: <a href="#" onMouseDown="javascript:getSel(); return false"
        !           684:  CLASS="page">Paste Selection to Keyword list</a> (requires N4+ and IE5+)&nbsp; &nbsp;
        !           685: <a href="javascript:kwhighlight()"; TARGET=_self>Keyword Highlight Attribute</a><br /><br />
1.35      ng        686: KEYWORDS
                    687:       }
1.30      ng        688:   }
1.35      ng        689: 
1.34      ng        690: #
                    691: # Student info
                    692:   $request->print(($counter == 0 ? '' : '<br /><hr><br />'));
1.36    ! ng        693:   my $fullname = ($ENV{'form.fullname'} ne '' ? $ENV{'form.fullname'} : &get_fullname($uname,$udom));
1.34      ng        694:   my $result.='<table border="0"><tr><td><b>Username: </b>'.$uname.
1.36    ! ng        695:       '</td><td><b>Fullname: </b>'.$fullname.
        !           696: #	  ($ENV{'form.fullname'} ne '' ? $ENV{'form.fullname'} : &get_fullname($uname,$udom)).
1.34      ng        697: 	      '</td><td><b>Domain: </b>'.$udom.'</td></tr>';
1.35      ng        698:   if ($ENV{'form.handgrade'} eq 'yes') {
                    699:       my $subonly = &get_last_submission($symb,$uname,$udom,$ENV{'request.course.id'});
                    700:       my ($classlist) = &getclasslist('all','0');
                    701:       my @collaborators;
                    702:       foreach ( sort(@{ $$classlist{'all'} }) ) {
                    703: 	  my ($sname,$sdom) = split(/:/);
                    704: 	  push @collaborators,$sname if (grep /\b$sname(\b|\.)/i,$subonly);
                    705:       }
                    706:       push @collaborators,'leede','carlandmm','freyniks'; # as a test to display collaborators.
                    707:       if (scalar(@collaborators) != 0) {
                    708: 	  $result.='<tr><td colspan=3><b>Collaborators: </b>';
                    709: 	  foreach (@collaborators) {
                    710: 	      $result.=$_.' ('.&get_fullname($_,$udom).') &nbsp; &nbsp;';
                    711: 	  }
                    712: 	  $result.='</td></tr>'."\n";
                    713: 	  $result.='<input type="hidden" name="collaborator'.$counter.
                    714: 	      '" value="'.(join ':',@collaborators).'" />'."\n";
                    715:       }
                    716:   }
1.36    ! ng        717:   $result.='</table>'."\n";
1.34      ng        718:   $request->print($result);
                    719: #
                    720: # print student answer
1.35      ng        721:   $request->print(&Apache::loncommon::get_previous_attempt($symb,$uname,$udom,
                    722: 							   $ENV{'request.course.id'},$last,
                    723: 							   '.submission','Apache::grades::keywords_highlight'));
                    724: #
1.34      ng        725: #
                    726:   my $wgt    = &Apache::lonnet::EXT('resource.partid.weight',$symb,$udom,$uname);
                    727:   my $wgtmsg = ($wgt > 0 ? '(problem weight)' : '<font color="red">problem weight assigned by computer</font>');
                    728:   $wgt       = ($wgt > 0 ? $wgt : '1');
                    729:   my %record = &Apache::lonnet::restore($symb,$ENV{'request.course.id'},$udom,$uname);
                    730:   my $score  = ($record{'resource.0.awarded'} eq '' ? '' : $record{'resource.0.awarded'}*$wgt);
1.18      albertel  731: 
1.34      ng        732: #
                    733: # display grading options
                    734:   $result='<input type="hidden" name="WGT'.$counter.'" value="'.$wgt.'" />'.
1.33      ng        735:       '<input type="hidden" name="unamedom'.$counter.'" value="'.$uname.':'.$udom.'" />'."\n";
1.36    ! ng        736:   $result.='<input type="hidden" name="newmsg'.$counter.'" value="" />'."\n".
        !           737:   $result.='<input type="hidden" name="includemsg'.$counter.'" value="" />'."\n".
1.32      ng        738:   $result.='<table border="0"><tr><td><b>Points</b></td><td>';
1.31      ng        739:   my $ctr = 0;
1.34      ng        740: 
                    741:   $result.='<table border="0"><tr>';  # display radio buttons in a nice table with 10 across
1.32      ng        742:   while ($ctr<=$wgt) {
1.34      ng        743:       $result.= '<td><input type="radio" name="RADVAL'.$counter.'" '.
1.33      ng        744: 	  'onclick="javascript:writeBox(this.form.GRADE_BOX'.$counter.
1.34      ng        745: 	    ',this.form.GRADE_SEL'.$counter.','.$ctr.','.$wgt.')" '.
                    746: 		($score eq $ctr ? 'checked':'').' /> '.$ctr."</td>\n";
                    747:       $result.=(($ctr+1)%10 == 0 ? '</tr><tr>' : '');
1.31      ng        748:       $ctr++;
                    749:   }
1.34      ng        750:   $result.='</tr></table>';
                    751: 
1.31      ng        752:   $result.='</td><td>&nbsp;<b>or</b>&nbsp;</td>';
1.33      ng        753:   $result.='<td><input type="text" name="GRADE_BOX'.$counter.'"'.
1.32      ng        754:       ($score ne ''? ' value = "'.$score.'"':'').' size="4" '.
1.33      ng        755:        'onChange="javascript:updateRadio(this.form.RADVAL'.$counter.
                    756:         ',this.form.GRADE_BOX'.$counter.
                    757: 	   ',this.form.GRADE_SEL'.$counter.',\''.$wgt.'\')" /></td>'."\n";
1.34      ng        758:   $result.='<td>/'.$wgt.' '.$wgtmsg.' </td><td>';
1.33      ng        759: 
                    760:   foreach my $part (&getpartlist($url)) {
                    761:     my ($temp,$part,$type)=split(/_/,$part);
                    762:     if ($type eq 'solved') {
                    763:       my ($status,$foo)=split(/_/,$record{"resource.$part.$type"},2);
1.34      ng        764:       $status = 'partial' if ($foo =~ /partially/);
                    765:       $status = 'nothing' if ($status eq '');
1.33      ng        766:       $result.='<select name="GRADE_SEL'.$counter.'">'."\n";
                    767:       my $optsel = '<option>correct</option><option>incorrect</option>'.
                    768: 	  '<option>excused</option><option>ungraded</option>'.
                    769: 	      '<option>partial</option><option>nothing</option>'."\n";
                    770:       $optsel =~ s/<option>$status/<option selected="on">$status/;
                    771:       $result.=$optsel;
1.36    ! ng        772:       $result.="</select>&nbsp&nbsp\n";
        !           773:       $result.='<a href="javascript:msgCenter(document.SCORE,'.$counter.
        !           774: 	  ',\''.$fullname.'\')"; TARGET=_self>Compose Message</a></td></tr>'."\n";
1.33      ng        775:     }
                    776:   }
1.34      ng        777:   $result.='</table>';
                    778:   $request->print($result);
                    779: #
                    780: # print end of form
                    781:   if ($counter == $total) {
                    782:       my $endform.='<table border="0"><tr><td><input type="submit" name="gradeOpt" value="Save & Next" />';
                    783:       my $ntstu ='<select name="NTSTU">'.
                    784: 	  '<option>1</option><option>2</option>'.
                    785: 	      '<option>3</option><option>5</option>'.
                    786: 		  '<option>7</option><option>10</option></select>'."\n";
                    787:       my $nsel = ($ENV{'form.NTSTU'} ne '' ? $ENV{'form.NTSTU'} : '1');
                    788:       $ntstu =~ s/<option>$nsel/<option selected="on">$nsel/;
                    789:       $endform.=$ntstu.'student(s) &nbsp;&nbsp;';
                    790:       $endform.='<input type="submit" name="gradeOpt" value="Next" />&nbsp';
                    791:       $endform.='<input type="submit" name="gradeOpt" value="Previous" />&nbsp';
                    792:       $endform.='(Next and Previous do not save the scores.)';
                    793:       $endform.='</td><tr></table></form>';
                    794:       $request->print($endform);
                    795:   }
                    796:   return '';
1.32      ng        797: }
1.30      ng        798: 
1.35      ng        799: sub get_last_submission {
                    800:   my ($symb,$username,$domain,$course)=@_;
                    801:   if ($symb) {
                    802:      my (%returnhash)=&Apache::lonnet::restore($symb,$course,$domain,$username);
                    803:      if ($returnhash{'version'}) {
                    804: 	my %lasthash=();
                    805: 	my $version;
                    806: 	for ($version=1;$version<=$returnhash{'version'};$version++) {
                    807: 	  foreach (sort(split(/\:/,$returnhash{$version.':keys'}))) {
                    808: 	      $lasthash{$_}=$returnhash{$version.':'.$_};
                    809: 	  }
                    810:         }
                    811: 	foreach ((keys %lasthash)) {
                    812: 	    if ($_ =~ /\.submission$/) {return $lasthash{$_}}
                    813: 	}
                    814: 	return '';
                    815:     }
                    816:   }
                    817: }
                    818: 
                    819: sub keywords_highlight {
                    820:   my $string  = shift;
                    821:   (my $url=$ENV{'form.url'})=~s-^http://($ENV{'SERVER_NAME'}|$ENV{'HTTP_HOST'})--;
                    822:   my $symb=($ENV{'form.symb'} ne '' ? $ENV{'form.symb'} : (&Apache::lonnet::symbread($url)));
                    823:   my %keyhash = &Apache::lonnet::get
                    824:       ('nohist_handgrade',[$symb.'_keywords'],
                    825:        $ENV{'course.'.$ENV{'request.course.id'}.'.domain'},
                    826:        $ENV{'course.'.$ENV{'request.course.id'}.'.num'});
                    827:   my @keylist = split(/[,\s+]/,$keyhash{$symb.'_keywords'});
                    828:   foreach my $word (@keylist) {
                    829:       next if ($word eq '');
                    830:       $string =~ s/\b$word(\b|\.)/\<font color\=red\>$word\<\/font\>/gi;
                    831:   }
                    832:   return $string;
                    833: }
                    834: 
1.32      ng        835: sub processHandGrade {
1.34      ng        836:   my ($request) = shift;
1.33      ng        837:   my $url    = $ENV{'form.url'};
                    838:   my $symb   = $ENV{'form.symb'};
                    839:   my $button = $ENV{'form.gradeOpt'};
                    840:   my $ngrade = $ENV{'form.NCT'};
                    841:   my $ntstu  = $ENV{'form.NTSTU'};
1.35      ng        842:   my $keywords= $ENV{'form.keywords'};
1.36    ! ng        843:   my $newmsg0  = $ENV{'form.newmsg0'};
        !           844:   print "newmsg0=$newmsg0<br>";
        !           845:   my $newmsg1  = $ENV{'form.newmsg1'};
        !           846:   print "newmsg1=$newmsg1<br>";
        !           847:   my $oldmsg1  = $ENV{'form.savemsg1'};
        !           848:   print "oldmsg1=$oldmsg1<br>";
        !           849:   my $oldmsg2  = $ENV{'form.savemsg2'};
        !           850:   print "oldmsg1=$oldmsg2<br>";
        !           851:   my $oldmsg3  = $ENV{'form.savemsg3'};
        !           852:   print "oldmsg1=$oldmsg3<br>";
        !           853:   my $oldmsg4  = $ENV{'form.savemsg4'};
        !           854:   print "oldmsg1=$oldmsg4<br>";
        !           855:   my $messages  = $ENV{'form.includemsg0'};
        !           856:   print "messages=$messages<br>";
        !           857:  if ($keywords ne '') {
1.35      ng        858:       my $crsname = $ENV{'course.'.$ENV{'request.course.id'}.'.num'};
                    859:       my $crsdom  = $ENV{'course.'.$ENV{'request.course.id'}.'.domain'};
1.36    ! ng        860: #      my $putresult = &Apache::lonnet::put
        !           861: #                ('nohist_handgrade',{$symb.'_keywords' => $keywords},
        !           862: #                 $crsdom,$crsname);
1.35      ng        863:   }
1.33      ng        864:   my (@parts) = sort(&getpartlist($url));
1.34      ng        865: 
1.33      ng        866:   if ($button eq 'Save & Next') {
                    867:       my $ctr = 0;
                    868:       while ($ctr < $ENV{'form.NCT'}) {
                    869: 	  my ($uname,$udom) = split(/:/,$ENV{'form.unamedom'.$ctr});
1.34      ng        870: 	  &saveHandGrade($url,$symb,$uname,$udom,$ctr,@parts);
1.35      ng        871: 	  if ($ENV{'form.collaborator'.$ctr}) {
                    872: 	      my (@collaborators) = split(/:/,$ENV{'form.collaborator'.$ctr});
                    873: 	      foreach (@collaborators) {
                    874: 		  &saveHandGrade($url,$symb,$_,$udom,$ctr,@parts);
                    875: 	      }
                    876: 	  }
1.33      ng        877: 	  $ctr++;
                    878:       }
                    879:   }
                    880:   my $firststu = $ENV{'form.unamedom0'};
                    881:   my $laststu  = $ENV{'form.unamedom'.($ngrade-1)};
                    882: 
1.35      ng        883:   my ($classlist) = &getclasslist($ENV{'form.section'},'0');
1.33      ng        884: 
                    885:   my (@nextlist,@prevlist);
1.34      ng        886:   my ($nextflg,$prevflg,$ctr,$ctprev) = (0,0,0,0);
1.35      ng        887:   foreach my $student ( sort(@{ $$classlist{$ENV{'form.section'}} }) ) {
1.33      ng        888:       my ($uname,$udom) = split(/:/,$student);
                    889:       if ($nextflg == 1 && $button =~ /Next$/) {
1.34      ng        890: 	  push @nextlist,$uname if ($ctr < $ntstu);
1.33      ng        891: 	  $ctr++;
                    892:       }
                    893:       $nextflg = 1 if ($student eq $laststu);
                    894:       $prevflg = 1 if ($student eq $firststu);
1.34      ng        895:       if ($prevflg == 0 && $button eq 'Previous') {
                    896: 	  push @prevlist,$uname;
                    897: 	  $ctprev++;
                    898:       }
1.33      ng        899:   }
1.34      ng        900:   if ($button eq 'Previous') {
                    901:       if ($ctprev <= $ntstu) {
                    902: 	  @nextlist = @prevlist;
                    903:       } else {
                    904: 	  my $idx = 0;
                    905: 	  my $start = $ctprev - $ntstu;
                    906: 	  while ($idx < $ntstu) {
                    907: 	      $nextlist[$idx] = $prevlist[$start+$idx];
                    908: 	      $idx++;
                    909: 	  }
                    910:       }
                    911:   }
                    912:   $ctr = 0;
                    913:   my $total = scalar(@nextlist)-1;
1.33      ng        914:   foreach my $student (@nextlist) {
                    915:       $ENV{'form.student'} = $student;
1.34      ng        916:       &submission($request,$ctr,$total);
                    917:       $ctr++;
1.33      ng        918:   }
                    919: 
                    920:   return 'The End';
                    921: }
                    922: 
                    923: sub saveHandGrade {
1.34      ng        924:   my ($url,$symb,$stuname,$domain,$newflg,@parts) = @_;
1.33      ng        925:   my %record=&Apache::lonnet::restore($symb,$ENV{'request.course.id'},$domain,$stuname);
1.32      ng        926:   my %newrecord;
                    927: 
                    928:   foreach my $part (@parts) {
1.31      ng        929:     my ($temp,$part,$type)=split(/_/,$part);
1.32      ng        930:     my $oldscore=$record{"resource.$part.$type"};
1.34      ng        931:     my $newscore;
                    932:     if ($type eq 'awarded' && $newflg >= 0) {
1.35      ng        933: 	my $pts    = ($ENV{'form.GRADE_BOX'.$newflg} ne '' ? 
                    934: 		      $ENV{'form.GRADE_BOX'.$newflg} : $ENV{'form.RADVAL'.$newflg});
1.34      ng        935: 	my $wgt    = $ENV{'form.WGT'.$newflg};
                    936: #	my $sel    = $ENV{'form.GRADE_SEL'.$newflg};
                    937: 	$newscore  = $pts/$wgt if ($wgt != 0);
                    938:     }
1.31      ng        939:     if ($type eq 'solved') {
1.34      ng        940:       $newscore = $ENV{'form.GRADE_SEL'.$newflg} if ($newflg >= 0);
1.32      ng        941:       my $update=0;
                    942:       if ($newscore eq 'nothing' ) {
                    943: 	if ($oldscore ne '') {
                    944: 	  $update=1;
                    945: 	  $newscore = '';
                    946: 	}
                    947:       } elsif ($oldscore !~ m/^$newscore/) {
                    948: 	$update=1;
                    949: 	if ($newscore eq 'correct')   { $newscore = 'correct_by_override'; }
                    950: 	if ($newscore eq 'incorrect') { $newscore = 'incorrect_by_override'; }
                    951: 	if ($newscore eq 'excused')   { $newscore = 'excused'; }
                    952: 	if ($newscore eq 'ungraded')  { $newscore = 'ungraded_attempted'; }
1.34      ng        953: 	if ($newscore eq 'partial')   { $newscore = 'correct_partially_by_override'; }
1.32      ng        954:       }
                    955:       if ($update) { $newrecord{"resource.$part.$type"}=$newscore; }
                    956:     } else {
                    957:       if ($oldscore ne $newscore) {
                    958: 	$newrecord{"resource.$part.$type"}=$newscore;
1.33      ng        959:       }
1.32      ng        960:     }
1.34      ng        961:   }
                    962:   if ( scalar(keys(%newrecord)) > 0 ) {
1.33      ng        963:       $newrecord{'resource.regrader'}="$ENV{'user.name'}:$ENV{'user.domain'}";
1.35      ng        964: #      while (my ($k,$v) = each %newrecord) {
                    965: #	 print "k=$k:v=$v:<br>\n";
                    966: #      }
                    967: #      print "symb=$symb,courseid=$ENV{'request.course.id'},dom=$domain,name=$stuname<br>";
1.34      ng        968: #     &Apache::lonnet::cstore(\%newrecord,$symb,$ENV{'request.course.id'},$domain,$stuname);
1.31      ng        969:   }
1.34      ng        970:   return '';
1.2       albertel  971: }
                    972: 
1.26      albertel  973: sub get_symb_and_url {
1.34      ng        974:   my ($request) = @_;
                    975:   (my $url=$ENV{'form.url'}) =~ s-^http://($ENV{'SERVER_NAME'}|$ENV{'HTTP_HOST'})--;
1.32      ng        976:   my $symb=($ENV{'form.symb'} ne '' ? $ENV{'form.symb'} : (&Apache::lonnet::symbread($url)));
1.13      albertel  977:   if ($symb eq '') { $request->print("Unable to handle ambiguous references:$url:."); return ''; }
1.34      ng        978:   return ($symb,$url);
1.29      albertel  979: }
                    980: 
                    981: sub show_grading_menu_form {
                    982:   my ($symb,$url)=@_;
                    983:   my $result.='<form action="/adm/grades" method="post">'."\n".
                    984:     '<input type="hidden" name="symb" value="'.$symb.'" />'."\n".
                    985:       '<input type="hidden" name="url" value="'.$url.'" />'."\n".
                    986: 	'<input type="hidden" name="command" value="gradingmenu" />'."\n".
                    987: 	  '<input type="submit" name="submit" value="Grading Menu" />'."\n".
                    988: 	    '</form>'."\n";
                    989:   return $result;
                    990: }
                    991: 
1.26      albertel  992: sub gradingmenu {
                    993:   my ($request) = @_;
                    994:   my ($symb,$url)=&get_symb_and_url($request);
                    995:   if (!$symb) {return '';}
1.35      ng        996:   my $allkeys = &Apache::lonnet::metadata($url,'keys');
                    997:   my $handgrade = ($allkeys =~ /parameter_.*?_handgrade/ ? 'yes' : 'no');
                    998:   my ($responsetype,$foo) = split(/_/,&Apache::lonnet::metadata($url,'packages'));
1.28      ng        999: 
1.34      ng       1000:   my $result='<h2>&nbsp;<font color="#339933">Select a Grading Method</font></h2>';
1.35      ng       1001:   $result.='<table border="0">';
                   1002:   $result.='<tr><td><font size=+1><b>Resource: </b></font></td>'.
                   1003:       '<td><font size=+1>'.$url.'</font></td></tr>';
                   1004:   $result.='<tr><td><font size=+1><b>Type: </b></font></td>'.
                   1005:       '<td><font size=+1>'.$responsetype.'&nbsp;&nbsp;<b>Handgrade: </b>'.$handgrade.'</font></td></tr>';
                   1006:   $result.='</table>';
1.34      ng       1007: 
                   1008:   $result.=&view_edit_entire_class_form($symb,$url).'<br />';
                   1009:   $result.=&upcsvScores_form($symb,$url).'<br />';
1.35      ng       1010:   $result.=&viewGradeaStu_form($symb,$url,$responsetype,$handgrade).'<br />';
1.34      ng       1011:   $result.=&verifyReceipt_form($symb,$url);
                   1012:   return $result;
                   1013: }
                   1014: 
                   1015: sub view_edit_entire_class_form {
                   1016:   my ($symb,$url)=@_;
                   1017:   my $result.='<table width=100% border=0><tr><td bgcolor=#777777>'."\n";
                   1018:   $result.='<table width=100% border=0><tr bgcolor="#e6ffff"><td>'."\n";
                   1019:   $result.='&nbsp;<b>View/Grade Entire Class</b></td></tr>'."\n";
1.28      ng       1020:   $result.='<tr bgcolor=#ffffe6><td>'."\n";
1.26      albertel 1021:   $result.='<form action="/adm/grades" method="post">'."\n".
1.34      ng       1022:     '<input type="hidden" name="symb" value="'.$symb.'" />'."\n".
1.26      albertel 1023:       '<input type="hidden" name="url" value="'.$url.'" />'."\n".
1.34      ng       1024: 	  '<input type="hidden" name="command" value="viewgrades" />'."\n";
                   1025:   $result.='&nbsp;<b>Display students who has: </b>'.
                   1026:       '<input type="radio" name="submitonly" value="yes" checked> submitted'.
                   1027: 	  '<input type="radio" name="submitonly" value="all"> everybody <br /><br />';
                   1028:   $result.='&nbsp;<input type="submit" name="submit" value="View/Grade" /></form>'."\n";
                   1029:   $result.='</td></tr></table>'."\n";
                   1030:   $result.='</td></tr></table>'."\n";
                   1031:   return $result;
                   1032: }
                   1033: 
                   1034: sub upcsvScores_form {
                   1035:   my ($symb,$url) = @_;
                   1036:   if (!$symb) {return '';}
                   1037:   my $result.='<table width=100% border=0><tr><td bgcolor=#777777>'."\n";
                   1038:   $result.='<table width=100% border=0><tr bgcolor="#e6ffff"><td>'."\n";
                   1039:   $result.='&nbsp;<b>Specify a file containing the class scores for above resource</b></td></tr>'."\n";
                   1040:   $result.='<tr bgcolor=#ffffe6><td>'."\n";
                   1041:   my $upfile_select=&Apache::loncommon::upfile_select_html();
                   1042:   $result.=<<ENDUPFORM;
                   1043: <form method="post" enctype="multipart/form-data" action="/adm/grades" name="gradesupload">
                   1044: <input type="hidden" name="symb" value="$symb" />
                   1045: <input type="hidden" name="url" value="$url" />
                   1046: <input type="hidden" name="command" value="csvuploadmap" />
                   1047: $upfile_select
                   1048: <br />&nbsp;<input type="submit" name="submit" value="Upload Grades" />
                   1049: </form>
                   1050: ENDUPFORM
                   1051:   $result.='</td></tr></table>'."\n";
                   1052:   $result.='</td></tr></table>'."\n";
                   1053:   return $result;
                   1054: }
                   1055: 
                   1056: sub viewGradeaStu_form {
1.35      ng       1057:   my ($symb,$url,$response,$handgrade) = @_;
1.34      ng       1058:   my ($classlist,$sections) = &getclasslist('all','0');
                   1059:   my $result.='<table width=100% border=0><tr><td bgcolor=#777777>'."\n";
                   1060:   $result.='<table width=100% border=0><tr bgcolor="#e6ffff"><td>'."\n";
                   1061:   $result.='&nbsp;<b>View/Grade an Individual Student\'s Submission</b></td></tr>'."\n";
                   1062:   $result.='<tr bgcolor=#ffffe6><td>'."\n";
1.26      albertel 1063:   $result.='<form action="/adm/grades" method="post">'."\n".
                   1064:      '<input type="hidden" name="symb" value="'.$symb.'" />'."\n".
                   1065:       '<input type="hidden" name="url" value="'.$url.'" />'."\n".
1.35      ng       1066:        '<input type="hidden" name="response" value="'.$response.'" />'."\n".
                   1067:         '<input type="hidden" name="handgrade" value="'.$handgrade.'" />'."\n".
                   1068: 	 '<input type="hidden" name="command" value="submission" />'."\n";
1.34      ng       1069: 
                   1070:   $result.='&nbsp;<b>Select section:</b> <select name="section">'."\n";
                   1071:   foreach my $section (sort (@$sections)) {
                   1072:       $result.= '<option>'.$section.'</option>'."\n";
                   1073:   }
                   1074:   $result.= '<option selected="on">all</select>'."\n";
                   1075:   $result.='&nbsp;&nbsp;<b>Display students who has: </b>'.
                   1076:       '<input type="radio" name="submitonly" value="yes" checked> submitted'.
                   1077: 	  '<input type="radio" name="submitonly" value="all"> everybody <br />';
1.35      ng       1078:   $result.='&nbsp;(Section "no" implies the students were not assigned a section.)<br />' 
                   1079:       if (grep /no/,@$sections);
1.34      ng       1080: 
                   1081:   $result.='<br />&nbsp;<input type="submit" name="submit" value="View/Grade" />'."\n".
                   1082:       '</form>'."\n";
                   1083:   $result.='</td></tr></table>'."\n";
                   1084:   $result.='</td></tr></table>'."\n";
                   1085:   return $result;
                   1086: }
                   1087: 
                   1088: sub verifyReceipt_form {
                   1089:   my ($symb,$url) = @_;
                   1090:   my $cdom=$ENV{"course.$ENV{'request.course.id'}.domain"};
                   1091:   my $cnum=$ENV{"course.$ENV{'request.course.id'}.num"};
                   1092:   my $hostver=unpack("%32C*",$Apache::lonnet::perlvar{'lonHostID'});
                   1093: 
                   1094:   my $result.='<table width=100% border=0><tr><td bgcolor=#777777>'."\n";
                   1095:   $result.='<table width=100% border=0><tr><td bgcolor=#e6ffff>'."\n";
                   1096:   $result.='&nbsp;<b>Verify a Submission Receipt Issued by this Server</td></tr>'."\n";
                   1097:   $result.='<tr bgcolor=#ffffe6><td>'."\n";
                   1098:   $result.='<form action="/adm/grades" method="post">'."\n";
                   1099:   $result.='&nbsp;<tt>'.$hostver.'-<input type="text" name="receipt" size="4"></tt><br />'."\n";
                   1100:   $result.='&nbsp;<input type="submit" name="submit" value="Verify Receipt">'."\n";
                   1101:   $result.='<input type="hidden" name="command" value="verify">'."\n";
                   1102:   if ($ENV{'form.url'}) {
                   1103:       $result.='<input type="hidden" name="url" value="'.$ENV{'form.url'}.'" />';
                   1104:   }
                   1105:   if ($ENV{'form.symb'}) {
                   1106:       $result.='<input type="hidden" name="symb" value="'.$ENV{'form.symb'}.'" />';
                   1107:   }
                   1108:   $result.='</form>';
1.28      ng       1109:   $result.='</td></tr></table>'."\n";
                   1110:   $result.='</td></tr></table>'."\n";
1.26      albertel 1111:   return $result;
                   1112: }
                   1113: 
                   1114: sub viewgrades {
                   1115:   my ($request) = @_;
                   1116:   my $result='';
                   1117: 
                   1118:   #get resource reference
                   1119:   my ($symb,$url)=&get_symb_and_url($request);
                   1120:   if (!$symb) {return '';}
1.13      albertel 1121:   #get classlist
                   1122:   my ($cdom,$cnum) = split(/_/,$ENV{'request.course.id'});
1.24      albertel 1123:   #print "Found $cdom:$cnum<br />";
1.34      ng       1124:   my ($classlist) = &getclasslist('all','0');
1.13      albertel 1125:   my $headerclr = '"#ccffff"';
                   1126:   my $cellclr = '"#ffffcc"';
                   1127: 
                   1128:   #get list of parts for this problem
1.29      albertel 1129:   my (@parts) = sort(&getpartlist($url));
1.13      albertel 1130: 
1.28      ng       1131:   $request->print ("<h2><font color=\"#339933\">Manual Grading</font></h2>");
1.13      albertel 1132: 
                   1133:   #start the form
                   1134:   $result = '<form action="/adm/grades" method="post">'."\n".
1.16      albertel 1135:     '<input type="hidden" name="symb" value="'.$symb.'" />'."\n".
                   1136:       '<input type="hidden" name="url" value="'.$url.'" />'."\n".
1.13      albertel 1137: 	'<input type="hidden" name="command" value="editgrades" />'."\n".
                   1138: 	  '<input type="submit" name="submit" value="Submit Changes" />'."\n".
1.32      ng       1139: 	    '<table border=0><tr><td bgcolor="#777777">'."\n".
1.13      albertel 1140: 	     '<table border=0>'."\n".
1.28      ng       1141: 	      '<tr bgcolor='.$headerclr.'><td><b>Username</b></td><td><b>Name</b></td><td><b>Domain</b></td>'."\n";
1.29      albertel 1142:   foreach my $part (@parts) {
1.13      albertel 1143:      my $display=&Apache::lonnet::metadata($url,$part.'.display');
                   1144:      if  (!$display) { $display = &Apache::lonnet::metadata($url,$part.'.name'); }
1.28      ng       1145:      $result.='<td><b>'.$display.'</b></td>'."\n";
1.30      ng       1146:   }
1.28      ng       1147:   $result.='</tr>';
1.13      albertel 1148:   #get info for each student
1.34      ng       1149:   foreach my $student ( sort(@{ $$classlist{'all'} }) ) {
1.31      ng       1150:     my $display=&viewstudentgrade($url,$symb,$ENV{'request.course.id'},$student,@parts);
                   1151: #      print "ID=$ENV{'request.course.id'}:STU=$student:DIS=$display:<br>\n";
1.13      albertel 1152:     $result.=&viewstudentgrade($url,$symb,$ENV{'request.course.id'},$student,@parts);
                   1153:   }
1.31      ng       1154:   $result.='</table></td></tr></table>';
                   1155:   $result.='<input type="submit" name="submit" value="Submit Changes" /></form>';
1.29      albertel 1156:   $result.=&show_grading_menu_form($symb,$url);
1.13      albertel 1157:   return $result;
1.5       albertel 1158: }
                   1159: 
                   1160: sub editgrades {
1.13      albertel 1161:   my ($request) = @_;
                   1162:   my $result='';
1.5       albertel 1163: 
1.13      albertel 1164:   my $symb=$ENV{'form.symb'};
                   1165:   if ($symb eq '') { $request->print("Unable to handle ambiguous references:$symb:$ENV{'form.url'}"); return ''; }
                   1166:   my $url=$ENV{'form.url'};
                   1167:   #get classlist
1.34      ng       1168: #  my ($cdom,$cnum) = split(/_/,$ENV{'request.course.id'});
1.24      albertel 1169:   #print "Found $cdom:$cnum<br />";
1.34      ng       1170:   my ($classlist) = &getclasslist('all','0');
1.13      albertel 1171: 
                   1172:   #get list of parts for this problem
                   1173:   my (@parts) = &getpartlist($url);
                   1174: 
                   1175:   $result.='<form action="/adm/grades" method="post">'."\n".
                   1176:     '<input type="hidden" name="symb" value="'.$symb.'" />'."\n".
                   1177:       '<input type="hidden" name="url" value="'.$url.'" />'."\n".
                   1178: 	'<input type="hidden" name="command" value="viewgrades" />'."\n".
                   1179: 	  '<input type="submit" name="submit" value="See Grades" /> <br />'."\n";
                   1180: 
1.35      ng       1181:   foreach my $student ( sort(@{ $$classlist{'all'} }) ) {
1.13      albertel 1182:     $result.=&setstudentgrade($url,$symb,$ENV{'request.course.id'},$student,@parts);
                   1183:   }
1.5       albertel 1184: 
1.13      albertel 1185:   $result.='<input type="submit" name="submit" value="See Grades" /></table></form>';
                   1186:   return $result;
1.5       albertel 1187: }
                   1188: 
1.27      albertel 1189: sub csvupload_javascript_reverse_associate {
                   1190:   return(<<ENDPICK);
                   1191:   function verify(vf) {
                   1192:     var foundsomething=0;
                   1193:     var founduname=0;
                   1194:     var founddomain=0;
                   1195:     for (i=0;i<=vf.nfields.value;i++) {
                   1196:       tw=eval('vf.f'+i+'.selectedIndex');
                   1197:       if (i==0 && tw!=0) { founduname=1; }
                   1198:       if (i==1 && tw!=0) { founddomain=1; }
                   1199:       if (i!=0 && i!=1 && tw!=0) { foundsomething=1; }
                   1200:     }
                   1201:     if (founduname==0 || founddomain==0) {
                   1202:       alert('You need to specify at both the username and domain');
                   1203:       return;
                   1204:     }
                   1205:     if (foundsomething==0) {
                   1206:       alert('You need to specify at least one grading field');
                   1207:       return;
                   1208:     }
                   1209:     vf.submit();
                   1210:   }
                   1211:   function flip(vf,tf) {
                   1212:     var nw=eval('vf.f'+tf+'.selectedIndex');
                   1213:     var i;
                   1214:     for (i=0;i<=vf.nfields.value;i++) {
                   1215:       //can not pick the same destination field for both name and domain
                   1216:       if (((i ==0)||(i ==1)) && 
                   1217:           ((tf==0)||(tf==1)) && 
                   1218:           (i!=tf) &&
                   1219:           (eval('vf.f'+i+'.selectedIndex')==nw)) {
                   1220:         eval('vf.f'+i+'.selectedIndex=0;')
                   1221:       }
                   1222:     }
                   1223:   }
                   1224: ENDPICK
                   1225: }
                   1226: 
                   1227: sub csvupload_javascript_forward_associate {
                   1228:   return(<<ENDPICK);
                   1229:   function verify(vf) {
                   1230:     var foundsomething=0;
                   1231:     var founduname=0;
                   1232:     var founddomain=0;
                   1233:     for (i=0;i<=vf.nfields.value;i++) {
                   1234:       tw=eval('vf.f'+i+'.selectedIndex');
                   1235:       if (tw==1) { founduname=1; }
                   1236:       if (tw==2) { founddomain=1; }
                   1237:       if (tw>2) { foundsomething=1; }
                   1238:     }
                   1239:     if (founduname==0 || founddomain==0) {
                   1240:       alert('You need to specify at both the username and domain');
                   1241:       return;
                   1242:     }
                   1243:     if (foundsomething==0) {
                   1244:       alert('You need to specify at least one grading field');
                   1245:       return;
                   1246:     }
                   1247:     vf.submit();
                   1248:   }
                   1249:   function flip(vf,tf) {
                   1250:     var nw=eval('vf.f'+tf+'.selectedIndex');
                   1251:     var i;
                   1252:     //can not pick the same destination field twice
                   1253:     for (i=0;i<=vf.nfields.value;i++) {
                   1254:       if ((i!=tf) && (eval('vf.f'+i+'.selectedIndex')==nw)) {
                   1255:         eval('vf.f'+i+'.selectedIndex=0;')
                   1256:       }
                   1257:     }
                   1258:   }
                   1259: ENDPICK
                   1260: }
                   1261: 
1.26      albertel 1262: sub csvuploadmap_header {
                   1263:   my ($request,$symb,$url,$datatoken,$distotal)= @_;
                   1264:   my $result;
                   1265:   my $javascript;
                   1266:   if ($ENV{'form.upfile_associate'} eq 'reverse') {
1.27      albertel 1267:     $javascript=&csvupload_javascript_reverse_associate();
1.26      albertel 1268:   } else {
1.27      albertel 1269:     $javascript=&csvupload_javascript_forward_associate();
1.26      albertel 1270:   }
                   1271:   $request->print(<<ENDPICK);
                   1272: <form method="post" enctype="multipart/form-data" action="/adm/grades" name="gradesupload">
                   1273: <h3>Uploading Class Grades for resource $url</h3>
                   1274: <hr>
                   1275: <h3>Identify fields</h3>
                   1276: Total number of records found in file: $distotal <hr />
                   1277: Enter as many fields as you can. The system will inform you and bring you back
                   1278: to this page if the data selected is insufficient to run your class.<hr />
                   1279: <input type="button" value="Reverse Association" onClick="javascript:this.form.associate.value='Reverse Association';submit(this.form);" />
                   1280: <input type="hidden" name="associate"  value="" />
                   1281: <input type="hidden" name="phase"      value="three" />
                   1282: <input type="hidden" name="datatoken"  value="$datatoken" />
                   1283: <input type="hidden" name="fileupload" value="$ENV{'form.fileupload'}" />
                   1284: <input type="hidden" name="upfiletype" value="$ENV{'form.upfiletype'}" />
                   1285: <input type="hidden" name="upfile_associate" 
                   1286:                                        value="$ENV{'form.upfile_associate'}" />
                   1287: <input type="hidden" name="symb"       value="$symb" />
                   1288: <input type="hidden" name="url"        value="$url" />
                   1289: <input type="hidden" name="command"    value="csvuploadassign" />
                   1290: <hr />
                   1291: <script type="text/javascript" language="Javascript">
                   1292: $javascript
                   1293: </script>
                   1294: ENDPICK
                   1295:   return '';
                   1296: 
                   1297: }
                   1298: 
                   1299: sub csvupload_fields {
                   1300:   my ($url) = @_;
                   1301:   my (@parts) = &getpartlist($url);
1.27      albertel 1302:   my @fields=(['username','Student Username'],['domain','Student Domain']);
                   1303:   foreach my $part (sort(@parts)) {
1.26      albertel 1304:     my @datum;
                   1305:     my $display=&Apache::lonnet::metadata($url,$part.'.display');
1.27      albertel 1306:     my $name=$part;
1.26      albertel 1307:     if  (!$display) { $display = $name; }
                   1308:     @datum=($name,$display);
                   1309:     push(@fields,\@datum);
                   1310:   }
                   1311:   return (@fields);
                   1312: }
                   1313: 
                   1314: sub csvuploadmap_footer {
                   1315:   my ($request,$i,$keyfields) =@_;
                   1316:   $request->print(<<ENDPICK);
                   1317: </table>
                   1318: <input type="hidden" name="nfields" value="$i" />
                   1319: <input type="hidden" name="keyfields" value="$keyfields" />
                   1320: <input type="button" onClick="javascript:verify(this.form)" value="Assign Grades" /><br />
                   1321: </form>
                   1322: ENDPICK
                   1323: }
                   1324: 
                   1325: sub csvuploadmap {
                   1326:   my ($request)= @_;
                   1327:   my ($symb,$url)=&get_symb_and_url($request);
                   1328:   if (!$symb) {return '';}
                   1329:   my $datatoken;
                   1330:   if (!$ENV{'form.datatoken'}) {
                   1331:     $datatoken=&Apache::loncommon::upfile_store($request);
                   1332:   } else {
                   1333:     $datatoken=$ENV{'form.datatoken'};
                   1334:     &Apache::loncommon::load_tmp_file($request);
                   1335:   }
                   1336:   my @records=&Apache::loncommon::upfile_record_sep();
                   1337:   &csvuploadmap_header($request,$symb,$url,$datatoken,$#records+1);
                   1338:   my $i;
                   1339:   my $keyfields;
                   1340:   if (@records) {
                   1341:     my @fields=&csvupload_fields($url);
                   1342:     if ($ENV{'form.upfile_associate'} eq 'reverse') {	
                   1343:       &Apache::loncommon::csv_print_samples($request,\@records);
                   1344:       $i=&Apache::loncommon::csv_print_select_table($request,\@records,
                   1345: 						    \@fields);
                   1346:       foreach (@fields) { $keyfields.=$_->[0].','; }
                   1347:       chop($keyfields);
                   1348:     } else {
                   1349:       unshift(@fields,['none','']);
                   1350:       $i=&Apache::loncommon::csv_samples_select_table($request,\@records,
                   1351: 						      \@fields);
                   1352:       my %sone=&Apache::loncommon::record_sep($records[0]);
                   1353:       $keyfields=join(',',sort(keys(%sone)));
                   1354:     }
                   1355:   }
                   1356:   &csvuploadmap_footer($request,$i,$keyfields);
                   1357:   return '';
1.27      albertel 1358: }
                   1359: 
                   1360: sub csvuploadassign {
                   1361:   my ($request)= @_;
                   1362:   my ($symb,$url)=&get_symb_and_url($request);
                   1363:   if (!$symb) {return '';}
                   1364:   &Apache::loncommon::load_tmp_file($request);
                   1365:   my @gradedata=&Apache::loncommon::upfile_record_sep();
                   1366:   my @keyfields = split(/\,/,$ENV{'form.keyfields'});
                   1367:   my %fields=();
                   1368:   for (my $i=0; $i<=$ENV{'form.nfields'}; $i++) {
                   1369:     if ($ENV{'form.upfile_associate'} eq 'reverse') {
                   1370:       if ($ENV{'form.f'.$i} ne 'none') {
                   1371: 	$fields{$keyfields[$i]}=$ENV{'form.f'.$i};
                   1372:       }
                   1373:     } else {
                   1374:       if ($ENV{'form.f'.$i} ne 'none') {
                   1375: 	$fields{$ENV{'form.f'.$i}}=$keyfields[$i];
                   1376:       }
                   1377:     }
                   1378:   }
                   1379:   $request->print('<h3>Assigning Grades</h3>');
                   1380:   my $courseid=$ENV{'request.course.id'};
1.34      ng       1381: #  my $cdom=$ENV{"course.$courseid.domain"};
                   1382: #  my $cnum=$ENV{"course.$courseid.num"};
                   1383:   my ($classlist) = &getclasslist('all','1');
1.29      albertel 1384:   my @skipped;
                   1385:   my $countdone=0;
                   1386:   foreach my $grade (@gradedata) {
                   1387:     my %entries=&Apache::loncommon::record_sep($grade);
                   1388:     my $username=$entries{$fields{'username'}};
                   1389:     my $domain=$entries{$fields{'domain'}};
1.34      ng       1390:     if (!exists($$classlist{"$username:$domain"})) {
1.29      albertel 1391:       push(@skipped,"$username:$domain");
                   1392:       next;
1.27      albertel 1393:     }
1.29      albertel 1394:     my %grades;
                   1395:     foreach my $dest (keys(%fields)) {
                   1396:       if ($dest eq 'username' || $dest eq 'domain') { next; }
                   1397:       if ($entries{$fields{$dest}} eq '') { next; }
                   1398:       my $store_key=$dest;
                   1399:       $store_key=~s/^stores/resource/;
                   1400:       $store_key=~s/_/\./g;
                   1401:       $grades{$store_key}=$entries{$fields{$dest}};
                   1402:     }
                   1403:     $grades{"resource.regrader"}="$ENV{'user.name'}:$ENV{'user.domain'}";
                   1404:     &Apache::lonnet::cstore(\%grades,$symb,$ENV{'request.course.id'},
                   1405: 			    $domain,$username);
                   1406:     $request->print('.');
                   1407:     $request->rflush();
                   1408:     $countdone++;
                   1409:   }
                   1410:   $request->print("<br />Stored $countdone students\n");
                   1411:   if (@skipped) {
                   1412:     $request->print('<br /><font size="+1"><b>Skipped Students</b></font><br />');
                   1413:     foreach my $student (@skipped) { $request->print("<br />$student"); }
1.27      albertel 1414:   }
1.29      albertel 1415:   $request->print(&view_edit_entire_class_form($symb,$url));
                   1416:   $request->print(&show_grading_menu_form($symb,$url));
                   1417:   return '';
1.26      albertel 1418: }
                   1419: 
1.2       albertel 1420: sub send_header {
1.13      albertel 1421:   my ($request)= @_;
                   1422:   $request->print(&Apache::lontexconvert::header());
1.6       albertel 1423: #  $request->print("
                   1424: #<script>
                   1425: #remotewindow=open('','homeworkremote');
                   1426: #remotewindow.close();
                   1427: #</script>"); 
1.13      albertel 1428:   $request->print('<body bgcolor="#FFFFFF">');
1.2       albertel 1429: }
                   1430: 
                   1431: sub send_footer {
1.13      albertel 1432:   my ($request)= @_;
1.2       albertel 1433:   $request->print('</body>');
                   1434:   $request->print(&Apache::lontexconvert::footer());
                   1435: }
                   1436: 
1.1       albertel 1437: sub handler {
1.13      albertel 1438:   my $request=$_[0];
                   1439: 
                   1440:   if ($ENV{'browser.mathml'}) {
                   1441:     $request->content_type('text/xml');
                   1442:   } else {
                   1443:     $request->content_type('text/html');
                   1444:   }
                   1445:   $request->send_http_header;
                   1446:   return OK if $request->header_only;
1.16      albertel 1447:   &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'});
1.13      albertel 1448:   my $url=$ENV{'form.url'};
                   1449:   my $symb=$ENV{'form.symb'};
                   1450:   my $command=$ENV{'form.command'};
1.16      albertel 1451:   if (!$url) {
                   1452:     my ($temp1,$temp2);
                   1453:     ($temp1,$temp2,$ENV{'form.url'})=split(/___/,$symb);
                   1454:     $url = $ENV{'form.url'};
                   1455:   }
1.13      albertel 1456:   &send_header($request);
                   1457:   if ($url eq '' && $symb eq '') {
1.14      www      1458:      if ($ENV{'user.adv'}) {
                   1459:          if (($ENV{'form.codeone'}) && ($ENV{'form.codetwo'}) &&
                   1460:              ($ENV{'form.codethree'})) {
                   1461:              my $token=$ENV{'form.codeone'}.'*'.$ENV{'form.codetwo'}.'*'.
                   1462: 		        $ENV{'form.codethree'};
                   1463:              my ($tsymb,$tuname,$tudom,$tcrsid)=
                   1464: 		 &Apache::lonnet::checkin($token);
                   1465:              if ($tsymb) {
                   1466:                 my ($map,$id,$url)=split(/\_\_\_/,$tsymb);
                   1467:                 if (&Apache::lonnet::allowed('mgr',$tcrsid)) {
                   1468:                    $request->print(
                   1469:                      &Apache::lonnet::ssi('/res/'.$url,
                   1470:                         ('grade_username' => $tuname,
                   1471:                          'grade_domain' => $tudom,
                   1472:                          'grade_courseid' => $tcrsid,
                   1473:                          'grade_symb' => $tsymb)));
                   1474:                 } else {
                   1475:                    $request->print('<h1>Not authorized: '.$token.'</h1>');
                   1476:                 }           
                   1477: 	    } else {
                   1478:                 $request->print('<h1>Not a valid DocID: '.$token.'</h1>');
                   1479:             }
                   1480: 	 } else {
                   1481:              $request->print(&Apache::lonxml::tokeninputfield());
                   1482:          }
                   1483:      }
1.13      albertel 1484:   } else {
1.29      albertel 1485:     #&Apache::lonhomework::showhashsubset(\%ENV,'^form');
1.13      albertel 1486:     $Apache::grades::viewgrades=&Apache::lonnet::allowed('vgr',$ENV{'request.course.id'});
                   1487:     if ($command eq 'submission') {
1.20      albertel 1488:       &listStudents($request) if ($ENV{'form.student'} eq '');
1.34      ng       1489:       &submission($request,0,0) if ($ENV{'form.student'} ne '');
                   1490:     } elsif ($command eq 'processGroup') {
                   1491:       &processGroup($request);
1.26      albertel 1492:     } elsif ($command eq 'gradingmenu') {
                   1493:       $request->print(&gradingmenu($request));
1.13      albertel 1494:     } elsif ($command eq 'viewgrades') {
                   1495:       $request->print(&viewgrades($request));
1.33      ng       1496:     } elsif ($command eq 'handgrade') {
                   1497:       $request->print(&processHandGrade($request));
1.13      albertel 1498:     } elsif ($command eq 'editgrades') {
                   1499:       $request->print(&editgrades($request));
1.23      www      1500:     } elsif ($command eq 'verify') {
                   1501:       $request->print(&verifyreceipt($request));
1.26      albertel 1502:     } elsif ($command eq 'csvupload') {
                   1503:       $request->print(&csvupload($request));
                   1504:     } elsif ($command eq 'csvuploadmap') {
                   1505:       $request->print(&csvuploadmap($request));
1.34      ng       1506: #    } elsif ($command eq 'receiptInput') {
                   1507: #      &receiptInput($request);
1.26      albertel 1508:     } elsif ($command eq 'csvuploadassign') {
                   1509:       if ($ENV{'form.associate'} ne 'Reverse Association') {
                   1510: 	$request->print(&csvuploadassign($request));
                   1511:       } else {
                   1512: 	if ( $ENV{'form.upfile_associate'} ne 'reverse' ) {
                   1513: 	  $ENV{'form.upfile_associate'} = 'reverse';
                   1514: 	} else {
                   1515: 	  $ENV{'form.upfile_associate'} = 'forward';
                   1516: 	}
                   1517: 	$request->print(&csvuploadmap($request));
                   1518:       }
1.12      harris41 1519:     } else {
1.23      www      1520:       $request->print("Unknown action: $command:");
1.2       albertel 1521:     }
1.13      albertel 1522:   }
                   1523:   &send_footer($request);
                   1524:   return OK;
1.1       albertel 1525: }
                   1526: 
                   1527: 1;
                   1528: 
1.13      albertel 1529: __END__;

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