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

1.17      albertel    1: # The LearningOnline Network with CAPA
1.13      albertel    2: # The LON-CAPA Grading handler
1.17      albertel    3: #
1.30    ! ng          4: # $Id: grades.pm,v 1.29 2002/06/20 22:04:21 albertel Exp $
1.17      albertel    5: #
                      6: # Copyright Michigan State University Board of Trustees
                      7: #
                      8: # This file is part of the LearningOnline Network with CAPA (LON-CAPA).
                      9: #
                     10: # LON-CAPA is free software; you can redistribute it and/or modify
                     11: # it under the terms of the GNU General Public License as published by
                     12: # the Free Software Foundation; either version 2 of the License, or
                     13: # (at your option) any later version.
                     14: #
                     15: # LON-CAPA is distributed in the hope that it will be useful,
                     16: # but WITHOUT ANY WARRANTY; without even the implied warranty of
                     17: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
                     18: # GNU General Public License for more details.
                     19: #
                     20: # You should have received a copy of the GNU General Public License
                     21: # along with LON-CAPA; if not, write to the Free Software
                     22: # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
                     23: #
                     24: # /home/httpd/html/adm/gpl.txt
                     25: #
                     26: # http://www.lon-capa.org/
                     27: #
1.13      albertel   28: # 2/9,2/13 Guy Albertelli
1.8       www        29: # 6/8 Gerd Kortemeyer
1.13      albertel   30: # 7/26 H.K. Ng
1.14      www        31: # 8/20 Gerd Kortemeyer
1.30    ! ng         32: # Year 2002
        !            33: # June 2002 H.K. 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:     }
                     56:     $request->print('<input type="hidden" name="command" value="'.$ENV{'form.command'}.'" />'."\n");
                     57:     $request->print("Student:".'<input type="text" name="student" value="'.$ENV{'form.student'}.'" />'."<br />\n");
                     58:     $request->print("Domain:".'<input type="text" name="domain" value="'.$ENV{'user.domain'}.'" />'."<br />\n");
                     59:     $request->print('<input type="submit" name="submit" value="ReSubmit" />'."<br />\n");
1.13      albertel   60:     $request->print('</form>');
                     61:   }
                     62:   return '';
1.2       albertel   63: }
                     64: 
1.23      www        65: sub verifyreceipt {
                     66:     my $request=shift;
                     67:     my $courseid=$ENV{'request.course.id'};
                     68:     my $cdom=$ENV{"course.$courseid.domain"};
                     69:     my $cnum=$ENV{"course.$courseid.num"};
                     70:     my $receipt=unpack("%32C*",$Apache::lonnet::perlvar{'lonHostID'}).'-'.
                     71:                 $ENV{'form.receipt'};
                     72:     $receipt=~s/[^\-\d]//g;
                     73:     my $symb=$ENV{'form.symb'};
                     74:     unless ($symb) {
                     75: 	$symb=&Apache::lonnet::symbread($ENV{'form.url'});
                     76:     }
                     77:     if ((&Apache::lonnet::allowed('mgr',$courseid)) && ($symb)) {
                     78:         $request->print('<h1>Verifying Submission Receipt '.$receipt.'</h1>');
                     79:         my $matches=0;
1.24      albertel   80:         my (%classlist) = &getclasslist($cdom,$cnum,'0');
1.23      www        81:         foreach my $student ( sort(@{ $classlist{'allids'} }) ) {
                     82:             my ($uname,$udom)=split(/\:/,$student);
                     83:             if ($receipt eq 
                     84:              &Apache::lonnet::ireceipt($uname,$udom,$courseid,$symb)) {
                     85:                $request->print('Matching '.$student.'<br>');
                     86:                $matches++;
                     87: 	   }
                     88:         }
1.30    ! ng         89:         $request->printf('<p>'.$matches." match%s</p>",$matches <= 1 ? '' : 'es');
1.23      www        90:     }
                     91:     return '';
                     92: }
