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