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

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.10    ! ng        215:   my $home=&Apache::lonnet::homeserver($uname,$udom);
        !           216:   my $answer=&Apache::loncommon::get_previous_attempt($symb,$uname,$udom,$home,
1.3       albertel  217: 						      $ENV{'request.course.id'});
                    218:   my $result="<h2> Submission Record </h2>  $uname:$udom for $url".$answer;
                    219:   return $result;
1.2       albertel  220: }
                    221: 
1.5       albertel  222: sub viewgrades {
                    223:   my ($request) = @_;
                    224:   my $result='';
                    225: 
                    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');
1.10    ! ng        238:   my $headerclr = '"#ccffff"';
        !           239:   my $cellclr = '"#ffffcc"';
1.5       albertel  240: 
                    241:   #get list of parts for this problem
                    242:   my (@parts) = &getpartlist($url);
                    243: 
1.10    ! ng        244:   $request->print ("<h2><font color=\"#339966\">Manual Grading</font></h2>");
        !           245: 
1.5       albertel  246:   #start the form
                    247:   $result = '<form action="/adm/grades" method="post">'."\n".
1.6       albertel  248:     '<input type="hidden" name="symb" value="'.$symb.'"/>'."\n".
                    249:       '<input type="hidden" name="url" value="'.$url.'"/>'."\n".
1.5       albertel  250: 	'<input type="hidden" name="command" value="editgrades" />'."\n".
                    251: 	  '<input type="submit" name="submit" value="Submit Changes" />'."\n".
1.10    ! ng        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";
1.6       albertel  255:   foreach my $part (@parts) {
                    256:      my $display=&Apache::lonnet::metadata($url,$part.'.display');
                    257:      if  (!$display) { $display = &Apache::lonnet::metadata($url,$part.'.name'); }
1.10    ! ng        258:      $result.='<td bgcolor='.$headerclr.'>'.$display.'</td>'."\n";
1.6       albertel  259:    }
                    260:   $result.="</tr>";
1.5       albertel  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:   }
1.10    ! ng        265:   $result.='</table></td></tr></table><input type="submit" name="submit" value="Submit Changes" /></form>';
1.5       albertel  266: 
                    267:   return $result;
                    268: }
                    269: 
                    270: sub editgrades {
                    271:   my ($request) = @_;
                    272:   my $result='';
                    273: 
                    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".
1.6       albertel  290: 	  '<input type="submit" name="submit" value="See Grades" /> <br />'."\n";
1.5       albertel  291: 
                    292:   foreach my $student ( sort(@{ $classlist{'allids'} }) ) {
                    293:     $result.=&setstudentgrade($url,$symb,$ENV{'request.course.id'},$student,@parts);
                    294:   }
                    295: 
                    296:   $result.='<input type="submit" name="submit" value="See Grades" /></table></form>';
                    297:   return $result;
                    298: }
                    299: 
1.2       albertel  300: sub send_header {
                    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.2       albertel  308:   $request->print('<body bgcolor="#FFFFFF">');
                    309: }
                    310: 
                    311: sub send_footer {
                    312:   my ($request)= @_;
                    313:   $request->print('</body>');
                    314:   $request->print(&Apache::lontexconvert::footer());
                    315: }
                    316: 
1.1       albertel  317: sub handler {
                    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'};
1.5       albertel  330:   my $symb=$ENV{'form.symb'};
1.2       albertel  331:   my $command=$ENV{'form.command'};
                    332:   
                    333:   &send_header($request);
1.5       albertel  334:   if ($url eq '' && $symb eq '') {
1.2       albertel  335:     $request->print("Non-Contextual Access Unsupported:$command:$url:");
1.1       albertel  336:   } else {
1.5       albertel  337:     $Apache::grades::viewgrades=&Apache::lonnet::allowed('vgr',$ENV{'request.course.id'});
1.2       albertel  338:     if ($command eq 'submission') {
1.10    ! ng        339:       $request->print(&listStudents($request)) if ($ENV{'form.student'} eq '');
        !           340:       $request->print(&submission($request)) if ($ENV{'form.student'} ne '');
1.5       albertel  341:     } elsif ($command eq 'viewgrades') {
                    342:       $request->print(&viewgrades($request));
                    343:     } elsif ($command eq 'editgrades') {
                    344:       $request->print(&editgrades($request));
1.2       albertel  345:     } else {
                    346:       $request->print("Unknown action:$command:");
                    347:     }
1.1       albertel  348:   }
1.2       albertel  349:   &send_footer($request);
1.1       albertel  350:   return OK;
                    351: }
                    352: 
                    353: 1;
                    354: 
                    355: __END__;

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