1.13      albertel   93: 
1.30    ! ng         94: sub receiptInput {
1.13      albertel   95:   my ($request) = shift;
1.23      www        96:   my $cdom=$ENV{"course.$ENV{'request.course.id'}.domain"};
                     97:   my $cnum=$ENV{"course.$ENV{'request.course.id'}.num"};
                     98:   my $hostver=unpack("%32C*",$Apache::lonnet::perlvar{'lonHostID'});
                     99:   $request->print(<<ENDHEADER);
1.28      ng        100: <h2><font color="#339933">Verify a Submission Receipt Issued by this Server</font></h2>
1.23      www       101: <form action="/adm/grades" method="post">
                    102: <tt>$hostver-<input type="text" name="receipt" size="4"></tt>
                    103: <input type="submit" name="submit" value="Verify">
                    104: <input type="hidden" name="command" value="verify">
                    105: ENDHEADER
                    106: 	  if ($ENV{'form.url'}) {
                    107: 	    $request->print(
                    108:     '<input type="hidden" name="url" value="'.$ENV{'form.url'}.'" />');
                    109: 	  }
                    110: 	  if ($ENV{'form.symb'}) {
                    111: 	    $request->print(
                    112:     '<input type="hidden" name="symb" value="'.$ENV{'form.symb'}.'" />');
                    113: 	  }
1.30    ! ng        114: #  $request->print(<<ENDTABLEST);
        !           115:   $request->print('</form>');
        !           116:     return '';
        !           117: }
        !           118: 
        !           119: sub listStudents {
        !           120:   my ($request) = shift;
        !           121:   my $cdom=$ENV{"course.$ENV{'request.course.id'}.domain"};
        !           122:   my $cnum=$ENV{"course.$ENV{'request.course.id'}.num"};
        !           123: 
1.23      www       124:   $request->print(<<ENDTABLEST);
1.28      ng        125: <h2><font color="#339933">Show Student Submissions on Assessment</font></h2>
                    126: 
1.29      albertel  127: <table border="0"><tr><td bgcolor="#000000">
                    128: <table border="0">
1.30    ! ng        129: <tr bgcolor="#e6ffff"><td colspan="6"><b>Resource: </b> $ENV{'form.url'}</td></tr>
        !           130: <tr bgcolor="#e6ffff"><td><b>Username</b></td><td><b>Name</b></td><td><b>Domain</b></td>
        !           131: <td><b>View Problem</b></td><td><b>Submissions</b></td><td><b>Action</b></td></tr>
1.23      www       132: ENDTABLEST
1.24      albertel  133:   my (%classlist) = &getclasslist($cdom,$cnum,'0');
1.13      albertel  134:   foreach my $student ( sort(@{ $classlist{'allids'} }) ) {
                    135:       my ($sname,$sdom) = split(/:/,$student);
                    136: 
1.25      albertel  137:       my %name=&Apache::lonnet::get('environment', ['lastname','generation',
                    138: 						    'firstname','middlename'],
                    139: 				    $sdom,$sname);
                    140:       my $fullname;
                    141:       my ($tmp) = keys(%name);
                    142:       if ($tmp !~ /^(con_lost|error|no_such_host)/i) {
                    143: 	$fullname=$name{'lastname'}.$name{'generation'};
1.28      ng        144: 	if ($fullname =~ /[^\s]+/) { $fullname.=', '; }
1.25      albertel  145: 	$fullname.=$name{'firstname'}.' '.$name{'middlename'};
                    146:       }
1.13      albertel  147:       if ( $Apache::grades::viewgrades eq 'F' ) {
1.30    ! ng        148: 	  $request->print("\n".'<tr bgcolor=#ffffe6>'."<td>$sname</td><td>$fullname</td><td>$sdom</td>".
1.21      albertel  149: 			  '<form action="/adm/grades" method="post">');
1.16      albertel  150: 	  if ($ENV{'form.url'}) {
1.19      www       151: 	    $request->print(
1.30    ! ng        152: 			    '<input type="hidden" name="url" value="'.$ENV{'form.url'}.'" />');
1.16      albertel  153: 	  }
                    154: 	  if ($ENV{'form.symb'}) {
1.19      www       155: 	    $request->print(
1.30    ! ng        156: 			    '<input type="hidden" name="symb" value="'.$ENV{'form.symb'}.'" />');
1.16      albertel  157: 	  }
1.19      www       158: 	  $request->print(
1.30    ! ng        159: 			  '<input type="hidden" name="command" value="'.$ENV{'form.command'}.'" />');
1.19      www       160: 	  $request->print(
1.30    ! ng        161: 			  '<input type="hidden" name="student" value="'.$sname.'" />');
1.19      www       162: 	  $request->print(
1.30    ! ng        163: 			  '<input type="hidden" name="fullname" value="'.$fullname.'" />');
1.28      ng        164: 	  $request->print(
1.30    ! ng        165: 			  '<input type="hidden" name="domain" value="'.$sdom.'" />');
        !           166: 	  $request->print('<td>'.
        !           167: 			  '<input type="radio" name="vProb" value="no" checked> no '.
        !           168: 			  '<input type="radio" name="vProb" value="yes"> yes </td>');
        !           169: 	  $request->print('<td>'.
        !           170: 			  '<input type="radio" name="submission" value="last" checked> last '.
        !           171: 			  '<input type="radio" name="submission" value="all"> all </td>');
1.19      www       172: 	  $request->print(
1.30    ! ng        173:                          '<td><input type="submit" name="submit" value="View/Grade" />');
1.28      ng        174: 	  $request->print('</td></tr></form>');
                    175: #	  $request->print('</form></td></tr>');
1.13      albertel  176:       }
                    177:   }
1.28      ng        178:   $request->print('</table></td></tr></table>');
1.10      ng        179: }
                    180: 
