File:  [LON-CAPA] / loncom / homework / grades.pm
Revision 1.21: download - view: text, annotated - select for diffs
Tue Apr 9 04:33:10 2002 UTC (22 years, 1 month ago) by albertel
Branches: MAIN
CVS tags: stable_2002_april, HEAD
- XHTML compliant (<form> not allowed inside <tr> must be insde <td> or outside <table>)

    1: # The LearningOnline Network with CAPA
    2: # The LON-CAPA Grading handler
    3: #
    4: # $Id: grades.pm,v 1.21 2002/04/09 04:33:10 albertel Exp $
    5: #
    6: # Copyright Michigan State University Board of Trustees
    7: #
    8: # This file is part of the LearningOnline Network with CAPA (LON-CAPA).
    9: #
   10: # LON-CAPA is free software; you can redistribute it and/or modify
   11: # it under the terms of the GNU General Public License as published by
   12: # the Free Software Foundation; either version 2 of the License, or
   13: # (at your option) any later version.
   14: #
   15: # LON-CAPA is distributed in the hope that it will be useful,
   16: # but WITHOUT ANY WARRANTY; without even the implied warranty of
   17: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   18: # GNU General Public License for more details.
   19: #
   20: # You should have received a copy of the GNU General Public License
   21: # along with LON-CAPA; if not, write to the Free Software
   22: # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
   23: #
   24: # /home/httpd/html/adm/gpl.txt
   25: #
   26: # http://www.lon-capa.org/
   27: #
   28: # 2/9,2/13 Guy Albertelli
   29: # 6/8 Gerd Kortemeyer
   30: # 7/26 H.K. Ng
   31: # 8/20 Gerd Kortemeyer
   32: 
   33: package Apache::grades;
   34: use strict;
   35: use Apache::style;
   36: use Apache::lonxml;
   37: use Apache::lonnet;
   38: use Apache::loncommon;
   39: use Apache::lonhomework;
   40: use Apache::Constants qw(:common);
   41: 
   42: sub moreinfo {
   43:   my ($request,$reason) = @_;
   44:   $request->print("Unable to process request: $reason");
   45:   if ( $Apache::grades::viewgrades eq 'F' ) {
   46:     $request->print('<form action="/adm/grades" method="post">'."\n");
   47:     if ($ENV{'form.url'}) {
   48:       $request->print('<input type="hidden" name="url" value="'.$ENV{'form.url'}.'" />'."\n");
   49:     }
   50:     if ($ENV{'form.symb'}) {
   51:       $request->print('<input type="hidden" name="symb" value="'.$ENV{'form.symb'}.'" />'."\n");
   52:     }
   53:     $request->print('<input type="hidden" name="command" value="'.$ENV{'form.command'}.'" />'."\n");
   54:     $request->print("Student:".'<input type="text" name="student" value="'.$ENV{'form.student'}.'" />'."<br />\n");
   55:     $request->print("Domain:".'<input type="text" name="domain" value="'.$ENV{'user.domain'}.'" />'."<br />\n");
   56:     $request->print('<input type="submit" name="submit" value="ReSubmit" />'."<br />\n");
   57:     $request->print('</form>');
   58:   }
   59:   return '';
   60: }
   61: 
   62: 
   63: sub listStudents {
   64:   my ($request) = shift;
   65:   my ($cdom,$cnum) = split(/_/,$ENV{'request.course.id'});
   66:   my $chome=$ENV{"course.$ENV{'request.course.id'}.home"};
   67:   $request->print ('<h1>Show Student Submissions on Assessment</h1>'.
   68:  '<table border="1"><tr><th>Username</th><th>Domain</th><th>Name</th><th>&nbsp;</th></tr>'
   69:  );
   70:   my (%classlist) = &getclasslist($cdom,$cnum,$chome,'0');
   71:   foreach my $student ( sort(@{ $classlist{'allids'} }) ) {
   72:       my ($sname,$sdom) = split(/:/,$student);
   73: 
   74:       my $reply=&Apache::lonnet::reply('get:'.$sdom.':'.$sname.
   75: 		     ':environment:lastname&generation&firstname&middlename',
   76: 		      &Apache::lonnet::homeserver($sname,$sdom));
   77:       #print "reply=$reply<br>";
   78:       my (@nameparts) = split /&/,$reply;
   79: #      my $sfullname = $Apache::lonnet::unescape($nameparts[0]);
   80: 
   81:       if ( $Apache::grades::viewgrades eq 'F' ) {
   82: 	  $request->print("\n".'<tr>'."<td>$sname</td><td>$sdom</td><td>@nameparts</td><td>".
   83: 			  '<form action="/adm/grades" method="post">');
   84: 	  if ($ENV{'form.url'}) {
   85: 	    $request->print(
   86:     '<input type="hidden" name="url" value="'.$ENV{'form.url'}.'" />');
   87: 	  }
   88: 	  if ($ENV{'form.symb'}) {
   89: 	    $request->print(
   90:     '<input type="hidden" name="symb" value="'.$ENV{'form.symb'}.'" />');
   91: 	  }
   92: 	  $request->print(
   93:     '<input type="hidden" name="command" value="'.$ENV{'form.command'}.'" />');
   94: 	  $request->print(
   95:              '<input type="hidden" name="student" value="'.$sname.'" />');
   96: 	  $request->print(
   97:              '<input type="hidden" name="domain" value="'.$sdom.'" />');
   98: 	  $request->print(
   99:                          '<input type="submit" name="submit" value="View" />');
  100: 	  $request->print('</form></td></tr>');
  101:       }
  102:   }
  103:   $request->print('</table>');
  104: }
  105: 
  106: 
  107: #FIXME - needs to handle multiple matches
  108: sub finduser {
  109:   my ($name) = @_;
  110:   my $domain = '';
  111: 
  112:   if ( $Apache::grades::viewgrades eq 'F' ) {
  113:     #get classlist
  114:     my ($cdom,$cnum) = split(/_/,$ENV{'request.course.id'});
  115:     my $chome=$ENV{"course.$ENV{'request.course.id'}.home"};
  116:     #print "Found $cdom:$cnum:$chome<br />";
  117:     my (%classlist) = &getclasslist($cdom,$cnum,$chome,'0');
  118:     foreach my $student ( sort(@{ $classlist{'allids'} }) ) {
  119:       my ($posname,$posdomain) = split(/:/,$student);
  120:       if ($posname =~ $name) { $name=$posname; $domain=$posdomain; last; }
  121:     }
  122:     return ($name,$domain);
  123:   } else {
  124:     return ($ENV{'user.name'},$ENV{'user.domain'});
  125:   }
  126: }
  127: 
  128: sub getclasslist {
  129:   my ($coursedomain,$coursenum,$coursehome,$hideexpired) = @_;
  130:   my $classlist=&Apache::lonnet::reply("dump:$coursedomain:$coursenum:classlist",$coursehome);
  131:   my %classlist=();
  132:   my $now = time;
  133:   foreach my $record (split /&/, $classlist) {
  134:     my ($name,$value)=split(/=/,&Apache::lonnet::unescape($record));
  135:     my ($end,$start)=split(/:/,$value);
  136:     # still a student?
  137:     if (($hideexpired) && ($end) && ($end < $now)) {
  138:       #print "Skipping:$name:$end:$now<br />\n";
  139:       next;
  140:     }
  141:     #print "record=$record<br>";
  142:     push( @{ $classlist{'allids'} }, $name); 
  143:   }
  144:   return (%classlist);
  145: }
  146: 
  147: sub getpartlist {
  148:   my ($url) = @_;
  149:   my @parts =();
  150:   my (@metakeys) = split(/,/,&Apache::lonnet::metadata($url,'keys'));
  151:   foreach my $key (@metakeys) {
  152:     if ( $key =~ m/stores_([0-9]+)_.*/ ) {
  153:       push(@parts,$key);
  154:     }
  155:   }
  156:   return @parts;
  157: }
  158: 
  159: sub viewstudentgrade {
  160:   my ($url,$symb,$courseid,$student,@parts) = @_;
  161:   my $result ='';
  162:   my $cellclr = '"#ffffdd"';
  163:   my ($stuname,$domain) = split(/:/,$student);
  164: 
  165:   my %record=&Apache::lonnet::restore($symb,$courseid,$domain,$stuname);
  166: 
  167:   $result.="<tr><td bgcolor=$cellclr>$stuname</td><td bgcolor=$cellclr align=\"middle\">$domain</td>\n";
  168:   foreach my $part (@parts) {
  169:     my ($temp,$part,$type)=split(/_/,$part);
  170:     #print "resource.$part.$type = ".$record{"resource.$part.$type"}." <br />\n";
  171:     if ($type eq 'awarded') {
  172:       my $score=$record{"resource.$part.$type"};
  173:       $result.="<td bgcolor=$cellclr align=\"middle\"><input type=\"text\" name=\"GRADE.$student.$part.$type\" value=\"$score\" size=\"4\" /></td>\n";
  174:     } elsif ($type eq 'tries') {
  175:       my $score=$record{"resource.$part.$type"};
  176:       $result.="<td bgcolor=$cellclr align=\"middle\"><input type=\"text\" name=\"GRADE.$student.$part.$type\" value=\"$score\" size=\"4\" /></td>\n"
  177:     } elsif ($type eq 'solved') {
  178:       my $score=$record{"resource.$part.$type"};
  179:       $result.="<td bgcolor=$cellclr align=\"middle\"><select name=\"GRADE.$student.$part.$type\">\n";
  180:       if ($score =~ /^correct/) {
  181: 	$result.="<option selected=\"on\">correct</option>\n<option>incorrect</option>\n<option>excused</option>\n<option>ungraded</option>\n<option>nothing</option>\n";
  182:       } elsif ($score =~ /^incorrect/) {
  183: 	$result.="<option>correct</option>\n<option selected=\"on\">incorrect</option>\n<option>excused</option>\n<option>ungraded</option>\n<option>nothing</option>\n";
  184:       } elsif ($score eq '') {
  185: 	$result.="<option>correct</option>\n<option>incorrect</option>\n<option>excused</option>\n<option>ungraded</option>\n<option selected=\"on\">nothing</option>\n";
  186:       } elsif ($score =~ /^excused/) {
  187: 	$result.="<option>correct</option>\n<option>incorrect</option>\n<option selected=\"on\">excused</option>\n<option>ungraded</option>\n<option>nothing</option>\n";
  188:       } elsif ($score =~ /^ungraded/) {
  189: 	$result.="<option>correct</option>\n<option>incorrect</option>\n<option>excused</option>\n<option selected=\"on\">ungraded</option>\n<option>nothing</option>\n";
  190:       }
  191:       $result.="</select></td>\n";
  192:     }
  193:   }
  194:   $result.='</tr>';
  195:   return $result;
  196: }
  197: #FIXME need to look at the meatdata <stores> spec on what type of data to accept and provide an
  198: #interface based on that, also do that to above function.
  199: sub setstudentgrade {
  200:   my ($url,$symb,$courseid,$student,@parts) = @_;
  201: 
  202:   my $result ='';
  203: 
  204:   my ($stuname,$domain) = split(/:/,$student);
  205: 
  206:   my %record=&Apache::lonnet::restore($symb,$courseid,$domain,$stuname);
  207: 
  208:   my %newrecord;
  209: 
  210:   foreach my $part (@parts) {
  211:     my ($temp,$part,$type)=split(/_/,$part);
  212:     my $oldscore=$record{"resource.$part.$type"};
  213:     my $newscore=$ENV{"form.GRADE.$student.$part.$type"};
  214:     if ($type eq 'solved') {
  215:       my $update=0;
  216:       if ($newscore eq 'nothing' ) {
  217: 	if ($oldscore ne '') {
  218: 	  $update=1;
  219: 	  $newscore = '';
  220: 	}
  221:       } elsif ($oldscore !~ m/^$newscore/) {
  222: 	$update=1;
  223: 	$result.="Updating $stuname to $newscore<br />\n";
  224: 	if ($newscore eq 'correct') { $newscore = 'correct_by_override'; }
  225: 	if ($newscore eq 'incorrect') { $newscore = 'incorrect_by_override'; }
  226: 	if ($newscore eq 'excused') { $newscore = 'excused'; }
  227: 	if ($newscore eq 'ungraded') { $newscore = 'ungraded_attempted'; }
  228:       } else {
  229: 	#$result.="$stuname:$part:$type:unchanged  $oldscore to $newscore:<br />\n";
  230:       }
  231:       if ($update) { $newrecord{"resource.$part.$type"}=$newscore; }
  232:     } else {
  233:       if ($oldscore ne $newscore) {
  234: 	$newrecord{"resource.$part.$type"}=$newscore;
  235: 	$result.="Updating $student"."'s status for $part.$type to $newscore<br />\n";
  236:       } else {
  237: 	#$result.="$stuname:$part:$type:unchanged  $oldscore to $newscore:<br />\n";
  238:       }
  239:     }
  240:   }
  241:   if ( scalar(keys(%newrecord)) > 0 ) {
  242:     $newrecord{"resource.regrader"}="$ENV{'user.name'}:$ENV{'user.domain'}";
  243:     &Apache::lonnet::cstore(\%newrecord,$symb,$courseid,$domain,$stuname);
  244: 
  245:     $result.="Stored away ".scalar(keys(%newrecord))." elements.<br />\n";
  246:   }
  247:   return $result;
  248: }
  249: 
  250: sub submission {
  251:   my ($request) = @_;
  252:   my $url=$ENV{'form.url'};
  253:   $url=~s-^http://($ENV{'SERVER_NAME'}|$ENV{'HTTP_HOST'})--;
  254:   if ($ENV{'form.student'} eq '') { &moreinfo($request,"Need student login id"); return ''; }
  255: #  if ($ENV{'form.student'} eq '') { &listStudents($request); return ''; }
  256:   my ($uname,$udom) = &finduser($ENV{'form.student'});
  257:   if ($uname eq '') { &moreinfo($request,"Unable to find student"); return ''; }
  258:   my $symb;
  259:   if ($ENV{'form.symb'}) {
  260:     $symb=$ENV{'form.symb'};
  261:   } else {
  262:     $symb=&Apache::lonnet::symbread($url);
  263:   }
  264:   if ($symb eq '') { $request->print("Unable to handle ambiguous references:$url:."); return ''; }
  265:   my $answer=&Apache::loncommon::get_previous_attempt($symb,$uname,$udom,
  266: 						      $ENV{'request.course.id'});
  267:   my $result="<h2> Submission Record </h2>  $uname:$udom for $url <br />".$answer;
  268:   my $rendered=&Apache::loncommon::get_student_view($symb,$uname,$udom,
  269: 						   $ENV{'request.course.id'});
  270:   $result.="Student's view of the problem:<br /> $rendered <br /> Correct answer:";
  271: 
  272:   my $answer=&Apache::loncommon::get_student_answers($symb,$uname,$udom,
  273: 						  $ENV{'request.course.id'});
  274:   $result.=$answer;
  275:   return $result;
  276: }
  277: 
  278: sub viewgrades {
  279:   my ($request) = @_;
  280:   my $result='';
  281: 
  282:   #get resource reference
  283:   my $url=$ENV{'form.url'};
  284:   $url=~s-^http://($ENV{'SERVER_NAME'}|$ENV{'HTTP_HOST'})--;
  285:   my $symb=$ENV{'form.symb'};
  286:   if (!$symb) { $symb=&Apache::lonnet::symbread($url); }
  287:   if ($symb eq '') { $request->print("Unable to handle ambiguous references:$url:."); return ''; }
  288: 
  289:   #get classlist
  290:   my ($cdom,$cnum) = split(/_/,$ENV{'request.course.id'});
  291:   my $chome=$ENV{"course.$ENV{'request.course.id'}.home"};
  292:   #print "Found $cdom:$cnum:$chome<br />";
  293:   my (%classlist) = &getclasslist($cdom,$cnum,$chome,'0');
  294:   my $headerclr = '"#ccffff"';
  295:   my $cellclr = '"#ffffcc"';
  296: 
  297:   #get list of parts for this problem
  298:   my (@parts) = &getpartlist($url);
  299: 
  300:   $request->print ("<h2><font color=\"#339966\">Manual Grading</font></h2>");
  301: 
  302:   #start the form
  303:   $result = '<form action="/adm/grades" method="post">'."\n".
  304:     '<input type="hidden" name="symb" value="'.$symb.'" />'."\n".
  305:       '<input type="hidden" name="url" value="'.$url.'" />'."\n".
  306: 	'<input type="hidden" name="command" value="editgrades" />'."\n".
  307: 	  '<input type="submit" name="submit" value="Submit Changes" />'."\n".
  308: 	    '<table border=0><tr><td bgcolor="#999999">'."\n".
  309: 	     '<table border=0>'."\n".
  310: 	      '<tr><td bgcolor='.$headerclr.'>UserId</td><td bgcolor='.$headerclr.'>Domain</td>'."\n";
  311:   foreach my $part (@parts) {
  312:      my $display=&Apache::lonnet::metadata($url,$part.'.display');
  313:      if  (!$display) { $display = &Apache::lonnet::metadata($url,$part.'.name'); }
  314:      $result.='<td bgcolor='.$headerclr.'>'.$display.'</td>'."\n";
  315:    }
  316:   $result.="</tr>";
  317:   #get info for each student
  318:   foreach my $student ( sort(@{ $classlist{'allids'} }) ) {
  319:     $result.=&viewstudentgrade($url,$symb,$ENV{'request.course.id'},$student,@parts);
  320:   }
  321:   $result.='</table></td></tr></table><input type="submit" name="submit" value="Submit Changes" /></form>';
  322: 
  323:   return $result;
  324: }
  325: 
  326: sub editgrades {
  327:   my ($request) = @_;
  328:   my $result='';
  329: 
  330:   my $symb=$ENV{'form.symb'};
  331:   if ($symb eq '') { $request->print("Unable to handle ambiguous references:$symb:$ENV{'form.url'}"); return ''; }
  332:   my $url=$ENV{'form.url'};
  333:   #get classlist
  334:   my ($cdom,$cnum) = split(/_/,$ENV{'request.course.id'});
  335:   my $chome=$ENV{"course.$ENV{'request.course.id'}.home"};
  336:   #print "Found $cdom:$cnum:$chome<br />";
  337:   my (%classlist) = &getclasslist($cdom,$cnum,$chome,'0');
  338: 
  339:   #get list of parts for this problem
  340:   my (@parts) = &getpartlist($url);
  341: 
  342:   $result.='<form action="/adm/grades" method="post">'."\n".
  343:     '<input type="hidden" name="symb" value="'.$symb.'" />'."\n".
  344:       '<input type="hidden" name="url" value="'.$url.'" />'."\n".
  345: 	'<input type="hidden" name="command" value="viewgrades" />'."\n".
  346: 	  '<input type="submit" name="submit" value="See Grades" /> <br />'."\n";
  347: 
  348:   foreach my $student ( sort(@{ $classlist{'allids'} }) ) {
  349:     $result.=&setstudentgrade($url,$symb,$ENV{'request.course.id'},$student,@parts);
  350:   }
  351: 
  352:   $result.='<input type="submit" name="submit" value="See Grades" /></table></form>';
  353:   return $result;
  354: }
  355: 
  356: sub send_header {
  357:   my ($request)= @_;
  358:   $request->print(&Apache::lontexconvert::header());
  359: #  $request->print("
  360: #<script>
  361: #remotewindow=open('','homeworkremote');
  362: #remotewindow.close();
  363: #</script>"); 
  364:   $request->print('<body bgcolor="#FFFFFF">');
  365: }
  366: 
  367: sub send_footer {
  368:   my ($request)= @_;
  369:   $request->print('</body>');
  370:   $request->print(&Apache::lontexconvert::footer());
  371: }
  372: 
  373: sub handler {
  374:   my $request=$_[0];
  375: 
  376:   if ( $ENV{'user.name'} eq 'albertel' ) {$Apache::lonxml::debug=1;} else {$Apache::lonxml::debug=0;}
  377:   
  378:   if ($ENV{'browser.mathml'}) {
  379:     $request->content_type('text/xml');
  380:   } else {
  381:     $request->content_type('text/html');
  382:   }
  383:   $request->send_http_header;
  384:   return OK if $request->header_only;
  385:   &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'});
  386:   my $url=$ENV{'form.url'};
  387:   my $symb=$ENV{'form.symb'};
  388:   my $command=$ENV{'form.command'};
  389:   if (!$url) {
  390:     my ($temp1,$temp2);
  391:     ($temp1,$temp2,$ENV{'form.url'})=split(/___/,$symb);
  392:     $url = $ENV{'form.url'};
  393:   }
  394:   &send_header($request);
  395:   if ($url eq '' && $symb eq '') {
  396:      if ($ENV{'user.adv'}) {
  397:          if (($ENV{'form.codeone'}) && ($ENV{'form.codetwo'}) &&
  398:              ($ENV{'form.codethree'})) {
  399:              my $token=$ENV{'form.codeone'}.'*'.$ENV{'form.codetwo'}.'*'.
  400: 		        $ENV{'form.codethree'};
  401:              my ($tsymb,$tuname,$tudom,$tcrsid)=
  402: 		 &Apache::lonnet::checkin($token);
  403:              if ($tsymb) {
  404:                 my ($map,$id,$url)=split(/\_\_\_/,$tsymb);
  405:                 if (&Apache::lonnet::allowed('mgr',$tcrsid)) {
  406:                    $request->print(
  407:                      &Apache::lonnet::ssi('/res/'.$url,
  408:                         ('grade_username' => $tuname,
  409:                          'grade_domain' => $tudom,
  410:                          'grade_courseid' => $tcrsid,
  411:                          'grade_symb' => $tsymb)));
  412:                 } else {
  413:                    $request->print('<h1>Not authorized: '.$token.'</h1>');
  414:                 }           
  415: 	    } else {
  416:                 $request->print('<h1>Not a valid DocID: '.$token.'</h1>');
  417:             }
  418: 	 } else {
  419:              $request->print(&Apache::lonxml::tokeninputfield());
  420:          }
  421:      }
  422:   } else {
  423:     $Apache::grades::viewgrades=&Apache::lonnet::allowed('vgr',$ENV{'request.course.id'});
  424:     if ($command eq 'submission') {
  425:       &listStudents($request) if ($ENV{'form.student'} eq '');
  426:       $request->print(&submission($request)) if ($ENV{'form.student'} ne '');
  427:     } elsif ($command eq 'viewgrades') {
  428:       $request->print(&viewgrades($request));
  429:     } elsif ($command eq 'editgrades') {
  430:       $request->print(&editgrades($request));
  431:     } else {
  432:       $request->print("Unknown action:$command:");
  433:     }
  434:   }
  435:   &send_footer($request);
  436:   return OK;
  437: }
  438: 
  439: 1;
  440: 
  441: __END__;

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