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

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

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