1.13      albertel  181: 
1.7       albertel  182: #FIXME - needs to handle multiple matches
1.2       albertel  183: sub finduser {
1.13      albertel  184:   my ($name) = @_;
                    185:   my $domain = '';
                    186: 
                    187:   if ( $Apache::grades::viewgrades eq 'F' ) {
                    188:     #get classlist
                    189:     my ($cdom,$cnum) = split(/_/,$ENV{'request.course.id'});
1.24      albertel  190:     #print "Found $cdom:$cnum<br />";
                    191:     my (%classlist) = &getclasslist($cdom,$cnum,'0');
1.13      albertel  192:     foreach my $student ( sort(@{ $classlist{'allids'} }) ) {
                    193:       my ($posname,$posdomain) = split(/:/,$student);
                    194:       if ($posname =~ $name) { $name=$posname; $domain=$posdomain; last; }
1.7       albertel  195:     }
1.13      albertel  196:     return ($name,$domain);
                    197:   } else {
                    198:     return ($ENV{'user.name'},$ENV{'user.domain'});
                    199:   }
1.5       albertel  200: }
                    201: 
                    202: sub getclasslist {
1.24      albertel  203:   my ($coursedomain,$coursenum,$hideexpired) = @_;
                    204:   my %classlist=&Apache::lonnet::dump('classlist',$coursedomain,$coursenum);
1.13      albertel  205:   my $now = time;
1.24      albertel  206:   foreach my $student (keys(%classlist)) {
                    207:     my ($end,$start)=split(/:/,$classlist{$student});
1.13      albertel  208:     # still a student?
                    209:     if (($hideexpired) && ($end) && ($end < $now)) {
1.15      albertel  210:       #print "Skipping:$name:$end:$now<br />\n";
1.13      albertel  211:       next;
                    212:     }
1.15      albertel  213:     #print "record=$record<br>";
1.24      albertel  214:     push( @{ $classlist{'allids'} }, $student);
1.13      albertel  215:   }
                    216:   return (%classlist);
1.5       albertel  217: }
                    218: 
                    219: sub getpartlist {
1.13      albertel  220:   my ($url) = @_;
                    221:   my @parts =();
                    222:   my (@metakeys) = split(/,/,&Apache::lonnet::metadata($url,'keys'));
                    223:   foreach my $key (@metakeys) {
1.30    ! ng        224:     if ( $key =~ m/stores_([0-9]+)_.*/) {
1.13      albertel  225:       push(@parts,$key);
1.6       albertel  226:     }
1.13      albertel  227:   }
                    228:   return @parts;
1.5       albertel  229: }
                    230: 
                    231: sub viewstudentgrade {
1.13      albertel  232:   my ($url,$symb,$courseid,$student,@parts) = @_;
                    233:   my $result ='';
                    234:   my $cellclr = '"#ffffdd"';
1.28      ng        235:   my ($username,$domain) = split(/:/,$student);
1.13      albertel  236: 
1.28      ng        237:   my (@requests) = ('lastname','firstname','middlename','generation');
                    238:   my (%name) = &Apache::lonnet::get('environment',\@requests,$domain,$username);
                    239:   my %record=&Apache::lonnet::restore($symb,$courseid,$domain,$username);
                    240:   my $fullname=$name{'lastname'}.$name{'generation'};
                    241:   if ($fullname =~ /[^\s]+/) { $fullname.=', '; }
                    242:   $fullname.=$name{'firstname'}.' '.$name{'middlename'};
1.13      albertel  243: 
1.28      ng        244:   $result.="<tr bgcolor=$cellclr><td>$username</td><td>$fullname</td><td align=\"middle\">$domain</td>\n";
1.13      albertel  245:   foreach my $part (@parts) {
                    246:     my ($temp,$part,$type)=split(/_/,$part);
                    247:     #print "resource.$part.$type = ".$record{"resource.$part.$type"}." <br />\n";
                    248:     if ($type eq 'awarded') {
                    249:       my $score=$record{"resource.$part.$type"};
1.28      ng        250:       $result.="<td align=\"middle\"><input type=\"text\" name=\"GRADE.$student.$part.$type\" value=\"$score\" size=\"4\" /></td>\n";
1.13      albertel  251:     } elsif ($type eq 'tries') {
                    252:       my $score=$record{"resource.$part.$type"};
1.28      ng        253:       $result.="<td align=\"middle\"><input type=\"text\" name=\"GRADE.$student.$part.$type\" value=\"$score\" size=\"4\" /></td>\n"
1.13      albertel  254:     } elsif ($type eq 'solved') {
                    255:       my $score=$record{"resource.$part.$type"};
1.28      ng        256:       $result.="<td align=\"middle\"><select name=\"GRADE.$student.$part.$type\">\n";
1.13      albertel  257:       if ($score =~ /^correct/) {
                    258: 	$result.="<option selected=\"on\">correct</option>\n<option>incorrect</option>\n<option>excused</option>\n<option>ungraded</option>\n<option>nothing</option>\n";
                    259:       } elsif ($score =~ /^incorrect/) {
                    260: 	$result.="<option>correct</option>\n<option selected=\"on\">incorrect</option>\n<option>excused</option>\n<option>ungraded</option>\n<option>nothing</option>\n";
                    261:       } elsif ($score eq '') {
                    262: 	$result.="<option>correct</option>\n<option>incorrect</option>\n<option>excused</option>\n<option>ungraded</option>\n<option selected=\"on\">nothing</option>\n";
                    263:       } elsif ($score =~ /^excused/) {
                    264: 	$result.="<option>correct</option>\n<option>incorrect</option>\n<option selected=\"on\">excused</option>\n<option>ungraded</option>\n<option>nothing</option>\n";
                    265:       } elsif ($score =~ /^ungraded/) {
                    266: 	$result.="<option>correct</option>\n<option>incorrect</option>\n<option>excused</option>\n<option selected=\"on\">ungraded</option>\n<option>nothing</option>\n";
                    267:       }
                    268:       $result.="</select></td>\n";
                    269:     }
                    270:   }
1.29      albertel  271:   $result.='<td></td></tr>';
1.13      albertel  272:   return $result;
1.5       albertel  273: }
1.13      albertel  274: #FIXME need to look at the meatdata <stores> spec on what type of data to accept and provide an
1.6       albertel  275: #interface based on that, also do that to above function.
1.5       albertel  276: sub setstudentgrade {
1.13      albertel  277:   my ($url,$symb,$courseid,$student,@parts) = @_;
                    278: 
                    279:   my $result ='';
                    280: 
                    281:   my ($stuname,$domain) = split(/:/,$student);
                    282: 
                    283:   my %record=&Apache::lonnet::restore($symb,$courseid,$domain,$stuname);
                    284: 
                    285:   my %newrecord;
                    286: 
                    287:   foreach my $part (@parts) {
                    288:     my ($temp,$part,$type)=split(/_/,$part);
                    289:     my $oldscore=$record{"resource.$part.$type"};
                    290:     my $newscore=$ENV{"form.GRADE.$student.$part.$type"};
                    291:     if ($type eq 'solved') {
                    292:       my $update=0;
                    293:       if ($newscore eq 'nothing' ) {
                    294: 	if ($oldscore ne '') {
                    295: 	  $update=1;
                    296: 	  $newscore = '';
1.6       albertel  297: 	}
1.13      albertel  298:       } elsif ($oldscore !~ m/^$newscore/) {
                    299: 	$update=1;
                    300: 	$result.="Updating $stuname to $newscore<br />\n";
                    301: 	if ($newscore eq 'correct') { $newscore = 'correct_by_override'; }
                    302: 	if ($newscore eq 'incorrect') { $newscore = 'incorrect_by_override'; }
                    303: 	if ($newscore eq 'excused') { $newscore = 'excused'; }
                    304: 	if ($newscore eq 'ungraded') { $newscore = 'ungraded_attempted'; }
                    305:       } else {
                    306: 	#$result.="$stuname:$part:$type:unchanged  $oldscore to $newscore:<br />\n";
                    307:       }
                    308:       if ($update) { $newrecord{"resource.$part.$type"}=$newscore; }
                    309:     } else {
                    310:       if ($oldscore ne $newscore) {
                    311: 	$newrecord{"resource.$part.$type"}=$newscore;
                    312: 	$result.="Updating $student"."'s status for $part.$type to $newscore<br />\n";
                    313:       } else {
                    314: 	#$result.="$stuname:$part:$type:unchanged  $oldscore to $newscore:<br />\n";
                    315:       }
                    316:     }
                    317:   }
                    318:   if ( scalar(keys(%newrecord)) > 0 ) {
                    319:     $newrecord{"resource.regrader"}="$ENV{'user.name'}:$ENV{'user.domain'}";
                    320:     &Apache::lonnet::cstore(\%newrecord,$symb,$courseid,$domain,$stuname);
                    321: 
                    322:     $result.="Stored away ".scalar(keys(%newrecord))." elements.<br />\n";
                    323:   }
                    324:   return $result;
1.2       albertel  325: }
                    326: 
                    327: sub submission {
1.13      albertel  328:   my ($request) = @_;
                    329:   my $url=$ENV{'form.url'};
                    330:   $url=~s-^http://($ENV{'SERVER_NAME'}|$ENV{'HTTP_HOST'})--;
                    331:   if ($ENV{'form.student'} eq '') { &moreinfo($request,"Need student login id"); return ''; }
1.10      ng        332: #  if ($ENV{'form.student'} eq '') { &listStudents($request); return ''; }
1.13      albertel  333:   my ($uname,$udom) = &finduser($ENV{'form.student'});
                    334:   if ($uname eq '') { &moreinfo($request,"Unable to find student"); return ''; }
1.16      albertel  335:   my $symb;
                    336:   if ($ENV{'form.symb'}) {
                    337:     $symb=$ENV{'form.symb'};
                    338:   } else {
                    339:     $symb=&Apache::lonnet::symbread($url);
                    340:   }
1.13      albertel  341:   if ($symb eq '') { $request->print("Unable to handle ambiguous references:$url:."); return ''; }
1.30    ! ng        342: 
1.28      ng        343:   my $result='<h2><font color="#339933">Submission Record</font></h2>';
1.30    ! ng        344:   $result.='<table border="0"><tr><td><b>Username: </b>'.$uname.'</td><td><b>Fullname: </b>'.$ENV{'form.fullname'}.'</td><td><b>Domain: </b>'.$udom.'</td></tr>';
        !           345:   $result.='<tr><td colspan=3><b>Resource: </b>'.$url.'</td></tr></table>';
        !           346:   if ($ENV{'form.vProb'} eq 'yes') {
        !           347:       my $rendered=&Apache::loncommon::get_student_view($symb,$uname,$udom,
1.17      albertel  348: 						   $ENV{'request.course.id'});
1.30    ! ng        349:       my $companswer=&Apache::loncommon::get_student_answers($symb,$uname,$udom,
        !           350: 							 $ENV{'request.course.id'});
        !           351:       $result.='<table border="0"><tr><td bgcolor="#000000">';
        !           352:       $result.='<table border="0"><tr><td bgcolor="#e6ffff">';
        !           353:       $result.='<b>Student\'s view of the problem:</b></td></tr><tr><td bgcolor="#ffffe6">'.$rendered.'<br />';
        !           354:       $result.='<b>Correct answer:</b><br />'.$companswer;
        !           355:       $result.='</td></tr></table>';
        !           356:       $result.='</td></tr></table><br />';
        !           357:   }
        !           358:   my $last = '';
        !           359:   $last = 'last' if ($ENV{'form.submission'} eq 'last');
        !           360:   my $answer=&Apache::loncommon::get_previous_attempt($symb,$uname,$udom,
        !           361: 						      $ENV{'request.course.id'},$last);
        !           362:   $result.=$answer.'<br />Needs an interface for inputting scores';
1.18      albertel  363: 
1.30    ! ng        364:   my (@parts) = sort(&getpartlist($url));
        !           365: 
        !           366:   #start the form
        !           367:   $result.= '<form action="/adm/grades" method="post">'."\n".
        !           368:     '<input type="hidden" name="symb" value="'.$symb.'" />'."\n".
        !           369:       '<input type="hidden" name="url" value="'.$url.'" />'."\n".
        !           370: 	'<input type="hidden" name="command" value="editgrades" />'."\n".
        !           371: 	    '<table border=0><tr><td bgcolor="#000000">'."\n".
        !           372: 	     '<table border=0>'."\n".
        !           373: 	      '<tr bgcolor="#e6ffff"><td>'."\n";
        !           374: 
        !           375:   $result.='</tr>';
        !           376:   #get info for each student
        !           377:     $result.=&viewstudentgrade($url,$symb,$ENV{'request.course.id'},$uname,@parts);
        !           378: 
        !           379:   $result.='</table></td></tr></table><input type="submit" name="submit" value="Submit Changes" /></form>';
1.13      albertel  380:   return $result;
1.2       albertel  381: }
                    382: 
