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