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

1.13      albertel    1: # The LON-CAPA Grading handler
                      2: # 2/9,2/13 Guy Albertelli
1.8       www         3: # 6/8 Gerd Kortemeyer
1.13      albertel    4: # 7/26 H.K. Ng
1.14      www         5: # 8/20 Gerd Kortemeyer
1.1       albertel    6: 
                      7: package Apache::grades;
                      8: use strict;
                      9: use Apache::style;
                     10: use Apache::lonxml;
                     11: use Apache::lonnet;
1.3       albertel   12: use Apache::loncommon;
1.1       albertel   13: use Apache::lonhomework;
                     14: use Apache::Constants qw(:common);
                     15: 
1.2       albertel   16: sub moreinfo {
1.13      albertel   17:   my ($request,$reason) = @_;
                     18:   $request->print("Unable to process request: $reason");
                     19:   if ( $Apache::grades::viewgrades eq 'F' ) {
                     20:     $request->print('<form action="/adm/grades" method="post">'."\n");
                     21:     $request->print('<input type="hidden" name="url" value="'.$ENV{'form.url'}.'"></input>'."\n");
                     22:     $request->print('<input type="hidden" name="command" value="'.$ENV{'form.command'}.'"></input>'."\n");
                     23:     $request->print("Student:".'<input type="text" name="student" value="'.$ENV{'form.student'}.'"></input>'."<br />\n");
                     24:     $request->print("Domain:".'<input type="text" name="domain" value="'.$ENV{'user.domain'}.'"></input>'."<br />\n");
                     25:     $request->print('<input type="submit" name="submit" value="ReSubmit"></input>'."<br />\n");
                     26:     $request->print('</form>');
                     27:   }
                     28:   return '';
1.2       albertel   29: }
                     30: 
1.13      albertel   31: 
1.10      ng         32: sub listStudents {
1.13      albertel   33:   my ($request) = shift;
                     34:   my ($cdom,$cnum) = split(/_/,$ENV{'request.course.id'});
                     35:   my $chome=$ENV{"course.$ENV{'request.course.id'}.home"};
                     36:   $request->print ("Found $cdom:$cnum:$chome<br />");
                     37:   my (%classlist) = &getclasslist($cdom,$cnum,$chome,'0');
                     38:   foreach my $student ( sort(@{ $classlist{'allids'} }) ) {
                     39:       my ($sname,$sdom) = split(/:/,$student);
                     40: 
                     41:       my $reply=&Apache::lonnet::reply('get:'.$sdom.':'.$sname.
                     42: 		     ':environment:lastname&generation&firstname&middlename',
                     43: 		      &Apache::lonnet::homeserver($sname,$sdom));
1.15    ! albertel   44:       #print "reply=$reply<br>";
1.13      albertel   45:       my (@nameparts) = split /&/,$reply;
                     46: #      my $sfullname = $Apache::lonnet::unescape($nameparts[0]);
                     47: 
                     48:       if ( $Apache::grades::viewgrades eq 'F' ) {
                     49: 	  $request->print('<form action="/adm/grades" method="post">'."\n");
                     50: 	  $request->print("$sname @nameparts\n");
                     51: 	  $request->print('<input type="hidden" name="url" value="'.$ENV{'form.url'}.'"></input>'."\n");
                     52: 	  $request->print('<input type="hidden" name="command" value="'.$ENV{'form.command'}.'"></input>'."\n");
                     53: 	  $request->print('<input type="hidden" name="student" value="'.$sname.'"></input>'."\n");
                     54: 	  $request->print('<input type="hidden" name="domain" value="'.$sdom.'"></input>'."\n");
                     55: 	  $request->print('<input type="submit" name="submit" value="View"></input>'."<br />\n");
                     56: 	  $request->print('</form>');
                     57:       }
                     58:   }
                     59: 
1.10      ng         60: }
                     61: 
