Annotation of loncom/homework/grades.pm, revision 1.22
1.17 albertel 1: # The LearningOnline Network with CAPA
1.13 albertel 2: # The LON-CAPA Grading handler
1.17 albertel 3: #
1.22 ! albertel 4: # $Id: grades.pm,v 1.21 2002/04/09 04:33:10 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.21 albertel 82: $request->print("\n".'<tr>'."<td>$sname</td><td>$sdom</td><td>@nameparts</td><td>".
83: '<form action="/adm/grades" method="post">');
1.16 albertel 84: if ($ENV{'form.url'}) {
1.19 www 85: $request->print(
86: '<input type="hidden" name="url" value="'.$ENV{'form.url'}.'" />');
1.16 albertel 87: }
88: if ($ENV{'form.symb'}) {
1.19 www 89: $request->print(
90: '<input type="hidden" name="symb" value="'.$ENV{'form.symb'}.'" />');
1.16 albertel 91: }
1.19 www 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" />');
1.21 albertel 100: $request->print('</form></td></tr>');
1.13 albertel 101: }
102: }
1.19 www 103: $request->print('</table>');
1.10 ng 104: }
105:
1.13 albertel 106:
1.7 albertel 107: #FIXME - needs to handle multiple matches
1.2 albertel 108: sub finduser {
1.13 albertel 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; }
1.7 albertel 121: }
1.13 albertel 122: return ($name,$domain);
123: } else {
124: return ($ENV{'user.name'},$ENV{'user.domain'});
125: }
1.5 albertel 126: }
127:
128: sub getclasslist {
1.13 albertel 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)) {
1.15 albertel 138: #print "Skipping:$name:$end:$now<br />\n";
1.13 albertel 139: next;
140: }
1.15 albertel 141: #print "record=$record<br>";
1.13 albertel 142: push( @{ $classlist{'allids'} }, $name);
143: }
144: return (%classlist);
1.5 albertel 145: }
146:
147: sub getpartlist {
1.13 albertel 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);
1.6 albertel 154: }
1.13 albertel 155: }
156: return @parts;
1.5 albertel 157: }
158:
159: sub viewstudentgrade {
1.13 albertel 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;
1.5 albertel 196: }
1.13 albertel 197: #FIXME need to look at the meatdata <stores> spec on what type of data to accept and provide an
1.6 albertel 198: #interface based on that, also do that to above function.
1.5 albertel 199: sub setstudentgrade {
1.13 albertel 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 = '';
1.6 albertel 220: }
1.13 albertel 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;
1.2 albertel 248: }
249:
250: sub submission {
1.13 albertel 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 ''; }
1.10 ng 255: # if ($ENV{'form.student'} eq '') { &listStudents($request); return ''; }
1.13 albertel 256: my ($uname,$udom) = &finduser($ENV{'form.student'});
257: if ($uname eq '') { &moreinfo($request,"Unable to find student"); return ''; }
1.16 albertel 258: my $symb;
259: if ($ENV{'form.symb'}) {
260: $symb=$ENV{'form.symb'};
261: } else {
262: $symb=&Apache::lonnet::symbread($url);
263: }
1.13 albertel 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;
1.17 albertel 268: my $rendered=&Apache::loncommon::get_student_view($symb,$uname,$udom,
269: $ENV{'request.course.id'});
1.22 ! albertel 270: $result.="Student's view of the problem:<br /> $rendered <br /> Correct answer:<br />";
1.18 albertel 271:
1.22 ! albertel 272: $answer=&Apache::loncommon::get_student_answers($symb,$uname,$udom,
1.18 albertel 273: $ENV{'request.course.id'});
274: $result.=$answer;
1.13 albertel 275: return $result;
1.2 albertel 276: }
277:
1.5 albertel 278: sub viewgrades {
1.13 albertel 279: my ($request) = @_;
280: my $result='';
1.5 albertel 281:
1.13 albertel 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".
1.16 albertel 304: '<input type="hidden" name="symb" value="'.$symb.'" />'."\n".
305: '<input type="hidden" name="url" value="'.$url.'" />'."\n".
1.13 albertel 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>';
1.5 albertel 322:
1.13 albertel 323: return $result;
1.5 albertel 324: }
325:
326: sub editgrades {
1.13 albertel 327: my ($request) = @_;
328: my $result='';
1.5 albertel 329:
1.13 albertel 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: }
1.5 albertel 351:
1.13 albertel 352: $result.='<input type="submit" name="submit" value="See Grades" /></table></form>';
353: return $result;
1.5 albertel 354: }
355:
1.2 albertel 356: sub send_header {
1.13 albertel 357: my ($request)= @_;
358: $request->print(&Apache::lontexconvert::header());
1.6 albertel 359: # $request->print("
360: #<script>
361: #remotewindow=open('','homeworkremote');
362: #remotewindow.close();
363: #</script>");
1.13 albertel 364: $request->print('<body bgcolor="#FFFFFF">');
1.2 albertel 365: }
366:
367: sub send_footer {
1.13 albertel 368: my ($request)= @_;
1.2 albertel 369: $request->print('</body>');
370: $request->print(&Apache::lontexconvert::footer());
371: }
372:
1.1 albertel 373: sub handler {
1.13 albertel 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;
1.16 albertel 385: &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'});
1.13 albertel 386: my $url=$ENV{'form.url'};
387: my $symb=$ENV{'form.symb'};
388: my $command=$ENV{'form.command'};
1.16 albertel 389: if (!$url) {
390: my ($temp1,$temp2);
391: ($temp1,$temp2,$ENV{'form.url'})=split(/___/,$symb);
392: $url = $ENV{'form.url'};
393: }
1.13 albertel 394: &send_header($request);
395: if ($url eq '' && $symb eq '') {
1.14 www 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: }
1.13 albertel 422: } else {
423: $Apache::grades::viewgrades=&Apache::lonnet::allowed('vgr',$ENV{'request.course.id'});
424: if ($command eq 'submission') {
1.20 albertel 425: &listStudents($request) if ($ENV{'form.student'} eq '');
1.13 albertel 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));
1.12 harris41 431: } else {
1.13 albertel 432: $request->print("Unknown action:$command:");
1.2 albertel 433: }
1.13 albertel 434: }
435: &send_footer($request);
436: return OK;
1.1 albertel 437: }
438:
439: 1;
440:
1.13 albertel 441: __END__;
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>