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