1.26      albertel  383: sub get_symb_and_url {
                    384:  my ($request) = @_;
1.13      albertel  385:   my $url=$ENV{'form.url'};
                    386:   $url=~s-^http://($ENV{'SERVER_NAME'}|$ENV{'HTTP_HOST'})--;
                    387:   my $symb=$ENV{'form.symb'};
                    388:   if (!$symb) { $symb=&Apache::lonnet::symbread($url); }
                    389:   if ($symb eq '') { $request->print("Unable to handle ambiguous references:$url:."); return ''; }
1.26      albertel  390:  return ($symb,$url);
                    391: }
1.13      albertel  392: 
1.29      albertel  393: sub view_edit_entire_class_form {
                    394:   my ($symb,$url)=@_;
                    395:   my $result.='<form action="/adm/grades" method="post">'."\n".
                    396:     '<input type="hidden" name="symb" value="'.$symb.'" />'."\n".
                    397:       '<input type="hidden" name="url" value="'.$url.'" />'."\n".
                    398: 	'<input type="hidden" name="command" value="viewgrades" />'."\n".
1.30    ! ng        399: 	  '<input type="submit" name="submit" value="View/Grade Entire Class" />'."\n".
1.29      albertel  400: 	    '</form>'."\n";
                    401:   return $result;
                    402: }
                    403: 
                    404: sub show_grading_menu_form {
                    405:   my ($symb,$url)=@_;
                    406:   my $result.='<form action="/adm/grades" method="post">'."\n".
                    407:     '<input type="hidden" name="symb" value="'.$symb.'" />'."\n".
                    408:       '<input type="hidden" name="url" value="'.$url.'" />'."\n".
                    409: 	'<input type="hidden" name="command" value="gradingmenu" />'."\n".
                    410: 	  '<input type="submit" name="submit" value="Grading Menu" />'."\n".
                    411: 	    '</form>'."\n";
                    412:   return $result;
                    413: }
                    414: 