1.13      albertel   62: 
1.7       albertel   63: #FIXME - needs to handle multiple matches
1.2       albertel   64: sub finduser {
1.13      albertel   65:   my ($name) = @_;
                     66:   my $domain = '';
                     67: 
                     68:   if ( $Apache::grades::viewgrades eq 'F' ) {
                     69:     #get classlist
                     70:     my ($cdom,$cnum) = split(/_/,$ENV{'request.course.id'});
                     71:     my $chome=$ENV{"course.$ENV{'request.course.id'}.home"};
                     72:     #print "Found $cdom:$cnum:$chome<br />";
                     73:     my (%classlist) = &getclasslist($cdom,$cnum,$chome,'0');
                     74:     foreach my $student ( sort(@{ $classlist{'allids'} }) ) {
                     75:       my ($posname,$posdomain) = split(/:/,$student);
                     76:       if ($posname =~ $name) { $name=$posname; $domain=$posdomain; last; }
1.7       albertel   77:     }
1.13      albertel   78:     return ($name,$domain);
                     79:   } else {
                     80:     return ($ENV{'user.name'},$ENV{'user.domain'});
                     81:   }
1.5       albertel   82: }
                     83: 
                     84: sub getclasslist {
1.13      albertel   85:   my ($coursedomain,$coursenum,$coursehome,$hideexpired) = @_;
                     86:   my $classlist=&Apache::lonnet::reply("dump:$coursedomain:$coursenum:classlist",$coursehome);
                     87:   my %classlist=();
                     88:   my $now = time;
                     89:   foreach my $record (split /&/, $classlist) {
                     90:     my ($name,$value)=split(/=/,&Apache::lonnet::unescape($record));
                     91:     my ($end,$start)=split(/:/,$value);
                     92:     # still a student?
                     93:     if (($hideexpired) && ($end) && ($end < $now)) {
1.15    ! albertel   94:       #print "Skipping:$name:$end:$now<br />\n";
1.13      albertel   95:       next;
                     96:     }
1.15    ! albertel   97:     #print "record=$record<br>";
1.13      albertel   98:     push( @{ $classlist{'allids'} }, $name); 
                     99:   }
                    100:   return (%classlist);
1.5       albertel  101: }
                    102: 
                    103: sub getpartlist {
1.13      albertel  104:   my ($url) = @_;
                    105:   my @parts =();
                    106:   my (@metakeys) = split(/,/,&Apache::lonnet::metadata($url,'keys'));
                    107:   foreach my $key (@metakeys) {
                    108:     if ( $key =~ m/stores_([0-9]+)_.*/ ) {
                    109:       push(@parts,$key);
1.6       albertel  110:     }
1.13      albertel  111:   }
                    112:   return @parts;
1.5       albertel  113: }
                    114: 
                    115: sub viewstudentgrade {
1.13      albertel  116:   my ($url,$symb,$courseid,$student,@parts) = @_;
                    117:   my $result ='';
                    118:   my $cellclr = '"#ffffdd"';
                    119:   my ($stuname,$domain) = split(/:/,$student);
                    120: 
                    121:   my %record=&Apache::lonnet::restore($symb,$courseid,$domain,$stuname);
                    122: 
                    123:   $result.="<tr><td bgcolor=$cellclr>$stuname</td><td bgcolor=$cellclr align=\"middle\">$domain</td>\n";
                    124:   foreach my $part (@parts) {
                    125:     my ($temp,$part,$type)=split(/_/,$part);
                    126:     #print "resource.$part.$type = ".$record{"resource.$part.$type"}." <br />\n";
                    127:     if ($type eq 'awarded') {
                    128:       my $score=$record{"resource.$part.$type"};
                    129:       $result.="<td bgcolor=$cellclr align=\"middle\"><input type=\"text\" name=\"GRADE.$student.$part.$type\" value=\"$score\" size=\"4\" /></td>\n";
                    130:     } elsif ($type eq 'tries') {
                    131:       my $score=$record{"resource.$part.$type"};
                    132:       $result.="<td bgcolor=$cellclr align=\"middle\"><input type=\"text\" name=\"GRADE.$student.$part.$type\" value=\"$score\" size=\"4\" /></td>\n"
                    133:     } elsif ($type eq 'solved') {
                    134:       my $score=$record{"resource.$part.$type"};
                    135:       $result.="<td bgcolor=$cellclr align=\"middle\"><select name=\"GRADE.$student.$part.$type\">\n";
                    136:       if ($score =~ /^correct/) {
                    137: 	$result.="<option selected=\"on\">correct</option>\n<option>incorrect</option>\n<option>excused</option>\n<option>ungraded</option>\n<option>nothing</option>\n";
                    138:       } elsif ($score =~ /^incorrect/) {
                    139: 	$result.="<option>correct</option>\n<option selected=\"on\">incorrect</option>\n<option>excused</option>\n<option>ungraded</option>\n<option>nothing</option>\n";
                    140:       } elsif ($score eq '') {
                    141: 	$result.="<option>correct</option>\n<option>incorrect</option>\n<option>excused</option>\n<option>ungraded</option>\n<option selected=\"on\">nothing</option>\n";
                    142:       } elsif ($score =~ /^excused/) {
                    143: 	$result.="<option>correct</option>\n<option>incorrect</option>\n<option selected=\"on\">excused</option>\n<option>ungraded</option>\n<option>nothing</option>\n";
                    144:       } elsif ($score =~ /^ungraded/) {
                    145: 	$result.="<option>correct</option>\n<option>incorrect</option>\n<option>excused</option>\n<option selected=\"on\">ungraded</option>\n<option>nothing</option>\n";
                    146:       }
                    147:       $result.="</select></td>\n";
                    148:     }
                    149:   }
                    150:   $result.='</tr>';
                    151:   return $result;
1.5       albertel  152: }
1.13      albertel  153: #FIXME need to look at the meatdata <stores> spec on what type of data to accept and provide an
1.6       albertel  154: #interface based on that, also do that to above function.
1.5       albertel  155: sub setstudentgrade {
1.13      albertel  156:   my ($url,$symb,$courseid,$student,@parts) = @_;
                    157: 
                    158:   my $result ='';
                    159: 
                    160:   my ($stuname,$domain) = split(/:/,$student);
                    161: 
                    162:   my %record=&Apache::lonnet::restore($symb,$courseid,$domain,$stuname);
                    163: 
                    164:   my %newrecord;
                    165: 
                    166:   foreach my $part (@parts) {
                    167:     my ($temp,$part,$type)=split(/_/,$part);
                    168:     my $oldscore=$record{"resource.$part.$type"};
                    169:     my $newscore=$ENV{"form.GRADE.$student.$part.$type"};
                    170:     if ($type eq 'solved') {
                    171:       my $update=0;
                    172:       if ($newscore eq 'nothing' ) {
                    173: 	if ($oldscore ne '') {
                    174: 	  $update=1;
                    175: 	  $newscore = '';
1.6       albertel  176: 	}
1.13      albertel  177:       } elsif ($oldscore !~ m/^$newscore/) {
                    178: 	$update=1;
                    179: 	$result.="Updating $stuname to $newscore<br />\n";
                    180: 	if ($newscore eq 'correct') { $newscore = 'correct_by_override'; }
                    181: 	if ($newscore eq 'incorrect') { $newscore = 'incorrect_by_override'; }
                    182: 	if ($newscore eq 'excused') { $newscore = 'excused'; }
                    183: 	if ($newscore eq 'ungraded') { $newscore = 'ungraded_attempted'; }
                    184:       } else {
                    185: 	#$result.="$stuname:$part:$type:unchanged  $oldscore to $newscore:<br />\n";
                    186:       }
                    187:       if ($update) { $newrecord{"resource.$part.$type"}=$newscore; }
                    188:     } else {
                    189:       if ($oldscore ne $newscore) {
                    190: 	$newrecord{"resource.$part.$type"}=$newscore;
                    191: 	$result.="Updating $student"."'s status for $part.$type to $newscore<br />\n";
                    192:       } else {
                    193: 	#$result.="$stuname:$part:$type:unchanged  $oldscore to $newscore:<br />\n";
                    194:       }
                    195:     }
                    196:   }
                    197:   if ( scalar(keys(%newrecord)) > 0 ) {
                    198:     $newrecord{"resource.regrader"}="$ENV{'user.name'}:$ENV{'user.domain'}";
                    199:     &Apache::lonnet::cstore(\%newrecord,$symb,$courseid,$domain,$stuname);
                    200: 
                    201:     $result.="Stored away ".scalar(keys(%newrecord))." elements.<br />\n";
                    202:   }
                    203:   return $result;
1.2       albertel  204: }
                    205: 
                    206: sub submission {
1.13      albertel  207:   my ($request) = @_;
                    208:   my $url=$ENV{'form.url'};
                    209:   $url=~s-^http://($ENV{'SERVER_NAME'}|$ENV{'HTTP_HOST'})--;
                    210:   if ($ENV{'form.student'} eq '') { &moreinfo($request,"Need student login id"); return ''; }
1.10      ng        211: #  if ($ENV{'form.student'} eq '') { &listStudents($request); return ''; }
1.13      albertel  212:   my ($uname,$udom) = &finduser($ENV{'form.student'});
                    213:   if ($uname eq '') { &moreinfo($request,"Unable to find student"); return ''; }
                    214:   my $symb=&Apache::lonnet::symbread($url);
                    215:   if ($symb eq '') { $request->print("Unable to handle ambiguous references:$url:."); return ''; }
                    216:   my $answer=&Apache::loncommon::get_previous_attempt($symb,$uname,$udom,
                    217: 						      $ENV{'request.course.id'});
                    218:   my $result="<h2> Submission Record </h2>  $uname:$udom for $url <br />".$answer;
                    219:   return $result;
1.2       albertel  220: }
                    221: 
