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

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

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