1.26      albertel  415: sub gradingmenu {
                    416:   my ($request) = @_;
                    417:   my ($symb,$url)=&get_symb_and_url($request);
                    418:   if (!$symb) {return '';}
1.28      ng        419: 
                    420:   my $result='<h2>&nbsp;<font color="#339933">Select a Grading Method</font></h2><br />';
1.29      albertel  421:   $result.='<table width=100% border=0><tr><td bgcolor=#000000>'."\n";
1.28      ng        422:   $result.='<table width=100% border=0><tr><td bgcolor=#e6ffff>'."\n";
                    423:   $result.='&nbsp;<b>Resource :</b> '.$url.'</td></tr>'."\n";
                    424:   $result.='<tr bgcolor=#ffffe6><td>'."\n";
1.29      albertel  425:   $result.=&view_edit_entire_class_form($symb,$url);
1.26      albertel  426:   $result.='<form action="/adm/grades" method="post">'."\n".
                    427:      '<input type="hidden" name="symb" value="'.$symb.'" />'."\n".
                    428:       '<input type="hidden" name="url" value="'.$url.'" />'."\n".
                    429: 	'<input type="hidden" name="command" value="csvupload" />'."\n".
                    430: 	  '<input type="submit" name="submit" value="Upload Scores" />'."\n".
1.28      ng        431: 	    '</form>'."\n";
1.26      albertel  432:   $result.='<form action="/adm/grades" method="post">'."\n".
                    433:      '<input type="hidden" name="symb" value="'.$symb.'" />'."\n".
                    434:       '<input type="hidden" name="url" value="'.$url.'" />'."\n".
                    435: 	'<input type="hidden" name="command" value="submission" />'."\n".
1.30    ! ng        436: 	  '<input type="submit" name="submit" value="View/Grade A Student" />'."\n".
        !           437: 	    '</form>'."\n";
        !           438:   $result.='<form action="/adm/grades" method="post">'."\n".
        !           439:      '<input type="hidden" name="symb" value="'.$symb.'" />'."\n".
        !           440:       '<input type="hidden" name="url" value="'.$url.'" />'."\n".
        !           441: 	'<input type="hidden" name="command" value="receiptInput" />'."\n".
        !           442: 	  '<input type="submit" name="submit" value="Verify Receipt" />'."\n".
1.28      ng        443: 	    '</form>'."\n";
                    444:   $result.='</td></tr></table>'."\n";
                    445:   $result.='</td></tr></table>'."\n";
1.26      albertel  446:   return $result;
                    447: }
                    448: 
                    449: sub viewgrades {
                    450:   my ($request) = @_;
                    451:   my $result='';
                    452: 
                    453:   #get resource reference
                    454:   my ($symb,$url)=&get_symb_and_url($request);
                    455:   if (!$symb) {return '';}
1.13      albertel  456:   #get classlist
                    457:   my ($cdom,$cnum) = split(/_/,$ENV{'request.course.id'});
1.24      albertel  458:   #print "Found $cdom:$cnum<br />";
                    459:   my (%classlist) = &getclasslist($cdom,$cnum,'0');
1.13      albertel  460:   my $headerclr = '"#ccffff"';
                    461:   my $cellclr = '"#ffffcc"';
                    462: 
                    463:   #get list of parts for this problem
1.29      albertel  464:   my (@parts) = sort(&getpartlist($url));
1.13      albertel  465: 
1.28      ng        466:   $request->print ("<h2><font color=\"#339933\">Manual Grading</font></h2>");
1.13      albertel  467: 
                    468:   #start the form
                    469:   $result = '<form action="/adm/grades" method="post">'."\n".
1.16      albertel  470:     '<input type="hidden" name="symb" value="'.$symb.'" />'."\n".
                    471:       '<input type="hidden" name="url" value="'.$url.'" />'."\n".
1.13      albertel  472: 	'<input type="hidden" name="command" value="editgrades" />'."\n".
                    473: 	  '<input type="submit" name="submit" value="Submit Changes" />'."\n".
1.29      albertel  474: 	    '<table border=0><tr><td bgcolor="#000000">'."\n".
1.13      albertel  475: 	     '<table border=0>'."\n".
1.28      ng        476: 	      '<tr bgcolor='.$headerclr.'><td><b>Username</b></td><td><b>Name</b></td><td><b>Domain</b></td>'."\n";
1.29      albertel  477:   foreach my $part (@parts) {
1.13      albertel  478:      my $display=&Apache::lonnet::metadata($url,$part.'.display');
                    479:      if  (!$display) { $display = &Apache::lonnet::metadata($url,$part.'.name'); }
1.28      ng        480:      $result.='<td><b>'.$display.'</b></td>'."\n";
1.30    ! ng        481:   }
1.28      ng        482:   $result.='</tr>';
1.13      albertel  483:   #get info for each student
                    484:   foreach my $student ( sort(@{ $classlist{'allids'} }) ) {
                    485:     $result.=&viewstudentgrade($url,$symb,$ENV{'request.course.id'},$student,@parts);
                    486:   }
                    487:   $result.='</table></td></tr></table><input type="submit" name="submit" value="Submit Changes" /></form>';
1.29      albertel  488:   $result.=&show_grading_menu_form($symb,$url);
1.13      albertel  489:   return $result;
1.5       albertel  490: }
                    491: 
                    492: sub editgrades {
1.13      albertel  493:   my ($request) = @_;
                    494:   my $result='';
1.5       albertel  495: 
1.13      albertel  496:   my $symb=$ENV{'form.symb'};
                    497:   if ($symb eq '') { $request->print("Unable to handle ambiguous references:$symb:$ENV{'form.url'}"); return ''; }
                    498:   my $url=$ENV{'form.url'};
                    499:   #get classlist
                    500:   my ($cdom,$cnum) = split(/_/,$ENV{'request.course.id'});
1.24      albertel  501:   #print "Found $cdom:$cnum<br />";
                    502:   my (%classlist) = &getclasslist($cdom,$cnum,'0');
1.13      albertel  503: 
                    504:   #get list of parts for this problem
                    505:   my (@parts) = &getpartlist($url);
                    506: 
                    507:   $result.='<form action="/adm/grades" method="post">'."\n".
                    508:     '<input type="hidden" name="symb" value="'.$symb.'" />'."\n".
                    509:       '<input type="hidden" name="url" value="'.$url.'" />'."\n".
                    510: 	'<input type="hidden" name="command" value="viewgrades" />'."\n".
                    511: 	  '<input type="submit" name="submit" value="See Grades" /> <br />'."\n";
                    512: 
                    513:   foreach my $student ( sort(@{ $classlist{'allids'} }) ) {
                    514:     $result.=&setstudentgrade($url,$symb,$ENV{'request.course.id'},$student,@parts);
                    515:   }
1.5       albertel  516: 
1.13      albertel  517:   $result.='<input type="submit" name="submit" value="See Grades" /></table></form>';
                    518:   return $result;
1.5       albertel  519: }
                    520: 