1.5       albertel  222: sub viewgrades {
1.13      albertel  223:   my ($request) = @_;
                    224:   my $result='';
1.5       albertel  225: 
1.13      albertel  226:   #get resource reference
                    227:   my $url=$ENV{'form.url'};
                    228:   $url=~s-^http://($ENV{'SERVER_NAME'}|$ENV{'HTTP_HOST'})--;
                    229:   my $symb=$ENV{'form.symb'};
                    230:   if (!$symb) { $symb=&Apache::lonnet::symbread($url); }
                    231:   if ($symb eq '') { $request->print("Unable to handle ambiguous references:$url:."); return ''; }
                    232: 
                    233:   #get classlist
                    234:   my ($cdom,$cnum) = split(/_/,$ENV{'request.course.id'});
                    235:   my $chome=$ENV{"course.$ENV{'request.course.id'}.home"};
                    236:   #print "Found $cdom:$cnum:$chome<br />";
                    237:   my (%classlist) = &getclasslist($cdom,$cnum,$chome,'0');
                    238:   my $headerclr = '"#ccffff"';
                    239:   my $cellclr = '"#ffffcc"';
                    240: 
                    241:   #get list of parts for this problem
                    242:   my (@parts) = &getpartlist($url);
                    243: 
                    244:   $request->print ("<h2><font color=\"#339966\">Manual Grading</font></h2>");
                    245: 
                    246:   #start the form
                    247:   $result = '<form action="/adm/grades" method="post">'."\n".
                    248:     '<input type="hidden" name="symb" value="'.$symb.'"/>'."\n".
                    249:       '<input type="hidden" name="url" value="'.$url.'"/>'."\n".
                    250: 	'<input type="hidden" name="command" value="editgrades" />'."\n".
                    251: 	  '<input type="submit" name="submit" value="Submit Changes" />'."\n".
                    252: 	    '<table border=0><tr><td bgcolor="#999999">'."\n".
                    253: 	     '<table border=0>'."\n".
                    254: 	      '<tr><td bgcolor='.$headerclr.'>UserId</td><td bgcolor='.$headerclr.'>Domain</td>'."\n";
                    255:   foreach my $part (@parts) {
                    256:      my $display=&Apache::lonnet::metadata($url,$part.'.display');
                    257:      if  (!$display) { $display = &Apache::lonnet::metadata($url,$part.'.name'); }
                    258:      $result.='<td bgcolor='.$headerclr.'>'.$display.'</td>'."\n";
                    259:    }
                    260:   $result.="</tr>";
                    261:   #get info for each student
                    262:   foreach my $student ( sort(@{ $classlist{'allids'} }) ) {
                    263:     $result.=&viewstudentgrade($url,$symb,$ENV{'request.course.id'},$student,@parts);
                    264:   }
                    265:   $result.='</table></td></tr></table><input type="submit" name="submit" value="Submit Changes" /></form>';
1.5       albertel  266: 
1.13      albertel  267:   return $result;
1.5       albertel  268: }
                    269: 
                    270: sub editgrades {
1.13      albertel  271:   my ($request) = @_;
                    272:   my $result='';
1.5       albertel  273: 
1.13      albertel  274:   my $symb=$ENV{'form.symb'};
                    275:   if ($symb eq '') { $request->print("Unable to handle ambiguous references:$symb:$ENV{'form.url'}"); return ''; }
                    276:   my $url=$ENV{'form.url'};
                    277:   #get classlist
                    278:   my ($cdom,$cnum) = split(/_/,$ENV{'request.course.id'});
                    279:   my $chome=$ENV{"course.$ENV{'request.course.id'}.home"};
                    280:   #print "Found $cdom:$cnum:$chome<br />";
                    281:   my (%classlist) = &getclasslist($cdom,$cnum,$chome,'0');
                    282: 
                    283:   #get list of parts for this problem
                    284:   my (@parts) = &getpartlist($url);
                    285: 
                    286:   $result.='<form action="/adm/grades" method="post">'."\n".
                    287:     '<input type="hidden" name="symb" value="'.$symb.'" />'."\n".
                    288:       '<input type="hidden" name="url" value="'.$url.'" />'."\n".
                    289: 	'<input type="hidden" name="command" value="viewgrades" />'."\n".
                    290: 	  '<input type="submit" name="submit" value="See Grades" /> <br />'."\n";
                    291: 
                    292:   foreach my $student ( sort(@{ $classlist{'allids'} }) ) {
                    293:     $result.=&setstudentgrade($url,$symb,$ENV{'request.course.id'},$student,@parts);
                    294:   }
1.5       albertel  295: 
1.13      albertel  296:   $result.='<input type="submit" name="submit" value="See Grades" /></table></form>';
                    297:   return $result;
1.5       albertel  298: }
                    299: 
