Annotation of loncom/homework/grades.pm, revision 1.17
1.17 ! albertel 1: # The LearningOnline Network with CAPA
1.13 albertel 2: # The LON-CAPA Grading handler
1.17 ! albertel 3: #
! 4: # $Id: gplheader.pl,v 1.1 2001/11/29 18:19:27 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: #
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.13 albertel 263: return $result;
1.2 albertel 264: }
265:
1.5 albertel 266: sub viewgrades {
1.13 albertel 267: my ($request) = @_;
268: my $result='';
1.5 albertel 269:
1.13 albertel 270: #get resource reference
271: my $url=$ENV{'form.url'};
272: $url=~s-^http://($ENV{'SERVER_NAME'}|$ENV{'HTTP_HOST'})--;
273: my $symb=$ENV{'form.symb'};
274: if (!$symb) { $symb=&Apache::lonnet::symbread($url); }
275: if ($symb eq '') { $request->print("Unable to handle ambiguous references:$url:."); return ''; }
276:
277: #get classlist
278: my ($cdom,$cnum) = split(/_/,$ENV{'request.course.id'});
279: my $chome=$ENV{"course.$ENV{'request.course.id'}.home"};
280: #print "Found $cdom:$cnum:$chome<br />";
281: my (%classlist) = &getclasslist($cdom,$cnum,$chome,'0');
282: my $headerclr = '"#ccffff"';
283: my $cellclr = '"#ffffcc"';
284:
285: #get list of parts for this problem
286: my (@parts) = &getpartlist($url);
287:
288: $request->print ("<h2><font color=\"#339966\">Manual Grading</font></h2>");
289:
290: #start the form
291: $result = '<form action="/adm/grades" method="post">'."\n".
1.16 albertel 292: '<input type="hidden" name="symb" value="'.$symb.'" />'."\n".
293: '<input type="hidden" name="url" value="'.$url.'" />'."\n".
1.13 albertel 294: '<input type="hidden" name="command" value="editgrades" />'."\n".
295: '<input type="submit" name="submit" value="Submit Changes" />'."\n".
296: '<table border=0><tr><td bgcolor="#999999">'."\n".
297: '<table border=0>'."\n".
298: '<tr><td bgcolor='.$headerclr.'>UserId</td><td bgcolor='.$headerclr.'>Domain</td>'."\n";
299: foreach my $part (@parts) {
300: my $display=&Apache::lonnet::metadata($url,$part.'.display');
301: if (!$display) { $display = &Apache::lonnet::metadata($url,$part.'.name'); }
302: $result.='<td bgcolor='.$headerclr.'>'.$display.'</td>'."\n";
303: }
304: $result.="</tr>";
305: #get info for each student
306: foreach my $student ( sort(@{ $classlist{'allids'} }) ) {
307: $result.=&viewstudentgrade($url,$symb,$ENV{'request.course.id'},$student,@parts);
308: }
309: $result.='</table></td></tr></table><input type="submit" name="submit" value="Submit Changes" /></form>';
1.5 albertel 310:
1.13 albertel 311: return $result;
1.5 albertel 312: }
313:
314: sub editgrades {
1.13 albertel 315: my ($request) = @_;
316: my $result='';
1.5 albertel 317:
1.13 albertel 318: my $symb=$ENV{'form.symb'};
319: if ($symb eq '') { $request->print("Unable to handle ambiguous references:$symb:$ENV{'form.url'}"); return ''; }
320: my $url=$ENV{'form.url'};
321: #get classlist
322: my ($cdom,$cnum) = split(/_/,$ENV{'request.course.id'});
323: my $chome=$ENV{"course.$ENV{'request.course.id'}.home"};
324: #print "Found $cdom:$cnum:$chome<br />";
325: my (%classlist) = &getclasslist($cdom,$cnum,$chome,'0');
326:
327: #get list of parts for this problem
328: my (@parts) = &getpartlist($url);
329:
330: $result.='<form action="/adm/grades" method="post">'."\n".
331: '<input type="hidden" name="symb" value="'.$symb.'" />'."\n".
332: '<input type="hidden" name="url" value="'.$url.'" />'."\n".
333: '<input type="hidden" name="command" value="viewgrades" />'."\n".
334: '<input type="submit" name="submit" value="See Grades" /> <br />'."\n";
335:
336: foreach my $student ( sort(@{ $classlist{'allids'} }) ) {
337: $result.=&setstudentgrade($url,$symb,$ENV{'request.course.id'},$student,@parts);
338: }
1.5 albertel 339:
1.13 albertel 340: $result.='<input type="submit" name="submit" value="See Grades" /></table></form>';
341: return $result;
1.5 albertel 342: }
343:
1.2 albertel 344: sub send_header {
1.13 albertel 345: my ($request)= @_;
346: $request->print(&Apache::lontexconvert::header());
1.6 albertel 347: # $request->print("
348: #<script>
349: #remotewindow=open('','homeworkremote');
350: #remotewindow.close();
351: #</script>");
1.13 albertel 352: $request->print('<body bgcolor="#FFFFFF">');
1.2 albertel 353: }
354:
355: sub send_footer {
1.13 albertel 356: my ($request)= @_;
1.2 albertel 357: $request->print('</body>');
358: $request->print(&Apache::lontexconvert::footer());
359: }
360:
1.1 albertel 361: sub handler {
1.13 albertel 362: my $request=$_[0];
363:
364: if ( $ENV{'user.name'} eq 'albertel' ) {$Apache::lonxml::debug=1;} else {$Apache::lonxml::debug=0;}
365:
366: if ($ENV{'browser.mathml'}) {
367: $request->content_type('text/xml');
368: } else {
369: $request->content_type('text/html');
370: }
371: $request->send_http_header;
372: return OK if $request->header_only;
1.16 albertel 373: &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'});
1.13 albertel 374: my $url=$ENV{'form.url'};
375: my $symb=$ENV{'form.symb'};
376: my $command=$ENV{'form.command'};
1.16 albertel 377: if (!$url) {
378: my ($temp1,$temp2);
379: ($temp1,$temp2,$ENV{'form.url'})=split(/___/,$symb);
380: $url = $ENV{'form.url'};
381: }
1.13 albertel 382: &send_header($request);
383: if ($url eq '' && $symb eq '') {
1.14 www 384: if ($ENV{'user.adv'}) {
385: if (($ENV{'form.codeone'}) && ($ENV{'form.codetwo'}) &&
386: ($ENV{'form.codethree'})) {
387: my $token=$ENV{'form.codeone'}.'*'.$ENV{'form.codetwo'}.'*'.
388: $ENV{'form.codethree'};
389: my ($tsymb,$tuname,$tudom,$tcrsid)=
390: &Apache::lonnet::checkin($token);
391: if ($tsymb) {
392: my ($map,$id,$url)=split(/\_\_\_/,$tsymb);
393: if (&Apache::lonnet::allowed('mgr',$tcrsid)) {
394: $request->print(
395: &Apache::lonnet::ssi('/res/'.$url,
396: ('grade_username' => $tuname,
397: 'grade_domain' => $tudom,
398: 'grade_courseid' => $tcrsid,
399: 'grade_symb' => $tsymb)));
400: } else {
401: $request->print('<h1>Not authorized: '.$token.'</h1>');
402: }
403: } else {
404: $request->print('<h1>Not a valid DocID: '.$token.'</h1>');
405: }
406: } else {
407: $request->print(&Apache::lonxml::tokeninputfield());
408: }
409: }
1.13 albertel 410: } else {
411: $Apache::grades::viewgrades=&Apache::lonnet::allowed('vgr',$ENV{'request.course.id'});
412: if ($command eq 'submission') {
413: $request->print(&listStudents($request)) if ($ENV{'form.student'} eq '');
414: $request->print(&submission($request)) if ($ENV{'form.student'} ne '');
415: } elsif ($command eq 'viewgrades') {
416: $request->print(&viewgrades($request));
417: } elsif ($command eq 'editgrades') {
418: $request->print(&editgrades($request));
1.12 harris41 419: } else {
1.13 albertel 420: $request->print("Unknown action:$command:");
1.2 albertel 421: }
1.13 albertel 422: }
423: &send_footer($request);
424: return OK;
1.1 albertel 425: }
426:
427: 1;
428:
1.13 albertel 429: __END__;
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>