1.26      albertel  521: sub csvupload {
                    522:   my ($request)= @_;
                    523:   my $result;
                    524:   my ($symb,$url)=&get_symb_and_url($request);
                    525:   if (!$symb) {return '';}
                    526:   my $upfile_select=&Apache::loncommon::upfile_select_html();
                    527:   $result.=<<ENDUPFORM;
                    528: <form method="post" enctype="multipart/form-data" action="/adm/grades" name="gradesupload">
                    529: <input type="hidden" name="symb" value="$symb" />
                    530: <input type="hidden" name="url" value="$url" />
                    531: <input type="hidden" name="command" value="csvuploadmap" />
                    532: <hr />
                    533: <h3>Specify a file containing the class grades for resource $url</h3>
                    534: $upfile_select
                    535: <p><input type="submit" name="submit" value="Upload Grades" />
                    536: ENDUPFORM
                    537:   return $result;
                    538: }
                    539: 
1.27      albertel  540: sub csvupload_javascript_reverse_associate {
                    541:   return(<<ENDPICK);
                    542:   function verify(vf) {
                    543:     var foundsomething=0;
                    544:     var founduname=0;
                    545:     var founddomain=0;
                    546:     for (i=0;i<=vf.nfields.value;i++) {
                    547:       tw=eval('vf.f'+i+'.selectedIndex');
                    548:       if (i==0 && tw!=0) { founduname=1; }
                    549:       if (i==1 && tw!=0) { founddomain=1; }
                    550:       if (i!=0 && i!=1 && tw!=0) { foundsomething=1; }
                    551:     }
                    552:     if (founduname==0 || founddomain==0) {
                    553:       alert('You need to specify at both the username and domain');
                    554:       return;
                    555:     }
                    556:     if (foundsomething==0) {
                    557:       alert('You need to specify at least one grading field');
                    558:       return;
                    559:     }
                    560:     vf.submit();
                    561:   }
                    562:   function flip(vf,tf) {
                    563:     var nw=eval('vf.f'+tf+'.selectedIndex');
                    564:     var i;
                    565:     for (i=0;i<=vf.nfields.value;i++) {
                    566:       //can not pick the same destination field for both name and domain
                    567:       if (((i ==0)||(i ==1)) && 
                    568:           ((tf==0)||(tf==1)) && 
                    569:           (i!=tf) &&
                    570:           (eval('vf.f'+i+'.selectedIndex')==nw)) {
                    571:         eval('vf.f'+i+'.selectedIndex=0;')
                    572:       }
                    573:     }
                    574:   }
                    575: ENDPICK
                    576: }
                    577: 
                    578: sub csvupload_javascript_forward_associate {
                    579:   return(<<ENDPICK);
                    580:   function verify(vf) {
                    581:     var foundsomething=0;
                    582:     var founduname=0;
                    583:     var founddomain=0;
                    584:     for (i=0;i<=vf.nfields.value;i++) {
                    585:       tw=eval('vf.f'+i+'.selectedIndex');
                    586:       if (tw==1) { founduname=1; }
                    587:       if (tw==2) { founddomain=1; }
                    588:       if (tw>2) { foundsomething=1; }
                    589:     }
                    590:     if (founduname==0 || founddomain==0) {
                    591:       alert('You need to specify at both the username and domain');
                    592:       return;
                    593:     }
                    594:     if (foundsomething==0) {
                    595:       alert('You need to specify at least one grading field');
                    596:       return;
                    597:     }
                    598:     vf.submit();
                    599:   }
                    600:   function flip(vf,tf) {
                    601:     var nw=eval('vf.f'+tf+'.selectedIndex');
                    602:     var i;
                    603:     //can not pick the same destination field twice
                    604:     for (i=0;i<=vf.nfields.value;i++) {
                    605:       if ((i!=tf) && (eval('vf.f'+i+'.selectedIndex')==nw)) {
                    606:         eval('vf.f'+i+'.selectedIndex=0;')
                    607:       }
                    608:     }
                    609:   }
                    610: ENDPICK
                    611: }
                    612: 