1.2       albertel  300: sub send_header {
1.13      albertel  301:   my ($request)= @_;
                    302:   $request->print(&Apache::lontexconvert::header());
1.6       albertel  303: #  $request->print("
                    304: #<script>
                    305: #remotewindow=open('','homeworkremote');
                    306: #remotewindow.close();
                    307: #</script>"); 
1.13      albertel  308:   $request->print('<body bgcolor="#FFFFFF">');
1.2       albertel  309: }
                    310: 
                    311: sub send_footer {
1.13      albertel  312:   my ($request)= @_;
1.2       albertel  313:   $request->print('</body>');
                    314:   $request->print(&Apache::lontexconvert::footer());
                    315: }
                    316: 
1.1       albertel  317: sub handler {
1.13      albertel  318:   my $request=$_[0];
                    319: 
                    320:   if ( $ENV{'user.name'} eq 'albertel' ) {$Apache::lonxml::debug=1;} else {$Apache::lonxml::debug=0;}
                    321:   
                    322:   if ($ENV{'browser.mathml'}) {
                    323:     $request->content_type('text/xml');
                    324:   } else {
                    325:     $request->content_type('text/html');
                    326:   }
                    327:   $request->send_http_header;
                    328:   return OK if $request->header_only;
                    329:   my $url=$ENV{'form.url'};
                    330:   my $symb=$ENV{'form.symb'};
                    331:   my $command=$ENV{'form.command'};
                    332:   
                    333:   &send_header($request);
                    334:   if ($url eq '' && $symb eq '') {
1.14      www       335:      if ($ENV{'user.adv'}) {
                    336:          if (($ENV{'form.codeone'}) && ($ENV{'form.codetwo'}) &&
                    337:              ($ENV{'form.codethree'})) {
                    338:              my $token=$ENV{'form.codeone'}.'*'.$ENV{'form.codetwo'}.'*'.
                    339: 		        $ENV{'form.codethree'};
                    340:              my ($tsymb,$tuname,$tudom,$tcrsid)=
                    341: 		 &Apache::lonnet::checkin($token);
                    342:              if ($tsymb) {
                    343:                 my ($map,$id,$url)=split(/\_\_\_/,$tsymb);
                    344:                 if (&Apache::lonnet::allowed('mgr',$tcrsid)) {
                    345:                    $request->print(
                    346:                      &Apache::lonnet::ssi('/res/'.$url,
                    347:                         ('grade_username' => $tuname,
                    348:                          'grade_domain' => $tudom,
                    349:                          'grade_courseid' => $tcrsid,
                    350:                          'grade_symb' => $tsymb)));
                    351:                 } else {
                    352:                    $request->print('<h1>Not authorized: '.$token.'</h1>');
                    353:                 }           
                    354: 	    } else {
                    355:                 $request->print('<h1>Not a valid DocID: '.$token.'</h1>');
                    356:             }
                    357: 	 } else {
                    358:              $request->print(&Apache::lonxml::tokeninputfield());
                    359:          }
                    360:      }
1.13      albertel  361:   } else {
                    362:     $Apache::grades::viewgrades=&Apache::lonnet::allowed('vgr',$ENV{'request.course.id'});
                    363:     if ($command eq 'submission') {
                    364:       $request->print(&listStudents($request)) if ($ENV{'form.student'} eq '');
                    365:       $request->print(&submission($request)) if ($ENV{'form.student'} ne '');
                    366:     } elsif ($command eq 'viewgrades') {
                    367:       $request->print(&viewgrades($request));
                    368:     } elsif ($command eq 'editgrades') {
                    369:       $request->print(&editgrades($request));
1.12      harris41  370:     } else {
1.13      albertel  371:       $request->print("Unknown action:$command:");
1.2       albertel  372:     }
1.13      albertel  373:   }
                    374:   &send_footer($request);
                    375:   return OK;
1.1       albertel  376: }
                    377: 
                    378: 1;
                    379: 
1.13      albertel  380: __END__;

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