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

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.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 {
1.13    ! albertel   16:   my ($request,$reason) = @_;
        !            17:   $request->print("Unable to process request: $reason");
        !            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: 
1.13    ! albertel   30: 
1.10      ng         31: sub listStudents {
1.13    ! albertel   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: 
1.10      ng         59: }
                     60: 
1.13    ! albertel   61: 
1.7       albertel   62: #FIXME - needs to handle multiple matches
1.2       albertel   63: sub finduser {
1.13    ! albertel   64:   my ($name) = @_;
        !            65:   my $domain = '';
        !            66: 
        !            67:   if ( $Apache::grades::viewgrades eq 'F' ) {
        !            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; }
1.7       albertel   76:     }
1.13    ! albertel   77:     return ($name,$domain);
        !            78:   } else {
        !            79:     return ($ENV{'user.name'},$ENV{'user.domain'});
        !            80:   }
1.5       albertel   81: }
                     82: 
                     83: sub getclasslist {
1.13    ! albertel   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:     }
        !            96:     print "record=$record<br>";
        !            97:     push( @{ $classlist{'allids'} }, $name); 
        !            98:   }
        !            99:   return (%classlist);
1.5       albertel  100: }
                    101: 
                    102: sub getpartlist {
1.13    ! albertel  103:   my ($url) = @_;
        !           104:   my @parts =();
        !           105:   my (@metakeys) = split(/,/,&Apache::lonnet::metadata($url,'keys'));
        !           106:   foreach my $key (@metakeys) {
        !           107:     if ( $key =~ m/stores_([0-9]+)_.*/ ) {
        !           108:       push(@parts,$key);
1.6       albertel  109:     }
1.13    ! albertel  110:   }
        !           111:   return @parts;
1.5       albertel  112: }
                    113: 
                    114: sub viewstudentgrade {
1.13    ! albertel  115:   my ($url,$symb,$courseid,$student,@parts) = @_;
        !           116:   my $result ='';
        !           117:   my $cellclr = '"#ffffdd"';
        !           118:   my ($stuname,$domain) = split(/:/,$student);
        !           119: 
        !           120:   my %record=&Apache::lonnet::restore($symb,$courseid,$domain,$stuname);
        !           121: 
        !           122:   $result.="<tr><td bgcolor=$cellclr>$stuname</td><td bgcolor=$cellclr align=\"middle\">$domain</td>\n";
        !           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"};
        !           128:       $result.="<td bgcolor=$cellclr align=\"middle\"><input type=\"text\" name=\"GRADE.$student.$part.$type\" value=\"$score\" size=\"4\" /></td>\n";
        !           129:     } elsif ($type eq 'tries') {
        !           130:       my $score=$record{"resource.$part.$type"};
        !           131:       $result.="<td bgcolor=$cellclr align=\"middle\"><input type=\"text\" name=\"GRADE.$student.$part.$type\" value=\"$score\" size=\"4\" /></td>\n"
        !           132:     } elsif ($type eq 'solved') {
        !           133:       my $score=$record{"resource.$part.$type"};
        !           134:       $result.="<td bgcolor=$cellclr align=\"middle\"><select name=\"GRADE.$student.$part.$type\">\n";
        !           135:       if ($score =~ /^correct/) {
        !           136: 	$result.="<option selected=\"on\">correct</option>\n<option>incorrect</option>\n<option>excused</option>\n<option>ungraded</option>\n<option>nothing</option>\n";
        !           137:       } elsif ($score =~ /^incorrect/) {
        !           138: 	$result.="<option>correct</option>\n<option selected=\"on\">incorrect</option>\n<option>excused</option>\n<option>ungraded</option>\n<option>nothing</option>\n";
        !           139:       } elsif ($score eq '') {
        !           140: 	$result.="<option>correct</option>\n<option>incorrect</option>\n<option>excused</option>\n<option>ungraded</option>\n<option selected=\"on\">nothing</option>\n";
        !           141:       } elsif ($score =~ /^excused/) {
        !           142: 	$result.="<option>correct</option>\n<option>incorrect</option>\n<option selected=\"on\">excused</option>\n<option>ungraded</option>\n<option>nothing</option>\n";
        !           143:       } elsif ($score =~ /^ungraded/) {
        !           144: 	$result.="<option>correct</option>\n<option>incorrect</option>\n<option>excused</option>\n<option selected=\"on\">ungraded</option>\n<option>nothing</option>\n";
        !           145:       }
        !           146:       $result.="</select></td>\n";
        !           147:     }
        !           148:   }
        !           149:   $result.='</tr>';
        !           150:   return $result;
1.5       albertel  151: }
1.13    ! albertel  152: #FIXME need to look at the meatdata <stores> spec on what type of data to accept and provide an
1.6       albertel  153: #interface based on that, also do that to above function.
1.5       albertel  154: sub setstudentgrade {
1.13    ! albertel  155:   my ($url,$symb,$courseid,$student,@parts) = @_;
        !           156: 
        !           157:   my $result ='';
        !           158: 
        !           159:   my ($stuname,$domain) = split(/:/,$student);
        !           160: 
        !           161:   my %record=&Apache::lonnet::restore($symb,$courseid,$domain,$stuname);
        !           162: 
        !           163:   my %newrecord;
        !           164: 
        !           165:   foreach my $part (@parts) {
        !           166:     my ($temp,$part,$type)=split(/_/,$part);
        !           167:     my $oldscore=$record{"resource.$part.$type"};
        !           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 = '';
1.6       albertel  175: 	}
1.13    ! albertel  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; }
        !           187:     } else {
        !           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:       }
        !           194:     }
        !           195:   }
        !           196:   if ( scalar(keys(%newrecord)) > 0 ) {
        !           197:     $newrecord{"resource.regrader"}="$ENV{'user.name'}:$ENV{'user.domain'}";
        !           198:     &Apache::lonnet::cstore(\%newrecord,$symb,$courseid,$domain,$stuname);
        !           199: 
        !           200:     $result.="Stored away ".scalar(keys(%newrecord))." elements.<br />\n";
        !           201:   }
        !           202:   return $result;