1.26      albertel  613: sub csvuploadmap_header {
                    614:   my ($request,$symb,$url,$datatoken,$distotal)= @_;
                    615:   my $result;
                    616:   my $javascript;
                    617:   if ($ENV{'form.upfile_associate'} eq 'reverse') {
1.27      albertel  618:     $javascript=&csvupload_javascript_reverse_associate();
1.26      albertel  619:   } else {
1.27      albertel  620:     $javascript=&csvupload_javascript_forward_associate();
1.26      albertel  621:   }
                    622:   $request->print(<<ENDPICK);
                    623: <form method="post" enctype="multipart/form-data" action="/adm/grades" name="gradesupload">
                    624: <h3>Uploading Class Grades for resource $url</h3>
                    625: <hr>
                    626: <h3>Identify fields</h3>
                    627: Total number of records found in file: $distotal <hr />
                    628: Enter as many fields as you can. The system will inform you and bring you back
                    629: to this page if the data selected is insufficient to run your class.<hr />
                    630: <input type="button" value="Reverse Association" onClick="javascript:this.form.associate.value='Reverse Association';submit(this.form);" />
                    631: <input type="hidden" name="associate"  value="" />
                    632: <input type="hidden" name="phase"      value="three" />
                    633: <input type="hidden" name="datatoken"  value="$datatoken" />
                    634: <input type="hidden" name="fileupload" value="$ENV{'form.fileupload'}" />
                    635: <input type="hidden" name="upfiletype" value="$ENV{'form.upfiletype'}" />
                    636: <input type="hidden" name="upfile_associate" 
                    637:                                        value="$ENV{'form.upfile_associate'}" />
                    638: <input type="hidden" name="symb"       value="$symb" />
                    639: <input type="hidden" name="url"        value="$url" />
                    640: <input type="hidden" name="command"    value="csvuploadassign" />
                    641: <hr />
                    642: <script type="text/javascript" language="Javascript">
                    643: $javascript
                    644: </script>
                    645: ENDPICK
                    646:   return '';
                    647: 
                    648: }
                    649: 
                    650: sub csvupload_fields {
                    651:   my ($url) = @_;
                    652:   my (@parts) = &getpartlist($url);
1.27      albertel  653:   my @fields=(['username','Student Username'],['domain','Student Domain']);
                    654:   foreach my $part (sort(@parts)) {
1.26      albertel  655:     my @datum;
                    656:     my $display=&Apache::lonnet::metadata($url,$part.'.display');
1.27      albertel  657:     my $name=$part;
1.26      albertel  658:     if  (!$display) { $display = $name; }
                    659:     @datum=($name,$display);
                    660:     push(@fields,\@datum);
                    661:   }
                    662:   return (@fields);
                    663: }
                    664: 
                    665: sub csvuploadmap_footer {
                    666:   my ($request,$i,$keyfields) =@_;
                    667:   $request->print(<<ENDPICK);
                    668: </table>
                    669: <input type="hidden" name="nfields" value="$i" />
                    670: <input type="hidden" name="keyfields" value="$keyfields" />
                    671: <input type="button" onClick="javascript:verify(this.form)" value="Assign Grades" /><br />
                    672: </form>
                    673: ENDPICK
                    674: }
                    675: 
                    676: sub csvuploadmap {
                    677:   my ($request)= @_;
                    678:   my ($symb,$url)=&get_symb_and_url($request);
                    679:   if (!$symb) {return '';}
                    680:   my $datatoken;
                    681:   if (!$ENV{'form.datatoken'}) {
                    682:     $datatoken=&Apache::loncommon::upfile_store($request);
                    683:   } else {
                    684:     $datatoken=$ENV{'form.datatoken'};
                    685:     &Apache::loncommon::load_tmp_file($request);
                    686:   }
                    687:   my @records=&Apache::loncommon::upfile_record_sep();
                    688:   &csvuploadmap_header($request,$symb,$url,$datatoken,$#records+1);
                    689:   my $i;
                    690:   my $keyfields;
                    691:   if (@records) {
                    692:     my @fields=&csvupload_fields($url);
                    693:     if ($ENV{'form.upfile_associate'} eq 'reverse') {	
                    694:       &Apache::loncommon::csv_print_samples($request,\@records);
                    695:       $i=&Apache::loncommon::csv_print_select_table($request,\@records,
                    696: 						    \@fields);
                    697:       foreach (@fields) { $keyfields.=$_->[0].','; }
                    698:       chop($keyfields);
                    699:     } else {
                    700:       unshift(@fields,['none','']);
                    701:       $i=&Apache::loncommon::csv_samples_select_table($request,\@records,
                    702: 						      \@fields);
                    703:       my %sone=&Apache::loncommon::record_sep($records[0]);
                    704:       $keyfields=join(',',sort(keys(%sone)));
                    705:     }
                    706:   }
                    707:   &csvuploadmap_footer($request,$i,$keyfields);
                    708:   return '';
1.27      albertel  709: }
                    710: 
                    711: sub csvuploadassign {
                    712:   my ($request)= @_;
                    713:   my ($symb,$url)=&get_symb_and_url($request);
                    714:   if (!$symb) {return '';}
                    715:   &Apache::loncommon::load_tmp_file($request);
                    716:   my @gradedata=&Apache::loncommon::upfile_record_sep();
                    717:   my @keyfields = split(/\,/,$ENV{'form.keyfields'});
                    718:   my %fields=();
                    719:   for (my $i=0; $i<=$ENV{'form.nfields'}; $i++) {
                    720:     if ($ENV{'form.upfile_associate'} eq 'reverse') {
                    721:       if ($ENV{'form.f'.$i} ne 'none') {
                    722: 	$fields{$keyfields[$i]}=$ENV{'form.f'.$i};
                    723:       }
                    724:     } else {
                    725:       if ($ENV{'form.f'.$i} ne 'none') {
                    726: 	$fields{$ENV{'form.f'.$i}}=$keyfields[$i];
                    727:       }
                    728:     }
                    729:   }
                    730:   $request->print('<h3>Assigning Grades</h3>');
                    731:   my $courseid=$ENV{'request.course.id'};
                    732:   my $cdom=$ENV{"course.$courseid.domain"};
                    733:   my $cnum=$ENV{"course.$courseid.num"};
                    734:   my (%classlist) = &getclasslist($cdom,$cnum,'1');
1.29      albertel  735:   my @skipped;
                    736:   my $countdone=0;
                    737:   foreach my $grade (@gradedata) {
                    738:     my %entries=&Apache::loncommon::record_sep($grade);
                    739:     my $username=$entries{$fields{'username'}};
                    740:     my $domain=$entries{$fields{'domain'}};
                    741:     if (!exists($classlist{"$username:$domain"})) {
                    742:       push(@skipped,"$username:$domain");
                    743:       next;
1.27      albertel  744:     }
1.29      albertel  745:     my %grades;
                    746:     foreach my $dest (keys(%fields)) {
                    747:       if ($dest eq 'username' || $dest eq 'domain') { next; }
                    748:       if ($entries{$fields{$dest}} eq '') { next; }
                    749:       my $store_key=$dest;
                    750:       $store_key=~s/^stores/resource/;
                    751:       $store_key=~s/_/\./g;
                    752:       $grades{$store_key}=$entries{$fields{$dest}};
                    753:     }
                    754:     $grades{"resource.regrader"}="$ENV{'user.name'}:$ENV{'user.domain'}";
                    755:     &Apache::lonnet::cstore(\%grades,$symb,$ENV{'request.course.id'},
                    756: 			    $domain,$username);
                    757:     $request->print('.');
                    758:     $request->rflush();
                    759:     $countdone++;
                    760:   }
                    761:   $request->print("<br />Stored $countdone students\n");
                    762:   if (@skipped) {
                    763:     $request->print('<br /><font size="+1"><b>Skipped Students</b></font><br />');
                    764:     foreach my $student (@skipped) { $request->print("<br />$student"); }
1.27      albertel  765:   }
1.29      albertel  766:   $request->print(&view_edit_entire_class_form($symb,$url));
                    767:   $request->print(&show_grading_menu_form($symb,$url));
                    768:   return '';
1.26      albertel  769: }
                    770: 
1.2       albertel  771: sub send_header {
1.13      albertel  772:   my ($request)= @_;
                    773:   $request->print(&Apache::lontexconvert::header());
1.6       albertel  774: #  $request->print("
                    775: #<script>
                    776: #remotewindow=open('','homeworkremote');
                    777: #remotewindow.close();
                    778: #</script>"); 
1.13      albertel  779:   $request->print('<body bgcolor="#FFFFFF">');
1.2       albertel  780: }
                    781: 
                    782: sub send_footer {
1.13      albertel  783:   my ($request)= @_;
1.2       albertel  784:   $request->print('</body>');
                    785:   $request->print(&Apache::lontexconvert::footer());
                    786: }
                    787: 
1.1       albertel  788: sub handler {
1.13      albertel  789:   my $request=$_[0];
                    790: 
                    791:   if ($ENV{'browser.mathml'}) {
                    792:     $request->content_type('text/xml');
                    793:   } else {
                    794:     $request->content_type('text/html');
                    795:   }
                    796:   $request->send_http_header;
                    797:   return OK if $request->header_only;
1.16      albertel  798:   &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'});
1.13      albertel  799:   my $url=$ENV{'form.url'};
                    800:   my $symb=$ENV{'form.symb'};
                    801:   my $command=$ENV{'form.command'};
1.16      albertel  802:   if (!$url) {
                    803:     my ($temp1,$temp2);
                    804:     ($temp1,$temp2,$ENV{'form.url'})=split(/___/,$symb);
                    805:     $url = $ENV{'form.url'};
                    806:   }
1.13      albertel  807:   &send_header($request);
                    808:   if ($url eq '' && $symb eq '') {
1.14      www       809:      if ($ENV{'user.adv'}) {
                    810:          if (($ENV{'form.codeone'}) && ($ENV{'form.codetwo'}) &&
                    811:              ($ENV{'form.codethree'})) {
                    812:              my $token=$ENV{'form.codeone'}.'*'.$ENV{'form.codetwo'}.'*'.
                    813: 		        $ENV{'form.codethree'};
                    814:              my ($tsymb,$tuname,$tudom,$tcrsid)=
                    815: 		 &Apache::lonnet::checkin($token);
                    816:              if ($tsymb) {
                    817:                 my ($map,$id,$url)=split(/\_\_\_/,$tsymb);
                    818:                 if (&Apache::lonnet::allowed('mgr',$tcrsid)) {
                    819:                    $request->print(
                    820:                      &Apache::lonnet::ssi('/res/'.$url,
                    821:                         ('grade_username' => $tuname,
                    822:                          'grade_domain' => $tudom,
                    823:                          'grade_courseid' => $tcrsid,
                    824:                          'grade_symb' => $tsymb)));
                    825:                 } else {
                    826:                    $request->print('<h1>Not authorized: '.$token.'</h1>');
                    827:                 }           
                    828: 	    } else {
                    829:                 $request->print('<h1>Not a valid DocID: '.$token.'</h1>');
                    830:             }
                    831: 	 } else {
                    832:              $request->print(&Apache::lonxml::tokeninputfield());
                    833:          }
                    834:      }
1.13      albertel  835:   } else {
1.29      albertel  836:     #&Apache::lonhomework::showhashsubset(\%ENV,'^form');
1.13      albertel  837:     $Apache::grades::viewgrades=&Apache::lonnet::allowed('vgr',$ENV{'request.course.id'});
                    838:     if ($command eq 'submission') {
1.20      albertel  839:       &listStudents($request) if ($ENV{'form.student'} eq '');
1.13      albertel  840:       $request->print(&submission($request)) if ($ENV{'form.student'} ne '');
1.26      albertel  841:     } elsif ($command eq 'gradingmenu') {
                    842:       $request->print(&gradingmenu($request));
1.13      albertel  843:     } elsif ($command eq 'viewgrades') {
                    844:       $request->print(&viewgrades($request));
                    845:     } elsif ($command eq 'editgrades') {
                    846:       $request->print(&editgrades($request));
1.23      www       847:     } elsif ($command eq 'verify') {
                    848:       $request->print(&verifyreceipt($request));
1.26      albertel  849:     } elsif ($command eq 'csvupload') {
                    850:       $request->print(&csvupload($request));
                    851:     } elsif ($command eq 'csvuploadmap') {
                    852:       $request->print(&csvuploadmap($request));
1.30    ! ng        853:     } elsif ($command eq 'receiptInput') {
        !           854:       &receiptInput($request);
1.26      albertel  855:     } elsif ($command eq 'csvuploadassign') {
                    856:       if ($ENV{'form.associate'} ne 'Reverse Association') {
                    857: 	$request->print(&csvuploadassign($request));
                    858:       } else {
                    859: 	if ( $ENV{'form.upfile_associate'} ne 'reverse' ) {
                    860: 	  $ENV{'form.upfile_associate'} = 'reverse';
                    861: 	} else {
                    862: 	  $ENV{'form.upfile_associate'} = 'forward';
                    863: 	}
                    864: 	$request->print(&csvuploadmap($request));
                    865:       }
1.12      harris41  866:     } else {
1.23      www       867:       $request->print("Unknown action: $command:");
1.2       albertel  868:     }
1.13      albertel  869:   }
                    870:   &send_footer($request);
                    871:   return OK;
1.1       albertel  872: }
                    873: 
                    874: 1;
                    875: 
1.13      albertel  876: __END__;

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