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