1.2       albertel  203: }
                    204: 
                    205: sub submission {
1.13    ! albertel  206:   my ($request) = @_;
        !           207:   my $url=$ENV{'form.url'};
        !           208:   $url=~s-^http://($ENV{'SERVER_NAME'}|$ENV{'HTTP_HOST'})--;
        !           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.13    ! albertel  211:   my ($uname,$udom) = &finduser($ENV{'form.student'});
        !           212:   if ($uname eq '') { &moreinfo($request,"Unable to find student"); return ''; }
        !           213:   my $symb=&Apache::lonnet::symbread($url);
        !           214:   if ($symb eq '') { $request->print("Unable to handle ambiguous references:$url:."); return ''; }
        !           215:   my $answer=&Apache::loncommon::get_previous_attempt($symb,$uname,$udom,
        !           216: 						      $ENV{'request.course.id'});
        !           217:   my $result="<h2> Submission Record </h2>  $uname:$udom for $url <br />".$answer;
        !           218:   return $result;
1.2       albertel  219: }
                    220: 
1.5       albertel  221: sub viewgrades {
1.13    ! albertel  222:   my ($request) = @_;
        !           223:   my $result='';
1.5       albertel  224: 
1.13    ! albertel  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');
        !           237:   my $headerclr = '"#ccffff"';
        !           238:   my $cellclr = '"#ffffcc"';
        !           239: 
        !           240:   #get list of parts for this problem
        !           241:   my (@parts) = &getpartlist($url);
        !           242: 
        !           243:   $request->print ("<h2><font color=\"#339966\">Manual Grading</font></h2>");
        !           244: 
        !           245:   #start the form
        !           246:   $result = '<form action="/adm/grades" method="post">'."\n".
        !           247:     '<input type="hidden" name="symb" value="'.$symb.'"/>'."\n".
        !           248:       '<input type="hidden" name="url" value="'.$url.'"/>'."\n".
        !           249: 	'<input type="hidden" name="command" value="editgrades" />'."\n".
        !           250: 	  '<input type="submit" name="submit" value="Submit Changes" />'."\n".
        !           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";
        !           254:   foreach my $part (@parts) {
        !           255:      my $display=&Apache::lonnet::metadata($url,$part.'.display');
        !           256:      if  (!$display) { $display = &Apache::lonnet::metadata($url,$part.'.name'); }
        !           257:      $result.='<td bgcolor='.$headerclr.'>'.$display.'</td>'."\n";
        !           258:    }
        !           259:   $result.="</tr>";
        !           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:   }
        !           264:   $result.='</table></td></tr></table><input type="submit" name="submit" value="Submit Changes" /></form>';
1.5       albertel  265: 
1.13    ! albertel  266:   return $result;
1.5       albertel  267: }
                    268: 
                    269: sub editgrades {
1.13    ! albertel  270:   my ($request) = @_;
        !           271:   my $result='';
1.5       albertel  272: 
1.13    ! albertel  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".
        !           289: 	  '<input type="submit" name="submit" value="See Grades" /> <br />'."\n";
        !           290: 
        !           291:   foreach my $student ( sort(@{ $classlist{'allids'} }) ) {
        !           292:     $result.=&setstudentgrade($url,$symb,$ENV{'request.course.id'},$student,@parts);
        !           293:   }
1.5       albertel  294: 
1.13    ! albertel  295:   $result.='<input type="submit" name="submit" value="See Grades" /></table></form>';
        !           296:   return $result;
1.5       albertel  297: }
                    298: 
1.2       albertel  299: sub send_header {
1.13    ! albertel  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.13    ! albertel  307:   $request->print('<body bgcolor="#FFFFFF">');
1.2       albertel  308: }
                    309: 
                    310: sub send_footer {
1.13    ! albertel  311:   my ($request)= @_;
1.2       albertel  312:   $request->print('</body>');
                    313:   $request->print(&Apache::lontexconvert::footer());
                    314: }
                    315: 
1.1       albertel  316: sub handler {
1.13    ! albertel  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'};
        !           329:   my $symb=$ENV{'form.symb'};
        !           330:   my $command=$ENV{'form.command'};
        !           331:   
        !           332:   &send_header($request);
        !           333:   if ($url eq '' && $symb eq '') {
        !           334:     $request->print("Non-Contextual Access Unsupported:$command:$url:");
        !           335:   } else {
        !           336:     $Apache::grades::viewgrades=&Apache::lonnet::allowed('vgr',$ENV{'request.course.id'});
        !           337:     if ($command eq 'submission') {
        !           338:       $request->print(&listStudents($request)) if ($ENV{'form.student'} eq '');
        !           339:       $request->print(&submission($request)) if ($ENV{'form.student'} ne '');
        !           340:     } elsif ($command eq 'viewgrades') {
        !           341:       $request->print(&viewgrades($request));
        !           342:     } elsif ($command eq 'editgrades') {
        !           343:       $request->print(&editgrades($request));
1.12      harris41  344:     } else {
1.13    ! albertel  345:       $request->print("Unknown action:$command:");
1.2       albertel  346:     }
1.13    ! albertel  347:   }
        !           348:   &send_footer($request);
        !           349:   return OK;
1.1       albertel  350: }
                    351: 
                    352: 1;
                    353: 
1.13    ! albertel  354: __END__;

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